|
| 1 | +import { createPublicClient, createWalletClient, http, toHex } from "viem"; |
| 2 | +import { privateKeyToAccount } from "viem/accounts"; |
| 3 | +import { anvil } from "viem/chains"; |
| 4 | +import { vaaV1ReceiveWithGasDropAbi } from "../../abis/vaaV1ReceiveWithGasDropoffAbi"; |
| 5 | +import type { ChainConfig } from "../../chains"; |
| 6 | +import { EVM_PRIVATE_KEY } from "../../consts"; |
| 7 | +import type { RelayRequestData, TxInfo } from "../../types"; |
| 8 | +import { |
| 9 | + getFirstDropOffInstruction, |
| 10 | + getTotalGasLimitAndMsgValue, |
| 11 | + getTotalMsgValueFromGasInstructions, |
| 12 | +} from "../../utils"; |
| 13 | +import { |
| 14 | + deserializeRelayInstructions, |
| 15 | + trimToAddress, |
| 16 | +} from "../../layouts/utils"; |
| 17 | + |
| 18 | +export const relayVAAv1 = async ( |
| 19 | + chainConfig: ChainConfig, |
| 20 | + relayRequest: RelayRequestData, |
| 21 | + base64Vaa: string, |
| 22 | +): Promise<Array<TxInfo>> => { |
| 23 | + const transport = http(chainConfig.rpc); |
| 24 | + const publicClient = createPublicClient({ |
| 25 | + chain: anvil, |
| 26 | + transport, |
| 27 | + }); |
| 28 | + |
| 29 | + const { maxMsgValue, gasDropOffLimit } = chainConfig.capabilities; |
| 30 | + |
| 31 | + const relayInstructions = deserializeRelayInstructions( |
| 32 | + relayRequest.requestForExecution.relayInstructionsBytes, |
| 33 | + ); |
| 34 | + |
| 35 | + const { gasLimit } = getTotalGasLimitAndMsgValue( |
| 36 | + relayRequest.requestForExecution.relayInstructionsBytes, |
| 37 | + ); |
| 38 | + |
| 39 | + const relayMsgValue = getTotalMsgValueFromGasInstructions( |
| 40 | + relayInstructions, |
| 41 | + maxMsgValue, |
| 42 | + ); |
| 43 | + |
| 44 | + const { dropOff, recipient } = getFirstDropOffInstruction( |
| 45 | + relayInstructions, |
| 46 | + gasDropOffLimit, |
| 47 | + ); |
| 48 | + |
| 49 | + const account = privateKeyToAccount(EVM_PRIVATE_KEY); |
| 50 | + |
| 51 | + const client = createWalletClient({ |
| 52 | + account, |
| 53 | + chain: chainConfig.viemChain, |
| 54 | + transport, |
| 55 | + }); |
| 56 | + |
| 57 | + const payloadHex = toHex(Buffer.from(base64Vaa, "base64")); |
| 58 | + |
| 59 | + const { request } = await publicClient.simulateContract({ |
| 60 | + account, |
| 61 | + address: "0x13b62003C8b126Ec0748376e7ab22F79Fb8bbDF2", |
| 62 | + gas: gasLimit, |
| 63 | + value: relayMsgValue + dropOff, |
| 64 | + abi: vaaV1ReceiveWithGasDropAbi, |
| 65 | + functionName: "receiveMessage", |
| 66 | + args: [ |
| 67 | + trimToAddress(relayRequest.requestForExecution.dstAddr), |
| 68 | + payloadHex, |
| 69 | + trimToAddress(toHex(recipient.address)), |
| 70 | + dropOff, |
| 71 | + ], |
| 72 | + }); |
| 73 | + |
| 74 | + const hash = await client.writeContract(request); |
| 75 | + |
| 76 | + const receipt = await publicClient.waitForTransactionReceipt({ |
| 77 | + hash, |
| 78 | + }); |
| 79 | + |
| 80 | + const block = await publicClient.getBlock({ blockHash: receipt.blockHash }); |
| 81 | + const blockTime = new Date(Number(block.timestamp) * 1000); |
| 82 | + |
| 83 | + let totalCostValue = |
| 84 | + receipt.effectiveGasPrice * receipt.gasUsed + (request.value || 0n); |
| 85 | + |
| 86 | + const txInfo = { |
| 87 | + txHash: receipt.transactionHash, |
| 88 | + chainId: chainConfig.wormholeChainId, |
| 89 | + blockNumber: receipt.blockNumber, |
| 90 | + blockTime, |
| 91 | + cost: totalCostValue, |
| 92 | + }; |
| 93 | + return [txInfo]; |
| 94 | +}; |
0 commit comments