Skip to content

Commit

Permalink
update tests #3 (mapNetworkArgs)
Browse files Browse the repository at this point in the history
  • Loading branch information
BeroBurny committed Jan 29, 2024
1 parent feefec7 commit 4f27b71
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 25 deletions.
14 changes: 0 additions & 14 deletions packages/plugin/test/MockHttpProvider.ts

This file was deleted.

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,
},
];
115 changes: 104 additions & 11 deletions packages/plugin/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import {
encodeInitData,
getConfigEnvironmentVariable,
getNetworkChainId,
mapNetworkArgs,
sumedFees,
} from "../src/utils";
import { createMockHttpProvider } from "./MockHttpProvider";
import { NetworkArguments } from "../src/types";
import { createMockHttpProvider, mockDomains } from "./mocks";
import { useEnvironment } from "./helpers";

use(chaiAsPromised);
Expand Down Expand Up @@ -66,7 +68,6 @@ describe("Unit tests for utils", function () {
});

describe("encodeInitData", function () {
useEnvironment("hardhat-project");
const { abi } = helloSygmaContract;

it("Return current encoded data for specified method", function () {
Expand All @@ -87,22 +88,114 @@ describe("Unit tests for utils", function () {
});

describe("mapNetworkArgs", function () {
useEnvironment("hardhat-project");
const { abi } = helloSygmaContract;

it("does works?", function () {
assert.deepEqual({}, {});
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("does works?", function () {
assert.deepEqual({}, {});
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("should fail?", function () {
assert.throw(() => {}, Error, "text");
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?", function () {
assert.throw(() => {}, Error, "text");
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"
);
});
});
});

0 comments on commit 4f27b71

Please sign in to comment.