All files / tests/utils context.ts

63.63% Statements 21/33
100% Branches 0/0
50% Functions 1/2
61.29% Lines 19/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 1921x 1x             1x 1x 1x                 1x   1x                                 1x             1x                                                                                       1x                         1x 1x 1x               1x     1x         1x 1x                                   1x     1x                                                                                                  
import { TokenMint, signAndSendInstructions } from "@bonfida/utils";
import {
  Connection,
  Keypair,
  SystemProgram,
  LAMPORTS_PER_SOL,
  PublicKey,
} from "@solana/web3.js";
import { createMarket, initializeAccount } from "../../src/bindings";
import BN from "bn.js";
import { DEX_ID } from "../../src/ids";
import {
  createCreateMetadataAccountV2Instruction,
  CreateMetadataAccountArgsV2,
  CreateMetadataAccountV2InstructionAccounts,
  DataV2,
  Creator,
} from "@metaplex-foundation/mpl-token-metadata";
import { getMetadataKeyFromMint } from "../../src/metadata";
import { NATIVE_MINT } from "@solana/spl-token";
 
export const createContext = async (
  connection: Connection,
  feePayer: Keypair,
  tickSize: BN,
  minBaseOrderSize: BN,
  baseDecimals: number,
  quoteDecimals: number,
  baseCurrencyMultiplier?: BN,
  quoteCurrencyMultiplier?: BN,
  creators?: { key: Keypair; share: number }[],
  sellerFeeBasisPoints?: number
) => {
  /**
   * Base and quote
   */
  // const base = await TokenMint.init(connection, feePayer, baseDecimals);
  // const quote = await TokenMint.init(connection, feePayer, quoteDecimals);
  const base = new TokenMint(
    new PublicKey("8EHBwP52zz2yH5TdYHz14ZDkFXXPBxCwkCxipPpLsFJu"),
    feePayer,
    connection,
    feePayer,
    0
  );
  const quote = new TokenMint(NATIVE_MINT, feePayer, connection, feePayer, 9);
  /**
   * Create metadata
   */
  // if (creators && sellerFeeBasisPoints) {
  //   const accounts: CreateMetadataAccountV2InstructionAccounts = {
  //     metadata: await getMetadataKeyFromMint(base.token),
  //     mint: base.token,
  //     mintAuthority: base.signer.publicKey,
  //     payer: feePayer.publicKey,
  //     updateAuthority: feePayer.publicKey,
  //   };
  //   const creators_: Creator[] = creators.map((e) => {
  //     return {
  //       address: e.key.publicKey,
  //       verified: false,
  //       share: e.share,
  //     };
  //   });
  //   const data: DataV2 = {
  //     name: "Test base",
  //     symbol: "BASE",
  //     uri: "https://google.com",
  //     creators: creators_,
  //     sellerFeeBasisPoints,
  //     collection: null,
  //     uses: null,
  //   };
  //   const args: CreateMetadataAccountArgsV2 = { data, isMutable: false };
  //   const ix = createCreateMetadataAccountV2Instruction(accounts, {
  //     createMetadataAccountArgsV2: args,
  //   });
  //   const tx = await signAndSendInstructions(
  //     connection,
  //     [base.signer],
  //     feePayer,
  //     [ix]
  //   );
  //   console.log(`Created metadata ${tx}`);
  // }
 
  /**
   * Create market
   */
  let ixs = await createMarket(
    connection,
    base.token,
    quote.token,
    new BN(minBaseOrderSize),
    feePayer.publicKey,
    feePayer.publicKey,
    tickSize,
    new BN(0),
    baseCurrencyMultiplier,
    quoteCurrencyMultiplier
  );
 
  for (let ix of ixs) {
    try {
      let tx = await signAndSendInstructions(
        connection,
        ix[0],
        feePayer,
        ix[1]
      );
      console.log(tx);
    } catch (e) {
      console.error(e);
    }
  }
  const marketKey = ixs[0][0][0].publicKey;
 
  /**
   * Users
   */
  const Alice = Keypair.generate();
  const Bob = Keypair.generate();
 
  /**
   * Transfer 0.5 SOL to each user
   */
  // await signAndSendInstructions(connection, [], feePayer, [
  //   SystemProgram.transfer({
  //     fromPubkey: feePayer.publicKey,
  //     toPubkey: Alice.publicKey,
  //     lamports: LAMPORTS_PER_SOL / 10,
  //   }),
  //   SystemProgram.transfer({
  //     fromPubkey: feePayer.publicKey,
  //     toPubkey: Bob.publicKey,
  //     lamports: LAMPORTS_PER_SOL / 10,
  //   }),
  // ]);
 
  return { marketKey, base, quote, Alice, Bob };
};
 
export const initializeTraders = async (
  connection: Connection,
  base: TokenMint,
  quote: TokenMint,
  Alice: Keypair,
  Bob: Keypair,
  feePayer: Keypair,
  marketKey: PublicKey,
  baseTokenAmount: number,
  quoteTokenAmount: number
) => {
  const aliceBaseAta = await base.getAssociatedTokenAccount(Alice.publicKey);
  const aliceQuoteAta = await quote.getAssociatedTokenAccount(Alice.publicKey);
 
  const bobBaseAta = await base.getAssociatedTokenAccount(Bob.publicKey);
  const bobQuoteAta = await quote.getAssociatedTokenAccount(Bob.publicKey);
 
  await base.mintInto(aliceBaseAta, baseTokenAmount);
  // await quote.mintInto(aliceQuoteAta, quoteTokenAmount);
 
  await base.mintInto(bobBaseAta, baseTokenAmount);
  // await quote.mintInto(bobQuoteAta, quoteTokenAmount);
 
  // Create user accounts
  let tx = await signAndSendInstructions(connection, [Alice, Bob], feePayer, [
    await initializeAccount(marketKey, Alice.publicKey, 20, feePayer.publicKey),
    await initializeAccount(marketKey, Bob.publicKey, 20, feePayer.publicKey),
  ]);
 
  console.log(`Initialized traders ${tx}`);
 
  const [aliceUa] = await PublicKey.findProgramAddress(
    [marketKey.toBuffer(), Alice.publicKey.toBuffer()],
    DEX_ID
  );
  const [bobUa] = await PublicKey.findProgramAddress(
    [marketKey.toBuffer(), Bob.publicKey.toBuffer()],
    DEX_ID
  );
 
  return {
    aliceBaseAta,
    aliceQuoteAta,
    bobBaseAta,
    bobQuoteAta,
    aliceUa,
    bobUa,
  };
};