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

Env usage localized in api.js #73

Merged
merged 6 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions examples/cross-contract-call/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ class OnCall extends NearContract {

@call
set_person_on_call(accountId) {
env.log(`Trying to set ${accountId} on-call`)
near.log(`Trying to set ${accountId} on-call`)
const status = near.jsvmCall('status-message.test.near', 'get_status', [accountId])
env.log(`${accountId} status is ${status}`)
near.log(`${accountId} status is ${status}`)
if (status === 'AVAILABLE') {
this.personOnCall = accountId
env.log(`${accountId} set on-call`)
near.log(`${accountId} set on-call`)
} else {
env.log(`${accountId} can not be set on-call`)
near.log(`${accountId} can not be set on-call`)
}
}

@view
person_on_call() {
env.log(`Returning person on-call: ${this.personOnCall}`)
near.log(`Returning person on-call: ${this.personOnCall}`)
return this.personOnCall
}
}
3 changes: 0 additions & 3 deletions examples/low-level/bignum.js

This file was deleted.

39 changes: 0 additions & 39 deletions examples/low-level/counter.js

This file was deleted.

3 changes: 0 additions & 3 deletions examples/low-level/debug_log.js

This file was deleted.

9 changes: 0 additions & 9 deletions examples/low-level/enclave_cross_contract.js

This file was deleted.

45 changes: 0 additions & 45 deletions examples/low-level/enclave_storage.js

This file was deleted.

17 changes: 0 additions & 17 deletions examples/low-level/hello_enclave.js

This file was deleted.

3 changes: 0 additions & 3 deletions examples/low-level/hello_near.js

This file was deleted.

51 changes: 0 additions & 51 deletions examples/low-level/nft.js

This file was deleted.

4 changes: 2 additions & 2 deletions examples/status-message/src/status-message-collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ class StatusMessage extends NearContract {
@call
set_status(message) {
let account_id = near.signerAccountId()
env.log(`${account_id} set_status with message ${message}`)
near.log(`${account_id} set_status with message ${message}`)
this.records.set(account_id, message)
this.uniqueValues.set(message)
}

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

Expand Down
4 changes: 2 additions & 2 deletions examples/status-message/src/status-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ class StatusMessage extends NearContract {
@call
set_status(message) {
let account_id = near.signerAccountId()
env.log(`${account_id} set_status with message ${message}`)
near.log(`${account_id} set_status with message ${message}`)
this.records[account_id] = message
}

@view
get_status(account_id) {
env.log(`get_status for account_id ${account_id}`)
near.log(`get_status for account_id ${account_id}`)
return this.records[account_id] || null
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/template/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {NearContract, NearBindgen, call, view} from 'near-sdk-js'
import {NearContract, NearBindgen, near, call, view} from 'near-sdk-js'
import {isUndefined} from 'lodash-es'

@NearBindgen
Expand All @@ -11,7 +11,7 @@ class Counter extends NearContract {
@call
increase(n=1) {
this.count += n
env.log(`Counter increased to ${this.count}`)
near.log(`Counter increased to ${this.count}`)
}

@call
Expand All @@ -23,7 +23,7 @@ class Counter extends NearContract {
} else {
this.count -= n
}
env.log(`Counter decreased to ${this.count}`)
near.log(`Counter decreased to ${this.count}`)
}

@view
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
],
"license": "(MIT AND Apache-2.0)",
"scripts": {
"test": "cd tests && yarn && yarn build && yarn test && cd .."
"test": "cd tests && yarn rebuild && yarn test && cd .."
},
"bin": {
"near-sdk": "cli/cli.js"
Expand Down
4 changes: 4 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const U64_MAX = 2n**64n - 1n
const EVICTED_REGISTER = U64_MAX - 1n

export function log(message) {
env.log(message)
}

export function signerAccountId() {
env.signer_account_id(0)
return env.read_register(0)
Expand Down
15 changes: 8 additions & 7 deletions src/near-contract.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import * as near from './api'

export class NearContract {
deserialize() {
let hasRead = env.jsvm_storage_read('STATE', 0)
if (hasRead != 0) {
let state = env.read_register(0)
let state = near.jsvmStorageRead('STATE');
if (state) {
Object.assign(this, JSON.parse(state))
} else
} else {
throw new Error('Contract state is empty')
}
}

serialize() {
env.jsvm_storage_write('STATE', JSON.stringify(this), 0)
near.jsvmStorageWrite('STATE', JSON.stringify(this));
}

static deserializeArgs() {
env.jsvm_args(0)
let args = env.read_register(0)
let args = near.jsvmArgs();
return JSON.parse(args || '[]')
}

Expand Down
1 change: 1 addition & 0 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"type": "module",
"scripts": {
"build": "./build.sh",
"rebuild": "rm -rf node_modules && rm -rf build && yarn && yarn build",
"test": "ava"
},
"author": "Near Inc <hello@nearprotocol.com>",
Expand Down
Loading