Skip to content

Commit

Permalink
feat: partial support of str slice
Browse files Browse the repository at this point in the history
  • Loading branch information
danielbate committed Jan 25, 2024
1 parent 013971e commit 6227dc7
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
29 changes: 29 additions & 0 deletions packages/abi-coder/src/coders/v1/str-slice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import { bn } from '@fuel-ts/math';
import { toUtf8String } from 'ethers';

import { WORD_SIZE } from '../../constants';
import { Coder } from '../abstract-coder';
import { U64Coder } from '../v0/u64';

export class StrSliceCoder extends Coder<number[], string> {
static memorySize = 1;
constructor() {
super('strSlice', 'str', 1);
}

encode(_value: number[]): Uint8Array {
throw new FuelError(ErrorCode.ENCODE_ERROR, `Bytes encode unsupported in v1`);
return new Uint8Array();
}

decode(data: Uint8Array, offset: number): [string, number] {
const offsetAndLength = offset + WORD_SIZE;
const lengthBytes = data.slice(offset, offsetAndLength);
const length = bn(new U64Coder().decode(lengthBytes, 0)[0]).toNumber();
const bytes = data.slice(offset, offsetAndLength + length);
const value = toUtf8String(bytes);

return [value, offsetAndLength + length];
}
}
12 changes: 12 additions & 0 deletions packages/fuel-gauge/src/experimental-logging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,16 @@ describe('Experimental Logging', () => {
expect(logsSome).toEqual([expectedSome]);
expect(logsNone).toEqual([expectedNone]);
});

// Requires v1 encoding to be supported for contract calls
it.skip('logs str slice', async () => {
const expected = 'fuel';

const { logs } = await contractInstance.functions
.log_str_slice(expected)
.txParams({ gasPrice, gasLimit: 10_000 })
.call();

expect(logs).toEqual([expected]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ abi LoggingContract {
fn log_struct_boolean(a: MyStruct, b: bool) -> (MyStruct, bool);
fn log_option_u8(a: Option<u8>) -> Option<u8>;
fn log_option_vec_u16(a: Option<Vec<u16>>) -> Option<Vec<u16>>;
fn log_str_slice(a: str) -> str;
}

impl LoggingContract for Contract {
Expand Down Expand Up @@ -248,4 +249,9 @@ impl LoggingContract for Contract {
log(a);
a
}

fn log_str_slice(a: str) -> str {
log(a);
a
}
}

0 comments on commit 6227dc7

Please sign in to comment.