I am the maintainer of @dao-xyz/borsh which is an alternative borsh library that simplifies the we you serialize and deserialize with the borsh spec. I could see that this would significantly simplify the borsh examples in the cookbook. You can usually get away with half amount of encoding code with this lib, for example
In https://solanacookbook.com/guides/serialization.html#how-to-serialize-instruction-data-on-the-client
import { serialize, deserialize, deserializeUnchecked } from "borsh";
// Get Solana
import {
Keypair,
Connection,
PublicKey,
Transaction,
TransactionInstruction,
sendAndConfirmTransaction,
} from "@solana/web3.js";
// Flexible class that takes properties and imbues them
// to the object instance
class Assignable {
constructor(properties) {
Object.keys(properties).map((key) => {
return (this[key] = properties[key]);
});
}
}
// Our instruction payload vocabulary
class Payload extends Assignable {}
// Borsh needs a schema describing the payload
const payloadSchema = new Map([
[
Payload,
{
kind: "struct",
fields: [
["id", "u8"],
["key", "string"],
["value", "string"],
],
},
],
]);
// Instruction variant indexes
enum InstructionVariant {
InitializeAccount = 0,
MintKeypair,
TransferKeypair,
BurnKeypair,
}
/**
* Mint a key value pair to account
* @param {Connection} connection - Solana RPC connection
* @param {PublicKey} progId - Sample Program public key
* @param {PublicKey} account - Target program owned account for Mint
* @param {Keypair} wallet - Wallet for signing and payment
* @param {string} mintKey - The key being minted key
* @param {string} mintValue - The value being minted
* @return {Promise<Keypair>} - Keypair
*/
export async function mintKV(
connection: Connection,
progId: PublicKey,
account: PublicKey,
wallet: Keypair,
mintKey: string,
mintValue: string
): Promise<string> {
// Construct the payload
const mint = new Payload({
id: InstructionVariant.MintKeypair,
key: mintKey, // 'ts key'
value: mintValue, // 'ts first value'
});
// Serialize the payload
const mintSerBuf = Buffer.from(serialize(payloadSchema, mint));
....
Could be rewritten as
import { serialize, deserialize, variant } from "@dao-xyz/borsh";
import { Buffer } from "buffer";
// Get Solana
import {
Keypair,
Connection,
PublicKey,
Transaction,
TransactionInstruction,
sendAndConfirmTransaction,
} from "@solana/web3.js";
abstract class ProgramInstruction {}
@variant(0)
class InitializeAccount extends ProgramInstruction {}
@variant(1)
class Mint extends ProgramInstruction
{
@field({type: 'string'})
key: string;
@field({type: 'string'})
value: string
constructor(id: Number, key: string, value: string )
{
super()
this.id = id;
this.key = key;
this.value = value
}
}
// etc.. you can do all the instructions this way
/**
* Mint a key value pair to account
* @param {Connection} connection - Solana RPC connection
* @param {PublicKey} progId - Sample Program public key
* @param {PublicKey} account - Target program owned account for Mint
* @param {Keypair} wallet - Wallet for signing and payment
* @param {string} mintKey - The key being minted key
* @param {string} mintValue - The value being minted
* @return {Promise<Keypair>} - Keypair
*/
export async function mintKV(
connection: Connection,
progId: PublicKey,
account: PublicKey,
wallet: Keypair,
mintKey: string,
mintValue: string
): Promise<string> {
// Construct the payload
const mint = new Mint({
key: mintKey, // 'ts key'
value: mintValue, // 'ts first value'
});
// Serialize the payload
const mintSerBuf = serialize(mint);
The benefit become even more obvious for the example that are later.
I am checking here if there is interest in updating the documentation for this I could perhaps make a PR for the changes
I am the maintainer of @dao-xyz/borsh which is an alternative borsh library that simplifies the we you serialize and deserialize with the borsh spec. I could see that this would significantly simplify the borsh examples in the cookbook. You can usually get away with half amount of encoding code with this lib, for example
In https://solanacookbook.com/guides/serialization.html#how-to-serialize-instruction-data-on-the-client
Could be rewritten as
The benefit become even more obvious for the example that are later.
I am checking here if there is interest in updating the documentation for this I could perhaps make a PR for the changes