Skip to content

Commit

Permalink
Add http transports for linea and scroll, may or may not work
Browse files Browse the repository at this point in the history
  • Loading branch information
haydenshively committed Apr 27, 2024
1 parent 7dbe109 commit aaef3f0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Constants.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { arbitrum, base, mainnet, optimism } from 'viem/chains';
import { arbitrum, base, linea, mainnet, optimism, scroll } from 'viem/chains';

export const FACTORY_ADDRESS = '0x000000009efdB26b970bCc0085E126C9dfc16ee8';

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',
Expand Down
43 changes: 41 additions & 2 deletions src/Contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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: {
Expand All @@ -39,15 +78,15 @@ 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({
cacheTime: 4_000,
pollingInterval: 10_000,
chain,
account: privateKeyToAccount(privateKey),
transport: webSocket(alchemyWssUrlFor(chain), { retryCount: 60 }),
transport,
});

const client = {
Expand Down
2 changes: 1 addition & 1 deletion src/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)], {});
Expand Down

0 comments on commit aaef3f0

Please sign in to comment.