@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>
288 lines (287 loc) • 15.7 kB
JavaScript
import { decodeAddress } from '@gear-js/api';
import { TypeRegistry } from '@polkadot/types';
import { TransactionBuilder, throwOnErrorReply, getServiceNamePrefix, getFnNamePrefix, ZERO_ADDRESS } from 'sails-js';
export class Erc20Token {
api;
_programId;
registry;
vft;
constructor(api, _programId) {
this.api = api;
this._programId = _programId;
const types = {};
this.registry = new TypeRegistry();
this.registry.setKnownTypes({ types });
this.registry.register(types);
this.vft = new Vft(this);
}
get programId() {
if (!this._programId)
throw new Error(`Program ID is not set`);
return this._programId;
}
newCtorFromCode(code, name, symbol, decimals) {
const builder = new TransactionBuilder(this.api, this.registry, 'upload_program', ['New', name, symbol, decimals], '(String, String, String, u8)', 'String', code);
this._programId = builder.programId;
return builder;
}
newCtorFromCodeId(codeId, name, symbol, decimals) {
const builder = new TransactionBuilder(this.api, this.registry, 'create_program', ['New', name, symbol, decimals], '(String, String, String, u8)', 'String', codeId);
this._programId = builder.programId;
return builder;
}
}
export class Vft {
_program;
constructor(_program) {
this._program = _program;
}
burn(from, value) {
if (!this._program.programId)
throw new Error('Program ID is not set');
return new TransactionBuilder(this._program.api, this._program.registry, 'send_message', ['Vft', 'Burn', from, value], '(String, String, [u8;32], U256)', 'bool', this._program.programId);
}
grantAdminRole(to) {
if (!this._program.programId)
throw new Error('Program ID is not set');
return new TransactionBuilder(this._program.api, this._program.registry, 'send_message', ['Vft', 'GrantAdminRole', to], '(String, String, [u8;32])', 'Null', this._program.programId);
}
grantBurnerRole(to) {
if (!this._program.programId)
throw new Error('Program ID is not set');
return new TransactionBuilder(this._program.api, this._program.registry, 'send_message', ['Vft', 'GrantBurnerRole', to], '(String, String, [u8;32])', 'Null', this._program.programId);
}
grantMinterRole(to) {
if (!this._program.programId)
throw new Error('Program ID is not set');
return new TransactionBuilder(this._program.api, this._program.registry, 'send_message', ['Vft', 'GrantMinterRole', to], '(String, String, [u8;32])', 'Null', this._program.programId);
}
mint(to, value) {
if (!this._program.programId)
throw new Error('Program ID is not set');
return new TransactionBuilder(this._program.api, this._program.registry, 'send_message', ['Vft', 'Mint', to, value], '(String, String, [u8;32], U256)', 'bool', this._program.programId);
}
revokeAdminRole(from) {
if (!this._program.programId)
throw new Error('Program ID is not set');
return new TransactionBuilder(this._program.api, this._program.registry, 'send_message', ['Vft', 'RevokeAdminRole', from], '(String, String, [u8;32])', 'Null', this._program.programId);
}
revokeBurnerRole(from) {
if (!this._program.programId)
throw new Error('Program ID is not set');
return new TransactionBuilder(this._program.api, this._program.registry, 'send_message', ['Vft', 'RevokeBurnerRole', from], '(String, String, [u8;32])', 'Null', this._program.programId);
}
revokeMinterRole(from) {
if (!this._program.programId)
throw new Error('Program ID is not set');
return new TransactionBuilder(this._program.api, this._program.registry, 'send_message', ['Vft', 'RevokeMinterRole', from], '(String, String, [u8;32])', 'Null', this._program.programId);
}
setTransferFail(flag) {
if (!this._program.programId)
throw new Error('Program ID is not set');
return new TransactionBuilder(this._program.api, this._program.registry, 'send_message', ['Vft', 'SetTransferFail', flag], '(String, String, bool)', 'Null', this._program.programId);
}
transfer(to, value) {
if (!this._program.programId)
throw new Error('Program ID is not set');
return new TransactionBuilder(this._program.api, this._program.registry, 'send_message', ['Vft', 'Transfer', to, value], '(String, String, [u8;32], U256)', 'bool', this._program.programId);
}
transferFrom(from, to, value) {
if (!this._program.programId)
throw new Error('Program ID is not set');
return new TransactionBuilder(this._program.api, this._program.registry, 'send_message', ['Vft', 'TransferFrom', from, to, value], '(String, String, [u8;32], [u8;32], U256)', 'bool', this._program.programId);
}
approve(spender, value) {
if (!this._program.programId)
throw new Error('Program ID is not set');
return new TransactionBuilder(this._program.api, this._program.registry, 'send_message', ['Vft', 'Approve', spender, value], '(String, String, [u8;32], U256)', 'bool', this._program.programId);
}
async admins(originAddress, value, atBlock) {
const payload = this._program.registry.createType('(String, String)', ['Vft', 'Admins']).toHex();
if (!this._program.programId)
throw new Error('Program ID is not set');
const reply = await this._program.api.message.calculateReply({
destination: this._program.programId,
origin: originAddress ? decodeAddress(originAddress) : ZERO_ADDRESS,
payload,
value: value || 0,
gasLimit: this._program.api.blockGasLimit.toBigInt(),
at: atBlock,
});
throwOnErrorReply(reply.code, reply.payload.toU8a(), this._program.api.specVersion, this._program.registry);
const result = this._program.registry.createType('(String, String, Vec<[u8;32]>)', reply.payload);
return result[2].toJSON();
}
async burners(originAddress, value, atBlock) {
const payload = this._program.registry.createType('(String, String)', ['Vft', 'Burners']).toHex();
if (!this._program.programId)
throw new Error('Program ID is not set');
const reply = await this._program.api.message.calculateReply({
destination: this._program.programId,
origin: originAddress ? decodeAddress(originAddress) : ZERO_ADDRESS,
payload,
value: value || 0,
gasLimit: this._program.api.blockGasLimit.toBigInt(),
at: atBlock,
});
throwOnErrorReply(reply.code, reply.payload.toU8a(), this._program.api.specVersion, this._program.registry);
const result = this._program.registry.createType('(String, String, Vec<[u8;32]>)', reply.payload);
return result[2].toJSON();
}
async minters(originAddress, value, atBlock) {
const payload = this._program.registry.createType('(String, String)', ['Vft', 'Minters']).toHex();
if (!this._program.programId)
throw new Error('Program ID is not set');
const reply = await this._program.api.message.calculateReply({
destination: this._program.programId,
origin: originAddress ? decodeAddress(originAddress) : ZERO_ADDRESS,
payload,
value: value || 0,
gasLimit: this._program.api.blockGasLimit.toBigInt(),
at: atBlock,
});
throwOnErrorReply(reply.code, reply.payload.toU8a(), this._program.api.specVersion, this._program.registry);
const result = this._program.registry.createType('(String, String, Vec<[u8;32]>)', reply.payload);
return result[2].toJSON();
}
async allowance(owner, spender, originAddress, value, atBlock) {
const payload = this._program.registry.createType('(String, String, [u8;32], [u8;32])', ['Vft', 'Allowance', owner, spender]).toHex();
if (!this._program.programId)
throw new Error('Program ID is not set');
const reply = await this._program.api.message.calculateReply({
destination: this._program.programId,
origin: originAddress ? decodeAddress(originAddress) : ZERO_ADDRESS,
payload,
value: value || 0,
gasLimit: this._program.api.blockGasLimit.toBigInt(),
at: atBlock,
});
throwOnErrorReply(reply.code, reply.payload.toU8a(), this._program.api.specVersion, this._program.registry);
const result = this._program.registry.createType('(String, String, U256)', reply.payload);
return result[2].toBigInt();
}
async balanceOf(account, originAddress, value, atBlock) {
const payload = this._program.registry.createType('(String, String, [u8;32])', ['Vft', 'BalanceOf', account]).toHex();
if (!this._program.programId)
throw new Error('Program ID is not set');
const reply = await this._program.api.message.calculateReply({
destination: this._program.programId,
origin: originAddress ? decodeAddress(originAddress) : ZERO_ADDRESS,
payload,
value: value || 0,
gasLimit: this._program.api.blockGasLimit.toBigInt(),
at: atBlock,
});
throwOnErrorReply(reply.code, reply.payload.toU8a(), this._program.api.specVersion, this._program.registry);
const result = this._program.registry.createType('(String, String, U256)', reply.payload);
return result[2].toBigInt();
}
async decimals(originAddress, value, atBlock) {
const payload = this._program.registry.createType('(String, String)', ['Vft', 'Decimals']).toHex();
if (!this._program.programId)
throw new Error('Program ID is not set');
const reply = await this._program.api.message.calculateReply({
destination: this._program.programId,
origin: originAddress ? decodeAddress(originAddress) : ZERO_ADDRESS,
payload,
value: value || 0,
gasLimit: this._program.api.blockGasLimit.toBigInt(),
at: atBlock,
});
throwOnErrorReply(reply.code, reply.payload.toU8a(), this._program.api.specVersion, this._program.registry);
const result = this._program.registry.createType('(String, String, u8)', reply.payload);
return result[2].toNumber();
}
async name(originAddress, value, atBlock) {
const payload = this._program.registry.createType('(String, String)', ['Vft', 'Name']).toHex();
if (!this._program.programId)
throw new Error('Program ID is not set');
const reply = await this._program.api.message.calculateReply({
destination: this._program.programId,
origin: originAddress ? decodeAddress(originAddress) : ZERO_ADDRESS,
payload,
value: value || 0,
gasLimit: this._program.api.blockGasLimit.toBigInt(),
at: atBlock,
});
throwOnErrorReply(reply.code, reply.payload.toU8a(), this._program.api.specVersion, this._program.registry);
const result = this._program.registry.createType('(String, String, String)', reply.payload);
return result[2].toString();
}
async symbol(originAddress, value, atBlock) {
const payload = this._program.registry.createType('(String, String)', ['Vft', 'Symbol']).toHex();
if (!this._program.programId)
throw new Error('Program ID is not set');
const reply = await this._program.api.message.calculateReply({
destination: this._program.programId,
origin: originAddress ? decodeAddress(originAddress) : ZERO_ADDRESS,
payload,
value: value || 0,
gasLimit: this._program.api.blockGasLimit.toBigInt(),
at: atBlock,
});
throwOnErrorReply(reply.code, reply.payload.toU8a(), this._program.api.specVersion, this._program.registry);
const result = this._program.registry.createType('(String, String, String)', reply.payload);
return result[2].toString();
}
async totalSupply(originAddress, value, atBlock) {
const payload = this._program.registry.createType('(String, String)', ['Vft', 'TotalSupply']).toHex();
if (!this._program.programId)
throw new Error('Program ID is not set');
const reply = await this._program.api.message.calculateReply({
destination: this._program.programId,
origin: originAddress ? decodeAddress(originAddress) : ZERO_ADDRESS,
payload,
value: value || 0,
gasLimit: this._program.api.blockGasLimit.toBigInt(),
at: atBlock,
});
throwOnErrorReply(reply.code, reply.payload.toU8a(), this._program.api.specVersion, this._program.registry);
const result = this._program.registry.createType('(String, String, U256)', reply.payload);
return result[2].toBigInt();
}
subscribeToMintedEvent(callback) {
return this._program.api.gearEvents.subscribeToGearEvent('UserMessageSent', ({ data: { message } }) => {
if (!message.source.eq(this._program.programId) || !message.destination.eq(ZERO_ADDRESS)) {
return;
}
const payload = message.payload.toHex();
if (getServiceNamePrefix(payload) === 'Vft' && getFnNamePrefix(payload) === 'Minted') {
callback(this._program.registry.createType('(String, String, {"to":"[u8;32]","value":"U256"})', message.payload)[2].toJSON());
}
});
}
subscribeToBurnedEvent(callback) {
return this._program.api.gearEvents.subscribeToGearEvent('UserMessageSent', ({ data: { message } }) => {
if (!message.source.eq(this._program.programId) || !message.destination.eq(ZERO_ADDRESS)) {
return;
}
const payload = message.payload.toHex();
if (getServiceNamePrefix(payload) === 'Vft' && getFnNamePrefix(payload) === 'Burned') {
callback(this._program.registry.createType('(String, String, {"from":"[u8;32]","value":"U256"})', message.payload)[2].toJSON());
}
});
}
subscribeToApprovalEvent(callback) {
return this._program.api.gearEvents.subscribeToGearEvent('UserMessageSent', ({ data: { message } }) => {
if (!message.source.eq(this._program.programId) || !message.destination.eq(ZERO_ADDRESS)) {
return;
}
const payload = message.payload.toHex();
if (getServiceNamePrefix(payload) === 'Vft' && getFnNamePrefix(payload) === 'Approval') {
callback(this._program.registry.createType('(String, String, {"owner":"[u8;32]","spender":"[u8;32]","value":"U256"})', message.payload)[2].toJSON());
}
});
}
subscribeToTransferEvent(callback) {
return this._program.api.gearEvents.subscribeToGearEvent('UserMessageSent', ({ data: { message } }) => {
if (!message.source.eq(this._program.programId) || !message.destination.eq(ZERO_ADDRESS)) {
return;
}
const payload = message.payload.toHex();
if (getServiceNamePrefix(payload) === 'Vft' && getFnNamePrefix(payload) === 'Transfer') {
callback(this._program.registry.createType('(String, String, {"from":"[u8;32]","to":"[u8;32]","value":"U256"})', message.payload)[2].toJSON());
}
});
}
}