How to get mock tokens to use in a test

I was wondering how to get Mock Tokens to use when writing test contracts in Sway, akin to a mock ERC20 in Solidity. I considered deploying a small test contract like

impl MockToken for Contract {
    #[storage(read, write)]
    fn faucet(amount: u64) {
        let sender: Identity = msg_sender().unwrap();
        mint_to(amount, sender);
    }
}

but I was wondering if there was an easier way to do this from the SDK within the context of a test to give arbitrary tokens to a wallet

2 Likes

Not super familiar with in-language testing, but this is how you do it in rust.
https://fuellabs.github.io/fuels-rs/v0.31.0/wallets/test-wallets.html

2 Likes

Here’s an example of how you can do it from the SDK (it assumes that you have a wallet file persisted to disk; alternatively, you can generate a random wallet using the SDK):

use fuels::{
    prelude::{
        setup_single_asset_coins, setup_test_client, AssetId, Config, Contract, Provider,
        TxParameters, WalletUnlocked, DEFAULT_COIN_AMOUNT,
    },
    signers::Signer,
};
use fuels_core::parameters::StorageConfiguration;

async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let wallet_path_str = wallet_path.as_os_str().to_str().unwrap();

    let mut wallet =
        WalletUnlocked::load_keystore(wallet_path_str, [WALLET_PASSWORD], None)
            .unwrap();

    let bin_path_str = [CONTRACT_BIN_PATH];
    let _compiled = Contract::load_contract(bin_path_str, &None).unwrap();

    let number_of_coins = 10;
    let asset_id = AssetId::zeroed();
    let coins = setup_single_asset_coins(
        wallet.address(),
        asset_id,
        number_of_coins,
        DEFAULT_COIN_AMOUNT,
    );

    let config = Config {
        utxo_validation: false,
        addr: [FUEL_NODE_ADDR],
        ..Config::local_node()
    };

    let (client, _) = setup_test_client(coins, vec![], Some(config), None, None).await;

    let provider = Provider::new(client);

    wallet.set_provider(provider.clone());

    let contract_id = Contract::deploy(
        bin_path_str,
        &wallet,
        TxParameters::default(),
        StorageConfiguration::default(),
    )
    .await
    .unwrap();

    Ok(())
}

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