Skip to content

Commit

Permalink
#70, #79: Update token wrapper to add KCS-4 functions and improve log…
Browse files Browse the repository at this point in the history
…ging of error messages
  • Loading branch information
mvandeberg committed Aug 9, 2024
1 parent f92da10 commit 0b515de
Show file tree
Hide file tree
Showing 2 changed files with 214 additions and 45 deletions.
92 changes: 74 additions & 18 deletions __tests__/token.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Base58, MockVM, Protobuf, StringBytes, Token } from "../index";
import { token, system_calls, chain } from '@koinos/proto-as';
import { Arrays, Base58, MockVM, Protobuf, StringBytes, Token } from "../index";
import { kcs4, system_calls, chain } from '@koinos/proto-as';


const mockTokenContractIdAccount = Base58.decode('1DQzuCcTKacbs9GGScFTU1Hc8BsyARTPqe');
Expand All @@ -10,9 +10,9 @@ const mockId = StringBytes.stringToBytes("0x12345");
describe('token', () => {
it('should get the name of a token', () => {
const tokenName = 'Token';
const nameRes = new token.name_result(tokenName);
const nameRes = new kcs4.name_result(tokenName);
MockVM.setCallContractResults([
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(nameRes, token.name_result.encode)))]);
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(nameRes, kcs4.name_result.encode)))]);

const tkn = new Token(mockTokenContractIdAccount);
const name = tkn.name();
Expand All @@ -22,9 +22,9 @@ describe('token', () => {

it('should get the symbol of a token', () => {
const tokenSymbol = 'TKN';
const symbolRes = new token.symbol_result(tokenSymbol);
const symbolRes = new kcs4.symbol_result(tokenSymbol);
MockVM.setCallContractResults([
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(symbolRes, token.symbol_result.encode)))]);
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(symbolRes, kcs4.symbol_result.encode)))]);

const tkn = new Token(mockTokenContractIdAccount);
const symbol = tkn.symbol();
Expand All @@ -34,21 +34,37 @@ describe('token', () => {

it('should get the decimals of a token', () => {
const tokenDecimals = 8;
const decimalsRes = new token.decimals_result(tokenDecimals);
const decimalsRes = new kcs4.decimals_result(tokenDecimals);
MockVM.setCallContractResults([
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(decimalsRes, token.decimals_result.encode)))]);
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(decimalsRes, kcs4.decimals_result.encode)))]);

const tkn = new Token(mockTokenContractIdAccount);
const decimals = tkn.decimals();

expect(decimals).toBe(tokenDecimals);
});

it('should get info of a token', () => {
const tokenName = "Token";
const tokenSymbol = "TKN";
const tokenDecimals = 8;
const getInfoRes = new kcs4.get_info_result(tokenName, tokenSymbol, tokenDecimals);
MockVM.setCallContractResults([
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(getInfoRes, kcs4.get_info_result.encode)))]);

const tkn = new Token(mockTokenContractIdAccount);
const info = tkn.getInfo();

expect(info.name).toBe(tokenName);
expect(info.symbol).toBe(tokenSymbol);
expect(info.decimals).toBe(tokenDecimals);
});

it('should get the total supply of a token', () => {
const tokenTotalSupply = 1008767;
const totalSupplyRes = new token.total_supply_result(tokenTotalSupply);
const totalSupplyRes = new kcs4.total_supply_result(tokenTotalSupply);
MockVM.setCallContractResults([
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(totalSupplyRes, token.total_supply_result.encode)))]);
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(totalSupplyRes, kcs4.total_supply_result.encode)))]);

const tkn = new Token(mockTokenContractIdAccount);
const totalSupply = tkn.totalSupply();
Expand All @@ -58,20 +74,49 @@ describe('token', () => {

it('should get the balance of an account', () => {
const accountBalance = 76231876;
const balanceOfRes = new token.balance_of_result(accountBalance);
const balanceOfRes = new kcs4.balance_of_result(accountBalance);
MockVM.setCallContractResults([
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(balanceOfRes, token.balance_of_result.encode)))]);
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(balanceOfRes, kcs4.balance_of_result.encode)))]);

const tkn = new Token(mockTokenContractIdAccount);
const balance = tkn.balanceOf(mockAccount1);

expect(balance).toBe(accountBalance);
});

it('should get an allowance', () => {
const allowanceValue = 12345678;
const allowanceRes = new kcs4.allowance_result(allowanceValue);
MockVM.setCallContractResults([
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(allowanceRes, kcs4.allowance_result.encode)))]);

const tkn = new Token(mockTokenContractIdAccount);
const allowance = tkn.allowance(mockAccount1, mockAccount2);

expect(allowance).toBe(allowanceValue);
});

it('should get allowances', () => {
const allowanceValueA = 12345678;
const allowanceValueB = 23456789;
const allowancesRes = new kcs4.get_allowances_result(mockAccount1, [new kcs4.spender_value(mockAccount1, allowanceValueA), new kcs4.spender_value(mockAccount2, allowanceValueB)]);
MockVM.setCallContractResults([
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(allowancesRes, kcs4.get_allowances_result.encode)))]);

const tkn = new Token(mockTokenContractIdAccount);
const allowances = tkn.getAllowances(mockAccount1, new Uint8Array(0), 10, true);

expect(allowances.length).toBe(2);
expect(Arrays.equal(allowances[0].spender, mockAccount1)).toBe(true);
expect(allowances[0].value).toBe(allowanceValueA);
expect(Arrays.equal(allowances[1].spender, mockAccount2)).toBe(true);
expect(allowances[1].value).toBe(allowanceValueB);
});

it('should transfer a token', () => {
let transferRes = new token.transfer_result();
let transferRes = new kcs4.transfer_result();
MockVM.setCallContractResults([
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(transferRes, token.transfer_result.encode)))]);
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(transferRes, kcs4.transfer_result.encode)))]);

const tkn = new Token(mockTokenContractIdAccount);
let transfer = tkn.transfer(mockAccount1, mockAccount2, 167);
Expand All @@ -80,9 +125,9 @@ describe('token', () => {
});

it('should/not mint a token', () => {
let mintRes = new token.mint_result();
let mintRes = new kcs4.mint_result();
MockVM.setCallContractResults([
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(mintRes, token.mint_result.encode)))]);
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(mintRes, kcs4.mint_result.encode)))]);

const tkn = new Token(mockTokenContractIdAccount);
let mint = tkn.mint(mockAccount1, 167);
Expand All @@ -91,13 +136,24 @@ describe('token', () => {
});

it('should burn a token', () => {
let burnRes = new token.burn_result();
let burnRes = new kcs4.burn_result();
MockVM.setCallContractResults([
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(burnRes, token.burn_result.encode)))]);
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(burnRes, kcs4.burn_result.encode)))]);

const tkn = new Token(mockTokenContractIdAccount);
let burn = tkn.burn(mockAccount1, 167);

expect(burn).toBe(true);
});

it('should/not create an allowance', () => {
let allowanceRes = new kcs4.approve_result();
MockVM.setCallContractResults([
new system_calls.exit_arguments(0, new chain.result(Protobuf.encode(allowanceRes, kcs4.approve_result.encode)))]);

const tkn = new Token(mockTokenContractIdAccount);
let approve = tkn.approve(mockAccount1, mockAccount2, 123);

expect(approve).toBe(true);
})
});
Loading

0 comments on commit 0b515de

Please sign in to comment.