Skip to content

Commit

Permalink
use dynamic storage on accountId length
Browse files Browse the repository at this point in the history
  • Loading branch information
idea404 committed Oct 11, 2022
1 parent 972cb40 commit 0f05f83
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions examples/__tests__/test-fungible-token.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test.beforeEach(async (t) => {
const worker = await Worker.init();

const totalSupply = 1000;
const yoctoAccountStorage = "324";
const yoctoAccountStorage = "45";

const root = worker.rootAccount;
const xcc = await root.devDeploy("./build/fungible-token-helper.wasm");
Expand Down Expand Up @@ -110,7 +110,7 @@ test("should return message when trying to pay for storage with less than the re
ft,
"storage_deposit",
{ account_id: alice.accountId },
{ attachedDeposit: NEAR.from("100").toJSON() }
{ attachedDeposit: NEAR.from("40").toJSON() }
);
t.is(
result.message,
Expand Down
10 changes: 6 additions & 4 deletions examples/src/fungible-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ export class FungibleToken {
this.accounts.set(owner_id, this.totalSupply);
}

internalGetMaxAccountStorageUsage(): bigint {
internalGetAccountStorageUsage(accountLength: number): bigint {
const initialStorageUsage = near.storageUsage();
const tempAccountId = "a".repeat(64);
this.accounts.set(tempAccountId, "0");
const maxAccountStorageUsage = near.storageUsage() - initialStorageUsage;
const len64StorageUsage = near.storageUsage() - initialStorageUsage;
const len1StorageUsage = len64StorageUsage / BigInt(64);
const lenAccountStorageUsage = len1StorageUsage * BigInt(accountLength);
this.accounts.remove(tempAccountId);
return maxAccountStorageUsage * BigInt(3); // we create an entry in 3 maps
return lenAccountStorageUsage * BigInt(3); // we create an entry in 3 maps
}

internalRegisterAccount({
Expand Down Expand Up @@ -120,7 +122,7 @@ export class FungibleToken {
}
return { message: "Account is already registered" };
}
const storageCost = this.internalGetMaxAccountStorageUsage();
const storageCost = this.internalGetAccountStorageUsage(accountId.length);
if (attachedDeposit < storageCost) {
this.internalSendNEAR(near.predecessorAccountId(), attachedDeposit);
return {
Expand Down

0 comments on commit 0f05f83

Please sign in to comment.