|
| 1 | +import { encodeFunctionData, type Address } from 'viem'; |
| 2 | + |
| 3 | +import { makeWalletClient, requestWalletAddress } from './clients'; |
| 4 | +import { getRelayBaseUrl, getTxMode } from './manifest'; |
| 5 | +import type { TxPhase } from '../components/TxStatus'; |
| 6 | + |
| 7 | +export type SubmitWriteTxResult = { |
| 8 | + hash: `0x${string}`; |
| 9 | + receipt: any; |
| 10 | +}; |
| 11 | + |
| 12 | +export async function submitWriteTx(args: { |
| 13 | + manifest: any; |
| 14 | + deployment: any; |
| 15 | + chain: any; |
| 16 | + publicClient: any; |
| 17 | + address: `0x${string}`; |
| 18 | + abi: any[]; |
| 19 | + functionName: string; |
| 20 | + contractArgs: any[]; |
| 21 | + value?: bigint; |
| 22 | + setStatus?: (s: string | null) => void; |
| 23 | + onPhase?: (phase: TxPhase) => void; |
| 24 | + onHash?: (hash: `0x${string}`) => void; |
| 25 | +}): Promise<SubmitWriteTxResult> { |
| 26 | + const mode = getTxMode(args.manifest); |
| 27 | + |
| 28 | + if (mode === 'sponsored') { |
| 29 | + args.onPhase?.('submitting'); |
| 30 | + args.setStatus?.('Submitting sponsored transaction…'); |
| 31 | + const data = encodeFunctionData({ |
| 32 | + abi: args.abi, |
| 33 | + functionName: args.functionName, |
| 34 | + args: args.contractArgs |
| 35 | + }); |
| 36 | + |
| 37 | + const relayBaseUrl = getRelayBaseUrl(args.manifest).replace(/\/+$/, ''); |
| 38 | + const relayUrl = relayBaseUrl.endsWith('/__tokenhost/relay') ? relayBaseUrl : `${relayBaseUrl}/__tokenhost/relay`; |
| 39 | + const res = await fetch(relayUrl, { |
| 40 | + method: 'POST', |
| 41 | + headers: { 'content-type': 'application/json' }, |
| 42 | + body: JSON.stringify({ |
| 43 | + to: args.address, |
| 44 | + data, |
| 45 | + value: args.value ? `0x${args.value.toString(16)}` : undefined |
| 46 | + }) |
| 47 | + }); |
| 48 | + |
| 49 | + const body = await res.json().catch(() => null); |
| 50 | + if (!res.ok || !body?.ok || !body?.txHash) { |
| 51 | + const msg = String(body?.error ?? `Relay request failed (HTTP ${res.status}).`); |
| 52 | + throw new Error(msg); |
| 53 | + } |
| 54 | + |
| 55 | + const hash = String(body.txHash) as `0x${string}`; |
| 56 | + args.onHash?.(hash); |
| 57 | + args.onPhase?.('submitted'); |
| 58 | + args.setStatus?.(`Submitted ${hash.slice(0, 10)}…`); |
| 59 | + args.onPhase?.('confirming'); |
| 60 | + args.setStatus?.('Waiting for confirmation…'); |
| 61 | + const receipt = await args.publicClient.waitForTransactionReceipt({ hash }); |
| 62 | + args.onPhase?.('confirmed'); |
| 63 | + args.setStatus?.(`Confirmed ${hash.slice(0, 10)}…`); |
| 64 | + return { hash, receipt }; |
| 65 | + } |
| 66 | + |
| 67 | + args.onPhase?.('submitting'); |
| 68 | + args.setStatus?.('Connecting wallet…'); |
| 69 | + const account = await requestWalletAddress(args.chain); |
| 70 | + const walletClient = makeWalletClient(args.chain); |
| 71 | + args.setStatus?.('Sending transaction…'); |
| 72 | + const hash = (await walletClient.writeContract({ |
| 73 | + address: args.address as Address, |
| 74 | + abi: args.abi, |
| 75 | + functionName: args.functionName, |
| 76 | + args: args.contractArgs, |
| 77 | + account, |
| 78 | + value: args.value, |
| 79 | + chain: args.chain |
| 80 | + })) as `0x${string}`; |
| 81 | + args.onHash?.(hash); |
| 82 | + args.onPhase?.('submitted'); |
| 83 | + args.setStatus?.(`Submitted ${hash.slice(0, 10)}…`); |
| 84 | + args.onPhase?.('confirming'); |
| 85 | + args.setStatus?.('Waiting for confirmation…'); |
| 86 | + const receipt = await args.publicClient.waitForTransactionReceipt({ hash }); |
| 87 | + args.onPhase?.('confirmed'); |
| 88 | + args.setStatus?.(`Confirmed ${hash.slice(0, 10)}…`); |
| 89 | + return { hash, receipt }; |
| 90 | +} |
0 commit comments