Transfer funds while ensuring min balance for sender
On-chain name of extrinsic: balances_transferKeepAlive
Parameters
avail-js
| parameter | type | optional | description | 
|---|---|---|---|
| dest | string | false | account that will receive funds | 
| value | BN | false | amount that is send. 10^18 is equal to 1 AVL | 
| waitFor | WaitFor | false | wait for block inclusion or finalization | 
| account | KeyringPair | false | account that will send and sign the transaction | 
| options | SignerOptions | true | used to overwrite existing signer options | 
Return value
On failure, a reason of failure is returned. On Success, TransferEvent event, transaction hash and block hash is returned.
Basic Example
avail-js
- Inside your-file-name.ts, add the following code:
avail-js
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:
terminal
ts-node your-file-name.tsLast updated on