|
1 | | -import { SequenceConnect, useOpenConnectModal } from "@0xsequence/connect"; |
2 | | -import { getAccount, getChainId, signMessage, switchChain } from "@wagmi/core"; |
| 1 | +import { sendTransactions, SequenceConnect, useOpenConnectModal } from "@0xsequence/connect"; |
| 2 | +import { SequenceIndexer } from "@0xsequence/indexer"; |
| 3 | +import { |
| 4 | + getAccount, |
| 5 | + getCallsStatus, |
| 6 | + getChainId, |
| 7 | + getPublicClient, |
| 8 | + getWalletClient, |
| 9 | + signMessage, |
| 10 | + switchChain, |
| 11 | +} from "@wagmi/core"; |
3 | 12 | import { useLayoutEffect, useRef } from "react"; |
| 13 | +import { concat, numberToHex } from "viem"; |
4 | 14 | import { useAccount, useDisconnect } from "wagmi"; |
5 | 15 |
|
| 16 | +import { ChainId, networks } from "@0xsequence/network"; |
6 | 17 | import hostExaApp from "./hostExaApp"; // host SDK: expose APIs to the iframe |
7 | 18 | import { connectConfig, wagmiConfig } from "./sequence"; |
8 | 19 |
|
@@ -38,6 +49,36 @@ function App() { |
38 | 49 | if (!Array.isArray(params) || params.length !== 2) throw new Error("bad params"); |
39 | 50 | if (params[1] !== getAccount(wagmiConfig).address) throw new Error("bad account"); |
40 | 51 | return signMessage(wagmiConfig, { message: { raw: params[0] } }); |
| 52 | + case "wallet_sendCalls": { |
| 53 | + if (!Array.isArray(params) || params.length !== 1) throw new Error("bad params"); |
| 54 | + const { address, chainId, connector } = getAccount(wagmiConfig); |
| 55 | + if (!address || !chainId || !connector) throw new Error("not connected"); |
| 56 | + if (params[0].from && params[0].from !== address) throw new Error("bad account"); |
| 57 | + if (params[0].chainId && Number(params[0].chainId) !== chainId) throw new Error("bad chain id"); |
| 58 | + const hashes = await Promise.all( |
| 59 | + ( |
| 60 | + await sendTransactions({ |
| 61 | + chainId, |
| 62 | + connector, |
| 63 | + senderAddress: address, |
| 64 | + publicClient: getPublicClient(wagmiConfig)!, |
| 65 | + walletClient: await getWalletClient(wagmiConfig)!, |
| 66 | + indexerClient: new SequenceIndexer( |
| 67 | + `https://${networks[chainId as ChainId]!.name}-indexer.sequence.app`, |
| 68 | + connectConfig.projectAccessKey, |
| 69 | + ), |
| 70 | + transactions: ( |
| 71 | + params[0].calls as { to: `0x${string}`; data?: `0x${string}`; value?: `0x${string}` }[] |
| 72 | + ).map(({ to, data, value }) => ({ to, data, value: value && BigInt(value) })), |
| 73 | + waitConfirmationForLastTransaction: false, |
| 74 | + }) |
| 75 | + ).map((send) => send() as Promise<`0x${string}`>), |
| 76 | + ); |
| 77 | + return { id: concat([...hashes, numberToHex(chainId, { size: 32 }), TX_MAGIC_ID]) }; |
| 78 | + } |
| 79 | + case "wallet_getCallsStatus": |
| 80 | + if (!Array.isArray(params) || params.length !== 1 || typeof params[0] !== "string") throw new Error("bad"); |
| 81 | + return getCallsStatus(wagmiConfig, { id: params[0] }); |
41 | 82 | default: |
42 | 83 | throw new Error(`${method} not supported`); |
43 | 84 | } |
@@ -75,3 +116,5 @@ export default function Layout() { |
75 | 116 | </SequenceConnect> |
76 | 117 | ); |
77 | 118 | } |
| 119 | + |
| 120 | +const TX_MAGIC_ID = "0x5792579257925792579257925792579257925792579257925792579257925792" as const; |
0 commit comments