Skip to content

Commit

Permalink
do not call estimate or create for aggregator that did not return the…
Browse files Browse the repository at this point in the history
… input tokens
  • Loading branch information
jorbuedo committed Feb 13, 2025
1 parent f11cccb commit c6696f8
Showing 1 changed file with 56 additions and 6 deletions.
62 changes: 56 additions & 6 deletions packages/swap/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ const apiManagerMaker = (
const autoApiMaker = (
adapters: Record<Swap.Aggregator, Swap.Api>,
): Swap.Api => {
const dhTokenList = new Set<Portfolio.Token.Id>(['.'])
const msTokenList = new Set<Portfolio.Token.Id>(['.'])

return freeze(
{
async tokens() {
Expand All @@ -86,13 +89,16 @@ const autoApiMaker = (
if (isLeft(muesliswapResponse)) return dexhunterResponse

const merged: Record<Portfolio.Token.Id, Portfolio.Token.Info> = {}
const append = (tokenInfo: Portfolio.Token.Info) => {
if (merged[tokenInfo.id] === undefined)
merged[tokenInfo.id] = tokenInfo
}
const append =
(tokenList: Set<Portfolio.Token.Id>) =>
(tokenInfo: Portfolio.Token.Info) => {
tokenList.add(tokenInfo.id)
if (merged[tokenInfo.id] === undefined)
merged[tokenInfo.id] = tokenInfo
}

dexhunterResponse.value.data.forEach(append)
muesliswapResponse.value.data.forEach(append)
dexhunterResponse.value.data.forEach(append(dhTokenList))
muesliswapResponse.value.data.forEach(append(msTokenList))

return {
tag: 'right',
Expand Down Expand Up @@ -159,6 +165,22 @@ const autoApiMaker = (
},

async estimate(body: Swap.EstimateRequest) {
const isDHValid = hasTokens(dhTokenList, body)
const isMSValid = hasTokens(msTokenList, body)

if (!isDHValid || !isMSValid) {
if (isDHValid) return adapters.dexhunter.estimate(body)
if (isMSValid) return adapters.muesliswap.estimate(body)
return {
tag: 'left',
error: {
status: -3,
message: 'Tokens not found in aggregators',
responseData: {},
},
}
}

const [dexhunterResponse, muesliswapResponse] = await Promise.all([
adapters.dexhunter.estimate(body),
adapters.muesliswap.estimate(body),
Expand All @@ -184,6 +206,22 @@ const autoApiMaker = (
},

async create(body: Swap.CreateRequest) {
const isDHValid = hasTokens(dhTokenList, body)
const isMSValid = hasTokens(msTokenList, body)

if (!isDHValid || !isMSValid) {
if (isDHValid) return adapters.dexhunter.create(body)
if (isMSValid) return adapters.muesliswap.create(body)
return {
tag: 'left',
error: {
status: -3,
message: 'Tokens not found in aggregators',
responseData: {},
},
}
}

const [dexhunterResponse, muesliswapResponse] = await Promise.all([
adapters.dexhunter.create(body),
adapters.muesliswap.create(body),
Expand Down Expand Up @@ -225,3 +263,15 @@ const warnAllLeft = (...responses: Array<Api.Response<any>>) => {
responses.map((response) => response.error.message),
)
}

const hasTokens = (
tokenList: Set<Portfolio.Token.Id>,
body: Swap.CreateRequest | Swap.EstimateRequest,
): boolean => {
const {tokenIn, tokenOut} = body

if (!tokenList.has(tokenIn)) return false
if (!tokenList.has(tokenOut)) return false

return true
}

0 comments on commit c6696f8

Please sign in to comment.