I want to interact with a program and discover this website: https://bettercallsol.dev/
Basically it allows you to serialize the data using both Borsh & Buffer layout.
Despite they are two different methods, but the input and output of serialization looks the same.
I just wonder what is the difference between them? Does program has to deal with them differently? Or is it just a preference and not much difference?
Because I have tested locally and it seems like both of them works on the same program.
Here is my program state source:
use borsh::{BorshDeserialize, BorshSerialize};
#[derive(BorshSerialize, BorshDeserialize, Debug, Clone)]
pub struct Counter {
pub count: u64,
pub label: u64,
}
impl Counter {
pub fn new(count: u64, label: u64) -> Self {
Counter {
count,
label
}
}
}
and here is my serialization code:
function createDecrementInstruction(
accounts: Instruction1Accounts,
args: Instruction1Args
): TransactionInstruction {
// option 1 to encode calldata using buffer layout
let calldata1 = encodeData(NATIVE_PROGRAM_INSTRUCTION_LAYOUTS.Instruction1, args)
// option 2 to encode calldata using borsh
const initializeSchema = new Map(
[
[
CounterArgs,
{
kind: "struct",
fields: [
["instruction", "u8"],
]
}
],
]
);
const c = new CounterArgs({
instruction: 1,
});
let calldata2 = Buffer.from(borsh.serialize(initializeSchema, c));
return new TransactionInstruction({
programId: PROGRAM_ID,
keys: [
{
pubkey: accounts.counter,
isSigner: false,
isWritable: true
}
],
data: calldata1
// data: calldata2, // ths works as well
})
}
Thanks!