⌘K

Accounts

Icon Link账户

ViewOnlyAccount trait 提供了一个通用接口来查询余额。

Account trait 除了上述功能之外,还提供了一个通用接口来检索可花费的资源或转移资产。在执行 SDK 中导致交易的操作时,通常需要提供一个账户,该账户将用于分配交易所需的资源,包括交易费用。

这两个 trait 都由以下类型实现:

Icon Link转移资产

一个账户实现了以下方法来转移资产:

  • transfer
  • force_transfer_to_contract
  • withdraw_to_base_layer

以下示例是针对 Wallet 账户的。Predicate 账户的工作方式类似,但在尝试花费其拥有的资源之前,您可能需要设置其断言数据。

使用 wallet.transfer 您可以启动一个交易,将资产从您的账户转移到目标地址。

use fuels::prelude::*;
 
// Setup 2 test wallets with 1 coin each
let num_wallets = Some(2);
let coins_per_wallet = Some(1);
let coin_amount = Some(1);
 
let wallets = launch_custom_provider_and_get_wallets(
    WalletsConfig::new(num_wallets, coins_per_wallet, coin_amount),
    None,
    None,
)
.await?;
 
// Transfer the base asset with amount 1 from wallet 1 to wallet 2
let asset_id = Default::default();
let (_tx_id, _receipts) = wallets[0]
    .transfer(wallets[1].address(), 1, asset_id, TxPolicies::default())
    .await?;
 
let wallet_2_final_coins = wallets[1].get_coins(AssetId::zeroed()).await?;
 
// Check that wallet 2 now has 2 coins
assert_eq!(wallet_2_final_coins.len(), 2);
 

您可以通过 wallet.force_transfer_to_contract 将资产转移到合约。

// Check the current balance of the contract with id 'contract_id'
let contract_balances = wallet
    .try_provider()?
    .get_contract_balances(&contract_id)
    .await?;
assert!(contract_balances.is_empty());
 
// Transfer an amount of 300 to the contract
let amount = 300;
let asset_id = random_asset_id;
let (_tx_id, _receipts) = wallet
    .force_transfer_to_contract(&contract_id, amount, asset_id, TxPolicies::default())
    .await?;
 
// Check that the contract now has 1 coin
let contract_balances = wallet
    .try_provider()?
    .get_contract_balances(&contract_id)
    .await?;
assert_eq!(contract_balances.len(), 1);
 
let random_asset_balance = contract_balances.get(&random_asset_id).unwrap();
assert_eq!(*random_asset_balance, 300);

要将资产转移到基础层链,您可以使用 wallet.withdraw_to_base_layer

use std::str::FromStr;
 
use fuels::prelude::*;
 
let wallets = launch_custom_provider_and_get_wallets(
    WalletsConfig::new(Some(1), None, None),
    None,
    None,
)
.await?;
let wallet = wallets.first().unwrap();
 
let amount = 1000;
let base_layer_address = Address::from_str(
    "0x4710162c2e3a95a6faff05139150017c9e38e5e280432d546fae345d6ce6d8fe",
)?;
let base_layer_address = Bech32Address::from(base_layer_address);
// Transfer an amount of 1000 to the specified base layer address
let (tx_id, msg_id, _receipts) = wallet
    .withdraw_to_base_layer(&base_layer_address, amount, TxPolicies::default())
    .await?;
 
let _block_height = wallet.try_provider()?.produce_blocks(1, None).await?;
 
// Retrieve a message proof from the provider
let proof = wallet
    .try_provider()?
    .get_message_proof(&tx_id, &msg_id, None, Some(2))
    .await?
    .expect("failed to retrieve message proof");
 
// Verify the amount and recipient
assert_eq!(proof.amount, amount);
assert_eq!(proof.recipient, base_layer_address);

上面的示例从字符串创建了一个 Address 并将其转换为 Bech32Address。接下来,它通过提供地址、要转移的金额和交易策略来调用 wallet.withdraw_to_base_layer。最后,为了验证转移是否成功,使用 provider.get_message_proof 检索相关的消息证明,并验证金额和接收方。