|
| 1 | +import { type Request, type Response } from "express"; |
| 2 | +import { enabledChains, type ChainConfig } from "../chains"; |
| 3 | +import { isHex, padHex, toBytes } from "viem"; |
| 4 | +import type { Quote } from "@wormhole-foundation/sdk-definitions"; |
| 5 | +import { |
| 6 | + PAYEE_PUBLIC_KEY, |
| 7 | + QUOTER_PRIVATE_KEY, |
| 8 | + QUOTER_PUBLIC_KEY, |
| 9 | +} from "../consts"; |
| 10 | +import { |
| 11 | + estimateQuote, |
| 12 | + getTotalGasLimitAndMsgValue, |
| 13 | + signQuote, |
| 14 | +} from "../utils"; |
| 15 | + |
| 16 | +function getChainConfig(chainId: string): ChainConfig | undefined { |
| 17 | + const numericId = parseInt(chainId); |
| 18 | + return enabledChains[numericId]; |
| 19 | +} |
| 20 | + |
| 21 | +export const quoteHandler = async (req: Request, res: Response) => { |
| 22 | + const enabledChainIds = Object.keys(enabledChains); |
| 23 | + |
| 24 | + const srcChainId = req.body.srcChain; |
| 25 | + const dstChainId = req.body.dstChain; |
| 26 | + const relayInstructions = req.body.relayInstructions; |
| 27 | + |
| 28 | + if (relayInstructions && !isHex(relayInstructions)) { |
| 29 | + res.status(400).send(`Invalid hex string for "relayInstructions"`); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + if (!enabledChainIds.includes(srcChainId.toString())) { |
| 34 | + res |
| 35 | + .status(400) |
| 36 | + .send( |
| 37 | + `Unsupported source chain: ${srcChainId}, supported chains: ${enabledChainIds.join( |
| 38 | + "," |
| 39 | + )}` |
| 40 | + ); |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + if (!enabledChainIds.includes(dstChainId.toString())) { |
| 45 | + res |
| 46 | + .status(400) |
| 47 | + .send( |
| 48 | + `Unsupported destination chain: ${dstChainId}, supported chains: ${enabledChainIds.join( |
| 49 | + "," |
| 50 | + )}` |
| 51 | + ); |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + const srcChain = getChainConfig(srcChainId); |
| 56 | + const dstChain = getChainConfig(dstChainId); |
| 57 | + |
| 58 | + if (!srcChain || !dstChain) { |
| 59 | + res.status(500).send("Internal error: Invalid chain configuration"); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const expiryTime = new Date(); |
| 64 | + |
| 65 | + expiryTime.setHours(expiryTime.getHours() + 1); |
| 66 | + |
| 67 | + const quote: Quote = { |
| 68 | + quote: { |
| 69 | + prefix: "EQ01", |
| 70 | + quoterAddress: toBytes(QUOTER_PUBLIC_KEY), |
| 71 | + payeeAddress: toBytes( |
| 72 | + padHex(PAYEE_PUBLIC_KEY, { |
| 73 | + dir: "left", |
| 74 | + size: 32, |
| 75 | + }) |
| 76 | + ), |
| 77 | + srcChain: parseInt(srcChainId), |
| 78 | + dstChain: parseInt(dstChainId), |
| 79 | + expiryTime, |
| 80 | + baseFee: 1n, |
| 81 | + dstGasPrice: 100n, |
| 82 | + srcPrice: 10000000000n, |
| 83 | + dstPrice: 10000000000n, |
| 84 | + }, |
| 85 | + }; |
| 86 | + |
| 87 | + const signedQuote = await signQuote(quote, QUOTER_PRIVATE_KEY); |
| 88 | + |
| 89 | + let response: { |
| 90 | + signedQuote: `0x${string}`; |
| 91 | + estimatedCost?: bigint; |
| 92 | + } = { |
| 93 | + signedQuote, |
| 94 | + }; |
| 95 | + |
| 96 | + if (relayInstructions) { |
| 97 | + const { gasLimit, msgValue } = |
| 98 | + getTotalGasLimitAndMsgValue(relayInstructions); |
| 99 | + |
| 100 | + if (gasLimit > dstChain.capabilities.maxGasLimit) { |
| 101 | + res |
| 102 | + .status(400) |
| 103 | + .send( |
| 104 | + `Request exceeds maxGasLimit: ${gasLimit.toString()} requested, ${dstChain.capabilities.maxGasLimit.toString()} maximum.` |
| 105 | + ); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + if (msgValue > dstChain.capabilities.maxMsgValue) { |
| 110 | + res |
| 111 | + .status(400) |
| 112 | + .send( |
| 113 | + `Request exceeds maxMsgValue: ${msgValue.toString()} requested, ${dstChain.capabilities.maxMsgValue.toString()} maximum.` |
| 114 | + ); |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + response.estimatedCost = estimateQuote( |
| 119 | + quote, |
| 120 | + gasLimit, |
| 121 | + msgValue, |
| 122 | + dstChain.gasPriceDecimals, |
| 123 | + srcChain.nativeDecimals, |
| 124 | + dstChain.nativeDecimals |
| 125 | + ); |
| 126 | + } |
| 127 | + res.status(200).json(response); |
| 128 | +}; |
0 commit comments