I am trying to write data to an account that is the account of the created bet. My smart contract should send reports from the user's account to the bet account. then write down some data (the amount in lamports, the selected party, the public key of the user who made the bet).
I created accounts
solana-keygen new -o bet.json
- bet account,
solana-keygen new -o user1.json
- user account.
client and smart contract code
the error indicates that it is not possible to deserialize bet.data. After I checked the betting account in curl, I did not find the data section. How to initialize it?
the structure that I want to write to the account and complete it, like an array
#[derive(BorshSerialize, BorshDeserialize, Debug)]
pub struct Bid {
/// XJUST lamports
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>
}
I am sending the following instructions from the client
//reading accounts
const BET_KEY = readKeypairFromPath(__dirname + '/../localnet/bet.json')
const USER1_KEY = readKeypairFromPath(__dirname + '/../localnet/user1.json')
//...
//createBid(u64, u8) in rust
let createSetValInstruction = (): Buffer => {
const layout = BufferLayout.struct([
BufferLayout.u8('instruction'),
BufferLayout.ns64('xjust'),
BufferLayout.u8('side')
])
console.log(layout.span);
const data = Buffer.alloc(layout.span)
layout.encode({
xjust: new BN("100000000"),
side: "1",
instruction: 2,
}, data)
return data
}
//...
const ti = new TransactionInstruction({
keys: [
{ pubkey: USER1_KEY.publicKey, isSigner: true, isWritable: true },
{ pubkey: BET_KEY.publicKey, isSigner: false, isWritable: true },
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
],
programId: PROGRAM_ID.publicKey,
data: createSetValInstruction(),
})
const res = await sendAndConfirmTransaction(connection, new Transaction().add(ti), [USER1_KEY])
instruction processing. There's an error going on here
HelloInstruction::CreateBid(xjust, side) => {
msg!("value: {:?}", xjust);
msg!("side: {:?}", side);
let mut bet_account = BidData::try_from_slice(&bet.data.borrow())?;
let bid = Bid {
side: side,
xjust: xjust,
pubkey: user.key.to_string()
};
msg!("bid: {:?}", bid);
bet_account.bids.push(bid);
bet_account.serialize(&mut &mut bet.data.borrow_mut()[..])?;
invoke(
&system_instruction::transfer(user.key, bet.key, xjust),
&[user.clone(), bet.clone()],
)?;
}
I guess that I created the account incorrectly or did not complete some transaction in order to be able to store data in the account. if so, please tell me.
I get an error failed: Failed to serialize or deserialize account data: Unknown'
, when executing this code:
let mut bet_account = BidData::try_from_slice(&bet.data.borrow())?;