Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: websockets rpc provider #125

Merged
merged 2 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 3 additions & 40 deletions src/domain/chainContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
reorgDepth,
reorgsTotal,
} from "../utils/metrics";
import { ConnectionInfo, hexZeroPad } from "ethers/lib/utils";
import { hexZeroPad } from "ethers/lib/utils";
import { FilterPolicy } from "../utils/filterPolicy";

const WATCHDOG_FREQUENCY = 5 * 1000; // 5 seconds
Expand Down Expand Up @@ -155,9 +155,9 @@ export class ChainContext {
options: RunSingleOptions,
storage: DBService
): Promise<ChainContext> {
const { rpc, rpcUser, rpcPassword, deploymentBlock } = options;
const { rpc, deploymentBlock } = options;

const provider = getProvider(rpc, rpcUser, rpcPassword);
const provider = new providers.WebSocketProvider(rpc);
const chainId = (await provider.getNetwork()).chainId;

const registry = await Registry.load(
Expand Down Expand Up @@ -444,43 +444,6 @@ export class ChainContext {
}
}

function getProvider(
rpc: string,
user?: string,
password?: string
): providers.Provider {
const providerConfig: ConnectionInfo = { url: rpc };

if (user && password) {
if (rpc.startsWith("http")) {
// FIXME: Not a good idea to use HTTP and basic auth, but this is a temporary solution to support it (some RPCs are only exposes under HTTP and have basic auth)
// Only run it in our own infra
providerConfig.headers = {
Authorization: getAuthHeader({ user, password }),
};
} else {
// Set basic auth
providerConfig.user = user;
providerConfig.password = password;
}
}

return new providers.JsonRpcProvider(providerConfig);
}

/**
* Helper method to create the basic AUTH header for the provider
* In principle, ethersjs already deals with it, however i need to manually set it when using HTTP.
*
* Its not a good idea to use auth headers with HTTP, so this is a temporary solution.
*
* @param params user and password
* @returns the basic auth header
*/
function getAuthHeader({ user, password }: { user: string; password: string }) {
return "Basic " + Buffer.from(`${user}:${password}`).toString("base64");
}

/**
* Process events in a block.
* @param context of the chain who's block is being processed
Expand Down
14 changes: 2 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ const databasePathOption = new Option(
.default(DEFAULT_DATABASE_PATH)
.env("DATABASE_PATH");

const CHAIN_CONFIG_PARAMS =
"<rpc>,<deploymentBlock>[,<watchdogTimeout>,<orderBookApi>,<filterPolicyConfig>,<rpcUser>,<rpcPassword>";
const chainConfigHelp = `Chain configuration in the format of ${CHAIN_CONFIG_PARAMS}], e.g. http://erigon.dappnode:8545,12345678,30,https://api.cow.fi/mainnet,https://raw.githubusercontent.com/cowprotocol/watch-tower/config/filter-policy-1.json`;
const chainConfigHelp = `Chain configuration in the format of <rpc>,<deploymentBlock>[,<watchdogTimeout>,<orderBookApi>,<filterPolicyConfig>], e.g. http://erigon.dappnode:8545,12345678,30,https://api.cow.fi/mainnet,https://raw.githubusercontent.com/cowprotocol/watch-tower/config/filter-policy-1.json`;
const multiChainConfigOption = new Option(
"--chain-config <chainConfig...>",
chainConfigHelp
Expand Down Expand Up @@ -233,7 +231,7 @@ function parseChainConfigOption(option: string): ChainConfigOptions {
// Ensure there are at least two parts (rpc and deploymentBlock)
if (parts.length < 2) {
throw new InvalidArgumentError(
`Chain configuration must be in the format of ${CHAIN_CONFIG_PARAMS}], e.g. http://erigon.dappnode:8545,12345678,30,https://api.cow.fi/mainnet,https://raw.githubusercontent.com/cowprotocol/watch-tower/config/filter-policy-1.json`
`Chain configuration must be in the format of <rpc>,<deploymentBlock>[,<watchdogTimeout>,<orderBookApi>,<filterPolicyConfig>], e.g. http://erigon.dappnode:8545,12345678,30,https://api.cow.fi/mainnet,https://raw.githubusercontent.com/cowprotocol/watch-tower/config/filter-policy-1.json`
);
}

Expand Down Expand Up @@ -285,16 +283,8 @@ function parseChainConfigOption(option: string): ChainConfigOptions {
);
}

// If there is a sixth part, it is the rpcUser
const rpcUserConfig = parts.length > 5 && parts[5] ? parts[5] : undefined;

// If there is a seventh part, it is the rpcUser
const rpcPasswordConfig = parts.length > 6 && parts[6] ? parts[6] : undefined;

return {
rpc,
rpcUser: rpcUserConfig,
rpcPassword: rpcPasswordConfig,
deploymentBlock,
watchdogTimeout,
orderBookApi,
Expand Down
2 changes: 0 additions & 2 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ export type ChainConfigOptions = {
watchdogTimeout: number;
orderBookApi?: string;
filterPolicyConfig?: string;
rpcUser?: string;
rpcPassword?: string;
// filterPolicyConfigAuthToken?: string; // TODO: Implement authToken
};

Expand Down
Loading