Skip to content

Commit

Permalink
Create second flavor of "buildContractCall" to do it viem-style
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgall committed May 5, 2024
1 parent f0c3317 commit 7f59a19
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions src/helpers/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
bytesToBigInt,
Hex,
isHex,
encodeFunctionData,
Abi,
} from 'viem';
import { ContractConnection } from '../types';
import { MetaTransaction, SafePostTransaction, SafeTransaction } from '../types/transaction';
Expand Down Expand Up @@ -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>,
): 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,
Expand All @@ -184,6 +184,45 @@ export const buildContractCall = (
);
};

export const buildContractCall = (
contract: Contract,
method: string,
params: any[],
nonce: number,
delegateCall?: boolean,
overrides?: Partial<SafeTransaction>,
): 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>,
): 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);
Expand Down

0 comments on commit 7f59a19

Please sign in to comment.