Skip to content

Commit

Permalink
Add scripts for fetching prices and populating config (#3309)
Browse files Browse the repository at this point in the history
### Description

- Simple scripts for generating token and gas price constants

### Related issues

- Fixes #3251 

### Backward compatibility

Yes

### Testing

Fork tests show diff onchain
  • Loading branch information
yorhodes authored Feb 24, 2024
1 parent 06d27e9 commit f606e3e
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 89 deletions.
102 changes: 13 additions & 89 deletions typescript/infra/config/environments/mainnet3/gas-oracle.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BigNumber, ethers } from 'ethers';

import { ChainMap, ChainName } from '@hyperlane-xyz/sdk';
import { objMap } from '@hyperlane-xyz/utils';

import {
AllStorageGasOracleConfigs,
Expand All @@ -12,95 +13,18 @@ import {
} from '../../../src/config/gas-oracle';

import { supportedChainNames } from './chains';

// Taken by looking at each network's gas history and overestimating
// Last updated Mar 9, 2023.
const gasPrices: ChainMap<BigNumber> = {
// https://bscscan.com/chart/gasprice
bsc: ethers.utils.parseUnits('3', 'gwei'),
// https://snowtrace.io/chart/gasprice
avalanche: ethers.utils.parseUnits('35', 'gwei'),
// https://polygonscan.com/chart/gasprice
polygon: ethers.utils.parseUnits('300', 'gwei'),
// https://celoscan.io/chart/gasprice
// This one is interesting - the average is high (~20 gwei)
// but the median is low (< 10). This is likely because a popular wallet is
// overpaying, but all our txs tend to be < 10 gwei.
celo: ethers.utils.parseUnits('10', 'gwei'),
// https://dune.com/Henrystats/arbitrum-metrics
// A bit higher to try to account for L1 fees
arbitrum: ethers.utils.parseUnits('1', 'gwei'),
// https://dune.com/optimismfnd/optimism-l1-batch-submission-fees-security-costs
// A bit higher to try to account for L1 fees
optimism: ethers.utils.parseUnits('1', 'gwei'),
// https://dune.com/hildobby/Gas
ethereum: ethers.utils.parseUnits('35', 'gwei'),
// https://moonscan.io/chart/gasprice
// Similar to Celo - average is ~200 gwei, but people
// generally are overpaying compared to us
moonbeam: ethers.utils.parseUnits('150', 'gwei'),
// https://gnosisscan.io/chart/gasprice
// People also seem to be overpaying here
gnosis: ethers.utils.parseUnits('10', 'gwei'),
// Arbitrarily chosen as gas prices aren't really a thing
// in Solana.
solana: ethers.BigNumber.from('28'),
base: ethers.utils.parseUnits('1', 'gwei'),
scroll: ethers.utils.parseUnits('1', 'gwei'),
polygonzkevm: ethers.utils.parseUnits('2', 'gwei'),
neutron: ethers.utils.parseUnits('1', 'gwei'),
mantapacific: ethers.utils.parseUnits('1', 'gwei'),
inevm: ethers.utils.parseUnits('1', 'gwei'),
injective: ethers.utils.parseUnits('1', 'gwei'),
viction: ethers.utils.parseUnits('0.25', 'gwei'),
};

// Accurate from coingecko as of Feb 9, 2024.
// These aren't overestimates because the exchange rates between
// tokens are what matters. These generally have high beta
const tokenUsdPrices: ChainMap<BigNumber> = {
// https://www.coingecko.com/en/coins/bnb
bsc: ethers.utils.parseUnits('323.61', TOKEN_EXCHANGE_RATE_DECIMALS),
// https://www.coingecko.com/en/coins/avalanche
avalanche: ethers.utils.parseUnits('38.27', TOKEN_EXCHANGE_RATE_DECIMALS),
// https://www.coingecko.com/en/coins/polygon
polygon: ethers.utils.parseUnits('0.85', TOKEN_EXCHANGE_RATE_DECIMALS),
// https://www.coingecko.com/en/coins/celo
celo: ethers.utils.parseUnits('0.73', TOKEN_EXCHANGE_RATE_DECIMALS),
// https://www.coingecko.com/en/coins/ethereum
arbitrum: ethers.utils.parseUnits('2495.00', TOKEN_EXCHANGE_RATE_DECIMALS),
// https://www.coingecko.com/en/coins/ethereum
optimism: ethers.utils.parseUnits('2495.00', TOKEN_EXCHANGE_RATE_DECIMALS),
// https://www.coingecko.com/en/coins/ethereum
ethereum: ethers.utils.parseUnits('2495.00', TOKEN_EXCHANGE_RATE_DECIMALS),
// https://www.coingecko.com/en/coins/ethereum
base: ethers.utils.parseUnits('2495.00', TOKEN_EXCHANGE_RATE_DECIMALS),
// https://www.coingecko.com/en/coins/ethereum
scroll: ethers.utils.parseUnits('2495.00', TOKEN_EXCHANGE_RATE_DECIMALS),
// https://www.coingecko.com/en/coins/ethereum
polygonzkevm: ethers.utils.parseUnits(
'2495.00',
TOKEN_EXCHANGE_RATE_DECIMALS,
),
// https://www.coingecko.com/en/coins/ethereum
mantapacific: ethers.utils.parseUnits(
'2495.00',
TOKEN_EXCHANGE_RATE_DECIMALS,
),
// https://www.coingecko.com/en/coins/moonbeam
moonbeam: ethers.utils.parseUnits('0.387', TOKEN_EXCHANGE_RATE_DECIMALS),
// xDAI
gnosis: ethers.utils.parseUnits('1.00', TOKEN_EXCHANGE_RATE_DECIMALS),
// https://www.coingecko.com/en/coins/solana
solana: ethers.utils.parseUnits('108.01', TOKEN_EXCHANGE_RATE_DECIMALS),
// https://www.coingecko.com/en/coins/neutron
neutron: ethers.utils.parseUnits('1.14', TOKEN_EXCHANGE_RATE_DECIMALS),
// https://www.coingecko.com/en/coins/injective
injective: ethers.utils.parseUnits('35.07', TOKEN_EXCHANGE_RATE_DECIMALS),
inevm: ethers.utils.parseUnits('35.07', TOKEN_EXCHANGE_RATE_DECIMALS), // 1:1 injective
// https://www.coingecko.com/en/coins/viction
viction: ethers.utils.parseUnits('0.726', TOKEN_EXCHANGE_RATE_DECIMALS),
};
import rawGasPrices from './gasPrices.json';
import rawTokenPrices from './tokenPrices.json';

const gasPrices: ChainMap<BigNumber> = objMap(rawGasPrices, (_, gasPrice) =>
ethers.utils.parseUnits(gasPrice, 'gwei'),
);

const tokenUsdPrices: ChainMap<BigNumber> = objMap(
rawTokenPrices,
(_, tokenUsdPrice) =>
ethers.utils.parseUnits(tokenUsdPrice, TOKEN_EXCHANGE_RATE_DECIMALS),
);

// Gets the exchange rate of the remote quoted in local tokens
function getTokenExchangeRate(local: ChainName, remote: ChainName): BigNumber {
Expand Down
18 changes: 18 additions & 0 deletions typescript/infra/config/environments/mainnet3/gasPrices.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"arbitrum": "0.1",
"avalanche": "43.212830197",
"bsc": "1.350070319",
"celo": "10.0",
"ethereum": "26.346912847",
"mantapacific": "0.100000057",
"moonbeam": "125.0",
"optimism": "0.003225814",
"polygon": "61.601287856",
"gnosis": "1.852997796",
"base": "0.0010003",
"scroll": "0.46",
"polygonzkevm": "3.95",
"inevm": "0.1",
"viction": "0.25",
"injective": "0.1"
}
18 changes: 18 additions & 0 deletions typescript/infra/config/environments/mainnet3/tokenPrices.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"arbitrum": "2914.61",
"avalanche": "35.71",
"bsc": "373.19",
"celo": "0.772619",
"ethereum": "2914.61",
"mantapacific": "2914.61",
"moonbeam": "0.419658",
"optimism": "2914.61",
"polygon": "0.982687",
"gnosis": "1.012",
"base": "2914.61",
"scroll": "2914.61",
"polygonzkevm": "2914.61",
"inevm": "32.77",
"viction": "0.750231",
"injective": "32.77"
}
39 changes: 39 additions & 0 deletions typescript/infra/scripts/print-gas-prices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ethers } from 'ethers';

import { MultiProtocolProvider, ProviderType } from '@hyperlane-xyz/sdk';
import { objMap, promiseObjAll } from '@hyperlane-xyz/utils';

import { mainnetConfigs } from '../config/environments/mainnet3/chains';

async function main() {
const metadata = mainnetConfigs;

const mpp = new MultiProtocolProvider(metadata);

const prices = await promiseObjAll(
objMap(metadata, async (chain, _) => {
const provider = mpp.getProvider(chain);
switch (provider.type) {
case ProviderType.EthersV5:
const gasPrice = await provider.provider.getGasPrice();
return ethers.utils.formatUnits(gasPrice, 'gwei');
case ProviderType.CosmJsWasm:
// TODO: get default gas price
return '0.1';
case ProviderType.SolanaWeb3:
return '0.001';
default:
throw new Error(`Unsupported provider type: ${provider.type}`);
}
}),
);

console.log(JSON.stringify(prices, null, 2));
}

main()
.then()
.catch((err) => {
console.error(err);
process.exit(1);
});
33 changes: 33 additions & 0 deletions typescript/infra/scripts/print-token-prices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { objMap } from '@hyperlane-xyz/utils';

import { mainnetConfigs } from '../config/environments/mainnet3/chains';

const CURRENCY = 'usd';

async function main() {
const metadata = mainnetConfigs;

const ids = objMap(
metadata,
(_, metadata) => metadata.gasCurrencyCoinGeckoId ?? metadata.name,
);

const resp = await fetch(
`https://api.coingecko.com/api/v3/simple/price?ids=${Object.entries(
ids,
).join(',')}&vs_currencies=${CURRENCY}`,
);

const idPrices = await resp.json();

const prices = objMap(ids, (_, id) => idPrices[id][CURRENCY].toString());

console.log(JSON.stringify(prices, null, 2));
}

main()
.then()
.catch((err) => {
console.error(err);
process.exit(1);
});
5 changes: 5 additions & 0 deletions typescript/sdk/src/consts/chainMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ export const inevm: ChainMetadata = {
displayName: 'Injective EVM',
displayNameShort: 'inEVM',
domainId: 2525,
gasCurrencyCoinGeckoId: 'injective-protocol',
name: Chains.inevm,
nativeToken: {
decimals: 18,
Expand All @@ -433,6 +434,7 @@ export const injective: ChainMetadata = {
chainId: 'injective-1',
displayName: 'Injective',
domainId: 6909546,
gasCurrencyCoinGeckoId: 'injective-protocol',
grpcUrls: [{ http: 'sentry.chain.grpc.injective.network:443' }],
name: Chains.injective,
nativeToken: {
Expand Down Expand Up @@ -488,6 +490,7 @@ export const mantapacific: ChainMetadata = {
displayName: 'Manta Pacific',
displayNameShort: 'Manta',
domainId: 169,
gasCurrencyCoinGeckoId: 'ethereum',
isTestnet: false,
name: Chains.mantapacific,
nativeToken: {
Expand Down Expand Up @@ -603,6 +606,7 @@ export const neutron: ChainMetadata = {
chainId: 'neutron-1',
displayName: 'Neutron',
domainId: 1853125230,
gasCurrencyCoinGeckoId: 'neutron-3',
grpcUrls: [{ http: 'grpc-kralum.neutron-1.neutron.org:80' }],
isTestnet: false,
name: Chains.neutron,
Expand Down Expand Up @@ -1046,6 +1050,7 @@ export const viction: ChainMetadata = {
chainId: 88,
displayName: 'Viction',
domainId: 88,
gasCurrencyCoinGeckoId: 'tomochain',
name: Chains.viction,
nativeToken: {
decimals: 18,
Expand Down

0 comments on commit f606e3e

Please sign in to comment.