Skip to content

Commit

Permalink
Merge pull request #56 from flashbots/brock/testnet-support-2024
Browse files Browse the repository at this point in the history
testnet support 2024
  • Loading branch information
zeroXbrock authored May 8, 2024
2 parents 5b937dd + f9aecec commit 4981412
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 43 deletions.
8 changes: 8 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"linter": {
"enabled": true,
"rules": {
"default": true
}
}
}
65 changes: 32 additions & 33 deletions src/api/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,40 @@ import { UnimplementedNetwork } from '../error'
/**
* Network connection presets supported by Flashbots.
*/
export class SupportedNetworks {
private static supportedNetworks = {
mainnet: {
name: "mainnet",
chainId: 1,
streamUrl: "https://mev-share.flashbots.net",
apiUrl: "https://relay.flashbots.net"
},
goerli: {
name: "goerli",
chainId: 5,
streamUrl: "https://mev-share-goerli.flashbots.net",
apiUrl: "https://relay-goerli.flashbots.net"
}
const networks = {
mainnet: {
name: "mainnet",
chainId: 1,
streamUrl: "https://mev-share.flashbots.net",
apiUrl: "https://relay.flashbots.net",
},
sepolia: {
name: "sepolia",
chainId: 11155111,
streamUrl: "https://mev-share-sepolia.flashbots.net",
apiUrl: "https://relay-sepolia.flashbots.net",
},
holesky: {
name: "holesky",
chainId: 17000,
streamUrl: "https://mev-share-holesky.flashbots.net",
apiUrl: "https://relay-holesky.flashbots.net",
}
} as const

// expose networks individually as class properties
public static mainnet = this.supportedNetworks.mainnet
public static goerli = this.supportedNetworks.goerli

/**
* Returns true if the given chainId is supported by the client.
*/
static supportsChainId(chainId: number) {
return Object.values(this.supportedNetworks).map(n => n.chainId).includes(chainId)
/**
* Gets the network preset matching the provided chainId,
* throws an UnimplementedNetwork error if not found.
*/
function getNetwork (chainId: number) {
const net = Object.values(networks).find(net => net.chainId === chainId)
if (net) {
return net
}
throw new UnimplementedNetwork({chainId})
}

/** Gets the network preset matching the provided chainId, throws an error if not found. */
static getNetwork(chainId: number) {
if (this.supportsChainId(chainId)) {
const net = Object.values(this.supportedNetworks).find(net => net.chainId === chainId)
if (net) {
return net
}
}
throw new UnimplementedNetwork({chainId})
}
export default {
...networks,
getNetwork,
}
16 changes: 10 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import axios, { AxiosError } from "axios"
import { Transaction, Wallet } from 'ethers'
import EventSource from "eventsource"
import { JsonRpcError, NetworkFailure, UnimplementedStreamEvent } from './error'

import { getRpcRequest, JsonRpcData } from './flashbots';
import {
BundleParams,
Expand All @@ -24,7 +23,7 @@ import {
EventHistoryEntry
} from './api/interfaces'
import { mungeBundleParams, mungePrivateTxParams, mungeSimBundleOptions } from "./api/mungers"
import { SupportedNetworks } from './api/networks'
import SupportedNetworks from './api/networks'
import { PendingBundle, PendingTransaction } from './api/events';
import { URLSearchParams } from 'url';

Expand All @@ -45,14 +44,19 @@ export default class MevShareClient {
return new MevShareClient(authSigner, SupportedNetworks.mainnet)
}

/** Connect to Flashbots MEV-Share node on Goerli. */
static useEthereumGoerli(authSigner: Wallet): MevShareClient {
return new MevShareClient(authSigner, SupportedNetworks.goerli)
/** Connect to Flashbots MEV-Share node on Sepolia. */
static useEthereumSepolia(authSigner: Wallet): MevShareClient {
return new MevShareClient(authSigner, SupportedNetworks.sepolia)
}

/** Connect to Flashbots MEV-Share node on Holesky. */
static useEthereumHolesky(authSigner: Wallet): MevShareClient {
return new MevShareClient(authSigner, SupportedNetworks.holesky)
}

/** Connect to supported networks by specifying a network with a `chainId`. */
static fromNetwork(authSigner: Wallet, {chainId}: {chainId: number | bigint}): MevShareClient {
const chainNum = typeof chainId == "bigint" ? Number(chainId) : chainId
const chainNum = typeof chainId === "bigint" ? Number(chainId) : chainId
const network = SupportedNetworks.getNetwork(chainNum)
return new MevShareClient(authSigner, network)
}
Expand Down
8 changes: 5 additions & 3 deletions src/examples/lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { JsonRpcProvider, Network, Wallet } from 'ethers'
import { JsonRpcProvider, Wallet } from 'ethers'
import MevShareClient from '../..'
import Env from './env'
import networks from '../../api/networks'

export function getProvider() {
return new JsonRpcProvider(Env.providerUrl, new Network("goerli", 5))
return new JsonRpcProvider(Env.providerUrl, networks.sepolia)
}

/** Initializes wallet and provider for examples, using Sepolia. */
export async function initExample(provider: JsonRpcProvider) {
const authSigner = new Wallet(Env.authKey).connect(provider)

return {
provider,
wallet: new Wallet(Env.senderKey).connect(provider),
authSigner,
mevshare: MevShareClient.useEthereumGoerli(authSigner),
mevshare: MevShareClient.useEthereumSepolia(authSigner),
feeData: await provider.getFeeData(),
}
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import SupportedNetworks from './api/networks'
import MevShareClient from './client'

export {
Expand All @@ -11,6 +12,6 @@ export {
EventHistoryEntry,
} from "./api/interfaces"

export {SupportedNetworks} from "./api/networks"
export {SupportedNetworks}

export default MevShareClient

0 comments on commit 4981412

Please sign in to comment.