diff --git a/.changeset/perfect-kings-camp.md b/.changeset/perfect-kings-camp.md new file mode 100644 index 0000000000..904c52a665 --- /dev/null +++ b/.changeset/perfect-kings-camp.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/abi-coder": minor +--- + +feat!: remove offset from `decodeLog` and `decodeFunctionResult` methods diff --git a/packages/abi-coder/src/Interface.ts b/packages/abi-coder/src/Interface.ts index c2f9404bb6..5f1f60939f 100644 --- a/packages/abi-coder/src/Interface.ts +++ b/packages/abi-coder/src/Interface.ts @@ -45,7 +45,7 @@ export class Interface { throw new FuelError( ErrorCode.FUNCTION_NOT_FOUND, - `function ${nameOrSignatureOrSelector} not found: ${JSON.stringify(fn)}.` + `Function ${nameOrSignatureOrSelector} not found.` ); } @@ -54,7 +54,7 @@ export class Interface { const fragment = typeof functionFragment === 'string' ? this.getFunction(functionFragment) : functionFragment; - return fragment.decodeOutput(data); + return fragment.decodeOutput(data)[0]; } decodeLog(data: BytesLike, logId: string): any { @@ -68,7 +68,7 @@ export class Interface { return AbiCoder.decode(this.jsonAbi, loggedType.loggedType, arrayify(data), 0, { encoding: this.encoding, - }); + })[0]; } encodeConfigurable(name: string, value: InputValue) { diff --git a/packages/abi-coder/test/Interface.test.ts b/packages/abi-coder/test/Interface.test.ts index 3d27d49a9f..f35c7a8267 100644 --- a/packages/abi-coder/test/Interface.test.ts +++ b/packages/abi-coder/test/Interface.test.ts @@ -1,10 +1,13 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ +import { randomBytes } from '@fuel-ts/crypto'; +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 { Interface } from '../src'; /** @knipignore */ -import type { JsonAbiConfigurable } from '../src/json-abi'; +import type { JsonAbiConfigurable } from '../src/types/JsonAbi'; import exhaustiveExamplesAbi from './fixtures/forc-projects/exhaustive-examples/out/release/exhaustive-examples-abi.json'; import { @@ -748,4 +751,56 @@ describe('Abi interface', () => { ); }); }); + + describe('decodeLog', () => { + it('should return decoded log by id', () => { + const data = exhaustiveExamplesInterface.decodeLog( + Uint8Array.from([1, 0, 0, 0, 32]), + '8500535089865083573' + ); + expect(data).toEqual({ + a: true, + b: 32, + }); + }); + + it('should throw an error when log does not exist', async () => { + const randomLogId = randomBytes(20).join(''); + await expectToThrowFuelError( + () => { + exhaustiveExamplesInterface.decodeLog(Uint8Array.from([1, 0, 0, 0, 32]), randomLogId); + }, + new FuelError( + ErrorCode.LOG_TYPE_NOT_FOUND, + `Log type with logId '${randomLogId}' doesn't exist in the ABI.` + ) + ); + }); + }); + + describe('decodeFunctionResult', () => { + it('should return decoded function result', () => { + const data = exhaustiveExamplesInterface.decodeFunctionResult( + 'struct_simple', + 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', + Uint8Array.from([1, 0, 0, 0, 32]) + ); + }, + new FuelError(ErrorCode.FUNCTION_NOT_FOUND, `Function doesnt_exist not found.`) + ); + }); + }); }); diff --git a/packages/abi-coder/test/fixtures/forc-projects/exhaustive-examples/src/main.sw b/packages/abi-coder/test/fixtures/forc-projects/exhaustive-examples/src/main.sw index abed2d93ca..2d9c79bbf7 100644 --- a/packages/abi-coder/test/fixtures/forc-projects/exhaustive-examples/src/main.sw +++ b/packages/abi-coder/test/fixtures/forc-projects/exhaustive-examples/src/main.sw @@ -181,6 +181,8 @@ abi MyContract { arg3: (str[5], bool), arg4: MyOtherStruct, ); + + fn log(); } impl MyContract for Contract { @@ -339,4 +341,11 @@ impl MyContract for Contract { fn simple_vector(arg: Vec) -> Vec { arg } + + fn log() { + log(SimpleStruct { + a: true, + b: 32u32, + }); + } } diff --git a/packages/account/src/providers/transaction-response/getDecodedLogs.ts b/packages/account/src/providers/transaction-response/getDecodedLogs.ts index bbea779d33..65205c8737 100644 --- a/packages/account/src/providers/transaction-response/getDecodedLogs.ts +++ b/packages/account/src/providers/transaction-response/getDecodedLogs.ts @@ -35,7 +35,7 @@ export function getDecodedLogs( ? 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); }