@subwallet/invariant-vara-sdk
Version:
<div align="center"> <h1>⚡Invariant protocol⚡</h1> <p> <a href="https://invariant.app/math-spec-vara.pdf">MATH SPEC 📄</a> | <a href="https://discord.gg/VzS3C9wR">DISCORD 🌐</a> | </p> </div>
124 lines (123 loc) • 5.38 kB
JavaScript
import { Erc20Token } from './erc20-token.js';
import { getWasm, integerSafeCast } from './utils.js';
import { FUNGIBLE_TOKEN_GAS_LIMIT, DEFAULT_ADDRESS } from './consts.js';
import { TransactionWrapper } from './utils.js';
export class FungibleToken {
gasLimit;
erc20;
admin;
constructor(gasLimit, erc20, admin) {
this.gasLimit = gasLimit;
this.erc20 = erc20;
this.admin = admin;
}
static async deploy(api, deployer, name = '', symbol = '', decimals = 0n, gasLimit = FUNGIBLE_TOKEN_GAS_LIMIT) {
const code = await getWasm('extended_vft');
const erc20 = new Erc20Token(api);
const deployTx = await erc20
.newCtorFromCode(code, name, symbol, integerSafeCast(decimals))
.withAccount(deployer)
.withGas(gasLimit);
{
const { response } = await deployTx.signAndSend();
response();
}
if (!erc20.programId) {
throw new Error('Failed to initialize FungibleToken program');
}
return erc20.programId;
}
static async load(api, gasLimit = FUNGIBLE_TOKEN_GAS_LIMIT) {
const erc20 = new Erc20Token(api);
return new FungibleToken(gasLimit, erc20);
}
programId() {
const id = this.erc20.programId;
if (id === undefined || id === null) {
throw new Error('Program id is not set');
}
return id;
}
setAdmin(admin) {
this.admin = admin;
}
async allowance(owner, spender, tokenAddress) {
const erc20 = new Erc20Token(this.erc20.api, tokenAddress);
return erc20.vft.allowance(owner, spender, DEFAULT_ADDRESS);
}
async balanceOf(owner, tokenAddress) {
const erc20 = new Erc20Token(this.erc20.api, tokenAddress);
return erc20.vft.balanceOf(owner, DEFAULT_ADDRESS);
}
async decimals(tokenAddress) {
const erc20 = new Erc20Token(this.erc20.api, tokenAddress);
return BigInt(await erc20.vft.decimals(DEFAULT_ADDRESS));
}
async name(tokenAddress) {
const erc20 = new Erc20Token(this.erc20.api, tokenAddress);
return erc20.vft.name(DEFAULT_ADDRESS);
}
async symbol(tokenAddress) {
const erc20 = new Erc20Token(this.erc20.api, tokenAddress);
return erc20.vft.symbol(DEFAULT_ADDRESS);
}
async totalSupply(tokenAddress) {
const erc20 = new Erc20Token(this.erc20.api, tokenAddress);
return erc20.vft.totalSupply(DEFAULT_ADDRESS);
}
async approveTx(spender, amount, tokenAddress, gasLimit = this.gasLimit) {
const erc20 = new Erc20Token(this.erc20.api, tokenAddress);
return new TransactionWrapper(await erc20.vft.approve(spender, amount).withGas(gasLimit));
}
async approve(owner, spender, amount, tokenAddress) {
const tx = await this.approveTx(spender, amount, tokenAddress);
return tx.withAccount(owner).signAndSend();
}
async burnTx(account, amount, tokenAddress, gasLimit = this.gasLimit) {
const erc20 = new Erc20Token(this.erc20.api, tokenAddress);
return new TransactionWrapper(await erc20.vft.burn(account, amount).withGas(gasLimit));
}
async burn(account, amount, tokenAddress) {
if (!this.admin) {
throw new Error('Admin account is required to burn tokens');
}
const tx = await this.burnTx(account, amount, tokenAddress);
return tx.withAccount(this.admin).signAndSend();
}
async mintTx(account, amount, tokenAddress, gasLimit = this.gasLimit) {
const erc20 = new Erc20Token(this.erc20.api, tokenAddress);
return new TransactionWrapper(await erc20.vft.mint(account, amount).withGas(gasLimit));
}
async mint(account, amount, tokenAddress) {
if (!this.admin) {
throw new Error('Admin account is required to mint tokens');
}
const tx = await this.mintTx(account, amount, tokenAddress);
return tx.withAccount(this.admin).signAndSend();
}
async setTransferFail(flag, tokenAddress, gasLimit = this.gasLimit) {
const erc20 = new Erc20Token(this.erc20.api, tokenAddress);
if (!this.admin) {
throw new Error('Admin account is required to set transfer failure');
}
const tx = await erc20.vft.setTransferFail(flag).withGas(gasLimit);
const { response } = await tx.withAccount(this.admin).signAndSend();
return response();
}
async transferTx(to, amount, tokenAddress, gasLimit = this.gasLimit) {
const erc20 = new Erc20Token(this.erc20.api, tokenAddress);
return new TransactionWrapper(await erc20.vft.transfer(to, amount).withGas(gasLimit));
}
async transfer(signer, to, amount, tokenAddress) {
const tx = await this.transferTx(to, amount, tokenAddress);
return tx.withAccount(signer).signAndSend();
}
async transferFromTx(from, to, amount, tokenAddress, gasLimit = this.gasLimit) {
const erc20 = new Erc20Token(this.erc20.api, tokenAddress);
return new TransactionWrapper(await erc20.vft.transferFrom(from, to, amount).withGas(gasLimit));
}
async transferFrom(signer, from, to, amount, tokenAddress) {
const tx = await this.transferFromTx(from, to, amount, tokenAddress);
return tx.withAccount(signer).signAndSend();
}
}