All files / tests/utils validator.ts

32.14% Statements 18/56
16.66% Branches 1/6
20% Functions 1/5
33.33% Lines 18/54

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 1241x             1x 1x 1x 1x 1x   1x 1x       1x           1x     1x 1x     1x 1x 1x 1x                   1x                                                                                                     1x                                                        
import {
  Keypair,
  PublicKey,
  Connection,
  LAMPORTS_PER_SOL,
  clusterApiUrl,
} from "@solana/web3.js";
import * as path from "path";
import { readFileSync, writeFileSync } from "fs";
import { ChildProcess, spawn, execSync } from "child_process";
import tmp from "tmp";
import { sleep } from "@bonfida/utils";
 
const programName = "dex_v4";
export const DEVNET_URL = clusterApiUrl("devnet");
 
// Spawns a local solana test validator. Caller is responsible for killing the
// process.
export async function spawnLocalSolana(): Promise<ChildProcess> {
  const ledger = tmp.dirSync();
  return spawn("solana-test-validator", ["-l", ledger.name]);
}
 
// Returns a keypair and key file name.
export async function initializePayer(
  connection: Connection
): Promise<[Keypair, string]> {
  const name = "/Users/michaelyu/.config/solana/fun.json";
  const currentWallet = Keypair.fromSecretKey(
    new Uint8Array(JSON.parse(readFileSync(name, "utf-8")))
  );
  const lamports = await connection.getBalance(currentWallet.publicKey);
  if (lamports > 0) {
    console.log(`Existing wallet with ${lamports}`);
    return [currentWallet, name];
  }
  console.log(`Creating new wallet`);
  const key = new Keypair();
  writeFileSync(name, JSON.stringify(Array.from(key.secretKey)));
  return [key, name];
}
 
// Deploys the agnostic order book program. Fees are paid with the fee payer
// whose key is in the given key file.
export function deployProgram(
  payerKeyFile: string,
  compile: boolean,
  compileFlag?: string,
  testBpf?: boolean
): PublicKey {
  const programDirectory = path.join(
    path.dirname(__filename),
    "../../../program"
  );
  const programSo = path.join(
    programDirectory,
    `target/deploy/${programName}.so`
  );
  const keyfile = path.join(
    path.dirname(programSo),
    `${programName}-keypair.json`
  );
  let compileCmd = "cargo build-bpf";
  Iif (compileFlag) {
    compileCmd += ` --features ${compileFlag}`;
  }
  Iif (compile) {
    execSync(compileCmd, {
      cwd: programDirectory,
    });
  }
  Iif (testBpf) {
    execSync("cargo test-bpf", {
      cwd: programDirectory,
    });
  }
 
  const bytes = readFileSync(keyfile, "utf-8");
  const keypair = Keypair.fromSecretKey(Uint8Array.from(JSON.parse(bytes)));
  execSync(
    [
      "solana program deploy",
      programSo,
      "--program-id",
      keyfile,
      `-u ${DEVNET_URL}`,
      "-k",
      payerKeyFile,
      "--commitment finalized",
    ].join(" ")
  );
  return keypair.publicKey;
}
 
// Funds the given account. Sleeps until the connection is ready.
export async function airdropPayer(
  connection: Connection,
  key: PublicKey,
  loop = 5
) {
  let c = 0;
  let balance = await connection.getBalance(key);
  console.log(`Fee payer SOL balance ${balance / LAMPORTS_PER_SOL}`);
  Iif (balance > 9 * LAMPORTS_PER_SOL) return;
  while (c < loop) {
    try {
      const signature = await connection.requestAirdrop(
        key,
        1 * LAMPORTS_PER_SOL
      );
      console.log(`Airdrop signature ${signature}`);
      await connection.confirmTransaction(signature, "confirmed");
      c++;
      await sleep(10_000);
      continue;
    } catch (e) {
      console.log(`Error airdropping ${e}`);
      await new Promise((resolve) => setTimeout(resolve, 1000));
      continue;
    }
  }
  console.log(`Finished airdropping`);
}