假设有一个用near-sdk-rs编写、部署的契约,其状态定义为:
#[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 ""
}如何在保留现有状态的情况下做到这一点(类似于进行数据库迁移)?
发布于 2021-04-12 19:52:32
您还可以看到我们创建的一些示例是如何使用版本控制的。
请参阅BerryClub:
#[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,
}
}
}还有其他人,但一旦我找到他们,就不得不回到这里来。
发布于 2021-04-12 19:18:18
到目前为止,我只能建议使用保持原样的旧Token结构部署一个临时协定,并使用NewToken实现NewToken::from_token和一个更改方法:
impl Token {
pub fn migrate(&mut self) {
near_sdk::env::storage_write(
"STATE",
NewToken::from_token(self).try_into_vec().unwrap()
);
}
}迁移状态后,您可以使用NewToken而不是Token来部署合约。
https://stackoverflow.com/questions/67041874
复制相似问题