Skip to content

Commit

Permalink
cloud_functions: clean up code.
Browse files Browse the repository at this point in the history
  • Loading branch information
panoel committed Aug 8, 2024
1 parent c886d54 commit 4d33836
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 35 deletions.
9 changes: 5 additions & 4 deletions cloud_functions/src/computeNTTRateLimits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getNetwork,
NTTEvmChain,
NTTChain,
isValidNTTChain,
} from '@wormhole-foundation/wormhole-monitor-common';
import { EvmPlatform, EvmChains } from '@wormhole-foundation/sdk-evm';
import { SolanaPlatform } from '@wormhole-foundation/sdk-solana';
Expand Down Expand Up @@ -54,7 +55,7 @@ async function computeNTTRateLimits_(
});
tokenDecimals = await getSolanaTokenDecimals(rpcEndpoint, tokenAddress);
} else {
const evmChain = chain as NTTEvmChain;
const evmChain = chain;
const platform = new EvmPlatform(network);
ntt = new EvmNtt(network, evmChain, platform.getRpc(evmChain), {
ntt: {
Expand All @@ -75,12 +76,12 @@ async function computeNTTRateLimits_(
let totalInboundCapacity = 0n;
const inboundRateLimits = await Promise.all(
inboundChains.map(async (inboundChain): Promise<NTTRateLimit> => {
const inboundCapacity = await ntt.getCurrentInboundCapacity(inboundChain as NTTChain);
const inboundCapacity = await ntt.getCurrentInboundCapacity(inboundChain);
totalInboundCapacity += inboundCapacity;

return {
tokenName: token,
srcChain: chainToChainId(inboundChain as Chain),
srcChain: chainToChainId(inboundChain),
destChain: chainToChainId(chain),
amount: {
amount: inboundCapacity.toString(),
Expand Down Expand Up @@ -124,7 +125,7 @@ export async function computeNTTRateLimits(req: any, res: any) {
Object.entries(managerContracts).map(async ([token, manager]) => {
const inboundCapacityPromises = Object.entries(manager)
.map(([chain, contract]) =>
contract ? computeNTTRateLimits_(network, token, chain as NTTChain) : null
contract && isValidNTTChain(chain) ? computeNTTRateLimits_(network, token, chain) : null
)
.filter(Boolean);

Expand Down
15 changes: 10 additions & 5 deletions cloud_functions/src/computeTotalSupplyAndLocked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getNetwork,
normalizeToDecimals,
NTTChain,
isValidNTTChain,
} from '@wormhole-foundation/wormhole-monitor-common';
import { PublicKey } from '@solana/web3.js';
import { Network, rpc, chainToChainId } from '@wormhole-foundation/sdk-base';
Expand Down Expand Up @@ -41,19 +42,23 @@ async function fetchTotalSupplyAndLocked(network: Network): Promise<NTTTotalSupp
let cumulativeEvmSupply = 0n;
for (const [supportedChain] of Object.entries(NTT_TOKENS[network][token])) {
if (supportedChain === 'Solana') continue;
if (!isValidNTTChain(supportedChain)) {
console.error(`Invalid chain: ${supportedChain}`);
continue;
}
const tokenSupply = await getEvmTotalSupply(
rpc.rpcAddress(network, supportedChain as NTTChain),
NTT_TOKENS[network][token][supportedChain as NTTChain]!
rpc.rpcAddress(network, supportedChain),
NTT_TOKENS[network][token][supportedChain]!
);

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

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

// This data structure is used in dashboard
export type NTTContract = {
Expand Down Expand Up @@ -113,19 +114,6 @@ export const NTT_TOKENS: NTTContract = {
Devnet: {},
};

export type NTTChain =
| 'Ethereum'
| 'Fantom'
| 'Solana'
| 'Arbitrum'
| 'Optimism'
| 'Base'
| 'Sepolia'
| 'Holesky'
| 'ArbitrumSepolia'
| 'BaseSepolia'
| 'OptimismSepolia';

const nttChains = [
'Ethereum',
'Fantom',
Expand All @@ -137,19 +125,15 @@ const nttChains = [
'ArbitrumSepolia',
'BaseSepolia',
'OptimismSepolia',
];

export type NTTEvmChain =
| 'Ethereum'
| 'Fantom'
| 'Arbitrum'
| 'Optimism'
| 'Base'
| 'Sepolia'
| 'Holesky'
| 'ArbitrumSepolia'
| 'BaseSepolia'
| 'OptimismSepolia';
] as const;

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

export type NTTEvmChain = NTTChain & EvmChains;

export function isValidNTTChain(chain: string): chain is NTTChain {
return (nttChains as readonly string[]).includes(chain);
}

export const NTT_MANAGER_CONTRACT_ARRAY =
convertNTTManagerContractToNTTContractArray(NTT_MANAGER_CONTRACT);
Expand Down

0 comments on commit 4d33836

Please sign in to comment.