token.spec.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: Apache-2.0
  2. import { getOrCreateAssociatedTokenAccount, createMint, TOKEN_PROGRAM_ID } from '@solana/spl-token';
  3. import { Keypair } from '@solana/web3.js';
  4. import { loadContractAndCallConstructor } from './setup';
  5. import { BN } from '@coral-xyz/anchor';
  6. import expect from 'expect';
  7. describe('Create spl-token and use from solidity', function () {
  8. this.timeout(500000);
  9. it('spl-token', async function name() {
  10. const { provider, storage, payer, program } = await loadContractAndCallConstructor('Token');
  11. const connection = provider.connection;
  12. const mintAuthority = Keypair.generate();
  13. const freezeAuthority = Keypair.generate();
  14. const mint = await createMint(
  15. connection,
  16. payer,
  17. mintAuthority.publicKey,
  18. freezeAuthority.publicKey,
  19. 3
  20. );
  21. await program.methods.setMint(mint)
  22. .accounts({ dataAccount: storage.publicKey })
  23. .rpc();
  24. let total_supply = await program.methods.totalSupply()
  25. .accounts({
  26. dataAccount: storage.publicKey,
  27. mint: mint
  28. })
  29. .view();
  30. expect(total_supply.toNumber()).toBe(0);
  31. const tokenAccount = await getOrCreateAssociatedTokenAccount(
  32. connection,
  33. payer,
  34. mint,
  35. payer.publicKey
  36. )
  37. let balance = await program.methods.getBalance()
  38. .accounts({account: tokenAccount.address})
  39. .view();
  40. expect(balance.toNumber()).toBe(0);
  41. // Now let's mint some tokens
  42. await program.methods.mintTo(
  43. new BN(100000))
  44. .accounts({
  45. dataAccount: storage.publicKey,
  46. mint: mint,
  47. account: tokenAccount.address,
  48. authority: mintAuthority.publicKey,
  49. })
  50. .signers([mintAuthority])
  51. .rpc();
  52. // let's check the balances
  53. total_supply = await program.methods.totalSupply()
  54. .accounts({
  55. dataAccount: storage.publicKey,
  56. mint: mint,
  57. })
  58. .view();
  59. expect(total_supply.toNumber()).toBe(100000);
  60. balance = await program.methods.getBalance()
  61. .accounts({account: tokenAccount.address})
  62. .view();
  63. expect(balance.toNumber()).toBe(100000);
  64. // transfer
  65. const theOutsider = Keypair.generate();
  66. const otherTokenAccount = await getOrCreateAssociatedTokenAccount(
  67. connection,
  68. payer,
  69. mint,
  70. theOutsider.publicKey
  71. )
  72. await program.methods.transfer(
  73. new BN(70000))
  74. .accounts(
  75. {
  76. from: tokenAccount.address,
  77. to: otherTokenAccount.address,
  78. owner: payer.publicKey
  79. }
  80. )
  81. .signers([payer])
  82. .rpc();
  83. total_supply = await program.methods.totalSupply()
  84. .accounts({
  85. dataAccount: storage.publicKey,
  86. mint: mint,
  87. })
  88. .view();
  89. expect(total_supply.toNumber()).toBe(100000);
  90. balance = await program.methods.getBalance()
  91. .accounts({account: tokenAccount.address})
  92. .view();
  93. expect(balance.toNumber()).toBe(30000);
  94. balance = await program.methods.getBalance()
  95. .accounts({account: otherTokenAccount.address})
  96. .view();
  97. expect(balance.toNumber()).toBe(70000);
  98. // burn
  99. await program.methods.burn(
  100. new BN(20000))
  101. .accounts({
  102. mint: mint,
  103. account: otherTokenAccount.address,
  104. owner: theOutsider.publicKey,
  105. })
  106. .signers([theOutsider])
  107. .rpc();
  108. total_supply = await program.methods.totalSupply()
  109. .accounts({
  110. dataAccount: storage.publicKey,
  111. mint: mint,
  112. })
  113. .view();
  114. expect(total_supply.toNumber()).toBe(80000);
  115. balance = await program.methods.getBalance()
  116. .accounts({account: tokenAccount.address})
  117. .view();
  118. expect(balance.toNumber()).toBe(30000);
  119. balance = await program.methods.getBalance()
  120. .accounts({account: otherTokenAccount.address})
  121. .view();
  122. expect(balance.toNumber()).toBe(50000);
  123. });
  124. });