Skip to content

Commit

Permalink
evm: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
panoel committed Dec 19, 2024
1 parent 497ba4a commit b74238f
Show file tree
Hide file tree
Showing 9 changed files with 3,484 additions and 305 deletions.
49 changes: 49 additions & 0 deletions evm/sdk/ts/src/platforms/evm/__tests__/admin.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { jest, expect, test } from "@jest/globals";
import { ethers } from "ethers";
import {
claimAdmin,
getAdmin,
getPendingAdmin,
transferAdmin,
} from "../src/admin";

jest.setTimeout(180000);

const fakeIntegrator = "0x1B5Ba8B47e656Afe522634ca7F058b2BE33075Af";
const nullAdmin = "0x0000000000000000000000000000000000000000";

const anvilPrivateKey =
"0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d";
const anvilEthProvider = new ethers.JsonRpcProvider("http://127.0.0.1:8545");
const anvilEthSigner = new ethers.Wallet(anvilPrivateKey, anvilEthProvider);
const whGuardiansAdapter = "0x8564C314028B778C968E11485E4bD6aC13CF0eeF";

describe("TS as Admin Tests", () => {
// Can only be run once per Anvil deployment.

describe("cancel transfer", () => {
test("getAdmin, getPendingAdmin, transferAdmin, claimAdmin", async () => {
const firstNonce = await anvilEthProvider.getTransactionCount(
anvilEthSigner.address,
);
console.log("Nonce before transferAdmin:", firstNonce);

await transferAdmin(whGuardiansAdapter, anvilEthSigner, fakeIntegrator);

let admin = await getAdmin(whGuardiansAdapter, anvilEthSigner);
let pendingAdmin = await getPendingAdmin(
whGuardiansAdapter,
anvilEthSigner,
);
expect(admin).toBe(anvilEthSigner.address);
expect(pendingAdmin).toBe(fakeIntegrator);

await claimAdmin(whGuardiansAdapter, anvilEthSigner);

admin = await getAdmin(whGuardiansAdapter, anvilEthSigner);
pendingAdmin = await getPendingAdmin(whGuardiansAdapter, anvilEthSigner);
expect(admin).toBe(anvilEthSigner.address);
expect(pendingAdmin).toBe(nullAdmin);
});
});
});
43 changes: 43 additions & 0 deletions evm/sdk/ts/src/platforms/evm/__tests__/peer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { jest, expect, test } from "@jest/globals";
import { ethers } from "ethers";
import { getPeer, getPeers, setPeer } from "../src/peer";

jest.setTimeout(180000);

const fakePeer =
"0x0000000000000000000000001B5Ba8B47e656Afe522634ca7F058b2BE33075Af".toLowerCase();
const nullPeer =
"0x0000000000000000000000000000000000000000000000000000000000000000";

const anvilPrivateKey =
"0x4f3edf983ac636a65a842ce7c78d9aa706d3b113bce9c46f30d7d21715b23b1d";
const anvilEthProvider = new ethers.JsonRpcProvider("http://127.0.0.1:8545");
const anvilEthSigner = new ethers.Wallet(anvilPrivateKey, anvilEthProvider);
const anvilWhGuardiansAdapter = "0x37DFeB004c7C76C8cE4954D1659A63ACd222d2Be";

describe("setPeer", () => {
test("setPeer, getPeer, getPeers", async () => {
const peerChain = 1;
let peer: string = await getPeer(
anvilWhGuardiansAdapter,
anvilEthSigner,
peerChain,
);
console.log("peer", peer);
expect(peer).toBe(nullPeer);

let peers = await getPeers(anvilWhGuardiansAdapter, anvilEthSigner);
console.log("peers", peers);
expect(peers.length).toBe(0);

await setPeer(anvilWhGuardiansAdapter, anvilEthSigner, peerChain, fakePeer);

peer = await getPeer(anvilWhGuardiansAdapter, anvilEthSigner, peerChain);
console.log("peer after set", peer);
expect(peer).toBe(fakePeer);

peers = await getPeers(anvilWhGuardiansAdapter, anvilEthSigner);
console.log("peers", peers);
expect(peers.length).toBe(1);
});
});
73 changes: 23 additions & 50 deletions evm/sdk/ts/src/platforms/evm/src/admin.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { ethers } from "ethers";
import {
WormholeGuardiansAdapter,
WormholeGuardiansAdapter__factory,
WormholeGuardiansAdapterWithExecutor,
WormholeGuardiansAdapterWithExecutor__factory,
} from "../../../abi";
import { getContract } from "./util";

export async function claimAdmin(
contractAddress: string,
signer: ethers.Signer,
): Promise<void> {
let contract = await getContract(contractAddress, signer);
const contract = await getContract(contractAddress, signer);
const tx = await contract.claimAdmin();
await tx.wait();
}
Expand All @@ -19,16 +14,32 @@ export async function discardAdmin(
contractAddress: string,
signer: ethers.Signer,
): Promise<void> {
let contract = await getContract(contractAddress, signer);
const contract = await getContract(contractAddress, signer);
const tx = await contract.discardAdmin();
await tx.wait();
}

export async function getAdmin(
contractAddress: string,
signer: ethers.Signer,
): Promise<string> {
const contract = await getContract(contractAddress, signer);
return await contract.admin();
}

export async function getPendingAdmin(
contractAddress: string,
signer: ethers.Signer,
): Promise<string> {
const contract = await getContract(contractAddress, signer);
return await contract.pendingAdmin();
}

export async function pendingAdmin(
contractAddress: string,
signer: ethers.Signer,
): Promise<string> {
let contract = await getContract(contractAddress, signer);
const contract = await getContract(contractAddress, signer);
return await contract.pendingAdmin();
}

Expand All @@ -37,7 +48,7 @@ export async function transferAdmin(
signer: ethers.Signer,
newAdmin: string,
): Promise<void> {
let contract = await getContract(contractAddress, signer);
const contract = await getContract(contractAddress, signer);
const tx = await contract.transferAdmin(newAdmin);
await tx.wait();
}
Expand All @@ -47,7 +58,7 @@ export async function updateAdmin(
signer: ethers.Signer,
newAdmin: string,
): Promise<void> {
let contract = await getContract(contractAddress, signer);
const contract = await getContract(contractAddress, signer);
const tx = await contract.updateAdmin(newAdmin);
await tx.wait();
}
Expand All @@ -56,44 +67,6 @@ export async function getAdapterType(
contractAddress: string,
signer: ethers.Signer,
): Promise<string> {
let contract = await getContract(contractAddress, signer);
const contract = await getContract(contractAddress, signer);
return await contract.getAdapterType();
}

export async function amIExecutor(
contractAddress: string,
signer: ethers.Signer,
): Promise<boolean> {
try {
const type: string = await getAdapterType(contractAddress, signer);
// TODO: See if this version string is somewhere in a const
if (type === "WormholeGuardiansAdapterWithExecutor-0.0.1") {
return true;
}
} catch (e) {
console.log("Error getting adapter type", e);
}
return false;
}

async function getContract(
contractAddress: string,
signer: ethers.Signer,
): Promise<WormholeGuardiansAdapter | WormholeGuardiansAdapterWithExecutor> {
try {
return WormholeGuardiansAdapterWithExecutor__factory.connect(
contractAddress,
signer,
);
} catch (e) {
console.log("Error connecting to contract with executor", e);
}
try {
return WormholeGuardiansAdapter__factory.connect(contractAddress, signer);
} catch (e) {
console.log("Error connecting to contract without executor", e);
}
throw new Error(
`Contract address ${contractAddress} is not a WormholeGuardiansAdapter or WormholeGuardiansAdapterWithExecutor contract`,
);
}
61 changes: 20 additions & 41 deletions evm/sdk/ts/src/platforms/evm/src/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,37 @@ import { ethers } from "ethers";
import {
IWormholeGuardiansAdapter,
WormholeGuardiansAdapter,
WormholeGuardiansAdapter__factory,
WormholeGuardiansAdapterWithExecutor,
WormholeGuardiansAdapterWithExecutor__factory,
} from "../../../abi";
import { getContract } from "./util";

export async function setPeer(
contractAddress: string,
signer: ethers.Signer,
peerChain: number,
peerContract: string,
withExecutor: boolean = true,
): Promise<void> {
let contract: WormholeGuardiansAdapter | WormholeGuardiansAdapterWithExecutor;
if (withExecutor) {
contract = WormholeGuardiansAdapterWithExecutor__factory.connect(
contractAddress,
signer,
);
} else {
contract = WormholeGuardiansAdapter__factory.connect(
contractAddress,
signer,
);
}
const contract:
| WormholeGuardiansAdapter
| WormholeGuardiansAdapterWithExecutor = await getContract(
contractAddress,
signer,
);

const tx = await contract.setPeer(peerChain, peerContract);
await tx.wait();
}

export async function getPeers(
contractAddress: string,
signer: ethers.Signer,
withExecutor: boolean = true,
): Promise<IWormholeGuardiansAdapter.PeerEntryStructOutput[]> {
let contract: WormholeGuardiansAdapter | WormholeGuardiansAdapterWithExecutor;
if (withExecutor) {
contract = WormholeGuardiansAdapterWithExecutor__factory.connect(
contractAddress,
signer,
);
} else {
contract = WormholeGuardiansAdapter__factory.connect(
contractAddress,
signer,
);
}
const contract:
| WormholeGuardiansAdapter
| WormholeGuardiansAdapterWithExecutor = await getContract(
contractAddress,
signer,
);
const result: IWormholeGuardiansAdapter.PeerEntryStructOutput[] =
await contract.getPeers();
return result;
Expand All @@ -56,20 +42,13 @@ export async function getPeer(
contractAddress: string,
signer: ethers.Signer,
peerChain: number,
withExecutor: boolean = true,
): Promise<string> {
let contract: WormholeGuardiansAdapter | WormholeGuardiansAdapterWithExecutor;
if (withExecutor) {
contract = WormholeGuardiansAdapterWithExecutor__factory.connect(
contractAddress,
signer,
);
} else {
contract = WormholeGuardiansAdapter__factory.connect(
contractAddress,
signer,
);
}
const contract:
| WormholeGuardiansAdapter
| WormholeGuardiansAdapterWithExecutor = await getContract(
contractAddress,
signer,
);
const result: string = await contract.getPeer(peerChain);
return result;
}
35 changes: 35 additions & 0 deletions evm/sdk/ts/src/platforms/evm/src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { ethers } from "ethers";
import {
WormholeGuardiansAdapter,
WormholeGuardiansAdapter__factory,
WormholeGuardiansAdapterWithExecutor,
WormholeGuardiansAdapterWithExecutor__factory,
} from "../../../abi";

export async function isExecutor(
contractAddress: string,
signer: ethers.Signer,
): Promise<boolean> {
const abiFragment = ["function executor() public view returns (address)"];
const contract = new ethers.Contract(contractAddress, abiFragment, signer);
try {
await contract.executor();
return true;
} catch {
return false;
}
}

export async function getContract(
contractAddress: string,
signer: ethers.Signer,
): Promise<WormholeGuardiansAdapter | WormholeGuardiansAdapterWithExecutor> {
if (await isExecutor(contractAddress, signer)) {
return WormholeGuardiansAdapterWithExecutor__factory.connect(
contractAddress,
signer,
);
} else {
return WormholeGuardiansAdapter__factory.connect(contractAddress, signer);
}
}
2 changes: 1 addition & 1 deletion evm/src/interfaces/IWormholeGuardiansAdapter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ interface IWormholeGuardiansAdapter is IAdapter {
error CallerNotAdmin(address caller);

/// @notice Error when an admin action is attempted while an admin transfer is pending.
/// @dev Selector: 9e78953d
/// @dev Selector: 0x9e78953d
error AdminTransferPending();

/// @notice Error when an attempt to claim the admin is made when there is no transfer pending.
Expand Down
9 changes: 9 additions & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"preset": "ts-jest/presets/default-esm",
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testMatch": ["**/__tests__/**/*.test.ts"],
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"],
"testPathIgnorePatterns": ["/node_modules/", "/dist/"]
}
Loading

0 comments on commit b74238f

Please sign in to comment.