basic-5.ts 3.5 KB

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