Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add BigInt serialization #228

Merged
merged 14 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"near-sdk-js": "file:../"
},
"devDependencies": {
"@types/lodash-es": "^4.17.6",
"ava": "^4.2.0",
"near-workspaces": "3.2.1",
"typescript": "^4.7.4"
Expand Down
2 changes: 1 addition & 1 deletion examples/src/clean-state.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NearBindgen, call, view, near } from "near-sdk-js";

@NearBindgen({})
class CleanState {
export class CleanState {
@call({})
clean({ keys }) {
keys.forEach((key) => near.storageRemove(key));
Expand Down
4 changes: 2 additions & 2 deletions examples/src/counter.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { NearBindgen, near, call, view, initialize } from "near-sdk-js";
import { NearBindgen, near, call, view } from "near-sdk-js";
import { isUndefined } from "lodash-es";

@NearBindgen({})
class Counter {
export class Counter {
constructor() {
this.count = 0;
}
Expand Down
4 changes: 2 additions & 2 deletions examples/src/counter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { NearBindgen, near, call, view, initialize } from "near-sdk-js";
import { NearBindgen, near, call, view } from "near-sdk-js";
import { isUndefined } from "lodash-es";
import { log } from "./log";

@NearBindgen({})
class Counter {
export class Counter {
count = 0;

@call({})
Expand Down
2 changes: 1 addition & 1 deletion examples/src/cross-contract-call.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NearBindgen, call, view, initialize, near, bytes } from "near-sdk-js";

@NearBindgen({ requireInit: true })
class OnCall {
export class OnCall {
constructor() {
this.personOnCall = "";
this.statusMessageContract = "";
Expand Down
2 changes: 1 addition & 1 deletion examples/src/fungible-token-helper.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NearBindgen, call, view } from "near-sdk-js";

@NearBindgen({})
class FungibleTokenHelper {
export class FungibleTokenHelper {
constructor() {
this.data = "";
}
Expand Down
2 changes: 1 addition & 1 deletion examples/src/fungible-token-lockable.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Account {
}

@NearBindgen({ initRequired: true })
class LockableFungibleToken {
export class LockableFungibleToken {
constructor() {
this.accounts = new LookupMap("a"); // Account ID -> Account mapping
this.totalSupply = 0; // Total supply of the all tokens
Expand Down
28 changes: 14 additions & 14 deletions examples/src/fungible-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@ import {
} from "near-sdk-js";

@NearBindgen({ initRequired: true })
class FungibleToken {
export class FungibleToken {
constructor() {
this.accounts = new LookupMap("a");
this.totalSupply = 0;
this.totalSupply = 0n;
}

@initialize({})
init({ prefix, totalSupply }) {
this.accounts = new LookupMap(prefix);
this.totalSupply = totalSupply;
this.totalSupply = BigInt(totalSupply);
this.accounts.set(near.signerAccountId(), this.totalSupply);
// In a real world Fungible Token contract, storage management is required to denfense drain-storage attack
}

internalDeposit({ accountId, amount }) {
let balance = this.accounts.get(accountId) || "0";
let newBalance = BigInt(balance) + BigInt(amount);
this.accounts.set(accountId, newBalance.toString());
this.totalSupply = (BigInt(this.totalSupply) + BigInt(amount)).toString();
let balance = this.accounts.get(accountId, { defaultValue: 0n });
let newBalance = balance + BigInt(amount);
this.accounts.set(accountId, newBalance);
this.totalSupply += BigInt(amount);
}

internalWithdraw({ accountId, amount }) {
let balance = this.accounts.get(accountId) || "0";
let newBalance = BigInt(balance) - BigInt(amount);
let balance = this.accounts.get(accountId, { defaultValue: 0n });
let newBalance = balance - BigInt(amount);
assert(newBalance >= 0n, "The account doesn't have enough balance");
this.accounts.set(accountId, newBalance.toString());
let newSupply = BigInt(this.totalSupply) - BigInt(amount);
this.accounts.set(accountId, newBalance);
let newSupply = this.totalSupply - BigInt(amount);
assert(newSupply >= 0n, "Total supply overflow");
this.totalSupply = newSupply.toString();
this.totalSupply = newSupply;
}

internalTransfer({ senderId, receiverId, amount, memo }) {
internalTransfer({ senderId, receiverId, amount, memo: _ }) {
assert(senderId != receiverId, "Sender and receiver should be different");
let amountInt = BigInt(amount);
assert(amountInt > 0n, "The amount should be a positive number");
Expand Down Expand Up @@ -82,6 +82,6 @@ class FungibleToken {

@view({})
ftBalanceOf({ accountId }) {
return this.accounts.get(accountId) || "0";
return this.accounts.get(accountId, { defaultValue: 0n });
}
}
2 changes: 1 addition & 1 deletion examples/src/log.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { near } from "near-sdk-js";

export function log(msg: any) {
export function log(msg: unknown) {
near.log(msg);
}
2 changes: 1 addition & 1 deletion examples/src/non-fungible-token-receiver.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NearBindgen, call, near, assert, initialize } from "near-sdk-js";

@NearBindgen({ requireInit: true })
class NftContract {
export class NftContract {
constructor() {
this.nonFungibleTokenAccountId = "";
}
Expand Down
12 changes: 9 additions & 3 deletions examples/src/non-fungible-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Token {
}

@NearBindgen({ requireInit: true })
class NftContract {
export class NftContract {
constructor() {
this.owner_id = "";
this.owner_by_id = new LookupMap("a");
Expand All @@ -29,7 +29,13 @@ class NftContract {
this.owner_by_id = new LookupMap(owner_by_id_prefix);
}

internalTransfer({ sender_id, receiver_id, token_id, approval_id, memo }) {
internalTransfer({
sender_id,
receiver_id,
token_id,
approval_id: _ai,
memo: _m,
}) {
let owner_id = this.owner_by_id.get(token_id);

assert(owner_id !== null, "Token not found");
Expand Down Expand Up @@ -125,7 +131,7 @@ class NftContract {
}

@call({})
nftMint({ token_id, token_owner_id, token_metadata }) {
nftMint({ token_id, token_owner_id, token_metadata: _ }) {
let sender_id = near.predecessorAccountId();
assert(sender_id === this.owner_id, "Unauthorized");
assert(this.owner_by_id.get(token_id) === null, "Token ID must be unique");
Expand Down
3 changes: 2 additions & 1 deletion examples/src/parking-lot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ class Engine {
}

@NearBindgen({})
class ParkingLot {
export class ParkingLot {
cars: LookupMap<CarSpecs>;

constructor() {
this.cars = new LookupMap<CarSpecs>("a");
}
Expand Down
2 changes: 1 addition & 1 deletion examples/src/status-message-collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "near-sdk-js";

@NearBindgen({})
class StatusMessage {
export class StatusMessage {
constructor() {
this.records = new UnorderedMap("a");
this.uniqueValues = new LookupSet("b");
Expand Down
2 changes: 1 addition & 1 deletion examples/src/status-message.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NearBindgen, call, view, near } from "near-sdk-js";

@NearBindgen({})
class StatusMessage {
export class StatusMessage {
constructor() {
this.records = {};
}
Expand Down
1 change: 1 addition & 0 deletions examples/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"experimentalDecorators": true,
"target": "es2020",
"moduleResolution": "node",
"noEmit": true
},
"exclude": ["node_modules"]
Expand Down
12 changes: 12 additions & 0 deletions examples/yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion jsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"exclude": ["node_modules"],
"include": ["cli/*.js"]
"include": ["cli"]
}
5 changes: 2 additions & 3 deletions lib/build-tools/near-bindgen-exporter.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 42 additions & 42 deletions lib/build-tools/near-bindgen-exporter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/collections/lookup-map.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading