nearprotocol - NearProtocol中,如何迁移合约状态

标签 nearprotocol near-sdk-rs

假设有一个用 Near-sdk-rs 编写的契约(Contract),已部署,状态定义为:

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct NFT {
    pub tokens: UnorderedMap<TokenId, Token>,
}

#[derive(BorshDeserialize, BorshSerialize)]
pub struct Token {
   pub owner: AccountId
}
现在这个合约有一些使用,结果是tokens的一些记录。存储在链上。
然后我想通过向 Token 添加一个字段来更新此合​​同。 :
pub struct Token {
   pub owner: AccountId
   pub name: String // For existing ones, this will be set to ""
}
如何在保留现有状态的情况下执行此操作(类似于执行数据库迁移)?

最佳答案

您还可以看到我们创建的一些示例如何使用版本控制。
见浆果俱乐部:

#[derive(BorshDeserialize, BorshSerialize)]
pub struct AccountVersionAvocado {
    pub account_id: AccountId,
    pub account_index: AccountIndex,
    pub balance: u128,
    pub num_pixels: u32,
    pub claim_timestamp: u64,
}

impl From<AccountVersionAvocado> for Account {
    fn from(account: AccountVersionAvocado) -> Self {
        Self {
            account_id: account.account_id,
            account_index: account.account_index,
            balances: vec![account.balance, 0],
            num_pixels: account.num_pixels,
            claim_timestamp: account.claim_timestamp,
            farming_preference: Berry::Avocado,
        }
    }
}
https://github.com/evgenykuzyakov/berryclub/blob/ad2b37045b14fa72181ab92831fb741a7c40234b/contract-rs/pixel-board/src/account.rs#L19-L39
还有其他的,但一旦我找到它们就必须回到这里

关于nearprotocol - NearProtocol中,如何迁移合约状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67041874/

相关文章:

nearprotocol - 如何让系统将余额转至近地址

nearprotocol - 目前近区 block 链的规模和预期规模是多少?

rust - NEAR-sdk #[near_bindgen] 与其他宏冲突

rust - 如何在以 WASM 为目标的 near-sdk Rust 代码中链接 WASM 二进制文件

javascript - 使用近库创建新的 NEAR 帐户 - JsonRpcProvider 问题

nearprotocol - 在 near_sdk_rust 中对 UnorderedSet 进行分页

nearprotocol - 有没有办法检查 NEAR 协议(protocol)使用的 RocksDB 实例的内容?