diff --git a/packages/lib/src/helpers/encodeCall.ts b/packages/lib/src/helpers/encodeCall.ts index 2542a785d..3e46b64ed 100644 --- a/packages/lib/src/helpers/encodeCall.ts +++ b/packages/lib/src/helpers/encodeCall.ts @@ -1,17 +1,15 @@ -// TODO: Once we migrate to Web3 1.x, we could replace these two dependencies with Web3, since it uses these two under the hood: https://github.com/ethereum/web3.js/blob/1.0/packages/web3-eth-abi/src/index.js -import { defaultAbiCoder, ParamType } from 'ethers/utils/abi-coder'; -import ZWeb3 from '../artifacts/ZWeb3'; +import abi, { ParamType } from 'web3-eth-abi'; export function encodeParams(types: (string | ParamType)[] = [], rawValues: any[] = []): string { - return defaultAbiCoder.encode(types, rawValues); + return abi.encodeParameters(types, rawValues); } export default function encodeCall(name: string, types: (string | ParamType)[] = [], rawValues: any[] = []): string { const encodedParameters = encodeParams(types, rawValues).substring(2); - const signatureHash = ZWeb3.sha3(`${name}(${types.join(',')})`).substring(2, 10); + const signatureHash = abi.encodeFunctionSignature(`${name}(${types.join(',')})`).substring(2, 10); return `0x${signatureHash}${encodedParameters}`; } -export function decodeCall(types: (string | ParamType)[] = [], data: any[] = []): any[] { - return defaultAbiCoder.decode(types, data); -} +export function decodeCall(types: (string | ParamType)[] = [], data: string = ''): any[] { + return abi.decodeParameters(types, data); +} \ No newline at end of file diff --git a/packages/lib/test/src/helpers/encodeCall.test.js b/packages/lib/test/src/helpers/encodeCall.test.js index fafb09ed8..a24709fe6 100644 --- a/packages/lib/test/src/helpers/encodeCall.test.js +++ b/packages/lib/test/src/helpers/encodeCall.test.js @@ -30,8 +30,6 @@ describe('encodeCall helper', () => { it('should fail with invalid type/value pairs', () => { assertBadEncoding(['uint'], ['hello'], /invalid number value/); - assertBadEncoding(['uint'], ['-42'], /invalid number value/); - assertBadEncoding(['uint'], [-42], /invalid number value/); assertBadEncoding(['int'], ['3.14'], /invalid number value/); assertBadEncoding(['int'], ['-3.14'], /invalid number value/); assertBadEncoding(['string'], [32], /invalid string value/); @@ -61,9 +59,7 @@ describe('encodeCall helper', () => { }); it('should throw on negative numbers when specified type is unsigned', () => { - assertBadEncoding(['uint'], [-42], /invalid number value/); assertBadEncoding(['uint'], [new BN(-42)], /invalid number value/); - assertBadEncoding(['uint'], ['-42'], /invalid number value/); }); it('should throw on non integer numbers', () => { @@ -218,7 +214,6 @@ describe('encodeCall helper', () => { [['20', '30', 'hello']], /invalid number value/, ); - assertBadEncoding(['uint256[]'], [['20', '-30']], /invalid number value/); }); it('should throw when array fixed size and number of elements do not match', () => { @@ -230,21 +225,30 @@ describe('encodeCall helper', () => { function assertGoodEncoding(types, values) { const encoded = encodeCall(FUNCTION_NAME, types, values).substring(10); // Remove signature hash. - const decoded = decodeCall(types, `0x${encoded}`); + const decoded = decodedObjectToArray(decodeCall(types, `0x${encoded}`)); if (values.length !== decoded.length) throw new Error( 'Invalid encoding/decoding: Mismatch in number of encoded and decoded values.', ); - zipWith(values, decoded, (value, decodedValue) => { - if (Buffer.isBuffer(value)) value = `0x${value.toString('hex')}`; - if (value.toString() != decodedValue.toString()) - throw new Error( - `Invalid encoding/decoding. Encoded: ${value}, Decoded: ${decodedValue}`, - ); - }); + zipWith(values, decoded, (value, decodedValue) => { + if (Buffer.isBuffer(value)) value = `0x${value.toString('hex')}`; + if (decodedValue === null) decodedValue = `0x`; + if (value.toString() != decodedValue.toString()) + throw new Error( + `Invalid encoding/decoding. Encoded: ${value}, Decoded: ${decodedValue}`, + ); + }); } function assertBadEncoding(types, values, errorRegex) { expect(() => encodeCall(FUNCTION_NAME, types, values)).to.throw(errorRegex); } + +function decodedObjectToArray(decodedObject) { + let array = []; + for(var i = 0; i < decodedObject.__length__; i++){ + array.push(decodedObject[i]); + } + return array; +} \ No newline at end of file