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

feat!: remove offset from decodeLog and decodeFunctionResult methods #2514

Closed
wants to merge 16 commits into from
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
---

feat!: remove offset from `decodeLog` and `decodeFunctionResult` methods
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.`
);
}

Expand All @@ -54,7 +54,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 @@ -68,7 +68,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,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 {
Expand Down Expand Up @@ -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.`)
);
});
});
});
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