account.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { AccountCommand } from "../target/types/account_command";
  4. import { assert } from "chai";
  5. import { execSync } from "child_process";
  6. import { sleep } from "@project-serum/common";
  7. describe("Test CLI account commands", () => {
  8. // Configure the client to use the local cluster.
  9. const provider = anchor.AnchorProvider.env();
  10. anchor.setProvider(provider);
  11. const program = anchor.workspace.AccountCommand as Program<AccountCommand>;
  12. it("Can fetch and deserialize account using the account command", async () => {
  13. const myAccount = anchor.web3.Keypair.generate();
  14. const balance = -2.5;
  15. const amount = 108;
  16. const memo = "account test";
  17. const values = [1, 2, 3, 1000];
  18. await program.methods
  19. .initialize(
  20. balance,
  21. new anchor.BN(amount),
  22. memo,
  23. values.map((x) => new anchor.BN(x))
  24. )
  25. .accounts({
  26. myAccount: myAccount.publicKey,
  27. user: provider.wallet.publicKey,
  28. systemProgram: anchor.web3.SystemProgram.programId,
  29. })
  30. .signers([myAccount])
  31. .rpc();
  32. let output: any = {};
  33. for (let tries = 0; tries < 20; tries++) {
  34. try {
  35. output = JSON.parse(
  36. execSync(
  37. `anchor account account_command.MyAccount ${myAccount.publicKey}`,
  38. { stdio: "pipe" }
  39. ).toString()
  40. );
  41. break;
  42. } catch (e) {
  43. if (!e.stderr.toString().startsWith("Error: AccountNotFound")) {
  44. throw e;
  45. }
  46. }
  47. await sleep(5000);
  48. }
  49. assert(output.balance == balance, "Balance deserialized incorrectly");
  50. assert(
  51. output.delegatePubkey == provider.wallet.publicKey,
  52. "delegatePubkey deserialized incorrectly"
  53. );
  54. assert(
  55. output.sub.state.Confirmed.amount === amount,
  56. "Amount deserialized incorrectly"
  57. );
  58. assert(
  59. output.sub.state.Confirmed.memo === memo,
  60. "Memo deserialized incorrectly"
  61. );
  62. for (let i = 0; i < values.length; i++) {
  63. assert(
  64. output.sub.values[i] == values[i],
  65. "Values deserialized incorrectly"
  66. );
  67. }
  68. });
  69. });