token-proxy.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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(500), {
  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(500)));
  40. assert.ok(fromAccount.amount.eq(new anchor.BN(500)));
  41. });
  42. it("Burns a token", async () => {
  43. await program.rpc.proxyBurn(new anchor.BN(499), {
  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. async function getTokenAccount(provider, addr) {
  60. return await serumCmn.getTokenAccount(provider, addr);
  61. }
  62. async function createMint(provider, authority) {
  63. if (authority === undefined) {
  64. authority = provider.wallet.publicKey;
  65. }
  66. const mint = new anchor.web3.Account();
  67. const instructions = await createMintInstructions(
  68. provider,
  69. authority,
  70. mint.publicKey
  71. );
  72. const tx = new anchor.web3.Transaction();
  73. tx.add(...instructions);
  74. await provider.send(tx, [mint]);
  75. return mint.publicKey;
  76. }
  77. async function createMintInstructions(provider, authority, mint) {
  78. let instructions = [
  79. anchor.web3.SystemProgram.createAccount({
  80. fromPubkey: provider.wallet.publicKey,
  81. newAccountPubkey: mint,
  82. space: 82,
  83. lamports: await provider.connection.getMinimumBalanceForRentExemption(82),
  84. programId: TokenInstructions.TOKEN_PROGRAM_ID,
  85. }),
  86. TokenInstructions.initializeMint({
  87. mint,
  88. decimals: 0,
  89. mintAuthority: authority,
  90. }),
  91. ];
  92. return instructions;
  93. }
  94. async function createTokenAccount(provider, mint, owner) {
  95. const vault = new anchor.web3.Account();
  96. const tx = new anchor.web3.Transaction();
  97. tx.add(
  98. ...(await createTokenAccountInstrs(provider, vault.publicKey, mint, owner))
  99. );
  100. await provider.send(tx, [vault]);
  101. return vault.publicKey;
  102. }
  103. async function createTokenAccountInstrs(
  104. provider,
  105. newAccountPubkey,
  106. mint,
  107. owner,
  108. lamports
  109. ) {
  110. if (lamports === undefined) {
  111. lamports = await provider.connection.getMinimumBalanceForRentExemption(165);
  112. }
  113. return [
  114. anchor.web3.SystemProgram.createAccount({
  115. fromPubkey: provider.wallet.publicKey,
  116. newAccountPubkey,
  117. space: 165,
  118. lamports,
  119. programId: TokenInstructions.TOKEN_PROGRAM_ID,
  120. }),
  121. TokenInstructions.initializeAccount({
  122. account: newAccountPubkey,
  123. mint,
  124. owner,
  125. }),
  126. ];
  127. }