How do I configure the test to use BASE_ASSET_ID?

I want to take the counter smart contract in the quick start guide here https://docs.fuel.network/guides/quickstart/building-a-smart-contract/ and turn it into a contract where assets can be transferred.

The modified contract now looks like this

contract;

use std::{
    address::Address,
    call_frames::contract_id,
    constants::BASE_ASSET_ID,
    identity::Identity,
    token::*,
};

storage {
    counter: u64 = 0,
}

abi Counter {
    #[storage(read, write)]
    fn increment();

    #[storage(read)]
    fn count() -> u64;

     #[payable]
    fn trigger_transferout();

    #[payable]
    fn trigger_transfer();
}

impl Counter for Contract {
    #[storage(read)]
    fn count() -> u64 {
        storage.counter.read()
    }

    #[storage(read, write)]
    fn increment() {
        let incremented = storage.counter.read() + 1;
        storage.counter.write(incremented);
    }

    #[payable]
    fn trigger_transfer() {
        // Transfer the asset back to the originating contract
        transfer(Identity::ContractId(contract_id()), BASE_ASSET_ID, 1);
    }

    #[payable]
    fn trigger_transferout() {
        const RECEIVER = Address::from(0x532ee5fb2cabec472409eb5f9b42b59644edb7bf9943eda9c2e3947305ed5e96);
        transfer(Identity::Address(RECEIVER), BASE_ASSET_ID, 1);
    }
}

and I modified the test to be like this

#[tokio::test]
async fn test_increment() {
    let (instance, _id) = get_contract_instance().await;
    instance.methods().trigger_transfer().call().await.unwrap();

}

But when I run the test it fails with

thread 'test_increment' panicked at tests/harness.rs:49:56:
called `Result::unwrap()` on an `Err` value: RevertTransactionError { reason: "NotEnoughBalance", revert_id: 0, receipts: [Call { id: 0000000000000000000000000000000000000000000000000000000000000000, to: a47c6c1a05138237a6b205304ce57c9d41885200e52ad4a13e2885aff5b5a888, amount: 0, asset_id: 0000000000000000000000000000000000000000000000000000000000000000, gas: 9999835, param1: 1187837885, param2: 1, pc: 11632, is: 11632 }, Panic { id: a47c6c1a05138237a6b205304ce57c9d41885200e52ad4a13e2885aff5b5a888, reason: PanicInstruction { reason: NotEnoughBalance, instruction: 1011418176 }, pc: 12568, is: 11632, contract_id: None }, ScriptResult { result: Panic, gas_used: 314 }] }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test test_increment ... FAILED

Based on the error I suspect the issue is in the setup. Basically here

    // Launch a local network and deploy the contract
    let mut wallets = launch_custom_provider_and_get_wallets(
        WalletsConfig::new(
            Some(1),             /* Single wallet */
            Some(1),             /* Single coin (UTXO) */
            Some(1_000_000_000), /* Amount per coin */
        ),
        None,
        None,
    )
    .await;
    let wallet = wallets.pop().unwrap();

I think this needs to be updated such that the wallet has got some amount of base asset.

Question is how do I do this? I have scoured the docs but could not seem to find how…

fuelup show
Default host: aarch64-apple-darwin
fuelup home: /Users/me/.fuelup

installed toolchains
--------------------
beta-3-aarch64-apple-darwin
latest-aarch64-apple-darwin (default)
beta-4-aarch64-apple-darwin

active toolchain
-----------------
latest-aarch64-apple-darwin (default)
  forc : 0.44.1
    - forc-client
      - forc-deploy : 0.44.1
      - forc-run : 0.44.1
    - forc-doc : 0.44.1
    - forc-explore : 0.28.1
    - forc-fmt : 0.44.1
    - forc-index : 0.20.7
    - forc-lsp : 0.44.1
    - forc-tx : 0.44.1
    - forc-wallet : 0.3.0
  fuel-core : 0.20.4
  fuel-core-keygen : Error getting version string
  fuel-indexer : 0.20.7

fuels versions
---------------
forc : 0.45
forc-wallet : 0.45

Hi @dade thanks for your question! The function you have written .trigger_transfer() is transferring assets out of the contract. However your test does only two things.

  1. Deploy the contract
  2. Immediately call the .trigger_transfer() function.

The problem is not with your wallet or your setup but the test you have written. The contract simply does not have asset’s to transfer out yet. Try funding the contract first. Hope this helps! :slight_smile:

#[tokio::test]
async fn test_increment() {
    let (instance, _id, wallets) = get_contract_instance().await;

    let wallet_1 = wallets.get(0).unwrap();

    println!("Wallet 1 balance {}", wallet_1.get_asset_balance(&BASE_ASSET_ID).await.unwrap());

    wallet_1.force_transfer_to_contract(&_id, 1_000_000, BASE_ASSET_ID, TxParameters::default()).await;

    instance
        .with_account(wallet_1.clone())
        .unwrap()
        .methods()
        .trigger_transfer()
        .call()
        .await
        .unwrap();
}

Thank you @calldelegation for the tip. I was able to test the first contract method trigger_transfer, ie the one that transfers the asset back to the contract.

But trying to test the second contract method, trigger_transferout the one that transfers the asset to an address does not work.

Here is the updated test

#[tokio::test]
async fn test_increment() {
    let (instance, _id, wallet_1) = get_contract_instance().await;

    println!("Wallet 1 balance {}", wallet_1.get_asset_balance(&BASE_ASSET_ID).await.unwrap());

    let _ = wallet_1.force_transfer_to_contract(&_id, 1_000_000, BASE_ASSET_ID, TxParameters::default()).await;

    instance.methods().trigger_transferout().call().await.unwrap();

}

But this fails with the error

running 1 test
Wallet 1 balance 1000000000
thread 'test_increment' panicked at tests/harness.rs:54:59:
called `Result::unwrap()` on an `Err` value: RevertTransactionError { reason: "failed transfer to address.", revert_id: 18446744073709486081, receipts: [Call { id: 0000000000000000000000000000000000000000000000000000000000000000, to: a47c6c1a05138237a6b205304ce57c9d41885200e52ad4a13e2885aff5b5a888, amount: 0, asset_id: 0000000000000000000000000000000000000000000000000000000000000000, gas: 9999835, param1: 1965078698, param2: 1, pc: 11632, is: 11632 }, Revert { id: a47c6c1a05138237a6b205304ce57c9d41885200e52ad4a13e2885aff5b5a888, ra: 18446744073709486081, pc: 13704, is: 11632 }, ScriptResult { result: Revert, gas_used: 294 }] }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
test test_increment ... FAILED

Is transfering to Account different from transferring to Contracts?

Seems so. I got it working by using transfer_to_address instead

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.