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

refactor: Simplify inventory client parsing #963

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
7 changes: 4 additions & 3 deletions src/relayer/RelayerConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigNumber, toBNWei, assert, toBN, replaceAddressCase, ethers } from "../utils";
import { BigNumber, toBNWei, assert, toBN, ethers } from "../utils";
import { CommonConfig, ProcessEnv } from "../common";
import * as Constants from "../common/Constants";
import { InventoryConfig } from "../interfaces";
Expand Down Expand Up @@ -65,11 +65,12 @@ export class RelayerConfig extends CommonConfig {
this.slowDepositors = SLOW_DEPOSITORS
? JSON.parse(SLOW_DEPOSITORS).map((depositor) => ethers.utils.getAddress(depositor))
: [];
this.inventoryConfig = RELAYER_INVENTORY_CONFIG ? JSON.parse(RELAYER_INVENTORY_CONFIG) : {};
this.inventoryConfig = JSON.parse(
RELAYER_INVENTORY_CONFIG?.replace(/0x[a-fA-F0-9]{40}/g, ethers.utils.getAddress) ?? "{}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think we should make this a helper function? I think there are several other places where we expect an address string set in the .env that might not be checksummed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make it a utility function; I only did it here because this was the only place I could see that we actually normalised addressed on input.

I'll update accordingly 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be easier to use a reviver in this case. JSON.parse's second parameter is an optional function that looks at keys/values. We could probably create a reviver function like:

/**
 * Converts a potential string/array of strings that match an address into a checksummed address
 **/
export function checksumAddress(value: unknown): unknown {
    const isArray = Array.isArray(value);
    const result: unknown[] = (isArray ? value : [value]).map((v) => utils.isAddress(v) ? utils.getAddress(v) : v;
    return isArray ? result : result[0];
}
JSON.parse(
    RELAYER_INVENTORY_CONFIG ?? {},
    (, v) => checksumAddress(v)
)

);
this.minRelayerFeePct = toBNWei(MIN_RELAYER_FEE_PCT || Constants.RELAYER_MIN_FEE_PCT);

if (Object.keys(this.inventoryConfig).length > 0) {
this.inventoryConfig = replaceAddressCase(this.inventoryConfig); // Cast any non-address case addresses.
this.inventoryConfig.wrapEtherThreshold = this.inventoryConfig.wrapEtherThreshold
? toBNWei(this.inventoryConfig.wrapEtherThreshold)
: toBNWei(1); // default to keeping 2 Eth on the target chains and wrapping the rest to WETH.
Expand Down
2 changes: 0 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export {
export type { Block, TransactionResponse, TransactionReceipt, Provider } from "@ethersproject/abstract-provider";

export { config } from "dotenv";

export { replaceAddressCase } from "@uma/common";
export { Logger } from "@uma/financial-templates-lib";

// TypeChain exports used in the bot.
Expand Down