Skip to content

Commit

Permalink
chore: stress test wallet rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Jun 7, 2024
1 parent ac525e4 commit 0848083
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
57 changes: 57 additions & 0 deletions static/scripts/rewards/web3/connect-wallet.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { JsonRpcSigner } from "@ethersproject/providers";
import { ethers } from "ethers";
import { buttonController, toaster } from "../toaster";
import { app } from "../app-state";

export async function connectWallet(): Promise<JsonRpcSigner | null> {
try {
Expand All @@ -12,18 +13,74 @@ export async function connectWallet(): Promise<JsonRpcSigner | null> {

const address = await signer.getAddress();

const provider = new ethers.providers.JsonRpcProvider(wallet.provider.url);

if (!address) {
buttonController.hideAll();
console.error("Wallet not connected");
return null;
}

const isOkay = await stressTestWalletRpc(provider);

if (!isOkay) {
toaster.create("error", "We have detected potential issues with your in-wallet RPC. Accept the request to replace it with a more reliable one.");
await addFastestHandlerNetwork();
return null;
}

return signer;
} catch (error: unknown) {
return connectErrorHandler(error);
}
}

async function addFastestHandlerNetwork() {
const provider = await useRpcHandler(app.networkId ?? provider.network.chainId);
const url = provider.connection.url;

try {
await provider.request({
method: "wallet_addEthereumChain",
params: [
{
chainId: app.networkId,
chainName: url,
rpcUrls: [url],
},
],
});
} catch (error) {
console.error(error);
toaster.create("error"`Failed to add optimal RPC network. Please add it manually. Network ID: ${app.networkId}, URL: ${url}`);
}
}

async function stressTestWalletRpc(provider: ethers.providers.Web3Provider) {
const success = [];

for (let i = 0; i < 10; i++) {
success.push(await testEthCall(provider));
}

return success.filter((s) => s === "0x" + "00".repeat(32)).length > 9;
}

async function testEthCall(provider: ethers.providers.Web3Provider) {
try {
return await provider.send("eth_call", [
{
to: "0x000000000022D473030F116dDEE9F6B43aC78BA3",
input: "0x4fe02b44000000000000000000000000d9530f3fbbea11bed01dc09e79318f2f20223716001fd097bcb5a1759ce02c0a671386a0bbbfa8216559e5855698a9d4de4cddea",
},
"latest",
]);
} catch (er) {
console.log(er);
return false;
}
}

function connectErrorHandler(error: unknown) {
if (error instanceof Error) {
console.error(error);
Expand Down
3 changes: 1 addition & 2 deletions static/scripts/rewards/web3/use-rpc-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { RPCHandler } from "@ubiquity-dao/rpc-handler";
import { AppState } from "../app-state";
import { ethers } from "ethers";

export async function useHandler(networkId: number) {
const config = {
Expand Down Expand Up @@ -29,5 +28,5 @@ export async function useRpcHandler(app: AppState) {
if (!url) {
throw new Error("Provider URL not set");
}
return new ethers.providers.JsonRpcProvider(provider.connection.url);
return provider;
}

0 comments on commit 0848083

Please sign in to comment.