index.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const spl = require("@solana/spl-token");
  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 = await spl.Token.createMint(
  22. provider.connection,
  23. provider.wallet.payer,
  24. authority,
  25. null,
  26. 6,
  27. TOKEN_PROGRAM_ID
  28. );
  29. return mint;
  30. }
  31. async function createTokenAccount(provider, mint, owner) {
  32. const token = new spl.Token(
  33. provider.connection,
  34. mint,
  35. TOKEN_PROGRAM_ID,
  36. provider.wallet.payer
  37. );
  38. let vault = await token.createAccount(owner);
  39. return vault;
  40. }
  41. module.exports = {
  42. TOKEN_PROGRAM_ID,
  43. sleep,
  44. getTokenAccount,
  45. createTokenAccount,
  46. createMint,
  47. };