import { PublicKey , TransactionInstruction } from '@solana/web3.js' ; import { Layout , rustEnum , struct } from '@project-serum/borsh' ; export type IdlInstruction = Create | CreateBuffer | Write | SetBuffer | SetAuthority ; type Create = { } ; type CreateBuffer = { } ; type Write = { } ; type SetBuffer = { } ; type SetAuthority = { } ; const IDL_INSTRUCTION_LAYOUT = rustEnum ( [ struct ( [ ] , 'create' ) , struct ( [ ] , 'createBuffer' ) , struct ( [ ] , 'write' ) , struct ( [ ] , 'setBuffer' ) , struct ( [ ] , 'setAuthority' ) , ] ) ; export function encodeInstruction ( i ) { const buffer = Buffer . alloc ( 1000 ) ; const len = IDL_INSTRUCTION_LAYOUT . encode ( i , buffer ) ; return Buffer . concat ( [ IDL_TAG , buffer . slice ( 0 , len ) ] ) ; } export const IDL_TAG = Buffer . from ( '0a69e9a778bcf440' , 'hex' ) . reverse ( ) ; export async function idlAddress ( programId ) { const base = ( await PublicKey . findProgramAddress ( [ ] , programId ) ) [ 0 ] ; return await PublicKey . createWithSeed ( base , seed ( ) , programId ) ; } export function seed ( ) { return 'anchor:idl' ; } export async function createSetBuffer ( programId , buffer , idlAccount , idlAuthority ) { const data = encodeInstruction ( { setBuffer : { } } ) ; const keys = [ { pubkey : buffer , isWritable : true , isSigner : false , } , { pubkey : idlAccount , isWritable : true , isSigner : false } , { pubkey : idlAuthority , isWritable : false , isSigner : true } , ] ; return new TransactionInstruction ( { keys , programId , data , } ) ; }