Skip to content

Commit

Permalink
refact vars name
Browse files Browse the repository at this point in the history
  • Loading branch information
Torres-ssf committed May 10, 2023
1 parent f713629 commit 25f1bdd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe(__filename, () => {

it('should successfully set new values for all configurable constants', async () => {
// #region configurable-constants-2
const configurablesConstants: typeof defaultValues = {
const configurableConstants: typeof defaultValues = {
age: 30,
tag: 'leuf',
grades: [10, 9, 8, 9],
Expand All @@ -39,47 +39,51 @@ describe(__filename, () => {

const factory = new ContractFactory(bin, abi, wallet);

const contract = await factory.deployContract({ configurablesConstants });
const contract = await factory.deployContract({
configurableConstants,
});
// #endregion configurable-constants-2

const { value } = await contract.functions.echo_configurables().get();

expect(value[0]).toEqual(configurablesConstants.age);
expect(value[1]).toEqual(configurablesConstants.tag);
expect(value[2]).toStrictEqual(configurablesConstants.grades);
expect(value[3]).toStrictEqual(configurablesConstants.my_struct);
expect(value[0]).toEqual(configurableConstants.age);
expect(value[1]).toEqual(configurableConstants.tag);
expect(value[2]).toStrictEqual(configurableConstants.grades);
expect(value[3]).toStrictEqual(configurableConstants.my_struct);
});

it('should successfully set new value for one configurable constants', async () => {
it('should successfully set new value for one configurable constant', async () => {
// #region configurable-constants-3
const configurablesConstants = {
const configurableConstants = {
age: 10,
};

const factory = new ContractFactory(bin, abi, wallet);

const contract = await factory.deployContract({ configurablesConstants });
const contract = await factory.deployContract({
configurableConstants,
});
// #endregion configurable-constants-3

const { value } = await contract.functions.echo_configurables().get();

expect(value[0]).toEqual(configurablesConstants.age);
expect(value[0]).toEqual(configurableConstants.age);
expect(value[1]).toEqual(defaultValues.tag);
expect(value[2]).toEqual(defaultValues.grades);
expect(value[3]).toEqual(defaultValues.my_struct);
});

it('should throw when not properly setting new values for structs', async () => {
// #region configurable-constants-4
const configurablesConstants = {
const configurableConstants = {
my_struct: {
x: 2,
},
};

const factory = new ContractFactory(bin, abi, wallet);

await expect(factory.deployContract({ configurablesConstants })).rejects.toThrowError();
await expect(factory.deployContract({ configurableConstants })).rejects.toThrowError();
// #endregion configurable-constants-4
});
});
14 changes: 7 additions & 7 deletions packages/contract/src/contract-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type DeployContractOptions = {
salt?: BytesLike;
storageSlots?: StorageSlot[];
stateRoot?: BytesLike;
configurablesConstants?: { [name: string]: unknown };
configurableConstants?: { [name: string]: unknown };
} & CreateTransactionRequestLike;

export default class ContractFactory {
Expand Down Expand Up @@ -109,10 +109,10 @@ export default class ContractFactory {
);
}

const { configurablesConstants } = deployContractOptions;
const { configurableConstants } = deployContractOptions;

if (configurablesConstants) {
this.setConfigurableConstants(configurablesConstants);
if (configurableConstants) {
this.setConfigurableConstants(configurableConstants);
}

const { contractId, transactionRequest } = this.createTransactionRequest(deployContractOptions);
Expand All @@ -123,15 +123,15 @@ export default class ContractFactory {
return new Contract(contractId, this.interface, this.account);
}

setConfigurableConstants(configurablesConstants: { [name: string]: unknown }) {
setConfigurableConstants(configurableConstants: { [name: string]: unknown }) {
try {
const hasConfigurable = Object.keys(this.interface.configurables).length;

if (!hasConfigurable) {
throw new Error('Contract has no configurables to be set');
}

Object.entries(configurablesConstants).forEach(([key, value]) => {
Object.entries(configurableConstants).forEach(([key, value]) => {
if (!this.interface.configurables[key]) {
throw new Error(`Contract has no configurable named: ${key}`);
}
Expand All @@ -151,7 +151,7 @@ export default class ContractFactory {
} catch (err) {
logger.throwError('Error setting configurables', Logger.errors.INVALID_ARGUMENT, {
error: err,
configurablesConstants,
configurableConstants,
});
}
}
Expand Down
88 changes: 44 additions & 44 deletions packages/fuel-gauge/src/configurable-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,163 +69,163 @@ describe('Configurable Contract', () => {
});

it('should set configurable constant before deploy contract (U8)', async () => {
const configurablesConstants = {
const configurableConstants = {
U8: 99,
};

expect(defaultValues.U8).not.toBe(configurablesConstants.U8);
expect(defaultValues.U8).not.toBe(configurableConstants.U8);

const contract = await factory.deployContract({ configurablesConstants });
const contract = await factory.deployContract({ configurableConstants });

const { value } = await contract.functions.echo_u8().get();

expect(value).toBe(configurablesConstants.U8);
expect(value).toBe(configurableConstants.U8);
});

it('should set configurable constant before deploy contract (U16)', async () => {
const configurablesConstants = {
const configurableConstants = {
U16: 499,
};

expect(defaultValues.U16).not.toBe(configurablesConstants.U16);
expect(defaultValues.U16).not.toBe(configurableConstants.U16);

const contract = await factory.deployContract({ configurablesConstants });
const contract = await factory.deployContract({ configurableConstants });

const { value } = await contract.functions.echo_u16().get();

expect(value).toBe(configurablesConstants.U16);
expect(value).toBe(configurableConstants.U16);
});

it('should set configurable constant before deploy contract (U32)', async () => {
const configurablesConstants = {
const configurableConstants = {
U32: 854,
};

expect(defaultValues.U32).not.toBe(configurablesConstants.U32);
expect(defaultValues.U32).not.toBe(configurableConstants.U32);

const contract = await factory.deployContract({ configurablesConstants });
const contract = await factory.deployContract({ configurableConstants });

const { value } = await contract.functions.echo_u32().get();

expect(value).toBe(configurablesConstants.U32);
expect(value).toBe(configurableConstants.U32);
});

it('should set configurable constant before deploy contract (U64)', async () => {
const configurablesConstants = {
const configurableConstants = {
U64: 999999,
};

expect(defaultValues.U64).not.toBe(configurablesConstants.U64);
expect(defaultValues.U64).not.toBe(configurableConstants.U64);

const contract = await factory.deployContract({ configurablesConstants });
const contract = await factory.deployContract({ configurableConstants });

const { value } = await contract.functions.echo_u64().get();

expect(new BN(value).toNumber()).toBe(configurablesConstants.U64);
expect(new BN(value).toNumber()).toBe(configurableConstants.U64);
});

it('should set configurable constant before deploy contract (BOOL)', async () => {
const configurablesConstants = {
const configurableConstants = {
BOOL: false,
};

expect(defaultValues.BOOL).not.toBe(configurablesConstants.BOOL);
expect(defaultValues.BOOL).not.toBe(configurableConstants.BOOL);

const contract = await factory.deployContract({ configurablesConstants });
const contract = await factory.deployContract({ configurableConstants });

const { value } = await contract.functions.echo_bool().get();

expect(value).toBe(configurablesConstants.BOOL);
expect(value).toBe(configurableConstants.BOOL);
});

it('should set configurable constant before deploy contract (B256)', async () => {
const configurablesConstants = {
const configurableConstants = {
B256: getRandomB256(),
};

expect(defaultValues.B256).not.toBe(configurablesConstants.B256);
expect(defaultValues.B256).not.toBe(configurableConstants.B256);

const contract = await factory.deployContract({ configurablesConstants });
const contract = await factory.deployContract({ configurableConstants });

const { value } = await contract.functions.echo_b256().get();

expect(value).toBe(configurablesConstants.B256);
expect(value).toBe(configurableConstants.B256);
});

it('should set configurable constant before deploy contract (ENUM)', async () => {
const configurablesConstants = {
const configurableConstants = {
ENUM: 'blue',
};

expect(defaultValues.ENUM).not.toBe(configurablesConstants.ENUM);
expect(defaultValues.ENUM).not.toBe(configurableConstants.ENUM);

const contract = await factory.deployContract({ configurablesConstants });
const contract = await factory.deployContract({ configurableConstants });

const { value } = await contract.functions.echo_enum().get();

expect(value).toBe(configurablesConstants.ENUM);
expect(value).toBe(configurableConstants.ENUM);
});

it('should set configurable constant before deploy contract (ARRAY)', async () => {
const configurablesConstants = {
const configurableConstants = {
ARRAY: [
[666, 667],
[656, 657],
],
};

expect(defaultValues.ARRAY).not.toStrictEqual(configurablesConstants.ARRAY);
expect(defaultValues.ARRAY).not.toStrictEqual(configurableConstants.ARRAY);

const contract = await factory.deployContract({ configurablesConstants });
const contract = await factory.deployContract({ configurableConstants });

const { value } = await contract.functions.echo_array().get();

expect(value).toStrictEqual(configurablesConstants.ARRAY);
expect(value).toStrictEqual(configurableConstants.ARRAY);
});

it('should set configurable constant before deploy contract (STR_4)', async () => {
const configurablesConstants = {
const configurableConstants = {
STR_4: 'leuf',
};

expect(defaultValues.STR_4).not.toBe(configurablesConstants.STR_4);
expect(defaultValues.STR_4).not.toBe(configurableConstants.STR_4);

const contract = await factory.deployContract({ configurablesConstants });
const contract = await factory.deployContract({ configurableConstants });

const { value } = await contract.functions.echo_str4().get();

expect(value).toBe(configurablesConstants.STR_4);
expect(value).toBe(configurableConstants.STR_4);
});

it('should set configurable constant before deploy contract (TUPLE)', async () => {
const configurablesConstants = {
const configurableConstants = {
TUPLE: [99, true, 'by'],
};

expect(defaultValues.TUPLE).not.toStrictEqual(configurablesConstants.TUPLE);
expect(defaultValues.TUPLE).not.toStrictEqual(configurableConstants.TUPLE);

const contract = await factory.deployContract({ configurablesConstants });
const contract = await factory.deployContract({ configurableConstants });

const { value } = await contract.functions.echo_tuple().get();

expect(value).toStrictEqual(configurablesConstants.TUPLE);
expect(value).toStrictEqual(configurableConstants.TUPLE);
});

it('should set configurable constant before deploy contract (STRUCT)', async () => {
const configurablesConstants = {
const configurableConstants = {
STRUCT_1: {
tag: '007',
age: 30,
scores: [10, 10, 10],
},
};

expect(defaultValues.STRUCT_1).not.toStrictEqual(configurablesConstants.STRUCT_1);
expect(defaultValues.STRUCT_1).not.toStrictEqual(configurableConstants.STRUCT_1);

const contract = await factory.deployContract({ configurablesConstants });
const contract = await factory.deployContract({ configurableConstants });

const { value } = await contract.functions.echo_struct().get();

expect(value).toStrictEqual(configurablesConstants.STRUCT_1);
expect(value).toStrictEqual(configurableConstants.STRUCT_1);
});
});

0 comments on commit 25f1bdd

Please sign in to comment.