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 all 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
5 changes: 5 additions & 0 deletions .changeset/perfect-kings-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/abi-coder": minor
---

fix: Changed decodeLog and decodeFunctionResult return types to have only decoded value without an offset
Copy link
Contributor

@petertonysmith94 petertonysmith94 May 27, 2024

Choose a reason for hiding this comment

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

Suggested change
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 😄

6 changes: 3 additions & 3 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,7 +71,7 @@ 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 {
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
57 changes: 56 additions & 1 deletion packages/abi-coder/test/Interface.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils';
import type { BN } from '@fuel-ts/math';
import { concat } from '@fuel-ts/utils';
import { concat, hexlify } from '@fuel-ts/utils';

import { Interface } from '../src';
/** @knipignore */
Expand Down Expand Up @@ -766,4 +768,57 @@ describe('Abi interface', () => {
);
});
});

describe('decodeLog', () => {
it('should return decoded log by id', () => {
const data = exhaustiveExamplesInterface.decodeLog(
hexlify(Uint8Array.from([1, 0, 0, 0, 32])),
'8500535089865083573'
);
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', async () => {
await expectToThrowFuelError(
() => {
exhaustiveExamplesInterface.decodeLog(
hexlify(Uint8Array.from([1, 0, 0, 0, 32])),
'8500535089865083573'
);
},
new FuelError(
ErrorCode.LOG_TYPE_NOT_FOUND,
`Log type with logId '8500535089865083573' doesn't exist in the ABI.`
)
);
});
});

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

it('should throw an error when function does not exist', async () => {
await expectToThrowFuelError(
() => {
exhaustiveExamplesInterface.decodeFunctionResult(
'doesnt_exist',
hexlify(Uint8Array.from([1, 0, 0, 0, 32]))
);
},
new FuelError(ErrorCode.FUNCTION_NOT_FOUND, `Function doesnt_exist not found.`)
);
});
});
});
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
Loading