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!: deprecate multiple encoding version support for configurable constants #2040

Merged
merged 10 commits into from
Apr 11, 2024
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
1 change: 0 additions & 1 deletion packages/abi-coder/src/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export class Interface<TAbi extends JsonAbi = JsonAbi> {

return AbiCoder.encode(this.jsonAbi, configurable.configurableType, value, {
isRightPadded: true,
encoding: this.jsonAbi.encoding,
petertonysmith94 marked this conversation as resolved.
Show resolved Hide resolved
});
}

Expand Down
26 changes: 24 additions & 2 deletions 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 type { Contract, ContractFactory } from 'fuels';
import { bn } from 'fuels';
import { join } from 'path';

import { setup } from './utils';
import { setup, setupFactory } 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 = await setupFactory({ contractBytecode, abi });
});

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);
});
});
11 changes: 9 additions & 2 deletions packages/fuel-gauge/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ let contractInstance: Contract;
const deployContract = async (
factory: ContractFactory,
provider: Provider,
useCache: boolean = true
useCache: boolean = true,
configurableConstants?: { [name: string]: unknown }
) => {
if (contractInstance && useCache) {
return contractInstance;
}
const { minGasPrice } = provider.getGasConfig();
contractInstance = await factory.deployContract({ gasPrice: minGasPrice });
contractInstance = await factory.deployContract({ configurableConstants, gasPrice: minGasPrice });
return contractInstance;
};

Expand All @@ -36,6 +37,7 @@ export type SetupConfig = {
contractBytecode: BytesLike;
abi: JsonAbi | Interface;
cache?: boolean;
configurableConstants?: { [name: string]: unknown };
arboleya marked this conversation as resolved.
Show resolved Hide resolved
};

export const setup = async ({ contractBytecode, abi, cache }: SetupConfig) => {
petertonysmith94 marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -46,6 +48,11 @@ export const setup = async ({ contractBytecode, abi, cache }: SetupConfig) => {
return contract;
};

export const setupFactory = async ({ contractBytecode, abi }: SetupConfig) => {
const wallet = await createWallet();
return new ContractFactory(contractBytecode, abi, wallet);
};

export const createSetupConfig =
(defaultConfig: SetupConfig) => async (config?: Partial<SetupConfig>) =>
setup({
Expand Down
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
}
}
Loading