Skip to content

Commit

Permalink
refactor(ui): Update Wormhole form/hooks for SDK client changes
Browse files Browse the repository at this point in the history
  • Loading branch information
wormat committed Oct 20, 2022
1 parent b735149 commit dfdb38e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 91 deletions.
8 changes: 6 additions & 2 deletions apps/ui/src/components/WormholeForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ import type { UseQueryResult } from "react-query";
import { useQuery } from "react-query";

import { wormholeTokens as rawWormholeTokens } from "../config";
import { useEvmWallet, useSplUserBalance, useWormholeTransfer } from "../hooks";
import {
useEvmWallet,
useUserSolanaTokenBalance,
useWormholeTransfer,
} from "../hooks";
import type { TxResult, WormholeToken, WormholeTokenDetails } from "../models";
import { generateId } from "../models";

Expand Down Expand Up @@ -157,7 +161,7 @@ export const WormholeForm = (): ReactElement => {

const sourceDetails = getDetailsByChainId(currentToken, sourceChainId);
const targetDetails = getDetailsByChainId(currentToken, targetChainId);
const splBalance = useSplUserBalance(
const splBalance = useUserSolanaTokenBalance(
sourceChainId === CHAIN_ID_SOLANA ? sourceDetails : null,
{ enabled: sourceChainId === CHAIN_ID_SOLANA },
);
Expand Down
66 changes: 32 additions & 34 deletions apps/ui/src/hooks/wormhole/useTransferEvmToEvmMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import {
getEmitterAddressEth,
parseSequenceFromLogEth,
} from "@certusone/wormhole-sdk";
import { isEvmEcosystemId } from "@swim-io/evm";
import type { EvmTx } from "@swim-io/evm";
import { EvmTxType, isEvmEcosystemId } from "@swim-io/evm";
import { findOrThrow, humanToAtomic } from "@swim-io/utils";
import { useMutation } from "react-query";
import shallow from "zustand/shallow.js";
Expand Down Expand Up @@ -77,34 +78,35 @@ export const useTransferEvmToEvmMutation = () => {

await evmWallet.switchNetwork(sourceChain.chainId);
// Process transfer if transfer txId does not exist
const { approvalResponses, transferResponse } =
await sourceClient.initiateWormholeTransfer({
atomicAmount: humanToAtomic(value, sourceDetails.decimals).toString(),
interactionId,
sourceAddress: sourceDetails.address,
targetAddress: formatWormholeAddress(Protocol.Evm, evmWalletAddress),
targetChainId: targetDetails.chainId,
wallet: evmWallet,
wrappedTokenInfo: getWrappedTokenInfoFromNativeDetails(
sourceDetails.chainId,
nativeDetails,
),
});
const sourceTxGenerator = sourceClient.generateInitiatePortalTransferTxs({
atomicAmount: humanToAtomic(value, sourceDetails.decimals).toString(),
interactionId,
sourceAddress: sourceDetails.address,
targetAddress: formatWormholeAddress(Protocol.Evm, evmWalletAddress),
targetChainId: targetDetails.chainId,
wallet: evmWallet,
wrappedTokenInfo: getWrappedTokenInfoFromNativeDetails(
sourceDetails.chainId,
nativeDetails,
),
});

approvalResponses.forEach((response) =>
let transferTx: EvmTx | null = null;
for await (const result of sourceTxGenerator) {
if (result.type === EvmTxType.PortalTransferTokens) {
transferTx = result.tx;
}
onTxResult({
chainId: sourceDetails.chainId,
txId: response.hash,
}),
);
txId: result.tx.id,
});
}

const transferTx = await sourceClient.getTx(transferResponse);
onTxResult({
chainId: sourceDetails.chainId,
txId: transferTx.id,
});
if (transferTx === null) {
throw new Error("Missing source transaction");
}
const sequence = parseSequenceFromLogEth(
transferTx.receipt,
transferTx.original,
sourceChain.wormhole.bridge,
);
const retries = getWormholeRetries(sourceDetails.chainId);
Expand All @@ -119,21 +121,17 @@ export const useTransferEvmToEvmMutation = () => {
);

await evmWallet.switchNetwork(targetChain.chainId);
const redeemResponse = await targetClient.completeWormholeTransfer({
const targetTxGenerator = targetClient.generateCompletePortalTransferTxs({
interactionId,
vaa,
wallet: evmWallet,
});
if (redeemResponse === null) {
throw new Error(
`Transaction not found: (unlock/mint on ${targetEcosystemId})`,
);
for await (const result of targetTxGenerator) {
onTxResult({
chainId: targetDetails.chainId,
txId: result.tx.id,
});
}
const redeemTx = await targetClient.getTx(redeemResponse);
onTxResult({
chainId: targetDetails.chainId,
txId: redeemTx.id,
});
},
);
};
78 changes: 40 additions & 38 deletions apps/ui/src/hooks/wormhole/useTransferEvmToSolanaMutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
parseSequenceFromLogEth,
} from "@certusone/wormhole-sdk";
import { Keypair } from "@solana/web3.js";
import { isEvmEcosystemId } from "@swim-io/evm";
import type { EvmTx } from "@swim-io/evm";
import { EvmTxType, isEvmEcosystemId } from "@swim-io/evm";
import { SOLANA_ECOSYSTEM_ID, solana } from "@swim-io/solana";
import { findOrThrow, humanToAtomic } from "@swim-io/utils";
import { WormholeChainId } from "@swim-io/wormhole";
Expand All @@ -22,7 +23,7 @@ import {
} from "../../models";
import { useWallets } from "../crossEcosystem";
import { useGetEvmClient } from "../evm";
import { useSolanaClient, useSplTokenAccountsQuery } from "../solana";
import { useSolanaClient, useUserSolanaTokenAccountsQuery } from "../solana";

export const useTransferEvmToSolanaMutation = () => {
const queryClient = useQueryClient();
Expand All @@ -32,7 +33,7 @@ export const useTransferEvmToSolanaMutation = () => {
const solanaClient = useSolanaClient();
const wallets = useWallets();
const solanaWallet = wallets[SOLANA_ECOSYSTEM_ID].wallet;
const { data: splTokenAccounts = [] } = useSplTokenAccountsQuery();
const { data: splTokenAccounts = [] } = useUserSolanaTokenAccountsQuery();

return useMutation(
async ({
Expand Down Expand Up @@ -89,37 +90,39 @@ export const useTransferEvmToSolanaMutation = () => {

await evmWallet.switchNetwork(evmChain.chainId);
// Process transfer if transfer txId does not exist
const { approvalResponses, transferResponse } =
await evmClient.initiateWormholeTransfer({
atomicAmount: humanToAtomic(value, sourceDetails.decimals).toString(),
interactionId,
sourceAddress: sourceDetails.address,
targetAddress: formatWormholeAddress(
Protocol.Solana,
splTokenAccountAddress,
),
targetChainId: solana.wormholeChainId,
wallet: evmWallet,
wrappedTokenInfo: getWrappedTokenInfoFromNativeDetails(
sourceDetails.chainId,
nativeDetails,
),
});
const evmTxGenerator = evmClient.generateInitiatePortalTransferTxs({
atomicAmount: humanToAtomic(value, sourceDetails.decimals).toString(),
interactionId,
sourceAddress: sourceDetails.address,
targetAddress: formatWormholeAddress(
Protocol.Solana,
splTokenAccountAddress,
),
targetChainId: solana.wormholeChainId,
wallet: evmWallet,
wrappedTokenInfo: getWrappedTokenInfoFromNativeDetails(
sourceDetails.chainId,
nativeDetails,
),
});

approvalResponses.forEach((response) =>
let evmTransferTx: EvmTx | null = null;
for await (const result of evmTxGenerator) {
if (result.type === EvmTxType.PortalTransferTokens) {
evmTransferTx = result.tx;
}
onTxResult({
chainId: sourceDetails.chainId,
txId: response.hash,
}),
);
txId: result.tx.id,
});
}

if (evmTransferTx === null) {
throw new Error("Missing EVM transaction");
}

const transferTx = await evmClient.getTx(transferResponse);
onTxResult({
chainId: sourceDetails.chainId,
txId: transferTx.id,
});
const sequence = parseSequenceFromLogEth(
transferTx.receipt,
evmTransferTx.original,
evmChain.wormhole.bridge,
);

Expand All @@ -134,18 +137,17 @@ export const useTransferEvmToSolanaMutation = () => {
undefined,
retries,
);
const unlockSplTokenTxIdsGenerator =
solanaClient.generateCompleteWormholeTransferTxIds({
interactionId,
vaa,
wallet: solanaWallet,
auxiliarySigner,
});
const splTxGenerator = solanaClient.generateCompletePortalTransferTxs({
interactionId,
vaa,
wallet: solanaWallet,
auxiliarySigner,
});

for await (const txId of unlockSplTokenTxIdsGenerator) {
for await (const result of splTxGenerator) {
onTxResult({
chainId: targetDetails.chainId,
txId,
txId: result.tx.id,
});
}
},
Expand Down
39 changes: 22 additions & 17 deletions apps/ui/src/hooks/wormhole/useTransferSolanaToEvmMutation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getEmitterAddressSolana } from "@certusone/wormhole-sdk";
import { Keypair } from "@solana/web3.js";
import { isEvmEcosystemId } from "@swim-io/evm";
import type { SolanaTx } from "@swim-io/solana";
import {
SOLANA_ECOSYSTEM_ID,
parseSequenceFromLogSolana,
Expand Down Expand Up @@ -74,7 +75,7 @@ export const useTransferSolanaToEvmMutation = () => {
}

const auxiliarySigner = Keypair.generate();
const transferSplTokenTxId = await solanaClient.initiateWormholeTransfer({
const solanaTxGenerator = solanaClient.generateInitiatePortalTransferTxs({
atomicAmount: humanToAtomic(value, sourceDetails.decimals).toString(),
interactionId,
sourceAddress: sourceDetails.address,
Expand All @@ -87,13 +88,20 @@ export const useTransferSolanaToEvmMutation = () => {
nativeDetails,
),
});
onTxResult({
chainId: sourceDetails.chainId,
txId: transferSplTokenTxId,
});

const solanaTx = await solanaClient.getTx(transferSplTokenTxId);
const sequence = parseSequenceFromLogSolana(solanaTx.parsedTx);
let solanaTx: SolanaTx | null = null;
for await (const result of solanaTxGenerator) {
solanaTx = result.tx;
onTxResult({
chainId: sourceDetails.chainId,
txId: result.tx.id,
});
}
if (solanaTx === null) {
throw new Error("Missing Solana transaction");
}

const sequence = parseSequenceFromLogSolana(solanaTx.original);
const emitterAddress = await getEmitterAddressSolana(
solanaChain.wormhole.portal,
);
Expand All @@ -108,21 +116,18 @@ export const useTransferSolanaToEvmMutation = () => {
retries,
);
await evmWallet.switchNetwork(evmChain.chainId);
const redeemResponse = await evmClient.completeWormholeTransfer({
const evmTxGenerator = evmClient.generateCompletePortalTransferTxs({
interactionId,
vaa,
wallet: evmWallet,
});
if (redeemResponse === null) {
throw new Error(
`Transaction not found: (unlock/mint on ${evmChain.ecosystem})`,
);

for await (const result of evmTxGenerator) {
onTxResult({
chainId: targetDetails.chainId,
txId: result.tx.id,
});
}
const evmTx = await evmClient.getTx(redeemResponse);
onTxResult({
chainId: targetDetails.chainId,
txId: evmTx.id,
});
},
);
};

0 comments on commit dfdb38e

Please sign in to comment.