Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jorbuedo committed Jan 2, 2025
1 parent bd5dc05 commit 686fae1
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 23 deletions.
88 changes: 75 additions & 13 deletions packages/swap/src/manager.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import {Swap} from '@yoroi/types'
import {Portfolio, Swap} from '@yoroi/types'
import {isLeft} from '@yoroi/common'

import {freeze} from 'immer'
import {dexhunterApiMaker} from './adapters/api/dexhunter/api-maker'
import {muesliswapApiMaker} from './adapters/api/muesliswap/api-maker'

Expand All @@ -19,36 +22,95 @@ export const swapManagerMaker: Swap.ManagerMaker = ({
stakingKey,
})

const config: Swap.ManagerConfig = { adapter: 'auto' }
const config: Swap.ManagerConfig = {adapter: 'auto'}

return {
api: apiManagerMaker({
[Swap.Aggregator.Dexhunter]: dexhunterApi, [Swap.Aggregator.Muesliswap]: muesliswapApi,

}, config),
assignConfig: (v: Swap.ManagerConfig): Swap.ManagerConfig => Object.assign(config, v),
api: apiManagerMaker(
{
[Swap.Aggregator.Dexhunter]: dexhunterApi,
[Swap.Aggregator.Muesliswap]: muesliswapApi,
},
config,
),
assignConfig: (v: Swap.ManagerConfig): Swap.ManagerConfig =>
Object.assign(config, v),
config,
clearStorage: storage.clear,
slippage: storage.slippage,
}
}

const apiManagerMaker = (adapters: Record<Swap.Aggregator, Swap.Api>, config: Swap.ManagerConfig): Swap.Api => {
const apiManagerMaker = (
adapters: Record<Swap.Aggregator, Swap.Api>,
config: Swap.ManagerConfig,
): Swap.Api => {
const autoApi = autoApiMaker(adapters)
return new Proxy(
{},
{
get({}, prop: keyof Swap.Api) {
return (...args: any[]) => {
if (config.adapter !== 'auto') return (adapters[config.adapter][prop] as Function)(...args)
if (config.adapter !== 'auto')
return (adapters[config.adapter][prop] as Function)(...args)
return (autoApi[prop] as Function)(...args)
}
},
},
) as Swap.Api
}

const autoApiMaker = (adapters: Record<Swap.Aggregator, Swap.Api>): Swap.Api => {
// TODO
return adapters[Swap.Aggregator.Muesliswap]

const autoApiMaker = (
adapters: Record<Swap.Aggregator, Swap.Api>,
): Swap.Api => {
return freeze(
{
async tokens() {
const [dexhunterResponse, muesliswapResponse] = await Promise.all([
adapters.dexhunter.tokens(),
adapters.muesliswap.tokens(),
])

if (isLeft(dexhunterResponse)) return muesliswapResponse
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
}

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

return {
tag: 'right',
value: {
status: 200,
data: Object.values(merged),
},
}
},

async orders() {
return adapters.muesliswap.orders()
},

async providers(body: Swap.ProvidersRequest) {
return adapters.muesliswap.providers(body)
},

async estimate(body: Swap.EstimateRequest) {
return adapters.muesliswap.estimate(body)
},

async create(body: Swap.CreateRequest) {
return adapters.muesliswap.create(body)
},

async cancel(body: Swap.CancelRequest) {
return adapters.muesliswap.cancel(body)
},
},
true,
)
}
16 changes: 6 additions & 10 deletions packages/types/src/swap/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,14 @@ export type SwapCancelResponse = {
additionalCancellationFee?: number
}
export type SwapApi = Readonly<{
orders: () => Promise<Readonly<ApiResponse<Array<SwapOrder>>>>
tokens: () => Promise<Readonly<ApiResponse<Array<PortfolioTokenInfo>>>>
orders: () => Promise<ApiResponse<Array<SwapOrder>>>
tokens: () => Promise<ApiResponse<Array<PortfolioTokenInfo>>>
providers(
args: SwapProvidersRequest,
): Promise<Readonly<ApiResponse<SwapProvidersResponse>>>
): Promise<ApiResponse<SwapProvidersResponse>>
estimate(
args: SwapEstimateRequest,
): Promise<Readonly<ApiResponse<SwapEstimateResponse>>>
create(
args: SwapCreateRequest,
): Promise<Readonly<ApiResponse<SwapCreateResponse>>>
cancel: (
args: SwapCancelRequest,
) => Promise<Readonly<ApiResponse<SwapCancelResponse>>>
): Promise<ApiResponse<SwapEstimateResponse>>
create(args: SwapCreateRequest): Promise<ApiResponse<SwapCreateResponse>>
cancel: (args: SwapCancelRequest) => Promise<ApiResponse<SwapCancelResponse>>
}>

0 comments on commit 686fae1

Please sign in to comment.