Skip to content

Commit

Permalink
feat!: deprecate multiple encoding version support for configurable c…
Browse files Browse the repository at this point in the history
…onstants (#2040)

* feat: deprecate v1 encoding support for configurables

* test: multiple encoding version support for params and configurables

* chore: changeset

* chore: update changeset

* chore: lint

* chore: forc format

* feat: use v0 encoding constant for encode configuralbes

* chore: add doc for configurable encoding support

Co-authored-by: Anderson Arboleya <anderson@arboleya.me>

* chore: remove fuel gauge factory helper functions

---------

Co-authored-by: Anderson Arboleya <anderson@arboleya.me>
  • Loading branch information
danielbate and arboleya authored Apr 11, 2024
1 parent 3e60d34 commit b51dc5e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-rice-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/abi-coder": minor
---

feat!: deprecate multiple encoding version support for configurable constants
4 changes: 3 additions & 1 deletion packages/abi-coder/src/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AbiCoder } from './AbiCoder';
import { FunctionFragment } from './FunctionFragment';
import type { InputValue } from './encoding/coders/AbstractCoder';
import type { JsonAbi, JsonAbiConfigurable } from './types/JsonAbi';
import { ENCODING_V0 } from './utils/constants';
import { findTypeById } from './utils/json-abi';

export class Interface<TAbi extends JsonAbi = JsonAbi> {
Expand Down Expand Up @@ -99,7 +100,8 @@ export class Interface<TAbi extends JsonAbi = JsonAbi> {

return AbiCoder.encode(this.jsonAbi, configurable.configurableType, value, {
isRightPadded: true,
encoding: this.jsonAbi.encoding,
// TODO: Review support for configurables in v1 encoding when it becomes available
encoding: ENCODING_V0,
});
}

Expand Down
24 changes: 23 additions & 1 deletion packages/fuel-gauge/src/experimental-contract.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { readFileSync } from 'fs';
import type { Contract } from 'fuels';
import { bn } from 'fuels';
import { ContractFactory, bn } from 'fuels';
import { join } from 'path';

import { setup } from './utils';

let contractInstance: Contract;
let contractFactory: ContractFactory;

const U8_MAX = 2 ** 8 - 1;
const U16_MAX = 2 ** 16 - 1;
Expand All @@ -29,6 +30,7 @@ describe('Experimental Contract', () => {
const abi = JSON.parse(readFileSync(`${path}-abi.json`, 'utf8'));

contractInstance = await setup({ contractBytecode, abi });
contractFactory = new ContractFactory(contractBytecode, abi, contractInstance.account);
});

it('echos mixed struct with all types', async () => {
Expand Down Expand Up @@ -86,4 +88,24 @@ describe('Experimental Contract', () => {
'The transaction reverted because a "require" statement has thrown "This is a revert error".'
);
});

it('echos configurable (both encoding versions)', async () => {
const param = [1, 2, 3, 'four'];
const { value } = await contractInstance.functions.echo_configurable(param).call();

expect(value).toStrictEqual(param);

const configurableParam = [5, 6, 7, 'fuel'];
const configurableConstants = {
CONF: configurableParam,
};
const configurableContractInstance = await contractFactory.deployContract({
configurableConstants,
});
const { value: configurableValue } = await configurableContractInstance.functions
.echo_configurable(configurableParam)
.call();

expect(configurableValue).toStrictEqual(configurableParam);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,14 @@ struct MixedStruct {
str_slice: str,
}

configurable {
CONF: (u8, u16, u32, str[4]) = (1, 2, 3, __to_str_array("four")),
}

abi MyContract {
fn echo_struct(param: MixedStruct) -> MixedStruct;
fn test_revert() -> bool;
fn echo_configurable(param: (u8, u16, u32, str[4])) -> (u8, u16, u32, str[4]);
}

impl MyContract for Contract {
Expand All @@ -79,4 +84,16 @@ impl MyContract for Contract {
require(false, "This is a revert error");
true
}

fn echo_configurable(param: (u8, u16, u32, str[4])) -> (u8, u16, u32, str[4]) {
assert_eq(param.0, CONF.0);
assert_eq(param.1, CONF.1);
assert_eq(param.2, CONF.2);

let param_str: str = from_str_array(param.3);
let conf_str: str = from_str_array(CONF.3);
assert_eq(param_str, conf_str);

CONF
}
}

0 comments on commit b51dc5e

Please sign in to comment.