Skip to content

Instantly share code, notes, and snippets.

@adilcpm
Created January 31, 2024 22:19
Show Gist options
  • Select an option

  • Save adilcpm/0d4ebca9901bb6c0bfd14ea531d5b267 to your computer and use it in GitHub Desktop.

Select an option

Save adilcpm/0d4ebca9901bb6c0bfd14ea531d5b267 to your computer and use it in GitHub Desktop.
Json To TransactionInstruction
const data = await axios.post(
'http://localhost:3001/api/dev/transactions/buySingleListing',
{
nftMint: 'E2Lo6nxNC6bGP1JZHTsce4ebVCn3cE6uDagbmhvUoit',
wallet_address: 'BGQDmkQDrxZjNiUHCsSZYuKP4XJMfoJ6AXtmJh7CaxLH',
},
)
const reconstructedInstructions = reconstructTransactionInstructions(
data.data as JsonTransactionInstruction[],
)
const tx = new Transaction().add(...reconstructedInstructions)
tx.feePayer = buyer
await getConnection().sendTransaction(tx)
export interface JsonAccountMeta {
pubkey: string
isSigner: boolean
isWritable: boolean
}
export interface JsonTransactionInstruction {
programId: string
keys: JsonAccountMeta[]
data: string
}
export const reconstructTransactionInstruction = (
jsonInstruction: JsonTransactionInstruction,
): TransactionInstruction => {
const programId = new PublicKey(jsonInstruction.programId)
const data = Buffer.from(jsonInstruction.data, 'base64')
const keys: AccountMeta[] = jsonInstruction.keys.map(key => ({
pubkey: new PublicKey(key.pubkey),
isSigner: key.isSigner,
isWritable: key.isWritable,
}))
return new TransactionInstruction({
programId,
keys,
data,
})
}
// For handling multiple instructions
export const reconstructTransactionInstructions = (
jsonInstructions: JsonTransactionInstruction[],
): TransactionInstruction[] => {
return jsonInstructions.map(reconstructTransactionInstruction)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment