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 e8f76a1 commit 12762c1
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 150 deletions.
16 changes: 5 additions & 11 deletions packages/swap/src/adapters/dexhunter-api/api-maker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,7 @@ export type DexhunterApiConfig = {
export const dexhunterApiMaker = (
config: DexhunterApiConfig,
): Readonly<Swap.Api> => {
const {
address,
primaryTokenInfo,
partnerId,
network,
request = fetchData,
} = config
const {address, partnerId, network, request = fetchData} = config

if (network !== Chain.Network.Mainnet)
return new Proxy(
Expand Down Expand Up @@ -60,7 +54,7 @@ export const dexhunterApiMaker = (
...(partnerId && {'X-Partner-Id': partnerId}),
}

const transformers = transformersMaker(primaryTokenInfo)
const transformers = transformersMaker(config)

return freeze(
{
Expand Down Expand Up @@ -120,7 +114,7 @@ export const dexhunterApiMaker = (
method: 'post',
url: `${baseUrl}${apiPaths[kind]}`,
headers,
data: transformers[kind].request(body, config),
data: transformers[kind].request(body),
})

if (isLeft(response)) return response
Expand All @@ -145,7 +139,7 @@ export const dexhunterApiMaker = (
method: 'post',
url: `${baseUrl}${apiPaths[kind]}`,
headers,
data: transformers[kind].request(body, config),
data: transformers[kind].request(body),
})

if (isLeft(response)) return response
Expand All @@ -167,7 +161,7 @@ export const dexhunterApiMaker = (
method: 'post',
url: `${baseUrl}${apiPaths.cancel}`,
headers,
data: transformers.cancel.request(body, config),
data: transformers.cancel.request(body),
})

if (isLeft(response)) return response
Expand Down
103 changes: 44 additions & 59 deletions packages/swap/src/adapters/dexhunter-api/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ const transformSplit = ({
priceDistortion: price_distortion,
priceImpact: price_impact,
})
export const transformersMaker = (primaryTokenInfo: Portfolio.Token.Info) => {
export const transformersMaker = ({
primaryTokenInfo,
address,
}: DexhunterApiConfig) => {
const tokenIdFromDexhunter = (tokenId: string): Portfolio.Token.Id =>
tokenId ===
'000000000000000000000000000000000000000000000000000000006c6f76656c616365'
Expand Down Expand Up @@ -138,10 +141,7 @@ export const transformersMaker = (primaryTokenInfo: Portfolio.Token.Info) => {
),
},
cancel: {
request: (
{order}: Swap.CancelRequest,
{address}: DexhunterApiConfig,
): CancelRequest => ({
request: ({order}: Swap.CancelRequest): CancelRequest => ({
address,
order_id: order.customId,
}),
Expand All @@ -154,16 +154,13 @@ export const transformersMaker = (primaryTokenInfo: Portfolio.Token.Info) => {
}),
},
estimate: {
request: (
{
amountIn,
blacklistedDexes,
slippage,
tokenIn,
tokenOut,
}: Swap.EstimateRequest,
_config: DexhunterApiConfig,
): EstimateRequest => ({
request: ({
amountIn,
blacklistedDexes,
slippage,
tokenIn,
tokenOut,
}: Swap.EstimateRequest): EstimateRequest => ({
amount_in: amountIn,
blacklisted_dexes: blacklistedDexes,
slippage,
Expand Down Expand Up @@ -193,16 +190,13 @@ export const transformersMaker = (primaryTokenInfo: Portfolio.Token.Info) => {
}),
},
reverseEstimate: {
request: (
{
amountOut,
blacklistedDexes,
slippage,
tokenIn,
tokenOut,
}: Swap.EstimateRequest,
_config: DexhunterApiConfig,
): ReverseEstimateRequest => ({
request: ({
amountOut,
blacklistedDexes,
slippage,
tokenIn,
tokenOut,
}: Swap.EstimateRequest): ReverseEstimateRequest => ({
amount_out: amountOut,
blacklisted_dexes: blacklistedDexes,
slippage,
Expand Down Expand Up @@ -234,18 +228,15 @@ export const transformersMaker = (primaryTokenInfo: Portfolio.Token.Info) => {
}),
},
limitEstimate: {
request: (
{
amountIn,
blacklistedDexes,
dex,
multiples,
tokenIn,
tokenOut,
wantedPrice,
}: Swap.EstimateRequest,
_config: DexhunterApiConfig,
): LimitEstimateRequest => ({
request: ({
amountIn,
blacklistedDexes,
dex,
multiples,
tokenIn,
tokenOut,
wantedPrice,
}: Swap.EstimateRequest): LimitEstimateRequest => ({
amount_in: amountIn,
blacklisted_dexes: blacklistedDexes,
dex: dex,
Expand Down Expand Up @@ -277,18 +268,15 @@ export const transformersMaker = (primaryTokenInfo: Portfolio.Token.Info) => {
}),
},
limitBuild: {
request: (
{
amountIn,
blacklistedDexes,
dex,
multiples,
tokenIn,
tokenOut,
wantedPrice,
}: Swap.CreateRequest,
{address}: DexhunterApiConfig,
): LimitBuildRequest => ({
request: ({
amountIn,
blacklistedDexes,
dex,
multiples,
tokenIn,
tokenOut,
wantedPrice,
}: Swap.CreateRequest): LimitBuildRequest => ({
amount_in: amountIn,
blacklisted_dexes: blacklistedDexes,
buyer_address: address,
Expand Down Expand Up @@ -321,16 +309,13 @@ export const transformersMaker = (primaryTokenInfo: Portfolio.Token.Info) => {
}),
},
build: {
request: (
{
amountIn,
blacklistedDexes,
slippage = 0,
tokenIn,
tokenOut,
}: Swap.CreateRequest,
{address}: DexhunterApiConfig,
): BuildRequest => ({
request: ({
amountIn,
blacklistedDexes,
slippage = 0,
tokenIn,
tokenOut,
}: Swap.CreateRequest): BuildRequest => ({
amount_in: amountIn,
blacklisted_dexes: blacklistedDexes,
buyer_address: address,
Expand Down
64 changes: 48 additions & 16 deletions packages/swap/src/adapters/muesliswap-api/api-maker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {FetchData, fetchData, isLeft} from '@yoroi/common'
import {Chain, Portfolio, Swap} from '@yoroi/types'
import {freeze} from 'immer'
import {TokensResponse} from './types'
import {
OrdersAggregatorResponse,
OrdersHistoryResponse,
TokensResponse,
} from './types'
import {transformersMaker} from './transformers'

export type MuesliswapApiConfig = {
Expand All @@ -14,7 +18,7 @@ export type MuesliswapApiConfig = {
export const muesliswapApiMaker = (
config: MuesliswapApiConfig,
): Readonly<Swap.Api> => {
const {stakingKey, primaryTokenInfo, network, request = fetchData} = config
const {stakingKey, address, network, request = fetchData} = config

if (network !== Chain.Network.Mainnet)
return new Proxy(
Expand All @@ -41,14 +45,14 @@ export const muesliswapApiMaker = (
'Accept': 'application/json',
}

const transformers = transformersMaker(primaryTokenInfo)
const transformers = transformersMaker(config)

return freeze(
{
async tokens() {
const response = await request<TokensResponse>({
method: 'get',
url: apiUrls.getTokenList,
url: apiUrls.tokens,
headers,
})

Expand All @@ -67,20 +71,49 @@ export const muesliswapApiMaker = (
},

async orders() {
const response = await request<OrdersResponse>({
method: 'get',
url: `${baseUrl}${apiPaths.orders({address})}`,
headers,
})
const [historyResponse, aggregatorResponse] = await Promise.all([
request<OrdersHistoryResponse>(
{
method: 'get',
url: apiUrls.ordersHistory,
headers,
},
{
params: {
'stake-key-hash': stakingKey,
},
},
),
request<OrdersAggregatorResponse>(
{
method: 'get',
url: apiUrls.ordersAggregator,
headers,
},
{
params: {
wallet: address,
},
},
),
])

if (isLeft(response)) return response
if (isLeft(historyResponse)) return historyResponse
if (isLeft(aggregatorResponse)) return aggregatorResponse

return freeze(
{
tag: 'right',
value: {
status: response.value.status,
data: transformers.orders.response(response.value.data),
status: 200,
data: [
...transformers.ordersHistory.response(
historyResponse.value.data,
),
...transformers.ordersAggregator.response(
aggregatorResponse.value.data,
),
],
},
},
true,
Expand Down Expand Up @@ -170,14 +203,13 @@ export const muesliswapApiMaker = (
}

const apiUrls = {
getTokenList: 'https://api.muesliswap.com/list', // only verified, extra data
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',
constructSwapDatum: 'https://aggregator.muesliswap.com/constructSwapDatum',
cancelSwapTransaction:
'https://aggregator.muesliswap.com/cancelSwapTransaction',
getCompletedOrders: 'https://api.muesliswap.com/orders/v3/history',
getPrice: 'https://api.muesliswap.com/price',
getTokens: 'https://api.muesliswap.com/token-list', // includes unverified so request is slow and heavy
} as const
Loading

0 comments on commit 12762c1

Please sign in to comment.