|
| 1 | +import { Chains, calculateDeadline } from "@balmy/sdk" |
| 2 | +import { AlwaysValidConfigAndContextSource } from "@balmy/sdk/dist/services/quotes/quote-sources/base/always-valid-source" |
| 3 | +import type { |
| 4 | + BuildTxParams, |
| 5 | + QuoteParams, |
| 6 | + QuoteSourceMetadata, |
| 7 | + SourceQuoteResponse, |
| 8 | + SourceQuoteTransaction, |
| 9 | +} from "@balmy/sdk/dist/services/quotes/quote-sources/types" |
| 10 | +import { |
| 11 | + addQuoteSlippage, |
| 12 | + calculateAllowanceTarget, |
| 13 | + failed, |
| 14 | +} from "@balmy/sdk/dist/services/quotes/quote-sources/utils" |
| 15 | +import qs from "qs" |
| 16 | + |
| 17 | +const PARASWAP_METADATA: QuoteSourceMetadata<ParaswapSupport> = { |
| 18 | + name: "Velora", |
| 19 | + supports: { |
| 20 | + chains: [ |
| 21 | + Chains.ETHEREUM.chainId, |
| 22 | + Chains.POLYGON.chainId, |
| 23 | + Chains.BNB_CHAIN.chainId, |
| 24 | + Chains.AVALANCHE.chainId, |
| 25 | + Chains.FANTOM.chainId, |
| 26 | + Chains.ARBITRUM.chainId, |
| 27 | + Chains.OPTIMISM.chainId, |
| 28 | + Chains.POLYGON_ZKEVM.chainId, |
| 29 | + Chains.BASE.chainId, |
| 30 | + Chains.GNOSIS.chainId, |
| 31 | + 130, // unichain |
| 32 | + ], |
| 33 | + swapAndTransfer: true, |
| 34 | + buyOrders: true, |
| 35 | + }, |
| 36 | + logoURI: "ipfs://QmVtj4RwZ5MMfKpbfv8qXksb5WYBJsQXkaZXLq7ipvMNW5", |
| 37 | +} |
| 38 | +type ParaswapSupport = { buyOrders: true; swapAndTransfer: true } |
| 39 | +type ParaswapConfig = { sourceAllowlist?: string[]; sourceDenylist?: string[] } |
| 40 | +type ParaswapData = { tx: SourceQuoteTransaction } |
| 41 | +export class CustomParaswapQuoteSource extends AlwaysValidConfigAndContextSource< |
| 42 | + ParaswapSupport, |
| 43 | + ParaswapConfig, |
| 44 | + ParaswapData |
| 45 | +> { |
| 46 | + getMetadata(): QuoteSourceMetadata<ParaswapSupport> { |
| 47 | + return PARASWAP_METADATA |
| 48 | + } |
| 49 | + |
| 50 | + async quote({ |
| 51 | + components: { fetchService }, |
| 52 | + request: { |
| 53 | + chainId, |
| 54 | + sellToken, |
| 55 | + buyToken, |
| 56 | + order, |
| 57 | + accounts: { takeFrom, recipient }, |
| 58 | + config: { timeout, slippagePercentage, txValidFor }, |
| 59 | + external, |
| 60 | + }, |
| 61 | + config, |
| 62 | + }: QuoteParams<ParaswapSupport, ParaswapConfig>): Promise< |
| 63 | + SourceQuoteResponse<ParaswapData> |
| 64 | + > { |
| 65 | + const { |
| 66 | + sellToken: { decimals: srcDecimals }, |
| 67 | + buyToken: { decimals: destDecimals }, |
| 68 | + } = await external.tokenData.request() |
| 69 | + const queryParams = { |
| 70 | + network: chainId, |
| 71 | + srcToken: sellToken, |
| 72 | + destToken: buyToken, |
| 73 | + amount: order.type === "sell" ? order.sellAmount : order.buyAmount, |
| 74 | + side: order.type.toUpperCase(), |
| 75 | + srcDecimals, |
| 76 | + destDecimals, |
| 77 | + includeDEXS: config.sourceAllowlist, |
| 78 | + excludeDEXS: config.sourceDenylist, |
| 79 | + slippage: slippagePercentage * 100, |
| 80 | + userAddress: takeFrom, |
| 81 | + receiver: takeFrom !== recipient ? recipient : undefined, |
| 82 | + partner: config.referrer?.name, |
| 83 | + partnerAddress: config.referrer?.address, |
| 84 | + partnerFeeBps: 0, |
| 85 | + deadline: calculateDeadline(txValidFor), |
| 86 | + version: "6.2", |
| 87 | + } |
| 88 | + const queryString = qs.stringify(queryParams, { |
| 89 | + skipNulls: true, |
| 90 | + arrayFormat: "comma", |
| 91 | + }) |
| 92 | + const url = `https://api.paraswap.io/swap?${queryString}` |
| 93 | + const response = await fetchService.fetch(url, { timeout }) |
| 94 | + if (!response.ok) { |
| 95 | + failed( |
| 96 | + PARASWAP_METADATA, |
| 97 | + chainId, |
| 98 | + sellToken, |
| 99 | + buyToken, |
| 100 | + await response.text(), |
| 101 | + ) |
| 102 | + } |
| 103 | + const { |
| 104 | + priceRoute, |
| 105 | + txParams: { to, data, value }, |
| 106 | + } = await response.json() |
| 107 | + const quote = { |
| 108 | + sellAmount: BigInt(priceRoute.srcAmount), |
| 109 | + buyAmount: BigInt(priceRoute.destAmount), |
| 110 | + // estimatedGas: BigInt(priceRoute.gasCost), |
| 111 | + allowanceTarget: calculateAllowanceTarget( |
| 112 | + sellToken, |
| 113 | + priceRoute.tokenTransferProxy, |
| 114 | + ), |
| 115 | + customData: { |
| 116 | + tx: { |
| 117 | + to, |
| 118 | + calldata: data, |
| 119 | + value: BigInt(value ?? 0), |
| 120 | + }, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + return addQuoteSlippage(quote, order.type, slippagePercentage) |
| 125 | + } |
| 126 | + |
| 127 | + async buildTx({ |
| 128 | + request, |
| 129 | + }: BuildTxParams< |
| 130 | + ParaswapConfig, |
| 131 | + ParaswapData |
| 132 | + >): Promise<SourceQuoteTransaction> { |
| 133 | + return request.customData.tx |
| 134 | + } |
| 135 | +} |
0 commit comments