Skip to content

Commit

Permalink
added example how to work with BigInt (js)
Browse files Browse the repository at this point in the history
  • Loading branch information
garikbesson committed Aug 13, 2024
1 parent d0048c9 commit 2bbc094
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions collections-js/sandbox-test/main.ava.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
12 changes: 12 additions & 0 deletions collections-js/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Storage {
};

greeting: string = 'Hello';
big_int: BigInt = BigInt(0);
vector: Vector<number> = new Vector<number>('uid-1');
lookup_set: LookupSet<number> = new LookupSet<number>('uid-2');
unordered_set: UnorderedSet<number> = new UnorderedSet<number>('uid-3');
Expand All @@ -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 }) {
Expand Down

0 comments on commit 2bbc094

Please sign in to comment.