Skip to content

Commit

Permalink
feat: rpc upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Mar 15, 2024
1 parent da14f1b commit 4b7d4bc
Show file tree
Hide file tree
Showing 21 changed files with 70 additions and 221 deletions.
3 changes: 2 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"URLSAFE",
"WXDAI",
"XDAI",
"xmark"
"xmark",
"keyrxng"
],
"dictionaries": ["typescript", "node", "software-terms", "html"],
"import": ["@cspell/dict-typescript/cspell-ext.json", "@cspell/dict-node/cspell-ext.json", "@cspell/dict-software-terms"],
Expand Down
5 changes: 1 addition & 4 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@
branch = v1.4.0
[submodule "lib/permit2"]
path = lib/permit2
url = https://github.com/Uniswap/permit2
[submodule "lib/chainlist"]
path = lib/chainlist
url = https://github.com/DefiLlama/chainlist.git
url = https://github.com/Uniswap/permit2
11 changes: 0 additions & 11 deletions build/esbuild-build.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import { execSync } from "child_process";
import { config } from "dotenv";
import esbuild from "esbuild";
import extraRpcs from "../lib/chainlist/constants/extraRpcs";
const typescriptEntries = ["static/scripts/rewards/init.ts"];
export const entries = [...typescriptEntries];

const allNetworkUrls: Record<string, string[]> = {};
// this flattens all the rpcs into a single object, with key names that match the networkIds. The arrays are just of URLs per network ID.

Object.keys(extraRpcs).forEach((networkId) => {
const officialUrls = extraRpcs[networkId].rpcs.filter((rpc) => typeof rpc === "string");
const extraUrls: string[] = extraRpcs[networkId].rpcs.filter((rpc) => rpc.url !== undefined).map((rpc) => rpc.url);
allNetworkUrls[networkId] = [...officialUrls, ...extraUrls];
});

export const esBuildContext: esbuild.BuildOptions = {
sourcemap: true,
entryPoints: entries,
Expand All @@ -29,7 +19,6 @@ export const esBuildContext: esbuild.BuildOptions = {
},
outdir: "static/out",
define: createEnvDefines(["SUPABASE_URL", "SUPABASE_ANON_KEY"], {
extraRpcs: allNetworkUrls,
commitHash: execSync(`git rev-parse --short HEAD`).toString().trim(),
}),
};
Expand Down
1 change: 0 additions & 1 deletion lib/chainlist
Submodule chainlist deleted from 805928
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
],
"dependencies": {
"@ethersproject/providers": "^5.7.2",
"@keyrxng/rpc-handler": "^1.0.1",
"@octokit/core": "^5.1.0",
"@octokit/plugin-create-or-update-text-file": "^4.0.1",
"@octokit/plugin-throttling": "^8.1.3",
Expand Down
2 changes: 1 addition & 1 deletion static/scripts/rewards/app-state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JsonRpcProvider } from "@ethersproject/providers";
import { networkExplorers } from "./constants";
import { networkExplorers } from "@keyrxng/rpc-handler/dist/esm/src/constants";
import { RewardPermit } from "./render-transaction/tx-type";

export class AppState {
Expand Down
16 changes: 9 additions & 7 deletions static/scripts/rewards/cirip/ens-lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ import { ethers } from "ethers";
import abi from "../abis/cirip.json";
import { fetchEns } from "./fetch-ens";
import { queryReverseEns } from "./query-reverse-ens";
import { RPCHandler } from "@keyrxng/rpc-handler/dist/esm/src";

export const UBIQUITY_RPC_ENDPOINT = "https://rpc-pay.ubq.fi/v1/mainnet";
// export const UBIQUITY_RPC_ENDPOINT = "https://rpc-pay.ubq.fi/v1/mainnet";
export const reverseEnsInterface = new ethers.utils.Interface(abi);

// addEventListener("fetch", event => {
// event.respondWith(handleRequest(event.request).catch(err => new Response(err.stack, { status: 500 })));
// });

export async function ensLookup(addr: string) {
export async function ensLookup(addr: string, handler: RPCHandler) {
const _address = "/".concat(addr); // quick adapter

// try {
Expand All @@ -22,12 +23,13 @@ export async function ensLookup(addr: string) {
const address = _address.substring(start + 1, start + 43).toLowerCase();

let reverseRecord = null as null | string;
// let response = "";
try {
reverseRecord = await queryReverseEns(address);
const responseParsed = JSON.parse(reverseRecord).result;
const _reverseRecord = ethers.utils.defaultAbiCoder.decode([ethers.utils.ParamType.from("string[]")], responseParsed);
reverseRecord = _reverseRecord[0][0];
reverseRecord = await queryReverseEns(address, handler);
if (reverseRecord !== undefined) {
const responseParsed = JSON.parse(reverseRecord).result;
const _reverseRecord = ethers.utils.defaultAbiCoder.decode([ethers.utils.ParamType.from("string[]")], responseParsed);
reverseRecord = _reverseRecord[0][0];
}
} catch (e) {
console.error(e);
// throw "Error contacting ethereum node. \nCause: '" + e + "'. \nResponse: " + response;
Expand Down
26 changes: 10 additions & 16 deletions static/scripts/rewards/cirip/query-reverse-ens.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { reverseEnsInterface, UBIQUITY_RPC_ENDPOINT } from "./ens-lookup";
import { RPCHandler } from "@keyrxng/rpc-handler/dist/esm/src";
import { reverseEnsInterface } from "./ens-lookup";

export async function queryReverseEns(address: string) {
export async function queryReverseEns(address: string, handler: RPCHandler) {
// Try to get the ENS name from localStorage
const cachedEnsName = localStorage.getItem(address);

Expand All @@ -11,22 +12,15 @@ export async function queryReverseEns(address: string) {
// If the ENS name is not in localStorage, fetch it from the API
const data = reverseEnsInterface.encodeFunctionData("getNames", [[address.substring(2)]]);

const response = await fetch(UBIQUITY_RPC_ENDPOINT, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
jsonrpc: "2.0",
id: "1",
method: "eth_call",
params: [{ to: "0x3671aE578E63FdF66ad4F3E12CC0c0d71Ac7510C", data: data }, "latest"],
}),
});
const provider = handler.getProvider();

const ensName = await response.text();
const ensName = await provider.send("eth_call", [{ to: "0x3671aE578E63FdF66ad4F3E12CC0c0d71Ac7510C", data: data }, "latest"]);

// Store the ENS name in localStorage for future use
if (ensName === "0x") {
return;
}

// Save the ENS name to localStorage
localStorage.setItem(address, ensName);

return ensName;
Expand Down
56 changes: 0 additions & 56 deletions static/scripts/rewards/constants.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { Type } from "@sinclair/typebox";
import { Value } from "@sinclair/typebox/value";
import { createClient } from "@supabase/supabase-js";
import { AppState, app } from "../app-state";
import { useFastestRpc } from "../rpc-optimization/get-optimal-provider";
import { buttonController, toaster } from "../toaster";
import { connectWallet } from "../web3/connect-wallet";
import { checkRenderInvalidatePermitAdminControl, checkRenderMakeClaimControl } from "../web3/erc20-permit";
Expand All @@ -11,7 +10,7 @@ import { claimRewardsPagination } from "./claim-rewards-pagination";
import { renderTransaction } from "./render-transaction";
import { setClaimMessage } from "./set-claim-message";
import { RewardPermit, claimTxT } from "./tx-type";

import { useRpcHandler } from "../web3/use-rpc-handler";
declare const SUPABASE_URL: string;
declare const SUPABASE_ANON_KEY: string;

Expand All @@ -31,7 +30,11 @@ export async function readClaimDataFromUrl(app: AppState) {

app.claims = decodeClaimData(base64encodedTxData).flat();
app.claimTxs = await getClaimedTxs(app);
app.provider = await useFastestRpc(app);

const handler = await useRpcHandler(app);

app.provider = handler.getProvider();
console.log("app.provider", app.provider);
if (window.ethereum) {
app.signer = await connectWallet().catch(console.error);
window.ethereum.on("accountsChanged", () => {
Expand Down
12 changes: 9 additions & 3 deletions static/scripts/rewards/render-transaction/render-ens-name.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
import { app } from "../app-state";
import { AppState, app } from "../app-state";
import { ensLookup } from "../cirip/ens-lookup";
import { useRpcHandler } from "../web3/use-rpc-handler";

type EnsParams =
| {
element: Element;
address: string;
tokenAddress: string;
tokenView: true;
networkId: number;
}
| {
element: Element;
address: string;
networkId: number;
tokenAddress?: undefined;
tokenView?: false;
};

export async function renderEnsName({ element, address, tokenAddress, tokenView }: EnsParams): Promise<void> {
export async function renderEnsName({ element, address, tokenAddress, tokenView, networkId }: EnsParams): Promise<void> {
let href: string = "";

const handler = await useRpcHandler({ networkId } as AppState);

try {
const resolved = await ensLookup(address);
const resolved = await ensLookup(address, handler);
let ensName: undefined | string;
if (resolved.reverseRecord) {
ensName = resolved.reverseRecord;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { app } from "../app-state";
import { networkExplorers } from "../constants";
import { networkExplorers } from "@keyrxng/rpc-handler/dist/esm/src/constants";
import { buttonController, getMakeClaimButton, viewClaimButton } from "../toaster";
import { checkRenderInvalidatePermitAdminControl, claimErc20PermitHandlerWrapper, fetchTreasury } from "../web3/erc20-permit";
import { claimErc721PermitHandler } from "../web3/erc721-permit";
Expand Down Expand Up @@ -44,7 +44,7 @@ export async function renderTransaction(): Promise<Success> {
}).catch(console.error);

const toElement = document.getElementById(`rewardRecipient`) as Element;
renderEnsName({ element: toElement, address: app.reward.transferDetails.to }).catch(console.error);
renderEnsName({ element: toElement, address: app.reward.transferDetails.to, networkId: app.networkId ?? app.reward.networkId }).catch(console.error);

if (app.provider) {
checkRenderInvalidatePermitAdminControl(app).catch(console.error);
Expand All @@ -71,7 +71,7 @@ export async function renderTransaction(): Promise<Success> {
}).catch(console.error);

const toElement = document.getElementById(`rewardRecipient`) as Element;
renderEnsName({ element: toElement, address: app.reward.transferDetails.to }).catch(console.error);
renderEnsName({ element: toElement, address: app.reward.transferDetails.to, networkId: app.networkId ?? app.reward.networkId }).catch(console.error);

getMakeClaimButton().addEventListener("click", claimErc721PermitHandler(app.reward));
}
Expand Down

This file was deleted.

26 changes: 0 additions & 26 deletions static/scripts/rewards/rpc-optimization/get-optimal-provider.ts

This file was deleted.

Loading

0 comments on commit 4b7d4bc

Please sign in to comment.