Skip to content

Commit

Permalink
WIP new muesli API
Browse files Browse the repository at this point in the history
  • Loading branch information
jorbuedo committed Nov 26, 2024
1 parent 9bfef73 commit 26e3a04
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 705 deletions.
3 changes: 1 addition & 2 deletions packages/swap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@
]
},
"dependencies": {
"@emurgo/cip14-js": "^3.0.1",
"lodash-es": "^4.17.21"
"@emurgo/cip14-js": "^3.0.1"
},
"devDependencies": {
"@commitlint/config-conventional": "^17.0.2",
Expand Down
14 changes: 0 additions & 14 deletions packages/swap/src/adapters/api/dexhunter/api-maker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,4 @@ const apiPaths = {
reverseEstimate: '/swap/reverseEstimate', // POST
build: '/swap/build', // POST
sign: '/swap/sign', // POST
averagePrice: ({
tokenInId,
tokenOutId,
}: {
tokenInId: string
tokenOutId: string
}) => `/swap/averagePrice/${tokenInId}/${tokenOutId}`, // GET
wallet: '/swap/wallet', // POST
charts: '/charts', // POST
dcaCancel: '/dca/cancel', // POST
dcaCreate: '/dca/create', // POST
dcaEstimate: '/dca/estimate', // POST
dcaByAdress: ({address}: {address: string}) => `/dca/${address}`, // GET
markingSubmit: '/marking/submit', // POST
} as const
143 changes: 37 additions & 106 deletions packages/swap/src/adapters/api/muesliswap/api-maker.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import {FetchData, fetchData, isLeft} from '@yoroi/common'
import {Api, App, Chain, Portfolio, Swap} from '@yoroi/types'
import {Chain, Portfolio, Swap} from '@yoroi/types'
import {freeze} from 'immer'
import {memoize} from 'lodash-es'
import {
CancelRequest,
CancelResponse,
ConstructSwapDatumResponse,
LiquidityPoolResponse,
OrdersAggregatorResponse,
OrdersHistoryResponse,
OpenOrdersResponse,
HistoryOrdersResponse,
TokensResponse,
CreateOrderResponse,
QuoteResponse,
} from './types'
import {transformersMaker} from './transformers'
import {estimateCalculation} from './calculations'

export type MuesliswapApiConfig = {
frontendFeeTiers?: ReadonlyArray<App.FrontendFeeTier>
getLpTokensHeld?: () => number
addressHex: string
address: string
primaryTokenInfo: Portfolio.Token.Info
Expand All @@ -27,15 +23,7 @@ export type MuesliswapApiConfig = {
export const muesliswapApiMaker = (
config: MuesliswapApiConfig,
): Readonly<Swap.Api> => {
const {
frontendFeeTiers,
getLpTokensHeld = () => 0,
stakingKey,
addressHex,
primaryTokenInfo,
network,
request = fetchData,
} = config
const {address, addressHex, network, request = fetchData} = config

if (network !== Chain.Network.Mainnet)
return new Proxy(
Expand Down Expand Up @@ -64,48 +52,6 @@ export const muesliswapApiMaker = (

const transformers = transformersMaker(config)

// Asking for pools on every amount change would be bad UI and third party API spam
const getLiquidityPools = memoize(
async (body: Swap.EstimateRequest) => {
const params = transformers.liquidityPools.request(body)

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

if (isLeft(response)) return response

return freeze(
{
tag: 'right' as const,
value: {
status: response.value.status,
data: transformers.liquidityPools.response(
response.value.data,
body,
),
},
},
true,
)
},
({tokenIn, tokenOut, dex, blacklistedDexes}) =>
[
new Date().getMinutes(), // cache every minute
tokenIn,
tokenOut,
dex,
blacklistedDexes?.join(),
].join('_'),
)

return freeze(
{
async tokens() {
Expand All @@ -131,27 +77,27 @@ export const muesliswapApiMaker = (

async orders() {
const [historyResponse, aggregatorResponse] = await Promise.all([
request<OrdersHistoryResponse>(
request<HistoryOrdersResponse>(
{
method: 'get',
url: apiUrls.ordersHistory,
url: apiUrls.orderHistory,
headers,
},
{
params: {
'stake-key-hash': stakingKey,
user_address: address,
},
},
),
request<OrdersAggregatorResponse>(
request<OpenOrdersResponse>(
{
method: 'get',
url: apiUrls.ordersAggregator,
url: apiUrls.openOrders,
headers,
},
{
params: {
wallet: addressHex,
user_address: addressHex,
},
},
),
Expand All @@ -166,10 +112,10 @@ export const muesliswapApiMaker = (
value: {
status: 200,
data: [
...transformers.ordersHistory.response(
...transformers.orderHistory.response(
historyResponse.value.data,
),
...transformers.ordersAggregator.response(
...transformers.openOrders.response(
aggregatorResponse.value.data,
),
],
Expand All @@ -180,10 +126,18 @@ export const muesliswapApiMaker = (
},

async estimate(body: Swap.EstimateRequest) {
// This cache is very dumb, clear on ocasion so it doesn't accumulate too many entries
if (body.amountIn === 0) getLiquidityPools.cache.clear?.()
const params = transformers.quote.request(body)

const response = await getLiquidityPools(body)
const response = await request<QuoteResponse>(
{
method: 'get',
url: apiUrls.create,
headers,
},
{
params,
},
)

if (isLeft(response)) return response

Expand All @@ -193,13 +147,7 @@ export const muesliswapApiMaker = (
tag: 'right',
value: {
status: response.value.status,
data: estimateCalculation(
response.value.data,
body,
primaryTokenInfo,
frontendFeeTiers,
getLpTokensHeld(),
),
data: transformers.quote.response(response.value.data),
},
},
true,
Expand All @@ -220,22 +168,12 @@ export const muesliswapApiMaker = (
},

async create(body: Swap.CreateRequest) {
const estimateResponse: Api.Response<Swap.EstimateResponse> =
await this.estimate({...body, slippage: body.slippage ?? 0})

if (isLeft(estimateResponse)) return estimateResponse

const lastEstimate = estimateResponse.value.data

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

const response = await request<ConstructSwapDatumResponse>(
// TODO LIMIT
const params = transformers.create.request(body)
const response = await request<CreateOrderResponse>(
{
method: 'get',
url: apiUrls.constructSwapDatum,
url: apiUrls.create,
headers,
},
{
Expand All @@ -250,10 +188,7 @@ export const muesliswapApiMaker = (
tag: 'right',
value: {
status: response.value.status,
data: transformers.constructSwapDatum.response(
response.value.data,
lastEstimate,
),
data: transformers.create.response(response.value.data),
},
},
true,
Expand Down Expand Up @@ -294,14 +229,10 @@ export const muesliswapApiMaker = (

const apiUrls = {
tokens: 'https://api.muesliswap.com/list',
ordersHistory: 'https://api.muesliswap.com/orders/v3/history',
ordersAggregator: 'https://api.muesliswap.com/orders/aggregator',
liquidityPools: 'https://api.muesliswap.com/liquidity/pools',
constructSwapDatum: 'https://aggregator.muesliswap.com/constructSwapDatum',
cancel: 'https://aggregator.muesliswap.com/cancelSwapTransaction',
orderHistory: 'https://aggregator-v2.muesliswap.com/order_history',
openOrders: 'https://aggregator-v2.muesliswap.com/open_orders',
quote: 'https://aggregator-v2.muesliswap.com/quote',
create: 'https://aggregator-v2.muesliswap.com/order',
createLimit: 'https://aggregator-v2.muesliswap.com/limit_order',
cancel: 'https://aggregator-v2.muesliswap.com/cancel',
} as const

export const milkTokenId =
'afbe91c0b44b3040e360057bf8354ead8c49c4979ae6ab7c4fbdc9eb.4d494c4b7632'
export const oldMilkTokenId =
'8a1cfae21368b8bebbbed9800fec304e95cce39a2a57dc35e2e3ebaa.4d494c4b'
Loading

0 comments on commit 26e3a04

Please sign in to comment.