I am trying to write a client code in typescript for a contract written in vanilla rust. I am unable to serialize an instruction such that it can be deserialized by the program
#[derive(Debug, BorshDeserialize, BorshSerialize)]
pub enum WalletInstruction {
/// Initialize a Personal Savings Wallet
///
/// Passed accounts:
///
/// (1) Wallet account
/// (2) Vault accounts
/// (3) Authority
/// (4) Rent sysvar
/// (5) System program
Initialize,
/// Deposit
///
/// Passed accounts:
///
/// (1) Wallet account
/// (2) Vault accounts
/// (3) Money Source
Deposit { amount: u64 },
/// Withdraw from Wallet
///
/// Passed accounts:
///
/// (1) Wallet account
/// (2) Vault accounts
/// (3) Authority
/// (4) Target Wallet account
Withdraw { amount: u64 },
}
pub fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
mut instruction_data: &[u8],
) -> ProgramResult {
match WalletInstruction::deserialize(&mut instruction_data)? {
WalletInstruction::Initialize => initialize(program_id, accounts),
WalletInstruction::Deposit { amount } => deposit(program_id, accounts, amount),
WalletInstruction::Withdraw { amount } => withdraw(program_id, accounts, amount),
}
}
In my client side, I have this
class Assignable {
constructor(properties) {
Object.keys(properties).map((key) => {
return (this[key] = properties[key]);
});
};
};
class Deposit extends Assignable {
toBuffer() {
return Buffer.from(borsh.serialize(DepositSchema, this));
}
}
const DepositSchema = new Map([
[
Deposit, {
kind: 'struct',
fields: [
['amount', 'u8'],
]
}
]
]);
const DepositInstructionData = new Deposit({
amount: deposit_amount,
})
let ix = new TransactionInstruction({
keys:[
{pubkey: wallet_adddress, isSigner: false, isWritable: true},// Wallet Account
{pubkey: vault_adddress, isSigner: false, isWritable: true},// Vault Account
{pubkey: rich_boi.publicKey, isSigner: true, isWritable: true},// Rich Boi Account
{pubkey: SystemProgram.programId, isSigner: false, isWritable: false}// System Program
],
programId: program.publicKey,
data: DepositInstructionData.toBuffer(),
})
When i execute the transaction, i am getting this error
Error: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: Failed to serialize or deserialize account data: Unknown