|
| 1 | +import { |
| 2 | + assertEnvironmentVariable, |
| 3 | + formatAndSendToSlack, |
| 4 | + SlackInfo, |
| 5 | +} from '@wormhole-foundation/wormhole-monitor-common'; |
| 6 | +import knex, { Knex } from 'knex'; |
| 7 | + |
| 8 | +let alarmSlackInfo: SlackInfo; |
| 9 | +let initialized = false; |
| 10 | +let pg: Knex; |
| 11 | + |
| 12 | +function initialize() { |
| 13 | + pg = knex({ |
| 14 | + client: 'pg', |
| 15 | + connection: { |
| 16 | + user: assertEnvironmentVariable('PG_USER'), |
| 17 | + password: assertEnvironmentVariable('PG_PASSWORD'), |
| 18 | + database: assertEnvironmentVariable('PG_FT_DATABASE'), |
| 19 | + host: assertEnvironmentVariable('PG_HOST'), |
| 20 | + }, |
| 21 | + }); |
| 22 | + console.log(`database = ${assertEnvironmentVariable('PG_FT_DATABASE')}`); |
| 23 | + |
| 24 | + alarmSlackInfo = { |
| 25 | + channelId: assertEnvironmentVariable('FT_MISSING_VAA_SLACK_CHANNEL_ID'), |
| 26 | + postUrl: assertEnvironmentVariable('MISSING_VAA_SLACK_POST_URL'), |
| 27 | + botToken: assertEnvironmentVariable('MISSING_VAA_SLACK_BOT_TOKEN'), |
| 28 | + bannerTxt: 'Wormhole Fast Transfer Alarm', |
| 29 | + msg: '', |
| 30 | + }; |
| 31 | + console.log(`channelId = ${assertEnvironmentVariable('FT_MISSING_VAA_SLACK_CHANNEL_ID')}`); |
| 32 | + console.log('initialized global variables'); |
| 33 | + initialized = true; |
| 34 | +} |
| 35 | + |
| 36 | +export async function alarmFastTransfer(req: any, res: any) { |
| 37 | + res.set('Access-Control-Allow-Origin', '*'); |
| 38 | + if (req.method === 'OPTIONS') { |
| 39 | + // Send response to OPTIONS requests |
| 40 | + res.set('Access-Control-Allow-Methods', 'GET'); |
| 41 | + res.set('Access-Control-Allow-Headers', 'Content-Type'); |
| 42 | + res.set('Access-Control-Max-Age', '3600'); |
| 43 | + res.status(204).send(''); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + try { |
| 48 | + if (!initialized) { |
| 49 | + initialize(); |
| 50 | + } |
| 51 | + |
| 52 | + // Get the last 30 minutes of delayed orders. |
| 53 | + // In this case, delayed means the execution time is greater than 20 seconds. |
| 54 | + // This cloud function is scheduled to run every 30 minutes. |
| 55 | + const alertOrders: DisplayRow[] = await getDelayedOrders(); |
| 56 | + for (const order of alertOrders) { |
| 57 | + const formattedAmountIn = formatBigInt(order.amountIn); |
| 58 | + const formattedAmountOut = formatBigInt(order.amountOut); |
| 59 | + alarmSlackInfo.msg = |
| 60 | + `🚨 Delayed Order Alert!\n` + |
| 61 | + `Source Chain: ${order.sourceChain}\n` + |
| 62 | + `Sequence: ${order.sequence}\n` + |
| 63 | + `Status: ${order.status}\n` + |
| 64 | + `Order Timestamp: ${order.market_order_timestamp.toISOString()}\n` + |
| 65 | + `Destination Chain: ${order.destinationChain}\n` + |
| 66 | + `Execution Time: ${order.executionTime} Sec\n` + |
| 67 | + `Amount In: ${formattedAmountIn}\n` + |
| 68 | + `Amount Out: ${formattedAmountOut}`; |
| 69 | + console.log(alarmSlackInfo.msg); |
| 70 | + await formatAndSendToSlack(alarmSlackInfo); |
| 71 | + } |
| 72 | + } catch (e) { |
| 73 | + console.error(e); |
| 74 | + res.sendStatus(500); |
| 75 | + } |
| 76 | + res.status(200).send('successfully alarmed delayed fast transfers'); |
| 77 | + return; |
| 78 | +} |
| 79 | + |
| 80 | +// Format the amount in bigint to a string with 2 decimal places. |
| 81 | +// We know that the bigint input is fixed 6 decimal places and we only need to show 2 decimal places. |
| 82 | +const formatBigInt = (amt: bigint) => { |
| 83 | + const str = (amt / 10_000n).toString().padStart(3, '0'); |
| 84 | + return str.substring(0, str.length - 2) + '.' + str.substring(str.length - 2); |
| 85 | +}; |
| 86 | + |
| 87 | +async function getDelayedOrders(): Promise<DisplayRow[]> { |
| 88 | + console.log('getDelayedOrders'); |
| 89 | + const result = await pg |
| 90 | + .select( |
| 91 | + 'mo.fast_vaa_id', |
| 92 | + 'mo.status', |
| 93 | + 'mo.market_order_timestamp', |
| 94 | + 'mo.dst_chain AS destinationChain', |
| 95 | + pg.raw( |
| 96 | + 'EXTRACT(EPOCH FROM (fte.execution_time - mo.market_order_timestamp)) AS "executionTime"' |
| 97 | + ), |
| 98 | + 'mo.amount_in AS amountIn', |
| 99 | + 'fte.user_amount' |
| 100 | + ) |
| 101 | + .from('market_orders AS mo') |
| 102 | + .join('fast_transfer_executions AS fte', 'mo.fast_vaa_hash', '=', 'fte.fast_vaa_hash') |
| 103 | + .where('mo.market_order_timestamp', '>=', pg.raw("NOW() - INTERVAL '30 MINUTES'")) // Get orders from last 30 minutes |
| 104 | + .andWhereRaw('EXTRACT(EPOCH FROM (fte.execution_time - mo.market_order_timestamp)) > 20') |
| 105 | + .orderBy('mo.market_order_timestamp', 'desc'); |
| 106 | + |
| 107 | + console.log('result', result); |
| 108 | + return result.map((row) => ({ |
| 109 | + sourceChain: row.fast_vaa_id.split('/')[0], |
| 110 | + sequence: row.fast_vaa_id.split('/')[2], |
| 111 | + status: row.status, |
| 112 | + market_order_timestamp: row.market_order_timestamp, |
| 113 | + destinationChain: row.destinationChain, |
| 114 | + executionTime: row.executionTime, |
| 115 | + amountIn: BigInt(row.amountIn), |
| 116 | + amountOut: BigInt(row.user_amount), |
| 117 | + })); |
| 118 | +} |
| 119 | + |
| 120 | +type DisplayRow = { |
| 121 | + sourceChain: number; // from fast_vaa_id |
| 122 | + sequence: bigint; // from fast_vaa_id |
| 123 | + status: string; // from MarketOrder |
| 124 | + market_order_timestamp: Date; // from MarketOrder |
| 125 | + destinationChain: number; // from MarketOrder |
| 126 | + executionTime: number; // execution_time - market_order_timestamp |
| 127 | + amountIn: bigint; // from MarketOrder |
| 128 | + amountOut: bigint; // from FastTransferExecutions |
| 129 | +}; |
0 commit comments