Skip to content

Commit

Permalink
Store inputValues in array instead of object to support unnamed params
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `CallContractTransactionInput.inputValues` is now an array rather than an object
  • Loading branch information
jfschwarz committed Oct 6, 2023
1 parent e9592a2 commit 2e392f9
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 23 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ethers-multisend",
"version": "2.4.0",
"version": "3.0.0",
"description": "A package for crafting multi-send transaction from a Zodiac Avatar, such as a Gnosis Safe, based on ethers.js",
"main": "build/cjs/index.js",
"typings": "build/cjs/index.d.ts",
Expand Down Expand Up @@ -68,4 +68,4 @@
"@ethersproject/solidity": "^5.0.0",
"@ethersproject/units": "^5.0.0"
}
}
}
7 changes: 1 addition & 6 deletions src/decodeSingle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,5 @@ const abiCoder = new AbiCoder((name: string, value: unknown) => {

const decodeArgs = (data: string, inputs: ParamType[]) => {
const result = abiCoder.decode(inputs, '0x' + data.substring(10))

const keys = Object.keys(result)
const namedKeys = keys.filter((key) => `${parseInt(key)}` !== key)
const allArgsHaveNames = namedKeys.length * 2 === keys.length
const keysToUse = allArgsHaveNames ? namedKeys : keys
return Object.assign({}, ...keysToUse.map((key) => ({ [key]: result[key] })))
return [...result] // turn result (array with named keys) into a plain array
}
2 changes: 1 addition & 1 deletion src/encodeSingle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const defaultValue = (paramType: ParamType) =>
const encodeFunctionCall = (tx: CallContractTransactionInput) => {
const iface = new Interface(tx.abi)
const values = iface.functions[tx.functionSignature].inputs.map(
(input) => tx.inputValues[input.name] || defaultValue(input)
(input, index) => tx.inputValues[index] || defaultValue(input)
)
return iface.encodeFunctionData(tx.functionSignature, values)
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface CallContractTransactionInput {
value: string // amount of wei to send
abi: string // ABI as JSON string
functionSignature: string
inputValues: { [key: string]: ValueType }
inputValues: ValueType[]
}

export interface TransferFundsTransactionInput {
Expand Down
12 changes: 6 additions & 6 deletions test/decodeSingle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,18 @@ describe('decodeSingle', () => {
abi,
functionSignature:
'logInputs(string,address[2],int256[][],(bytes8,bool))',
inputValues: {
stringParam: 'test',
fixedSizeAddressArrayParam: [
inputValues: [
'test',
[
'0xfF6D102f7A5b52B6A2b654a048b0bA650bE90c59',
'0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984',
],
int2DArrayParam: [
[
['1', '2'],
['3', '4'],
],
tupleParam: ['0x0000000000000000', true],
},
['0x0000000000000000', true],
],
value: '0',
})
})
Expand Down
14 changes: 7 additions & 7 deletions test/encodeSingle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ describe('encodeSingle', () => {
value: '',
functionSignature:
'logInputs(string,address[2],int256[][],(bytes8,bool))',
inputValues: {
stringParam: 'test',
fixedSizeAddressArrayParam: [testToken.address, testNft.address],
int2DArrayParam: [
inputValues: [
'test',
[testToken.address, testNft.address],
[
['1', '2'],
['3', '4'],
],
tupleParam: { bytesMember: hexZeroPad('0x00', 8), boolMember: true },
},
{ bytesMember: hexZeroPad('0x00', 8), boolMember: true },
],
id: '',
})

Expand All @@ -145,7 +145,7 @@ describe('encodeSingle', () => {
abi: inputsLogger.interface.format(FormatTypes.json) as string,
value: '',
functionSignature: 'logInputs(string,address[2])',
inputValues: {},
inputValues: [],
id: '',
})

Expand Down

0 comments on commit 2e392f9

Please sign in to comment.