-
I’ve got a couple question regarding the JS SDK. I’ve been trying to get a contract running for so long and I’m reaching a point where I think i’m really stuck. I put a TLDR at the bottom of this message before my contract since I know it’s a bit long. I’ve got a very simple contract (I think) and I can’t seem to get anything to work if I’m passing in values into a constructor. I’ve attached my contract at the bottom. When deploying, I’ve tried almost every single combination of trying to pass in an initial total supply of 10. What seems the most correct (since it mirrors the CLI) is this command to call the initialization function: near call $JSVM_ACCOUNT call_js_contract --accountId $JSPROJ --args $(node encode_call.js $JSPROJ init '{"totalSupply": 10}') --base64 --amount 1 But this throws an interesting error: near call $JSVM_ACCOUNT call_js_contract --accountId $JSPROJ --args $(node encode_call.js $JSPROJ init '') --base64 --amount 1 which returned:
This however sets the total supply to null and breaks the contract. The workaround I’m using for this is to get rid of the parameter being passed into the constructor and just hard set What was confusing, however, was when I called the near call $JSVM_ACCOUNT call_js_contract --accountId $JSPROJ --args $(node encode_call.js $JSPROJ getTotal '') --base64 returns:
I then wondered if it was resetting the totalSupply everytime I ran the getTotal function. I tested this by attempting to increment the totalSupply and then calling the getTotal function again. I ran into the same problem of not being able to pass anything into the increment function but instead of receiving the value is not iterable error, it simply thinks I passed in null: near call $JSVM_ACCOUNT call_js_contract --accountId $JSPROJ --args $(node encode_call.js $JSPROJ increaseTotal '{"n": 10}') --base64 --amount 1 which returns:
After checking the current totalSupply using getTotal, it returned null: near call $JSVM_ACCOUNT call_js_contract --accountId $JSPROJ --args $(node encode_call.js $JSPROJ getTotal '') --base64 returns:
TLDR:
import {NearContract, NearBindgen, call, view} from '../../sdk'
// import { init } from './build/project.js';
@NearBindgen
class MyContract extends NearContract {
constructor(totalSupply) {
env.log("Constructor called!")
super()
env.log("Super Called!")
this.totalSupply = totalSupply
env.log("Total supply set")
}
@call
increaseTotal(n) {
env.log("Increase total called with: ", n)
this.totalSupply += n
env.log(this.totalSupply)
return this.totalSupply
}
@view
getTotal() {
env.log("Get total called!")
return this.totalSupply
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
From @bucanero: about the super(), as many OOP languages do, it’s used to call the parent class’ constructor. Example: That’s the general theory, but I guess you need it in your smart contract as some initialization from the NearContract class is required. |
Beta Was this translation helpful? Give feedback.
-
From @ailisp: In response to "how do I pass in values into a function? and also the error about passing value to constructor": near call $JSVM_ACCOUNT call_js_contract --accountId $JSPROJ --args $(node encode_call.js $JSPROJ init '[10]') --base64 --amount 1 If there were more than one arguments, for example: (totalSupply, name) , pass it as: [10, "alice"] In response to "how come my getTotal()function runs through the constructor everytime it’s called?": Your observation is right. This is an intentional designed code generation to adapt JS's OOP model to NEAR's contract function call lifecycle. And it's the same design as near-sdk-rs's near_bindgen macro. In JS, there're classes and objects. In a smart contract, the entry point can only be a function. When you call
In response to "how do I invoke a view method without signing a transaction?": Call view_js_contract method. This is currently not possible with near-cli due to this: near/near-cli#953, but it's doable with near-api-js, see examples/project/near-workspaces/tests/project.ava.ts for an example In response to "How is state cleanup handled? If I wanna redeploy a contract to an account, I can’t simply delete the account and recreate it because the state is stored on the jsvm contract correct?": You will need to write a js contract, do a couple calls to jsvm_storage_delete to clear state |
Beta Was this translation helpful? Give feedback.
-
@BenKurrek this discussion looks mostly stale. We have new |
Beta Was this translation helpful? Give feedback.
From @ailisp:
In response to "how do I pass in values into a function? and also the error about passing value to constructor":
You need to pass argument as array:
If there were more than one arguments, for example: (totalSupply, name) , pass it as: [10, "alice"]
In response to "how come my getTotal()function runs through the constructor everytime it’s called?":
Your observation is right. This is an intentional designed code generation to adapt JS's OOP model to NEAR's contract function call lifecycle. And it's the same design as near-sdk-rs's near_bindgen macro.