Skip to content

Commit

Permalink
chore: add anvil check for funding
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed May 29, 2024
1 parent 4c8577a commit 0b92554
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
23 changes: 3 additions & 20 deletions cypress/scripts/anvil.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,14 @@
import { spawnSync } from "child_process";
import { useHandler } from "../../static/scripts/rewards/web3/use-rpc-handler";
// @ts-expect-error - Missing types
import { RPCHandler } from "@ubiquity-dao/rpc-handler";

function useHandler(networkId: number) {
const config = {
networkId: networkId,
autoStorage: false,
cacheRefreshCycles: 5,
rpcTimeout: 5000,
networkName: null,
runtimeRpcs: null,
networkRpcs: null,
};

// No RPCs are tested at this point
return new RPCHandler(config);
}

class Anvil {
rpcs: string[] = [];
rpcHandler: RPCHandler;

constructor() {
this.rpcHandler = useHandler(100);
}
rpcHandler: RPCHandler | null = null;

async init() {
this.rpcHandler = await useHandler(100);
console.log(`[RPCHandler] Fetching RPCs...`);
await this.rpcHandler.testRpcPerformance();
const latencies: Record<string, number> = this.rpcHandler.getLatencies();
Expand Down
34 changes: 33 additions & 1 deletion cypress/scripts/funding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class TestFunder {
const steps = {
impersonate: async () => await this._impersonateAccount(this.whale),
approveFunding: async () => await this._approvePayload(this.fundingWallet),
approveBeneficiary: async () => await this._approvePayload(this.beneficiary),
transfer: async () => await this._transferPayload(),
};

Expand Down Expand Up @@ -217,6 +216,24 @@ class TestFunder {
return clear.status === 0;
}

async pingAnvil() {
try {
const resp = await fetch("http://localhost:8545", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ method: "eth_blockNumber", params: [], id: 1, jsonrpc: "2.0" }),
});

const { result } = await resp.json();

if (parseInt(result) > 0) {
return true;
}
} catch {
//
}
return false;
}
loader() {
const steps = ["|", "/", "-", "\\"];
let i = 0;
Expand All @@ -229,8 +246,23 @@ class TestFunder {

async function main() {
const funder = new TestFunder();
let isAnvilReady = false;
let retries = 5;

while (!isAnvilReady && retries > 0) {
isAnvilReady = await funder.pingAnvil();
retries--;
console.log(`Waiting for Anvil to ready up...`);
await new Promise((resolve) => setTimeout(resolve, 5000));

if (retries === 0) {
throw new Error(`Could not connect to Anvil`);
}
}

await funder.execute();
}

main()
.catch((error) => {
console.error(error);
Expand Down
2 changes: 1 addition & 1 deletion static/scripts/rewards/web3/use-rpc-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RPCHandler } from "@ubiquity-dao/rpc-handler";
import { AppState } from "../app-state";
import { ethers } from "ethers";

async function useHandler(networkId: number) {
export async function useHandler(networkId: number) {
const config = {
networkId: networkId,
autoStorage: true,
Expand Down

0 comments on commit 0b92554

Please sign in to comment.