Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/encoded bool function parameter #1350

Merged
merged 7 commits into from
Feb 12, 2024
Merged
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
24 changes: 23 additions & 1 deletion src/utils/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,32 @@ export const encodeFunction = (
}
});

const boolify = (parameter: string) => {
mudrila marked this conversation as resolved.
Show resolved Hide resolved
if (['false'].includes(parameter.toLowerCase())) {
return false;
} else if (['true'].includes(parameter.toLowerCase())) {
return true;
} else {
return parameter;
}
};

const parametersFixedWithBool = parametersFixed?.map(parameter => {
if (typeof parameter === 'string') {
return boolify(parameter);
} else if (Array.isArray(parameter)) {
return parameter.map(innerParameter => {
return boolify(innerParameter);
});
} else {
throw new Error('parameter type not as expected');
}
});

try {
return new utils.Interface([functionSignature]).encodeFunctionData(
_functionName,
parametersFixed
parametersFixedWithBool
);
} catch (e) {
logError(e);
Expand Down
69 changes: 65 additions & 4 deletions test/encodeFunction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ test('Function encoding with [boolean=true]', () => {
expect(encodeFunction('foo', 'bool', 'true')).toEqual(encoded);
});

// test('Function encoding with [boolean=false]', () => {
// const encoded = new utils.Interface(['function foo(bool)']).encodeFunctionData('foo', [false]);
// expect(encodeFunction('foo', 'bool', 'false')).toEqual(encoded);
// });
test('Function encoding with [boolean=false]', () => {
const encoded = new utils.Interface(['function foo(bool)']).encodeFunctionData('foo', [false]);
expect(encodeFunction('foo', 'bool', 'false')).toEqual(encoded);
});

test('Function encoding with [uint=0]', () => {
const encoded = new utils.Interface(['function foo(uint)']).encodeFunctionData('foo', [0]);
Expand All @@ -36,3 +36,64 @@ test('Function encoding with [uint8=100]', () => {
const encoded = new utils.Interface(['function foo(uint8)']).encodeFunctionData('foo', [100]);
expect(encodeFunction('foo', 'uint8', '100')).toEqual(encoded);
});

test('Function encoding with tuple', () => {
const encoded = new utils.Interface([
'function someFooWithTupleAndLargeNumbers((address,address,address,uint88,uint88,uint88,uint88,uint88,uint64,uint64,uint40,uint40,uint40,uint40,bool,bytes32),uint256,uint256,bytes32)'
]).encodeFunctionData(
'someFooWithTupleAndLargeNumbers',
[
[
'0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9',
'0x7f63C82b83B9375c21efbEAd2010F003d7FAD746',
'0xE19f640d1FC22FeAf12DbD86b52bEa8Ac7d43E41',
0,
0,
'309485009821345068724781055',
'309485009821345068724781055',
'309485009821345068724781055',
'990000000000000000',
'10000000000000000',
1708516800,
1708905540,
0,
0,
true,
'0x0000000000000000000000000000000000000000000000000000000000000000'
],
'40000000000000000000000000',
'1000000000000000000',
'0x1111111111111111111111111111111111111111111111111111111111111111'
]);
expect(
encodeFunction(
'someFooWithTupleAndLargeNumbers',
'(address,address,address,uint88,uint88,uint88,uint88,uint88,uint64,uint64,uint40,uint40,uint40,uint40,bool,bytes32),uint256,uint256,bytes32',
'(0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9,0x7f63C82b83B9375c21efbEAd2010F003d7FAD746,0xE19f640d1FC22FeAf12DbD86b52bEa8Ac7d43E41,0,0,309485009821345068724781055,309485009821345068724781055,309485009821345068724781055,990000000000000000,10000000000000000,1708516800,1708905540,0,0,true,0x0000000000000000000000000000000000000000000000000000000000000000),40000000000000000000000000,1000000000000000000,0x1111111111111111111111111111111111111111111111111111111111111111'
)).toEqual(encoded)
})

// TODO: This test cases would fail, which is known issue. We'll need to improve our implementation
test.skip('Function encoding with [string="true"]', () => {
const encoded = new utils.Interface(['function foo(string)']).encodeFunctionData('foo', ['true']);
expect(encodeFunction('foo', 'string', 'true')).toEqual(encoded);
});

test.skip('Function encoding with [string="false"]', () => {
const encoded = new utils.Interface(['function foo(string)']).encodeFunctionData('foo', [
'false',
]);
expect(encodeFunction('foo', 'string', 'false')).toEqual(encoded);
});

test.skip('Function encoding with [string=""', () => {
const encoded = new utils.Interface(['function foo(string)']).encodeFunctionData('foo', ['']);
expect(encodeFunction('foo', 'string', '')).toEqual(encoded);
});

test.skip('Function encoding with [string="hello, world"', () => {
const encoded = new utils.Interface(['function foo(string)']).encodeFunctionData('foo', [
'hello, world',
]);
expect(encodeFunction('foo', 'string', 'hello, world')).toEqual(encoded);
});
Loading