token.spec.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 { loadContract } 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 loadContract('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({ dataAccount: storage.publicKey })
  26. .remainingAccounts([{ pubkey: mint, isSigner: false, isWritable: false }])
  27. .view();
  28. expect(total_supply.toNumber()).toBe(0);
  29. const tokenAccount = await getOrCreateAssociatedTokenAccount(
  30. connection,
  31. payer,
  32. mint,
  33. payer.publicKey
  34. )
  35. let balance = await program.methods.getBalance(tokenAccount.address)
  36. .accounts({ dataAccount: storage.publicKey })
  37. .remainingAccounts([{ pubkey: tokenAccount.address, isSigner: false, isWritable: false }])
  38. .view();
  39. expect(balance.toNumber()).toBe(0);
  40. // Now let's mint some tokens
  41. await program.methods.mintTo(
  42. tokenAccount.address,
  43. mintAuthority.publicKey,
  44. new BN(100000))
  45. .accounts({ dataAccount: storage.publicKey })
  46. .remainingAccounts([
  47. { pubkey: mint, isSigner: false, isWritable: true },
  48. { pubkey: tokenAccount.address, isSigner: false, isWritable: true },
  49. { pubkey: mintAuthority.publicKey, isSigner: true, isWritable: true },
  50. ])
  51. .signers([mintAuthority])
  52. .rpc();
  53. // let's check the balances
  54. total_supply = await program.methods.totalSupply()
  55. .accounts({ dataAccount: storage.publicKey })
  56. .remainingAccounts([{ pubkey: mint, isSigner: false, isWritable: false }])
  57. .view();
  58. expect(total_supply.toNumber()).toBe(100000);
  59. balance = await program.methods.getBalance(tokenAccount.address)
  60. .accounts({ dataAccount: storage.publicKey })
  61. .remainingAccounts([{ pubkey: tokenAccount.address, isSigner: false, isWritable: false }])
  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. tokenAccount.address,
  74. otherTokenAccount.address,
  75. payer.publicKey,
  76. new BN(70000))
  77. .accounts({ dataAccount: storage.publicKey })
  78. .remainingAccounts([
  79. { pubkey: otherTokenAccount.address, isSigner: false, isWritable: true },
  80. { pubkey: tokenAccount.address, isSigner: false, isWritable: true },
  81. { pubkey: payer.publicKey, isSigner: true, isWritable: true },
  82. ])
  83. .signers([payer])
  84. .rpc();
  85. total_supply = await program.methods.totalSupply()
  86. .accounts({ dataAccount: storage.publicKey })
  87. .remainingAccounts([{ pubkey: mint, isSigner: false, isWritable: false }])
  88. .view();
  89. expect(total_supply.toNumber()).toBe(100000);
  90. balance = await program.methods.getBalance(tokenAccount.address)
  91. .accounts({ dataAccount: storage.publicKey })
  92. .remainingAccounts([{ pubkey: tokenAccount.address, isSigner: false, isWritable: false }])
  93. .view();
  94. expect(balance.toNumber()).toBe(30000);
  95. balance = await program.methods.getBalance(otherTokenAccount.address)
  96. .accounts({ dataAccount: storage.publicKey })
  97. .remainingAccounts([{ pubkey: otherTokenAccount.address, isSigner: false, isWritable: false }])
  98. .view();
  99. expect(balance.toNumber()).toBe(70000);
  100. // burn
  101. await program.methods.burn(
  102. otherTokenAccount.address,
  103. theOutsider.publicKey,
  104. new BN(20000))
  105. .accounts({ dataAccount: storage.publicKey })
  106. .remainingAccounts([
  107. { pubkey: otherTokenAccount.address, isSigner: false, isWritable: true },
  108. { pubkey: mint, isSigner: false, isWritable: true },
  109. { pubkey: theOutsider.publicKey, isSigner: true, isWritable: true },
  110. ])
  111. .signers([theOutsider])
  112. .rpc();
  113. total_supply = await program.methods.totalSupply()
  114. .accounts({ dataAccount: storage.publicKey })
  115. .remainingAccounts([{ pubkey: mint, isSigner: false, isWritable: false }])
  116. .view();
  117. expect(total_supply.toNumber()).toBe(80000);
  118. balance = await program.methods.getBalance(tokenAccount.address)
  119. .accounts({ dataAccount: storage.publicKey })
  120. .remainingAccounts([{ pubkey: tokenAccount.address, isSigner: false, isWritable: false }])
  121. .view();
  122. expect(balance.toNumber()).toBe(30000);
  123. balance = await program.methods.getBalance(otherTokenAccount.address)
  124. .accounts({ dataAccount: storage.publicKey })
  125. .remainingAccounts([{ pubkey: otherTokenAccount.address, isSigner: false, isWritable: false }])
  126. .view();
  127. expect(balance.toNumber()).toBe(50000);
  128. });
  129. });