Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Utils Unit Test #32

Merged
merged 6 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@
"@types/fs-extra": "^5.0.4",
"@types/mocha": "^5.2.6",
"@types/node": "^18",
"@types/sinon": "^17",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"eslint": "^8",
"hardhat": "^2.0.0",
"mocha": "^10",
"sinon": "^17.0.1",
"ts-node": "^10",
"typescript": "^5"
},
Expand Down
5 changes: 3 additions & 2 deletions packages/plugin/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ export function getConfigEnvironmentVariable(

export async function getNetworkChainId(
network: string,
hre: HardhatRuntimeEnvironment
hre: HardhatRuntimeEnvironment,
HttpProviderClass = HttpProvider
): Promise<number> {
const networkConfig = hre.config.networks[network];
let chainID = networkConfig.chainId;
if (!chainID) {
assert("httpHeaders" in networkConfig);
const httpProvider = new HttpProvider(networkConfig.url, {
const httpProvider = new HttpProviderClass(networkConfig.url, {
providerOptions: { headers: networkConfig.httpHeaders },
});
const web3 = new Web3(httpProvider);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// We load the plugin here.
import {HardhatUserConfig} from "hardhat/types";
import {Environment} from "@buildwithsygma/sygma-sdk-core";
import { HardhatUserConfig } from "hardhat/types";
import { Environment } from "@buildwithsygma/sygma-sdk-core";

import "../../../src/index";

Expand All @@ -10,12 +10,15 @@ const config: HardhatUserConfig = {
networks: {
sepolia: {
chainId: 11155111,
url: 'https://localhost:8080'
url: "https://localhost:8080",
},
goerli: {
chainId: 5,
url: 'https://localhost:8080'
}
url: "https://localhost:8080",
},
goerliNoChainId: {
url: "https://localhost:8080",
},
},
multichain: {
environment: Environment.TESTNET,
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin/test/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "path";
import { resetHardhatContext } from "hardhat/plugins-testing";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import path from "path";

declare module "mocha" {
interface Context {
Expand Down
42 changes: 42 additions & 0 deletions packages/plugin/test/mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { HttpProvider } from "web3";
import { Domain, Network } from "@buildwithsygma/sygma-sdk-core";

// TODO: Refactor to be reusable... and maybe add sinon
export function createMockHttpProvider(): typeof HttpProvider {
return MockHttpProvider as unknown as typeof HttpProvider;
}

class MockHttpProvider {
async request(payload: { method: string }) {
if (payload.method === "eth_chainId") {
return { jsonrpc: "2.0", id: 1, result: "0x5" }; // Example chain ID in hex format
}
}
}

export const mockDomains: Domain[] = [
{
id: 1,
chainId: 5,
name: "goerli",
type: Network.EVM,
},
{
id: 2,
chainId: 11155111,
name: "sepolia",
type: Network.EVM,
},
{
id: 6,
chainId: 17000,
name: "holesky",
type: Network.EVM,
},
{
id: 7,
chainId: 80001,
name: "mumbai",
type: Network.EVM,
},
];
7 changes: 3 additions & 4 deletions packages/plugin/test/project.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'mocha'
import {assert, expect, use} from "chai";
import "mocha";
import { assert, use } from "chai";
import chaiAsPromised from "chai-as-promised";

import { Environment } from "@buildwithsygma/sygma-sdk-core";
import { MultichainHardhatRuntimeEnvironmentField } from "../src/MultichainHardhatRuntimeEnvironmentField";

import { useEnvironment } from "./helpers";
import {Environment} from "@buildwithsygma/sygma-sdk-core";

use(chaiAsPromised);

Expand Down Expand Up @@ -34,6 +34,5 @@ describe("Integration tests examples", function () {

describe("Hardhat Runtime Environment extension", function () {
useEnvironment("hardhat-project");

});
});
201 changes: 201 additions & 0 deletions packages/plugin/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
import "mocha";
import { assert, expect, use } from "chai";
import chaiAsPromised from "chai-as-promised";

import { Environment } from "@buildwithsygma/sygma-sdk-core";
import helloSygmaContract from "@chainsafe/hardhat-plugin-multichain-deploy-contracts/artifacts/contracts/mocks/HelloSygma.sol/HelloSygma";
import { HardhatPluginError } from "hardhat/plugins";
import {
encodeInitData,
getConfigEnvironmentVariable,
getNetworkChainId,
mapNetworkArgs,
sumedFees,
} from "../src/utils";
import { NetworkArguments } from "../src/types";
import { createMockHttpProvider, mockDomains } from "./mocks";
import { useEnvironment } from "./helpers";

use(chaiAsPromised);

describe("Unit tests for utils", function () {
describe("getConfigEnvironmentVariable", function () {
useEnvironment("hardhat-project");

it("Should return valid environment from config", function () {
assert.deepEqual(
getConfigEnvironmentVariable(this.hre),
Environment.TESTNET
);
});
});

describe("getNetworkChainId", function () {
useEnvironment("hardhat-project");

it("Retrieve chainID from config", async function () {
const chainId = await getNetworkChainId("goerli", this.hre);
expect(chainId).to.equal(5);
});

it("Retrieve chainID from node if is not available in config", async function () {
const chainId = await getNetworkChainId(
"goerliNoChainId",
this.hre,
createMockHttpProvider()
);
expect(chainId).to.equal(5);
});
});

describe("sumedFees", function () {
it("Sum's all available types and return current result", function () {
const sum = sumedFees([4, BigInt(3), "2", "0x1"]);
expect(sum).to.equal("10");
});

[
{ type: "Number", values: [5, 5, 5] },
{ type: "Bigint", values: [BigInt(5), BigInt(5), BigInt(5)] },
{ type: "String", values: ["5", "5", "5"] },
{ type: "HexString", values: ["0x5", "0x5", "0x5"] },
].forEach(({ type, values }) => {
it(`Sum's values of type ${type} and return current value`, function () {
const sum = sumedFees(values);
expect(sum).to.equal("15");
});
});
});

describe("encodeInitData", function () {
const { abi } = helloSygmaContract;

it("Return current encoded data for specified method", function () {
expect(encodeInitData(abi, "setName", ["Pepe"])).to.be.equal(
"0xc47f0027000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000045065706500000000000000000000000000000000000000000000000000000000"
);
});

it("Should fail on unexciting method", function () {
assert.throw(
() => {
encodeInitData(abi, "nonExisting", ["GirlFriend"]);
},
HardhatPluginError,
"InitMethod nonExisting not foud in ABI"
);
});
});

describe("mapNetworkArgs", function () {
const { abi } = helloSygmaContract;

it("Return current encoded data, without init data", function () {
const networkArgs: NetworkArguments<typeof abi> = {
goerli: { args: [5] },
holesky: { args: [5] },
};

assert.deepEqual(mapNetworkArgs(abi, networkArgs, mockDomains), {
deployDomainIDs: [BigInt(1), BigInt(6)],
constructorArgs: [
"0x0000000000000000000000000000000000000000000000000000000000000005",
"0x0000000000000000000000000000000000000000000000000000000000000005",
],
initDatas: [new Uint8Array(), new Uint8Array()],
});
});

it("Return current encoded data, with init data", function () {
const networkArgs: NetworkArguments<typeof abi> = {
goerli: {
args: [5],
initData: { initMethodName: "setName", initMethodArgs: ["chain"] },
},
holesky: {
args: [5],
initData: { initMethodName: "setName", initMethodArgs: ["safe"] },
},
};

assert.deepEqual(mapNetworkArgs(abi, networkArgs, mockDomains), {
deployDomainIDs: [BigInt(1), BigInt(6)],
constructorArgs: [
"0x0000000000000000000000000000000000000000000000000000000000000005",
"0x0000000000000000000000000000000000000000000000000000000000000005",
],
initDatas: [
"0xc47f002700000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000005636861696e000000000000000000000000000000000000000000000000000000",
"0xc47f0027000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000047361666500000000000000000000000000000000000000000000000000000000",
],
});
});

it("Return current encoded data, with mixed init data", function () {
const networkArgs: NetworkArguments<typeof abi> = {
goerli: { args: [5] },
holesky: {
args: [5],
initData: { initMethodName: "setName", initMethodArgs: ["safe"] },
},
};

assert.deepEqual(mapNetworkArgs(abi, networkArgs, mockDomains), {
deployDomainIDs: [BigInt(1), BigInt(6)],
constructorArgs: [
"0x0000000000000000000000000000000000000000000000000000000000000005",
"0x0000000000000000000000000000000000000000000000000000000000000005",
],
initDatas: [
new Uint8Array(),
"0xc47f0027000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000047361666500000000000000000000000000000000000000000000000000000000",
],
});
});

it("Should fail on missing domain", function () {
const networkArgs: NetworkArguments<typeof abi> = {
sepolia: {
args: [5],
initData: { initMethodName: "setName", initMethodArgs: ["chain"] },
},
goreli: {
args: [5],
initData: { initMethodName: "setName", initMethodArgs: ["safe"] },
},
};

assert.throw(
() => {
mapNetworkArgs(abi, networkArgs, mockDomains);
},
HardhatPluginError,
"Unavailable Networks in networkArgs"
);
});

it("Should fail on missing wrong initMethodName", function () {
const networkArgs: NetworkArguments<typeof abi> = {
sepolia: {
args: [5],
initData: {
initMethodName: "setLevel" as any /* Hack to bypass type-check */,
initMethodArgs: ["22"],
},
},
goreli: {
args: [5],
initData: { initMethodName: "setName", initMethodArgs: ["safe"] },
},
};

assert.throw(
() => {
mapNetworkArgs(abi, networkArgs, mockDomains);
},
HardhatPluginError,
"InitMethod setLevel not foud in ABI"
);
});
});
});
Loading
Loading