简体   繁体   中英

Solana program. failed: Failed to serialize or deserialize account data: Unknown'

I am getting an error: Failed to serialize or deserialize account data: Unknown' . I'm trying to get data like this: let mut bet_account = BidData::try_from_slice(&bet.data.borrow()[..])?; , where BidData contains the field bids: Vec<Bid> .

#[derive(BorshSerialize, BorshDeserialize, Debug)]
pub struct Bid {
    /// XJUST coins
    pub xjust: u64,
    /// selected side
    pub side: u8,
    /// user key
    pub pubkey: String,
}

#[derive(BorshDeserialize, BorshSerialize, Debug)]
pub struct BidData {
    // list bids
    pub bids: Vec<Bid>
}

Easier to just use what the derive macro for borsh provided:

#[derive(BorshSerialize, BorshDeserialize, Debug)]
pub struct Bid {
    /// XJUST coins
    pub xjust: u64,
    /// selected side
    pub side: u8,
    /// user key
    pub pubkey: String,
}

#[derive(BorshDeserialize, BorshSerialize, Debug, Default)]
pub struct BidData {
    // list bids
    pub bids: Vec<Bid>,
}

#[cfg(test)]
mod test {
    use borsh::BorshSerialize;

    use super::{Bid, BidData};

    #[test]
    fn test_ser() {
        let bid1 = Bid {
            xjust: 100_000,
            side: 2,
            pubkey: "".to_string(),
        };
        let mut bid_data = BidData::default();
        bid_data.bids.push(bid1);
        let ser_bid_data = bid_data.try_to_vec().unwrap();
        println!("{:?}", ser_bid_data);

        let deser_bid_data = BidData::try_from_slice(&ser_bid_data);
        println!("{:?}", deser_bid_data);
     }
}

Produces:

running 1 test
[1, 0, 0, 0, 160, 134, 1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0]

Ok(BidData { bids: [Bid { xjust: 100000, side: 2, pubkey: "" }] })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM