From 0adb4cc7596c0ac72dd989bf0da714c87215c706 Mon Sep 17 00:00:00 2001 From: Dennis <10233439+idea404@users.noreply.github.com> Date: Thu, 13 Oct 2022 11:59:48 +0000 Subject: [PATCH] save numerical values as bigint --- examples/__tests__/test-fungible-token.ava.js | 2 +- examples/src/fungible-token.ts | 26 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/examples/__tests__/test-fungible-token.ava.js b/examples/__tests__/test-fungible-token.ava.js index 2ab79732..e39d0f43 100644 --- a/examples/__tests__/test-fungible-token.ava.js +++ b/examples/__tests__/test-fungible-token.ava.js @@ -5,7 +5,7 @@ test.beforeEach(async (t) => { const worker = await Worker.init(); const totalSupply = 1000; - const yoctoAccountStorage = "45"; + const yoctoAccountStorage = "90"; const root = worker.rootAccount; const xcc = await root.devDeploy("./build/fungible-token-helper.wasm"); diff --git a/examples/src/fungible-token.ts b/examples/src/fungible-token.ts index db92ec57..8a4aa7ee 100644 --- a/examples/src/fungible-token.ts +++ b/examples/src/fungible-token.ts @@ -11,30 +11,30 @@ import { @NearBindgen({ requireInit: true }) export class FungibleToken { - accounts: LookupMap; + accounts: LookupMap; accountRegistrants: LookupMap; - accountDeposits: LookupMap; - totalSupply: string; + accountDeposits: LookupMap; + totalSupply: bigint; constructor() { this.accounts = new LookupMap("a"); this.accountRegistrants = new LookupMap("r"); this.accountDeposits = new LookupMap("d"); - this.totalSupply = "0"; + this.totalSupply = BigInt("0"); } @initialize({}) init({ owner_id, total_supply }: { owner_id: string; total_supply: string }) { Assertions.isLeftGreaterThanRight(total_supply, 0); validateAccountId(owner_id); - this.totalSupply = total_supply; + this.totalSupply = BigInt(total_supply); this.accounts.set(owner_id, this.totalSupply); } internalGetAccountStorageUsage(accountLength: number): bigint { const initialStorageUsage = near.storageUsage(); const tempAccountId = "a".repeat(64); - this.accounts.set(tempAccountId, "0"); + this.accounts.set(tempAccountId, BigInt("0")); const len64StorageUsage = near.storageUsage() - initialStorageUsage; const len1StorageUsage = len64StorageUsage / BigInt(64); const lenAccountStorageUsage = len1StorageUsage * BigInt(accountLength); @@ -55,9 +55,9 @@ export class FungibleToken { !this.accounts.containsKey(accountId), "Account is already registered" ); - this.accounts.set(accountId, "0"); + this.accounts.set(accountId, BigInt("0")); this.accountRegistrants.set(accountId, registrantAccountId); - this.accountDeposits.set(accountId, amount); + this.accountDeposits.set(accountId, BigInt(amount)); } internalSendNEAR(receivingAccountId: string, amount: bigint) { @@ -77,15 +77,15 @@ export class FungibleToken { this.accounts.containsKey(accountId), `Account ${accountId} is not registered` ); - return this.accounts.get(accountId); + return this.accounts.get(accountId).toString(); } internalDeposit(accountId: string, amount: string) { const balance = this.internalGetBalance(accountId); const newBalance = BigInt(balance) + BigInt(amount); - this.accounts.set(accountId, newBalance.toString()); + this.accounts.set(accountId, newBalance); const newSupply = BigInt(this.totalSupply) + BigInt(amount); - this.totalSupply = newSupply.toString(); + this.totalSupply = newSupply; } internalWithdraw(accountId: string, amount: string) { @@ -98,8 +98,8 @@ export class FungibleToken { "The account doesn't have enough balance" ); Assertions.isLeftGreaterThanRight(newSupply, -1, "Total supply overflow"); - this.accounts.set(accountId, newBalance.toString()); - this.totalSupply = newSupply.toString(); + this.accounts.set(accountId, newBalance); + this.totalSupply = newSupply; } internalTransfer(