Skip to content

Commit

Permalink
refactor(RelayerConfig): Simplify InventoryConfig parsing (#1459)
Browse files Browse the repository at this point in the history
- Only append unwrapWethThreshold for WETH.
- Dereference the l1Token/chainId pair for less repetition.
  • Loading branch information
pxrl authored May 1, 2024
1 parent 4cf3aaf commit e4b70b6
Showing 1 changed file with 55 additions and 39 deletions.
94 changes: 55 additions & 39 deletions src/relayer/RelayerConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { typeguards } from "@across-protocol/sdk-v2";
import { BigNumber, toBNWei, assert, isDefined, readFileSync, toBN, replaceAddressCase, ethers } from "../utils";
import {
BigNumber,
toBNWei,
assert,
isDefined,
readFileSync,
toBN,
replaceAddressCase,
ethers,
TOKEN_SYMBOLS_MAP,
} from "../utils";
import { CommonConfig, ProcessEnv } from "../common";
import * as Constants from "../common/Constants";
import { InventoryConfig } from "../interfaces";
Expand Down Expand Up @@ -95,46 +105,51 @@ export class RelayerConfig extends CommonConfig {

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.
this.inventoryConfig.wrapEtherThresholdPerChain ??= {};
this.inventoryConfig.wrapEtherTarget = this.inventoryConfig.wrapEtherTarget
? toBNWei(this.inventoryConfig.wrapEtherTarget)
: this.inventoryConfig.wrapEtherThreshold; // default to wrapping ETH to threshold, same as target.
this.inventoryConfig.wrapEtherTargetPerChain ??= {};

const { inventoryConfig } = this;

// Default to 1 Eth on the target chains and wrapping the rest to WETH.
inventoryConfig.wrapEtherThreshold = toBNWei(inventoryConfig.wrapEtherThreshold ?? 1);

inventoryConfig.wrapEtherThresholdPerChain ??= {};
inventoryConfig.wrapEtherTarget = inventoryConfig.wrapEtherTarget
? toBNWei(inventoryConfig.wrapEtherTarget)
: inventoryConfig.wrapEtherThreshold; // default to wrapping ETH to threshold, same as target.

inventoryConfig.wrapEtherTargetPerChain ??= {};
assert(
this.inventoryConfig.wrapEtherThreshold.gte(this.inventoryConfig.wrapEtherTarget),
`default wrapEtherThreshold ${this.inventoryConfig.wrapEtherThreshold} must be >= default wrapEtherTarget ${this.inventoryConfig.wrapEtherTarget}`
inventoryConfig.wrapEtherThreshold.gte(inventoryConfig.wrapEtherTarget),
`default wrapEtherThreshold ${inventoryConfig.wrapEtherThreshold} must be >= default wrapEtherTarget ${inventoryConfig.wrapEtherTarget}`
);

// Validate the per chain target and thresholds for wrapping ETH:
Object.keys(this.inventoryConfig.wrapEtherThresholdPerChain).forEach((chainId) => {
if (this.inventoryConfig.wrapEtherThresholdPerChain[chainId] !== undefined) {
this.inventoryConfig.wrapEtherThresholdPerChain[chainId] = toBNWei(
this.inventoryConfig.wrapEtherThresholdPerChain[chainId]
);
const wrapThresholds = inventoryConfig.wrapEtherThresholdPerChain;
const wrapTargets = inventoryConfig.wrapEtherTargetPerChain;
Object.keys(inventoryConfig.wrapEtherThresholdPerChain).forEach((chainId) => {
if (wrapThresholds[chainId] !== undefined) {
wrapThresholds[chainId] = toBNWei(wrapThresholds[chainId]); // Promote to 18 decimals.
}
});
Object.keys(this.inventoryConfig.wrapEtherTargetPerChain).forEach((chainId) => {
if (this.inventoryConfig.wrapEtherTargetPerChain[chainId] !== undefined) {
this.inventoryConfig.wrapEtherTargetPerChain[chainId] = toBNWei(
this.inventoryConfig.wrapEtherTargetPerChain[chainId]
);

Object.keys(inventoryConfig.wrapEtherTargetPerChain).forEach((chainId) => {
if (wrapTargets[chainId] !== undefined) {
wrapTargets[chainId] = toBNWei(wrapTargets[chainId]); // Promote to 18 decimals.

// Check newly set target against threshold
const threshold =
this.inventoryConfig.wrapEtherThresholdPerChain[chainId] ?? this.inventoryConfig.wrapEtherThreshold;
const target = this.inventoryConfig.wrapEtherTargetPerChain[chainId];
const threshold = wrapThresholds[chainId] ?? inventoryConfig.wrapEtherThreshold;
const target = wrapTargets[chainId];
assert(
threshold.gte(target),
`wrapEtherThresholdPerChain ${threshold.toString()} must be >= wrapEtherTargetPerChain ${target}`
`Chain ${chainId} wrapEtherThresholdPerChain ${threshold} must be >= wrapEtherTargetPerChain ${target}`
);
}
});
Object.keys(this.inventoryConfig?.tokenConfig ?? {}).forEach((l1Token) => {
Object.keys(this.inventoryConfig.tokenConfig[l1Token]).forEach((chainId) => {
const { targetPct, thresholdPct, unwrapWethThreshold, unwrapWethTarget, targetOverageBuffer } =
this.inventoryConfig.tokenConfig[l1Token][chainId];

Object.keys(inventoryConfig?.tokenConfig ?? {}).forEach((l1Token) => {
Object.keys(inventoryConfig.tokenConfig[l1Token]).forEach((chainId) => {
const tokenConfig = inventoryConfig.tokenConfig[l1Token][chainId];

const { targetPct, thresholdPct, unwrapWethThreshold, unwrapWethTarget, targetOverageBuffer } = tokenConfig;
assert(
targetPct !== undefined && thresholdPct !== undefined,
`Bad config. Must specify targetPct, thresholdPct for ${l1Token} on ${chainId}`
Expand All @@ -143,20 +158,21 @@ export class RelayerConfig extends CommonConfig {
toBN(thresholdPct).lte(toBN(targetPct)),
`Bad config. thresholdPct<=targetPct for ${l1Token} on ${chainId}`
);
this.inventoryConfig.tokenConfig[l1Token][chainId].targetPct = toBNWei(targetPct).div(100);
this.inventoryConfig.tokenConfig[l1Token][chainId].thresholdPct = toBNWei(thresholdPct).div(100);
tokenConfig.targetPct = toBNWei(targetPct).div(100);
tokenConfig.thresholdPct = toBNWei(thresholdPct).div(100);

// Default to 150% the targetPct. targetOverageBuffer does not have to be defined so that no existing configs
// are broken. This is a reasonable default because it allows the relayer to be a bit more flexible in
// holding more tokens than the targetPct, but perhaps a better default is 100%
this.inventoryConfig.tokenConfig[l1Token][chainId].targetOverageBuffer = toBNWei(
targetOverageBuffer ?? "1.5"
);
if (unwrapWethThreshold !== undefined) {
this.inventoryConfig.tokenConfig[l1Token][chainId].unwrapWethThreshold = toBNWei(unwrapWethThreshold);
tokenConfig.targetOverageBuffer = toBNWei(targetOverageBuffer ?? "1.5");

// For WETH, also consider any unwrap target/threshold.
if (l1Token === TOKEN_SYMBOLS_MAP.WETH.addresses[this.hubPoolChainId]) {
if (unwrapWethThreshold !== undefined) {
tokenConfig.unwrapWethThreshold = toBNWei(unwrapWethThreshold);
}
tokenConfig.unwrapWethTarget = toBNWei(unwrapWethTarget ?? 2);
}
this.inventoryConfig.tokenConfig[l1Token][chainId].unwrapWethTarget = unwrapWethTarget
? toBNWei(unwrapWethTarget)
: toBNWei(2);
});
});
}
Expand Down

0 comments on commit e4b70b6

Please sign in to comment.