📢 Gate Square Exclusive: #WXTM Creative Contest# Is Now Live!
Celebrate CandyDrop Round 59 featuring MinoTari (WXTM) — compete for a 70,000 WXTM prize pool!
🎯 About MinoTari (WXTM)
Tari is a Rust-based blockchain protocol centered around digital assets.
It empowers creators to build new types of digital experiences and narratives.
With Tari, digitally scarce assets—like collectibles or in-game items—unlock new business opportunities for creators.
🎨 Event Period:
Aug 7, 2025, 09:00 – Aug 12, 2025, 16:00 (UTC)
📌 How to Participate:
Post original content on Gate Square related to WXTM or its
Rust smart contracts upgrade tips: Safe update methods for NEAR contracts
Rust smart contracts upgrade techniques
Smart contracts, as a form of program code, inevitably have flaws and vulnerabilities. Even after extensive testing and auditing, security issues may still arise. Once a contract vulnerability is exploited by an attacker, it can lead to severe consequences such as the loss of user assets. Therefore, contract upgrades are crucial for fixing vulnerabilities and adding new features. This article will introduce the upgrade methods for Rust smart contracts.
NEAR Contract Upgrade Common Methods
Taking the StatusMessage project as an example, this article introduces common upgrade methods for NEAR smart contracts:
rust #[near_bindgen] #[derive(BorshDeserialize, BorshSerialize)] pub struct StatusMessage { records: LookupMap<string, string="">, }
impl Default for StatusMessage { Self { Self { records: LookupMap::new(b'r'.to_vec)((, } } }
#[near_bindgen] impl StatusMessage { pub fn set_status)&mut self, message: String) { let account_id = env::signer_account_id(); self.records.insert(&account_id, &message); }
Option<string> { return self.records.get(&account_id); } }
( contract data structure not modified
If only new functions are added and there is no modification to the data structure, you can directly use the near deploy command to redeploy the new code. The original data can be read normally.
) The contract data structure has been modified.
If the data structure is modified, redeploying directly will cause the new and old data structures to be incompatible, making it impossible to read data normally. In this case, the Migrate method needs to be used for the upgrade:
rust #( #[init)ignore_state###] Self { let old_state: OldStatusMessage = env::state_read###[private].expect('failed'); Self { taglines: old_state.records, bios: LookupMap::new(b'b'.to_vec)(), } }
near deploy
--wasmFile target/wasm32-unknown-unknown/release/status_message.wasm
--initFunction 'migrate'
--initArgs '{}' \ --accountId statusmessage.example.testnet
This allows for the migration of old data into the new data structure.
Contract Upgrade Security Considerations
Implement access control, allowing only developers or DAO to upgrade the smart contracts.
It is recommended to set the contract owner to DAO, managing it collaboratively through proposals and voting.
Add #[init(ignore_state(] before the migrate function.
Delete the migrate function after migration is complete to ensure it is called only once.
The new data structure is initialized during migration.
By using the above methods, the upgrade of Rust smart contracts can be completed safely and efficiently.
![])https://img-cdn.gateio.im/webp-social/moments-af3fe22c1999da5db0e2853b8a271276.webp)</string,>