token-proxy.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. const anchor = require("@project-serum/anchor");
  2. const assert = require("assert");
  3. describe("token", () => {
  4. const provider = anchor.Provider.local();
  5. // Configure the client to use the local cluster.
  6. anchor.setProvider(provider);
  7. const program = anchor.workspace.TokenProxy;
  8. let mint = null;
  9. let from = null;
  10. let to = null;
  11. it("Initializes test state", async () => {
  12. mint = await createMint(provider);
  13. from = await createTokenAccount(provider, mint, provider.wallet.publicKey);
  14. to = await createTokenAccount(provider, mint, provider.wallet.publicKey);
  15. });
  16. it("Mints a token", async () => {
  17. await program.rpc.proxyMintTo(new anchor.BN(1000), {
  18. accounts: {
  19. authority: provider.wallet.publicKey,
  20. mint,
  21. to: from,
  22. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  23. },
  24. });
  25. const fromAccount = await getTokenAccount(provider, from);
  26. assert.ok(fromAccount.amount.eq(new anchor.BN(1000)));
  27. });
  28. it("Transfers a token", async () => {
  29. await program.rpc.proxyTransfer(new anchor.BN(400), {
  30. accounts: {
  31. authority: provider.wallet.publicKey,
  32. to,
  33. from,
  34. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  35. },
  36. });
  37. const fromAccount = await getTokenAccount(provider, from);
  38. const toAccount = await getTokenAccount(provider, to);
  39. assert.ok(fromAccount.amount.eq(new anchor.BN(600)));
  40. assert.ok(toAccount.amount.eq(new anchor.BN(400)));
  41. });
  42. it("Burns a token", async () => {
  43. await program.rpc.proxyBurn(new anchor.BN(399), {
  44. accounts: {
  45. authority: provider.wallet.publicKey,
  46. mint,
  47. to,
  48. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  49. },
  50. });
  51. const toAccount = await getTokenAccount(provider, to);
  52. assert.ok(toAccount.amount.eq(new anchor.BN(1)));
  53. });
  54. it("Set new mint authority", async () => {
  55. const newMintAuthority = anchor.web3.Keypair.generate();
  56. await program.rpc.proxySetAuthority(
  57. { mintTokens: {} },
  58. newMintAuthority.publicKey,
  59. {
  60. accounts: {
  61. accountOrMint: mint,
  62. currentAuthority: provider.wallet.publicKey,
  63. tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID,
  64. },
  65. }
  66. );
  67. const mintInfo = await getMintInfo(provider, mint);
  68. assert.ok(mintInfo.mintAuthority.equals(newMintAuthority.publicKey));
  69. });
  70. });
  71. // SPL token client boilerplate for test initialization. Everything below here is
  72. // mostly irrelevant to the point of the example.
  73. const serumCmn = require("@project-serum/common");
  74. const TokenInstructions = require("@project-serum/serum").TokenInstructions;
  75. // TODO: remove this constant once @project-serum/serum uses the same version
  76. // of @solana/web3.js as anchor (or switch packages).
  77. const TOKEN_PROGRAM_ID = new anchor.web3.PublicKey(
  78. TokenInstructions.TOKEN_PROGRAM_ID.toString()
  79. );
  80. async function getTokenAccount(provider, addr) {
  81. return await serumCmn.getTokenAccount(provider, addr);
  82. }
  83. async function getMintInfo(provider, mintAddr) {
  84. return await serumCmn.getMintInfo(provider, mintAddr);
  85. }
  86. async function createMint(provider, authority) {
  87. if (authority === undefined) {
  88. authority = provider.wallet.publicKey;
  89. }
  90. const mint = anchor.web3.Keypair.generate();
  91. const instructions = await createMintInstructions(
  92. provider,
  93. authority,
  94. mint.publicKey
  95. );
  96. const tx = new anchor.web3.Transaction();
  97. tx.add(...instructions);
  98. await provider.send(tx, [mint]);
  99. return mint.publicKey;
  100. }
  101. async function createMintInstructions(provider, authority, mint) {
  102. let instructions = [
  103. anchor.web3.SystemProgram.createAccount({
  104. fromPubkey: provider.wallet.publicKey,
  105. newAccountPubkey: mint,
  106. space: 82,
  107. lamports: await provider.connection.getMinimumBalanceForRentExemption(82),
  108. programId: TOKEN_PROGRAM_ID,
  109. }),
  110. TokenInstructions.initializeMint({
  111. mint,
  112. decimals: 0,
  113. mintAuthority: authority,
  114. }),
  115. ];
  116. return instructions;
  117. }
  118. async function createTokenAccount(provider, mint, owner) {
  119. const vault = anchor.web3.Keypair.generate();
  120. const tx = new anchor.web3.Transaction();
  121. tx.add(
  122. ...(await createTokenAccountInstrs(provider, vault.publicKey, mint, owner))
  123. );
  124. await provider.send(tx, [vault]);
  125. return vault.publicKey;
  126. }
  127. async function createTokenAccountInstrs(
  128. provider,
  129. newAccountPubkey,
  130. mint,
  131. owner,
  132. lamports
  133. ) {
  134. if (lamports === undefined) {
  135. lamports = await provider.connection.getMinimumBalanceForRentExemption(165);
  136. }
  137. return [
  138. anchor.web3.SystemProgram.createAccount({
  139. fromPubkey: provider.wallet.publicKey,
  140. newAccountPubkey,
  141. space: 165,
  142. lamports,
  143. programId: TOKEN_PROGRAM_ID,
  144. }),
  145. TokenInstructions.initializeAccount({
  146. account: newAccountPubkey,
  147. mint,
  148. owner,
  149. }),
  150. ];
  151. }