-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
fix: remove offset from decodeLog
and decodeFunctionResult
methods
#2308
base: master
Are you sure you want to change the base?
fix: remove offset from decodeLog
and decodeFunctionResult
methods
#2308
Conversation
Hey @DZakh, thanks for taking an interest in helping us to improve the TS SDK, we appreciate it 🙏. Could you please elaborate on why you believe these changes are necessary? Understanding your reasoning will help us assess the impact and benefits of this modification. |
I don't have a strong feeling about the change, and it's not required for my project.
If you say it's intentional, it would be nice to change the return type from |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @DZakh, after investigation I do agree with you and I think it was oversight that these entry points for function and log value decoding would include the offset. Most SDK consumers will utilise the TransactionResponse
or FunctionInvocationResult
classes that we return on sendTransaction()
or call()
respectively that internally will pick off the decoded values rather than returning the tuple.
To get this over the line, it'd be great if you can add a changeset (run pnpm changeset
on the monorepo and it would also be great to get some tests so this is not missed in the future, I would reccomend adding them to packages/abi-coder/test/Interface.test.ts
. Let me know if I can help with either of these.
I'll try to find some time during the week 👌 |
a32515f
to
99aa766
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you take a look, please
packages/abi-coder/src/Interface.ts
Outdated
} | ||
|
||
decodeLog(data: BytesLike, logId: string): any { | ||
const loggedType = this.jsonAbi.loggedTypes.find((type) => type.logId === logId); | ||
const loggedType = this.jsonAbi.loggedTypes.find((type) => type.logId.toString() === logId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a bug in latest version after logId
was changed to string
.
Will rb
become a string
type? Why was the change needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @nedsalk
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DZakh the change is part of work on #2313, specifically tied to FuelLabs/sway#5953.
What is the bug you're referring to? Could you give me an example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that you might have tested this solution out with a forc
version that's not 0.59.0
. After you build with it, you won't need this type.logId.toString()
change because the logId
s are strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's probably the case
const data = exhaustiveExamplesInterface.decodeLog('0x01000000000000000000000000000020', '0'); | ||
expect(data).toEqual({ | ||
a: true, | ||
b: 32, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test is failing. The decoded field value is 0
. Probably something is broken.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rewriting the test like below works:
const data = exhaustiveExamplesInterface.decodeFunctionResult(
'struct_simple',
hexlify(Uint8Array.from([1, 0, 0, 0, 32]))
);
The hexlified value is 0x0100000020
.
Please note that Interface.test.ts
is not an ideal test suite because its test data is written with the assumption that types are encoded in a specific way, which should be refactored in the future. That being said, it's okay that you added these tests here.
`function ${nameOrSignatureOrSelector} not found: ${JSON.stringify(fn)}.` | ||
`Function ${nameOrSignatureOrSelector} not found.` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fn
is undefined here
I'm done with my changes. Ready for re-review |
|
||
describe('decodeLog', () => { | ||
it('should return decoded log by id', () => { | ||
const data = exhaustiveExamplesInterface.decodeLog('0x01000000000000000000000000000020', '0'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logId
passed here should be "8500535089865083573"
when the sway program is built with forc 0.59.0
.
Also, passing in Uint8Array.from([1, 0, 0, 0, 32])
here works.
it('should throw an error when log does not exist', () => { | ||
expect(() => | ||
exhaustiveExamplesInterface.decodeLog('0x01000000000000000000000000000020', '1') | ||
).toThrowError(`Log type with logId '1' doesn't exist in the ABI.`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use the expectToThrowFuelError
utility here (example).
'doesnt_exist', | ||
'0x01000000000000000000000000000020' | ||
); | ||
}).toThrowError(/^Function doesnt_exist not found\.$/); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should use the expectToThrowFuelError
utility here (example).
.changeset/perfect-kings-camp.md
Outdated
"@fuel-ts/abi-coder": minor | ||
--- | ||
|
||
Changed decodeLog and decodeFunctionResult return types to have only decoded value without an offset |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed decodeLog and decodeFunctionResult return types to have only decoded value without an offset | |
fix!: don't return offset from public decode functions |
We have some CI logic that mandates that this text is equal to the pr title.
@DZakh Thanks for the work! I agree that the offset shouldn't be returned. |
"@fuel-ts/abi-coder": minor | ||
--- | ||
|
||
fix: Changed decodeLog and decodeFunctionResult return types to have only decoded value without an offset |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix: Changed decodeLog and decodeFunctionResult return types to have only decoded value without an offset | |
fix!: changed `decodeLog` and `decodeFunctionResult` return types to have only decoded value without an offset |
I believe this is a breaking change, as @nedsalk mentioned, this will need to be in sync with the title 😄
decodeLog
and decodeFunctionResult
methods
It feels like public
decode
functions should return the decoded value itself. Not a tuple with an internal offset.