|
|
@ -7,10 +7,13 @@ import { ERC20__factory } from '@hyperlane-xyz/core'; |
|
|
|
import { |
|
|
|
import { |
|
|
|
ChainMap, |
|
|
|
ChainMap, |
|
|
|
ChainName, |
|
|
|
ChainName, |
|
|
|
|
|
|
|
CosmNativeTokenAdapter, |
|
|
|
CwNativeTokenAdapter, |
|
|
|
CwNativeTokenAdapter, |
|
|
|
MultiProtocolProvider, |
|
|
|
MultiProtocolProvider, |
|
|
|
SealevelHypCollateralAdapter, |
|
|
|
SealevelHypCollateralAdapter, |
|
|
|
TokenType, |
|
|
|
TokenType, |
|
|
|
|
|
|
|
WarpRouteConfig, |
|
|
|
|
|
|
|
WarpRouteConfigSchema, |
|
|
|
} from '@hyperlane-xyz/sdk'; |
|
|
|
} from '@hyperlane-xyz/sdk'; |
|
|
|
import { |
|
|
|
import { |
|
|
|
ProtocolType, |
|
|
|
ProtocolType, |
|
|
@ -19,12 +22,8 @@ import { |
|
|
|
promiseObjAll, |
|
|
|
promiseObjAll, |
|
|
|
} from '@hyperlane-xyz/utils'; |
|
|
|
} from '@hyperlane-xyz/utils'; |
|
|
|
|
|
|
|
|
|
|
|
import { |
|
|
|
|
|
|
|
WarpTokenConfig, |
|
|
|
|
|
|
|
nautilusList, |
|
|
|
|
|
|
|
neutronList, |
|
|
|
|
|
|
|
} from '../../src/config/grafana_token_config'; |
|
|
|
|
|
|
|
import { startMetricsServer } from '../../src/utils/metrics'; |
|
|
|
import { startMetricsServer } from '../../src/utils/metrics'; |
|
|
|
|
|
|
|
import { readYaml } from '../../src/utils/utils'; |
|
|
|
|
|
|
|
|
|
|
|
const metricsRegister = new Registry(); |
|
|
|
const metricsRegister = new Registry(); |
|
|
|
const warpRouteTokenBalance = new Gauge({ |
|
|
|
const warpRouteTokenBalance = new Gauge({ |
|
|
@ -40,20 +39,36 @@ const warpRouteTokenBalance = new Gauge({ |
|
|
|
], |
|
|
|
], |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function readWarpRouteConfig(filePath: string) { |
|
|
|
|
|
|
|
const config = readYaml(filePath); |
|
|
|
|
|
|
|
if (!config) throw new Error(`No warp config found at ${filePath}`); |
|
|
|
|
|
|
|
const result = WarpRouteConfigSchema.safeParse(config); |
|
|
|
|
|
|
|
if (!result.success) { |
|
|
|
|
|
|
|
const errorMessages = result.error.issues.map( |
|
|
|
|
|
|
|
(issue: any) => `${issue.path} => ${issue.message}`, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
throw new Error(`Invalid warp config:\n ${errorMessages.join('\n')}`); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return result.data; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function main(): Promise<boolean> { |
|
|
|
async function main(): Promise<boolean> { |
|
|
|
const { checkFrequency, config } = await yargs(process.argv.slice(2)) |
|
|
|
const { checkFrequency, filePath } = await yargs(process.argv.slice(2)) |
|
|
|
.describe('checkFrequency', 'frequency to check balances in ms') |
|
|
|
.describe('checkFrequency', 'frequency to check balances in ms') |
|
|
|
.demandOption('checkFrequency') |
|
|
|
.demandOption('checkFrequency') |
|
|
|
.alias('l', 'checkFrequency') |
|
|
|
.alias('v', 'checkFrequency') // v as in Greek letter nu
|
|
|
|
.number('checkFrequency') |
|
|
|
.number('checkFrequency') |
|
|
|
.alias('c', 'config') |
|
|
|
.alias('f', 'filePath') |
|
|
|
.describe('config', 'choose warp token config') |
|
|
|
.describe( |
|
|
|
.demandOption('config') |
|
|
|
'filePath', |
|
|
|
.choices('config', ['neutron', 'nautilus']) |
|
|
|
'indicate the filepatch to the warp route yaml file relative to typescript/infra', |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.demandOption('filePath') |
|
|
|
|
|
|
|
.string('filePath') |
|
|
|
.parse(); |
|
|
|
.parse(); |
|
|
|
|
|
|
|
|
|
|
|
const tokenList: WarpTokenConfig = |
|
|
|
const tokenConfig: WarpRouteConfig = |
|
|
|
config === 'neutron' ? neutronList : nautilusList; |
|
|
|
readWarpRouteConfig(filePath).data.config; |
|
|
|
|
|
|
|
|
|
|
|
startMetricsServer(metricsRegister); |
|
|
|
startMetricsServer(metricsRegister); |
|
|
|
|
|
|
|
|
|
|
@ -63,8 +78,8 @@ async function main(): Promise<boolean> { |
|
|
|
setInterval(async () => { |
|
|
|
setInterval(async () => { |
|
|
|
try { |
|
|
|
try { |
|
|
|
debug('Checking balances'); |
|
|
|
debug('Checking balances'); |
|
|
|
const balances = await checkBalance(tokenList, multiProtocolProvider); |
|
|
|
const balances = await checkBalance(tokenConfig, multiProtocolProvider); |
|
|
|
updateTokenBalanceMetrics(tokenList, balances); |
|
|
|
updateTokenBalanceMetrics(tokenConfig, balances); |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
console.error('Error checking balances', e); |
|
|
|
console.error('Error checking balances', e); |
|
|
|
} |
|
|
|
} |
|
|
@ -74,20 +89,18 @@ async function main(): Promise<boolean> { |
|
|
|
|
|
|
|
|
|
|
|
// TODO: see issue https://github.com/hyperlane-xyz/hyperlane-monorepo/issues/2708
|
|
|
|
// TODO: see issue https://github.com/hyperlane-xyz/hyperlane-monorepo/issues/2708
|
|
|
|
async function checkBalance( |
|
|
|
async function checkBalance( |
|
|
|
tokenConfig: WarpTokenConfig, |
|
|
|
tokenConfig: WarpRouteConfig, |
|
|
|
multiProtocolProvider: MultiProtocolProvider, |
|
|
|
multiProtocolProvider: MultiProtocolProvider, |
|
|
|
): Promise<ChainMap<number>> { |
|
|
|
): Promise<ChainMap<number>> { |
|
|
|
const output: ChainMap<Promise<number>> = objMap( |
|
|
|
const output = objMap( |
|
|
|
tokenConfig, |
|
|
|
tokenConfig, |
|
|
|
async (chain: ChainName, token: WarpTokenConfig[ChainName]) => { |
|
|
|
async (chain: ChainName, token: WarpRouteConfig[ChainName]) => { |
|
|
|
switch (token.type) { |
|
|
|
switch (token.type) { |
|
|
|
case TokenType.native: { |
|
|
|
case TokenType.native: { |
|
|
|
switch (token.protocolType) { |
|
|
|
switch (token.protocolType) { |
|
|
|
case ProtocolType.Ethereum: { |
|
|
|
case ProtocolType.Ethereum: { |
|
|
|
const provider = multiProtocolProvider.getEthersV5Provider(chain); |
|
|
|
const provider = multiProtocolProvider.getEthersV5Provider(chain); |
|
|
|
const nativeBalance = await provider.getBalance( |
|
|
|
const nativeBalance = await provider.getBalance(token.hypAddress); |
|
|
|
token.hypNativeAddress, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
return parseFloat( |
|
|
|
return parseFloat( |
|
|
|
ethers.utils.formatUnits(nativeBalance, token.decimals), |
|
|
|
ethers.utils.formatUnits(nativeBalance, token.decimals), |
|
|
|
); |
|
|
|
); |
|
|
@ -95,9 +108,20 @@ async function checkBalance( |
|
|
|
case ProtocolType.Sealevel: |
|
|
|
case ProtocolType.Sealevel: |
|
|
|
// TODO - solana native
|
|
|
|
// TODO - solana native
|
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
case ProtocolType.Cosmos: |
|
|
|
case ProtocolType.Cosmos: { |
|
|
|
// TODO - cosmos native
|
|
|
|
if (!token.ibcDenom) |
|
|
|
return 0; |
|
|
|
throw new Error('IBC denom missing for native token'); |
|
|
|
|
|
|
|
const adapter = new CosmNativeTokenAdapter( |
|
|
|
|
|
|
|
chain, |
|
|
|
|
|
|
|
multiProtocolProvider, |
|
|
|
|
|
|
|
{}, |
|
|
|
|
|
|
|
{ ibcDenom: token.ibcDenom }, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
const tokenBalance = await adapter.getBalance(token.hypAddress); |
|
|
|
|
|
|
|
return parseFloat( |
|
|
|
|
|
|
|
ethers.utils.formatUnits(tokenBalance, token.decimals), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
@ -105,12 +129,14 @@ async function checkBalance( |
|
|
|
switch (token.protocolType) { |
|
|
|
switch (token.protocolType) { |
|
|
|
case ProtocolType.Ethereum: { |
|
|
|
case ProtocolType.Ethereum: { |
|
|
|
const provider = multiProtocolProvider.getEthersV5Provider(chain); |
|
|
|
const provider = multiProtocolProvider.getEthersV5Provider(chain); |
|
|
|
|
|
|
|
if (!token.tokenAddress) |
|
|
|
|
|
|
|
throw new Error('Token address missing for collateral token'); |
|
|
|
const tokenContract = ERC20__factory.connect( |
|
|
|
const tokenContract = ERC20__factory.connect( |
|
|
|
token.address, |
|
|
|
token.tokenAddress, |
|
|
|
provider, |
|
|
|
provider, |
|
|
|
); |
|
|
|
); |
|
|
|
const collateralBalance = await tokenContract.balanceOf( |
|
|
|
const collateralBalance = await tokenContract.balanceOf( |
|
|
|
token.hypCollateralAddress, |
|
|
|
token.hypAddress, |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
return parseFloat( |
|
|
|
return parseFloat( |
|
|
@ -118,19 +144,21 @@ async function checkBalance( |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
case ProtocolType.Sealevel: { |
|
|
|
case ProtocolType.Sealevel: { |
|
|
|
|
|
|
|
if (!token.tokenAddress) |
|
|
|
|
|
|
|
throw new Error('Token address missing for synthetic token'); |
|
|
|
const adapter = new SealevelHypCollateralAdapter( |
|
|
|
const adapter = new SealevelHypCollateralAdapter( |
|
|
|
chain, |
|
|
|
chain, |
|
|
|
multiProtocolProvider, |
|
|
|
multiProtocolProvider, |
|
|
|
{ |
|
|
|
{ |
|
|
|
token: token.address, |
|
|
|
token: token.tokenAddress, |
|
|
|
warpRouter: token.hypCollateralAddress, |
|
|
|
warpRouter: token.hypAddress, |
|
|
|
// Mailbox only required for transfers, using system as placeholder
|
|
|
|
// Mailbox only required for transfers, using system as placeholder
|
|
|
|
mailbox: SystemProgram.programId.toBase58(), |
|
|
|
mailbox: SystemProgram.programId.toBase58(), |
|
|
|
}, |
|
|
|
}, |
|
|
|
token.isSpl2022, |
|
|
|
token?.isSpl2022 ?? false, |
|
|
|
); |
|
|
|
); |
|
|
|
const collateralBalance = ethers.BigNumber.from( |
|
|
|
const collateralBalance = ethers.BigNumber.from( |
|
|
|
await adapter.getBalance(token.hypCollateralAddress), |
|
|
|
await adapter.getBalance(token.hypAddress), |
|
|
|
); |
|
|
|
); |
|
|
|
return parseFloat( |
|
|
|
return parseFloat( |
|
|
|
ethers.utils.formatUnits(collateralBalance, token.decimals), |
|
|
|
ethers.utils.formatUnits(collateralBalance, token.decimals), |
|
|
@ -141,12 +169,12 @@ async function checkBalance( |
|
|
|
chain, |
|
|
|
chain, |
|
|
|
multiProtocolProvider, |
|
|
|
multiProtocolProvider, |
|
|
|
{ |
|
|
|
{ |
|
|
|
token: token.address, |
|
|
|
token: token.hypAddress, |
|
|
|
}, |
|
|
|
}, |
|
|
|
token.address, |
|
|
|
token.tokenAddress, |
|
|
|
); |
|
|
|
); |
|
|
|
const collateralBalance = ethers.BigNumber.from( |
|
|
|
const collateralBalance = ethers.BigNumber.from( |
|
|
|
await adapter.getBalance(token.hypCollateralAddress), |
|
|
|
await adapter.getBalance(token.hypAddress), |
|
|
|
); |
|
|
|
); |
|
|
|
return parseFloat( |
|
|
|
return parseFloat( |
|
|
|
ethers.utils.formatUnits(collateralBalance, token.decimals), |
|
|
|
ethers.utils.formatUnits(collateralBalance, token.decimals), |
|
|
@ -160,7 +188,7 @@ async function checkBalance( |
|
|
|
case ProtocolType.Ethereum: { |
|
|
|
case ProtocolType.Ethereum: { |
|
|
|
const provider = multiProtocolProvider.getEthersV5Provider(chain); |
|
|
|
const provider = multiProtocolProvider.getEthersV5Provider(chain); |
|
|
|
const tokenContract = ERC20__factory.connect( |
|
|
|
const tokenContract = ERC20__factory.connect( |
|
|
|
token.hypSyntheticAddress, |
|
|
|
token.hypAddress, |
|
|
|
provider, |
|
|
|
provider, |
|
|
|
); |
|
|
|
); |
|
|
|
const syntheticBalance = await tokenContract.totalSupply(); |
|
|
|
const syntheticBalance = await tokenContract.totalSupply(); |
|
|
@ -178,36 +206,24 @@ async function checkBalance( |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return 0; |
|
|
|
}, |
|
|
|
}, |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
return await promiseObjAll(output); |
|
|
|
return await promiseObjAll(output); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function updateTokenBalanceMetrics( |
|
|
|
export function updateTokenBalanceMetrics( |
|
|
|
tokenConfig: WarpTokenConfig, |
|
|
|
tokenConfig: WarpRouteConfig, |
|
|
|
balances: ChainMap<number>, |
|
|
|
balances: ChainMap<number>, |
|
|
|
) { |
|
|
|
) { |
|
|
|
objMap(tokenConfig, (chain: ChainName, token: WarpTokenConfig[ChainName]) => { |
|
|
|
objMap(tokenConfig, (chain: ChainName, token: WarpRouteConfig[ChainName]) => { |
|
|
|
const tokenAddress = |
|
|
|
|
|
|
|
token.type === TokenType.native |
|
|
|
|
|
|
|
? ethers.constants.AddressZero |
|
|
|
|
|
|
|
: token.type === TokenType.collateral |
|
|
|
|
|
|
|
? token.address |
|
|
|
|
|
|
|
: token.hypSyntheticAddress; |
|
|
|
|
|
|
|
const walletAddress = |
|
|
|
|
|
|
|
token.type === TokenType.native |
|
|
|
|
|
|
|
? token.hypNativeAddress |
|
|
|
|
|
|
|
: token.type === TokenType.collateral |
|
|
|
|
|
|
|
? token.hypCollateralAddress |
|
|
|
|
|
|
|
: token.hypSyntheticAddress; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
warpRouteTokenBalance |
|
|
|
warpRouteTokenBalance |
|
|
|
.labels({ |
|
|
|
.labels({ |
|
|
|
chain_name: chain, |
|
|
|
chain_name: chain, |
|
|
|
token_address: tokenAddress, |
|
|
|
token_address: token.tokenAddress ?? ethers.constants.AddressZero, |
|
|
|
token_name: token.name, |
|
|
|
token_name: token.name, |
|
|
|
wallet_address: walletAddress, |
|
|
|
wallet_address: token.hypAddress, |
|
|
|
token_type: token.type, |
|
|
|
token_type: token.type, |
|
|
|
}) |
|
|
|
}) |
|
|
|
.set(balances[chain]); |
|
|
|
.set(balances[chain]); |
|
|
|