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

fix!: remove offset from decodeLog and decodeFunctionResult methods #2308

Closed
Closed
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
8 changes: 4 additions & 4 deletions packages/abi-coder/src/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class Interface<TAbi extends JsonAbi = JsonAbi> {

throw new FuelError(
ErrorCode.FUNCTION_NOT_FOUND,
`function ${nameOrSignatureOrSelector} not found: ${JSON.stringify(fn)}.`
`Function ${nameOrSignatureOrSelector} not found.`
Copy link
Author

Choose a reason for hiding this comment

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

fn is undefined here

);
}

Expand All @@ -71,11 +71,11 @@ export class Interface<TAbi extends JsonAbi = JsonAbi> {
const fragment =
typeof functionFragment === 'string' ? this.getFunction(functionFragment) : functionFragment;

return fragment.decodeOutput(data);
return fragment.decodeOutput(data)[0];
}

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);
nedsalk marked this conversation as resolved.
Show resolved Hide resolved
if (!loggedType) {
throw new FuelError(
ErrorCode.LOG_TYPE_NOT_FOUND,
Expand All @@ -85,7 +85,7 @@ export class Interface<TAbi extends JsonAbi = JsonAbi> {

return AbiCoder.decode(this.jsonAbi, loggedType.loggedType, arrayify(data), 0, {
encoding: this.encoding,
});
})[0];
}

encodeConfigurable(name: string, value: InputValue) {
Expand Down
38 changes: 38 additions & 0 deletions packages/abi-coder/test/Interface.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,4 +766,42 @@ describe('Abi interface', () => {
);
});
});

describe('decodeLog', () => {
it('should return decoded log by id', () => {
const data = exhaustiveExamplesInterface.decodeLog('0x01000000000000000000000000000020', '0');
Copy link
Contributor

@nedsalk nedsalk May 24, 2024

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.

expect(data).toEqual({
a: true,
b: 32,
nedsalk marked this conversation as resolved.
Show resolved Hide resolved
});
});

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.`);
Copy link
Contributor

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).

});
});

describe('decodeFunctionResult', () => {
it('should return decoded function result', () => {
const data = exhaustiveExamplesInterface.decodeFunctionResult(
'struct_simple',
'0x01000000000000000000000000000020'
);
expect(data).toEqual({
a: true,
b: 32,
});
});

it('should throw an error when function does not exist', () => {
expect(() => {
exhaustiveExamplesInterface.decodeFunctionResult(
'doesnt_exist',
'0x01000000000000000000000000000020'
);
}).toThrowError(/^Function doesnt_exist not found\.$/);
Copy link
Contributor

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).

});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ abi MyContract {
arg3: (str[5], bool),
arg4: MyOtherStruct,
);

fn log();
}

impl MyContract for Contract {
Expand Down Expand Up @@ -339,4 +341,11 @@ impl MyContract for Contract {
fn simple_vector(arg: Vec<u8>) -> Vec<u8> {
arg
}

fn log() {
log(SimpleStruct {
a: true,
b: 32u32,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function getDecodedLogs<T = unknown>(
? new BigNumberCoder('u64').encode(receipt.val0)
: receipt.data;

const [decodedLog] = interfaceToUse.decodeLog(data, receipt.val1.toString());
const decodedLog = interfaceToUse.decodeLog(data, receipt.val1.toString());
logs.push(decodedLog);
}

Expand Down