Skip to content

Commit

Permalink
chore(ui): rename functions, DRYer
Browse files Browse the repository at this point in the history
  • Loading branch information
ramenuncle committed Oct 25, 2022
1 parent ded6b6a commit fa7bfd2
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ const buildClaimTokenOnSolanaStep = (
interactionStatus: InteractionStatusV2,
): EuiStepProps => {
const {
verifySignatureTxId,
verifySignaturesTxId,
postVaaOnSolanaTxId,
completeNativeWithPayloadTxId,
processSwimPayloadTxId,
Expand All @@ -338,7 +338,7 @@ const buildClaimTokenOnSolanaStep = (
isLoading={status === "loading"}
tokenConfig={toTokenData.tokenConfig}
transactions={[
verifySignatureTxId,
verifySignaturesTxId,
postVaaOnSolanaTxId,
completeNativeWithPayloadTxId,
processSwimPayloadTxId,
Expand Down
4 changes: 2 additions & 2 deletions apps/ui/src/fixtures/swim/interactionStateV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ export const CROSS_CHAIN_EVM_TO_SOLANA_SWAP_INTERACTION_STATE_INIT: CrossChainEv
approvalTxIds: [],
crossChainInitiateTxId: null,
auxiliarySignerPublicKey: null,
verifySignatureTxId: null,
verifySignaturesTxId: null,
postVaaOnSolanaTxId: null,
completeNativeWithPayloadTxId: null,
processSwimPayloadTxId: null,
Expand Down Expand Up @@ -399,7 +399,7 @@ export const CROSS_CHAIN_EVM_TO_SOLANA_SWAP_INTERACTION_STATE_SWAP_AND_TRANSFER_
export const CROSS_CHAIN_EVM_TO_SOLANA_SWAP_INTERACTION_STATE_POST_VAA_COMPLETED: CrossChainEvmToSolanaSwapInteractionState =
{
...CROSS_CHAIN_EVM_TO_SOLANA_SWAP_INTERACTION_STATE_SWAP_AND_TRANSFER_COMPLETED,
verifySignatureTxId:
verifySignaturesTxId:
"53r98E5EiffkmJ6WVA2VKmq78LVCT4zcRVxo76EWoUFiNpdxbno7UVeUT6oQgsVM3xeU99mQmnUjFVscz7PC1gK8",
postVaaOnSolanaTxId:
"53r98E5EiffkmJ6WVA2VKmq78LVCT4zcRVxo76EWoUFiNpdxbno7UVeUT6oQgsVM3xeU99mQmnUjFVscz7PC1gK9",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ Object {
},
},
"swapType": "CrossChainEvmToSolana",
"verifySignatureTxId": null,
"verifySignaturesTxId": null,
"version": 2,
}
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ const createSwapInteractionState = (
approvalTxIds: [],
crossChainInitiateTxId: null,
auxiliarySignerPublicKey: null,
verifySignatureTxId: null,
verifySignaturesTxId: null,
postVaaOnSolanaTxId: null,
completeNativeWithPayloadTxId: null,
processSwimPayloadTxId: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export const useCrossChainEvmToSolanaSwapInteractionMutation = () => {

switch (result.type) {
case SolanaTxType.WormholeVerifySignatures:
draft.verifySignatureTxId = result.tx.id;
draft.verifySignaturesTxId = result.tx.id;
break;
case SolanaTxType.WormholePostVaa:
draft.postVaaOnSolanaTxId = result.tx.id;
Expand Down
2 changes: 1 addition & 1 deletion apps/ui/src/models/swim/interactionStateV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export interface CrossChainEvmToSolanaSwapInteractionState {
readonly approvalTxIds: readonly EvmTx["id"][];
readonly crossChainInitiateTxId: EvmTx["id"] | null;
readonly auxiliarySignerPublicKey: string | null;
readonly verifySignatureTxId: SolanaTx["id"] | null;
readonly verifySignaturesTxId: SolanaTx["id"] | null;
readonly postVaaOnSolanaTxId: SolanaTx["id"] | null;
readonly completeNativeWithPayloadTxId: SolanaTx["id"] | null;
readonly processSwimPayloadTxId: SolanaTx["id"] | null;
Expand Down
104 changes: 30 additions & 74 deletions packages/solana/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,14 @@ import { Client, getTokenDetails } from "@swim-io/core";
import type { Propeller } from "@swim-io/solana-contracts";
import { idl } from "@swim-io/solana-contracts";
import { TokenProjectId } from "@swim-io/token-projects";
import type { ReadonlyRecord } from "@swim-io/utils";
import { atomicToHuman, chunks, humanToAtomic, sleep } from "@swim-io/utils";
import BN from "bn.js";
import Decimal from "decimal.js";

import {
createCompleteNativeWithPayloadAccounts,
createProcessSwimPayloadAccounts,
getProcessSwimPayloadAccounts,
getAddAccounts,
getCompleteNativeWithPayloadAccounts,
getPropellerTransferAccounts,
} from "./getAccounts";
import type {
Expand All @@ -54,6 +53,8 @@ import type {
import { SOLANA_ECOSYSTEM_ID, SolanaTxType } from "./protocol";
import type { TokenAccount } from "./serialization";
import { deserializeTokenAccount } from "./serialization";
import type { SupportedTokenProjectId } from "./supportedTokenProjectIds";
import { isSupportedTokenProjectId } from "./supportedTokenProjectIds";
import {
createApproveAndRevokeIxs,
createTx,
Expand Down Expand Up @@ -85,21 +86,6 @@ interface GenerateVerifySignaturesTxsParams
readonly auxiliarySigner: Keypair;
}

type SupportedTokenProjectId =
| TokenProjectId.SwimUsd
| TokenProjectId.Usdc
| TokenProjectId.Usdt;

const SUPPORTED_TOKEN_PROJECT_IDS = [
TokenProjectId.SwimUsd,
TokenProjectId.Usdc,
TokenProjectId.Usdt,
];

const isSupportedTokenProjectId = (
id: TokenProjectId,
): id is SupportedTokenProjectId => SUPPORTED_TOKEN_PROJECT_IDS.includes(id);

interface PropellerAddParams {
readonly wallet: SolanaWalletAdapter;
readonly routingContract: Program<Propeller>;
Expand Down Expand Up @@ -392,18 +378,13 @@ export class SolanaClient extends Client<
> {
const walletPublicKey = wallet.publicKey;
if (walletPublicKey === null) {
throw new Error("Missing Solana wallet");
throw new Error("Missing Solana wallet public key");
}
const routingContract = this.getRoutingContract(wallet);
const swimUsdAtaPublicKey = getAssociatedTokenAddressSync(
new PublicKey(this.chainConfig.swimUsdDetails.address),
walletPublicKey,
);
const accounts = await createCompleteNativeWithPayloadAccounts(
const accounts = await getCompleteNativeWithPayloadAccounts(
this.chainConfig,
new PublicKey(walletPublicKey),
walletPublicKey,
signedVaa,
swimUsdAtaPublicKey,
sourceWormholeChainId,
sourceChainConfig,
);
Expand Down Expand Up @@ -434,13 +415,13 @@ export class SolanaClient extends Client<
interactionId,
signedVaa,
targetTokenNumber,
minOutputAmount,
minimumOutputAmount,
}: {
readonly wallet: SolanaWalletAdapter;
readonly interactionId: string;
readonly signedVaa: Buffer;
readonly targetTokenNumber: number;
readonly minOutputAmount: string;
readonly minimumOutputAmount: string;
}): AsyncGenerator<
TxGeneratorResult<ParsedTransactionWithMeta, SolanaTx, SolanaTxType>,
any,
Expand All @@ -449,43 +430,25 @@ export class SolanaClient extends Client<
const [twoPoolConfig] = this.chainConfig.pools;
const walletPublicKey = wallet.publicKey;
if (walletPublicKey === null) {
throw new Error("Missing Solana wallet");
throw new Error("Missing Solana wallet public key");
}
const routingContract = this.getRoutingContract(wallet);
const poolTokenAccounts = [...twoPoolConfig.tokenAccounts.values()].map(
(address) => new PublicKey(address),
);
const userTokenAccounts = SUPPORTED_TOKEN_PROJECT_IDS.reduce(
(accumulator, tokenProjectId) => {
const { address } = getTokenDetails(this.chainConfig, tokenProjectId);
return {
...accumulator,
[tokenProjectId]: getAssociatedTokenAddressSync(
new PublicKey(address),
walletPublicKey,
),
};
},
{} as ReadonlyRecord<SupportedTokenProjectId, PublicKey>,
);
const accounts = await createProcessSwimPayloadAccounts(
const poolTokenAccountPublicKeys = [
...twoPoolConfig.tokenAccounts.values(),
].map((address) => new PublicKey(address));
const accounts = await getProcessSwimPayloadAccounts(
this.chainConfig,
new PublicKey(walletPublicKey),
signedVaa,
userTokenAccounts[TokenProjectId.SwimUsd],
[
userTokenAccounts[TokenProjectId.Usdc],
userTokenAccounts[TokenProjectId.Usdt],
],
poolTokenAccounts,
poolTokenAccountPublicKeys,
new PublicKey(twoPoolConfig.governanceFeeAccount),
targetTokenNumber,
);
const setComputeUnitLimitIx = ComputeBudgetProgram.setComputeUnitLimit({
units: 900_000,
});
const txRequest = await routingContract.methods
.processSwimPayload(targetTokenNumber, new BN(minOutputAmount))
.processSwimPayload(targetTokenNumber, new BN(minimumOutputAmount))
.accounts(accounts)
.preInstructions([setComputeUnitLimitIx])
.postInstructions([createMemoInstruction(interactionId)])
Expand Down Expand Up @@ -957,43 +920,36 @@ export class SolanaClient extends Client<
inputAmountAtomic,
auxiliarySigner = Keypair.generate(),
}: WithOptionalAuxiliarySigner<PropellerAddParams>): Promise<SolanaTx> {
const walletPublicKey = wallet.publicKey;
if (walletPublicKey === null) {
throw new Error("Missing Solana wallet public key");
}
const [twoPoolConfig] = this.chainConfig.pools;
const addInputAmounts =
sourceTokenId === TokenProjectId.Usdc
? [inputAmountAtomic, "0"]
: ["0", inputAmountAtomic];
const addMaxFee = "0"; // TODO: Change to a real value

const userTokenAccounts = SUPPORTED_TOKEN_PROJECT_IDS.reduce(
(accumulator, tokenProjectId) => {
const { address } = getTokenDetails(this.chainConfig, tokenProjectId);
return {
...accumulator,
[tokenProjectId]: getAssociatedTokenAddressSync(
new PublicKey(address),
senderPublicKey,
),
};
},
{} as ReadonlyRecord<SupportedTokenProjectId, PublicKey>,
);
const addAccounts = getAddAccounts(
this.chainConfig,
userTokenAccounts[TokenProjectId.SwimUsd],
[
userTokenAccounts[TokenProjectId.Usdc],
userTokenAccounts[TokenProjectId.Usdt],
],
walletPublicKey,
auxiliarySigner.publicKey,
new PublicKey(this.chainConfig.swimUsdDetails.address),
[...twoPoolConfig.tokenAccounts.values()].map(
(address) => new PublicKey(address),
),
new PublicKey(twoPoolConfig.governanceFeeAccount),
);

const sourceTokenMint = getTokenDetails(
this.chainConfig,
sourceTokenId,
).address;
const sourceTokenAccountPublicKey = getAssociatedTokenAddressSync(
new PublicKey(sourceTokenMint),
senderPublicKey,
);
const [approveIx, revokeIx] = await createApproveAndRevokeIxs(
userTokenAccounts[sourceTokenId],
sourceTokenAccountPublicKey,
inputAmountAtomic,
auxiliarySigner.publicKey,
senderPublicKey,
Expand Down
Loading

0 comments on commit fa7bfd2

Please sign in to comment.