Mint and Transfer TransactionsInteracting with Fungible Token Program
9/11 tutorials
82%

Mint and Transfer Transactions: Utilizing Sails Functions

For the sake of simplicity, continue from the previous step of the tutorial using the program instance with specified program ID.

Program transactions are handled using the Sails-JS functions provided by the generated program.

A particular service of the program contains all the necessary message methods for managing tokens, such as minting and transferring. By utilizing them, all required transactions can be easily performed.

Mint Tokens

To mint new tokens, initiate a mint transaction using the program instance. To do it, use the mint method from the VFT service of the program instance.

This involves specifying the recipient's address and the amount of tokens to mint.

The transaction is signed with a developer account and the necessary gas is calculated.

Once the transaction is sent and confirmed, the tokens are minted and added to the recipient's account.

Transfer Tokens

Continuing interaction with the program, transfer tokens from one account to another. Similarly to minting tokens, use the transfer method from the VFT service of the program instance.

This involves creating a transfer transaction by specifying the recipient's address and the amount of tokens to transfer.

The transaction is signed with a developer account and the necessary gas is calculated.

Once the transaction is sent and confirmed, the tokens are transferred to the recipient's account.

import { decodeAddress, GearApi } from '@gear-js/api';
import { Keyring } from '@polkadot/api';
import { mnemonicGenerate } from '@polkadot/util-crypto';

import { Program } from './lib';

const VARA_TESTNET_ENDPOINT = 'wss://testnet.vara.network';
const api = await GearApi.create({ providerAddress: VARA_TESTNET_ENDPOINT });

const mnemonic = mnemonicGenerate();
const keyring = new Keyring({ type: 'sr25519' }).addFromMnemonic(mnemonic);
const aliceKeyring = new Keyring({ type: 'sr25519' }).addFromUri('//Alice');
const accountAddress = decodeAddress(keyring.address);
const aliceAccountAddress = decodeAddress(aliceKeyring.address);

const TOKEN = {
  NAME: 'Tutorial Token',
  SYMBOL: 'TT',
  DECIMALS: 12,
} as const;

const TOKENS_AMOUNT = 1 * 10 ** TOKEN.DECIMALS;

const PROGRAM_ID = '0x00';
const vftProgram = new Program(api, PROGRAM_ID);

const mintTokens = async () => {
  const mintTransaction = await vftProgram.vft
    .mint(accountAddress, TOKENS_AMOUNT)
    .withAccount(keyring)
    .calculateGas();

  const { response: mintResponse } = await mintTransaction.signAndSend();

  const mintResult = await mintResponse();

  return mintResult;
};

const transferTokens = async () => {
  const transferTransaction = await vftProgram.vft
    .transfer(aliceAccountAddress, TOKENS_AMOUNT)
    .withAccount(keyring)
    .calculateGas();

  const { response: transferResponse } =
    await transferTransaction.signAndSend();

  const transferResult = await transferResponse();

  return transferResult;
};