All files / utils testhelpers.ts

50% Statements 35/70
15.38% Branches 2/13
50% Functions 3/6
52.45% Lines 32/61

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    28x 21x 21x 21x 21x       21x                 21x           21x   21x           16x 16x 16x 16x 16x 16x 16x 16x 16x 3x 3x                                                                         16x   3x   3x                     16x 16x       16x 16x               17x 17x   17x 6426x     17x    
/* eslint-disable @typescript-eslint/no-inferrable-types */
export {
  accountBalance,
  accountBalanceMina,
  sleep,
  makeString,
  initBlockchain,
  blockchain,
};
 
import {
  fetchAccount,
  PrivateKey,
  Mina,
  PublicKey,
  UInt64,
  SmartContract,
  ZkProgram,
} from "o1js";
import {
  initBlockchain as initBlockchainMina,
  accountBalance,
  accountBalanceMina,
} from "../src/mina";
import { blockchain } from "../src/networks";
import { Memory } from "../src/mina";
 
import { DEPLOYER, DEPLOYERS } from "../env.json";
 
async function initBlockchain(
  instance: blockchain,
  deployersNumber: number = 0
): Promise<{ deployer: PrivateKey; deployers: PrivateKey[] } | undefined> {
  Memory.info(`initial`);
  let deployer: PrivateKey | undefined = undefined;
  const deployers: PrivateKey[] = [];
  if (instance === "local") {
    const Local = await Mina.LocalBlockchain({ proofsEnabled: true });
    Mina.setActiveInstance(Local);
    const privateKey = Local.testAccounts[0].key;
    deployer = privateKey;
    for (let i = 1; i <= deployersNumber; i++) {
      const privateKey = Local.testAccounts[i].key;
      deployers.push(privateKey);
    }
  } else Eif (instance === "berkeley") {
    initBlockchainMina("berkeley");
    deployer = PrivateKey.fromBase58(DEPLOYER);
    for (let i = 0; i < deployersNumber; i++) {
      const privateKey = PrivateKey.fromBase58(DEPLOYERS[i]);
      deployers.push(privateKey);
    }
  } else if (instance === "devnet") {
    initBlockchainMina("devnet");
    deployer = PrivateKey.fromBase58(DEPLOYER);
    for (let i = 0; i < deployersNumber; i++) {
      const privateKey = PrivateKey.fromBase58(DEPLOYERS[i]);
      deployers.push(privateKey);
    }
  } else if (instance === "zeko") {
    initBlockchainMina("zeko");
    deployer = PrivateKey.fromBase58(
      "EKE6vwZjfG9wHhaaPDvq4GuZABVsyzqA8fHVGK2QdYsoNHb78mYF"
    );
    for (let i = 0; i < deployersNumber; i++) {
      const privateKey = PrivateKey.fromBase58(DEPLOYERS[i]);
      deployers.push(privateKey);
    }
  } else if (instance === "testworld2") {
    initBlockchainMina("testworld2");
    deployer = PrivateKey.fromBase58(DEPLOYER);
    for (let i = 0; i < deployersNumber; i++) {
      const privateKey = PrivateKey.fromBase58(DEPLOYERS[i]);
      deployers.push(privateKey);
    }
  } else {
    console.log("Mainnet is not supported yet");
    return undefined;
  }
 
  for (let i = 0; i < deployersNumber; i++) {
    const balanceDeployer =
      Number((await accountBalance(deployers[i].toPublicKey())).toBigInt()) /
      1e9;
    Iif (balanceDeployer <= 5) {
      console.log(
        `Balance of the Deployer`,
        i,
        `is`,
        balanceDeployer.toLocaleString(`en`)
      );
      return undefined;
    }
  }
  const balanceDeployer =
    Number((await accountBalance(deployer.toPublicKey())).toBigInt()) / 1e9;
  console.log(
    `Balance of the Deployer is`,
    balanceDeployer.toLocaleString(`en`)
  );
  Iif (balanceDeployer <= 2) return undefined;
  return { deployer, deployers };
}
 
function sleep(ms: number) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}
 
function makeString(length: number): string {
  let outString: string = ``;
  const inOptions: string = `abcdefghijklmnopqrstuvwxyz0123456789`;
 
  for (let i = 0; i < length; i++) {
    outString += inOptions.charAt(Math.floor(Math.random() * inOptions.length));
  }
 
  return outString;
}