Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jorbuedo committed Dec 10, 2024
1 parent 34fa22f commit e34a490
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 51 deletions.
65 changes: 52 additions & 13 deletions packages/swap/src/adapters/api/dexhunter/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
LimitEstimateRequest,
LimitEstimateResponse,
OrdersResponse,
Provider,
ReverseEstimateRequest,
ReverseEstimateResponse,
SignRequest,
Expand All @@ -20,6 +21,7 @@ import {
} from './types'
import {isPrimaryToken} from '@yoroi/portfolio'
import {DexhunterApiConfig} from './api-maker'
import {SwapProvider} from '@yoroi/types/lib/typescript/swap/api'

const tokenIdToDexhunter = (tokenId: Portfolio.Token.Id) =>
isPrimaryToken(tokenId) ? 'ADA' : tokenId.replace('.', '')
Expand All @@ -28,7 +30,7 @@ const transformSplit = ({
amount_in = 0,
batcher_fee = 0,
deposits = 0,
dex = '',
dex,
expected_output = 0,
expected_output_without_slippage = 0,
fee = 0,
Expand All @@ -42,7 +44,7 @@ const transformSplit = ({
amountIn: amount_in,
batcherFee: batcher_fee,
deposits,
dex,
dex: toSwapProvider(dex),
expectedOutput: expected_output,
expectedOutputWithoutSlippage: expected_output_without_slippage,
fee,
Expand Down Expand Up @@ -111,7 +113,7 @@ export const transformersMaker = ({
_id,
actual_out_amount = 0,
amount_in = 0,
dex = '',
dex,
expected_out_amount = 0,
is_dexhunter = false,
last_update,
Expand All @@ -126,7 +128,7 @@ export const transformersMaker = ({
aggregator: is_dexhunter
? Swap.Aggregator.Dexhunter
: Swap.Aggregator.Muesliswap,
dex,
dex: toSwapProvider(dex),
placedAt: new Date(submission_time).getTime(),
lastUpdate: new Date(last_update).getTime(),
status,
Expand Down Expand Up @@ -164,7 +166,9 @@ export const transformersMaker = ({
tokenOut,
}: Swap.EstimateRequest): EstimateRequest => ({
amount_in: amountIn,
blacklisted_dexes: blacklistedDexes,
blacklisted_dexes: blacklistedDexes
?.map(fromSwapProvider)
.filter((v): v is Provider => !!v),
slippage,
token_in: tokenIdToDexhunter(tokenIn),
token_out: tokenIdToDexhunter(tokenOut),
Expand Down Expand Up @@ -200,7 +204,9 @@ export const transformersMaker = ({
tokenOut,
}: Swap.EstimateRequest): ReverseEstimateRequest => ({
amount_out: amountOut,
blacklisted_dexes: blacklistedDexes,
blacklisted_dexes: blacklistedDexes
?.map(fromSwapProvider)
.filter((v): v is Provider => !!v),
slippage,
token_in: tokenIdToDexhunter(tokenIn),
token_out: tokenIdToDexhunter(tokenOut),
Expand Down Expand Up @@ -231,15 +237,17 @@ export const transformersMaker = ({
request: ({
amountIn,
blacklistedDexes,
dex,
dex = SwapProvider.Splash_v1,
multiples,
tokenIn,
tokenOut,
wantedPrice,
}: Swap.EstimateRequest): LimitEstimateRequest => ({
amount_in: amountIn,
blacklisted_dexes: blacklistedDexes,
dex: dex,
blacklisted_dexes: blacklistedDexes
?.map(fromSwapProvider)
.filter((v): v is Provider => !!v),
dex: fromSwapProvider(dex) ?? Provider.Splash_v1,
multiples,
token_in: tokenIdToDexhunter(tokenIn),
token_out: tokenIdToDexhunter(tokenOut),
Expand Down Expand Up @@ -271,16 +279,18 @@ export const transformersMaker = ({
request: ({
amountIn,
blacklistedDexes,
dex,
dex = Swap.Provider.Splash_v1,
multiples,
tokenIn,
tokenOut,
wantedPrice,
}: Swap.CreateRequest): LimitBuildRequest => ({
amount_in: amountIn,
blacklisted_dexes: blacklistedDexes,
blacklisted_dexes: blacklistedDexes
?.map(fromSwapProvider)
.filter((v): v is Provider => !!v),
buyer_address: address,
dex: dex,
dex: fromSwapProvider(dex) ?? Provider.Splash_v1,
multiples,
token_in: tokenIdToDexhunter(tokenIn),
token_out: tokenIdToDexhunter(tokenOut),
Expand Down Expand Up @@ -318,7 +328,9 @@ export const transformersMaker = ({
tokenOut,
}: Swap.CreateRequest): BuildRequest => ({
amount_in: amountIn,
blacklisted_dexes: blacklistedDexes,
blacklisted_dexes: blacklistedDexes
?.map(fromSwapProvider)
.filter((v): v is Provider => v !== undefined),
buyer_address: address,
slippage,
token_in: tokenIdToDexhunter(tokenIn),
Expand Down Expand Up @@ -360,3 +372,30 @@ export const transformersMaker = ({
},
} as const
}

const toSwapProvider = (dex: Provider): Swap.Provider =>
({
[Provider.Minswap_v1]: Swap.Provider.Minswap_v1,
[Provider.Minswap_v2]: Swap.Provider.Minswap_v2,
[Provider.Wingriders_v1]: Swap.Provider.Wingriders_v1,
[Provider.Wingriders_v2]: Swap.Provider.Wingriders_v2,
[Provider.Vyfi_v1]: Swap.Provider.Vyfi_v1,
[Provider.Sundaeswap_v1]: Swap.Provider.Sundaeswap_v1,
[Provider.Sundaeswap_v3]: Swap.Provider.Sundaeswap_v3,
[Provider.Splash_v1]: Swap.Provider.Splash_v1,
}[dex])

const fromSwapProvider = (dex: Swap.Provider): Provider | undefined =>
({
[Swap.Provider.Minswap_v1]: Provider.Minswap_v1,
[Swap.Provider.Minswap_v2]: Provider.Minswap_v2,
[Swap.Provider.Wingriders_v1]: Provider.Wingriders_v1,
[Swap.Provider.Wingriders_v2]: Provider.Wingriders_v2,
[Swap.Provider.Vyfi_v1]: Provider.Vyfi_v1,
[Swap.Provider.Sundaeswap_v1]: Provider.Sundaeswap_v1,
[Swap.Provider.Sundaeswap_v3]: Provider.Sundaeswap_v3,
[Swap.Provider.Splash_v1]: Provider.Splash_v1,
[Swap.Provider.Teddy_v1]: undefined,
[Swap.Provider.Muesliswap_v2]: undefined,
[Swap.Provider.Spectrum_v1]: undefined,
}[dex])
33 changes: 23 additions & 10 deletions packages/swap/src/adapters/api/dexhunter/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
export const Provider = {
Minswap_v1: 'MINSWAP',
Minswap_v2: 'MINSWAPV2',
Wingriders_v1: 'WINGRIDER',
Wingriders_v2: 'WINGRIDERV2',
Vyfi_v1: 'VYFI',
Sundaeswap_v1: 'SUNDAESWAP',
Sundaeswap_v3: 'SUNDAESWAPV3',
Splash_v1: 'SPLASH',
} as const

export type Provider = (typeof Provider)[keyof typeof Provider]

export type TokensResponse = Array<{
token_id: string
token_decimals: number
Expand All @@ -16,7 +29,7 @@ export type OrdersResponse = Array<{
amount_in?: number
batcher_fee?: number
deposit?: number
dex?: string
dex: Provider
expected_out_amount?: number
is_dexhunter?: boolean
is_oor?: boolean
Expand Down Expand Up @@ -47,7 +60,7 @@ export type Split = {
amount_in?: number
batcher_fee?: number
deposits?: number
dex?: string
dex: Provider
expected_output?: number
expected_output_without_slippage?: number
fee?: number
Expand All @@ -61,7 +74,7 @@ export type Split = {

export type EstimateRequest = {
amount_in?: number
blacklisted_dexes?: string[]
blacklisted_dexes?: Provider[]
slippage?: number
token_in?: string
token_out?: string
Expand All @@ -88,7 +101,7 @@ export type EstimateResponse = {

export type ReverseEstimateRequest = {
amount_out?: number
blacklisted_dexes?: string[]
blacklisted_dexes?: Provider[]
slippage: number
token_in: string
token_out: string
Expand All @@ -115,8 +128,8 @@ export type ReverseEstimateResponse = {

export type LimitEstimateRequest = {
amount_in?: number
blacklisted_dexes?: string[]
dex?: string
blacklisted_dexes?: Provider[]
dex: Provider
multiples?: number
token_in?: string
token_out?: string
Expand All @@ -125,7 +138,7 @@ export type LimitEstimateRequest = {

export type LimitEstimateResponse = {
batcher_fee?: number
blacklisted_dexes?: string[]
blacklisted_dexes?: Provider[]
deposits?: number
dexhunter_fee?: number
net_price?: number
Expand All @@ -142,9 +155,9 @@ export type LimitEstimateResponse = {

export type LimitBuildRequest = {
amount_in?: number
blacklisted_dexes?: string[]
blacklisted_dexes?: Provider[]
buyer_address?: string
dex?: string
dex: Provider
multiples?: number
token_in?: string
token_out?: string
Expand All @@ -169,7 +182,7 @@ export type LimitBuildResponse = {

export type BuildRequest = {
amount_in: number
blacklisted_dexes?: string[]
blacklisted_dexes?: Provider[]
buyer_address: string
tx_optimization?: boolean
slippage: number
Expand Down
45 changes: 37 additions & 8 deletions packages/swap/src/adapters/api/muesliswap/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from './types'
import {MuesliswapApiConfig} from './api-maker'
import {asTokenFingerprint, asTokenName} from '../../../helpers/transformers'
import {SwapProvider} from '@yoroi/types/lib/typescript/swap/api'

export const transformersMaker = ({
primaryTokenInfo,
Expand Down Expand Up @@ -103,11 +104,11 @@ export const transformersMaker = ({
txHash,
finalizedTxHash,
status,
dex = 'muesliswap',
dex = Provider.Muesliswap_v2,
outputIdx,
}) => ({
aggregator: Swap.Aggregator.Muesliswap,
dex,
dex: toSwapProvider(dex),
placedAt: placedAt ? placedAt * 1000 : undefined,
lastUpdate: finalizedAt ? finalizedAt * 1000 : undefined,
status,
Expand All @@ -133,7 +134,7 @@ export const transformersMaker = ({
},
quote: {
request: ({
dex,
dex = SwapProvider.Muesliswap_v2,
blacklistedDexes,
tokenIn,
tokenOut,
Expand All @@ -142,7 +143,7 @@ export const transformersMaker = ({
slippage,
}: Swap.EstimateRequest): QuoteRequest => ({
dex: dex
? [dex as Provider]
? [fromSwapProvider(dex)]
: Object.values(Provider).filter(
(provider) => !blacklistedDexes?.includes(provider),
),
Expand Down Expand Up @@ -186,7 +187,7 @@ export const transformersMaker = ({
slippage,
}: Swap.CreateRequest): CreateOrderRequest => ({
dex: dex
? [dex as Provider]
? [fromSwapProvider(dex)]
: Object.values(Provider).filter(
(provider) => !blacklistedDexes?.includes(provider),
),
Expand Down Expand Up @@ -227,13 +228,13 @@ export const transformersMaker = ({
},
createLimit: {
request: ({
dex,
dex = SwapProvider.Muesliswap_v2,
tokenIn,
tokenOut,
amountIn,
wantedPrice = 0,
}: Swap.CreateRequest): LimitOrderRequest => ({
dex: dex as Provider,
dex: fromSwapProvider(dex),
sell_token: tokenIn,
buy_token: tokenOut,
sell_amount: amountIn,
Expand Down Expand Up @@ -275,7 +276,7 @@ const transformSplit = ({
amountIn: amount_in,
batcherFee: batcher_fee,
deposits: deposit,
dex,
dex: toSwapProvider(dex),
expectedOutput: expected_output,
expectedOutputWithoutSlippage: expected_output_without_slippage,
fee: pool_fee,
Expand All @@ -286,3 +287,31 @@ const transformSplit = ({
priceDistortion: price_impact,
priceImpact: price_impact,
})

const toSwapProvider = (dex: Provider): Swap.Provider =>
({
[Provider.Minswap_v1]: Swap.Provider.Minswap_v1,
[Provider.Minswap_v2]: Swap.Provider.Minswap_v2,
[Provider.Wingriders_v1]: Swap.Provider.Wingriders_v1,
[Provider.Vyfi_v1]: Swap.Provider.Vyfi_v1,
[Provider.Sundaeswap_v1]: Swap.Provider.Sundaeswap_v1,
[Provider.Sundaeswap_v3]: Swap.Provider.Sundaeswap_v3,
[Provider.Muesliswap_v2]: Swap.Provider.Muesliswap_v2,
[Provider.Spectrum_v1]: Swap.Provider.Spectrum_v1,
[Provider.Teddy_v1]: Swap.Provider.Teddy_v1,
}[dex])

const fromSwapProvider = (dex: Swap.Provider): Provider =>
({
[Swap.Provider.Minswap_v1]: Provider.Minswap_v1,
[Swap.Provider.Minswap_v2]: Provider.Minswap_v2,
[Swap.Provider.Wingriders_v1]: Provider.Wingriders_v1,
[Swap.Provider.Wingriders_v2]: undefined,
[Swap.Provider.Vyfi_v1]: Provider.Vyfi_v1,
[Swap.Provider.Sundaeswap_v1]: Provider.Sundaeswap_v1,
[Swap.Provider.Sundaeswap_v3]: Provider.Sundaeswap_v3,
[Swap.Provider.Splash_v1]: undefined,
[Swap.Provider.Teddy_v1]: Provider.Teddy_v1,
[Swap.Provider.Muesliswap_v2]: Provider.Muesliswap_v2,
[Swap.Provider.Spectrum_v1]: Provider.Spectrum_v1,
}[dex] ?? Provider.Muesliswap_v2)
28 changes: 14 additions & 14 deletions packages/swap/src/adapters/api/muesliswap/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import {Portfolio} from '@yoroi/types'

export const Provider = {
Muesliswap_v2: 'muesliswap-v2',
Minswap_v1: 'minswap-v1',
Minswap_v2: 'minswap-v2',
Spectrum_v1: 'spectrum-v1',
Teddy_v1: 'teddy-v1',
Wingriders_v1: 'wingriders-v1',
Vyfi_v1: 'vyfi-v1',
Sundaeswap_v1: 'sundaeswap-v1',
Sundaeswap_v3: 'sundaeswap-v3',
} as const

export type Provider = (typeof Provider)[keyof typeof Provider]

export type TokensResponse = Array<{
info: {
supply: {
Expand Down Expand Up @@ -91,20 +105,6 @@ export type CancelResponse = {
tx_cbor: string
}

export const Provider = {
Muesliswap_v2: 'muesliswap-v2',
Minswap_v1: 'minswap-v1',
Minswap_v2: 'minswap-v2',
Spectrum_v1: 'spectrum-v1',
Teddy_v1: 'teddy-v1',
Wingriders_v1: 'wingriders-v1',
Vyfi_v1: 'vyfi-v1',
Sundaeswap_v1: 'sundaeswap-v1',
Sundaeswap_v3: 'sundaeswap-v3',
} as const

export type Provider = (typeof Provider)[keyof typeof Provider]

export type LimitOrderRequest = {
buy_token: string
sell_token: string
Expand Down
Loading

0 comments on commit e34a490

Please sign in to comment.