From 2c735e247c7952a72aaf731c54a7c30971865a35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tha=CC=80nh=20Pha=CC=A3m=20Ngo=CC=A3c?= Date: Mon, 5 Jun 2023 13:49:50 +0700 Subject: [PATCH] feat: support swap on jupiter --- src/adapters/defi.ts | 10 +++++- src/commands/swap/index/processor.ts | 49 ++++++++++++++-------------- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/adapters/defi.ts b/src/adapters/defi.ts index ea8bee7ba..2fb3341f1 100644 --- a/src/adapters/defi.ts +++ b/src/adapters/defi.ts @@ -482,13 +482,21 @@ class Defi extends Fetcher { }) } - async swap(userDiscordId: string, chainName: string, routeSummary: any) { + async swap( + userDiscordId: string, + chainName: string, + routeSummary: any, + aggregator: string, + swapData: any + ) { return await this.jsonFetch(`${API_BASE_URL}/swap`, { method: "POST", body: { userDiscordId, chainName, routeSummary, + aggregator, + swapData, }, bodyCamelToSnake: false, }) diff --git a/src/commands/swap/index/processor.ts b/src/commands/swap/index/processor.ts index 379eb119a..3b934f999 100644 --- a/src/commands/swap/index/processor.ts +++ b/src/commands/swap/index/processor.ts @@ -225,7 +225,9 @@ export async function handleSwap(i: ButtonInteraction) { const { status, ok, log, error, curl } = await defi.swap( i.user.id, chainName, - swapCacheData.routeSummary + swapCacheData.routeSummary, + swapCacheData.aggregator, + swapCacheData.swapData ) if (!ok) { if (status === 400) { @@ -281,6 +283,7 @@ export async function render( i: CommandInteraction | ButtonInteraction, data: { routeSummary: RouteSummary + aggregator: string tokenIn: { decimals: number } tokenOut: { decimals: number } }, @@ -310,28 +313,24 @@ export async function render( }) const tradeRoutes = Object.values(routes).map((route, i) => { - return `${i === 0 ? "" : "\n"}${getEmoji("REPLY_3")}${ - route.percent - } ${fromEmo} ${from}\n${route.hops - .map((hop, j) => { - const lastOfLast = - i === Object.values(routes).length - 1 && j === route.hops.length - 1 - - return `${ - lastOfLast ? getEmoji("REPLY") : getEmoji("REPLY_2") - } ${getEmojiToken(hop.tokenOutSymbol as TokenEmojiKey, false)} ${ - hop.tokenOutSymbol - }\n${hop.pools - .map((p, o) => { - return `${lastOfLast ? getEmoji("BLANK") : getEmoji("REPLY_3")}${ - o === hop.pools.length - 1 - ? getEmoji("REPLY") - : getEmoji("REPLY_2") - }[(${p.name}: ${p.percent})](${HOMEPAGE_URL})` - }) - .join("\n")}` - }) - .join("\n")}` + return `${i === 0 ? "" : "\n"}${getEmoji("REPLY_3")}${route.percent + } ${fromEmo} ${from}\n${route.hops + .map((hop, j) => { + const lastOfLast = + i === Object.values(routes).length - 1 && j === route.hops.length - 1 + + return `${lastOfLast ? getEmoji("REPLY") : getEmoji("REPLY_2") + } ${getEmojiToken(hop.tokenOutSymbol as TokenEmojiKey, false)} ${hop.tokenOutSymbol + }\n${hop.pools + .map((p, o) => { + return `${lastOfLast ? getEmoji("BLANK") : getEmoji("REPLY_3")}${o === hop.pools.length - 1 + ? getEmoji("REPLY") + : getEmoji("REPLY_2") + }[(${p.name}: ${p.percent})](${HOMEPAGE_URL})` + }) + .join("\n")}` + }) + .join("\n")}` }) // check to see if we can combine routes without exceeding discord 1024 char limit @@ -406,7 +405,9 @@ export async function render( .setStyle("PRIMARY"), new MessageButton() .setURL( - `https://kyberswap.com/swap/${chainName}/${from.toLowerCase()}-to-${to.toLowerCase()}` + data.aggregator === "jupiter" + ? `https://jup.ag/swap/${from.toUpperCase()}-${to.toUpperCase()}` + : `https://kyberswap.com/swap/${chainName}/${from.toLowerCase()}-to-${to.toLowerCase()}` ) .setLabel("View in web") .setStyle("LINK")