basic-5.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import * as anchor from "@coral-xyz/anchor";
  2. import {
  3. TransactionInstruction,
  4. TransactionMessage,
  5. VersionedTransaction,
  6. } from "@solana/web3.js";
  7. import { Basic5 } from "../target/types/basic_5";
  8. describe("basic-5", () => {
  9. const provider = anchor.AnchorProvider.local();
  10. // Configure the client to use the local cluster.
  11. anchor.setProvider(provider);
  12. const program = anchor.workspace.Basic5 as anchor.Program<Basic5>;
  13. const user = provider.wallet.publicKey;
  14. let [actionState] = anchor.web3.PublicKey.findProgramAddressSync(
  15. [Buffer.from("action-state"), user.toBuffer()],
  16. program.programId
  17. );
  18. it("basic-5: Robot actions!", async () => {
  19. // Create instruction: set up the Solana accounts to be used
  20. const createInstruction = await program.methods
  21. .create()
  22. .accounts({
  23. actionState,
  24. user,
  25. systemProgram: anchor.web3.SystemProgram.programId,
  26. })
  27. .instruction();
  28. // Walk instruction: Invoke the Robot to walk
  29. const walkInstruction = await program.methods
  30. .walk()
  31. .accounts({
  32. actionState,
  33. user,
  34. })
  35. .instruction();
  36. // Run instruction: Invoke the Robot to run
  37. const runInstruction = await program.methods
  38. .run()
  39. .accounts({
  40. actionState,
  41. user,
  42. })
  43. .instruction();
  44. // Jump instruction: Invoke the Robot to jump
  45. const jumpInstruction = await program.methods
  46. .jump()
  47. .accounts({
  48. actionState,
  49. user,
  50. })
  51. .instruction();
  52. // Reset instruction: Reset actions of the Robot
  53. const resetInstruction = await program.methods
  54. .reset()
  55. .accounts({
  56. actionState,
  57. user,
  58. })
  59. .instruction();
  60. // Array of instructions
  61. const instructions: TransactionInstruction[] = [
  62. createInstruction,
  63. walkInstruction,
  64. runInstruction,
  65. jumpInstruction,
  66. resetInstruction,
  67. ];
  68. await createAndSendV0Tx(instructions);
  69. });
  70. async function createAndSendV0Tx(txInstructions: TransactionInstruction[]) {
  71. // Step 1 - Fetch the latest blockhash
  72. let latestBlockhash = await provider.connection.getLatestBlockhash(
  73. "confirmed"
  74. );
  75. console.log(
  76. " ✅ - Fetched latest blockhash. Last Valid Height:",
  77. latestBlockhash.lastValidBlockHeight
  78. );
  79. // Step 2 - Generate Transaction Message
  80. const messageV0 = new TransactionMessage({
  81. payerKey: user,
  82. recentBlockhash: latestBlockhash.blockhash,
  83. instructions: txInstructions,
  84. }).compileToV0Message();
  85. console.log(" ✅ - Compiled Transaction Message");
  86. const transaction = new VersionedTransaction(messageV0);
  87. // Step 3 - Sign your transaction with the required `Signers`
  88. provider.wallet.signTransaction(transaction);
  89. console.log(" ✅ - Transaction Signed");
  90. // Step 4 - Send our v0 transaction to the cluster
  91. const txid = await provider.connection.sendTransaction(transaction, {
  92. maxRetries: 5,
  93. });
  94. console.log(" ✅ - Transaction sent to network");
  95. // Step 5 - Confirm Transaction
  96. const confirmation = await provider.connection.confirmTransaction({
  97. signature: txid,
  98. blockhash: latestBlockhash.blockhash,
  99. lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
  100. });
  101. if (confirmation.value.err) {
  102. throw new Error(
  103. ` ❌ - Transaction not confirmed.\nReason: ${confirmation.value.err}`
  104. );
  105. }
  106. console.log("🎉 Transaction Succesfully Confirmed!");
  107. let result = await program.account.actionState.fetch(actionState);
  108. console.log("Robot action state details: ", result);
  109. }
  110. });