Skip to content

Commit

Permalink
chore: optimize and eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed May 29, 2024
1 parent b584761 commit 4c8577a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 59 deletions.
2 changes: 2 additions & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"useGitignore": true,
"language": "en",
"words": [
"funder",
"Funder",
"binkey",
"binsec",
"chainlist",
Expand Down
1 change: 1 addition & 0 deletions cypress/scripts/anvil.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { spawnSync } from "child_process";
// @ts-expect-error - Missing types
import { RPCHandler } from "@ubiquity-dao/rpc-handler";

function useHandler(networkId: number) {
Expand Down
95 changes: 38 additions & 57 deletions cypress/scripts/funding.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable sonarjs/no-duplicate-string */
import { spawnSync } from "child_process";
import { SpawnSyncOptionsWithStringEncoding, spawnSync } from "child_process";

/**
* Handles the async funding of the testing environment
Expand All @@ -23,62 +23,31 @@ class TestFunder {
balance: "337888400000000000000000",
};

loader() {
const steps = ["|", "/", "-", "\\"];
let i = 0;
return setInterval(() => {
process.stdout.write(`\r${steps[i++]}`);
i = i % steps.length;
}, 100);
}

async execute() {
const loader = this.loader();

let isMoving = true;

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(),
};

while (isMoving) {
console.log(`Attempting to fund the testing environment`);
if (!(await this._impersonateAccount(this.whale))) {
console.log(`Failed to impersonate account -> retrying...`);
const isSuccess = await this.retry(() => this._impersonateAccount(this.whale));

if (!isSuccess) {
console.log(`Failed to impersonate account -> exiting...`);
isMoving = false;
}
}

console.log(`Approving for funding wallet`);
if (!(await this._approvePayload(this.fundingWallet))) {
console.log(`Failed to approve funding wallet -> retrying...`);
const isSuccess = await this.retry(() => this._approvePayload(this.fundingWallet));

if (!isSuccess) {
console.log(`Failed to approve funding wallet -> exiting...`);
isMoving = false;
}
}

console.log(`Approving for beneficiary wallet`);
if (!(await this._approvePayload(this.beneficiary))) {
console.log(`Failed to approve beneficiary wallet -> retrying...`);
const isSuccess = await this.retry(() => this._approvePayload(this.beneficiary));
for (const [step, fn] of Object.entries(steps)) {
console.log(`Running step: ${step}`);
if (!(await fn())) {
console.log(`Failed to run step: ${step} -> retrying...`);
const isSuccess = await this.retry(fn);

if (!isSuccess) {
console.log(`Failed to approve beneficiary wallet -> exiting...`);
isMoving = false;
}
}

console.log(`Transferring funds to funding wallet`);
if (!(await this._transferPayload())) {
console.log(`Failed to transfer funds to funding wallet -> retrying...`);
const isSuccess = await this.retry(() => this._transferPayload());

if (!isSuccess) {
console.log(`Failed to transfer funds to funding wallet -> exiting...`);
isMoving = false;
if (!isSuccess) {
console.log(`Failed to run step: ${step} -> exiting...`);
isMoving = false;
}
}
}

Expand All @@ -92,7 +61,7 @@ class TestFunder {
clearInterval(loader);
}

async retry(fn: () => Promise<any>, retries = 5) {
async retry(fn: () => Promise<boolean>, retries = 5) {
let i = 0;
let isSuccess = false;
while (i < retries) {
Expand Down Expand Up @@ -120,11 +89,11 @@ class TestFunder {
throw new Error("Balance is not set correctly");
}

console.log(`Funding wallet is ready for testing`);
console.log(`Allowance: ${allowance}\n Balance: ${balance}`);
console.log(`Funding wallet is ready for testing\n`);
console.log(`Allowance: ${allowance}\nBalance: ${balance}`);
}

private async _exec(payload: { command: string; args: string[]; options: any }) {
private async _exec(payload: { command: string; args: string[]; options: SpawnSyncOptionsWithStringEncoding }) {
const { command, args, options } = payload;
const result = spawnSync(command, args, options);
if (result.error) {
Expand All @@ -138,7 +107,7 @@ class TestFunder {
const impersonate = await this._exec({
command: "cast",
args: ["rpc", "--rpc-url", this.anvilRPC, "anvil_impersonateAccount", address],
options: { stdio: "inherit" },
options: { encoding: "utf8", stdio: "inherit" },
});

await new Promise((resolve) => setTimeout(resolve, 2000));
Expand All @@ -155,6 +124,7 @@ class TestFunder {

return allowance.stdout;
}

private async _fundingBalanceCheck() {
const balance = await this._exec({
command: "cast",
Expand All @@ -164,6 +134,7 @@ class TestFunder {

return balance.stdout;
}

private async _approvePayload(address: string) {
const approve = await this._exec({
command: "cast",
Expand All @@ -179,13 +150,14 @@ class TestFunder {
this.permit2,
this.expected.allowance,
],
options: { stdio: "inherit" },
options: { encoding: "utf8", stdio: "inherit" },
});

await new Promise((resolve) => setTimeout(resolve, 2000));

return approve.status === 0;
}

private async _transferPayload() {
const balance = parseInt(await this._fundingBalanceCheck());
const expected = parseInt(this.expected.balance);
Expand All @@ -210,7 +182,7 @@ class TestFunder {
this.fundingWallet,
this.expected.balance,
],
options: { stdio: "inherit" },
options: { encoding: "utf8", stdio: "inherit" },
});

await new Promise((resolve) => setTimeout(resolve, 2000));
Expand All @@ -219,7 +191,7 @@ class TestFunder {
}

private async _clearBalance() {
console.log(`Funder was overfunded, clearing excess funds`);
console.log(`Funder was over-funded, clearing excess funds`);
const balance = parseInt(await this._fundingBalanceCheck());
const difference = BigInt(balance - parseInt(this.expected.balance));

Expand All @@ -237,13 +209,22 @@ class TestFunder {
this.whale,
difference.toString(),
],
options: { stdio: "inherit" },
options: { encoding: "utf8", stdio: "inherit" },
});

await new Promise((resolve) => setTimeout(resolve, 2000));

return clear.status === 0;
}

loader() {
const steps = ["|", "/", "-", "\\"];
let i = 0;
return setInterval(() => {
process.stdout.write(`\r${steps[i++]}`);
i = i % steps.length;
}, 100);
}
}

async function main() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"test:start": "yarn start",
"test:run": "cypress run",
"test:open": "cypress open",
"test:fund": "cast rpc --rpc-url http://127.0.0.1:8545 anvil_impersonateAccount 0xba12222222228d8ba445958a75a0704d566bf2c8 & cast send --rpc-url http://127.0.0.1:8545 0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d --unlocked --from 0xba12222222228d8ba445958a75a0704d566bf2c8 \"transfer(address,uint256)(bool)\" 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 337888400000000000000000 & cast send --rpc-url http://127.0.0.1:8545 0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d --unlocked --from 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 \"approve(address,uint256)(bool)\" 0x000000000022D473030F116dDEE9F6B43aC78BA3 9999999999999991111111119999999999999999 & cast send --rpc-url http://127.0.0.1:8545 0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d --unlocked --from 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 \"approve(address,uint256)(bool)\" 0x000000000022D473030F116dDEE9F6B43aC78BA3 999999999999999111119999999999999999 & cast send --rpc-url http://127.0.0.1:8545 0xfB9124a8Bd01c250942de7beeE1E50aB9Ce36493 --unlocked --from 0xba12222222228d8ba445958a75a0704d566bf2c8 --value 0.1ether"
"test:fund": "tsx cypress/scripts/funding.ts"
},
"keywords": [
"typescript",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { createClient } from "@supabase/supabase-js";
import { decodePermits } from "@ubiquibot/permit-generation/handlers";
import { Permit } from "@ubiquibot/permit-generation/types";
import { app, AppState } from "../app-state";
import { useFastestRpc } from "../rpc-optimization/get-optimal-provider";
import { toaster } from "../toaster";
import { connectWallet } from "../web3/connect-wallet";
import { checkRenderInvalidatePermitAdminControl, checkRenderMakeClaimControl } from "../web3/erc20-permit";
Expand Down

0 comments on commit 4c8577a

Please sign in to comment.