Created
January 31, 2024 22:19
-
-
Save adilcpm/0d4ebca9901bb6c0bfd14ea531d5b267 to your computer and use it in GitHub Desktop.
Json To TransactionInstruction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export interface JsonAccountMeta { | |
| pubkey: string | |
| isSigner: boolean | |
| isWritable: boolean | |
| } | |
| export interface JsonTransactionInstruction { | |
| programId: string | |
| keys: JsonAccountMeta[] | |
| data: string | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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