From aaef3f0e0b0d488ffede277c3c3562ae5c487c32 Mon Sep 17 00:00:00 2001 From: Hayden Shively <17186559+haydenshively@users.noreply.github.com> Date: Fri, 26 Apr 2024 21:19:54 -0500 Subject: [PATCH] Add http transports for linea and scroll, may or may not work --- src/Constants.ts | 4 ++-- src/Contracts.ts | 43 +++++++++++++++++++++++++++++++++++++++++-- src/example.ts | 2 +- src/index.ts | 4 ++-- 4 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/Constants.ts b/src/Constants.ts index a02e3d7..9d874e9 100644 --- a/src/Constants.ts +++ b/src/Constants.ts @@ -1,4 +1,4 @@ -import { arbitrum, base, mainnet, optimism } from 'viem/chains'; +import { arbitrum, base, linea, mainnet, optimism, scroll } from 'viem/chains'; export const FACTORY_ADDRESS = '0x000000009efdB26b970bCc0085E126C9dfc16ee8'; @@ -6,7 +6,7 @@ export const BORROWER_LENS_ADDRESS = '0x267Fa142FA270F39738443b914FB7d3F95462451 export const LIQUIDATOR_ADDRESS = '0xC8eD78424824Ff7eA3602733909eC57c7d7F7301'; -export const aloeChains = [mainnet, optimism, arbitrum, base]; +export const aloeChains = [mainnet, optimism, arbitrum, base, linea, scroll]; export const CHAIN_ID_TO_ALCHEMY_URL_PREFIX: { [chainId: number]: string } = { [mainnet.id]: 'eth-mainnet', diff --git a/src/Contracts.ts b/src/Contracts.ts index 81b08d4..65b29bf 100644 --- a/src/Contracts.ts +++ b/src/Contracts.ts @@ -2,10 +2,13 @@ import { Chain, createPublicClient, createWalletClient, + fallback, getContract, + http, webSocket, } from "viem"; import { privateKeyToAccount } from "viem/accounts"; +import { arbitrum, base, mainnet, optimism, linea, scroll } from "viem/chains"; import { factoryAbi } from "./abis/Factory"; import { @@ -28,7 +31,43 @@ function alchemyWssUrlFor(chain: Chain) { return `wss://${prefix}.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`; } +function getTransportFor(chain: Chain) { + const config = { + batch: true, + retryCount: 5, + retryDelay: 1000, + }; + if (chain.id === linea.id) { + return fallback( + [ + http("https://rpc.linea.build", config), + http("https://linea.decubate.com", config), + http("https://linea.drpc.org", config), + ], + { + retryCount: 5, + retryDelay: 1000, + } + ); + } + if (chain.id === scroll.id) { + return fallback( + [ + http("https://scroll.drpc.org", config), + http("https://1rpc.io/scroll", config), + http("https://rpc.ankr.com/scroll", config), + ], + { + retryCount: 5, + retryDelay: 1000, + } + ); + } + return webSocket(alchemyWssUrlFor(chain), { retryCount: 60 }); +} + export function setupViemFor(chain: Chain, privateKey: `0x${string}`) { + const transport = getTransportFor(chain); const publicClient = createPublicClient({ batch: { multicall: { @@ -39,7 +78,7 @@ export function setupViemFor(chain: Chain, privateKey: `0x${string}`) { cacheTime: 4_000, pollingInterval: 10_000, chain, - transport: webSocket(alchemyWssUrlFor(chain), { retryCount: 60 }), + transport, }); const walletClient = createWalletClient({ @@ -47,7 +86,7 @@ export function setupViemFor(chain: Chain, privateKey: `0x${string}`) { pollingInterval: 10_000, chain, account: privateKeyToAccount(privateKey), - transport: webSocket(alchemyWssUrlFor(chain), { retryCount: 60 }), + transport, }); const client = { diff --git a/src/example.ts b/src/example.ts index 79a56f7..73122be 100644 --- a/src/example.ts +++ b/src/example.ts @@ -15,7 +15,7 @@ import * as Sentry from "@sentry/node"; const chainId = Number(process.argv[process.argv.indexOf("--chain") + 1]); const chain = extractChain({ chains: aloeChains, - id: chainId as 1 | 10 | 8453 | 42161, + id: chainId as 1 | 10 | 8453 | 42161 | 59144 | 534352, }); if ( diff --git a/src/index.ts b/src/index.ts index 29e624b..39890b8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import { fork } from "child_process"; import express, { NextFunction, Request, Response } from "express"; import * as Sentry from "@sentry/node"; -import { arbitrum, base, mainnet, optimism } from "viem/chains"; +import { arbitrum, base, mainnet, optimism, linea, scroll } from "viem/chains"; import helmet from "helmet"; const port = process.env.PORT || 8080; @@ -36,7 +36,7 @@ app.use(helmet()); app.disable("x-powered-by"); app.set("trust proxy", true); -const chains = [mainnet.id, optimism.id, arbitrum.id, base.id]; +const chains = [mainnet.id, optimism.id, arbitrum.id, base.id, linea.id, scroll.id]; chains.forEach((chain) => { const child = fork("lib/example.js", ["--chain", chain.toFixed(0)], {});