custom-coder.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import * as anchor from "@project-serum/anchor";
  2. import { Spl } from "@project-serum/anchor";
  3. import * as assert from "assert";
  4. import BN from "bn.js";
  5. import { Keypair, SYSVAR_RENT_PUBKEY } from "@solana/web3.js";
  6. describe("custom-coder", () => {
  7. // Configure the client to use the local cluster.
  8. anchor.setProvider(anchor.Provider.env());
  9. // Client.
  10. const program = Spl.token();
  11. // Constants.
  12. const mintKeypair = Keypair.generate();
  13. const aliceTokenKeypair = Keypair.generate();
  14. const bobTokenKeypair = Keypair.generate();
  15. const rent = SYSVAR_RENT_PUBKEY;
  16. it("Creates a mint", async () => {
  17. await program.rpc.initializeMint(
  18. 6,
  19. program.provider.wallet.publicKey,
  20. null,
  21. {
  22. accounts: {
  23. mint: mintKeypair.publicKey,
  24. rent,
  25. },
  26. signers: [mintKeypair],
  27. preInstructions: [
  28. await program.account.mint.createInstruction(mintKeypair),
  29. ],
  30. }
  31. );
  32. const mintAccount = await program.account.mint.fetch(mintKeypair.publicKey);
  33. assert.ok(
  34. mintAccount.mintAuthority.equals(program.provider.wallet.publicKey)
  35. );
  36. assert.ok(mintAccount.freezeAuthority === null);
  37. assert.ok(mintAccount.decimals === 6);
  38. assert.ok(mintAccount.isInitialized);
  39. assert.ok(mintAccount.supply.toNumber() === 0);
  40. });
  41. it("Creates a token account for alice", async () => {
  42. await program.rpc.initializeAccount({
  43. accounts: {
  44. account: aliceTokenKeypair.publicKey,
  45. mint: mintKeypair.publicKey,
  46. authority: program.provider.wallet.publicKey,
  47. rent,
  48. },
  49. signers: [aliceTokenKeypair],
  50. preInstructions: [
  51. await program.account.token.createInstruction(aliceTokenKeypair),
  52. ],
  53. });
  54. const token = await program.account.token.fetch(
  55. aliceTokenKeypair.publicKey
  56. );
  57. assert.ok(token.authority.equals(program.provider.wallet.publicKey));
  58. assert.ok(token.mint.equals(mintKeypair.publicKey));
  59. assert.ok(token.amount.toNumber() === 0);
  60. assert.ok(token.delegate === null);
  61. assert.ok(token.state === 1);
  62. assert.ok(token.isNative === null);
  63. assert.ok(token.delegatedAmount.toNumber() === 0);
  64. assert.ok(token.closeAuthority === null);
  65. });
  66. it("Mints a token to alice", async () => {
  67. await program.rpc.mintTo(new BN(2), {
  68. accounts: {
  69. mint: mintKeypair.publicKey,
  70. to: aliceTokenKeypair.publicKey,
  71. authority: program.provider.wallet.publicKey,
  72. },
  73. });
  74. const token = await program.account.token.fetch(
  75. aliceTokenKeypair.publicKey
  76. );
  77. const mint = await program.account.mint.fetch(mintKeypair.publicKey);
  78. assert.ok(token.amount.toNumber() === 2);
  79. assert.ok(mint.supply.toNumber() === 2);
  80. });
  81. it("Creates a token for bob", async () => {
  82. await program.rpc.initializeAccount({
  83. accounts: {
  84. account: bobTokenKeypair.publicKey,
  85. mint: mintKeypair.publicKey,
  86. authority: program.provider.wallet.publicKey,
  87. rent,
  88. },
  89. signers: [bobTokenKeypair],
  90. preInstructions: [
  91. await program.account.token.createInstruction(bobTokenKeypair),
  92. ],
  93. });
  94. });
  95. it("Transfer a token from alice to bob", async () => {
  96. await program.rpc.transfer(new BN(1), {
  97. accounts: {
  98. source: aliceTokenKeypair.publicKey,
  99. destination: bobTokenKeypair.publicKey,
  100. authority: program.provider.wallet.publicKey,
  101. },
  102. });
  103. const aliceToken = await program.account.token.fetch(
  104. aliceTokenKeypair.publicKey
  105. );
  106. const bobToken = await program.account.token.fetch(
  107. bobTokenKeypair.publicKey
  108. );
  109. assert.ok(aliceToken.amount.toNumber() === 1);
  110. assert.ok(bobToken.amount.toNumber() === 1);
  111. });
  112. it("Alice burns a token", async () => {
  113. await program.rpc.burn(new BN(1), {
  114. accounts: {
  115. source: aliceTokenKeypair.publicKey,
  116. mint: mintKeypair.publicKey,
  117. authority: program.provider.wallet.publicKey,
  118. },
  119. });
  120. const aliceToken = await program.account.token.fetch(
  121. aliceTokenKeypair.publicKey
  122. );
  123. const mint = await program.account.mint.fetch(mintKeypair.publicKey);
  124. assert.ok(aliceToken.amount.toNumber() === 0);
  125. assert.ok(mint.supply.toNumber() === 1);
  126. });
  127. });