basic-5.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const anchor = require("@project-serum/anchor");
  2. const assert = require("assert");
  3. describe("basic-5", () => {
  4. // Configure the client to use the local cluster.
  5. anchor.setProvider(anchor.Provider.env());
  6. const program = anchor.workspace.Basic5;
  7. const mint = anchor.web3.Keypair.generate();
  8. // Setup. Not important for the point of the example.
  9. it("Sets up the test", async () => {
  10. // Create the mint account.
  11. await program.rpc.createMint({
  12. accounts: {
  13. mint: mint.publicKey,
  14. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  15. },
  16. instructions: [await program.account.mint.createInstruction(mint)],
  17. signers: [mint],
  18. });
  19. });
  20. it("Creates an associated token account", async () => {
  21. // #region test
  22. // Calculate the associated token address.
  23. const authority = program.provider.wallet.publicKey;
  24. const associatedToken = await program.account.token.associatedAddress(
  25. authority,
  26. mint.publicKey
  27. );
  28. // Execute the transaction to create the associated token account.
  29. await program.rpc.createToken({
  30. accounts: {
  31. token: associatedToken,
  32. authority,
  33. mint: mint.publicKey,
  34. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  35. systemProgram: anchor.web3.SystemProgram.programId,
  36. },
  37. });
  38. // Fetch the new associated account.
  39. const account = await program.account.token.associated(
  40. authority,
  41. mint.publicKey
  42. );
  43. // #endregion test
  44. // Check it was created correctly.
  45. assert.ok(account.amount === 0);
  46. assert.ok(account.authority.equals(authority));
  47. assert.ok(account.mint.equals(mint.publicKey));
  48. });
  49. });