Skip to content
This repository has been archived by the owner on Jan 24, 2022. It is now read-only.

Migrate encodeCall.ts to web3 per comment #1166

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions packages/lib/src/helpers/encodeCall.ts
Original file line number Diff line number Diff line change
@@ -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);
}
30 changes: 17 additions & 13 deletions packages/lib/test/src/helpers/encodeCall.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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;
}