Skip to content

Commit

Permalink
cloud_functions: add NTT specific types
Browse files Browse the repository at this point in the history
  • Loading branch information
panoel committed Aug 8, 2024
1 parent bb22378 commit 86417fb
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 55 deletions.
21 changes: 11 additions & 10 deletions cloud_functions/src/computeNTTRateLimits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import {
getSolanaTokenDecimals,
NTTRateLimit,
getNetwork,
NTTEvmChain,
NTTChain,
nttChains,
} from '@wormhole-foundation/wormhole-monitor-common';
import { EvmPlatform, EvmChains } from '@wormhole-foundation/sdk-evm';
import { EvmPlatform } from '@wormhole-foundation/sdk-evm';
import { SolanaPlatform } from '@wormhole-foundation/sdk-solana';
import { EvmNtt } from '@wormhole-foundation/sdk-evm-ntt';
import { SolanaNtt } from '@wormhole-foundation/sdk-solana-ntt';
import { Network, Chain, contracts, rpc, chainToChainId } from '@wormhole-foundation/sdk-base';
import { Network, contracts, rpc, chainToChainId } from '@wormhole-foundation/sdk-base';
import { Storage } from '@google-cloud/storage';

const storage = new Storage();
Expand All @@ -29,11 +32,11 @@ const cloudStorageCache = cacheBucket.file(cacheFileName);
async function computeNTTRateLimits_(
network: Network,
token: string,
chain: Chain
chain: NTTChain
): Promise<NTTRateLimit> {
let ntt: EvmNtt<Network, EvmChains> | SolanaNtt<Network, 'Solana'>;
let ntt: EvmNtt<Network, NTTEvmChain> | SolanaNtt<Network, 'Solana'>;
let tokenDecimals: number;
const rpcEndpoint = rpc.rpcAddress(network, chain as Chain);
const rpcEndpoint = rpc.rpcAddress(network, chain);
const tokenAddress = NTT_TOKENS[network][token][chain]!;
const managerContract = NTT_MANAGER_CONTRACT[network][token][chain]!;
const transceiverContract = NTT_TRANSCEIVER_CONTRACT[network][token][chain]!;
Expand All @@ -52,7 +55,7 @@ async function computeNTTRateLimits_(
});
tokenDecimals = await getSolanaTokenDecimals(rpcEndpoint, tokenAddress);
} else {
const evmChain = chain as EvmChains;
const evmChain = chain;
const platform = new EvmPlatform(network);
ntt = new EvmNtt(network, evmChain, platform.getRpc(evmChain), {
ntt: {
Expand Down Expand Up @@ -120,10 +123,8 @@ export async function computeNTTRateLimits(req: any, res: any) {

const rateLimits = await Promise.all(
Object.entries(managerContracts).map(async ([token, manager]) => {
const inboundCapacityPromises = Object.entries(manager)
.map(([chain, contract]) =>
contract ? computeNTTRateLimits_(network, token, chain as Chain) : null
)
const inboundCapacityPromises = nttChains
.map((chain) => (manager[chain] ? computeNTTRateLimits_(network, token, chain) : null))
.filter(Boolean);

const inboundCapacity = await Promise.all(inboundCapacityPromises);
Expand Down
18 changes: 11 additions & 7 deletions cloud_functions/src/computeTotalSupplyAndLocked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import {
getEvmTotalSupply,
getNetwork,
normalizeToDecimals,
nttChains,
} from '@wormhole-foundation/wormhole-monitor-common';
import { PublicKey } from '@solana/web3.js';
import { Network, rpc, Chain, chainToChainId } from '@wormhole-foundation/sdk-base';
import { Network, rpc, chainToChainId } from '@wormhole-foundation/sdk-base';
import { Storage } from '@google-cloud/storage';

const storage = new Storage();
Expand All @@ -38,21 +39,24 @@ async function fetchTotalSupplyAndLocked(network: Network): Promise<NTTTotalSupp

const evmTotalSupply: NTTTotalSupplyAndLockedData[] = [];
let cumulativeEvmSupply = 0n;
for (const [supportedChain] of Object.entries(NTT_TOKENS[network][token])) {
for (const supportedChain of nttChains) {
const tokenContract = NTT_TOKENS[network][token][supportedChain];
const managerContract = NTT_MANAGER_CONTRACT[network][token][supportedChain];
if (!tokenContract || !managerContract) continue;
if (supportedChain === 'Solana') continue;
const tokenSupply = await getEvmTotalSupply(
rpc.rpcAddress(network, supportedChain as Chain),
NTT_TOKENS[network][token][supportedChain as Chain]!
rpc.rpcAddress(network, supportedChain),
tokenContract
);

const tokenDecimals = await getEvmTokenDecimals(
rpc.rpcAddress(network, supportedChain as Chain),
NTT_MANAGER_CONTRACT[network][token][supportedChain as Chain]!
rpc.rpcAddress(network, supportedChain),
managerContract
);

evmTotalSupply.push({
tokenName: token,
chain: chainToChainId(supportedChain as Chain),
chain: chainToChainId(supportedChain),
totalSupply: {
amount: tokenSupply.toString(),
decimals: tokenDecimals,
Expand Down
45 changes: 32 additions & 13 deletions common/src/nttConsts.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
import { Chain, Network, chains } from '@wormhole-foundation/sdk-base';
import { Network, networks } from '@wormhole-foundation/sdk-base';
import { EvmChains } from '@wormhole-foundation/sdk-evm';

// This data structure is used in dashboard
export type NTTContract = {
[key in Network]: { [tokenName: string]: { [key in Chain]?: string } };
[key in Network]: { [tokenName: string]: { [key in NTTChain]?: string } };
};

// This data structure is used in watchers
export type NTTContractArray = {
[key in Network]: { [key in Chain]?: string[] };
[key in Network]: { [key in NTTChain]?: string[] };
};

export const nttChains = [
'Ethereum',
'Fantom',
'Solana',
'Arbitrum',
'Optimism',
'Base',
'Sepolia',
'ArbitrumSepolia',
'BaseSepolia',
'OptimismSepolia',
'Holesky',
] as const;

function convertNTTManagerContractToNTTContractArray(
nttManagerContract: NTTContract
): NTTContractArray {
const nttContract: NTTContractArray = {} as NTTContractArray;

for (const network in nttManagerContract) {
nttContract[network as Network] = {};
for (const network of networks) {
nttContract[network] = {};

for (const tokenName in nttManagerContract[network as Network]) {
for (const chain in nttManagerContract[network as Network][tokenName]) {
const tokenAddress = nttManagerContract[network as Network][tokenName][chain as Chain];
for (const tokenName in nttManagerContract[network]) {
for (const chain of nttChains) {
const tokenAddress = nttManagerContract[network][tokenName][chain];

if (tokenAddress) {
if (!nttContract[network as Network][chain as Chain]) {
nttContract[network as Network][chain as Chain] = [];
if (!nttContract[network][chain]) {
nttContract[network][chain] = [];
}
nttContract[network as Network][chain as Chain]!.push(tokenAddress);
nttContract[network][chain]!.push(tokenAddress);
}
}
}
Expand Down Expand Up @@ -113,14 +128,18 @@ export const NTT_TOKENS: NTTContract = {
Devnet: {},
};

export type NTTChain = (typeof nttChains)[number];

export type NTTEvmChain = NTTChain & EvmChains;

export const NTT_MANAGER_CONTRACT_ARRAY =
convertNTTManagerContractToNTTContractArray(NTT_MANAGER_CONTRACT);

export function NTT_SUPPORTED_CHAINS(network: Network, token: string): Chain[] {
export function NTT_SUPPORTED_CHAINS(network: Network, token: string): NTTChain[] {
const contractDetails = NTT_MANAGER_CONTRACT[network][token];
if (!contractDetails) {
return [];
}

return chains.filter((chain) => chain in contractDetails);
return nttChains.filter((chain) => chain in contractDetails);
}
2 changes: 1 addition & 1 deletion dashboard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@types/node": "^18.6.4",
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6",
"@wormhole-foundation/sdk-icons": "^0.6.6-beta.1",
"@wormhole-foundation/sdk-icons": "^0.9.1",
"buffer": "^6.0.3",
"numeral": "^2.0.6",
"react": "^18.2.0",
Expand Down
18 changes: 5 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion watcher/src/watchers/EVMWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class EVMWatcher extends Watcher {
this.lastTimestamp = 0;
this.latestFinalizedBlockNumber = 0;
this.finalizedBlockTag = finalizedBlockTag;
if (chain === 'Acala' || chain === 'Karura') {
if (chain === 'Acala' || chain === 'Karura' || chain === 'Berachain') {
this.maximumBatchSize = 50;
}
}
Expand Down
14 changes: 8 additions & 6 deletions watcher/src/watchers/NTTWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import {
chainToChainId,
contracts,
} from '@wormhole-foundation/sdk-base';
import { assertEnvironmentVariable } from '@wormhole-foundation/wormhole-monitor-common';
import {
assertEnvironmentVariable,
NTTChain,
NTTEvmChain,
} from '@wormhole-foundation/wormhole-monitor-common';
import axios from 'axios';
import { BigNumber } from 'ethers';
import knex, { Knex } from 'knex';
Expand Down Expand Up @@ -48,17 +52,15 @@ export type ErrorBlock = {
};

export class NTTWatcher extends Watcher {
chain: NTTChain;
finalizedBlockTag: BlockTag;
lastTimestamp: number;
latestFinalizedBlockNumber: number;
pg: Knex;

constructor(
network: Network,
chain: PlatformToChains<'Evm'>,
finalizedBlockTag: BlockTag = 'latest'
) {
constructor(network: Network, chain: NTTEvmChain, finalizedBlockTag: BlockTag = 'latest') {
super(network, chain, 'ntt');
this.chain = chain;
this.lastTimestamp = 0;
this.latestFinalizedBlockNumber = 0;
this.finalizedBlockTag = finalizedBlockTag;
Expand Down
8 changes: 4 additions & 4 deletions watcher/src/watchers/__tests__/NearWatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ describe('getNearProvider', () => {

test('with archive RPC', async () => {
const provider = await getNearProvider('Mainnet', NEAR_ARCHIVE_RPC);
// grab first block with activity from core contract
expect(
await provider.block({ blockId: 'Asie8hpJFKaipvw8jh1wPfBwwbjP6JUfsQdCuQvwr3Sz' })
).toBeTruthy();
const retval = await provider.block({
blockId: '8NWwminTTAPwYzzqTeP8c3MEGXgTretBWpCSSvSqJXdv',
});
expect(retval).toBeTruthy();
});
});

Expand Down

0 comments on commit 86417fb

Please sign in to comment.