我正在尝试将以太坊 DeFi 合约移植到 Solana 的 Rust 程序中......我已经了解了如何在程序的帐户数据中保存结构或数组,但仍然不知道如何保存 HashMap<address in string, amount in u64>进入程序的帐户数据...然后如何读取此 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);