Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs!: fix wrong info at variable outputs page #1962

Merged
merged 13 commits into from
Mar 28, 2024
5 changes: 5 additions & 0 deletions .changeset/empty-buses-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/transactions": minor
---

docs!: fix wrong info at variable outputs page
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ describe(__filename, () => {

it('should successfully execute contract call with txParams', async () => {
// #region transaction-parameters-2
// #region variable-outputs-1
const { minGasPrice } = provider.getGasConfig();

const { transactionResult } = await contract.functions
Expand All @@ -27,7 +26,6 @@ describe(__filename, () => {
variableOutputs: 1,
})
.call();
// #endregion variable-outputs-1
// #endregion transaction-parameters-2

const { transaction } = transactionResult;
Expand Down
35 changes: 35 additions & 0 deletions apps/docs-snippets/src/guide/contracts/variable-outputs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import type { Contract } from 'fuels';
import { getMintedAssetId, getRandomB256, Wallet } from 'fuels';

import { DocSnippetProjectsEnum } from '../../../test/fixtures/forc-projects';
import { createAndDeployContractFromProject } from '../../utils';

/**
* @group node
*/
describe(__filename, () => {
let contract: Contract;
beforeAll(async () => {
contract = await createAndDeployContractFromProject(DocSnippetProjectsEnum.TOKEN);
});

it('should successfully execute contract call with variable outputs', async () => {
const subId = getRandomB256();

await contract.functions.mint_coins(subId, 100).call();

const address = { value: Wallet.generate().address.toB256() };
const assetId = { value: getMintedAssetId(contract.id.toB256(), subId) };

// #region variable-outputs-2
const { transactionResult } = await contract.functions
.transfer_to_address(address, assetId, 100)
.txParams({
variableOutputs: 1,
})
.call();
// #endregion variable-outputs-2

expect(transactionResult.isStatusSuccess).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Contract, WalletUnlocked, Provider } from 'fuels';
import { ContractFactory, BaseAssetId, Wallet, ZeroBytes32, getAssetId } from 'fuels';
import { ContractFactory, BaseAssetId, Wallet, ZeroBytes32, getMintedAssetId } from 'fuels';

import {
DocSnippetProjectsEnum,
Expand Down Expand Up @@ -36,7 +36,7 @@ describe(__filename, () => {
const subId = ZeroBytes32;
const contractId = liquidityPoolContract.id.toB256();

const assetId = getAssetId(contractId, subId);
const assetId = getMintedAssetId(contractId, subId);

await liquidityPoolContract.functions
.deposit({ value: liquidityOwner.address.toB256() })
Expand Down
1 change: 1 addition & 0 deletions apps/docs-snippets/test/fixtures/forc-projects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export enum DocSnippetProjectsEnum {
ECHO_U64_ARRAY = 'echo-u64-array',
RETURN_CONTEXT = 'return-context',
TOKEN_DEPOSITOR = 'token-depositor',
TOKEN = 'token',
LIQUIDITY_POOL = 'liquidity-pool',
SIMPLE_PREDICATE = 'simple-predicate',
ECHO_CONFIGURABLES = 'echo-configurables',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ impl Token for Contract {
fn transfer_to_contract(target: ContractId, asset_id: AssetId, amount: u64) {
force_transfer_to_contract(target, asset_id, amount);
}

// #endregion variable-outputs-1
fn mint_coins(sub_id: b256, mint_amount: u64) {
mint(sub_id, mint_amount);
}

fn burn_coins(sub_id: b256, burn_amount: u64) {
burn(sub_id, burn_amount);
}
// #endregion variable-outputs-1
}
8 changes: 3 additions & 5 deletions apps/docs/src/guide/contracts/variable-outputs.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ When using these transfer functions within your Sway projects, it is important t

For instance, if a contract function calls a Sway transfer function 3 times, it will require 3 Output Variables present within the list of outputs in your transaction.

## Example: Sway's built-in functions that requires `Output Variable`
## Example: Sway functions that requires `Output Variable`

<<< @/../../docs-snippets/test/fixtures/forc-projects/token/src/main.sw#variable-outputs-1{ts:line-numbers}

> **Note:** Functions like `mint` and `burn` also requires an Output Variable for each call, as they internally execute the transfer function.

## Adding Variable Outputs to the contract call

When your contract invokes any of these functions, or if it calls a function that leads to another contract invoking these functions, you need to add the appropriate number of Output Variables.

This can be done as shown in the following example:

<<< @/../../docs-snippets/src/guide/contracts/transaction-parameters.test.ts#variable-outputs-1{ts:line-numbers}
<<< @/../../docs-snippets/src/guide/contracts/variable-outputs.test.ts#variable-outputs-2{ts:line-numbers}

In the TypeScript SDK, the Output Variables are automatically added to the transaction's list of outputs.

This process is done by a brute-force strategy, performing sequential dry runs until no errors are returned. This method identifies the number of Output Variables required to process the transaction.

However, this can significantly delay the transaction processing. Therefore, it is **highly recommended** to manually add the correct number of Output Variables before submitting the transaction.
However, this can significantly delay the transaction processing. Therefore, it is **highly recommended** to manually add the correct number of Output Variables before submitting the transaction.
6 changes: 3 additions & 3 deletions packages/transactions/src/coders/receipt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ export type ReceiptMint = {
is: BN;
};

export const getAssetId = (contractId: string, subId: string): string => {
export const getMintedAssetId = (contractId: string, subId: string): string => {
const contractIdBytes = arrayify(contractId);
const subIdBytes = arrayify(subId);

Expand All @@ -780,7 +780,7 @@ export class ReceiptMintCoder extends Coder<ReceiptMint, ReceiptMint> {
}

static getAssetId(contractId: string, subId: string): string {
return getAssetId(contractId, subId);
return getMintedAssetId(contractId, subId);
}

encode(value: ReceiptMint): Uint8Array {
Expand Down Expand Up @@ -848,7 +848,7 @@ export class ReceiptBurnCoder extends Coder<ReceiptBurn, ReceiptBurn> {
}

static getAssetId(contractId: string, subId: string): string {
return getAssetId(contractId, subId);
return getMintedAssetId(contractId, subId);
}

encode(value: ReceiptBurn): Uint8Array {
Expand Down
Loading