我正在尝试将以太DeFi合同移植到Solana的Rust程序中...我已经学会了如何在程序的帐户数据中保存结构或数组,但仍然不知道如何将字符串中的HashMap中的数量保存到程序的帐户数据中……然后如何读取此HashMap的值,如检查每个地址的赌注金额。请帮帮忙。谢谢!
我的Solana Rust程序:
pub fn get_init_hashmap() -> HashMap<&'static str, u64> {
let mut staked_amount: HashMap<&str, u64> = HashMap::new();
staked_amount.insert("9Ao3CgcFg3RB2...", 0);
staked_amount.insert("8Uyuz5PUS47GB...", 0);
staked_amount.insert("CRURHng6s7DGR...", 0);
staked_amount
}
pub fn process_instruction(...) -> ProgramResult {
msg!("about to decode account data");
let acct_data_decoded = match HashMap::try_from_slice(&account.data.borrow_mut()) {
Ok(data) => data,//to be of type `HashMap`
Err(err) => {
if err.kind() == InvalidData {
msg!("InvalidData so initializing account data");
get_init_hashmap()
} else {
panic!("Unknown error decoding account data {:?}", err)
}
}
};
msg!("acct_data_decoded: {:?}", acct_data_decoded);发布于 2021-07-22 07:23:50
索拉纳不会公开这样的HashMap。在可靠性方面,通常会有一个顶级HashMap来跟踪地址到用户值。
在Solana上,替代它的一种常见模式是使用PDA(程序派生地址)。您可以Hash user SOL wallet以确保PDA的唯一性,然后使用离线曲柄对其进行迭代。
发布于 2021-07-22 04:59:12
Solana dev对Discord的支持回答道: HashMap目前不能在链上工作,所以你必须使用BTreeMap。
至于实际保存,您可以遍历键-值对并序列化每个键-值对。
一般来说,我们建议使用多个帐户,并为每个帐户使用程序派生的地址:https://docs.solana.com/developing/programming-model/calling-between-programs#program-derived-addresses
https://stackoverflow.com/questions/68454062
复制相似问题