diff --git a/.changeset/smart-flowers-work.md b/.changeset/smart-flowers-work.md new file mode 100644 index 00000000000..4fca29d0b38 --- /dev/null +++ b/.changeset/smart-flowers-work.md @@ -0,0 +1,4 @@ +--- +--- + +docs: migrate `guide/transactions` snippets diff --git a/apps/docs-snippets/src/guide/transactions/transaction-policies.test.ts b/apps/docs-snippets/src/guide/transactions/transaction-policies.test.ts deleted file mode 100644 index e0a013fda0e..00000000000 --- a/apps/docs-snippets/src/guide/transactions/transaction-policies.test.ts +++ /dev/null @@ -1,92 +0,0 @@ -import type { TransactionResponse, Policy, BNInput } from 'fuels'; -import { ScriptTransactionRequest, bn, PolicyType } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { SumScript } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('Transaction Policies', () => { - it('sets policies', () => { - // #region transaction-policies-1 - // #import { ScriptTransactionRequest }; - - const transactionRequest = new ScriptTransactionRequest({ - tip: bn(10), // Sets the tip policy - witnessLimit: bn(1), // Sets the witness limit policy - maturity: 1, // Sets the maturity policy - maxFee: bn(1), // Sets the max fee policy - }); - // #endregion transaction-policies-1 - - const { policyTypes, policies } = ScriptTransactionRequest.getPolicyMeta(transactionRequest); - - expect(policyTypes).toBe(15); - expect(policies?.[0].type).toBe(PolicyType.Tip); - expect(bn(policies?.[0].data).eq(transactionRequest.tip as BNInput)).toBeTruthy(); - expect(policies?.[1].type).toBe(PolicyType.WitnessLimit); - expect(bn(policies?.[1].data).eq(bn(transactionRequest.witnessLimit))).toBeTruthy(); - expect(policies?.[2].type).toBe(PolicyType.Maturity); - expect(policies?.[2]?.data).toBe(transactionRequest.maturity); - expect(policies?.[3].type).toBe(PolicyType.MaxFee); - expect(bn(policies?.[3].data).eq(bn(transactionRequest.maxFee))).toBeTruthy(); - }); - - it('gets transaction response from tx id', async () => { - using launched = await launchTestNode({ - nodeOptions: { - args: ['--poa-instant', 'false', '--poa-interval-period', '1ms'], - }, - }); - const { - wallets: [wallet], - } = launched; - const { provider } = launched; - const scriptMainFunctionArguments = [1]; - const resources = await wallet.getResourcesToSpend([ - { amount: 1000, assetId: provider.getBaseAssetId() }, - ]); - - // #region transaction-policies-2 - // #import { ScriptTransactionRequest, TransactionResponse, Policy }; - - // Instantiate the transaction request with transaction parameters that would - // set the respective policies. - const transactionRequest = new ScriptTransactionRequest({ - script: SumScript.bytecode, - gasLimit: bn(2000), - maturity: 2, - tip: bn(3), - witnessLimit: 900, - maxFee: bn(60_000), - }); - - // Set the script main function arguments - transactionRequest.setData(SumScript.abi, scriptMainFunctionArguments); - - // Fund the transaction - transactionRequest.addResources(resources); - - // Submit the transaction and retrieve the transaction response - const tx: TransactionResponse = await wallet.sendTransaction(transactionRequest); - const response = await tx.waitForResult(); - // is undefined if the transaction had no policies applied. - const policies: Policy[] | undefined = response.transaction.policies; - // #endregion transaction-policies-2 - - if (!policies) { - throw new Error('No policies found'); - } - - expect(policies?.[0].type).toBe(PolicyType.Tip); - expect(bn(policies?.[0].data).eq(transactionRequest.tip as BNInput)).toBeTruthy(); - expect(policies?.[1].type).toBe(PolicyType.WitnessLimit); - expect(bn(policies?.[1].data).eq(bn(transactionRequest.witnessLimit))).toBeTruthy(); - expect(policies?.[2].type).toBe(PolicyType.Maturity); - expect(policies?.[2]?.data).toBe(transactionRequest.maturity); - expect(policies?.[3].type).toBe(PolicyType.MaxFee); - expect(bn(policies?.[3].data).eq(bn(transactionRequest.maxFee))).toBeTruthy(); - }); -}); diff --git a/apps/docs-snippets/src/guide/transactions/transaction-request.test.ts b/apps/docs-snippets/src/guide/transactions/transaction-request.test.ts deleted file mode 100644 index a83500e6012..00000000000 --- a/apps/docs-snippets/src/guide/transactions/transaction-request.test.ts +++ /dev/null @@ -1,216 +0,0 @@ -import type { Account, Coin, Resource } from 'fuels'; -import { - CreateTransactionRequest, - ScriptTransactionRequest, - arrayify, - ZeroBytes32, - Address, - bn, - Predicate, - WalletUnlocked, -} from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { SimplePredicate, SumScript } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('Transaction Request', () => { - const address = Address.fromRandom(); - - it('creates a transaction request from ScriptTransactionRequest', () => { - const scriptMainFunctionArguments = [1]; - - // #region transaction-request-1 - // #import { ScriptTransactionRequest }; - - // Instantiate the transaction request using a ScriptTransactionRequest - const transactionRequest = new ScriptTransactionRequest({ - script: SumScript.bytecode, - }); - - // Set the script main function arguments (can also be passed in the class constructor) - transactionRequest.setData(SumScript.abi, scriptMainFunctionArguments); - // #endregion transaction-request-1 - - expect(transactionRequest.script).toEqual(arrayify(SumScript.bytecode)); - }); - - it('creates a transaction request fromm a CreateTransactionRequest', () => { - const contractByteCode = ZeroBytes32; - - // #region transaction-request-2 - // #import { CreateTransactionRequest }; - - // Instantiate the transaction request using a CreateTransactionRequest - const transactionRequest = new CreateTransactionRequest({ - witnesses: [contractByteCode], - }); - // #endregion transaction-request-2 - - expect(transactionRequest.witnesses[0]).toEqual(contractByteCode); - }); - - it('modifies a transaction request', async () => { - using launched = await launchTestNode(); - const { provider } = launched; - - const message = { - assetId: provider.getBaseAssetId(), - sender: address, - recipient: address, - nonce: '0x', - amount: bn(0), - daHeight: bn(0), - }; - const coin: Coin = { - id: '0x', - assetId: provider.getBaseAssetId(), - amount: bn(0), - owner: address, - blockCreated: bn(0), - txCreatedIdx: bn(0), - }; - const recipientAddress = address; - const resource = coin; - const resources: Resource[] = [resource]; - - // #region transaction-request-3 - // #import { ScriptTransactionRequest }; - - // Instantiate the transaction request - const transactionRequest = new ScriptTransactionRequest({ - script: SumScript.bytecode, - }); - - // Adding resources (coins or messages) - transactionRequest.addResources(resources); - transactionRequest.addResource(resource); - - // Adding coin inputs and outputs (including transfer to recipient) - transactionRequest.addCoinInput(coin); - transactionRequest.addCoinOutput(recipientAddress, 1000, provider.getBaseAssetId()); - - // Adding message inputs - transactionRequest.addMessageInput(message); - // #endregion transaction-request-3 - - expect(transactionRequest.script).toEqual(arrayify(SumScript.bytecode)); - expect(transactionRequest.inputs.length).toEqual(4); - expect(transactionRequest.outputs.length).toEqual(2); - expect(transactionRequest.witnesses.length).toEqual(1); - }); - - it('adds a contract to a transaction request', () => { - const contractId = Address.fromRandom(); - - // #region transaction-request-4 - // #import { ScriptTransactionRequest }; - - // Instantiate the transaction request - const transactionRequest = new ScriptTransactionRequest({ - script: SumScript.bytecode, - }); - - // Add the contract input and output using the contract ID - transactionRequest.addContractInputAndOutput(contractId); - // #endregion transaction-request-4 - - expect(transactionRequest.inputs.length).toEqual(1); - expect(transactionRequest.outputs.length).toEqual(1); - }); - - it('adds a predicate to a transaction request', async () => { - using launched = await launchTestNode(); - const { - provider, - wallets: [fundedWallet], - } = launched; - - const dataToValidatePredicate = [ZeroBytes32]; - - // #region transaction-request-5 - // #import { ScriptTransactionRequest, Predicate }; - - // Instantiate the transaction request - const transactionRequest = new ScriptTransactionRequest({ - script: SumScript.bytecode, - }); - - // Instantiate the predicate and pass valid input data to validate - // the predicate and unlock the funds - const predicate = new Predicate({ - bytecode: SimplePredicate.bytecode, - abi: SimplePredicate.abi, - data: dataToValidatePredicate, - provider, - }); - - // fund the predicate - const tx1 = await fundedWallet.transfer(predicate.address, bn(100_000)); - await tx1.waitForResult(); - - const predicateCoins = await predicate.getResourcesToSpend([ - { amount: 2000, assetId: provider.getBaseAssetId() }, - ]); - - // Add the predicate input and resources - transactionRequest.addResources(predicateCoins); - // #endregion transaction-request-5 - - expect(transactionRequest.inputs.length).toBeGreaterThanOrEqual(1); - expect(transactionRequest.outputs.length).toEqual(1); - }); - - it('adds a witness to a transaction request', async () => { - using launched = await launchTestNode(); - const { provider } = launched; - - const witness = ZeroBytes32; - - // #region transaction-request-6 - // #import { ScriptTransactionRequest, Account, WalletUnlocked }; - - // Instantiate the transaction request - const transactionRequest = new ScriptTransactionRequest({ - script: SumScript.bytecode, - }); - - // Add a witness directly - transactionRequest.addWitness(witness); - - // Add a witness using an account - const account: Account = WalletUnlocked.generate({ provider }); - await transactionRequest.addAccountWitnesses(account); - // #endregion transaction-request-6 - - expect(transactionRequest.witnesses.length).toEqual(2); - }); - - it('gets the transaction ID', async () => { - using launched = await launchTestNode(); - const { provider } = launched; - - // #region transaction-request-7 - // #import { ScriptTransactionRequest }; - - // Instantiate the transaction request - const transactionRequest = new ScriptTransactionRequest({ - script: SumScript.bytecode, - }); - - // Get the chain ID - const chainId = provider.getChainId(); - - // Get the transaction ID using the Chain ID - const transactionId = transactionRequest.getTransactionId(chainId); - // TX ID: 0x420f6... - // #endregion transaction-request-7 - - expect(transactionId).toBe( - '0x489c8694c5e20bf9202150728cb6ccad4b21e215c509d6ef065a5c9354e0ba08' - ); - }); -}); diff --git a/apps/docs-snippets/src/guide/transactions/transaction-response.test.ts b/apps/docs-snippets/src/guide/transactions/transaction-response.test.ts deleted file mode 100644 index 028bd05ab5b..00000000000 --- a/apps/docs-snippets/src/guide/transactions/transaction-response.test.ts +++ /dev/null @@ -1,115 +0,0 @@ -import { ScriptTransactionRequest, TransactionResponse } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -import { CounterFactory, SumScript } from '../../../test/typegen'; - -/** - * @group node - * @group browser - */ -describe('Transaction Response', () => { - it('gets transaction response from contract call', async () => { - using launched = await launchTestNode({ - contractsConfigs: [ - { - factory: CounterFactory, - }, - ], - }); - - const { - contracts: [contract], - } = launched; - - // #region transaction-response-1 - // Call a contract function - const call = await contract.functions.increment_counter(15).call(); - - // Wait for the result - const { transactionResponse } = await call.waitForResult(); - - // Retrieve the full transaction summary - const transactionSummary = await transactionResponse.getTransactionSummary(); - // #endregion transaction-response-1 - - expect(call.transactionId).toEqual(transactionSummary.id); - }); - - it('gets transaction response from transaction request', async () => { - using launched = await launchTestNode(); - const { - wallets: [wallet], - } = launched; - - const scriptMainFunctionArguments = [1]; - - // #region transaction-response-2 - // #import { ScriptTransactionRequest, TransactionResponse }; - - // Instantiate the transaction request using a ScriptTransactionRequest and set - // the script main function arguments - const transactionRequest = new ScriptTransactionRequest({ - script: SumScript.bytecode, - }); - transactionRequest.setData(SumScript.abi, scriptMainFunctionArguments); - - // Fund the transaction - const txCost = await wallet.getTransactionCost(transactionRequest); - - transactionRequest.maxFee = txCost.maxFee; - transactionRequest.gasLimit = txCost.gasUsed; - - await wallet.fund(transactionRequest, txCost); - - // Submit the transaction - const response: TransactionResponse = await wallet.sendTransaction(transactionRequest); - - // Generate the transaction summary - const transactionSummary = await response.getTransactionSummary(); - // #endregion transaction-response-2 - - expect(response.id).toEqual(transactionSummary.id); - }); - - it('gets transaction response from tx id', async () => { - using launched = await launchTestNode(); - const { - provider, - wallets: [wallet], - } = launched; - - const scriptMainFunctionArguments = [1]; - - const transactionRequest = new ScriptTransactionRequest({ - script: SumScript.bytecode, - }); - transactionRequest.setData(SumScript.abi, scriptMainFunctionArguments); - - const txCost = await wallet.getTransactionCost(transactionRequest); - - transactionRequest.maxFee = txCost.maxFee; - transactionRequest.gasLimit = txCost.gasUsed; - - await wallet.fund(transactionRequest, txCost); - const response: TransactionResponse = await wallet.sendTransaction(transactionRequest); - - const previouslySubmittedTransactionId = transactionRequest.getTransactionId( - provider.getChainId() - ); - - // #region transaction-response-3 - // Take a transaction ID from a previous transaction - const transactionId = previouslySubmittedTransactionId; - // 0x... - - // Retrieve the transaction response from the transaction ID - const transactionResponse = await TransactionResponse.create(transactionId, provider); - - // Generate the transaction summary - const transactionSummary = await transactionResponse.getTransactionSummary(); - // #endregion transaction-response-3 - - expect(response.id).toEqual(transactionSummary.id); - expect(transactionId).toEqual(transactionSummary.id); - }); -}); diff --git a/apps/docs-snippets/src/guide/transactions/transactions.test.ts b/apps/docs-snippets/src/guide/transactions/transactions.test.ts deleted file mode 100644 index 4b572a3926d..00000000000 --- a/apps/docs-snippets/src/guide/transactions/transactions.test.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { Wallet } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -/** - * @group node - * @group browser - */ -describe('Transactions', () => { - it('transfers assets', async () => { - using launched = await launchTestNode(); - const { - wallets: [sender], - provider, - } = launched; - const assetIdToTransfer = provider.getBaseAssetId(); - const receiver = Wallet.generate({ provider }); - - const initialBalance = await receiver.getBalance(assetIdToTransfer); - expect(initialBalance.toNumber()).toBe(0); - - // #region transactions-1 - const tx = await sender.transfer(receiver.address, 100, assetIdToTransfer); - await tx.waitForResult(); - - const newBalance = await receiver.getBalance(provider.getBaseAssetId()); - // 100 - // #endregion transactions-1 - - expect(newBalance.toNumber()).toBe(100); - }); -}); diff --git a/apps/docs-snippets2/src/transactions/transaction-policies/policies-from-response.ts b/apps/docs-snippets2/src/transactions/transaction-policies/policies-from-response.ts new file mode 100644 index 00000000000..2799c5e4d99 --- /dev/null +++ b/apps/docs-snippets2/src/transactions/transaction-policies/policies-from-response.ts @@ -0,0 +1,40 @@ +// #region transaction-policies-2 +import type { Policy } from 'fuels'; +import { Provider, Wallet, ScriptTransactionRequest, bn } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { ScriptSum } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +/** + * Instantiate the transaction request with transaction parameters + * that will set the respective policies. + */ +const transactionRequest = new ScriptTransactionRequest({ + script: ScriptSum.bytecode, + gasLimit: bn(2000), + tip: bn(10), + witnessLimit: 900, + maxFee: bn(2000), +}); + +// Set the script main function arguments +const scriptArguments = [1]; +transactionRequest.setData(ScriptSum.abi, scriptArguments); + +// Fund the transaction +const resources = await wallet.getResourcesToSpend([ + { amount: 1000, assetId: provider.getBaseAssetId() }, +]); + +transactionRequest.addResources(resources); + +// Submit the transaction and retrieve the transaction response +const tx = await wallet.sendTransaction(transactionRequest); +const response = await tx.waitForResult(); +const policies: Policy[] | undefined = response.transaction.policies; + +console.log('policies', policies); +// #endregion transaction-policies-2 diff --git a/apps/docs-snippets2/src/transactions/transaction-policies/setting-policies.ts b/apps/docs-snippets2/src/transactions/transaction-policies/setting-policies.ts new file mode 100644 index 00000000000..65611d36a66 --- /dev/null +++ b/apps/docs-snippets2/src/transactions/transaction-policies/setting-policies.ts @@ -0,0 +1,12 @@ +// #region transaction-policies-1 +import { bn, ScriptTransactionRequest } from 'fuels'; + +const transactionRequest = new ScriptTransactionRequest({ + tip: bn(10), // Sets the tip policy + witnessLimit: bn(1), // Sets the witness limit policy + maturity: 1, // Sets the maturity policy + maxFee: bn(1), // Sets the max fee policy +}); +// #endregion transaction-policies-1 + +console.log('transactionRequest', transactionRequest); diff --git a/apps/docs-snippets2/src/transactions/transaction-request/add-predicate.ts b/apps/docs-snippets2/src/transactions/transaction-request/add-predicate.ts new file mode 100644 index 00000000000..ed60d61920f --- /dev/null +++ b/apps/docs-snippets2/src/transactions/transaction-request/add-predicate.ts @@ -0,0 +1,45 @@ +import { + bn, + Predicate, + Provider, + ScriptTransactionRequest, + Wallet, + ZeroBytes32, +} from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { ScriptSum, SimplePredicate } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +// #region transaction-request-5 +// Instantiate the transaction request +const transactionRequest = new ScriptTransactionRequest({ + script: ScriptSum.bytecode, +}); + +const predicateArguments = [ZeroBytes32]; + +/** + * Instantiate the predicate and pass valid input data to validate + * the predicate and unlock the funds + */ +const predicate = new Predicate({ + bytecode: SimplePredicate.bytecode, + abi: SimplePredicate.abi, + data: predicateArguments, + provider, +}); + +// Fund the predicate +const tx = await wallet.transfer(predicate.address, bn(100_000)); +await tx.waitForResult(); + +const predicateCoins = await predicate.getResourcesToSpend([ + { amount: 2000, assetId: provider.getBaseAssetId() }, +]); + +// Add the predicate input and resources +transactionRequest.addResources(predicateCoins); +// #endregion transaction-request-5 diff --git a/apps/docs-snippets2/src/transactions/transaction-request/add-witness.ts b/apps/docs-snippets2/src/transactions/transaction-request/add-witness.ts new file mode 100644 index 00000000000..5ac35e1a72d --- /dev/null +++ b/apps/docs-snippets2/src/transactions/transaction-request/add-witness.ts @@ -0,0 +1,38 @@ +import type { Account } from 'fuels'; +import { + Provider, + ScriptTransactionRequest, + WalletUnlocked, + ZeroBytes32, +} from 'fuels'; + +import { LOCAL_NETWORK_URL } from '../../env'; +import { ScriptSum } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const witness = ZeroBytes32; + +const transactionRequest = new ScriptTransactionRequest({ + script: ScriptSum.bytecode, +}); + +// #region transaction-request-6 +// Add a witness directly +transactionRequest.addWitness(witness); + +// Add a witness using an account +const account: Account = WalletUnlocked.generate({ provider }); +await transactionRequest.addAccountWitnesses(account); +// #endregion transaction-request-6 + +// #region transaction-request-7 +// Get the chain ID +const chainId = provider.getChainId(); + +// Get the transaction ID using the Chain ID +const transactionId = transactionRequest.getTransactionId(chainId); +// TX ID: 0x420f6... +// #endregion transaction-request-7 + +console.log('transactionId', transactionId); +console.log('witnesses', transactionRequest.witnesses.length === 2); diff --git a/apps/docs-snippets2/src/transactions/transaction-request/create-request.ts b/apps/docs-snippets2/src/transactions/transaction-request/create-request.ts new file mode 100644 index 00000000000..bd8bd18b476 --- /dev/null +++ b/apps/docs-snippets2/src/transactions/transaction-request/create-request.ts @@ -0,0 +1,30 @@ +// #region transaction-request-1 +import { + CreateTransactionRequest, + ScriptTransactionRequest, + ZeroBytes32, +} from 'fuels'; + +import { ScriptSum } from '../../typegend'; + +// Instantiate the transaction request using a ScriptTransactionRequest +const scriptTransactionRequest = new ScriptTransactionRequest({ + script: ScriptSum.bytecode, +}); + +const scriptData = [1]; + +// Set the script main function arguments (can also be passed in the class constructor) +scriptTransactionRequest.setData(ScriptSum.abi, scriptData); +// #endregion transaction-request-1 + +const contractByteCode = ZeroBytes32; + +// #region transaction-request-2 +// Instantiate the transaction request using a CreateTransactionRequest +const createTransactionRequest = new CreateTransactionRequest({ + witnesses: [contractByteCode], +}); +// #endregion transaction-request-2 + +console.log('createTransactionRequest', createTransactionRequest); diff --git a/apps/docs-snippets2/src/transactions/transaction-request/fund-request.ts b/apps/docs-snippets2/src/transactions/transaction-request/fund-request.ts new file mode 100644 index 00000000000..3bd2b3a9f70 --- /dev/null +++ b/apps/docs-snippets2/src/transactions/transaction-request/fund-request.ts @@ -0,0 +1,59 @@ +import type { Coin, MessageCoin, Resource } from 'fuels'; +import { Address, bn, Provider, ScriptTransactionRequest, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { CounterFactory, ScriptSum } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const address = Address.fromRandom(); + +const message: MessageCoin = { + assetId: provider.getBaseAssetId(), + sender: address, + recipient: address, + nonce: '0x', + amount: bn(0), + daHeight: bn(0), +}; +const coin: Coin = { + id: '0x', + assetId: provider.getBaseAssetId(), + amount: bn(0), + owner: address, + blockCreated: bn(0), + txCreatedIdx: bn(0), +}; +const recipientAddress = address; +const resource = coin; +const resources: Resource[] = [resource]; + +// #region transaction-request-3 +// Instantiate the transaction request +const transactionRequest = new ScriptTransactionRequest({ + script: ScriptSum.bytecode, +}); + +// Adding resources (coins or messages) +transactionRequest.addResources(resources); +transactionRequest.addResource(resource); + +// Adding coin inputs and outputs (including transfer to recipient) +transactionRequest.addCoinInput(coin); +transactionRequest.addCoinOutput( + recipientAddress, + 1000, + provider.getBaseAssetId() +); + +// Adding message inputs +transactionRequest.addMessageInput(message); +// #endregion transaction-request-3 + +// #region transaction-request-4 +const deploy = await CounterFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// Add the contract input and output using the contract ID +transactionRequest.addContractInputAndOutput(contract.id); +// #endregion transaction-request-4 diff --git a/apps/docs-snippets2/src/transactions/transaction-response/contract-call.ts b/apps/docs-snippets2/src/transactions/transaction-response/contract-call.ts new file mode 100644 index 00000000000..ec7ce203469 --- /dev/null +++ b/apps/docs-snippets2/src/transactions/transaction-response/contract-call.ts @@ -0,0 +1,23 @@ +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { CounterFactory } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +const deploy = await CounterFactory.deploy(wallet); +const { contract } = await deploy.waitForResult(); + +// #region transaction-response-1 +// Call a contract function +const call = await contract.functions.increment_count(15).call(); + +// Wait for the result +const { transactionResponse } = await call.waitForResult(); + +// Retrieve the full transaction summary +const transactionSummary = await transactionResponse.getTransactionSummary(); +// #endregion transaction-response-1 + +console.log('transactionSummary', transactionSummary); diff --git a/apps/docs-snippets2/src/transactions/transaction-response/from-submitted-request.ts b/apps/docs-snippets2/src/transactions/transaction-response/from-submitted-request.ts new file mode 100644 index 00000000000..55233a03019 --- /dev/null +++ b/apps/docs-snippets2/src/transactions/transaction-response/from-submitted-request.ts @@ -0,0 +1,61 @@ +import { + Provider, + ScriptTransactionRequest, + TransactionResponse, + Wallet, +} from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; +import { ScriptSum } from '../../typegend'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +// #region transaction-response-2 +/** + * Instantiate the transaction request using a ScriptTransactionRequest and + * set the script main function arguments + */ +const transactionRequest = new ScriptTransactionRequest({ + script: ScriptSum.bytecode, +}); + +const scriptMainFunctionArguments = [1]; + +transactionRequest.setData(ScriptSum.abi, scriptMainFunctionArguments); + +// Fund the transaction +const txCost = await wallet.getTransactionCost(transactionRequest); + +transactionRequest.maxFee = txCost.maxFee; +transactionRequest.gasLimit = txCost.gasUsed; + +await wallet.fund(transactionRequest, txCost); + +// Submit the transaction +const response = await wallet.sendTransaction(transactionRequest); + +// Generate the transaction summary +const transactionSummary = await response.getTransactionSummary(); +// #endregion transaction-response-2 + +console.log('transactionSummary', transactionSummary); + +const previouslySubmittedTransactionId = response.id; + +// #region transaction-response-3 +// Take a transaction ID from a previous transaction +const transactionId = previouslySubmittedTransactionId; +// 0x... + +// Retrieve the transaction response from the transaction ID +const transactionResponse = await TransactionResponse.create( + transactionId, + provider +); + +// Generate the transaction summary +const summary = await transactionResponse.getTransactionSummary(); +// #endregion transaction-response-3 + +console.log('summary', summary); diff --git a/apps/docs-snippets2/src/transactions/transaction.ts b/apps/docs-snippets2/src/transactions/transaction.ts new file mode 100644 index 00000000000..3bdb10cbcfd --- /dev/null +++ b/apps/docs-snippets2/src/transactions/transaction.ts @@ -0,0 +1,17 @@ +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY, WALLET_PVT_KEY_2 } from '../env'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const sender = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const assetId = provider.getBaseAssetId(); +const receiver = Wallet.fromPrivateKey(WALLET_PVT_KEY_2, provider); + +// #region transactions-1 +const tx = await sender.transfer(receiver.address, 100, assetId); +await tx.waitForResult(); + +const newBalance = await receiver.getBalance(provider.getBaseAssetId()); +// 100 +// #endregion transactions-1 +console.log('balance', newBalance.toNumber()); diff --git a/apps/docs-snippets2/sway/Forc.toml b/apps/docs-snippets2/sway/Forc.toml index 5165ca3bdbd..32cda926a6d 100644 --- a/apps/docs-snippets2/sway/Forc.toml +++ b/apps/docs-snippets2/sway/Forc.toml @@ -1,17 +1,18 @@ [workspace] members = [ + "bytecode-input", "counter", - "script-sum", - "echo-values", "echo-asset-id", "echo-bytes", - "echo-enum", - "sum-option-u8", - "employee-data", - "echo-std-string", - "bytecode-input", "echo-employee-data-vector", - "echo-raw-slice", + "echo-enum", "echo-evm-address", + "echo-raw-slice", + "echo-std-string", + "echo-values", + "employee-data", "input-output-types", + "script-sum", + "simple-predicate", + "sum-option-u8", ] diff --git a/apps/docs-snippets2/sway/simple-predicate/Forc.toml b/apps/docs-snippets2/sway/simple-predicate/Forc.toml new file mode 100644 index 00000000000..199aae5c457 --- /dev/null +++ b/apps/docs-snippets2/sway/simple-predicate/Forc.toml @@ -0,0 +1,7 @@ +[project] +authors = ["Fuel Labs "] +entry = "main.sw" +license = "Apache-2.0" +name = "simple-predicate" + +[dependencies] diff --git a/apps/docs-snippets2/sway/simple-predicate/src/main.sw b/apps/docs-snippets2/sway/simple-predicate/src/main.sw new file mode 100644 index 00000000000..9dae8161a69 --- /dev/null +++ b/apps/docs-snippets2/sway/simple-predicate/src/main.sw @@ -0,0 +1,9 @@ +// #region send-and-spend-funds-from-predicates-1 +predicate; + +fn main(input_address: b256) -> bool { + let valid_address = 0xfc05c23a8f7f66222377170ddcbfea9c543dff0dd2d2ba4d0478a4521423a9d4; + + input_address == valid_address +} +// #endregion send-and-spend-funds-from-predicates-1 diff --git a/apps/docs/src/guide/transactions/index.md b/apps/docs/src/guide/transactions/index.md index 4c650e4c31c..117700a95f9 100644 --- a/apps/docs/src/guide/transactions/index.md +++ b/apps/docs/src/guide/transactions/index.md @@ -4,7 +4,7 @@ A transaction is a way of interacting with a Fuel blockchain and can include act Transferring assets is the most common transaction type and can be be executed by calling the `transfer` function from an account to a recipient address: -<<< @/../../docs-snippets/src/guide/transactions/transactions.test.ts#transactions-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/transactions/transaction.ts#transactions-1{ts:line-numbers} Deploying and interacting with contracts are other common transactions. More information on this can be found in the [contracts guide](../contracts/index.md), either through the [contract deployment guide](../contracts/deploying-contracts.md) or the [contract interaction guide](../contracts/methods.md). diff --git a/apps/docs/src/guide/transactions/transaction-policies.md b/apps/docs/src/guide/transactions/transaction-policies.md index 2689a59bb95..778c73c4fc2 100644 --- a/apps/docs/src/guide/transactions/transaction-policies.md +++ b/apps/docs/src/guide/transactions/transaction-policies.md @@ -22,10 +22,10 @@ The maximum amount you're willing to pay for the transaction using the base asse The following snippet shows which transaction parameters correspond to which policies: -<<< @/../../docs-snippets/src/guide/transactions/transaction-policies.test.ts#transaction-policies-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/transactions/transaction-policies/setting-policies.ts#transaction-policies-1{ts:line-numbers} ## Retrieving Transaction Policies from a Transaction Policies used for a transaction can be retrieved from a transaction using a `TransactionResponse`. The below snippet will show how to retrieve the policies from a transaction: -<<< @/../../docs-snippets/src/guide/transactions/transaction-policies.test.ts#transaction-policies-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/transactions/transaction-policies/policies-from-response.ts#transaction-policies-2{ts:line-numbers} diff --git a/apps/docs/src/guide/transactions/transaction-request.md b/apps/docs/src/guide/transactions/transaction-request.md index b7d14287d9c..00c1e473421 100644 --- a/apps/docs/src/guide/transactions/transaction-request.md +++ b/apps/docs/src/guide/transactions/transaction-request.md @@ -18,11 +18,11 @@ To create a transaction request, you must first instantiate either a `ScriptTran A `ScriptTransactionRequest` is used for script transactions, which allows you to execute bytecode on chain to perform a task or chain of tasks. Within the SDK they can be created like so: -<<< @/../../docs-snippets/src/guide/transactions/transaction-request.test.ts#transaction-request-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/transactions/transaction-request/create-request.ts#transaction-request-1{ts:line-numbers} A `CreateTransactionRequest` is used for create transactions, which are transactions that create a new contract on the blockchain. -<<< @/../../docs-snippets/src/guide/transactions/transaction-request.test.ts#transaction-request-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/transactions/transaction-request/create-request.ts#transaction-request-2{ts:line-numbers} > **Note**: We recommend you use the `ContractFactory` for contract deployment as this will shape the create transaction request for you. Information on this can be found in the [contract deployment guide](../contracts/deploying-contracts.md#2-contract-deployment). @@ -34,19 +34,19 @@ Once you have instantiated a transaction request, you can modify it by setting t Resources populate the inputs and outputs of a transaction request. This can take the form of coins, messages or contracts. The SDK provides a range of methods for dealing with resources. Below will detail how coins and messages can be added to a transaction request. -<<< @/../../docs-snippets/src/guide/transactions/transaction-request.test.ts#transaction-request-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/transactions/transaction-request/fund-request.ts#transaction-request-3{ts:line-numbers} ### Adding a Contract to a Transaction Request Scripts can perform multiple actions on chain, therefore you may want to chain contract calls. For this you will need to add a contract to the transaction request. This can be done like so: -<<< @/../../docs-snippets/src/guide/transactions/transaction-request.test.ts#transaction-request-4{ts:line-numbers} +<<< @/../../docs-snippets2/src/transactions/transaction-request/fund-request.ts#transaction-request-4{ts:line-numbers} ### Adding a Predicate to a Transaction Request Predicates are used to define the conditions under which a transaction can be executed. Therefore you may want to add a predicate to a transaction request to unlock funds that are utilized by a script. This can be added like so: -<<< @/../../docs-snippets/src/guide/transactions/transaction-request.test.ts#transaction-request-5{ts:line-numbers} +<<< @/../../docs-snippets2/src/transactions/transaction-request/add-predicate.ts#transaction-request-5{ts:line-numbers} > **Note**: For more information on predicates, including information on configuring them, funding them and using them to unlock funds, please refer to the [predicate guide](../predicates/index.md). @@ -54,7 +54,7 @@ Predicates are used to define the conditions under which a transaction can be ex The SDK provides a way of either modifying the witnesses for a transaction request directly, or by passing accounts. This will then sign the transaction request with the account's private key. Below will detail how to add a witness to a transaction request: -<<< @/../../docs-snippets/src/guide/transactions/transaction-request.test.ts#transaction-request-6{ts:line-numbers} +<<< @/../../docs-snippets2/src/transactions/transaction-request/add-witness.ts#transaction-request-6{ts:line-numbers} A more complex example of adding multiple witnesses to a transaction request can be seen in the multiple signers guide [here](../cookbook/transactions-with-multiple-signers.md), which validates the signatures inside the script itself. @@ -64,6 +64,6 @@ A more complex example of adding multiple witnesses to a transaction request can The transaction ID is a SHA-256 hash of the entire transaction request. This can be useful for tracking the transaction on chain. To get the transaction ID, you can use the following method: -<<< @/../../docs-snippets/src/guide/transactions/transaction-request.test.ts#transaction-request-7{ts:line-numbers} +<<< @/../../docs-snippets2/src/transactions/transaction-request/add-witness.ts#transaction-request-7{ts:line-numbers} > **Note**: Any changes made to a transaction request will alter the transaction ID. Therefore, you should only get the transaction ID after all modifications have been made. diff --git a/apps/docs/src/guide/transactions/transaction-response.md b/apps/docs/src/guide/transactions/transaction-response.md index 1394bddd23b..855e5e17f9c 100644 --- a/apps/docs/src/guide/transactions/transaction-response.md +++ b/apps/docs/src/guide/transactions/transaction-response.md @@ -9,14 +9,14 @@ Once a transaction has been submitted, you may want to extract information regar - Date and time of the transaction - The block the transaction was included in -Firstly, we can extract this information from the result of a submitted transaction: +We can easily extract this information from a contract call: -<<< @/../../docs-snippets/src/guide/transactions/transaction-response.test.ts#transaction-response-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/transactions/transaction-response/contract-call.ts#transaction-response-1{ts:line-numbers} We can also use the result of a transaction request to extract a transaction summary: -<<< @/../../docs-snippets/src/guide/transactions/transaction-response.test.ts#transaction-response-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/transactions/transaction-response/from-submitted-request.ts#transaction-response-2{ts:line-numbers} Or we can build a transaction summary from a stored transaction ID: -<<< @/../../docs-snippets/src/guide/transactions/transaction-response.test.ts#transaction-response-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/transactions/transaction-response/from-submitted-request.ts#transaction-response-3{ts:line-numbers}