Transfer Balances
Transfer AVAIL tokens between accounts using transferKeepAlive, transferAllowDeath, or transferAll.
Note
SETTING UP THE DEV ENVIRONMENT
In this guide we will use Avail's dedicated SDKs to interact with the Turing testnet. To set up a dev environment for the SDK of your choice, please follow the steps outlined here.
In this guide we will use Avail's dedicated SDKs to interact with the Turing testnet. To set up a dev environment for the SDK of your choice, please follow the steps outlined here.
Transferring funds programmatically
AVAIL from one account to another programmatically by calling any one of these three extrinsics
from the balances pallet on an Avail node:
balances_transferKeepAlive: Transfers funds from one account to another, but does not allow the sender balance to dip below the existential deposit.balances_transferAllowDeath: Transfers funds from one account to another, and allows the sender balance to dip below the existential deposit.balances_transferAll: Transfers all funds from one account to another.
Note
EXISTENTIAL DEPOSIT
Only accounts with a balance equal to or greater than the
Only accounts with a balance equal to or greater than the
existential deposit are stored on the state trie.
The current value of the existential deposit is 0.000001 AVAIL.
Any account whose balance dips below this amount is 'reaped'.Transferring funds using balances_transferKeepAlive
- Inside
your-file-name.ts, add the following code:
import * as dotenv from 'dotenv';
import { Account, SDK, BN } from 'avail-js-sdk';
dotenv.config();
export async function transferKeepAlive() {
// Initialize SDK with Turing endpoint
const sdk = await SDK.New('wss://turing-rpc.avail.so/ws');
// Create account from seed in .env file
const seed = process.env.SEED;
if (!seed) {
throw new Error("SEED environment variable is not set");
}
// Create account from seed
const account = Account.new(seed);
console.log("Sender Address: ", account.address);
// Destination address to send AVAIL to
const destinationAddress = "5DDY2yzh8uCysYFAiRSTeQVwtZSKNF49CkQkyPH852xvrYKk";
console.log("Recipient Address: ", destinationAddress);
// Amount to transfer: 12 AVAIL
const amount = new BN('12000000000000000000'); // 12 with 18 zeroes
console.log("Transfer Amount: 12 AVAIL");
// Create transfer transaction
const tx = sdk.tx.balances.transferKeepAlive(destinationAddress, amount);
console.log("Submitting transfer transaction...");
// Execute and wait for inclusion
const res = await tx.executeWaitForInclusion(account, {});
// Check if transaction was successful
const isOk = res.isSuccessful();
if (isOk === undefined) {
throw new Error("Cannot check if transaction was successful");
}
else if (!isOk) {
throw new Error("Transaction failed");
}
console.log(`Transaction Hash: ${res.txHash}`);
console.log(`Block Hash: ${res.blockHash}`);
console.log(`Block Number: ${res.blockNumber}`);
console.log("Transfer completed successfully");
process.exit(0);
}
// Execute the function
transferKeepAlive();- Run the code using:
ts-node your-file-name.tsSample Response:
From=5CqgQkrDcdg5QrtuxT3H7WszrqgrBMhdwRbmMVXQzc4VSiEg, To=5DDY2yzh8uCysYFAiRSTeQVwtZSKNF49CkQkyPH852xvrYKk, Amount=10000000000000000000
TxHash=0x45a3ec18b96c2bff0d92d70ba5f0fa904b79a49610e845b72d16ccf7c094533d, BlockHash=0x9fa20525f0db53e144ff595f00728611d60bd5d5e597f07b82123301067e90be
Transferring funds using balances_transferAllowDeath
- Inside
your-file-name.ts, add the following code:
import * as dotenv from 'dotenv';
import { Account, SDK, BN } from 'avail-js-sdk';
dotenv.config();
export async function transferAllowDeath() {
// Initialize SDK with Turing endpoint
const sdk = await SDK.New('wss://turing-rpc.avail.so/ws');
// Create account from seed in .env file
const seed = process.env.SEED;
if (!seed) {
throw new Error("SEED environment variable is not set");
}
// Create account from seed
const account = Account.new(seed);
console.log("Sender Address: ", account.address);
// Destination address to send AVAIL to
const destinationAddress = "5DDY2yzh8uCysYFAiRSTeQVwtZSKNF49CkQkyPH852xvrYKk";
console.log("Recipient Address: ", destinationAddress);
// Amount to transfer: 12.345 AVAIL
const amount = new BN('12345000000000000000');
// Create transfer transaction
const tx = sdk.tx.balances.transferAllowDeath(destinationAddress, amount);
console.log("Submitting transfer transaction...");
// Execute and wait for inclusion
const res = await tx.executeWaitForInclusion(account, {});
// Check if transaction was successful
const isOk = res.isSuccessful();
if (isOk === undefined) {
throw new Error("Cannot check if transaction was successful");
}
else if (!isOk) {
throw new Error("Transaction failed");
}
console.log(`Transaction Hash: ${res.txHash}`);
console.log(`Block Hash: ${res.blockHash}`);
console.log(`Block Number: ${res.blockNumber}`);
console.log("Transfer completed successfully");
process.exit(0);
}
// Execute the function
transferAllowDeath();- Run the code using:
ts-node your-file-name.tsSample Response:
From=5CqgQkrDcdg5QrtuxT3H7WszrqgrBMhdwRbmMVXQzc4VSiEg, To=5HGjWAeFDfFCWPsjFQdVV2Msvz2XtMktvgocEZcCj68kUMaw, Amount=12000000000000000000
MaybeKilled=undefined
TxHash=0x4be89fffca3b7849066c3a7b6b29af318caa49ca12a1ba17610b0a30d97fd30e, BlockHash=0x861531ee9512849cec0bde5294bb65098424c84e3ab64b6c25722574370d8224
Transferring funds using balances_transferAll
- Inside
your-file-name.ts, add the following code:
import * as dotenv from 'dotenv';
import { Account, SDK } from 'avail-js-sdk';
dotenv.config();
export async function transferAll() {
// Initialize SDK with Turing endpoint
const sdk = await SDK.New('wss://turing-rpc.avail.so/ws');
// Create account from seed in .env file
const seed = process.env.SEED;
if (!seed) {
throw new Error("SEED environment variable is not set");
}
// Create account from seed
const account = Account.new(seed);
console.log("Sender Address: ", account.address);
// Destination address to send all available AVAIL to
const destinationAddress = "5DDY2yzh8uCysYFAiRSTeQVwtZSKNF49CkQkyPH852xvrYKk";
console.log("Recipient Address: ", destinationAddress);
// Setting this value to false will reap your account
const keepAlive = true;
// Create transfer all transaction
const tx = sdk.tx.balances.transferAll(destinationAddress, keepAlive);
console.log("Submitting transfer all transaction...");
// Execute and wait for inclusion
const res = await tx.executeWaitForInclusion(account, {});
// Check if transaction was successful
const isOk = res.isSuccessful();
if (isOk === undefined) {
throw new Error("Cannot check if transaction was successful");
}
else if (!isOk) {
throw new Error("Transaction failed");
}
console.log("Transfer all completed successfully");
console.log(`Transaction Hash: ${res.txHash}`);
console.log(`Block Hash: ${res.blockHash}`);
console.log(`Block Number: ${res.blockNumber}`);
process.exit(0);
}
// Execute the function
transferAll();- Run the code using:
ts-node your-file-name.tsSample Response:
From=5CqgQkrDcdg5QrtuxT3H7WszrqgrBMhdwRbmMVXQzc4VSiEg, To=5DDY2yzh8uCysYFAiRSTeQVwtZSKNF49CkQkyPH852xvrYKk, Amount=14967497534555492726990
TxHash=0x3dbf0865aae15137e1fe3f922d70b7ff514a8c27e712d12ea1a8a1d4a7af9437, BlockHash=0x129745d18065f66f106b3744fe73ab5f9a1d7cb6205b271d13119399d3a56d31
If you check the balance of the sender account now, it will either be0or equal to theexistential depositamount, if the account is unable to be reaped due to dependencies on the network. In this case, while the account will continue to exist on the state trie, it's balance will be too low to perform any operations.
How is this guide?