-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
feat!: remove offset from decodeLog
and decodeFunctionResult
methods
#2514
Conversation
…rom-decode-result
…kh-forks/fuels-ts into fix/remove-offset-from-decode-result
✨ A PR has been created under the |
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 we highlight the breaking changes in the description please?
Coverage Report:
Changed Files:Coverage values did not change👌. |
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.
Are there downsides to removing the offset
property?
Can someone be looking for the offset
in the future?
@arboleya the offset value is equal to |
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.
Pattern
Could we be breaking the patterns already in use?
fuels-ts/packages/transactions/src/coders/transaction.ts
Lines 104 to 133 in e165e37
let decoded; | |
let o = offset; | |
[decoded, o] = new BigNumberCoder('u64').decode(data, o); | |
const scriptGasLimit = decoded; | |
[decoded, o] = new B256Coder().decode(data, o); | |
const receiptsRoot = decoded; | |
[decoded, o] = new BigNumberCoder('u64').decode(data, o); | |
const scriptLength = decoded; | |
[decoded, o] = new BigNumberCoder('u64').decode(data, o); | |
const scriptDataLength = decoded; | |
[decoded, o] = new NumberCoder('u32', { padToWordSize: true }).decode(data, o); | |
const policyTypes = decoded; | |
[decoded, o] = new NumberCoder('u16', { padToWordSize: true }).decode(data, o); | |
const inputsCount = decoded; | |
[decoded, o] = new NumberCoder('u16', { padToWordSize: true }).decode(data, o); | |
const outputsCount = decoded; | |
[decoded, o] = new NumberCoder('u16', { padToWordSize: true }).decode(data, o); | |
const witnessesCount = decoded; | |
[decoded, o] = new ByteArrayCoder(scriptLength.toNumber()).decode(data, o); | |
const script = decoded; | |
[decoded, o] = new ByteArrayCoder(scriptDataLength.toNumber()).decode(data, o); | |
const scriptData = decoded; | |
[decoded, o] = new PoliciesCoder().decode(data, o, policyTypes); | |
const policies = decoded; | |
[decoded, o] = new ArrayCoder(new InputCoder(), inputsCount).decode(data, o); | |
const inputs = decoded; | |
[decoded, o] = new ArrayCoder(new OutputCoder(), outputsCount).decode(data, o); | |
const outputs = decoded; | |
[decoded, o] = new ArrayCoder(new WitnessCoder(), witnessesCount).decode(data, o); | |
const witnesses = decoded; |
Exceptions
Also, it seems some Coders have non-obvious ways of computing the offset:
return [toHex(bytes, 32), offset + 32]; |
fuels-ts/packages/abi-coder/src/encoding/coders/VecCoder.ts
Lines 63 to 69 in e165e37
let newOffset = offsetAndLength; | |
const chunks = []; | |
for (let i = 0; i < length; i++) { | |
const [decoded, optionOffset] = this.coder.decode(data, newOffset); | |
chunks.push(decoded); | |
newOffset = optionOffset; | |
} |
API Preferences
Regarding personal preference, I'd go with Objects instead of Arrays.
But only if we'd change the pattern everywhere, which is not the case.
const { decodedLog, offset } = theInterface.decodeLog(data, logId);
// vs
const [decodedLog, offset] = theInterface.decodeLog(data, logId);
Conclusion
I'm not sure if the breaking change is worth it.
The change is subtle, but the latter hides information.
const [decodedLog] = theInterface.decodeLog(data, logId);
// vs
const decodedLog = theInterface.decodeLog(data, logId);
As for if this is a good or a bad thing, I'll let others chime in.
@arboleya you're right, introducing this would break the pattern present in other coders; I didn't think of that. If I remember correctly, there was talk on the offsite around not needing the offset value across the board on all our coders and that this would be considered in #1795. The argument for merging it right now is that this is a high level API relative to the coders and we can get the DX benefits of it right now because the offset value is never needed when using these methods. The counter-argument is that API consistency is more important and that it should be done in tandem with #1795. In the end, I agree with your uncertainty
and I'll close this PR. I've added a comment on #1795 linking to this PR. |
Interface.decodeLog
andInterface.decodeFunctionResult
are top-level functions that don't need to return a data offset because you pass in all the data and the returned offset is always the length of that data. If somebody wants to get theoffset
value after this change, they can just calldata.length
.I tagged it as a
feat
because it improves the DX of the methods.Supersedes #2308