You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
import{serialize,deserialize,deserializeUnchecked}from"borsh";// Get Solanaimport{Keypair,Connection,PublicKey,Transaction,TransactionInstruction,sendAndConfirmTransaction,}from"@solana/web3.js";// Flexible class that takes properties and imbues them// to the object instanceclassAssignable{constructor(properties){Object.keys(properties).map((key)=>{return(this[key]=properties[key]);});}}// Our instruction payload vocabularyclassPayloadextendsAssignable{}// Borsh needs a schema describing the payloadconstpayloadSchema=newMap([[Payload,{kind: "struct",fields: [["id","u8"],["key","string"],["value","string"],],},],]);// Instruction variant indexesenumInstructionVariant{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 */exportasyncfunctionmintKV(connection: Connection,progId: PublicKey,account: PublicKey,wallet: Keypair,mintKey: string,mintValue: string): Promise<string>{// Construct the payloadconstmint=newPayload({id: InstructionVariant.MintKeypair,key: mintKey,// 'ts key'value: mintValue,// 'ts first value'});// Serialize the payloadconstmintSerBuf=Buffer.from(serialize(payloadSchema,mint));
....
Could be rewritten as
import{serialize,deserialize,variant}from"@dao-xyz/borsh";import{Buffer}from"buffer";// Get Solanaimport{Keypair,Connection,PublicKey,Transaction,TransactionInstruction,sendAndConfirmTransaction,}from"@solana/web3.js";abstractclassProgramInstruction{}
@variant(0)classInitializeAccountextendsProgramInstruction{}
@variant(1)classMintextendsProgramInstruction{
@field({type: 'string'})key: string;
@field({type: 'string'})value: stringconstructor(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 */exportasyncfunctionmintKV(connection: Connection,progId: PublicKey,account: PublicKey,wallet: Keypair,mintKey: string,mintValue: string): Promise<string>{// Construct the payloadconstmint=newMint({key: mintKey,// 'ts key'value: mintValue,// 'ts first value'});// Serialize the payloadconstmintSerBuf=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
The text was updated successfully, but these errors were encountered:
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
The text was updated successfully, but these errors were encountered: