From 59b36db4c76ed7f8c95d0e786ce8787d8498d443 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nedim=20Salki=C4=87?= Date: Mon, 11 Nov 2024 05:28:37 +0100 Subject: [PATCH] docs: half of contract snippets (#3366) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * moved call-parameters * configurable-constants * contract-balance * cost-estimation * dependency-estimation * deploying-contracts * index * inter-contract-calls * logs * managing-deployed-contracts * remove unused contracts * formatting * revert docs-snippets forc.toml * revert removed contracts * linting * chore: changeset * forc format * fix test * fix test * fix snippet * fix problem hopefully * Update apps/docs/src/guide/contracts/dependency-estimation.md Co-authored-by: Sérgio Torres <30977845+Torres-ssf@users.noreply.github.com> * update link --------- Co-authored-by: Sérgio Torres <30977845+Torres-ssf@users.noreply.github.com> --- .changeset/gorgeous-bikes-swim.md | 2 + .../guide/contracts/call-parameters.test.ts | 104 ---------------- .../contracts/configurable-constants.test.ts | 109 ----------------- .../guide/contracts/contract-balance.test.ts | 56 --------- .../guide/contracts/cost-estimation.test.ts | 73 ------------ .../contracts/deploying-contracts.test.ts | 111 ------------------ .../src/guide/contracts/index.test.ts | 33 ------ .../contracts/inter-contract-calls.test.ts | 50 -------- .../src/guide/contracts/logs.test.ts | 43 ------- .../managing-deployed-contracts.test.ts | 63 ---------- apps/docs-snippets2/fuels.config.ts | 2 + apps/docs-snippets2/scripts/pretest.sh | 8 +- .../src/contracts/call-parameters/forward.ts | 26 ++++ .../src/contracts/call-parameters/gas-fee.ts | 25 ++++ .../setting-both-parameters.ts | 31 +++++ .../src/contracts/configurable-constants.ts | 46 ++++++++ .../src/contracts/contract-balance.ts | 40 +++++++ .../src/contracts/cost-estimation.ts | 36 ++++++ .../deploying-contracts/deployment.ts | 49 ++++++++ .../deploying-contracts/get-max-size.ts | 15 +++ .../src/contracts/inter-contract-calls.ts | 44 +++++++ .../src/contracts/introduction.ts | 20 ++++ apps/docs-snippets2/src/contracts/logs.ts | 30 +++++ .../contracts/managing-deployed-contracts.ts | 28 +++++ apps/docs-snippets2/sway/Forc.toml | 8 ++ .../sway/echo-configurables/Forc.toml | 7 ++ .../sway/echo-configurables/src/main.sw | 35 ++++++ apps/docs-snippets2/sway/log-values/Forc.toml | 7 ++ .../sway/log-values/src/main.sw | 18 +++ .../sway/my-contract/.gitignore | 2 + .../docs-snippets2/sway/my-contract/Forc.toml | 7 ++ .../sway/my-contract/src/main.sw | 11 ++ .../sway/return-context/Forc.toml | 7 ++ .../sway/return-context/src/main.sw | 17 +++ .../sway/simple-token-abi/Forc.toml | 7 ++ .../sway/simple-token-abi/src/main.sw | 9 ++ .../sway/simple-token/Forc.toml | 8 ++ .../sway/simple-token/src/main.sw | 22 ++++ .../sway/token-depositor/Forc.toml | 8 ++ .../sway/token-depositor/src/main.sw | 26 ++++ .../sway/transfer-to-address/Forc.toml | 7 ++ .../sway/transfer-to-address/src/main.sw | 23 ++++ apps/docs/spell-check-custom-words.txt | 2 + .../src/guide/contracts/call-parameters.md | 18 +-- .../guide/contracts/configurable-constants.md | 18 +-- .../src/guide/contracts/contract-balance.md | 16 ++- .../src/guide/contracts/cost-estimation.md | 6 +- .../guide/contracts/dependency-estimation.md | 8 +- .../guide/contracts/deploying-contracts.md | 14 +-- apps/docs/src/guide/contracts/index.md | 4 +- .../guide/contracts/inter-contract-calls.md | 6 +- apps/docs/src/guide/contracts/logs.md | 4 +- .../contracts/managing-deployed-contracts.md | 4 +- packages/account/src/providers/provider.ts | 2 - packages/program/src/contract.ts | 2 - scripts/forc-format.sh | 4 +- 56 files changed, 676 insertions(+), 705 deletions(-) create mode 100644 .changeset/gorgeous-bikes-swim.md delete mode 100644 apps/docs-snippets/src/guide/contracts/call-parameters.test.ts delete mode 100644 apps/docs-snippets/src/guide/contracts/configurable-constants.test.ts delete mode 100644 apps/docs-snippets/src/guide/contracts/contract-balance.test.ts delete mode 100644 apps/docs-snippets/src/guide/contracts/cost-estimation.test.ts delete mode 100644 apps/docs-snippets/src/guide/contracts/deploying-contracts.test.ts delete mode 100644 apps/docs-snippets/src/guide/contracts/index.test.ts delete mode 100644 apps/docs-snippets/src/guide/contracts/inter-contract-calls.test.ts delete mode 100644 apps/docs-snippets/src/guide/contracts/logs.test.ts delete mode 100644 apps/docs-snippets/src/guide/contracts/managing-deployed-contracts.test.ts create mode 100644 apps/docs-snippets2/src/contracts/call-parameters/forward.ts create mode 100644 apps/docs-snippets2/src/contracts/call-parameters/gas-fee.ts create mode 100644 apps/docs-snippets2/src/contracts/call-parameters/setting-both-parameters.ts create mode 100644 apps/docs-snippets2/src/contracts/configurable-constants.ts create mode 100644 apps/docs-snippets2/src/contracts/contract-balance.ts create mode 100644 apps/docs-snippets2/src/contracts/cost-estimation.ts create mode 100644 apps/docs-snippets2/src/contracts/deploying-contracts/deployment.ts create mode 100644 apps/docs-snippets2/src/contracts/deploying-contracts/get-max-size.ts create mode 100644 apps/docs-snippets2/src/contracts/inter-contract-calls.ts create mode 100644 apps/docs-snippets2/src/contracts/introduction.ts create mode 100644 apps/docs-snippets2/src/contracts/logs.ts create mode 100644 apps/docs-snippets2/src/contracts/managing-deployed-contracts.ts create mode 100644 apps/docs-snippets2/sway/echo-configurables/Forc.toml create mode 100644 apps/docs-snippets2/sway/echo-configurables/src/main.sw create mode 100644 apps/docs-snippets2/sway/log-values/Forc.toml create mode 100644 apps/docs-snippets2/sway/log-values/src/main.sw create mode 100644 apps/docs-snippets2/sway/my-contract/.gitignore create mode 100644 apps/docs-snippets2/sway/my-contract/Forc.toml create mode 100644 apps/docs-snippets2/sway/my-contract/src/main.sw create mode 100644 apps/docs-snippets2/sway/return-context/Forc.toml create mode 100644 apps/docs-snippets2/sway/return-context/src/main.sw create mode 100644 apps/docs-snippets2/sway/simple-token-abi/Forc.toml create mode 100644 apps/docs-snippets2/sway/simple-token-abi/src/main.sw create mode 100644 apps/docs-snippets2/sway/simple-token/Forc.toml create mode 100644 apps/docs-snippets2/sway/simple-token/src/main.sw create mode 100644 apps/docs-snippets2/sway/token-depositor/Forc.toml create mode 100644 apps/docs-snippets2/sway/token-depositor/src/main.sw create mode 100644 apps/docs-snippets2/sway/transfer-to-address/Forc.toml create mode 100644 apps/docs-snippets2/sway/transfer-to-address/src/main.sw diff --git a/.changeset/gorgeous-bikes-swim.md b/.changeset/gorgeous-bikes-swim.md new file mode 100644 index 00000000000..a845151cc84 --- /dev/null +++ b/.changeset/gorgeous-bikes-swim.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/apps/docs-snippets/src/guide/contracts/call-parameters.test.ts b/apps/docs-snippets/src/guide/contracts/call-parameters.test.ts deleted file mode 100644 index ab4040e9c1c..00000000000 --- a/apps/docs-snippets/src/guide/contracts/call-parameters.test.ts +++ /dev/null @@ -1,104 +0,0 @@ -import { BN } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { ReturnContextFactory } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('Call Parameters', () => { - it('should successfully execute contract call with forwarded amount', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: ReturnContextFactory, - }, - ], - }); - - const { - provider, - contracts: [contract], - } = launched; - - // #region call-params-1 - const amountToForward = 10; - - const { waitForResult } = await contract.functions - .return_context_amount() - .callParams({ - forward: [amountToForward, provider.getBaseAssetId()], - }) - .call(); - - const { value } = await waitForResult(); - - expect(new BN(value).toNumber()).toBe(amountToForward); - // #endregion call-params-1 - }); - - it('should throw error due not enough gas', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: ReturnContextFactory, - }, - ], - }); - const { - provider, - contracts: [contract], - } = launched; - // #region call-params-2 - - await expect(async () => { - const call = await contract.functions - .return_context_amount() - .callParams({ - forward: [10, provider.getBaseAssetId()], - gasLimit: 1, - }) - .call(); - - await call.waitForResult(); - }).rejects.toThrow('The transaction reverted with reason: "OutOfGas"'); - // #endregion call-params-2 - }); - - it('should successfully execute transaction with `txParams` and `callParams`', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: ReturnContextFactory, - }, - ], - }); - const { - provider, - contracts: [contract], - } = launched; - // #region call-params-3 - const amountToForward = 10; - const contractCallGasLimit = 4000; - const transactionGasLimit = 100_000; - - const { waitForResult } = await contract.functions - .return_context_amount() - .callParams({ - forward: [amountToForward, provider.getBaseAssetId()], - gasLimit: contractCallGasLimit, - }) - .txParams({ - gasLimit: transactionGasLimit, - }) - .call(); - - const result = await waitForResult(); - const { value } = result; - const expectedValue = 10; - - expect(new BN(value).toNumber()).toBe(expectedValue); - // #endregion call-params-3 - }); -}); diff --git a/apps/docs-snippets/src/guide/contracts/configurable-constants.test.ts b/apps/docs-snippets/src/guide/contracts/configurable-constants.test.ts deleted file mode 100644 index c0223c89078..00000000000 --- a/apps/docs-snippets/src/guide/contracts/configurable-constants.test.ts +++ /dev/null @@ -1,109 +0,0 @@ -import { launchTestNode } from 'fuels/test-utils'; - -import { EchoConfigurablesFactory } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('configurable-constants', () => { - const defaultValues = { - age: 25, - tag: 'fuel', - grades: [3, 4, 3, 2], - my_struct: { - x: 1, - y: 2, - state: 'Pending', - }, - }; - - it('should successfully set new values for all configurable constants', async () => { - using launched = await launchTestNode(); - - const { - wallets: [wallet], - } = launched; - - // #region configurable-constants-2 - const configurableConstants: typeof defaultValues = { - age: 30, - tag: 'leuf', - grades: [10, 9, 8, 9], - my_struct: { - x: 11, - y: 22, - state: 'Checked', - }, - }; - - const echoFactory = new EchoConfigurablesFactory(wallet); - - const { waitForResult } = await echoFactory.deploy({ - configurableConstants, - }); - - const { contract } = await waitForResult(); - // #endregion configurable-constants-2 - - const { value } = await contract.functions.echo_configurables(true).simulate(); - - 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 constant', async () => { - using launched = await launchTestNode(); - - const { - wallets: [wallet], - } = launched; - - // #region configurable-constants-3 - const configurableConstants = { - age: 10, - }; - - const echoFactory = new EchoConfigurablesFactory(wallet); - - const { waitForResult } = await echoFactory.deploy({ - configurableConstants, - }); - - const { contract } = await waitForResult(); - // #endregion configurable-constants-3 - - const { value } = await contract.functions.echo_configurables(false).simulate(); - - 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 () => { - using launched = await launchTestNode(); - - const { - wallets: [wallet], - } = launched; - - // #region configurable-constants-4 - const configurableConstants = { - my_struct: { - x: 2, - }, - }; - - const echoFactory = new EchoConfigurablesFactory(wallet); - - await expect( - echoFactory.deploy({ - configurableConstants, - }) - ).rejects.toThrowError(); - // #endregion configurable-constants-4 - }); -}); diff --git a/apps/docs-snippets/src/guide/contracts/contract-balance.test.ts b/apps/docs-snippets/src/guide/contracts/contract-balance.test.ts deleted file mode 100644 index 60e42443810..00000000000 --- a/apps/docs-snippets/src/guide/contracts/contract-balance.test.ts +++ /dev/null @@ -1,56 +0,0 @@ -import type { AssetId } from 'fuels'; -import { Wallet, BN } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { TransferToAddressFactory } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('Contract Balance', () => { - it('should successfully get a contract balance', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: TransferToAddressFactory, - }, - ], - }); - const { - provider, - contracts: [contract], - } = launched; - - // #region contract-balance-3 - // #import { AssetId, Wallet, BN }; - - const amountToForward = 40; - const amountToTransfer = 10; - const baseAssetId = provider.getBaseAssetId(); - - const recipient = Wallet.generate({ - provider, - }); - - const asset: AssetId = { - bits: baseAssetId, - }; - - const { waitForResult } = await contract.functions - .transfer(amountToTransfer, asset, recipient.address.toB256()) - .callParams({ - forward: [amountToForward, baseAssetId], - }) - .call(); - - await waitForResult(); - - const contractBalance = await contract.getBalance(baseAssetId); - - const expectedBalance = amountToForward - amountToTransfer; - - expect(new BN(contractBalance).toNumber()).toBe(expectedBalance); - // #endregion contract-balance-3 - }); -}); diff --git a/apps/docs-snippets/src/guide/contracts/cost-estimation.test.ts b/apps/docs-snippets/src/guide/contracts/cost-estimation.test.ts deleted file mode 100644 index c5b94384c0e..00000000000 --- a/apps/docs-snippets/src/guide/contracts/cost-estimation.test.ts +++ /dev/null @@ -1,73 +0,0 @@ -import { launchTestNode } from 'fuels/test-utils'; - -import { ReturnContextFactory } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('Cost Estimation', () => { - it('should successfully get transaction cost estimate for a single contract call', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: ReturnContextFactory, - }, - ], - }); - - const { - contracts: [contract], - provider, - } = launched; - - // #region cost-estimation-1 - const cost = await contract.functions - .return_context_amount() - .callParams({ - forward: [100, provider.getBaseAssetId()], - }) - .getTransactionCost(); - - expect(cost.minFee).toBeDefined(); - expect(cost.maxFee).toBeDefined(); - expect(cost.gasPrice).toBeDefined(); - expect(cost.gasUsed).toBeDefined(); - expect(cost.gasPrice).toBeDefined(); - // #endregion cost-estimation-1 - }); - - it('should get transaction cost estimate for multi contract calls just fine', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: ReturnContextFactory, - }, - ], - }); - - const { - contracts: [contract], - provider, - } = launched; - - // #region cost-estimation-2 - const scope = contract.multiCall([ - contract.functions.return_context_amount().callParams({ - forward: [100, provider.getBaseAssetId()], - }), - contract.functions.return_context_amount().callParams({ - forward: [300, provider.getBaseAssetId()], - }), - ]); - - const cost = await scope.getTransactionCost(); - - expect(cost.minFee).toBeDefined(); - expect(cost.maxFee).toBeDefined(); - expect(cost.gasPrice).toBeDefined(); - expect(cost.gasUsed).toBeDefined(); - expect(cost.gasPrice).toBeDefined(); - // #endregion cost-estimation-2 - }); -}); diff --git a/apps/docs-snippets/src/guide/contracts/deploying-contracts.test.ts b/apps/docs-snippets/src/guide/contracts/deploying-contracts.test.ts deleted file mode 100644 index 1458c01061e..00000000000 --- a/apps/docs-snippets/src/guide/contracts/deploying-contracts.test.ts +++ /dev/null @@ -1,111 +0,0 @@ -import { Provider, Wallet, ContractFactory, hexlify } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { EchoValuesFactory as TypegenFactory, EchoValues } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('Deploying Contracts', () => { - it('gets the max contract size for the chain', async () => { - using launched = await launchTestNode(); - - const { provider: testProvider } = launched; - const providerUrl = testProvider.url; - - // #region get-contract-max-size - // #import { Provider }; - - const provider = await Provider.create(providerUrl); - const { consensusParameters } = provider.getChain(); - const contractSizeLimit = consensusParameters.contractParameters.contractMaxSize; - // #endregion get-contract-max-size - expect(contractSizeLimit).toBeDefined(); - }); - - it('deploys a contract', async () => { - using launched = await launchTestNode(); - - const { - provider: testProvider, - wallets: [testWallet], - } = launched; - const providerUrl = testProvider.url; - const WALLET_PVT_KEY = testWallet.privateKey; - const bytecode = TypegenFactory.bytecode; - - // #region setup - // #import { Provider, Wallet }; - // #context import { WALLET_PVT_KEY } from 'path/to/my/env/file'; - // #context import { TypegenFactory } from 'path/to/typegen/outputs'; - - const provider = await Provider.create(providerUrl); - const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); - const factory = new TypegenFactory(wallet); - // #endregion setup - expect(hexlify(factory.bytecode)).toBe(hexlify(bytecode)); - - // #region deploy - // Deploy the contract - const { waitForResult, contractId, waitForTransactionId } = await factory.deploy(); - // Retrieve the transactionId - const transactionId = await waitForTransactionId(); - // Await it's deployment - const { contract, transactionResult } = await waitForResult(); - // #endregion deploy - expect(contract).toBeDefined(); - expect(transactionId).toBeDefined(); - expect(transactionResult.status).toBeTruthy(); - expect(contractId).toBe(contract.id.toB256()); - - // #region call - // Call the contract - const { waitForResult: waitForCallResult } = await contract.functions.echo_u8(10).call(); - // Await the result of the call - const { value } = await waitForCallResult(); - // #endregion call - expect(value).toBe(10); - }); - - it('deploys a large contract as blobs', async () => { - using launched = await launchTestNode(); - - const { - provider: testProvider, - wallets: [testWallet], - } = launched; - const providerUrl = testProvider.url; - const WALLET_PVT_KEY = testWallet.privateKey; - const abi = EchoValues.abi; - const bytecode = TypegenFactory.bytecode; - - // #region blobs - // #import { Provider, Wallet, ContractFactory }; - // #context import { WALLET_PVT_KEY } from 'path/to/my/env/file'; - // #context import { bytecode, abi } from 'path/to/typegen/outputs'; - - const provider = await Provider.create(providerUrl); - const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); - const factory = new ContractFactory(bytecode, abi, wallet); - - // Deploy the contract as blobs - const { waitForResult, contractId, waitForTransactionId } = await factory.deployAsBlobTx({ - // Increasing chunk size multiplier to be 90% of the max chunk size - chunkSizeMultiplier: 0.9, - }); - // Await it's deployment - const { contract, transactionResult } = await waitForResult(); - // #endregion blobs - - const transactionId = await waitForTransactionId(); - expect(contract).toBeDefined(); - expect(transactionId).toBeDefined(); - expect(transactionResult.status).toBeTruthy(); - expect(contractId).toBe(contract.id.toB256()); - - const { waitForResult: waitForCallResult } = await contract.functions.echo_u8(10).call(); - const { value } = await waitForCallResult(); - expect(value).toBe(10); - }); -}); diff --git a/apps/docs-snippets/src/guide/contracts/index.test.ts b/apps/docs-snippets/src/guide/contracts/index.test.ts deleted file mode 100644 index 91064552913..00000000000 --- a/apps/docs-snippets/src/guide/contracts/index.test.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { launchTestNode } from 'fuels/test-utils'; - -import { EchoValuesFactory } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('Contract echo values', () => { - it('should successfully call contract and echo values', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoValuesFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - // #region echo-values - const u8Value = 10; - const str8Value = 'fuel-sdk'; - - const res1 = await contract.functions.echo_u8(u8Value).simulate(); - const res2 = await contract.functions.echo_str_8(str8Value).simulate(); - - expect(res1.value).toBe(u8Value); - expect(res2.value).toBe(str8Value); - // #endregion echo-values - }); -}); diff --git a/apps/docs-snippets/src/guide/contracts/inter-contract-calls.test.ts b/apps/docs-snippets/src/guide/contracts/inter-contract-calls.test.ts deleted file mode 100644 index 7e7081a4588..00000000000 --- a/apps/docs-snippets/src/guide/contracts/inter-contract-calls.test.ts +++ /dev/null @@ -1,50 +0,0 @@ -import { BN } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { SimpleTokenFactory, TokenDepositorFactory } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('Inter-Contract Calls', () => { - it('should successfully make call to another contract', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: SimpleTokenFactory, - }, - { - factory: TokenDepositorFactory, - }, - ], - }); - - const { - contracts: [simpleToken, tokenDepositor], - wallets: [wallet], - } = launched; - - // #region inter-contract-calls-3 - const amountToDeposit = 70; - const call1 = await simpleToken.functions.get_balance(wallet.address.toB256()).call(); - - const { value: initialBalance } = await call1.waitForResult(); - - expect(new BN(initialBalance).toNumber()).toBe(0); - - const call2 = await tokenDepositor.functions - .deposit_to_simple_token(simpleToken.id.toB256(), amountToDeposit) - .addContracts([simpleToken]) - .call(); - - await call2.waitForResult(); - - const call3 = await simpleToken.functions.get_balance(wallet.address.toB256()).call(); - - const { value: finalBalance } = await call3.waitForResult(); - - expect(new BN(finalBalance).toNumber()).toBe(amountToDeposit); - // #endregion inter-contract-calls-3 - }); -}); diff --git a/apps/docs-snippets/src/guide/contracts/logs.test.ts b/apps/docs-snippets/src/guide/contracts/logs.test.ts deleted file mode 100644 index 43b14aabf72..00000000000 --- a/apps/docs-snippets/src/guide/contracts/logs.test.ts +++ /dev/null @@ -1,43 +0,0 @@ -import type { BigNumberish } from 'fuels'; -import { BN } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { LogValuesFactory } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('Contract Logs', () => { - it('should successfully execute contract call with forwarded amount', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: LogValuesFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region log-2 - const value1 = 500; - const value2 = '0xef86afa9696cf0dc6385e2c407a6e159a1103cefb7e2ae0636fb33d3cb2a9e4a'; - const value3 = 'Fuel'; - const value4: [BigNumberish, BigNumberish, BigNumberish] = [1, 2, 3]; - - const { waitForResult } = await contract.functions - .log_values(value1, value2, value3, value4) - .call(); - - const { logs } = await waitForResult(); - - expect(new BN(logs[0]).toNumber()).toBe(value1); - expect(logs[1]).toBe(value2); - expect(logs[2]).toBe(value3); - expect(logs[3]).toEqual(value4); - // #endregion log-2 - }); -}); diff --git a/apps/docs-snippets/src/guide/contracts/managing-deployed-contracts.test.ts b/apps/docs-snippets/src/guide/contracts/managing-deployed-contracts.test.ts deleted file mode 100644 index ade43d1ffe5..00000000000 --- a/apps/docs-snippets/src/guide/contracts/managing-deployed-contracts.test.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { Contract } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { EchoValuesFactory } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('Managing deployed contracts', () => { - it('should successfully interact with a deployed contract', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoValuesFactory, - }, - ], - }); - - const { - contracts: [contract], - wallets: [wallet], - } = launched; - - const { id: contractId, interface: abi } = contract; - - // #region managing-deployed-contracts-1 - const deployedEchoContract = new Contract(contractId, abi, wallet); - - const { value } = await deployedEchoContract.functions.echo_u8(10).simulate(); - - expect(value).toEqual(10); - // #endregion managing-deployed-contracts-1 - }); - - it('should successfully interact with a deployed contract [hexed contract id]', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: EchoValuesFactory, - }, - ], - }); - - const { - contracts: [contract], - wallets: [wallet], - } = launched; - - const b256 = contract.id.toB256(); - const { interface: abi } = contract; - - // #region managing-deployed-contracts-2 - // #context const b256 = '0x50007a55ccc29075bc0e9c0ea0524add4a7ed4f91afbe1fdcc661caabfe4a82f'; - - const deployedContract = new Contract(b256, abi, wallet); - - const { value } = await deployedContract.functions.echo_u8(50).simulate(); - - expect(value).toEqual(50); - // #endregion managing-deployed-contracts-2 - }); -}); diff --git a/apps/docs-snippets2/fuels.config.ts b/apps/docs-snippets2/fuels.config.ts index 5b0109ac896..e74ede4976e 100644 --- a/apps/docs-snippets2/fuels.config.ts +++ b/apps/docs-snippets2/fuels.config.ts @@ -6,4 +6,6 @@ export default createConfig({ forcBuildFlags: ['--release'], forcPath: 'fuels-forc', fuelCorePath: 'fuels-core', + fuelCorePort: 4001, + providerUrl: 'http://127.0.0.1:4001/v1/graphql', }); diff --git a/apps/docs-snippets2/scripts/pretest.sh b/apps/docs-snippets2/scripts/pretest.sh index 3843d22753c..14b0dba09f5 100755 --- a/apps/docs-snippets2/scripts/pretest.sh +++ b/apps/docs-snippets2/scripts/pretest.sh @@ -1,7 +1,7 @@ -# Check if node is already running at port 4000, if not start it +# Check if node is already running at port 4001, if not start it # TODO: This is a temporary solution to avoid conflicts with the test node in docs-snippets -if ! lsof -t -i:4000 > /dev/null; then - pnpm fuels node > /dev/null 2>&1 & +if ! lsof -t -i:4001 >/dev/null; then + pnpm fuels node >/dev/null 2>&1 & fi # Builds projects @@ -11,4 +11,4 @@ pnpm fuels build pnpm fuels deploy # Checks for type errors -pnpm tsc --noEmit \ No newline at end of file +pnpm tsc --noEmit diff --git a/apps/docs-snippets2/src/contracts/call-parameters/forward.ts b/apps/docs-snippets2/src/contracts/call-parameters/forward.ts new file mode 100644 index 00000000000..3bc06ef0963 --- /dev/null +++ b/apps/docs-snippets2/src/contracts/call-parameters/forward.ts @@ -0,0 +1,26 @@ +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { ReturnContextFactory } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +const deploy = await ReturnContextFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region forward +const amountToForward = 10; + +const { waitForResult } = await contract.functions + .return_context_amount() + .callParams({ + forward: [amountToForward, provider.getBaseAssetId()], + }) + .call(); + +const { value } = await waitForResult(); + +console.log('forwarded amount:', value.toNumber()); +// forwarded amount: 10 +// #endregion forward diff --git a/apps/docs-snippets2/src/contracts/call-parameters/gas-fee.ts b/apps/docs-snippets2/src/contracts/call-parameters/gas-fee.ts new file mode 100644 index 00000000000..c4ed5e64662 --- /dev/null +++ b/apps/docs-snippets2/src/contracts/call-parameters/gas-fee.ts @@ -0,0 +1,25 @@ +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { ReturnContextFactory } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +const deploy = await ReturnContextFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region gas-fee +try { + await contract.functions + .return_context_amount() + .callParams({ + forward: [10, provider.getBaseAssetId()], + gasLimit: 1, + }) + .call(); +} catch (e) { + console.log('error', e); + // error _FuelError: The transaction reverted with reason: "OutOfGas" +} +// #endregion gas-fee diff --git a/apps/docs-snippets2/src/contracts/call-parameters/setting-both-parameters.ts b/apps/docs-snippets2/src/contracts/call-parameters/setting-both-parameters.ts new file mode 100644 index 00000000000..fbfcda64c29 --- /dev/null +++ b/apps/docs-snippets2/src/contracts/call-parameters/setting-both-parameters.ts @@ -0,0 +1,31 @@ +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { ReturnContextFactory } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +const deploy = await ReturnContextFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region setting-both-parameters +const contractCallGasLimit = 4_000; +const transactionGasLimit = 100_000; + +const call = await contract.functions + .return_context_amount() + .callParams({ + forward: [10, provider.getBaseAssetId()], + gasLimit: contractCallGasLimit, + }) + .txParams({ + gasLimit: transactionGasLimit, + }) + .call(); +// #endregion setting-both-parameters + +const { + transactionResult: { isStatusSuccess }, +} = await call.waitForResult(); +console.log('tx successful', isStatusSuccess); diff --git a/apps/docs-snippets2/src/contracts/configurable-constants.ts b/apps/docs-snippets2/src/contracts/configurable-constants.ts new file mode 100644 index 00000000000..b2e2a8172b0 --- /dev/null +++ b/apps/docs-snippets2/src/contracts/configurable-constants.ts @@ -0,0 +1,46 @@ +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; +import { EchoConfigurablesFactory } from '../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +// #region setting-configurable-constant +const configurableConstants = { + age: 10, +}; + +const deploy = await EchoConfigurablesFactory.deploy(wallet, { + configurableConstants, +}); +const { contract } = await deploy.waitForResult(); + +const { + value: [age, tag, grades, myStruct], +} = await contract.functions.echo_configurables().get(); + +// age got updated +console.log('age', age); // 10 +// while the rest are default values +console.log('tag', tag); // 'fuel' +console.log('grades', grades); // [3, 4, 3, 2] +console.log('myStruct', myStruct); // { x: 1, y: 2, state: 'Pending' } +// #endregion setting-configurable-constant + +// #region invalid-configurable +const invalidConfigurables = { + my_struct: { + x: 10, + }, +}; +try { + await EchoConfigurablesFactory.deploy(wallet, { + configurableConstants: invalidConfigurables, + }); +} catch (e) { + console.log('error', e); + // error: Error setting configurable constants on contract: + // Invalid struct MyStruct. Field "y" not present. +} +// #endregion invalid-configurable diff --git a/apps/docs-snippets2/src/contracts/contract-balance.ts b/apps/docs-snippets2/src/contracts/contract-balance.ts new file mode 100644 index 00000000000..0f6f5f7ae75 --- /dev/null +++ b/apps/docs-snippets2/src/contracts/contract-balance.ts @@ -0,0 +1,40 @@ +// #region example +import type { AssetId } from 'fuels'; +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; +import { TransferToAddressFactory } from '../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const { waitForResult: waitForDeploy } = + await TransferToAddressFactory.deploy(wallet); +const { contract } = await waitForDeploy(); + +const amountToForward = 40; +const amountToTransfer = 10; +const baseAssetId = provider.getBaseAssetId(); + +const recipient = Wallet.generate({ + provider, +}); + +const asset: AssetId = { + bits: baseAssetId, +}; + +const { waitForResult } = await contract.functions + .transfer(amountToTransfer, asset, recipient.address.toB256()) + .callParams({ + forward: [amountToForward, baseAssetId], + }) + .call(); + +await waitForResult(); + +const contractBalance = await contract.getBalance(baseAssetId); +console.log( + 'contract balance reduced by amountToTransfer', + contractBalance.toNumber() === amountToForward - amountToTransfer +); +// #endregion example diff --git a/apps/docs-snippets2/src/contracts/cost-estimation.ts b/apps/docs-snippets2/src/contracts/cost-estimation.ts new file mode 100644 index 00000000000..162abdf5e92 --- /dev/null +++ b/apps/docs-snippets2/src/contracts/cost-estimation.ts @@ -0,0 +1,36 @@ +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; +import { ReturnContextFactory } from '../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +const deploy = await ReturnContextFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region cost-estimation-1 +const cost = await contract.functions + .return_context_amount() + .callParams({ + forward: [100, provider.getBaseAssetId()], + }) + .getTransactionCost(); + +console.log('costs', cost); +// #endregion cost-estimation-1 + +// #region cost-estimation-2 +const scope = contract.multiCall([ + contract.functions.return_context_amount().callParams({ + forward: [100, provider.getBaseAssetId()], + }), + contract.functions.return_context_amount().callParams({ + forward: [300, provider.getBaseAssetId()], + }), +]); + +const txCost = await scope.getTransactionCost(); + +console.log('costs', txCost); +// #endregion cost-estimation-2 diff --git a/apps/docs-snippets2/src/contracts/deploying-contracts/deployment.ts b/apps/docs-snippets2/src/contracts/deploying-contracts/deployment.ts new file mode 100644 index 00000000000..8bbce9f39d4 --- /dev/null +++ b/apps/docs-snippets2/src/contracts/deploying-contracts/deployment.ts @@ -0,0 +1,49 @@ +// #region setup +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { MyContractFactory } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const factory = new MyContractFactory(wallet); +// #endregion setup + +// #region deploy +// Deploy the contract +const { waitForResult, contractId, waitForTransactionId } = + await factory.deploy(); +// Retrieve the transactionId +const transactionId = await waitForTransactionId(); +// Await it's deployment +const { contract, transactionResult } = await waitForResult(); +// #endregion deploy + +console.log('contractId', contractId); +console.log('transactionId', transactionId); +console.log('transactionResult', transactionResult); + +// #region call +// Call the contract +const { waitForResult: waitForCallResult } = await contract.functions + .test_function() + .call(); +// Await the result of the call +const { value } = await waitForCallResult(); +// #endregion call +console.log('value', value); + +// #region blobs +// Deploy the contract as blobs +const { waitForResult: waitForBlobsAndContractDeployment } = + await factory.deployAsBlobTx({ + // setting chunk size multiplier to be 90% of the max chunk size + chunkSizeMultiplier: 0.9, + }); + +// Await its deployment +const { contract: contractFromBlobs } = + await waitForBlobsAndContractDeployment(); +// #endregion blobs + +console.log('contractFromBlobs', contractFromBlobs); diff --git a/apps/docs-snippets2/src/contracts/deploying-contracts/get-max-size.ts b/apps/docs-snippets2/src/contracts/deploying-contracts/get-max-size.ts new file mode 100644 index 00000000000..d2b0d718a36 --- /dev/null +++ b/apps/docs-snippets2/src/contracts/deploying-contracts/get-max-size.ts @@ -0,0 +1,15 @@ +// #region full +import { Provider } from 'fuels'; + +import { LOCAL_NETWORK_URL } from '../../env'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); + +const { + consensusParameters: { + contractParameters: { contractMaxSize }, + }, +} = provider.getChain(); +// #endregion full + +console.log('contractMaxSize', contractMaxSize); diff --git a/apps/docs-snippets2/src/contracts/inter-contract-calls.ts b/apps/docs-snippets2/src/contracts/inter-contract-calls.ts new file mode 100644 index 00000000000..5800579386f --- /dev/null +++ b/apps/docs-snippets2/src/contracts/inter-contract-calls.ts @@ -0,0 +1,44 @@ +// #region full +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; +import { SimpleTokenFactory, TokenDepositorFactory } from '../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +const { waitForResult: waitForSimpleToken } = + await SimpleTokenFactory.deploy(wallet); + +const { contract: simpleToken } = await waitForSimpleToken(); + +const { waitForResult: waitForTokenDepositor } = + await TokenDepositorFactory.deploy(wallet); + +const { contract: tokenDepositor } = await waitForTokenDepositor(); + +const amountToDeposit = 70; +const call1 = await simpleToken.functions + .get_balance(wallet.address.toB256()) + .call(); + +const { value: initialBalance } = await call1.waitForResult(); + +const call2 = await tokenDepositor.functions + .deposit_to_simple_token(simpleToken.id.toB256(), amountToDeposit) + .addContracts([simpleToken]) + .call(); + +await call2.waitForResult(); + +const call3 = await simpleToken.functions + .get_balance(wallet.address.toB256()) + .call(); + +const { value: finalBalance } = await call3.waitForResult(); +// #endregion full +console.log('no initial balance', initialBalance.toNumber() === 0); +console.log( + 'final balance matches deposited amount', + finalBalance.toNumber() === amountToDeposit +); diff --git a/apps/docs-snippets2/src/contracts/introduction.ts b/apps/docs-snippets2/src/contracts/introduction.ts new file mode 100644 index 00000000000..f404e53c152 --- /dev/null +++ b/apps/docs-snippets2/src/contracts/introduction.ts @@ -0,0 +1,20 @@ +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; +import { EchoValuesFactory } from '../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +const deploy = await EchoValuesFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region method-calls +const u8Value = 10; +const str8Value = 'fuel-sdk'; + +const res1 = await contract.functions.echo_u8(u8Value).simulate(); +const res2 = await contract.functions.echo_str_8(str8Value).simulate(); +// #endregion method-calls +console.log('res1', res1); +console.log('res2', res2); diff --git a/apps/docs-snippets2/src/contracts/logs.ts b/apps/docs-snippets2/src/contracts/logs.ts new file mode 100644 index 00000000000..a74bd655d9b --- /dev/null +++ b/apps/docs-snippets2/src/contracts/logs.ts @@ -0,0 +1,30 @@ +// #region full +import type { BigNumberish } from 'fuels'; +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; +import { LogValuesFactory } from '../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +const deploy = await LogValuesFactory.deploy(wallet); + +const { contract } = await deploy.waitForResult(); + +const value1 = 500; +const value2 = + '0xef86afa9696cf0dc6385e2c407a6e159a1103cefb7e2ae0636fb33d3cb2a9e4a'; +const value3 = 'Fuel'; +const value4: [BigNumberish, BigNumberish, BigNumberish] = [1, 2, 3]; + +const { waitForResult } = await contract.functions + .log_values(value1, value2, value3, value4) + .call(); + +const { logs } = await waitForResult(); +// #endregion full +console.log('log[0]', logs[0].toNumber() === value1); +console.log('log[1]', logs[1] === value2); +console.log('log[2]', logs[2] === value3); +console.log('log[3]', logs[3][0] === value4[0]); diff --git a/apps/docs-snippets2/src/contracts/managing-deployed-contracts.ts b/apps/docs-snippets2/src/contracts/managing-deployed-contracts.ts new file mode 100644 index 00000000000..49149fe0cc1 --- /dev/null +++ b/apps/docs-snippets2/src/contracts/managing-deployed-contracts.ts @@ -0,0 +1,28 @@ +import { Contract, Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../env'; +import { EchoValues, EchoValuesFactory } from '../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const abi = EchoValues.abi; +const { waitForResult, contractId } = await EchoValuesFactory.deploy(wallet); + +await waitForResult(); + +// #region with-contractId +const deployedEchoContract = new Contract(contractId, abi, wallet); + +const { value: echoed10 } = await deployedEchoContract.functions + .echo_u8(10) + .simulate(); +// value 10 +// #endregion with-contractId +console.log('echoed10', echoed10 === 10); +const b256 = deployedEchoContract.id.toB256(); +// #region with-b256 +const contract = new Contract(b256, abi, wallet); + +const { value: echoed50 } = await contract.functions.echo_u8(50).simulate(); +// #endregion with-b256 +console.log('echoed50', echoed50 === 50); diff --git a/apps/docs-snippets2/sway/Forc.toml b/apps/docs-snippets2/sway/Forc.toml index e93d927a97c..512284fe60b 100644 --- a/apps/docs-snippets2/sway/Forc.toml +++ b/apps/docs-snippets2/sway/Forc.toml @@ -3,10 +3,13 @@ members = [ "bytecode-input", "configurable-pin", "counter", + "log-values", + "my-contract", "echo-values", "script-transfer-to-contract", "echo-asset-id", "echo-bytes", + "echo-configurables", "echo-employee-data-vector", "echo-enum", "echo-evm-address", @@ -19,6 +22,11 @@ members = [ "return-true-predicate", "script-sum", "simple-predicate", + "simple-token", + "simple-token-abi", "sum-option-u8", + "token-depositor", + "return-context", + "transfer-to-address", "whitelisted-address-predicate", ] diff --git a/apps/docs-snippets2/sway/echo-configurables/Forc.toml b/apps/docs-snippets2/sway/echo-configurables/Forc.toml new file mode 100644 index 00000000000..ce6ac587af2 --- /dev/null +++ b/apps/docs-snippets2/sway/echo-configurables/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "echo-configurables" + +[dependencies] diff --git a/apps/docs-snippets2/sway/echo-configurables/src/main.sw b/apps/docs-snippets2/sway/echo-configurables/src/main.sw new file mode 100644 index 00000000000..d4f583f14d5 --- /dev/null +++ b/apps/docs-snippets2/sway/echo-configurables/src/main.sw @@ -0,0 +1,35 @@ +// #region configurable-constants-1 +contract; + +enum MyEnum { + Checked: (), + Pending: (), +} + +struct MyStruct { + x: u8, + y: u8, + state: MyEnum, +} + +configurable { + age: u8 = 25, + tag: str[4] = __to_str_array("fuel"), + grades: [u8; 4] = [3, 4, 3, 2], + my_struct: MyStruct = MyStruct { + x: 1, + y: 2, + state: MyEnum::Pending, + }, +} + +abi EchoConfigurables { + fn echo_configurables() -> (u8, str[4], [u8; 4], MyStruct); +} + +impl EchoConfigurables for Contract { + fn echo_configurables() -> (u8, str[4], [u8; 4], MyStruct) { + (age, tag, grades, my_struct) + } +} +// #endregion configurable-constants-1 diff --git a/apps/docs-snippets2/sway/log-values/Forc.toml b/apps/docs-snippets2/sway/log-values/Forc.toml new file mode 100644 index 00000000000..bb128fa0380 --- /dev/null +++ b/apps/docs-snippets2/sway/log-values/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "log-values" + +[dependencies] diff --git a/apps/docs-snippets2/sway/log-values/src/main.sw b/apps/docs-snippets2/sway/log-values/src/main.sw new file mode 100644 index 00000000000..5ac38ce1094 --- /dev/null +++ b/apps/docs-snippets2/sway/log-values/src/main.sw @@ -0,0 +1,18 @@ +// #region log-1 +contract; + +use std::logging::log; + +abi LogValues { + fn log_values(val1: u64, val2: b256, val3: str[4], val4: [u8; 3]); +} + +impl LogValues for Contract { + fn log_values(val1: u64, val2: b256, val3: str[4], val4: [u8; 3]) { + log(val1); + log(val2); + log(val3); + log(val4); + } +} +// #endregion log-1 diff --git a/apps/docs-snippets2/sway/my-contract/.gitignore b/apps/docs-snippets2/sway/my-contract/.gitignore new file mode 100644 index 00000000000..77d3844f58c --- /dev/null +++ b/apps/docs-snippets2/sway/my-contract/.gitignore @@ -0,0 +1,2 @@ +out +target diff --git a/apps/docs-snippets2/sway/my-contract/Forc.toml b/apps/docs-snippets2/sway/my-contract/Forc.toml new file mode 100644 index 00000000000..d77e0baea3c --- /dev/null +++ b/apps/docs-snippets2/sway/my-contract/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "my-contract" + +[dependencies] diff --git a/apps/docs-snippets2/sway/my-contract/src/main.sw b/apps/docs-snippets2/sway/my-contract/src/main.sw new file mode 100644 index 00000000000..7d4a75493c6 --- /dev/null +++ b/apps/docs-snippets2/sway/my-contract/src/main.sw @@ -0,0 +1,11 @@ +contract; + +abi MyContract { + fn test_function() -> bool; +} + +impl MyContract for Contract { + fn test_function() -> bool { + true + } +} diff --git a/apps/docs-snippets2/sway/return-context/Forc.toml b/apps/docs-snippets2/sway/return-context/Forc.toml new file mode 100644 index 00000000000..e148adc88e0 --- /dev/null +++ b/apps/docs-snippets2/sway/return-context/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "return-context" + +[dependencies] diff --git a/apps/docs-snippets2/sway/return-context/src/main.sw b/apps/docs-snippets2/sway/return-context/src/main.sw new file mode 100644 index 00000000000..5e0233719d3 --- /dev/null +++ b/apps/docs-snippets2/sway/return-context/src/main.sw @@ -0,0 +1,17 @@ +contract; + +use std::context::msg_amount; + +abi ReturnContext { + #[payable] + fn return_context_amount() -> u64; +} + +// #region return-context-contract +impl ReturnContext for Contract { + #[payable] + fn return_context_amount() -> u64 { + msg_amount() + } +} +// #endregion return-context-contract diff --git a/apps/docs-snippets2/sway/simple-token-abi/Forc.toml b/apps/docs-snippets2/sway/simple-token-abi/Forc.toml new file mode 100644 index 00000000000..c0186ab00ec --- /dev/null +++ b/apps/docs-snippets2/sway/simple-token-abi/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "simple_token_abi" + +[dependencies] diff --git a/apps/docs-snippets2/sway/simple-token-abi/src/main.sw b/apps/docs-snippets2/sway/simple-token-abi/src/main.sw new file mode 100644 index 00000000000..172836379dd --- /dev/null +++ b/apps/docs-snippets2/sway/simple-token-abi/src/main.sw @@ -0,0 +1,9 @@ +library; + +abi SimpleToken { + #[storage(read, write)] + fn deposit(address: b256, amount: u64); + + #[storage(read)] + fn get_balance(address: b256) -> u64; +} diff --git a/apps/docs-snippets2/sway/simple-token/Forc.toml b/apps/docs-snippets2/sway/simple-token/Forc.toml new file mode 100644 index 00000000000..273ee3a76b5 --- /dev/null +++ b/apps/docs-snippets2/sway/simple-token/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "simple-token" + +[dependencies] +simple_token_abi = { path = "../simple-token-abi" } diff --git a/apps/docs-snippets2/sway/simple-token/src/main.sw b/apps/docs-snippets2/sway/simple-token/src/main.sw new file mode 100644 index 00000000000..b77372198b2 --- /dev/null +++ b/apps/docs-snippets2/sway/simple-token/src/main.sw @@ -0,0 +1,22 @@ +// #region inter-contract-calls-1 +contract; + +use ::simple_token_abi::SimpleToken; + +use std::hash::*; +storage { + balances: StorageMap = StorageMap {}, +} +impl SimpleToken for Contract { + #[storage(read, write)] + fn deposit(address: b256, amount: u64) { + let current_balance = storage.balances.get(address).try_read().unwrap_or(0); + storage.balances.insert(address, current_balance + amount); + } + #[storage(read)] + fn get_balance(address: b256) -> u64 { + let balance = storage.balances.get(address).try_read().unwrap_or(0); + balance + } +} +// #endregion inter-contract-calls-1 diff --git a/apps/docs-snippets2/sway/token-depositor/Forc.toml b/apps/docs-snippets2/sway/token-depositor/Forc.toml new file mode 100644 index 00000000000..a021df06388 --- /dev/null +++ b/apps/docs-snippets2/sway/token-depositor/Forc.toml @@ -0,0 +1,8 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "token-depositor" + +[dependencies] +simple_token_abi = { path = "../simple-token-abi" } diff --git a/apps/docs-snippets2/sway/token-depositor/src/main.sw b/apps/docs-snippets2/sway/token-depositor/src/main.sw new file mode 100644 index 00000000000..75caff02db3 --- /dev/null +++ b/apps/docs-snippets2/sway/token-depositor/src/main.sw @@ -0,0 +1,26 @@ +// #region inter-contract-calls-2 +contract; + +use std::auth::msg_sender; + +use ::simple_token_abi::SimpleToken; + +abi TokenDepositor { + fn deposit_to_simple_token(contract_id: b256, amount: u64); +} + +impl TokenDepositor for Contract { + fn deposit_to_simple_token(contract_id: b256, amount: u64) { + let simple_token_contract = abi(SimpleToken, contract_id); + + let sender = msg_sender().unwrap(); + + let address: b256 = match sender { + Identity::Address(sender_param) => sender_param.bits(), + _ => revert(0), + }; + + simple_token_contract.deposit(address, amount); + } +} +// #endregion inter-contract-calls-2 diff --git a/apps/docs-snippets2/sway/transfer-to-address/Forc.toml b/apps/docs-snippets2/sway/transfer-to-address/Forc.toml new file mode 100644 index 00000000000..ae799670520 --- /dev/null +++ b/apps/docs-snippets2/sway/transfer-to-address/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "transfer-to-address" + +[dependencies] diff --git a/apps/docs-snippets2/sway/transfer-to-address/src/main.sw b/apps/docs-snippets2/sway/transfer-to-address/src/main.sw new file mode 100644 index 00000000000..254a4e98e63 --- /dev/null +++ b/apps/docs-snippets2/sway/transfer-to-address/src/main.sw @@ -0,0 +1,23 @@ +// #region full +contract; + +use std::asset::transfer; + +abi TransferToAddress { + #[payable] + fn transfer(amount_to_transfer: u64, asset_id: AssetId, recipient: b256); +} + +impl TransferToAddress for Contract { + #[payable] + fn transfer(amount_to_transfer: u64, asset_id: AssetId, recipient: b256) { + let recipient_address = Address::from(recipient); + + transfer( + Identity::Address(recipient_address), + asset_id, + amount_to_transfer, + ); + } +} +// #endregion full diff --git a/apps/docs/spell-check-custom-words.txt b/apps/docs/spell-check-custom-words.txt index 09ac6bcc297..ff1ea80f563 100644 --- a/apps/docs/spell-check-custom-words.txt +++ b/apps/docs/spell-check-custom-words.txt @@ -55,6 +55,7 @@ Collateralized compositional config config +configurables Config configs ContractFactory @@ -112,6 +113,7 @@ Executablity exponentiate Exponentiate extractable +estimateTxDependencies FIPS forc formatter diff --git a/apps/docs/src/guide/contracts/call-parameters.md b/apps/docs/src/guide/contracts/call-parameters.md index 32c6f870cb6..77034dcc392 100644 --- a/apps/docs/src/guide/contracts/call-parameters.md +++ b/apps/docs/src/guide/contracts/call-parameters.md @@ -11,6 +11,10 @@ When interacting with contracts, you can configure specific parameters for contr > **Note**: Setting transaction parameters is also available when calling contracts. More information on this can be found at [Transaction Parameters](../transactions/transaction-parameters.md). +The contract in use in this section has the following implementation: + +<<< @/../../docs-snippets2/sway/return-context/src/main.sw#return-context-contract{rust:line-numbers} + ## Forward Parameter @@ -20,7 +24,7 @@ The `forward` parameter allows the sending of a specific amount of coins to a co -<<< @/../../docs-snippets/src/guide/contracts/call-parameters.test.ts#call-params-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/call-parameters/forward.ts#forward{ts:line-numbers} ## Gas Limit Parameter @@ -31,16 +35,16 @@ The `gasLimit` refers to the maximum amount of gas that can be consumed specific -<<< @/../../docs-snippets/src/guide/contracts/call-parameters.test.ts#call-params-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/call-parameters/gas-fee.ts#gas-fee{ts:line-numbers} -## Call Parameters `gasLimit` vs Transaction Parameters `gasLimit` +## Call Parameter `gasLimit` vs Transaction Parameter `gasLimit` -The Call `gasLimit` parameter sets the maximum gas allowed for the actual contract call, whereas the Transaction `gasLimit` _(see [Transaction Parameters](../transactions/transaction-parameters.md))_ sets the maximum gas allowed for the entire transaction and constrains the `gasLimit` for the Call. If the Call `gasLimit` is set to a value greater than the _available_ Transaction gas, then the entire available Transaction gas will be allocated for the contract call execution. +The call parameter `gasLimit` sets the maximum gas allowed for the actual contract call, whereas the transaction parameter `gasLimit` _(see [Transaction Parameters](../transactions/transaction-parameters.md))_ sets the maximum gas allowed for the entire transaction and constrains the `gasLimit` call parameter. If the call parameter `gasLimit` is set to a value greater than the _available_ transaction gas, then the entire available transaction gas will be allocated for the contract call execution. -If you don't set the `gasLimit` for the Call, the Transaction `gasLimit` will be applied. +If you don't set the `gasLimit` for the call, the transaction `gasLimit` will be applied. ## Setting Both Parameters -You can set both Call Parameters and Transaction Parameters within the same contract function call. +You can set both call parameters and transaction parameters within the same contract function call. -<<< @/../../docs-snippets/src/guide/contracts/call-parameters.test.ts#call-params-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/call-parameters/setting-both-parameters.ts#setting-both-parameters{ts:line-numbers} diff --git a/apps/docs/src/guide/contracts/configurable-constants.md b/apps/docs/src/guide/contracts/configurable-constants.md index 90f3974e7b5..f0378c77592 100644 --- a/apps/docs/src/guide/contracts/configurable-constants.md +++ b/apps/docs/src/guide/contracts/configurable-constants.md @@ -10,24 +10,16 @@ This feature provides flexibility for dynamic contract environments. It allows a Below is an example of a contract in which we declare four configurable constants: -<<< @/../../docs-snippets/test/fixtures/forc-projects/echo-configurables/src/main.sw#configurable-constants-1{rust:line-numbers} +<<< @/../../docs-snippets2/sway/echo-configurables/src/main.sw#configurable-constants-1{rust:line-numbers} -In this contract, we have a function `echo_configurables` that returns the values of the configurable constants. - -If each of these constants has new values that have been assigned to them, the function will return the updated values. Otherwise, the function will return the default values. +In this contract, the function `echo_configurables` returns the values of the configurable constants, which we'll use for demonstrating the setting of configurables via the SDK. ## Setting New Values For Configurable Constants -During contract deployment, you can define new values for the configurable constants. This is achieved as follows: - -<<< @/../../docs-snippets/src/guide/contracts/configurable-constants.test.ts#configurable-constants-2{ts:line-numbers} - -You can assign new values to any of these configurable constants. - -If you wish to assign a new value to just one constant, you can do the following: +During contract deployment, you can define new values for any/all of the configurable constants. The example below shows setting of one configurable constant, while the others will have default values. -<<< @/../../docs-snippets/src/guide/contracts/configurable-constants.test.ts#configurable-constants-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/configurable-constants.ts#setting-configurable-constant{ts:line-numbers} Please note that when assigning new values for a `Struct`, all properties of the `Struct` must be defined. Failing to do so will result in an error: -<<< @/../../docs-snippets/src/guide/contracts/configurable-constants.test.ts#configurable-constants-4{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/configurable-constants.ts#invalid-configurable{ts:line-numbers} diff --git a/apps/docs/src/guide/contracts/contract-balance.md b/apps/docs/src/guide/contracts/contract-balance.md index 8bd08f0dc4f..4f3f1a4bd0f 100644 --- a/apps/docs/src/guide/contracts/contract-balance.md +++ b/apps/docs/src/guide/contracts/contract-balance.md @@ -4,32 +4,30 @@ When working with contracts, it's crucial to be aware of the available contract ## The `getBalance` Method -The [`Contract`](../../api/Program/Contract.md) class includes a method called `getBalance` that retrieves the available balance of a specific asset for a contract. This method is particularly useful for determining the remaining balance after sending assets to a contract and executing contract calls. +The [`Contract.getBalance`](../../api/Program/Contract.md#getbalance) method retrieves the available balance of a specific asset on your contract. This method is particularly useful for determining the remaining balance after sending assets to a contract and executing contract calls. -<<< @/../../../packages/program/src/contract.ts#contract-balance-1{ts:line-numbers} +It is important to note that this method returns the total available contract balance, regardless of how often assets have been sent to it or spent. ## Checking Contract Balance Consider a simple contract that transfers a specified amount of a given asset to an address: -<<< @/../../docs-snippets/test/fixtures/forc-projects/transfer-to-address/src/main.sw#contract-balance-2{rust:line-numbers} +<<< @/../../docs-snippets2/sway/transfer-to-address/src/main.sw#full{rust:line-numbers} The `transfer` function has three parameters: 1. `amount_to_transfer`: The amount that is being transferred. -2. `asset`: The address of the deployed contract Token. +2. `asset`: The address of the deployed contract token. -3. `recipient`: The address from the receiver's wallet. +3. `recipient`: The address of the receiver's wallet. The `transfer` function calls the built-in Sway function `transfer_to_address`, which does precisely what the name suggests. Let's execute this contract and use the `getBalance` method to validate the remaining asset amount the contract has left to spend. -<<< @/../../docs-snippets/src/guide/contracts/contract-balance.test.ts#contract-balance-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/contract-balance.ts#example{ts:line-numbers} -In this example, we first forward an asset amount greater than the amount required for the transfer and then execute the contract call. +In this example, we first forward an asset amount greater than the amount required for the transfer, and then we execute the contract call. Finally, we use the `getBalance` method to confirm that the contract balance is precisely the total forwarded amount minus the transferred amount. - -It is important to note that this method returns the total available contract balance, regardless of how often assets have been sent or spent on costly operations. diff --git a/apps/docs/src/guide/contracts/cost-estimation.md b/apps/docs/src/guide/contracts/cost-estimation.md index 7190bf53612..d3f76d9a333 100644 --- a/apps/docs/src/guide/contracts/cost-estimation.md +++ b/apps/docs/src/guide/contracts/cost-estimation.md @@ -1,6 +1,6 @@ # Estimating Contract Call Cost -The `getTransactionCost` function provided by the [Account](../../api/Account/Account.md) allows you to estimate the cost of a specific contract call. The return type, `TransactionCost`, is an object containing relevant information for the estimation: +The [`FunctionInvocationScope.getTransactionCost`](../../api/Program/FunctionInvocationScope.md#gettransactioncost) method allows you to estimate the cost of a specific contract call. The return type, `TransactionCost`, is an object containing relevant information for the estimation: <<< @/../../../packages/account/src/providers/provider.ts#cost-estimation-1{ts:line-numbers} @@ -8,10 +8,10 @@ The following example demonstrates how to get the estimated transaction cost for ## 1. Single contract call transaction: -<<< @/../../docs-snippets/src/guide/contracts/cost-estimation.test.ts#cost-estimation-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/cost-estimation.ts#cost-estimation-1{ts:line-numbers} ## 2. Multiple contract calls transaction: -<<< @/../../docs-snippets/src/guide/contracts/cost-estimation.test.ts#cost-estimation-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/cost-estimation.ts#cost-estimation-2{ts:line-numbers} You can use the transaction cost estimation to set the gas limit for an actual call or display the estimated cost to the user. diff --git a/apps/docs/src/guide/contracts/dependency-estimation.md b/apps/docs/src/guide/contracts/dependency-estimation.md index 77a8f434a72..7b8213381af 100644 --- a/apps/docs/src/guide/contracts/dependency-estimation.md +++ b/apps/docs/src/guide/contracts/dependency-estimation.md @@ -1,11 +1,9 @@ # Transaction Dependency Estimation -[Previously](./variable-outputs.md), we mentioned that a contract call might require you to manually specify external contracts or variable outputs. +In [variable outputs](./variable-outputs.md), we mention that a contract call might require you to manually specify external contracts or variable outputs. -However, the SDK always automatically estimates these dependencies and double-checks if everything is in order whenever you invoke a contract function or attempt to send a transaction. +However, by default the SDK always automatically estimates these dependencies and double-checks if everything is in order whenever you invoke a contract function or attempt to send a transaction. -The SDK uses the `estimateTxDependencies` helper function to set any missing dependencies identified during the estimation process. This requires simulating the transaction a few times in the background. - -<<< @/../../../packages/account/src/providers/provider.ts#Provider-sendTransaction{ts:line-numbers} +The SDK uses the [Provider.estimateTxDependencies](../../api/Account/Provider.md#estimatetxdependencies) method to set any missing dependencies identified during the estimation process. This requires simulating the transaction a few times in the background. While relying on the SDK's automatic estimation is a decent default behavior, we recommend manually specifying the dependencies if they are known in advance to avoid the performance impact of the estimation process. diff --git a/apps/docs/src/guide/contracts/deploying-contracts.md b/apps/docs/src/guide/contracts/deploying-contracts.md index 0d07a8cbb01..65013a7e1d5 100644 --- a/apps/docs/src/guide/contracts/deploying-contracts.md +++ b/apps/docs/src/guide/contracts/deploying-contracts.md @@ -11,7 +11,7 @@ To deploy a contract using the SDK, you can use the `ContractFactory`. This proc The SDK utilizes two different deployment processes, depending on the contract's size. The threshold for the contract size is dictated by the chain and can be queried: -<<< @/../../docs-snippets/src/guide/contracts/deploying-contracts.test.ts#get-contract-max-size{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/deploying-contracts/get-max-size.ts#full{ts:line-numbers} It either uses a single create transaction to deploy the entire contract bytecode, or it splits the contract bytecode into multiple chunks, deploys them as blobs (on chain data accessible to the VM), and then generates a contract from the associated blob IDs. That generated contract is then deployed as a create transaction. @@ -33,7 +33,7 @@ After writing a contract in Sway you can build the necessary deployment artifact Once you have the contract artifacts, it can be passed to the `ContractFactory` for deployment, like so: -<<< @/../../docs-snippets/src/guide/contracts/deploying-contracts.test.ts#setup{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/deploying-contracts/deployment.ts#setup{ts:line-numbers} ### 2. Contract Deployment @@ -41,7 +41,7 @@ As mentioned earlier, there are two different processes for contract deployment This call resolves as soon as the transaction to deploy the contract is submitted and returns three items: the `contractId`, a `waitForTransactionId` function and a `waitForResult` function. -<<< @/../../docs-snippets/src/guide/contracts/deploying-contracts.test.ts#deploy{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/deploying-contracts/deployment.ts#deploy{ts:line-numbers} The `contract` instance will be returned only after calling `waitForResult` and waiting for it to resolve. To avoid blocking the rest of your code, you can attach this promise to a hook or listener that will use the contract only after it is fully deployed. Similarly, the transaction ID is only available once the underlying transaction has been funded. To avoid blocking the code until the ID is ready, you can use the `waitForTransactionId` function to await it's retrieval. @@ -49,14 +49,14 @@ The `contract` instance will be returned only after calling `waitForResult` and Now that the contract is deployed, you can interact with it by submitting a contract call: -<<< @/../../docs-snippets/src/guide/contracts/deploying-contracts.test.ts#call{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/deploying-contracts/deployment.ts#call{ts:line-numbers} ## Deploying a Large Contract as Blobs -In the above guide we use the recommended `deploy` method. If you are working with a contract that is too large to be deployed in a single transaction, then the SDK will chunk the contract for you and submit it as blobs, to then be accessed later by a create transaction. This process is handled by the `deployAsBlobTx` method, also available on the `ContractFactory` should you want to use that directly. +In the above guide we use the recommended `deploy` method. If you are working with a contract that is too large to be deployed in a single transaction, then the SDK will chunk the contract for you and submit it as blobs, to then be accessed later by a create transaction. This process is handled by the [`ContractFactory.deployAsBlobTx`](../../api/Contract/ContractFactory.md#deployasblobtx) method. -<<< @/../../docs-snippets/src/guide/contracts/deploying-contracts.test.ts#blobs{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/deploying-contracts/deployment.ts#blobs{ts:line-numbers} In the above example, we also pass a `chunkSizeMultiplier` option to the deployment method. The SDK will attempt to chunk the contract to the most optimal about, however the transaction size can fluctuate and you can also be limited by request size limits against the node. By default we set a multiplier of 0.95, meaning the chunk size will be 95% of the potential maximum size, however you can adjust this to suit your needs and ensure the transaction passes. It must be set to a value between 0 and 1. -> **Note:** Deploying large contracts using blob transactions will take more time. Each transaction is dependent and has to wait for a block to be produced before it gets mined. Then a create transaction is submitted as normal. So you will need to wait longer than usual for the contract to be fully deployed and can be interacted with. \ No newline at end of file +> **Note:** Deploying large contracts using blob transactions will take more time. Each transaction is dependent and has to wait for a block to be produced before it gets mined. Then a create transaction is submitted as normal. So you will need to wait longer than usual for the contract to be fully deployed and can be interacted with. diff --git a/apps/docs/src/guide/contracts/index.md b/apps/docs/src/guide/contracts/index.md index 7a3ab6211c6..48b27499e10 100644 --- a/apps/docs/src/guide/contracts/index.md +++ b/apps/docs/src/guide/contracts/index.md @@ -9,6 +9,6 @@ In the Fuel Network, contracts play a crucial role in facilitating interactions For instance, consider a Sway contract with two ABI methods called `echo_str_8(str[8])` and `echo_u8(u8)`. After deploying the contract, you can call the methods as follows: -<<< @/../../docs-snippets/src/guide/contracts/index.test.ts#echo-values{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/introduction.ts#method-calls{ts:line-numbers} -The example above demonstrates a simple contract call using default configurations. The following sections will explore how to further configure various parameters for contract calls, allowing for more advanced interactions with your deployed contracts in the Fuel Network. \ No newline at end of file +The example above demonstrates a simple contract call using default configurations. The following sections will explore how to further configure various parameters for contract calls, allowing for more advanced interactions with your deployed contracts in the Fuel Network. diff --git a/apps/docs/src/guide/contracts/inter-contract-calls.md b/apps/docs/src/guide/contracts/inter-contract-calls.md index ad2aa1795b5..5cbcbf68ef7 100644 --- a/apps/docs/src/guide/contracts/inter-contract-calls.md +++ b/apps/docs/src/guide/contracts/inter-contract-calls.md @@ -10,18 +10,18 @@ In this example, we have a `SimpleToken` contract representing a basic token con Here's a simple token contract that allows holding balances: -<<< @/../../docs-snippets/test/fixtures/forc-projects/simple-token/src/main.sw#inter-contract-calls-1{ts:line-numbers} +<<< @/../../docs-snippets2/sway/simple-token/src/main.sw#inter-contract-calls-1{rs:line-numbers} ### Contract: `TokenDepositor` The `TokenDepositor` contract imports the `SimpleToken` contract and calls its `deposit` function to deposit tokens: -<<< @/../../docs-snippets/test/fixtures/forc-projects/token-depositor/src/main.sw#inter-contract-calls-2{ts:line-numbers} +<<< @/../../docs-snippets2/sway/token-depositor/src/main.sw#inter-contract-calls-2{rs:line-numbers} ## Inter-contract calls using the SDK Once both contracts are deployed, we can use the SDK to make the `TokenDepositor` contract to call the `SimpleToken` contract. -<<< @/../../docs-snippets/src/guide/contracts/inter-contract-calls.test.ts#inter-contract-calls-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/inter-contract-calls.ts#full{ts:line-numbers} Pay attention to the method `addContracts` called by the `TokenDepositor` contract. This method accepts an array of instances of deployed contracts. Without calling this method, the inter-contract call will not work. diff --git a/apps/docs/src/guide/contracts/logs.md b/apps/docs/src/guide/contracts/logs.md index 0c4628e0835..3692d97e120 100644 --- a/apps/docs/src/guide/contracts/logs.md +++ b/apps/docs/src/guide/contracts/logs.md @@ -4,10 +4,10 @@ When you log a value within a contract method, it generates a log entry that is Consider the following example contract: -<<< @/../../docs-snippets/test/fixtures/forc-projects/log-values/src/main.sw#log-1{rust:line-numbers} +<<< @/../../docs-snippets2/sway/log-values/src/main.sw#log-1{rust:line-numbers} To access the logged values in TypeScript, use the `logs` property in the response of a contract call. The logs data will be stored in an `Array`: -<<< @/../../docs-snippets/src/guide/contracts/logs.test.ts#log-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/logs.ts#full{ts:line-numbers} This approach allows you to work seamlessly with logged values in your contract, making it easier to understand and debug the contract's behavior. diff --git a/apps/docs/src/guide/contracts/managing-deployed-contracts.md b/apps/docs/src/guide/contracts/managing-deployed-contracts.md index dc13f7a19a0..a86bf63d169 100644 --- a/apps/docs/src/guide/contracts/managing-deployed-contracts.md +++ b/apps/docs/src/guide/contracts/managing-deployed-contracts.md @@ -20,7 +20,7 @@ When you log the `contractId` property of an instantiated Contract using `consol If you have already an instantiated and deployed contract in hands you can create another contract instance simply by using the `contractId` property and the contract JSON ABI: -<<< @/../../docs-snippets/src/guide/contracts/managing-deployed-contracts.test.ts#managing-deployed-contracts-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/managing-deployed-contracts.ts#with-contractId{ts:line-numbers} The previous example assumes that you have a [`Contract`](../../api/Program/Contract.md) instance at hand. However, some Fuel tools and Sway use the [`b256`](../types/bits256.md) type format, a hex-encoded string-like type, for contract IDs. @@ -28,4 +28,4 @@ You might have this format instead, for example, if you have deployed your contr The process of instantiating a [`Contract`](../../api/Program/Contract.md) remains the same when using a contract ID of type `b256`: -<<< @/../../docs-snippets/src/guide/contracts/managing-deployed-contracts.test.ts#managing-deployed-contracts-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/contracts/managing-deployed-contracts.ts#with-b256{ts:line-numbers} diff --git a/packages/account/src/providers/provider.ts b/packages/account/src/providers/provider.ts index c5119258914..c143a3bbee5 100644 --- a/packages/account/src/providers/provider.ts +++ b/packages/account/src/providers/provider.ts @@ -881,11 +881,9 @@ Supported fuel-core version: ${supportedVersion}.` { estimateTxDependencies = true }: ProviderSendTxParams = {} ): Promise { const transactionRequest = transactionRequestify(transactionRequestLike); - // #region Provider-sendTransaction if (estimateTxDependencies) { await this.estimateTxDependencies(transactionRequest); } - // #endregion Provider-sendTransaction this.validateTransaction(transactionRequest); diff --git a/packages/program/src/contract.ts b/packages/program/src/contract.ts index 1d2009e404d..e1caf4e8c21 100644 --- a/packages/program/src/contract.ts +++ b/packages/program/src/contract.ts @@ -118,9 +118,7 @@ export default class Contract implements AbstractContract { * @param assetId - The specified asset ID. * @returns The balance of the contract for the specified asset. */ - // #region contract-balance-1 getBalance(assetId: BytesLike) { return this.provider.getContractBalance(this.id, assetId); } - // #endregion contract-balance-1 } diff --git a/scripts/forc-format.sh b/scripts/forc-format.sh index 20f95316610..6b13df22634 100755 --- a/scripts/forc-format.sh +++ b/scripts/forc-format.sh @@ -1,13 +1,13 @@ #!/bin/bash main_dir=$(pwd) -forc_tomls=$(find . -type f -name "Forc.toml") +forc_tomls=$(find . -type f -name "Forc.toml" -not -path "./internal/**/*") forc_fmt=$(realpath ./internal/forc/forc-binaries/forc-fmt) expected_authors="authors = [\"Fuel Labs \"]" for forc_toml in $forc_tomls; do - # cd into the respective forc project + # cd into the respective forc project cd ${forc_toml/Forc.toml/''} # fix forc formatting