Skip to content

Commit

Permalink
fix: gas oracle overhead calculation (#4571)
Browse files Browse the repository at this point in the history
fix: gas oracle overhead calculation

as spotted by @tkporter
https://canary.discord.com/channels/935678348330434570/1288601439006097498/1288602404950114334

---------

Signed-off-by: pbio <10051819+paulbalaji@users.noreply.github.com>
  • Loading branch information
paulbalaji authored Sep 25, 2024
1 parent 3857cfb commit 9f5a17b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
12 changes: 6 additions & 6 deletions typescript/infra/config/environments/mainnet3/igp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { exclude, objMap } from '@hyperlane-xyz/utils';
import {
AllStorageGasOracleConfigs,
getAllStorageGasOracleConfigs,
getOverhead,
getTokenExchangeRateFromValues,
remoteOverhead,
} from '../../../src/config/gas-oracle.js';

import { ethereumChainNames } from './chains.js';
Expand All @@ -16,9 +16,9 @@ import rawTokenPrices from './tokenPrices.json';

const tokenPrices: ChainMap<string> = rawTokenPrices;

const remoteOverheadWithOverrides = (chain: ChainName) => {
let overhead = remoteOverhead(chain, ethereumChainNames);
if (chain === 'moonbeam') {
const getOverheadWithOverrides = (local: ChainName, remote: ChainName) => {
let overhead = getOverhead(local, remote, ethereumChainNames);
if (remote === 'moonbeam') {
overhead *= 4;
}
return overhead;
Expand All @@ -31,7 +31,7 @@ const storageGasOracleConfig: AllStorageGasOracleConfigs =
(local, remote) =>
getTokenExchangeRateFromValues(local, remote, tokenPrices),
(local) => parseFloat(tokenPrices[local]),
remoteOverheadWithOverrides,
getOverheadWithOverrides,
);

export const igp: ChainMap<IgpConfig> = objMap(
Expand All @@ -49,7 +49,7 @@ export const igp: ChainMap<IgpConfig> = objMap(
overhead: Object.fromEntries(
exclude(local, supportedChainNames).map((remote) => [
remote,
remoteOverhead(remote, ethereumChainNames),
getOverheadWithOverrides(local, remote),
]),
),
oracleConfig: storageGasOracleConfig[local],
Expand Down
4 changes: 2 additions & 2 deletions typescript/infra/config/environments/test/igp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export const igp: ChainMap<IgpConfig> = objMap(
exclude(chain, testChainNames).map((remote) => [
remote,
multisigIsmVerificationCost(
multisigIsm[remote].threshold,
multisigIsm[remote].validators.length,
multisigIsm[chain].threshold,
multisigIsm[chain].validators.length,
),
]),
);
Expand Down
6 changes: 3 additions & 3 deletions typescript/infra/config/environments/testnet4/igp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { Address, exclude, objMap } from '@hyperlane-xyz/utils';
import {
AllStorageGasOracleConfigs,
getAllStorageGasOracleConfigs,
getOverhead,
getTokenExchangeRateFromValues,
remoteOverhead,
} from '../../../src/config/gas-oracle.js';

import { ethereumChainNames } from './chains.js';
Expand All @@ -23,7 +23,7 @@ export const storageGasOracleConfig: AllStorageGasOracleConfigs =
(local, remote) =>
getTokenExchangeRateFromValues(local, remote, tokenPrices),
(local) => parseFloat(tokenPrices[local]),
(local) => remoteOverhead(local, ethereumChainNames),
(local, remote) => getOverhead(local, remote, ethereumChainNames),
);

export const igp: ChainMap<IgpConfig> = objMap(
Expand All @@ -38,7 +38,7 @@ export const igp: ChainMap<IgpConfig> = objMap(
overhead: Object.fromEntries(
exclude(chain, supportedChainNames).map((remote) => [
remote,
remoteOverhead(remote, ethereumChainNames),
getOverhead(chain, remote, ethereumChainNames),
]),
),
};
Expand Down
17 changes: 9 additions & 8 deletions typescript/infra/src/config/gas-oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function getLocalStorageGasOracleConfig(
gasPrices: ChainMap<GasPriceConfig>,
getTokenExchangeRate: (local: ChainName, remote: ChainName) => BigNumber,
getTokenUsdPrice?: (chain: ChainName) => number,
remoteOverhead?: (remote: ChainName) => number,
getOverhead?: (local: ChainName, remote: ChainName) => number,
): StorageGasOracleConfig {
return remotes.reduce((agg, remote) => {
let exchangeRate = getTokenExchangeRate(local, remote);
Expand Down Expand Up @@ -96,8 +96,8 @@ function getLocalStorageGasOracleConfig(

// If we have access to these, let's use the USD prices to apply some minimum
// typical USD payment heuristics.
if (getTokenUsdPrice && remoteOverhead) {
const typicalRemoteGasAmount = remoteOverhead(remote) + 50_000;
if (getTokenUsdPrice && getOverhead) {
const typicalRemoteGasAmount = getOverhead(local, remote) + 50_000;
const typicalIgpQuoteUsd = getUsdQuote(
local,
gasPriceBn,
Expand Down Expand Up @@ -187,14 +187,15 @@ function getUsdQuote(
const FOREIGN_DEFAULT_OVERHEAD = 600_000;

// Overhead for interchain messaging
export function remoteOverhead(
export function getOverhead(
local: ChainName,
remote: ChainName,
ethereumChainNames: ChainName[],
): number {
return ethereumChainNames.includes(remote as any)
? multisigIsmVerificationCost(
defaultMultisigConfigs[remote].threshold,
defaultMultisigConfigs[remote].validators.length,
defaultMultisigConfigs[local].threshold,
defaultMultisigConfigs[local].validators.length,
)
: FOREIGN_DEFAULT_OVERHEAD; // non-ethereum overhead
}
Expand All @@ -205,7 +206,7 @@ export function getAllStorageGasOracleConfigs(
gasPrices: ChainMap<GasPriceConfig>,
getTokenExchangeRate: (local: ChainName, remote: ChainName) => BigNumber,
getTokenUsdPrice?: (chain: ChainName) => number,
remoteOverhead?: (remote: ChainName) => number,
getOverhead?: (local: ChainName, remote: ChainName) => number,
): AllStorageGasOracleConfigs {
return chainNames.filter(isEthereumProtocolChain).reduce((agg, local) => {
const remotes = chainNames.filter((chain) => local !== chain);
Expand All @@ -217,7 +218,7 @@ export function getAllStorageGasOracleConfigs(
gasPrices,
getTokenExchangeRate,
getTokenUsdPrice,
remoteOverhead,
getOverhead,
),
};
}, {}) as AllStorageGasOracleConfigs;
Expand Down

0 comments on commit 9f5a17b

Please sign in to comment.