From e5fce4410ea59ea9ebe11b1f317df49465521bf5 Mon Sep 17 00:00:00 2001 From: yann300 Date: Mon, 12 Sep 2022 10:38:23 +0200 Subject: [PATCH] fix resolving function abi with tuple --- libs/remix-lib/src/execution/txHelper.ts | 24 ++++++++++++++++-------- libs/remix-lib/test/txHelper.ts | 13 +++++++++++++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/libs/remix-lib/src/execution/txHelper.ts b/libs/remix-lib/src/execution/txHelper.ts index 0b50d96463..f3c78930b1 100644 --- a/libs/remix-lib/src/execution/txHelper.ts +++ b/libs/remix-lib/src/execution/txHelper.ts @@ -102,17 +102,25 @@ export function extractSize (type) { return size ? size[2] : '' } +export function getFunctionLiner (fn, detailTuple: boolean = true) { + /* + if detailsTuple is True, this will return something like fnName((uint, string)) + if detailsTuple is False, this will return something like fnName(tuple) + */ + return fn.name + '(' + fn.inputs.map((value) => { + if (detailTuple && value.components) { + const fullType = makeFullTypeDefinition(value) + return fullType.replace(/tuple/g, '') // return of makeFullTypeDefinition might contain `tuple`, need to remove it cause `methodIdentifier` (fnName) does not include `tuple` keyword + } else { + return value.type + } + }).join(',') + ')' +} + export function getFunction (abi, fnName) { for (let i = 0; i < abi.length; i++) { const fn = abi[i] - if (fn.type === 'function' && fnName === fn.name + '(' + fn.inputs.map((value) => { - if (value.components) { - const fullType = makeFullTypeDefinition(value) - return fullType.replace(/tuple/g, '') // return of makeFullTypeDefinition might contain `tuple`, need to remove it cause `methodIdentifier` (fnName) does not include `tuple` keyword - } else { - return value.type - } - }).join(',') + ')') { + if (fn.type === 'function' && (fnName === getFunctionLiner(fn, true) || fnName === getFunctionLiner(fn, false))) { return fn } } diff --git a/libs/remix-lib/test/txHelper.ts b/libs/remix-lib/test/txHelper.ts index 2532bcd34d..9ddd23788e 100644 --- a/libs/remix-lib/test/txHelper.ts +++ b/libs/remix-lib/test/txHelper.ts @@ -21,6 +21,17 @@ tape('getFunction', function (st) { fn = txHelper.getReceiveInterface(JSON.parse(abi)) st.equal(fn.type, 'receive') + + fn = txHelper.getFunction(testTupleAbi, 'setUser(tuple)') // some compiler version might resolve to tuple. + st.equal(fn.name, 'setUser') + st.equal(fn.inputs[0].type, 'tuple') + st.equal(fn.inputs[0].name, 'user') + + fn = txHelper.getFunctionLiner(testTupleAbi[0], true) + st.equal(fn, 'setUser((string,uint256))') + + fn = txHelper.getFunctionLiner(testTupleAbi[0], false) + st.equal(fn, 'setUser(tuple)') }) const abi = `[ @@ -153,3 +164,5 @@ const abi = `[ "type": "receive" } ]` + +const testTupleAbi = [{"inputs":[{"components":[{"internalType":"string","name":"name","type":"string"},{"internalType":"uint256","name":"age","type":"uint256"}],"internalType":"struct Example.User","name":"user","type":"tuple"}],"name":"setUser","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"userByAddress","outputs":[{"internalType":"string","name":"name","type":"string"},{"internalType":"uint256","name":"age","type":"uint256"}],"stateMutability":"view","type":"function"}] \ No newline at end of file