From 7f59a1916617c468306381c25381b764e7cc7615 Mon Sep 17 00:00:00 2001 From: Adam Gall Date: Sun, 5 May 2024 15:01:16 -0400 Subject: [PATCH] Create second flavor of "buildContractCall" to do it viem-style --- src/helpers/crypto.ts | 53 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/src/helpers/crypto.ts b/src/helpers/crypto.ts index 93ac50b795..a7a3359ed0 100644 --- a/src/helpers/crypto.ts +++ b/src/helpers/crypto.ts @@ -12,6 +12,8 @@ import { bytesToBigInt, Hex, isHex, + encodeFunctionData, + Abi, } from 'viem'; import { ContractConnection } from '../types'; import { MetaTransaction, SafePostTransaction, SafeTransaction } from '../types/transaction'; @@ -161,20 +163,18 @@ export const buildSafeAPIPost = async ( }; }; -export const buildContractCall = ( - contract: Contract, - method: string, - params: any[], +const finishBuildingConractCall = ( + data: Hex, nonce: number, + contractAddress: Address, delegateCall?: boolean, overrides?: Partial, -): SafeTransaction => { - const data = contract.interface.encodeFunctionData(method, params); +) => { const operation: 0 | 1 = delegateCall ? 1 : 0; return buildSafeTransaction( Object.assign( { - to: contract.address, + to: contractAddress, data, operation, nonce, @@ -184,6 +184,45 @@ export const buildContractCall = ( ); }; +export const buildContractCall = ( + contract: Contract, + method: string, + params: any[], + nonce: number, + delegateCall?: boolean, + overrides?: Partial, +): SafeTransaction => { + const data = contract.interface.encodeFunctionData(method, params); + if (!isHex(data)) { + throw new Error('encoded function data not hexadecimal'); + } + return finishBuildingConractCall( + data, + nonce, + getAddress(contract.address), + delegateCall, + overrides, + ); +}; + +export const buildContractCallViem = ( + contractAbi: Abi, + contractAddress: Address, + method: string, + params: any[], + nonce: number, + delegateCall?: boolean, + overrides?: Partial, +): SafeTransaction => { + const data = encodeFunctionData({ + abi: contractAbi, + functionName: method, + args: params, + }); + + return finishBuildingConractCall(data, nonce, contractAddress, delegateCall, overrides); +}; + const encodeMetaTransaction = (tx: MetaTransaction): string => { const txDataBytes = toBytes(tx.data); const txDataHex = toHex(txDataBytes);