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