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
63 changes: 50 additions & 13 deletions api/limits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ const handler = async (
query,
});
try {
const {
MIN_DEPOSIT_USD, // The global minimum deposit in USD for all destination chains. The minimum deposit
// returned by the relayerFeeDetails() call will be floor'd with this value (after converting to token units).
} = getEnvs();
const provider = getProvider(HUB_POOL_CHAIN_ID);

assert(query, LimitsQueryParamsSchema);
Expand Down Expand Up @@ -131,12 +127,11 @@ const handler = async (
);
}

let minDepositUsdForDestinationChainId = Number(
getEnvs()[`MIN_DEPOSIT_USD_${destinationChainId}`] ?? MIN_DEPOSIT_USD
const minDepositUsd = getMinDepositUsd(
computedOriginChainId,
destinationChainId,
inputToken.symbol
);
if (isNaN(minDepositUsdForDestinationChainId)) {
minDepositUsdForDestinationChainId = 0;
}

const hubPool = getHubPool(provider);
const configStoreClient = new sdk.contracts.acrossConfigStore.Client(
Expand Down Expand Up @@ -328,10 +323,7 @@ const handler = async (
let minDepositFloor = tokenPriceUsd.lte(0)
? ethers.BigNumber.from(0)
: ethers.utils
.parseUnits(
minDepositUsdForDestinationChainId.toString(),
inputToken.decimals
)
.parseUnits(minDepositUsd.toString(), inputToken.decimals)
.mul(ethers.utils.parseUnits("1"))
.div(tokenPriceUsd);

Expand Down Expand Up @@ -574,4 +566,49 @@ const parseAndConvertUsdToTokenUnits = (
return sdk.utils.toBN(tokenValue);
};

/**
* Returns the global minimum deposit in USD for all destination chains. The minimum deposit
* returned by the relayerFeeDetails() call will be floor'd with this value (after converting to token units).
* @param originChainId The origin chain ID
* @param destinationChainId The destination chain ID
* @param inputTokenSymbol The input token symbol
* @returns The minimum deposit in USD
*/
function getMinDepositUsd(
originChainId: number,
destinationChainId: number,
inputTokenSymbol: string
) {
const { MIN_DEPOSIT_USD, MIN_DEPOSIT_USD_OVERRIDES } = getEnvs();
const minDepositUsdOverrides = JSON.parse(
Copy link
Member

Choose a reason for hiding this comment

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

Since we're using a nested JSON in an environment variable, it could be useful to wrap this in a try-catch (or maybe in a new util function like safeParse which does this) in case the type check fails at runtime. Or maybe it's not needed if we don't expect the environment variable to change much - just a thought.

MIN_DEPOSIT_USD_OVERRIDES || "{}"
) as {
routes?: { [key: string]: string };
originChains?: { [key: string]: string };
destinationChains?: { [key: string]: string };
tokens?: { [key: string]: string };
Comment on lines +586 to +589
Copy link
Member

Choose a reason for hiding this comment

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

Think this type could be reusable for other overrides? I like how it goes from most specific to least specific. (Token + Chain) -> (Token) -> (Chain).

};
const routeOverride =
minDepositUsdOverrides.routes?.[
`${originChainId}-${destinationChainId}-${inputTokenSymbol}`
];
const tokenOverride = minDepositUsdOverrides.tokens?.[inputTokenSymbol];
const originChainOverride =
minDepositUsdOverrides.originChains?.[originChainId];
const destinationChainOverride =
minDepositUsdOverrides.destinationChains?.[destinationChainId];
let minDepositUsd = Number(
routeOverride ??
tokenOverride ??
originChainOverride ??
destinationChainOverride ??
MIN_DEPOSIT_USD
);

if (isNaN(minDepositUsd)) {
minDepositUsd = 0;
}
return minDepositUsd;
}

export default handler;