From 6ab3e6b15c21d87eda901beac0605b6f8a15a9db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nedim=20Salki=C4=87?= Date: Tue, 19 Nov 2024 08:09:26 +0100 Subject: [PATCH] feat: better typegen contract factory integration with `launchTestNode` (#3398) * feat: better typegen contract factory integration with `launchTestNode` * give name to type * remove type assertion to improve inference * update docs to new approach * changeset * revert unnecessary change * rerun workflows * revert change * give name to intermediary type --------- Co-authored-by: Chad Nehemiah --- .changeset/twenty-needles-buy.md | 5 ++ .../src/testing/launching-a-test-node.ts | 2 +- .../src/test-utils/launch-test-node.test.ts | 32 ++++++++-- .../src/test-utils/launch-test-node.ts | 58 ++++++++++++------- packages/fuel-gauge/src/contract.test.ts | 14 +---- .../fuel-gauge/src/coverage-contract.test.ts | 4 +- 6 files changed, 72 insertions(+), 43 deletions(-) create mode 100644 .changeset/twenty-needles-buy.md diff --git a/.changeset/twenty-needles-buy.md b/.changeset/twenty-needles-buy.md new file mode 100644 index 00000000000..1d73fa27fd9 --- /dev/null +++ b/.changeset/twenty-needles-buy.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/contract": patch +--- + +feat: better typegen contract factory integration with `launchTestNode` diff --git a/apps/docs-snippets2/src/testing/launching-a-test-node.ts b/apps/docs-snippets2/src/testing/launching-a-test-node.ts index ac89dcb517b..763b2638dd4 100644 --- a/apps/docs-snippets2/src/testing/launching-a-test-node.ts +++ b/apps/docs-snippets2/src/testing/launching-a-test-node.ts @@ -38,7 +38,7 @@ customLaunchTestNode.cleanup(); import { CounterFactory } from '../typegend/contracts/CounterFactory'; using launchedContractNode = await launchTestNode({ - contractsConfigs: [{ factory: CounterFactory }], + contractsConfigs: [CounterFactory], }); const { diff --git a/packages/contract/src/test-utils/launch-test-node.test.ts b/packages/contract/src/test-utils/launch-test-node.test.ts index 0b4949ca1a7..bb729be49da 100644 --- a/packages/contract/src/test-utils/launch-test-node.test.ts +++ b/packages/contract/src/test-utils/launch-test-node.test.ts @@ -136,15 +136,37 @@ describe('launchTestNode', () => { expect(response.value).toBe(true); }); + test('a contract can be deployed by passing the static factory method directly', async () => { + using launched = await launchTestNode({ + contractsConfigs: [ + { + deploy: async (wallet, options) => { + const factory = new ContractFactory(binHexlified, abiContents, wallet); + return factory.deploy(options); + }, + }, + ], + }); + + const { + contracts: [contract], + wallets: [wallet], + } = launched; + + const { waitForResult } = await contract.functions.test_function().call(); + const response = await waitForResult(); + expect(response.value).toBe(true); + + expect(contract.account).toEqual(wallet); + }); + test('multiple contracts can be deployed', async () => { using launched = await launchTestNode({ contractsConfigs: [ { - factory: { - deploy: async (wallet, options) => { - const factory = new ContractFactory(binHexlified, abiContents, wallet); - return factory.deploy(options); - }, + deploy: async (wallet, options) => { + const factory = new ContractFactory(binHexlified, abiContents, wallet); + return factory.deploy(options); }, }, { diff --git a/packages/contract/src/test-utils/launch-test-node.ts b/packages/contract/src/test-utils/launch-test-node.ts index 5cc4fe34949..00c2d902e61 100644 --- a/packages/contract/src/test-utils/launch-test-node.ts +++ b/packages/contract/src/test-utils/launch-test-node.ts @@ -16,20 +16,22 @@ export interface DeployableContractFactory { deploy(wallet: Account, options?: DeployContractOptions): Promise; } -export interface DeployContractConfig { - /** - * Contract factory class outputted by `pnpm fuels typegen`. - */ - factory: DeployableContractFactory; - /** - * Options for contract deployment taken from `ContractFactory`. - */ - options?: DeployContractOptions; - /** - * Index of wallet to be used for deployment. Defaults to `0` (first wallet). - */ - walletIndex?: number; -} +export type DeployContractConfig = + | DeployableContractFactory + | { + /** + * Contract factory class outputted by `pnpm fuels typegen`. + */ + factory: DeployableContractFactory; + /** + * Options for contract deployment taken from `ContractFactory`. + */ + options?: DeployContractOptions; + /** + * Index of wallet to be used for deployment. Defaults to `0` (first wallet). + */ + walletIndex?: number; + }; export interface LaunchTestNodeOptions extends LaunchCustomProviderAndGetWalletsOptions { @@ -38,10 +40,23 @@ export interface LaunchTestNodeOptions + >['waitForResult'], +> = Awaited>['contract']; + export type TContracts = { - [K in keyof T]: Awaited< - ReturnType>['waitForResult']> - >['contract']; + [K in keyof T]: ExtractDeployedContract; }; export interface LaunchTestNodeReturn extends SetupTestProviderAndWalletsReturn { @@ -105,7 +120,7 @@ function getFuelCoreArgs( } function getWalletForDeployment(config: DeployContractConfig, wallets: WalletUnlocked[]) { - if (!config.walletIndex) { + if (!('walletIndex' in config) || !config.walletIndex) { return wallets[0]; } @@ -144,10 +159,9 @@ export async function launchTestNode