Skip to content

Commit

Permalink
update to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
idea404 committed Oct 11, 2022
1 parent 5f21d33 commit 5832324
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"build:counter-ts": "near-sdk-js build src/counter.ts build/counter-ts.wasm",
"build:cross-contract-call": "near-sdk-js build src/status-message.js build/status-message.wasm && near-sdk-js build src/cross-contract-call.js build/cross-contract-call.wasm",
"build:fungible-token-lockable": "near-sdk-js build src/fungible-token-lockable.js build/fungible-token-lockable.wasm",
"build:fungible-token": "near-sdk-js build src/fungible-token.js build/fungible-token.wasm && near-sdk-js build src/fungible-token-helper.js build/fungible-token-helper.wasm",
"build:fungible-token": "near-sdk-js build src/fungible-token.ts build/fungible-token.wasm && near-sdk-js build src/fungible-token-helper.ts build/fungible-token-helper.wasm",
"build:non-fungible-token": "near-sdk-js build src/non-fungible-token-receiver.js build/non-fungible-token-receiver.wasm && near-sdk-js build src/non-fungible-token.js build/non-fungible-token.wasm",
"build:status-message-collections": "near-sdk-js build src/status-message-collections.js build/status-message-collections.wasm",
"build:parking-lot": "near-sdk-js build src/parking-lot.ts build/parking-lot.wasm",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ import { NearBindgen, call, view } from "near-sdk-js";

@NearBindgen({})
class FungibleTokenHelper {
constructor() {
this.data = "";
}
data = "";

@call({})
ft_on_transfer({ sender_id, amount, msg, receiver_id }) {
ft_on_transfer({ sender_id, amount, msg, receiver_id }: { sender_id: string, amount: string, msg: string, receiver_id: string }) {
const concatString = `[${amount} from ${sender_id} to ${receiver_id}] ${msg} `;
this.data = this.data.concat("", concatString);
}
Expand Down
38 changes: 20 additions & 18 deletions examples/src/fungible-token.js → examples/src/fungible-token.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { NearBindgen, call, view, initialize, near, LookupMap, assert, validateAccountId } from "near-sdk-js";

@NearBindgen({ initRequired: true })
@NearBindgen({ requireInit: true })
export class FungibleToken {
accounts = new LookupMap("a");
accountRegistrants = new LookupMap("r");
accountDeposits = new LookupMap("c");
accounts: LookupMap<string> = new LookupMap("a");
accountRegistrants: LookupMap<string> = new LookupMap("r");
accountDeposits: LookupMap<string> = new LookupMap("c");
totalSupply = "0";

@initialize({})
init({ owner_id, total_supply }) {
init({ owner_id, total_supply }: { owner_id: string, total_supply: string }) {
Assertions.isLeftGreaterThanRight(total_supply, 0);
validateAccountId(owner_id);
this.totalSupply = total_supply;
this.accounts.set(owner_id, this.totalSupply);
}

internalGetMaxAccountStorageUsage() {
internalGetMaxAccountStorageUsage(): bigint {
const initialStorageUsage = near.storageUsage();
const tempAccountId = "a".repeat(64);
this.accounts.set(tempAccountId, "0");
Expand All @@ -24,35 +24,35 @@ export class FungibleToken {
return maxAccountStorageUsage * BigInt(3); // we create an entry in 3 maps
}

internalRegisterAccount({ registrantAccountId, accountId, amountStr }) {
internalRegisterAccount({ registrantAccountId, accountId, amountStr }: { registrantAccountId: string, accountId: string, amountStr: string }) {
assert(!this.accounts.containsKey(accountId), "Account is already registered");
this.accounts.set(accountId, "0");
this.accountRegistrants.set(accountId, registrantAccountId);
this.accountDeposits.set(accountId, amountStr);
}

internalSendNEAR(receivingAccountId, amountBigInt) {
internalSendNEAR(receivingAccountId: string, amountBigInt: bigint) {
Assertions.isLeftGreaterThanRight(amountBigInt, 0);
Assertions.isLeftGreaterThanRight(near.accountBalance(), amountBigInt, `Not enough balance ${near.accountBalance()} to send ${amountBigInt}`);
const promise = near.promiseBatchCreate(receivingAccountId);
near.promiseBatchActionTransfer(promise, amountBigInt);
near.promiseReturn(promise);
}

internalGetBalance(accountId) {
internalGetBalance(accountId: string): string {
assert(this.accounts.containsKey(accountId), `Account ${accountId} is not registered`);
return this.accounts.get(accountId);
}

internalDeposit(accountId, amount) {
internalDeposit(accountId: string, amount: string) {
let balance = this.internalGetBalance(accountId);
let newBalance = BigInt(balance) + BigInt(amount);
this.accounts.set(accountId, newBalance.toString());
let newSupply = BigInt(this.totalSupply) + BigInt(amount);
this.totalSupply = newSupply.toString();
}

internalWithdraw(accountId, amount) {
internalWithdraw(accountId: string, amount: string) {
let balance = this.internalGetBalance(accountId);
let newBalance = BigInt(balance) - BigInt(amount);
let newSupply = BigInt(this.totalSupply) - BigInt(amount);
Expand All @@ -62,16 +62,17 @@ export class FungibleToken {
this.totalSupply = newSupply.toString();
}

internalTransfer(senderId, receiverId, amount, memo = null) {
internalTransfer(senderId: string, receiverId: string, amount: string, _memo: string = null) {
assert(senderId != receiverId, "Sender and receiver should be different");
Assertions.isLeftGreaterThanRight(amount, 0);
this.internalWithdraw(senderId, amount);
this.internalDeposit(receiverId, amount);
}

@call({ payableFunction: true })
storage_deposit({ account_id }) {
storage_deposit({ account_id }: { account_id: string }) {
const accountId = account_id || near.predecessorAccountId();
validateAccountId(accountId);
let attachedDeposit = near.attachedDeposit();
if (this.accounts.containsKey(accountId)) {
if (attachedDeposit > 0) {
Expand Down Expand Up @@ -99,15 +100,15 @@ export class FungibleToken {
}

@call({ payableFunction: true })
ft_transfer({ receiver_id, amount, memo }) {
ft_transfer({ receiver_id, amount, memo }: { receiver_id: string, amount: string, memo: string }) {
Assertions.hasAtLeastOneAttachedYocto();
let senderId = near.predecessorAccountId();
near.log("Transfer " + amount + " token from " + senderId + " to " + receiver_id);
this.internalTransfer(senderId, receiver_id, amount, memo);
}

@call({ payableFunction: true })
ft_transfer_call({ receiver_id, amount, memo, msg }) {
ft_transfer_call({ receiver_id, amount, memo, msg }: { receiver_id: string, amount: string, memo: string, msg: string }) {
Assertions.hasAtLeastOneAttachedYocto();
let senderId = near.predecessorAccountId();
this.internalTransfer(senderId, receiver_id, amount, memo);
Expand All @@ -120,7 +121,7 @@ export class FungibleToken {
};
near.log("Transfer call " + amount + " token from " + senderId + " to " + receiver_id + " with message " + msg);
near.promiseBatchActionFunctionCall(promise, "ft_on_transfer", JSON.stringify(params), 0, 30000000000000);
return near.promiseReturn();
return near.promiseReturn(promise);
}

@view({})
Expand All @@ -129,7 +130,8 @@ export class FungibleToken {
}

@view({})
ft_balance_of({ account_id }) {
ft_balance_of({ account_id }: { account_id: string }) {
validateAccountId(account_id);
return this.internalGetBalance(account_id);
}
}
Expand All @@ -139,7 +141,7 @@ class Assertions {
assert(near.attachedDeposit() > BigInt(0), "Requires at least 1 yoctoNEAR to ensure signature");
}

static isLeftGreaterThanRight(left, right, message=null) {
static isLeftGreaterThanRight(left, right, message = null) {
const msg = message || `Provided amount ${left} should be greater than ${right}`;
assert(BigInt(left) > BigInt(right), msg);
}
Expand Down

0 comments on commit 5832324

Please sign in to comment.