Skip to content

Commit

Permalink
dashboard, watcher: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
panoel committed Jan 25, 2024
1 parent c5c9f5d commit ede02f2
Show file tree
Hide file tree
Showing 51 changed files with 327 additions and 429 deletions.
17 changes: 9 additions & 8 deletions cloud_functions/src/alarmMissingVaas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { CHAIN_ID_TO_NAME, ChainId, ChainName } from '@certusone/wormhole-sdk';
import { MissingVaasByChain, commonGetMissingVaas } from './getMissingVaas';
import { assertEnvironmentVariable, formatAndSendToSlack, isVAASigned } from './utils';
import { ObservedMessage, ReobserveInfo, SlackInfo } from './types';
import { NETWORK, explorerBlock, explorerTx } from '@wormhole-foundation/wormhole-monitor-common';
import {
Environment,
getEnvironment,
explorerBlock,
explorerTx,
} from '@wormhole-foundation/wormhole-monitor-common';
import { Firestore } from 'firebase-admin/firestore';

interface EnqueuedVAAResponse {
Expand Down Expand Up @@ -34,10 +39,7 @@ interface GovernedVAA {
// The key is the vaaKey
type GovernedVAAMap = Map<string, GovernedVAA>;

const network: NETWORK =
assertEnvironmentVariable('NETWORK').toLowerCase() === 'mainnet'
? NETWORK.MAINNET
: NETWORK.TESTNET;
const network: Environment = getEnvironment();

export async function alarmMissingVaas(req: any, res: any) {
res.set('Access-Control-Allow-Origin', '*');
Expand Down Expand Up @@ -127,7 +129,7 @@ export async function alarmMissingVaas(req: any, res: any) {
txhash: msg.txHash,
vaaKey: vaaKey,
});
if (network === NETWORK.MAINNET) {
if (network === 'mainnet') {
alarmSlackInfo.msg = formatMessage(msg);
await formatAndSendToSlack(alarmSlackInfo);
}
Expand Down Expand Up @@ -323,7 +325,6 @@ async function alarmOldBlockTimes(latestTimes: LatestTimeByChain): Promise<void>
bannerTxt: 'Wormhole Missing VAA Alarm',
msg: '',
};
const envNetwork: string = network === NETWORK.MAINNET ? 'mainnet' : 'testnet';

let alarmsToStore: AlarmedChainTime[] = [];
// Read in the already alarmed chains.
Expand Down Expand Up @@ -351,7 +352,7 @@ async function alarmOldBlockTimes(latestTimes: LatestTimeByChain): Promise<void>
const chainTime: Date = new Date(latestTime);
const cName: string = CHAIN_ID_TO_NAME[chainId] as ChainName;
const deltaTime: number = (now.getTime() - chainTime.getTime()) / (1000 * 60 * 60 * 24);
alarmSlackInfo.msg = `*Chain:* ${cName}(${chainId})\nThe ${envNetwork} watcher is behind by ${deltaTime} days.`;
alarmSlackInfo.msg = `*Chain:* ${cName}(${chainId})\nThe ${network} watcher is behind by ${deltaTime} days.`;
await formatAndSendToSlack(alarmSlackInfo);
alarmsToStore.push({ chain: chainId, alarmTime: now.toISOString() });
}
Expand Down
26 changes: 16 additions & 10 deletions common/src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,19 @@ import {
coalesceChainName,
} from '@certusone/wormhole-sdk';

export enum NETWORK {
MAINNET,
TESTNET,
}
export type Environment = 'mainnet' | 'testnet' | 'devnet';
export type Network = {
env: Environment;
endpoint: string;
name: string;
logo: string;
type: 'guardian' | 'cloudfunction';
};

export const INITIAL_DEPLOYMENT_BLOCK_BY_NETWORK_AND_CHAIN: {
[key in NETWORK]: { [key in ChainName]?: string };
[key in Environment]: { [key in ChainName]?: string };
} = {
[NETWORK.MAINNET]: {
['mainnet']: {
ethereum: '12959638',
terra: '4810000', // not sure exactly but this should be before the first known message
bsc: '9745450',
Expand All @@ -66,7 +70,7 @@ export const INITIAL_DEPLOYMENT_BLOCK_BY_NETWORK_AND_CHAIN: {
sei: '238594',
wormchain: '4510119', // https://bigdipper.live/wormhole/transactions/4D861F1BE86325D227FA006CA2745BBC6748AF5B5E0811DE536D02792928472A },
},
[NETWORK.TESTNET]: {
['testnet']: {
ethereum: '0',
terra: '0',
bsc: '0',
Expand All @@ -93,6 +97,7 @@ export const INITIAL_DEPLOYMENT_BLOCK_BY_NETWORK_AND_CHAIN: {
sei: '0',
wormchain: '0',
},
['devnet']: {},
};

export const TOKEN_BRIDGE_EMITTERS: { [key in ChainName]?: string } = {
Expand Down Expand Up @@ -179,8 +184,8 @@ export type CHAIN_INFO = {
explorerStem: string;
};

export const CHAIN_INFO_MAP: { [key in NETWORK]: { [key: string]: CHAIN_INFO } } = {
[NETWORK.MAINNET]: {
export const CHAIN_INFO_MAP: { [key in Environment]: { [key: string]: CHAIN_INFO } } = {
['mainnet']: {
1: {
name: 'solana',
evm: false,
Expand Down Expand Up @@ -371,7 +376,7 @@ export const CHAIN_INFO_MAP: { [key in NETWORK]: { [key: string]: CHAIN_INFO } }
explorerStem: '',
},
},
[NETWORK.TESTNET]: {
['testnet']: {
// The chains not, currently, supported on testnet are commented out.
1: {
name: 'solana',
Expand Down Expand Up @@ -556,6 +561,7 @@ export const CHAIN_INFO_MAP: { [key in NETWORK]: { [key: string]: CHAIN_INFO } }
// explorerStem: '',
// },
},
['devnet']: {},
};

export const JUMP_GUARDIAN_ADDRESS = '58cc3ae5c097b213ce3c81979e1b9f9570746aa5';
Expand Down
12 changes: 6 additions & 6 deletions common/src/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ import {
CHAIN_ID_XPLA,
} from '@certusone/wormhole-sdk';
import { base58 } from 'ethers/lib/utils';
import { CHAIN_INFO_MAP, NETWORK } from './consts';
import { CHAIN_INFO_MAP, Environment } from './consts';

export const explorerBlock = (network: NETWORK, chainId: ChainId, block: string) =>
network === NETWORK.MAINNET
export const explorerBlock = (network: Environment, chainId: ChainId, block: string) =>
network === 'mainnet'
? chainId === CHAIN_ID_ETH
? `https://etherscan.io/block/${block}`
: chainId === CHAIN_ID_BSC
Expand Down Expand Up @@ -120,8 +120,8 @@ export const explorerBlock = (network: NETWORK, chainId: ChainId, block: string)
? `https://goerli.basescan.org/block/${block}`
: '';

export const explorerTx = (network: NETWORK, chainId: ChainId, tx: string) =>
network === NETWORK.MAINNET
export const explorerTx = (network: Environment, chainId: ChainId, tx: string) =>
network === 'mainnet'
? chainId === CHAIN_ID_ETH
? `https://etherscan.io/tx/${tx}`
: chainId === CHAIN_ID_BSC
Expand Down Expand Up @@ -212,7 +212,7 @@ export const explorerVaa = (network: string, key: string) =>
? `https://wormholescan.io/#/tx/${key}`
: `https://wormholescan.io/#/tx/${key}?network=TESTNET`;

export const getExplorerTxHash = (network: NETWORK, chain: ChainId, txHash: string) => {
export const getExplorerTxHash = (network: Environment, chain: ChainId, txHash: string) => {
let explorerTxHash = '';
if (isCosmWasmChain(chain)) {
explorerTxHash = txHash.slice(2);
Expand Down
10 changes: 10 additions & 0 deletions common/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Environment } from './consts';

export async function sleep(timeout: number) {
return new Promise((resolve) => setTimeout(resolve, timeout));
}
Expand All @@ -13,3 +15,11 @@ export const padUint64 = (s: string): string => s.padStart(MAX_UINT_64.length, '
// make a bigtable row key for the `signedVAAs` table
export const makeSignedVAAsRowKey = (chain: number, emitter: string, sequence: string): string =>
`${padUint16(chain.toString())}/${emitter}/${padUint64(sequence)}`;

export function getEnvironment(): Environment {
const network: string = assertEnvironmentVariable('NETWORK').toLowerCase();
if (network === 'mainnet' || network === 'testnet' || network === 'devnet') {
return network;
}
throw new Error(`Unknown network: ${network}`);
}
32 changes: 7 additions & 25 deletions dashboard/src/components/Accountant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
Card,
InputAdornment,
LinearProgress,
Link,
TextField,
Tooltip,
Typography,
Expand All @@ -29,16 +28,13 @@ import useGetAccountantPendingTransfers, {
} from '../hooks/useGetAccountantPendingTransfers';
import chainIdToName from '../utils/chainIdToName';
import { CHAIN_ICON_MAP, GUARDIAN_SET_3 } from '../utils/consts';
import {
CHAIN_INFO_MAP,
explorerTx,
getExplorerTxHash,
} from '@wormhole-foundation/wormhole-monitor-common';
import { CHAIN_INFO_MAP } from '@wormhole-foundation/wormhole-monitor-common';
import CollapsibleSection from './CollapsibleSection';
import Table from './Table';
import useTokenData, { TokenDataEntry } from '../hooks/useTokenData';
import numeral from 'numeral';
import { GetNetworkFromEnv } from '../utils/GetNetworkFromEnv';
import { useCurrentEnvironment } from '../contexts/NetworkContext';
import { ExplorerTxHash } from './ExplorerTxHash';

type PendingTransferForAcct = PendingTransfer & { isEnqueuedInGov: boolean };
type AccountWithTokenData = Account & {
Expand Down Expand Up @@ -143,23 +139,9 @@ const pendingTransferColumns = [
}),
pendingTransferColumnHelper.accessor('data.0.tx_hash', {
header: () => 'Tx',
cell: (info) => {
const tx = '0x' + Buffer.from(info.getValue(), 'base64').toString('hex');
const chain = info.row.original.key.emitter_chain;
const network = GetNetworkFromEnv();
const chainInfo = CHAIN_INFO_MAP[network][chain];
if (!chainInfo) return tx;
const txHash = getExplorerTxHash(network, chainInfo.chainId, tx);
return (
<Link
href={explorerTx(network, chainInfo.chainId, txHash)}
target="_blank"
rel="noopener noreferrer"
>
{txHash}
</Link>
);
},
cell: (info) => (
<ExplorerTxHash chain={info.row.original.key.emitter_chain} rawTxHash={info.getValue()} />
),
}),
pendingTransferColumnHelper.accessor('data.0.signatures', {
header: () => 'Signatures',
Expand Down Expand Up @@ -430,7 +412,7 @@ function Accountant({ governorInfo }: { governorInfo: CloudGovernorInfo }) {
}, {} as { [chainId: number]: number }),
[pendingTransferInfo]
);
const network = GetNetworkFromEnv();
const network = useCurrentEnvironment();
return (
<CollapsibleSection
defaultExpanded={false}
Expand Down
38 changes: 38 additions & 0 deletions dashboard/src/components/ExplorerAssetURL.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { CHAIN_INFO_MAP } from '@wormhole-foundation/wormhole-monitor-common';
import { useCurrentEnvironment } from '../contexts/NetworkContext';
import {
CHAIN_ID_ALGORAND,
CHAIN_ID_NEAR,
CHAIN_ID_TERRA2,
ChainId,
tryHexToNativeAssetString,
} from '@certusone/wormhole-sdk';
import { Link } from '@mui/material';

export function ExplorerAssetURL({ chain, assetAddr }: { chain: number; assetAddr: string }) {
const network = useCurrentEnvironment();
const chainInfo = CHAIN_INFO_MAP[network][chain];
if (!chainInfo) return <>{assetAddr}</>;
const chainId: ChainId = chainInfo.chainId;
var tokenAddress: string = '';
if (chainId === CHAIN_ID_ALGORAND || chainId === CHAIN_ID_NEAR || chainId === CHAIN_ID_TERRA2) {
return <>{assetAddr}</>;
}
try {
tokenAddress = tryHexToNativeAssetString(
assetAddr.slice(2),
CHAIN_INFO_MAP[network][chain]?.chainId
);
} catch (e) {
console.log(e);
tokenAddress = assetAddr;
}

const explorerString = chainInfo?.explorerStem;
const url = `${explorerString}/address/${tokenAddress}`;
return (
<Link href={url} target="_blank" rel="noopener noreferrer">
{tokenAddress}
</Link>
);
}
23 changes: 23 additions & 0 deletions dashboard/src/components/ExplorerTxHash.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
CHAIN_INFO_MAP,
explorerTx,
getExplorerTxHash,
} from '@wormhole-foundation/wormhole-monitor-common';
import { useCurrentEnvironment } from '../contexts/NetworkContext';
import { Link } from '@mui/material';

export function ExplorerTxHash({ chain, rawTxHash }: { chain: number; rawTxHash: string }) {
const network = useCurrentEnvironment();
const chainInfo = CHAIN_INFO_MAP[network][chain];
if (!chainInfo) return <>{rawTxHash}</>;
const txHash = getExplorerTxHash(network, chainInfo.chainId, rawTxHash);
return (
<Link
href={explorerTx(network, chainInfo.chainId, txHash)}
target="_blank"
rel="noopener noreferrer"
>
{txHash}
</Link>
);
}
Loading

0 comments on commit ede02f2

Please sign in to comment.