token-proxy.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. });
  55. // SPL token client boilerplate for test initialization. Everything below here is
  56. // mostly irrelevant to the point of the example.
  57. const serumCmn = require("@project-serum/common");
  58. const TokenInstructions = require("@project-serum/serum").TokenInstructions;
  59. // TODO: remove this constant once @project-serum/serum uses the same version
  60. // of @solana/web3.js as anchor (or switch packages).
  61. const TOKEN_PROGRAM_ID = new anchor.web3.PublicKey(
  62. TokenInstructions.TOKEN_PROGRAM_ID.toString()
  63. );
  64. async function getTokenAccount(provider, addr) {
  65. return await serumCmn.getTokenAccount(provider, addr);
  66. }
  67. async function createMint(provider, authority) {
  68. if (authority === undefined) {
  69. authority = provider.wallet.publicKey;
  70. }
  71. const mint = new anchor.web3.Account();
  72. const instructions = await createMintInstructions(
  73. provider,
  74. authority,
  75. mint.publicKey
  76. );
  77. const tx = new anchor.web3.Transaction();
  78. tx.add(...instructions);
  79. await provider.send(tx, [mint]);
  80. return mint.publicKey;
  81. }
  82. async function createMintInstructions(provider, authority, mint) {
  83. let instructions = [
  84. anchor.web3.SystemProgram.createAccount({
  85. fromPubkey: provider.wallet.publicKey,
  86. newAccountPubkey: mint,
  87. space: 82,
  88. lamports: await provider.connection.getMinimumBalanceForRentExemption(82),
  89. programId: TOKEN_PROGRAM_ID,
  90. }),
  91. TokenInstructions.initializeMint({
  92. mint,
  93. decimals: 0,
  94. mintAuthority: authority,
  95. }),
  96. ];
  97. return instructions;
  98. }
  99. async function createTokenAccount(provider, mint, owner) {
  100. const vault = new anchor.web3.Account();
  101. const tx = new anchor.web3.Transaction();
  102. tx.add(
  103. ...(await createTokenAccountInstrs(provider, vault.publicKey, mint, owner))
  104. );
  105. await provider.send(tx, [vault]);
  106. return vault.publicKey;
  107. }
  108. async function createTokenAccountInstrs(
  109. provider,
  110. newAccountPubkey,
  111. mint,
  112. owner,
  113. lamports
  114. ) {
  115. if (lamports === undefined) {
  116. lamports = await provider.connection.getMinimumBalanceForRentExemption(165);
  117. }
  118. return [
  119. anchor.web3.SystemProgram.createAccount({
  120. fromPubkey: provider.wallet.publicKey,
  121. newAccountPubkey,
  122. space: 165,
  123. lamports,
  124. programId: TOKEN_PROGRAM_ID,
  125. }),
  126. TokenInstructions.initializeAccount({
  127. account: newAccountPubkey,
  128. mint,
  129. owner,
  130. }),
  131. ];
  132. }