Skip to content

Commit

Permalink
Merge pull request #1350 from decent-dao/bugfix/encoded-bool-function…
Browse files Browse the repository at this point in the history
…-parameter

Bugfix/encoded bool function parameter
  • Loading branch information
mudrila authored Feb 12, 2024
2 parents f234625 + b0f1692 commit c159034
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 5 deletions.
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) => {
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);
});

0 comments on commit c159034

Please sign in to comment.