-
Notifications
You must be signed in to change notification settings - Fork 71
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
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0598cb9
sdk api used instead of env in src/near-contract
volovyks 4ab7317
stale examples deleted, env usage reduced
volovyks 5d6010c
env usage fix
volovyks 2e0b11c
unit test dependencie isssue fixed
volovyks 2b8701c
low level examples deleted
volovyks ce118c4
Merge branch 'master' into env
volovyks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,24 @@ | ||
import * as near from '../../src/api' | ||
|
||
export function get_num() { | ||
let hasRead = env.jsvm_storage_read("a", 0) | ||
if (hasRead != 0) | ||
env.jsvm_value_return(env.read_register(0)) | ||
else | ||
env.jsvm_value_return("0") | ||
let res = near.jsvmStorageRead("a") | ||
return res ? res : "0" | ||
} | ||
|
||
export function increment() { | ||
let n | ||
let hasRead = env.jsvm_storage_read("a", 0) | ||
if (hasRead != 0) { | ||
n = env.read_register(0) | ||
n = Number(n)+1 | ||
} else { | ||
n = 1 | ||
} | ||
let log_message = "Increased number to " + n | ||
env.jsvm_storage_write("a", String(n), 0) | ||
env.log(log_message) | ||
let n = near.jsvmStorageRead("a") | ||
n = n ? Number(n) + 1 : 1 | ||
near.jsvmStorageWrite("a", String(n)) | ||
near.log("Increased number to " + n) | ||
} | ||
|
||
export function decrement() { | ||
let n | ||
let hasRead = env.jsvm_storage_read("a", 0) | ||
if (hasRead != 0) { | ||
n = env.read_register(0) | ||
n = Number(n)-1 | ||
} else { | ||
n = 1 | ||
} | ||
let log_message = "Decreased number to " + n | ||
env.jsvm_storage_write("a", String(n), 0) | ||
env.log(log_message) | ||
let n = near.jsvmStorageRead("a") | ||
n = n ? Number(n) - 1 : 1 | ||
near.jsvmStorageWrite("a", String(n)) | ||
near.log("Decreased number to " + n) | ||
} | ||
|
||
export function reset() { | ||
env.jsvm_storage_write("a", "0", 0) | ||
near.jsvmStorageWrite("a", "0") | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,18 @@ | ||
import * as near from '../../src/api' | ||
|
||
const MAX_SUPPLY = 10 | ||
const TOTAL_SUPPLY = 'c' | ||
|
||
export function mint_to() { | ||
let tokenId | ||
let hasRead = env.jsvm_storage_read(TOTAL_SUPPLY, 0) | ||
if (hasRead != 0) { | ||
tokenId = Number(env.read_register(0))+1 | ||
} else { | ||
tokenId = 1 | ||
} | ||
let tokenId = near.jsvmStorageRead(TOTAL_SUPPLY) | ||
tokenId = tokenId ? Number(tokenId) + 1 : 1 | ||
if (tokenId > MAX_SUPPLY) { | ||
throw new Error('Maximum token limit reached') | ||
} | ||
env.jsvm_args(0) | ||
let input = env.read_register(0) | ||
let parsedInput = JSON.parse(input) | ||
let owner_id = parsedInput['owner'] | ||
env.jsvm_storage_write(TOTAL_SUPPLY, tokenId.toString(), 0) | ||
env.jsvm_storage_write('token_to_owner_' + tokenId, owner_id, 0) | ||
env.log(`Minted NFT ${tokenId} to ${owner_id}`) | ||
env.jsvm_value_return(tokenId.toString()) | ||
} | ||
|
||
|
||
// What we want, a user friendly high-level API like this one: | ||
// * class based | ||
// * arguments and return are auto converted from env.input() and env.value_return | ||
// * class properties are auto load and save to blockchain state | ||
/* | ||
@NearBindgen | ||
class MyNFT { | ||
constructor(maxSupply) { | ||
this.maxSupply = maxSupply | ||
this.totalSupply = 0 | ||
this.tokenOwner = new LookupMap('token_to_owner_') | ||
} | ||
|
||
mintTo(owner) { | ||
if (this.totalSupply < this.maxSupply) { | ||
this.totalSupply++ | ||
} else { | ||
throw new Error('Maximum token limit reached') | ||
} | ||
let tokenId = this.totalSupply; | ||
this.tokenOwner.insert(owner, tokenId); | ||
env.log(`Minted NFT ${tokenId} to ${ownerId}`) | ||
return tokenId | ||
} | ||
let input = near.jsvmArgs(); | ||
let ownerId = input['owner'] | ||
near.jsvmStorageWrite(TOTAL_SUPPLY, tokenId.toString()) | ||
near.jsvmStorageWrite('token_to_owner_' + tokenId, ownerId) | ||
near.log(`Minted NFT ${tokenId} to ${ownerId}`) | ||
// env.jsvm_value_return(tokenId.toString()) //TODO: Not implemented. Should we add it? | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ailisp I'm not sure what should be done with this function call. The documentation says that it is used together with
env.jsvm_call(...args)
, but we do not have it here.Also, something I have noticed:
In this function we are using
env.read_register(...args)
, notenv.jsvm_value_return()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad on describing the behavior. jsvm_value_return does: 1) store the return in a C static variable, so if this were called by another contract, that contract can retrieve this info. 2) plus the normal host function value_return.
This contract doesn't call other contract so there's no jsvm_call, but this contract could be called by another contract. Therefore, for any JS contract, jsvm_value_return should always be used instead of value_return.
env.jsvm_call
saves the the return (if there were jsvm_value_return in the call) in both a C static variable and the given register:If we just want to return the value to runtime,
jsvm_value_return(value)
would do that. If we want to further process this value, we need to read it from register (e.g. injsvmCall
)