|
| 1 | +import { |
| 2 | + assertEnvironmentVariable, |
| 3 | + NTT_MANAGER_CONTRACT, |
| 4 | + NTT_TOKENS, |
| 5 | + NTT_TRANSCEIVER_CONTRACT, |
| 6 | + NTT_SUPPORTED_CHAINS, |
| 7 | + getEvmTokenDecimals, |
| 8 | + getSolanaTokenDecimals, |
| 9 | + NTTRateLimit, |
| 10 | +} from '@wormhole-foundation/wormhole-monitor-common'; |
| 11 | +import { EvmPlatform, EvmChains } from '@wormhole-foundation/sdk-evm'; |
| 12 | +import { SolanaPlatform } from '@wormhole-foundation/sdk-solana'; |
| 13 | +import { EvmNtt } from '@wormhole-foundation/sdk-evm-ntt'; |
| 14 | +import { SolanaNtt } from '@wormhole-foundation/sdk-solana-ntt'; |
| 15 | +import { Network, Chain, contracts, rpc, chainToChainId } from '@wormhole-foundation/sdk-base'; |
| 16 | +import { Storage } from '@google-cloud/storage'; |
| 17 | + |
| 18 | +const storage = new Storage(); |
| 19 | +let bucketName: string = 'wormhole-ntt-cache'; |
| 20 | +if (assertEnvironmentVariable('NETWORK') === 'Testnet') { |
| 21 | + bucketName = 'wormhole-ntt-cache-testnet'; |
| 22 | +} |
| 23 | + |
| 24 | +const cacheBucket = storage.bucket(bucketName); |
| 25 | +const cacheFileName = 'ntt-rate-limits-cache.json'; |
| 26 | +const cloudStorageCache = cacheBucket.file(cacheFileName); |
| 27 | + |
| 28 | +async function computeNTTRateLimits_( |
| 29 | + network: Network, |
| 30 | + token: string, |
| 31 | + chain: Chain |
| 32 | +): Promise<NTTRateLimit> { |
| 33 | + let ntt: EvmNtt<Network, EvmChains> | SolanaNtt<Network, 'Solana'>; |
| 34 | + let tokenDecimals: number; |
| 35 | + const rpcEndpoint = rpc.rpcAddress(network, chain as Chain); |
| 36 | + const tokenAddress = NTT_TOKENS[network][token][chain]!; |
| 37 | + const managerContract = NTT_MANAGER_CONTRACT[network][token][chain]!; |
| 38 | + const transceiverContract = NTT_TRANSCEIVER_CONTRACT[network][token][chain]!; |
| 39 | + |
| 40 | + if (chain === 'Solana') { |
| 41 | + const platform = new SolanaPlatform(network); |
| 42 | + ntt = new SolanaNtt(network, chain, platform.getRpc(chain), { |
| 43 | + coreBridge: contracts.coreBridge(network, chain), |
| 44 | + ntt: { |
| 45 | + token: tokenAddress, |
| 46 | + manager: managerContract, |
| 47 | + transceiver: { |
| 48 | + wormhole: transceiverContract, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }); |
| 52 | + tokenDecimals = await getSolanaTokenDecimals(rpcEndpoint, tokenAddress); |
| 53 | + } else { |
| 54 | + const evmChain = chain as EvmChains; |
| 55 | + const platform = new EvmPlatform(network); |
| 56 | + ntt = new EvmNtt(network, evmChain, platform.getRpc(evmChain), { |
| 57 | + ntt: { |
| 58 | + token: tokenAddress, |
| 59 | + manager: managerContract, |
| 60 | + transceiver: { |
| 61 | + wormhole: transceiverContract, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }); |
| 65 | + tokenDecimals = await getEvmTokenDecimals(rpcEndpoint, managerContract); |
| 66 | + } |
| 67 | + |
| 68 | + const outboundCapacity = await ntt.getCurrentOutboundCapacity(); |
| 69 | + const normalizedOutboundCapacity = outboundCapacity / BigInt(10 ** tokenDecimals); |
| 70 | + |
| 71 | + const inboundChains = NTT_SUPPORTED_CHAINS(network, token).filter( |
| 72 | + (inboundChain) => inboundChain !== chain |
| 73 | + ); |
| 74 | + |
| 75 | + let totalInboundCapacity = BigInt(0); |
| 76 | + const inboundRateLimits = await Promise.all( |
| 77 | + inboundChains.map(async (inboundChain): Promise<NTTRateLimit> => { |
| 78 | + const inboundCapacity = await ntt.getCurrentInboundCapacity(inboundChain); |
| 79 | + const normalizedInboundCapacity = inboundCapacity / BigInt(10 ** tokenDecimals); |
| 80 | + totalInboundCapacity += normalizedInboundCapacity; |
| 81 | + |
| 82 | + return { |
| 83 | + tokenName: token, |
| 84 | + srcChain: chainToChainId(inboundChain), |
| 85 | + destChain: chainToChainId(chain), |
| 86 | + amount: normalizedInboundCapacity.toString(), |
| 87 | + }; |
| 88 | + }) |
| 89 | + ); |
| 90 | + |
| 91 | + return { |
| 92 | + tokenName: token, |
| 93 | + srcChain: chainToChainId(chain), |
| 94 | + amount: normalizedOutboundCapacity.toString(), |
| 95 | + inboundCapacity: inboundRateLimits, |
| 96 | + totalInboundCapacity: totalInboundCapacity.toString(), |
| 97 | + }; |
| 98 | +} |
| 99 | + |
| 100 | +export async function computeNTTRateLimits(req: any, res: any) { |
| 101 | + res.set('Access-Control-Allow-Origin', '*'); |
| 102 | + if (req.method === 'OPTIONS') { |
| 103 | + res.set('Access-Control-Allow-Methods', 'GET'); |
| 104 | + res.set('Access-Control-Allow-Headers', 'Content-Type'); |
| 105 | + res.set('Access-Control-Max-Age', '3600'); |
| 106 | + res.sendStatus(204); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + try { |
| 111 | + const network = assertEnvironmentVariable('NETWORK') as Network; |
| 112 | + const managerContracts = NTT_MANAGER_CONTRACT[network]; |
| 113 | + |
| 114 | + const rateLimits = await Promise.all( |
| 115 | + Object.entries(managerContracts).map(async ([token, manager]) => { |
| 116 | + const inboundCapacityPromises = Object.entries(manager) |
| 117 | + .map(([chain, contract]) => |
| 118 | + contract ? computeNTTRateLimits_(network, token, chain as Chain) : null |
| 119 | + ) |
| 120 | + .filter(Boolean); |
| 121 | + |
| 122 | + const inboundCapacity = await Promise.all(inboundCapacityPromises); |
| 123 | + |
| 124 | + return { |
| 125 | + tokenName: token, |
| 126 | + inboundCapacity: inboundCapacity, |
| 127 | + }; |
| 128 | + }) |
| 129 | + ); |
| 130 | + await cloudStorageCache.save(JSON.stringify(rateLimits)); |
| 131 | + res.status(200).send('Rate limits saved'); |
| 132 | + } catch (e) { |
| 133 | + console.error(e); |
| 134 | + res.sendStatus(500); |
| 135 | + } |
| 136 | +} |
0 commit comments