token.spec.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 '@project-serum/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: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
  48. { pubkey: mint, isSigner: false, isWritable: true },
  49. { pubkey: tokenAccount.address, isSigner: false, isWritable: true },
  50. { pubkey: mintAuthority.publicKey, isSigner: true, isWritable: true },
  51. ])
  52. .signers([mintAuthority])
  53. .rpc();
  54. // let's check the balances
  55. total_supply = await program.methods.totalSupply()
  56. .accounts({ dataAccount: storage.publicKey })
  57. .remainingAccounts([{ pubkey: mint, isSigner: false, isWritable: false }])
  58. .view();
  59. expect(total_supply.toNumber()).toBe(100000);
  60. balance = await program.methods.getBalance(tokenAccount.address)
  61. .accounts({ dataAccount: storage.publicKey })
  62. .remainingAccounts([{ pubkey: tokenAccount.address, isSigner: false, isWritable: false }])
  63. .view();
  64. expect(balance.toNumber()).toBe(100000);
  65. // transfer
  66. const theOutsider = Keypair.generate();
  67. const otherTokenAccount = await getOrCreateAssociatedTokenAccount(
  68. connection,
  69. payer,
  70. mint,
  71. theOutsider.publicKey
  72. )
  73. await program.methods.transfer(
  74. tokenAccount.address,
  75. otherTokenAccount.address,
  76. payer.publicKey,
  77. new BN(70000))
  78. .accounts({ dataAccount: storage.publicKey })
  79. .remainingAccounts([
  80. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
  81. { pubkey: otherTokenAccount.address, isSigner: false, isWritable: true },
  82. { pubkey: tokenAccount.address, isSigner: false, isWritable: true },
  83. { pubkey: payer.publicKey, isSigner: true, isWritable: true },
  84. ])
  85. .signers([payer])
  86. .rpc();
  87. total_supply = await program.methods.totalSupply()
  88. .accounts({ dataAccount: storage.publicKey })
  89. .remainingAccounts([{ pubkey: mint, isSigner: false, isWritable: false }])
  90. .view();
  91. expect(total_supply.toNumber()).toBe(100000);
  92. balance = await program.methods.getBalance(tokenAccount.address)
  93. .accounts({ dataAccount: storage.publicKey })
  94. .remainingAccounts([{ pubkey: tokenAccount.address, isSigner: false, isWritable: false }])
  95. .view();
  96. expect(balance.toNumber()).toBe(30000);
  97. balance = await program.methods.getBalance(otherTokenAccount.address)
  98. .accounts({ dataAccount: storage.publicKey })
  99. .remainingAccounts([{ pubkey: otherTokenAccount.address, isSigner: false, isWritable: false }])
  100. .view();
  101. expect(balance.toNumber()).toBe(70000);
  102. // burn
  103. await program.methods.burn(
  104. otherTokenAccount.address,
  105. theOutsider.publicKey,
  106. new BN(20000))
  107. .accounts({ dataAccount: storage.publicKey })
  108. .remainingAccounts([
  109. { pubkey: TOKEN_PROGRAM_ID, isSigner: false, isWritable: false },
  110. { pubkey: otherTokenAccount.address, isSigner: false, isWritable: true },
  111. { pubkey: mint, isSigner: false, isWritable: true },
  112. { pubkey: theOutsider.publicKey, isSigner: true, isWritable: true },
  113. ])
  114. .signers([theOutsider])
  115. .rpc();
  116. total_supply = await program.methods.totalSupply()
  117. .accounts({ dataAccount: storage.publicKey })
  118. .remainingAccounts([{ pubkey: mint, isSigner: false, isWritable: false }])
  119. .view();
  120. expect(total_supply.toNumber()).toBe(80000);
  121. balance = await program.methods.getBalance(tokenAccount.address)
  122. .accounts({ dataAccount: storage.publicKey })
  123. .remainingAccounts([{ pubkey: tokenAccount.address, isSigner: false, isWritable: false }])
  124. .view();
  125. expect(balance.toNumber()).toBe(30000);
  126. balance = await program.methods.getBalance(otherTokenAccount.address)
  127. .accounts({ dataAccount: storage.publicKey })
  128. .remainingAccounts([{ pubkey: otherTokenAccount.address, isSigner: false, isWritable: false }])
  129. .view();
  130. expect(balance.toNumber()).toBe(50000);
  131. });
  132. });