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 4 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
32 changes: 28 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,27 @@ 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 [string="true"]', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds more like an edge-case though, but I guess it highlights overall flakiness and fragileness of our encodeFunction

const encoded = new utils.Interface(['function foo(string)']).encodeFunctionData('foo', ['true']);
expect(encodeFunction('foo', 'string', 'true')).toEqual(encoded);
});

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

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

test('Function encoding with [string="hello, world"', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this failing test I'd rather comment this out - our UX flow currently depends on separating params with commas, so in order to get this working - we need to adjust the UX flow itself. Making it look and work more like we have in ProposalTemplate definitely will give us ability to support strings with comma and pretty much any other characters

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Da-Colon Added test.skip for those expected-to-fail test cases - feel free to remove those skips if you'll figure out proper implementation

const encoded = new utils.Interface(['function foo(string)']).encodeFunctionData('foo', [
'hello, world',
]);
expect(encodeFunction('foo', 'string', 'hello, world')).toEqual(encoded);
});
Loading