From 2bbc0941e227193643acb40d0d969ba77db8727d Mon Sep 17 00:00:00 2001 From: garikbesson Date: Tue, 13 Aug 2024 14:26:31 +0200 Subject: [PATCH] added example how to work with BigInt (js) --- collections-js/sandbox-test/main.ava.js | 13 +++++++++++++ collections-js/src/contract.ts | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/collections-js/sandbox-test/main.ava.js b/collections-js/sandbox-test/main.ava.js index 338d2b1..33c114e 100644 --- a/collections-js/sandbox-test/main.ava.js +++ b/collections-js/sandbox-test/main.ava.js @@ -32,6 +32,19 @@ test.afterEach.always(async (t) => { }); }); +test('testing bigint methods', async (t) => { + const { root, contract } = t.context.accounts; + const newValue = '1'; + + let currentValue = await contract.view('get_big_int', {}); + t.assert(currentValue === '0', "Incorrect big int"); + + await root.call(contract, 'set_big_int', { value: newValue }); + + currentValue = await contract.view('get_big_int', {}); + t.assert(currentValue === currentValue, "Incorrect big int"); +}); + test('testing string methods', async (t) => { const { root, contract } = t.context.accounts; const newGreeting = 'Hi'; diff --git a/collections-js/src/contract.ts b/collections-js/src/contract.ts index 187c172..264daf3 100644 --- a/collections-js/src/contract.ts +++ b/collections-js/src/contract.ts @@ -13,6 +13,7 @@ class Storage { }; greeting: string = 'Hello'; + big_int: BigInt = BigInt(0); vector: Vector = new Vector('uid-1'); lookup_set: LookupSet = new LookupSet('uid-2'); unordered_set: UnorderedSet = new UnorderedSet('uid-3'); @@ -31,6 +32,17 @@ class Storage { this.greeting = greeting; } + @view({}) // This method is read-only and can be called for free + get_big_int(): string { + return this.big_int as unknown as string; // Because of serializing process it actually returns BigInt as string for view methods + } + + @call({}) // This method changes the state, for which it cost gas + set_big_int({ value }: { value: string }): void { + near.log(`Saving big int value: ${value}`); + this.big_int = BigInt(value); + } + // Vector @call({}) push_vector({ value }: { value: number }) {