Skip to content

Commit

Permalink
feat: scripts use infura key rpcs if available (#2037)
Browse files Browse the repository at this point in the history
  • Loading branch information
douglance authored Nov 1, 2024
1 parent 0d66fd7 commit dc2a202
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 27 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/add-orbit-chain.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,5 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ISSUE_NUMBER: ${{ github.event.issue.number || inputs.issue_number }}
NEXT_PUBLIC_INFURA_KEY: ${{ secrets.NEXT_PUBLIC_INFURA_KEY }}
NEXT_PUBLIC_INFURA_KEY_HOLESKY: ${{ secrets.NEXT_PUBLIC_INFURA_KEY_HOLESKY }}
1 change: 1 addition & 0 deletions packages/arb-token-bridge-ui/.env.local.sample
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ NEXT_PUBLIC_INFURA_KEY=
NEXT_PUBLIC_INFURA_KEY_ETHEREUM=
# L1 Testnet
NEXT_PUBLIC_INFURA_KEY_SEPOLIA=
NEXT_PUBLIC_INFURA_KEY_HOLESKY=

# L2
NEXT_PUBLIC_INFURA_KEY_ARBITRUM_ONE=
Expand Down
1 change: 1 addition & 0 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"@vitest/coverage-v8": "^0.34.0",
"dotenv": "^16.4.5",
"eslint": "^8.0.0",
"typescript": "^5.0.0",
"vite": "^5.4.7",
Expand Down
28 changes: 28 additions & 0 deletions packages/scripts/src/addOrbitChain/provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { StaticJsonRpcProvider } from "@ethersproject/providers";
import { ConnectionInfo } from "ethers/lib/utils";

export const getProvider = (chainInfo: {
rpcUrl: string;
name: string;
chainId: number;
}) => {
const connection: ConnectionInfo = {
url: chainInfo.rpcUrl,
timeout: 30000,
allowGzip: true,
skipFetchSetup: true,
throttleLimit: 3,
throttleSlotInterval: 1000,
headers: {
Accept: "*/*",
"Accept-Encoding": "gzip, deflate, br",
},
};

const provider = new StaticJsonRpcProvider(connection, {
name: chainInfo.name,
chainId: chainInfo.chainId,
});

return provider;
};
64 changes: 41 additions & 23 deletions packages/scripts/src/addOrbitChain/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,64 @@
import { z } from "zod";
import { constants, ethers } from "ethers";
import { constants } from "ethers";
import { warning } from "@actions/core";
import { getOctokit } from "@actions/github";
import path from "path";
import * as dotenv from "dotenv";
import { getProvider } from "./provider";

// Load .env from the UI project directory
dotenv.config({
path: path.resolve(__dirname, "../../../arb-token-bridge-ui/.env"),
});
export const TESTNET_PARENT_CHAIN_IDS = [11155111, 421614, 17000, 84532];
const ZERO_ADDRESS = constants.AddressZero;

export const getParentChainInfo = (parentChainId: number) => {
const INFURA_KEY = process.env.NEXT_PUBLIC_INFURA_KEY;
const HOLESKY_INFURA_KEY = process.env.NEXT_PUBLIC_INFURA_KEY_HOLESKY;

switch (parentChainId) {
case 1: // Ethereum Mainnet
return {
rpcUrl: "https://eth.llamarpc.com",
rpcUrl: INFURA_KEY
? `https://mainnet.infura.io/v3/${INFURA_KEY}`
: "https://eth.llamarpc.com",
blockExplorer: "https://etherscan.io",
chainId: 1,
name: "Ethereum",
};
case 42161: // Arbitrum One
return {
rpcUrl: "https://arb1.arbitrum.io/rpc",
rpcUrl: INFURA_KEY
? `https://arbitrum-mainnet.infura.io/v3/${INFURA_KEY}`
: "https://arb1.arbitrum.io/rpc",
blockExplorer: "https://arbiscan.io",
chainId: 42161,
name: "Arbitrum One",
};
case 11155111: // Sepolia
return {
rpcUrl: "https://ethereum-sepolia-rpc.publicnode.com",
rpcUrl: INFURA_KEY
? `https://sepolia.infura.io/v3/${INFURA_KEY}`
: "https://ethereum-sepolia-rpc.publicnode.com",
blockExplorer: "https://sepolia.etherscan.io",
chainId: 11155111,
name: "Sepolia",
};
case 421614: // Arbitrum Sepolia
return {
rpcUrl: "https://sepolia-rollup.arbitrum.io/rpc",
rpcUrl: INFURA_KEY
? `https://arbitrum-sepolia.infura.io/v3/${INFURA_KEY}`
: "https://sepolia-rollup.arbitrum.io/rpc",
blockExplorer: "https://sepolia.arbiscan.io",
chainId: 421614,
name: "Arbitrum Sepolia",
};
case 17000: // Holesky
return {
rpcUrl: "https://ethereum-holesky-rpc.publicnode.com",
rpcUrl: HOLESKY_INFURA_KEY
? `https://holesky.infura.io/v3/${HOLESKY_INFURA_KEY}`
: "https://ethereum-holesky-rpc.publicnode.com",
blockExplorer: "https://holesky.etherscan.io/",
chainId: 17000,
name: "Holesky",
Expand Down Expand Up @@ -188,30 +209,27 @@ export const chainSchema = z
chainId: number,
chainName: string
) => {
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
const provider = getProvider({ rpcUrl, name: chainName, chainId });

for (const address of addresses) {
try {
const code = await provider.getCode(address);
if (code === "0x") {
const explorerLink = `${blockExplorer}/address/${address}`;
console.warn(
`Address ${address} on ${chainName} (chainId: ${chainId}) is not a contract. Verify manually: ${explorerLink}`
);
// TODO: Uncomment this when we can verify all contracts
// ctx.addIssue({
// code: z.ZodIssueCode.custom,
// message: `Address at ${address} is not a contract on ${chainName}. Verify manually: ${explorerLink}`,
// });
throw new Error("Address is not a contract");
}
} catch (error) {
const explorerLink = `${blockExplorer}/address/${address}`;
console.log(
`Error checking contract at ${address} on ${chainName} (chainId: ${chainId}). Verify manually: ${explorerLink}`
);
// ctx.addIssue({
// code: z.ZodIssueCode.custom,
// message: `Error checking contract at ${address} on ${chainName}. Verify manually: ${explorerLink}`,
// });
const warningMsg = `
Failed to verify contract at ${address} on ${chainName}
Please verify contract manually by visiting ${explorerLink}`;
console.error(warningMsg + `\n\n==================\n\n${error}`);
warning(warningMsg);

ctx.addIssue({
code: z.ZodIssueCode.custom,
message: warningMsg,
});
}
}
};
Expand Down
5 changes: 5 additions & 0 deletions packages/scripts/src/addOrbitChain/tests/setup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import dotenv from "dotenv";

// Load environment variables for tests
dotenv.config();

// Set GITHUB_TOKEN to "test" if it's not already set
const githubToken = process.env.GITHUB_TOKEN || "test";
process.env.GITHUB_TOKEN = githubToken;
4 changes: 2 additions & 2 deletions packages/scripts/src/addOrbitChain/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import * as core from "@actions/core";
import { warning } from "@actions/core";
import { getArbitrumNetworkInformationFromRollup } from "@arbitrum/sdk";
import { JsonRpcProvider } from "@ethersproject/providers";
import axios from "axios";
import { fileTypeFromBuffer } from "file-type";
import * as fs from "fs";
Expand Down Expand Up @@ -32,6 +31,7 @@ import {
validateOrbitChain,
validateOrbitChainsList,
} from "./schemas";
import { getProvider } from "./provider";

const SUPPORTED_IMAGE_EXTENSIONS = ["png", "svg", "jpg", "jpeg", "webp"];
const MAX_IMAGE_SIZE_KB = 100;
Expand Down Expand Up @@ -367,7 +367,7 @@ export const transformIncomingDataToOrbitChain = async (
const parentChainId = parseInt(chainData.parentChainId, 10);
const isTestnet = TESTNET_PARENT_CHAIN_IDS.includes(parentChainId);
const parentChainInfo = getParentChainInfo(parentChainId);
const provider = new JsonRpcProvider(parentChainInfo.rpcUrl);
const provider = getProvider(parentChainInfo);
const rollupData = await getArbitrumNetworkInformationFromRollup(
chainData.rollup,
provider
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6300,9 +6300,9 @@ dotenv-parse-variables@^2.0.0:
debug "^4.3.1"
is-string-and-not-blank "^0.0.2"

dotenv@^16.3.1:
dotenv@^16.3.1, dotenv@^16.4.5:
version "16.4.5"
resolved "https://registry.npmjs.org/dotenv/-/dotenv-16.4.5.tgz"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.4.5.tgz#cdd3b3b604cb327e286b4762e13502f717cb099f"
integrity sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==

download@^8.0.0:
Expand Down

0 comments on commit dc2a202

Please sign in to comment.