cashiers-check.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const anchor = require("@project-serum/anchor");
  2. const serumCmn = require("@project-serum/common");
  3. const TokenInstructions = require("@project-serum/serum").TokenInstructions;
  4. const assert = require("assert");
  5. describe("cashiers-check", () => {
  6. // Configure the client to use the local cluster.
  7. anchor.setProvider(anchor.Provider.env());
  8. const program = anchor.workspace.CashiersCheck;
  9. let mint = null;
  10. let god = null;
  11. let receiver = null;
  12. it("Sets up initial test state", async () => {
  13. const [_mint, _god] = await serumCmn.createMintAndVault(
  14. program.provider,
  15. new anchor.BN(1000000)
  16. );
  17. mint = _mint;
  18. god = _god;
  19. receiver = await serumCmn.createTokenAccount(
  20. program.provider,
  21. mint,
  22. program.provider.wallet.publicKey
  23. );
  24. });
  25. const check = anchor.web3.Keypair.generate();
  26. const vault = anchor.web3.Keypair.generate();
  27. let checkSigner = null;
  28. it("Creates a check!", async () => {
  29. let [_checkSigner, nonce] = await anchor.web3.PublicKey.findProgramAddress(
  30. [check.publicKey.toBuffer()],
  31. program.programId
  32. );
  33. checkSigner = _checkSigner;
  34. await program.rpc.createCheck(new anchor.BN(100), "Hello world", nonce, {
  35. accounts: {
  36. check: check.publicKey,
  37. vault: vault.publicKey,
  38. checkSigner,
  39. from: god,
  40. to: receiver,
  41. owner: program.provider.wallet.publicKey,
  42. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  43. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  44. },
  45. signers: [check, vault],
  46. instructions: [
  47. await program.account.check.createInstruction(check, 300),
  48. ...(await serumCmn.createTokenAccountInstrs(
  49. program.provider,
  50. vault.publicKey,
  51. mint,
  52. checkSigner
  53. )),
  54. ],
  55. });
  56. const checkAccount = await program.account.check.fetch(check.publicKey);
  57. assert.ok(checkAccount.from.equals(god));
  58. assert.ok(checkAccount.to.equals(receiver));
  59. assert.ok(checkAccount.amount.eq(new anchor.BN(100)));
  60. assert.ok(checkAccount.memo === "Hello world");
  61. assert.ok(checkAccount.vault.equals(vault.publicKey));
  62. assert.ok(checkAccount.nonce === nonce);
  63. assert.ok(checkAccount.burned === false);
  64. let vaultAccount = await serumCmn.getTokenAccount(
  65. program.provider,
  66. checkAccount.vault
  67. );
  68. assert.ok(vaultAccount.amount.eq(new anchor.BN(100)));
  69. });
  70. it("Cashes a check", async () => {
  71. await program.rpc.cashCheck({
  72. accounts: {
  73. check: check.publicKey,
  74. vault: vault.publicKey,
  75. checkSigner: checkSigner,
  76. to: receiver,
  77. owner: program.provider.wallet.publicKey,
  78. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  79. },
  80. });
  81. const checkAccount = await program.account.check.fetch(check.publicKey);
  82. assert.ok(checkAccount.burned === true);
  83. let vaultAccount = await serumCmn.getTokenAccount(
  84. program.provider,
  85. checkAccount.vault
  86. );
  87. assert.ok(vaultAccount.amount.eq(new anchor.BN(0)));
  88. let receiverAccount = await serumCmn.getTokenAccount(
  89. program.provider,
  90. receiver
  91. );
  92. assert.ok(receiverAccount.amount.eq(new anchor.BN(100)));
  93. });
  94. });