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

fix: make properties of configurables optional in typegen #2815

Merged
merged 10 commits into from
Jul 23, 2024
5 changes: 5 additions & 0 deletions .changeset/tasty-fireants-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/abi-typegen": patch
---

fix: make properties of configurables optional in typegen
4 changes: 2 additions & 2 deletions packages/abi-typegen/src/templates/contract/dts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ export type {{structName}}Output{{typeAnnotations}} = { {{outputValues}} };
{{/each}}

{{#if formattedConfigurables}}
export type {{capitalizedName}}Configurables = {
export type {{capitalizedName}}Configurables = Partial<{
{{#each formattedConfigurables}}
{{configurableName}}: {{configurableType}};
{{/each}}
};
}>;
{{/if}}

interface {{capitalizedName}}Interface extends Interface {
Expand Down
4 changes: 2 additions & 2 deletions packages/abi-typegen/src/templates/predicate/factory.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ export type {{structName}}Output{{typeAnnotations}} = { {{outputValues}} };
{{/if}}
{{/each}}

export type {{capitalizedName}}Configurables = {
export type {{capitalizedName}}Configurables = Partial<{
{{#each formattedConfigurables}}
{{configurableName}}: {{configurableType}};
{{/each}}
};
}>;

export type {{capitalizedName}}Inputs = [{{inputs}}];

Expand Down
4 changes: 2 additions & 2 deletions packages/abi-typegen/src/templates/script/factory.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ type {{capitalizedName}}Inputs = [{{inputs}}];
type {{capitalizedName}}Output = {{output}};

{{#if formattedConfigurables}}
export type {{capitalizedName}}Configurables = {
export type {{capitalizedName}}Configurables = Partial<{
{{#each formattedConfigurables}}
{{configurableName}}: {{configurableType}};
{{/each}}
};
}>;
{{/if}}

const _abi = {{abiJsonString}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import type {
InvokeFunction,
} from 'fuels';

export type MyContractAbiConfigurables = {
export type MyContractAbiConfigurables = Partial<{
SHOULD_RETURN: boolean;
};
}>;

interface MyContractAbiInterface extends Interface {
functions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import {
Provider,
} from 'fuels';

export type MyPredicateAbiConfigurables = {
export type MyPredicateAbiConfigurables = Partial<{
FEE: BigNumberish;
ADDRESS: string;
};
}>;

export type MyPredicateAbiInputs = [fee: BigNumberish, address: string];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export type MyGenericStructOutput<T> = MyGenericStructInput<T>;
export type ValidationInput = { has_account: boolean, total_complete: BigNumberish };
export type ValidationOutput = { has_account: boolean, total_complete: BN };

export type MyPredicateAbiConfigurables = {
};
export type MyPredicateAbiConfigurables = Partial<{
}>;

export type MyPredicateAbiInputs = [vec: Vec<ValidationInput>, enm: MyGenericEnumInput<BigNumberish>, opt: Option<BigNumberish>, res: Result<MyGenericStructInput<string>, BigNumberish>];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export type ScoreOutput = { user: number, points: number };
type MyScriptAbiInputs = [score: ScoreInput];
type MyScriptAbiOutput = boolean;

export type MyScriptAbiConfigurables = {
export type MyScriptAbiConfigurables = Partial<{
SHOULD_RETURN: boolean;
};
}>;

const _abi = {
"encoding": "1",
Expand Down
8 changes: 4 additions & 4 deletions packages/fuel-gauge/src/policies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ describe('Policies', () => {
} = launched;

const factory = new ContractFactory(
ScriptMainArgsAbi__factory.bin,
ScriptMainArgsAbi__factory.abi,
PayableAnnotationAbiHex,
maschad marked this conversation as resolved.
Show resolved Hide resolved
PayableAnnotationAbi__factory.abi,
wallet
);

Expand Down Expand Up @@ -445,8 +445,8 @@ describe('Policies', () => {
const maxFee = 1;

const factory = new ContractFactory(
ScriptMainArgsAbi__factory.bin,
ScriptMainArgsAbi__factory.abi,
PayableAnnotationAbiHex,
PayableAnnotationAbi__factory.abi,
wallet
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,48 @@ describe('Predicate', () => {
await assertBalance(destination, amountToTransfer, provider.getBaseAssetId());
});

it('calls a predicate with partial configurables being set', async () => {
using launched = await launchTestNode();

const {
provider,
wallets: [wallet],
} = launched;

const configurableConstants = {
ADDRESS: getRandomB256(),
};

const amountToTransfer = 300;

const predicate = PredicateWithConfigurableAbi__factory.createInstance(
provider,
[defaultValues.FEE, configurableConstants.ADDRESS],
configurableConstants
);

const destination = WalletUnlocked.generate({
provider: wallet.provider,
});

await assertBalance(destination, 0, provider.getBaseAssetId());

await fundPredicate(wallet, predicate, amountToPredicate);

const tx = await predicate.transfer(
destination.address,
amountToTransfer,
provider.getBaseAssetId(),
{
gasLimit: 1000,
}
);

await tx.waitForResult();

await assertBalance(destination, amountToTransfer, provider.getBaseAssetId());
});

it('throws when configurable data is not set', async () => {
using launched = await launchTestNode();

Expand Down
9 changes: 5 additions & 4 deletions packages/fuel-gauge/src/script-with-configurable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import { Script } from 'fuels';
import { launchTestNode } from 'fuels/test-utils';

import { ScriptWithConfigurableAbi__factory } from '../test/typegen';
import type { ScriptWithConfigurableAbiConfigurables } from '../test/typegen/scripts/factories/ScriptWithConfigurableAbi__factory';

const defaultValues = {
const defaultValues: ScriptWithConfigurableAbiConfigurables = {
FEE: 5,
};

Expand Down Expand Up @@ -40,7 +41,7 @@ describe('Script With Configurable', () => {
wallets: [wallet],
} = launched;

const configurableConstants = { FEE: 71 };
const configurableConstants: ScriptWithConfigurableAbiConfigurables = { FEE: 71 };

expect(configurableConstants.FEE).not.toEqual(defaultValues.FEE);

Expand All @@ -65,7 +66,7 @@ describe('Script With Configurable', () => {
wallets: [wallet],
} = launched;

const configurableConstants = { FEE: 35 };
const configurableConstants: ScriptWithConfigurableAbiConfigurables = { FEE: 35 };

const script = new Script(
ScriptWithConfigurableAbi__factory.bin,
Expand All @@ -88,7 +89,7 @@ describe('Script With Configurable', () => {
wallets: [wallet],
} = launched;

const configurableConstants = { FEE: 10 };
const configurableConstants: ScriptWithConfigurableAbiConfigurables = { FEE: 10 };

const input = { FEE: 15 };

Expand Down
Loading