-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working
- Loading branch information
Showing
8 changed files
with
415 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/client/ui/react-ui/src/components/embed/v3/crypto/utils/handleEvmTransaction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import type { EmbeddedCheckoutV3IFrameEmitter } from "@crossmint/client-sdk-base"; | ||
import { | ||
type BlockchainIncludingTestnet, | ||
blockchainToChainId, | ||
type EVMBlockchainIncludingTestnet, | ||
} from "@crossmint/common-sdk-base"; | ||
import type { EthereumWallet } from "@dynamic-labs/ethereum-core"; | ||
import { parseTransaction, type TransactionSerializableEIP1559 } from "viem"; | ||
|
||
export async function handleEvmTransaction({ | ||
primaryWallet, | ||
chain, | ||
serializedTransaction, | ||
iframeClient, | ||
}: { | ||
primaryWallet: EthereumWallet; | ||
chain: BlockchainIncludingTestnet; | ||
serializedTransaction: string; | ||
iframeClient: EmbeddedCheckoutV3IFrameEmitter; | ||
}) { | ||
try { | ||
await primaryWallet.switchNetwork(blockchainToChainId(chain as EVMBlockchainIncludingTestnet)); | ||
} catch (error) { | ||
console.error("[CryptoWalletConnectionHandler] failed to switch network", error); | ||
iframeClient.send("crypto:send-transaction:failed", { | ||
error: (error as Error).message, | ||
}); | ||
return; | ||
} | ||
|
||
let walletClient: Awaited<ReturnType<typeof primaryWallet.getWalletClient>>; | ||
try { | ||
walletClient = await primaryWallet.getWalletClient(); | ||
} catch (error) { | ||
console.error("[CryptoWalletConnectionHandler] failed to get wallet client", error); | ||
iframeClient.send("crypto:send-transaction:failed", { | ||
error: (error as Error).message, | ||
}); | ||
return; | ||
} | ||
|
||
let parsedTransaction: TransactionSerializableEIP1559; | ||
try { | ||
parsedTransaction = parseTransaction(serializedTransaction as `0x${string}`) as TransactionSerializableEIP1559; | ||
} catch (error) { | ||
console.error("[CryptoWalletConnectionHandler] failed to parse transaction", error); | ||
iframeClient.send("crypto:send-transaction:failed", { | ||
error: (error as Error).message, | ||
}); | ||
return; | ||
} | ||
|
||
try { | ||
const txId = await walletClient.sendTransaction(parsedTransaction); | ||
console.log("[CryptoWalletConnectionHandler] txId", txId); | ||
iframeClient.send("crypto:send-transaction:success", { | ||
txId, | ||
}); | ||
} catch (error) { | ||
console.error("[CryptoWalletConnectionHandler] failed to send transaction", error); | ||
iframeClient.send("crypto:send-transaction:failed", { | ||
error: (error as Error).message, | ||
}); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
packages/client/ui/react-ui/src/components/embed/v3/crypto/utils/handleSendTransaction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { Wallet } from "@dynamic-labs/sdk-react-core"; | ||
|
||
import type { BlockchainIncludingTestnet } from "@crossmint/common-sdk-base"; | ||
|
||
import { handleEvmTransaction } from "./handleEvmTransaction"; | ||
|
||
import { isSolanaWallet } from "@dynamic-labs/solana"; | ||
import { handleSolanaTransaction } from "./handleSolanaTransaction"; | ||
import { isEthereumWallet } from "@dynamic-labs/ethereum"; | ||
import type { EmbeddedCheckoutV3IFrameEmitter } from "@crossmint/client-sdk-base"; | ||
|
||
export async function handleSendTransaction( | ||
primaryWallet: Wallet, | ||
chain: BlockchainIncludingTestnet, | ||
serializedTransaction: string, | ||
iframeClient: EmbeddedCheckoutV3IFrameEmitter | ||
) { | ||
const commonParams = { | ||
chain, | ||
serializedTransaction, | ||
iframeClient, | ||
}; | ||
if (isSolanaWallet(primaryWallet)) { | ||
return await handleSolanaTransaction({ | ||
...commonParams, | ||
primaryWallet, | ||
}); | ||
} else if (isEthereumWallet(primaryWallet)) { | ||
return await handleEvmTransaction({ ...commonParams, primaryWallet }); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
packages/client/ui/react-ui/src/components/embed/v3/crypto/utils/handleSolanaTransaction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { EmbeddedCheckoutV3IFrameEmitter } from "@crossmint/client-sdk-base"; | ||
import type { SolanaWallet } from "@dynamic-labs/solana-core"; | ||
import { Transaction } from "@solana/web3.js"; | ||
import base58 from "bs58"; | ||
|
||
export async function handleSolanaTransaction({ | ||
primaryWallet, | ||
serializedTransaction, | ||
iframeClient, | ||
}: { | ||
primaryWallet: SolanaWallet; | ||
serializedTransaction: string; | ||
iframeClient: EmbeddedCheckoutV3IFrameEmitter; | ||
}) { | ||
// TODO: Handle switch network | ||
|
||
let signer: Awaited<ReturnType<typeof primaryWallet.getSigner>>; | ||
try { | ||
signer = await primaryWallet.getSigner(); | ||
} catch (error) { | ||
console.error("[CryptoWalletConnectionHandler] failed to get signer", error); | ||
iframeClient.send("crypto:send-transaction:failed", { | ||
error: "Failed to get signer", | ||
}); | ||
return; | ||
} | ||
|
||
let deserializedTransaction: Transaction; | ||
try { | ||
deserializedTransaction = Transaction.from(base58.decode(serializedTransaction)); | ||
} catch (error) { | ||
console.error("[CryptoWalletConnectionHandler] failed to deserialize transaction", error); | ||
iframeClient.send("crypto:send-transaction:failed", { | ||
error: "Failed to deserialize transaction", | ||
}); | ||
return; | ||
} | ||
|
||
try { | ||
const { signature: txId } = await signer.signAndSendTransaction(deserializedTransaction); | ||
console.log("[CryptoWalletConnectionHandler] txId", txId); | ||
iframeClient.send("crypto:send-transaction:success", { | ||
txId, | ||
}); | ||
} catch (error) { | ||
console.error("[CryptoWalletConnectionHandler] failed to send transaction", error); | ||
iframeClient.send("crypto:send-transaction:failed", { | ||
error: (error as Error).message, | ||
}); | ||
} | ||
} |
Oops, something went wrong.