Program InstanceInteracting with Fungible Token Program
8/11 tutorials
73%

Initializing Program Instance: Utilizing Sails Constructors

To interact with a deployed program on the Vara Network, create an instance of the program using its library - TypeScript file generated by the Sails-JS CLI tool, which provides a type-safe API for interacting with the program.

Using the Sails-JS library, the deploy of a program is handled through its constructors, and available through newCtorFromCode and newCtorFromCodeId methods.

Obtaining Program Instance

If the program's ID is already known and the program has been previously deployed, specify the program ID to create the instance. This allows direct interaction with the program without any additional setup.

Uploading Program

To deploy a program programmatically with a compiled .opt.wasm file, use the upload program functionality.

This involves reading the .wasm file, creating a new program instance using the Program's library, and uploading it to the network.

The process includes calculating the necessary gas and signing the transaction with a developer account. Once uploaded, the program is ready for interaction.

Creating Program

Alternatively, if the desired code ID of the program to deploy is known, use the create program functionality.

This method involves specifying the code ID, creating a new program instance using the Program's library, and deploying it to the network.

Similar to the upload process, it includes calculating gas and signing the transaction with a developer account. Once created, the program is ready for interaction.

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

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 uploadProgram = async () => {
  const vftProgram = new Program(api);
  const optWasmBuffer = readFileSync('./extended_vft.opt.wasm');

  const uploadProgramTransaction = await vftProgram
    .newCtorFromCode(optWasmBuffer, TOKEN.NAME, TOKEN.SYMBOL, TOKEN.DECIMALS)
    .withAccount(keyring)
    .calculateGas();

  const { response: uploadProgramResponse } =
    await uploadProgramTransaction.signAndSend();

  await uploadProgramResponse();

  return vftProgram.programId;
};

const createProgram = async () => {
  const vftProgram = new Program(api);
  const CODE_ID = '0x00';

  const createProgramTransaction = await vftProgram
    .newCtorFromCodeId(CODE_ID, TOKEN.NAME, TOKEN.SYMBOL, TOKEN.DECIMALS)
    .withAccount(keyring)
    .calculateGas();

  const { response: createProgramResponse } =
    await createProgramTransaction.signAndSend();

  await createProgramResponse();

  return vftProgram.programId;
};

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