假设有一个用 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/