-1

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>
}

The problem is described in more detail here.

  • if in the example with the counter we use a program account that has a data field and the program works, then how to initialize this field in accounts not created by the program? – Nikon Dolgushin Jul 21 '22 at 09:41

1 Answers1

0

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: "" }] })
Frank C.
  • 7,758
  • 4
  • 35
  • 45