NEAR: Creating & Upgrading a simple message contract
Build, deploy, and upgrade a NEAR smart contract using near-sdk-rs and NEAR CLI through a Chainstack node. NEAR support is now deprecated on Chainstack.
Chainstack deprecated support for NEAR nodes. This page here is for legacy and in case you may find it useful. Aurora nodes are still supported.
This tutorial guides you through creating a simple message contract, deploying the contract, interacting with it, and upgrading it.In short, you will do the following:
You are now going to upgrade the contract by changing the following:
Change the key-value pair name records to taglines and the corresponding state changing function set_status to set_tagline.
Add the new callable key-value pair bios.
Provide a way to retrieve with the new contract code the original state on the chain set through the records key-value pair. This is done through the migrate function.
Edit the src/lib.rs file to provide the upgraded version of the contract:
You will now deploy the upgraded contract code on the same contract account where the initial contract is running.Since you interacted with the original contract, you have changed the chain state. Deploying an upgraded version of the contract does not purge the changed state. This means that you must have a way to retrieve the changed state through the upgraded contract interface. In our simple message contract, this is done through the migrate function.You will migrate the original state at the upgraded contract deployment.Run:
near view simplemessage.upgradablecontract.testnet get_tagline '{"account_id": "upgradablecontract.testnet"}' --node_url NEAR_ENDPOINT
where:
simplemessage — the name of the contract account that you deployed the upgraded contract to.
upgradablecontract — the name of the account on the NEAR testnet that you used to set the original message.
NEAR_ENDPOINT — your NEAR node HTTPS endpoint.
Note that since this is an upgraded contract, you are retrieving the original message through the new interface: through get_tagline instead of get_status.You can also change the state with the new function that you added to the contract: set_bio.Example:
near call simplemessage.upgradablecontract.testnet set_bio '{"message": "Bye"}' --accountId upgradablecontract.testnet --node_url NEAR_ENDPOINT
And retrieve the new state:
near view simplemessage.upgradablecontract.testnet get_bio '{"account_id": "upgradablecontract.testnet"}' --node_url NEAR_ENDPOINT
This tutorial guided you through the basics of creating a simple NEAR contract, deploying the initial contract on the NEAR network, interacting with the contract, and upgrading the contract.This tutorial uses the testnet, however, the exact same instructions and sequence will work on the mainnet as well.