All files / tests/helpers compile.ts

90.69% Statements 39/43
90% Branches 9/10
100% Functions 6/6
90.69% Lines 39/43

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 882x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x   2x     2x                       2x 2x 2x 2x 2x     2x 2x 2x 2x 2x 2x     2x 2x 2x 2x 2x     2x 2x 2x                                               2x     2x 2x 2x 2x 2x 2x      
import { describe, expect, it } from "@jest/globals";
import { Mina, VerificationKey, Field, Cache } from "o1js";
import { initBlockchain, blockchain } from "zkcloudworker";
import { NFTAdminContract } from "../../src/admin";
import { VerificationKeyUpgradeAuthority } from "../../src/upgrade/upgrade";
import { NFT, CollectionContract } from "../../src/contracts";
import fs from "fs/promises";
import { version } from "../../node_modules/o1js/package.json";
import { NFTVerificationKeys } from "../../src/contracts/vk";
 
export function compileContracts(chain: blockchain) {
  const networkId = chain === "mainnet" ? "mainnet" : "testnet";
 
  const cache: Cache = Cache.FileSystem(
    networkId === "mainnet" ? "./cache-mainnet" : "./cache"
  );
  const NFTAdmin = NFTAdminContract({
    upgradeContract: VerificationKeyUpgradeAuthority,
  });
  class Collection extends CollectionContract({
    adminContract: NFTAdmin,
    upgradeContract: VerificationKeyUpgradeAuthority,
    networkId,
  }) {}
 
  let collectionVk: VerificationKey;
  let nftVk: VerificationKey;
 
  it("should initialize a blockchain", async () => {
    await initBlockchain(chain);
    console.log("chain:", chain);
    console.log("networkId:", Mina.getNetworkId());
    console.log(`o1js version:`, version);
  });
 
  it("should compile NFT Contract", async () => {
    console.log("compiling...");
    console.time("compiled NFTContract");
    const { verificationKey } = await NFT.compile({ cache });
    nftVk = verificationKey;
    console.timeEnd("compiled NFTContract");
  });
 
  it("should compile Collection", async () => {
    console.time("compiled Collection");
    const { verificationKey } = await Collection.compile({ cache });
    collectionVk = verificationKey;
    console.timeEnd("compiled Collection");
  });
 
  it("should save new verification keys", async () => {
    const vk = NFTVerificationKeys[networkId];
    Iif (
      collectionVk.hash.toJSON() !== vk.collection.hash ||
      nftVk.hash.toJSON() !== vk.nft.hash ||
      collectionVk.data !== vk.collection.data ||
      nftVk.data !== vk.nft.data
    ) {
      console.log("saving new verification keys");
      const vk: any = {};
 
      vk[networkId] = {
        collection: {
          hash: collectionVk.hash.toJSON(),
          data: collectionVk.data,
        },
        nft: {
          hash: nftVk.hash.toJSON(),
          data: nftVk.data,
        },
      };
      await fs.writeFile(
        `${networkId}-verification-keys.json`,
        JSON.stringify(vk, null, 2)
      );
    } else {
      console.log("verification keys have not changed");
    }
  });
  it("should compare verification keys", async () => {
    const vk = NFTVerificationKeys[networkId];
    expect(collectionVk.hash.toJSON()).toEqual(vk.collection.hash);
    expect(nftVk.hash.toJSON()).toEqual(vk.nft.hash);
    expect(collectionVk.data).toEqual(vk.collection.data);
    expect(nftVk.data).toEqual(vk.nft.data);
  });
}