custom-coder.ts 4.2 KB

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