forked from FuelLabs/fuels-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix!: assemble of
Transfer
operation (FuelLabs#1787)
- Loading branch information
1 parent
c57860a
commit 086dbba
Showing
16 changed files
with
580 additions
and
320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--- | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@fuel-ts/account": minor | ||
--- | ||
|
||
fix assemble of transfer operations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
apps/docs-snippets/test/fixtures/forc-projects/token/Forc.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
entry = "main.sw" | ||
license = "Apache-2.0" | ||
name = "token" | ||
|
||
[dependencies] |
30 changes: 30 additions & 0 deletions
30
apps/docs-snippets/test/fixtures/forc-projects/token/src/main.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
contract; | ||
|
||
use std::asset::{burn, force_transfer_to_contract, mint, transfer_to_address,}; | ||
|
||
abi Token { | ||
fn transfer_to_address(target: Address, asset_id: AssetId, coins: u64); | ||
fn transfer_to_contract(recipient: ContractId, asset_id: AssetId, coins: u64); | ||
fn mint_coins(sub_id: b256, mint_amount: u64); | ||
fn burn_coins(sub_id: b256, burn_amount: u64); | ||
} | ||
|
||
impl Token for Contract { | ||
// #region variable-outputs-1 | ||
fn transfer_to_address(recipient: Address, asset_id: AssetId, amount: u64) { | ||
transfer_to_address(recipient, asset_id, amount); | ||
} | ||
|
||
fn transfer_to_contract(target: ContractId, asset_id: AssetId, amount: u64) { | ||
force_transfer_to_contract(target, asset_id, amount); | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,27 @@ | ||
<!-- NOTE: Review the relevance of this documentation page. The TypeScript SDK manages Output variables automatically, which may make the current content lack sufficient context. Consider providing a detailed explanation of how transactions work in a UTXO-based blockchain before discussing Output variables. This approach will ensure users have a better understanding of the topic and its importance. --> | ||
|
||
# Variable Outputs | ||
|
||
You may need to send funds to the transaction output in certain scenarios. Sway provides a method called `transfer_to_address(coins, asset_id, recipient)` that we can use for this purpose, which allows you to transfer a specific number of coins for a given asset to a recipient address. | ||
Sway includes robust functions for transferring assets to wallets and contracts. | ||
|
||
When using these transfer functions within your Sway projects, it is important to be aware that each call will require an [Output Variable](https://specs.fuel.network/master/tx-format/output.html#outputvariable) within the [Outputs](https://specs.fuel.network/master/tx-format/output.html) of the transaction. | ||
|
||
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: Using `transfer_to_address` in a Contract | ||
## Example: Sway's built-in functions that requires `Output Variable` | ||
|
||
Here's an example of a contract function that utilizes the `transfer_to_address` method: | ||
<<< @/../../docs-snippets/test/fixtures/forc-projects/token/src/main.sw#variable-outputs-1{ts:line-numbers} | ||
|
||
```rust:line-numbers | ||
fn transfer_coins_to_output(coins: u64, asset_id: ContractId, recipient: Address) { | ||
transfer_to_address(coins, asset_id, recipient); | ||
} | ||
``` | ||
> **Note:** Functions like `mint` and `burn` also requires an Output Variable for each call, as they internally execute the transfer function. | ||
## Using the SDK to Call the `transfer_coins_to_output` Function | ||
## Adding Variable Outputs to the contract call | ||
|
||
With the SDK, you can call `transfer_coins_to_output` by chaining the `txParams` and adding the property `variableOutputs: amount` to your contract call. Like this: | ||
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} | ||
|
||
In the TypeScript SDK, the output variables are automatically added to the transaction's list of outputs. The output's amount and owner may vary based on the transaction execution. | ||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.