Request Funds
Once you have created your account,
you can start using wallet.rs for value transactions. Since these examples target the testnet, you can request funds
using the request_funds_from_faucet
function.
Online Faucet
You can request test funds from the Shimmer Testnet Faucet.
Code Example
The following example will:
- Create an account manager.
- Get Alice's account which was created in the first guide.
- Retrieve an address related to Alice's account.
- Request funds to the
FAUCET_URL
you defined in the.env
file.
- Rust
- Nodejs
- Python
- Java
Dotenv
This example uses dotenv, which is not safe for use in production environments.
// Copyright 2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
//! cargo run --example get_funds --release
// In this example we request funds from the faucet to our address
// Rename `.env.example` to `.env` first
use std::env;
use dotenv::dotenv;
use iota_client::request_funds_from_faucet;
use iota_wallet::{account_manager::AccountManager, Result};
#[tokio::main]
async fn main() -> Result<()> {
// This example uses dotenv, which is not safe for use in production
dotenv().ok();
// Create the account manager
let manager = AccountManager::builder().finish().await?;
// Get the account we generated with `01_create_wallet`
let account = manager.get_account("Alice").await?;
let address = account.addresses().await?;
let faucet_response =
request_funds_from_faucet(&env::var("FAUCET_URL").unwrap(), &address[0].address().to_bech32()).await?;
println!("{faucet_response}");
Ok(())
}
Run the example by running the following command:
cargo run --example get_funds --release
/**
* This example will request funds from a faucet
*/
const getUnlockedManager = require('./account-manager');
async function run() {
try {
const manager = await getUnlockedManager();
const account = await manager.getAccount('0');
const addressObject = (await account.addresses())[0];
if (!process.env.FAUCET_URL) {
throw new Error('.env FAUCET_URL is undefined, see .env.example');
}
const faucetResponse = await account.requestFundsFromFaucet(process.env.FAUCET_URL, addressObject.address);
console.log(faucetResponse);
} catch (error) {
console.log('Error: ', error);
}
process.exit(0);
}
run();
You can run the example by running the following command from the wallet/bindings/nodejs/examples/
folder:
node 33-request-funds.js
from iota_wallet import IotaWallet
import time
# This example requests funds from the faucet
wallet = IotaWallet('./alice-database')
account = wallet.get_account('Alice')
# Sync account with the node
response = account.sync()
print(f'Synced: {response}')
# Balance before funding
balance = account.get_balance()
print(f'balance before faucet request: { balance[ "baseCoin" ][ "available" ] }')
response = account.request_funds_from_faucet("https://faucet.testnet.shimmer.network/api/enqueue", "rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu")
time.sleep(20)
# Sync account with the node
response = account.sync()
# Balance after funding
balance = account.get_balance()
print(f'balance after faucet request: { balance[ "baseCoin" ][ "available" ] }')
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
import org.iota.Wallet;
import org.iota.types.*;
import org.iota.types.account_methods.SyncAccount;
import org.iota.types.exceptions.InitializeWalletException;
import org.iota.types.exceptions.NoFundsReceivedFromFaucetException;
import org.iota.types.exceptions.WalletException;
import org.iota.types.ids.account.AccountAlias;
import org.iota.types.secret.StrongholdSecretManager;
public class RequestFundsFromFaucet {
public static void main(String[] args) throws WalletException, InterruptedException, InitializeWalletException, NoFundsReceivedFromFaucetException {
// This example assumes that a wallet has already been created using the ´SetupWallet.java´ example.
// If you haven't run the ´SetupWallet.java´ example yet, you must run it first to be able to load the wallet as shown below:
Wallet wallet = new Wallet(new WalletConfig()
.withClientOptions(new ClientConfig().withNodes(Env.NODE))
.withSecretManager(new StrongholdSecretManager(Env.STRONGHOLD_PASSWORD, null, Env.STRONGHOLD_VAULT_PATH))
.withCoinType(CoinType.Shimmer)
.withStoragePath(Env.STORAGE_PATH)
);
// Get account and sync it with the registered node to ensure that its balances are up-to-date.
AccountHandle a = wallet.getAccount(new AccountAlias(Env.ACCOUNT_NAME));
AccountBalance balance = a.syncAccount(new SyncAccount().withOptions(new SyncOptions()));
// Print the account balance before asking the faucet for funds.
System.out.println("available account balance before faucet request: " + balance.getBaseCoin().getAvailable());
// Get an address to fund.
String address = a.getAddresses()[0].getAddress();
// Syncs the account with the provided sync options and request funds from the faucet.
a.requestFundsFromFaucet(new org.iota.types.account_methods.RequestFundsFromFaucet("https://faucet.testnet.shimmer.network/api/enqueue", address), 10000000, new SyncOptions());
// Print the account balance after asking the faucet for funds.
System.out.println("available account balance after faucet request: " + a.syncAccount(new SyncAccount()).getBaseCoin().getAvailable());
// In case you are done and don't need the wallet instance anymore you can destroy the instance to clean up memory.
// For this, check out the ´DestroyWallet.java´ example.
}
}
Expected Output
- Rust
- Nodejs
- Python
- Java
{
"address": "tst1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jax7rgxyj",
"waitingRequests": 1
}
{"address":"rms1qzrux5tuhpfrc84gwau7ldg0xjvnkuheg3pnmjx94jrpah8rqqmuz3crlzm","waitingRequests":1}
balance before faucet request: 0
available account balance after faucet request: 1000000000
available account balance before faucet request: 0
available account balance after faucet request: 1000000000