index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // TODO: use the `@solana/spl-token` package instead of utils here.
  2. const anchor = require("@project-serum/anchor");
  3. const serumCmn = require("@project-serum/common");
  4. const TokenInstructions = require("@project-serum/serum").TokenInstructions;
  5. // TODO: remove this constant once @project-serum/serum uses the same version
  6. // of @solana/web3.js as anchor (or switch packages).
  7. const TOKEN_PROGRAM_ID = new anchor.web3.PublicKey(
  8. TokenInstructions.TOKEN_PROGRAM_ID.toString()
  9. );
  10. // Our own sleep function.
  11. function sleep(ms) {
  12. return new Promise((resolve) => setTimeout(resolve, ms));
  13. }
  14. async function getTokenAccount(provider, addr) {
  15. return await serumCmn.getTokenAccount(provider, addr);
  16. }
  17. async function createMint(provider, authority) {
  18. if (authority === undefined) {
  19. authority = provider.wallet.publicKey;
  20. }
  21. const mint = new anchor.web3.Account();
  22. const instructions = await createMintInstructions(
  23. provider,
  24. authority,
  25. mint.publicKey
  26. );
  27. const tx = new anchor.web3.Transaction();
  28. tx.add(...instructions);
  29. await provider.send(tx, [mint]);
  30. return mint.publicKey;
  31. }
  32. async function createMintInstructions(provider, authority, mint) {
  33. let instructions = [
  34. anchor.web3.SystemProgram.createAccount({
  35. fromPubkey: provider.wallet.publicKey,
  36. newAccountPubkey: mint,
  37. space: 82,
  38. lamports: await provider.connection.getMinimumBalanceForRentExemption(82),
  39. programId: TOKEN_PROGRAM_ID,
  40. }),
  41. TokenInstructions.initializeMint({
  42. mint,
  43. decimals: 6,
  44. mintAuthority: authority,
  45. }),
  46. ];
  47. return instructions;
  48. }
  49. async function createTokenAccount(provider, mint, owner) {
  50. const vault = new anchor.web3.Account();
  51. const tx = new anchor.web3.Transaction();
  52. tx.add(
  53. ...(await createTokenAccountInstrs(provider, vault.publicKey, mint, owner))
  54. );
  55. await provider.send(tx, [vault]);
  56. return vault.publicKey;
  57. }
  58. async function createTokenAccountInstrs(
  59. provider,
  60. newAccountPubkey,
  61. mint,
  62. owner,
  63. lamports
  64. ) {
  65. if (lamports === undefined) {
  66. lamports = await provider.connection.getMinimumBalanceForRentExemption(165);
  67. }
  68. return [
  69. anchor.web3.SystemProgram.createAccount({
  70. fromPubkey: provider.wallet.publicKey,
  71. newAccountPubkey,
  72. space: 165,
  73. lamports,
  74. programId: TOKEN_PROGRAM_ID,
  75. }),
  76. TokenInstructions.initializeAccount({
  77. account: newAccountPubkey,
  78. mint,
  79. owner,
  80. }),
  81. ];
  82. }
  83. async function mintToAccount(
  84. provider,
  85. mint,
  86. destination,
  87. amount,
  88. mintAuthority
  89. ) {
  90. // mint authority is the provider
  91. const tx = new anchor.web3.Transaction();
  92. tx.add(
  93. ...(await createMintToAccountInstrs(
  94. mint,
  95. destination,
  96. amount,
  97. mintAuthority
  98. ))
  99. );
  100. await provider.send(tx, []);
  101. return;
  102. }
  103. async function createMintToAccountInstrs(
  104. mint,
  105. destination,
  106. amount,
  107. mintAuthority
  108. ) {
  109. return [
  110. TokenInstructions.mintTo({
  111. mint,
  112. destination: destination,
  113. amount: amount,
  114. mintAuthority: mintAuthority,
  115. }),
  116. ];
  117. }
  118. module.exports = {
  119. TOKEN_PROGRAM_ID,
  120. sleep,
  121. getTokenAccount,
  122. createMint,
  123. createTokenAccount,
  124. mintToAccount,
  125. };