123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- // TODO: use the `@solana/spl-token` package instead of utils here.
- const anchor = require("@project-serum/anchor");
- const serumCmn = require("@project-serum/common");
- const TokenInstructions = require("@project-serum/serum").TokenInstructions;
- // TODO: remove this constant once @project-serum/serum uses the same version
- // of @solana/web3.js as anchor (or switch packages).
- const TOKEN_PROGRAM_ID = new anchor.web3.PublicKey(
- TokenInstructions.TOKEN_PROGRAM_ID.toString()
- );
- // Our own sleep function.
- function sleep(ms) {
- return new Promise((resolve) => setTimeout(resolve, ms));
- }
- async function getTokenAccount(provider, addr) {
- return await serumCmn.getTokenAccount(provider, addr);
- }
- async function createMint(provider, authority) {
- if (authority === undefined) {
- authority = provider.wallet.publicKey;
- }
- const mint = new anchor.web3.Account();
- const instructions = await createMintInstructions(
- provider,
- authority,
- mint.publicKey
- );
- const tx = new anchor.web3.Transaction();
- tx.add(...instructions);
- await provider.send(tx, [mint]);
- return mint.publicKey;
- }
- async function createMintInstructions(provider, authority, mint) {
- let instructions = [
- anchor.web3.SystemProgram.createAccount({
- fromPubkey: provider.wallet.publicKey,
- newAccountPubkey: mint,
- space: 82,
- lamports: await provider.connection.getMinimumBalanceForRentExemption(82),
- programId: TOKEN_PROGRAM_ID,
- }),
- TokenInstructions.initializeMint({
- mint,
- decimals: 6,
- mintAuthority: authority,
- }),
- ];
- return instructions;
- }
- async function createTokenAccount(provider, mint, owner) {
- const vault = new anchor.web3.Account();
- const tx = new anchor.web3.Transaction();
- tx.add(
- ...(await createTokenAccountInstrs(provider, vault.publicKey, mint, owner))
- );
- await provider.send(tx, [vault]);
- return vault.publicKey;
- }
- async function createTokenAccountInstrs(
- provider,
- newAccountPubkey,
- mint,
- owner,
- lamports
- ) {
- if (lamports === undefined) {
- lamports = await provider.connection.getMinimumBalanceForRentExemption(165);
- }
- return [
- anchor.web3.SystemProgram.createAccount({
- fromPubkey: provider.wallet.publicKey,
- newAccountPubkey,
- space: 165,
- lamports,
- programId: TOKEN_PROGRAM_ID,
- }),
- TokenInstructions.initializeAccount({
- account: newAccountPubkey,
- mint,
- owner,
- }),
- ];
- }
- async function mintToAccount(
- provider,
- mint,
- destination,
- amount,
- mintAuthority
- ) {
- // mint authority is the provider
- const tx = new anchor.web3.Transaction();
- tx.add(
- ...(await createMintToAccountInstrs(
- mint,
- destination,
- amount,
- mintAuthority
- ))
- );
- await provider.send(tx, []);
- return;
- }
- async function createMintToAccountInstrs(
- mint,
- destination,
- amount,
- mintAuthority
- ) {
- return [
- TokenInstructions.mintTo({
- mint,
- destination: destination,
- amount: amount,
- mintAuthority: mintAuthority,
- }),
- ];
- }
- module.exports = {
- TOKEN_PROGRAM_ID,
- sleep,
- getTokenAccount,
- createMint,
- createTokenAccount,
- mintToAccount,
- };
|