Skip to content

Commit

Permalink
private and payable functionality (#204)
Browse files Browse the repository at this point in the history
* test contracts for payable and private added

* basic payable test structure added

* added decorator parameters

* payable tests added

* private tests added

* brackets added to all contracts

* build added

* privateFunction is used in exampels

* fix call return

* typo fixed

* high level promise api tests syntax fixed

Co-authored-by: Bo Yao <bo@near.org>
  • Loading branch information
volovyks and ailisp authored Sep 7, 2022
1 parent 3d20fae commit 5667584
Show file tree
Hide file tree
Showing 33 changed files with 351 additions and 135 deletions.
6 changes: 3 additions & 3 deletions examples/src/clean-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ import { NearBindgen, call, view, near } from 'near-sdk-js'

@NearBindgen({})
class CleanState {
@call
@call({})
clean({ keys }) {
keys.forEach(key => near.storageRemove(key))
}

@call
@call({})
put({ key, value }) {
near.storageWrite(key, value)
}

@view
@view({})
get({ key }) {

return near.storageRead(key)
Expand Down
6 changes: 3 additions & 3 deletions examples/src/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class Counter {
this.count = 0
}

@call
@call({})
increase({ n = 1 }) {
this.count += n
near.log(`Counter increased to ${this.count}`)
}

@call
@call({})
decrease({ n }) {
// you can use default argument `n=1` too
// this is to illustrate a npm dependency: lodash can be used
Expand All @@ -25,7 +25,7 @@ class Counter {
near.log(`Counter decreased to ${this.count}`)
}

@view
@view({})
getCount() {
return this.count
}
Expand Down
6 changes: 3 additions & 3 deletions examples/src/counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { log } from './log'
class Counter {
count: number = 0;

@call
@call({})
increase({ n = 1 }: { n: number }) {
this.count += n
near.log(`Counter increased to ${this.count}`)
}

@call
@call({})
decrease({ n }: { n: number }) {
// you can use default argument `n=1` too
// this is to illustrate a npm dependency: lodash can be used
Expand All @@ -25,7 +25,7 @@ class Counter {
log(`Counter decreased to ${this.count}`)
}

@view
@view({})
getCount(): number {
return this.count
}
Expand Down
11 changes: 4 additions & 7 deletions examples/src/cross-contract-call.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,23 @@ class OnCall {
this.statusMessageContract = ''
}

@initialize
@initialize({})
init({ statusMessageContract }) {
this.personOnCall = "undefined"
this.statusMessageContract = statusMessageContract
}

@call
@call({})
set_person_on_call({ accountId }) {
near.log(`Trying to set ${accountId} on-call`)
const promise = near.promiseBatchCreate(this.statusMessageContract)
near.promiseBatchActionFunctionCall(promise, 'get_status', bytes(JSON.stringify({ account_id: accountId })), 0, 30000000000000)
near.promiseThen(promise, near.currentAccountId(), '_set_person_on_call_private', bytes(JSON.stringify({ accountId: accountId })), 0, 30000000000000);
}

@call
@call({ privateFunction: true })
_set_person_on_call_private({ accountId }) {
near.log(`_set_person_on_call_private called, accountId ${accountId}`)
if (near.currentAccountId() !== near.predecessorAccountId()) {
throw Error('Function can be used as a callback only')
}
const status = JSON.parse(near.promiseResult(0))
near.log(`${accountId} status is ${status}`)
if (status === 'AVAILABLE') {
Expand All @@ -37,7 +34,7 @@ class OnCall {
}
}

@view
@view({})
person_on_call() {
near.log(`Returning person on-call: ${this.personOnCall}`)
return this.personOnCall
Expand Down
4 changes: 2 additions & 2 deletions examples/src/fungible-token-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ class FungibleTokenHelper {
this.data = "";
}

@call
@call({})
ftOnTransfer({ senderId, amount, msg, receiverId }) {
const concatString = `[${amount} from ${senderId} to ${receiverId}] ${msg} `;
this.data = this.data.concat("", concatString);
}

@view
@view({})
getContractData() {
return this.data;
}
Expand Down
22 changes: 11 additions & 11 deletions examples/src/fungible-token-lockable.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class LockableFungibleToken {
this.totalSupply = 0 // Total supply of the all tokens
}

@initialize
@initialize({})
init({ prefix, totalSupply }) {
this.accounts = new LookupMap(prefix)
this.totalSupply = totalSupply
Expand All @@ -78,7 +78,7 @@ class LockableFungibleToken {
this.accounts.set(accountId, account)
}

@call
@call({})
setAllowance({ escrowAccountId, allowance }) {
let ownerId = near.predecessorAccountId()
if (escrowAccountId === ownerId) {
Expand All @@ -94,7 +94,7 @@ class LockableFungibleToken {
this.setAccount(ownerId, account)
}

@call
@call({})
lock({ ownerId, lockAmount }) {
if (lockAmount <= 0) {
throw Error("Can't lock 0 or less tokens")
Expand Down Expand Up @@ -124,7 +124,7 @@ class LockableFungibleToken {
this.setAccount(ownerId, account)
}

@call
@call({})
unlock({ ownerId, unlockAmount }) {
if (unlockAmount <= 0) {
throw Error("Can't unlock 0 or less tokens")
Expand All @@ -151,7 +151,7 @@ class LockableFungibleToken {
this.setAccount(ownerId, account)
}

@call
@call({})
transferFrom({ ownerId, newOwnerId, amount }) {
if (amount <= 0) {
throw Error("Can't transfer 0 or less tokens")
Expand Down Expand Up @@ -197,32 +197,32 @@ class LockableFungibleToken {
this.setAccount(newOwnerId, newAccount)
}

@call
@call({})
transfer({ newOwnerId, amount }) {
this.transferFrom({ ownerId: near.predecessorAccountId(), newOwnerId, amount })
}

@view
@view({})
getTotalSupply() {
return this.totalSupply
}

@view
@view({})
getTotalBalance({ ownerId }) {
return this.getAccount(ownerId).totalBalance()
}

@view
@view({})
getUnlockedBalance({ ownerId }) {
return this.getAccount(ownerId).balance
}

@view
@view({})
getAllowance({ ownerId, escrowAccountId }) {
return this.getAccount(ownerId).getAllowance(escrowAccountId)
}

@view
@view({})
getLockedBalance({ ownerId, escrowAccountId }) {
return this.getAccount(ownerId).getLockedBalance(escrowAccountId)
}
Expand Down
10 changes: 5 additions & 5 deletions examples/src/fungible-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class FungibleToken {
this.totalSupply = 0
}

@initialize
@initialize({})
init({ prefix, totalSupply }) {
this.accounts = new LookupMap(prefix)
this.totalSupply = totalSupply
Expand Down Expand Up @@ -49,13 +49,13 @@ class FungibleToken {
this.internalDeposit({ accountId: receiverId, amount })
}

@call
@call({})
ftTransfer({ receiverId, amount, memo }) {
let senderId = near.predecessorAccountId()
this.internalTransfer({ senderId, receiverId, amount, memo })
}

@call
@call({})
ftTransferCall({ receiverId, amount, memo, msg }) {
let senderId = near.predecessorAccountId()
this.internalTransfer({ senderId, receiverId, amount, memo });
Expand All @@ -65,12 +65,12 @@ class FungibleToken {
return near.promiseReturn();
}

@view
@view({})
ftTotalSupply() {
return this.totalSupply
}

@view
@view({})
ftBalanceOf({ accountId }) {
return this.accounts.get(accountId) || '0'
}
Expand Down
4 changes: 2 additions & 2 deletions examples/src/non-fungible-token-receiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ class NftContract {
this.nonFungibleTokenAccountId = ''
}

@initialize
@initialize({})
init({ nonFungibleTokenAccountId }) {
this.nonFungibleTokenAccountId = nonFungibleTokenAccountId
}

@call
@call({})
nftOnTransfer({ senderId, previousOwnerId, tokenId, msg }) {
near.log(`nftOnTransfer called, params: senderId: ${senderId}, previousOwnerId: ${previousOwnerId}, tokenId: ${tokenId}, msg: ${msg}`)
assert(
Expand Down
15 changes: 6 additions & 9 deletions examples/src/non-fungible-token.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class NftContract {
this.owner_by_id = new LookupMap('a')
}

@initialize
@initialize({})
init({ owner_id, owner_by_id_prefix }) {
this.owner_id = owner_id
this.owner_by_id = new LookupMap(owner_by_id_prefix)
Expand All @@ -32,13 +32,13 @@ class NftContract {
return owner_id
}

@call
@call({})
nftTransfer({ receiver_id, token_id, approval_id, memo }) {
let sender_id = near.predecessorAccountId()
this.internalTransfer({ sender_id, receiver_id, token_id, approval_id, memo })
}

@call
@call({})
nftTransferCall({ receiver_id, token_id, approval_id, memo, msg }) {
near.log(`nftTransferCall called, receiver_id ${receiver_id}, token_id ${token_id}`)
let sender_id = near.predecessorAccountId()
Expand All @@ -49,12 +49,9 @@ class NftContract {
near.promiseThen(promise, near.currentAccountId(), '_nftResolveTransfer', bytes(JSON.stringify({ sender_id, receiver_id, token_id })), 0, 30000000000000);
}

@call
@call({ privateFunction: true })
_nftResolveTransfer({ sender_id, receiver_id, token_id }) {
near.log(`_nftResolveTransfer called, receiver_id ${receiver_id}, token_id ${token_id}`)
if (near.currentAccountId() == !near.predecessorAccountId()) {
throw Error('Function can be used as a callback only')
}
const isTokenTransfered = JSON.parse(near.promiseResult(0))
near.log(`${token_id} ${isTokenTransfered ? 'was transfered' : 'was NOT transfered'}`)

Expand All @@ -70,7 +67,7 @@ class NftContract {
}
}

@call
@call({})
nftMint({ token_id, token_owner_id, token_metadata }) {
let sender_id = near.predecessorAccountId()
assert(sender_id === this.owner_id, "Unauthorized")
Expand All @@ -81,7 +78,7 @@ class NftContract {
return new Token(token_id, token_owner_id)
}

@view
@view({})
nftToken({ token_id }) {
let owner_id = this.owner_by_id.get(token_id)
if (owner_id === null) {
Expand Down
8 changes: 4 additions & 4 deletions examples/src/parking-lot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ParkingLot {
this.cars = new LookupMap('a');
}

@call
@call({})
addCar({ name, id, color, price, engineHp }: { name: string, id: number, color: string, price: number, engineHp: number }) {
// args can be json arguments only, they cannot be of a JS/TS class like following, unless override NearContract.deserializeArgs method.
// addCar({ name, specs }: { name: string, specs: CarSpecs }) {
Expand All @@ -47,19 +47,19 @@ class ParkingLot {
this.cars.set(name, car)
}

@call
@call({})
removeCar({ name }: { name: string }) {
near.log(`removeCar() called, name: ${name}`)
this.cars.remove(name)
}

@view
@view({})
getCarSpecs({ name }: { name: string }) {
near.log(`getCarSpecs() called, name: ${name}`)
return this.cars.get(name)
}

@view
@view({})
runCar({ name }: { name: string }) {
/* We are getting plain carSpecs object from the storage.
It needs to be converted to the class object in order to execute engine.run() function.*/
Expand Down
8 changes: 4 additions & 4 deletions examples/src/status-message-collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@ class StatusMessage {
this.uniqueValues = new LookupSet('b')
}

@call
@call({})
set_status({ message }) {
let account_id = near.signerAccountId()
near.log(`${account_id} set_status with message ${message}`)
this.records.set(account_id, message)
this.uniqueValues.set(message)
}

@view
@view({})
get_status({ account_id }) {
near.log(`get_status for account_id ${account_id}`)
return this.records.get(account_id)
}

@view
@view({})
has_status({ message }) {
// used for test LookupMap
return this.uniqueValues.contains(message)
}

@view
@view({})
get_all_statuses() {
// used for test UnorderedMap
return this.records.toArray()
Expand Down
Loading

0 comments on commit 5667584

Please sign in to comment.