Skip to content

Commit

Permalink
Merge pull request #740 from bcnmy/feat/mev-protection
Browse files Browse the repository at this point in the history
fix(relayers): implement support for MEV protected RPCs
  • Loading branch information
TheDivic authored Nov 8, 2024
2 parents 992801b + 02bbbea commit 6155c5f
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 21 deletions.
26 changes: 12 additions & 14 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
5003, 100, 10200, 195, 196, 2810, 997, 713715, 3799, 167009, 80084, 5845,
167000, 1328, 1329, 995, 28882, 288, 920637907288165, 1740, 1750, 4202, 1135
],
"supportedNetworksV07": [
84532, 8453, 10, 11155420
],
"supportedNetworksV07": [84532, 8453, 10, 11155420],
"EIP1559SupportedNetworks": [
1, 137, 42161, 10, 43114, 43113, 8453, 59140, 59144, 204, 5611, 421614,
11155111, 84532, 168587773, 80085, 81457, 42170, 169, 56400, 11155420,
80002, 27827, 4653, 100, 10200, 997, 713715, 3799, 167009, 80084, 5845,
167000, 1328, 1329, 1715, 995, 28882, 288, 920637907288165, 1740, 1750, 4202,
1135
167000, 1328, 1329, 1715, 995, 28882, 288, 920637907288165, 1740, 1750,
4202, 1135
],
"testnetNetworks": [
97, 43113, 59140, 5611, 81, 88882, 59144, 421614, 11155111, 1115, 3441005,
Expand Down Expand Up @@ -133,7 +131,7 @@
"type": "private"
}
],
"84532": [
"84532": [
{
"url": "https://sepolia.base.org",
"type": "public"
Expand Down Expand Up @@ -484,7 +482,7 @@
"9980": 1,
"7116": 40,
"421614": 5,
"11155111": 10,
"11155111": 1,
"80085": 2,
"168587773": 20,
"84532": 2,
Expand Down Expand Up @@ -527,7 +525,7 @@
},
"maxRelayerCount": {
"137": 400,
"1": 2,
"1": 5,
"97": 7,
"56": 30,
"1101": 10,
Expand Down Expand Up @@ -556,7 +554,7 @@
"9980": 5,
"7116": 100,
"421614": 20,
"11155111": 20,
"11155111": 1,
"80085": 7,
"84532": 7,
"168587773": 30,
Expand Down Expand Up @@ -744,7 +742,7 @@
"fundingRelayerAmount": {
"137": 5,
"97": 0.1,
"1": 1,
"1": 0.2,
"56": 0.15,
"1101": 0.2,
"42161": 0.01,
Expand Down Expand Up @@ -816,7 +814,7 @@
"fundingBalanceThreshold": {
"137": 3,
"97": 0.05,
"1": 0.5,
"1": 0.1,
"56": 0.003,
"1101": 0.01,
"42161": 0.002,
Expand Down Expand Up @@ -1014,8 +1012,8 @@
84532, 168587773, 43113, 80085, 81457, 42170, 534351, 534352, 56400,
7000, 11155420, 80002, 27827, 7001, 2442, 4653, 5003, 8101902,
666666666, 100, 10200, 195, 196, 2810, 997, 713715, 3799, 167009, 80084,
5845, 167000, 1328, 1329, 995, 28882, 288, 920637907288165, 1740, 1750, 4202,
1135
5845, 167000, 1328, 1329, 995, 28882, 288, 920637907288165, 1740, 1750,
4202, 1135
]
},
"0x00000061fefce24a79343c27127435286bb7a4e1": {
Expand Down Expand Up @@ -1127,7 +1125,7 @@
"84532": {
"verificationGasLimit": 2000000,
"callGasLimit": 2000000,
"preVerificationGas": 45000000
"preVerificationGas": 45000000
}
}
}
33 changes: 27 additions & 6 deletions src/common/network/EVMNetworkService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,51 @@ export class EVMNetworkService

rpcUrl: string;

mevProtectedRpcUrl: string | undefined;

provider: PublicClient;

mevProtectedProvider: PublicClient | null;

constructor(options: { chainId: number; rpcUrl: string }) {
this.chainId = options.chainId;
this.rpcUrl = options.rpcUrl;

const providers = config.chains.providers[this.chainId];
if (!providers) {
const regularProviders = config.chains.providers[this.chainId];
if (!regularProviders) {
throw new Error(
`No providers found for chainId: ${this.chainId} in the config`,
`No RPC providers found for chainId: ${this.chainId} in the config`,
);
}

if (providers.length > 1) {
if (regularProviders.length > 1) {
this.provider = createPublicClient({
transport: fallback(
config.chains.providers[this.chainId].map((p) => http(p.url)),
config.chains.providers[this.chainId]
.filter((p) => p.type !== "mev-protected")
.map((p) => http(p.url)),
LOAD_BALANCER_DEFAULT,
),
});
} else {
this.provider = createPublicClient({
transport: http(providers[0].url),
transport: http(regularProviders[0].url),
});
}

const mevProtectedProviders = config.chains.providers[this.chainId].filter(
(p) => p.type === "mev-protected",
);
if (mevProtectedProviders.length > 1) {
this.mevProtectedRpcUrl = mevProtectedProviders[0].url;
this.mevProtectedProvider = createPublicClient({
transport: fallback(
mevProtectedProviders.map((p) => http(p.url)),
LOAD_BALANCER_DEFAULT,
),
});
} else {
this.mevProtectedProvider = null;
}
}

Expand Down
1 change: 1 addition & 0 deletions src/common/network/interface/INetworkService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
export interface INetworkService<AccountType, RawTransactionType> {
chainId: number;
rpcUrl: string;
mevProtectedRpcUrl?: string;
provider: PublicClient;

sendRpcCall(method: string, params: Array<any>): Promise<any>;
Expand Down
1 change: 1 addition & 0 deletions src/config/interface/IConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TransactionType } from "../../common/types";
enum RpcProviderType {
PUBLIC = "public",
PRIVATE = "private",
MEV_PROTECTED = "mev-protected",
}

interface RpcProvider {
Expand Down
6 changes: 5 additions & 1 deletion src/relayer/account/EVMAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { EVMRawTransactionType } from "../../common/types";
import { IEVMAccount } from "./interface/IEVMAccount";

export class EVMAccount implements IEVMAccount {
public rpcUrl: string;

private account: PrivateKeyAccount;

private publicKey: string;
Expand All @@ -14,10 +16,12 @@ export class EVMAccount implements IEVMAccount {
accountPublicKey: string,
accountPrivateKey: string,
rpcUrl: string,
mevProtectedRpcUrl?: string,
) {
this.rpcUrl = mevProtectedRpcUrl || rpcUrl;
this.account = privateKeyToAccount(`0x${accountPrivateKey}`);
this.walletClient = createWalletClient({
transport: http(rpcUrl),
transport: http(this.rpcUrl),
account: privateKeyToAccount(`0x${accountPrivateKey}`),
});
this.publicKey = accountPublicKey;
Expand Down
1 change: 1 addition & 0 deletions src/relayer/account/interface/IEVMAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EVMRawTransactionType } from "../../../common/types";
import { IAccount } from "./IAccount";

export interface IEVMAccount extends IAccount {
rpcUrl: string;
getPublicKey(): string;
signMessage(message: string): Promise<string>;
signTransaction(rawTransaction: EVMRawTransactionType): Promise<string>;
Expand Down
1 change: 1 addition & 0 deletions src/relayer/relayer-manager/EVMRelayerManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ export class EVMRelayerManager
address,
privateKey,
this.networkService.rpcUrl,
this.networkService.mevProtectedRpcUrl,
);
this.relayerMap[address] = relayer;
relayers.push(relayer);
Expand Down

0 comments on commit 6155c5f

Please sign in to comment.