Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jorbuedo committed Nov 11, 2024
1 parent 33689a7 commit c857655
Show file tree
Hide file tree
Showing 3 changed files with 241 additions and 236 deletions.
106 changes: 67 additions & 39 deletions packages/swap/src/adapters/muesliswap-api/api-maker.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import {FetchData, fetchData, isLeft} from '@yoroi/common'
import {Chain, Portfolio, Swap} from '@yoroi/types'
import {Api, Chain, Portfolio, Swap} from '@yoroi/types'
import {freeze} from 'immer'
import {
CancelRequest,
CancelResponse,
ConstructSwapDatumResponse,
LiquidityPoolResponse,
OrdersAggregatorResponse,
OrdersHistoryResponse,
TokensResponse,
} from './types'
import {transformersMaker} from './transformers'

export type MuesliswapApiConfig = {
addressHex: string
address: string
primaryTokenInfo: Portfolio.Token.Info
stakingKey: string
Expand All @@ -20,7 +23,7 @@ export type MuesliswapApiConfig = {
export const muesliswapApiMaker = (
config: MuesliswapApiConfig,
): Readonly<Swap.Api> => {
const {stakingKey, address, network, request = fetchData} = config
const {stakingKey, addressHex, network, request = fetchData} = config

if (network !== Chain.Network.Mainnet)
return new Proxy(
Expand Down Expand Up @@ -94,7 +97,7 @@ export const muesliswapApiMaker = (
},
{
params: {
wallet: address,
wallet: addressHex,
},
},
),
Expand Down Expand Up @@ -123,46 +126,70 @@ export const muesliswapApiMaker = (
},

async estimate(body: Swap.EstimateRequest) {
const kind: 'estimate' | 'reverseEstimate' | 'limitEstimate' =
body.wantedPrice !== undefined
? 'limitEstimate'
: body.amountOut !== undefined
? 'reverseEstimate'
: 'estimate'

const response = await request<
EstimateResponse | ReverseEstimateResponse | LimitEstimateResponse
>({
method: 'post',
url: `${baseUrl}${apiPaths[kind]}`,
headers,
data: transformers[kind].request(body, config),
})
const params = transformers.estimate.request(body)

if (isLeft(response)) return response

return freeze(
const response = await request<LiquidityPoolResponse>(
{
tag: 'right',
value: {
status: response.value.status,
data: transformers[kind].response(response.value.data as any),
},
method: 'get',
url: apiUrls.liquidityPools,
headers,
},
{
params,
},
true,
)

if (isLeft(response)) return response

try {
return freeze(
{
tag: 'right',
value: {
status: response.value.status,
data: transformers.estimate.response(response.value.data, body),
},
},
true,
)
} finally {
return freeze(
{
tag: 'left',
error: {
status: -3,
message: 'No liquidity pools satisfy the estimate requirements',
responseData: response.value.data,
},
},
true,
)
}
},

async create(body: Swap.CreateRequest) {
const kind: 'build' | 'limitBuild' =
body.wantedPrice !== undefined ? 'limitBuild' : 'build'
const estimateResponse: Api.Response<Swap.EstimateResponse> =
await this.estimate({...body, slippage: body.slippage ?? 0})

const response = await request<BuildResponse | LimitBuildResponse>({
method: 'post',
url: `${baseUrl}${apiPaths[kind]}`,
headers,
data: transformers[kind].request(body, config),
})
if (isLeft(estimateResponse)) return estimateResponse

const lastEstimate = estimateResponse.value.data

const params = transformers.constructSwapDatum.request(
body,
lastEstimate.splits[0]!,
)

const response = await request<ConstructSwapDatumResponse>(
{
method: 'get',
url: apiUrls.constructSwapDatum,
headers,
},
{
params,
},
)

if (isLeft(response)) return response

Expand All @@ -171,7 +198,10 @@ export const muesliswapApiMaker = (
tag: 'right',
value: {
status: response.value.status,
data: transformers[kind].response(response.value.data as any),
data: transformers.constructSwapDatum.response(
response.value.data,
lastEstimate,
),
},
},
true,
Expand Down Expand Up @@ -214,9 +244,7 @@ const apiUrls = {
tokens: 'https://api.muesliswap.com/list',
ordersHistory: 'https://api.muesliswap.com/orders/v3/history',
ordersAggregator: 'https://api.muesliswap.com/orders/aggregator',
getOrders: 'https://onchain2.muesliswap.com/orders/all/',
getPoolsPair: 'https://onchain2.muesliswap.com/pools/pair',
getLiquidityPools: 'https://api.muesliswap.com/liquidity/pools',
liquidityPools: 'https://api.muesliswap.com/liquidity/pools',
constructSwapDatum: 'https://aggregator.muesliswap.com/constructSwapDatum',
cancel: 'https://aggregator.muesliswap.com/cancelSwapTransaction',
} as const
Loading

0 comments on commit c857655

Please sign in to comment.