Skip to content
Open
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
48 changes: 38 additions & 10 deletions src/refiller/RefillerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,31 @@ export class RefillerConfig extends CommonConfig {
constructor(env: ProcessEnv) {
super(env);

const { REFILL_BALANCES, NATIVE_MARKETS_API_KEY, NATIVE_MARKETS_API_BASE, MIN_USDH_REBALANCE_AMOUNT } = env;
const {
REFILL_BALANCES,
REFILL_BALANCES_2,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

nb. naming is temporary - if we migrate to this format I'd anticipate deprecating REFILL_BALANCES and stealing its name.

NATIVE_MARKETS_API_KEY,
NATIVE_MARKETS_API_BASE,
MIN_USDH_REBALANCE_AMOUNT,
} = env;

const validate = (chainId: number, account: string, target: number, trigger: number) => {
if (Number.isNaN(target) || target <= 0) {
throw new Error(`target for ${chainId} and ${account} must be > 0, got ${target}`);
}
if (Number.isNaN(trigger) || trigger <= 0) {
throw new Error(`trigger for ${chainId} and ${account} must be > 0, got ${trigger}`);
}
if (trigger >= target) {
throw new Error("trigger must be < target");
}
};

// Used to send tokens if available in wallet to balances under target balances.
if (REFILL_BALANCES) {
this.refillEnabledBalances = JSON.parse(REFILL_BALANCES).map(
({ chainId, account, isHubPool, target, trigger, token }) => {
if (Number.isNaN(target) || target <= 0) {
throw new Error(`target for ${chainId} and ${account} must be > 0, got ${target}`);
}
if (Number.isNaN(trigger) || trigger <= 0) {
throw new Error(`trigger for ${chainId} and ${account} must be > 0, got ${trigger}`);
}
if (trigger >= target) {
throw new Error("trigger must be < target");
}
validate(chainId, account, target, trigger);
return {
// Required fields:
chainId,
Expand All @@ -46,6 +56,24 @@ export class RefillerConfig extends CommonConfig {
};
}
);
} else if (REFILL_BALANCES_2) {
this.refillEnabledBalances = [];
const config = JSON.parse(REFILL_BALANCES_2);
Object.entries(config).forEach(([account, chainConfig]) => {
Object.entries(chainConfig).forEach(([_chainId, tokenConfig]) => {
const chainId = Number(_chainId);
const { target, trigger, isHubPool, token } = tokenConfig;
validate(chainId, account, target, trigger);
this.refillEnabledBalances.push({
chainId,
account: toAddressType(account, chainId),
target,
trigger,
isHubPool: Boolean(isHubPool),
token: isDefined(token) ? toAddressType(token, chainId) : getNativeTokenAddressForChain(chainId),
});
});
});
}

if (isDefined(NATIVE_MARKETS_API_KEY) && isDefined(NATIVE_MARKETS_API_BASE)) {
Expand Down