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 12762c1 commit 33689a7
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 80 deletions.
10 changes: 6 additions & 4 deletions packages/swap/src/adapters/dexhunter-api/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,27 +114,29 @@ export const transformersMaker = ({
dex = '',
expected_out_amount = 0,
is_dexhunter = false,
last_update = '',
last_update,
status = '',
submission_time = '',
submission_time,
token_id_in = '',
token_id_out = '',
tx_hash = '',
update_tx_hash = '',
output_index,
}) => ({
aggregator: is_dexhunter
? Swap.Aggregator.Dexhunter
: Swap.Aggregator.Muesliswap,
dex,
placedAt: submission_time,
lastUpdate: last_update,
placedAt: new Date(submission_time).getTime(),
lastUpdate: new Date(last_update).getTime(),
status,
tokenIn: tokenIdFromDexhunter(token_id_in),
tokenOut: tokenIdFromDexhunter(token_id_out),
amountIn: amount_in,
actualAmountOut: actual_out_amount,
expectedAmountOut: expected_out_amount,
txHash: tx_hash,
outputIndex: output_index,
updateTxHash: update_tx_hash,
customId: _id,
}),
Expand Down
4 changes: 2 additions & 2 deletions packages/swap/src/adapters/dexhunter-api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export type OrdersResponse = Array<{
is_dexhunter?: boolean
is_oor?: boolean
is_stop_loss?: boolean
last_update?: string
last_update: string
output_index?: number
status?: string
submission_time?: string
submission_time: string
token_id_in?: string
token_id_out?: string
tx_hash?: string
Expand Down
23 changes: 15 additions & 8 deletions packages/swap/src/adapters/muesliswap-api/api-maker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {FetchData, fetchData, isLeft} from '@yoroi/common'
import {Chain, Portfolio, Swap} from '@yoroi/types'
import {freeze} from 'immer'
import {
CancelRequest,
CancelResponse,
OrdersAggregatorResponse,
OrdersHistoryResponse,
TokensResponse,
Expand Down Expand Up @@ -177,12 +179,18 @@ export const muesliswapApiMaker = (
},

async cancel(body: Swap.CancelRequest) {
const response = await request<CancelResponse>({
method: 'post',
url: `${baseUrl}${apiPaths.cancel}`,
headers,
data: transformers.cancel.request(body, config),
})
const params: CancelRequest = transformers.cancel.request(body)

const response = await request<CancelResponse>(
{
method: 'post',
url: apiUrls.cancel,
headers,
},
{
params,
},
)

if (isLeft(response)) return response

Expand Down Expand Up @@ -210,6 +218,5 @@ const apiUrls = {
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',
cancel: 'https://aggregator.muesliswap.com/cancelSwapTransaction',
} as const
112 changes: 51 additions & 61 deletions packages/swap/src/adapters/muesliswap-api/transformers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {Portfolio, Swap} from '@yoroi/types'
import {
CancelRequest,
CancelResponse,
OrdersAggregatorResponse,
OrdersHistoryResponse,
TokensResponse,
Expand Down Expand Up @@ -65,85 +67,73 @@ export const transformersMaker = ({
response: (res: OrdersAggregatorResponse): Array<Swap.Order> =>
res.map(
({
_id,
actual_out_amount = 0,
amount_in = 0,
dex = '',
expected_out_amount = 0,
is_dexhunter = false,
last_update = '',
status = '',
submission_time = '',
token_id_in = '',
token_id_out = '',
tx_hash = '',
update_tx_hash = '',
provider,
placedAt,
finalizedAt,
status,
fromToken: {address: fromToken},
toToken: {address: toToken},
fromAmount,
toAmount,
txHash,
outputIdx,
}) => ({
aggregator: is_dexhunter
? Swap.Aggregator.Muesliswap
: Swap.Aggregator.Muesliswap,
dex,
placedAt: submission_time,
lastUpdate: last_update,
aggregator: Swap.Aggregator.Muesliswap,
dex: provider,
placedAt: placedAt ? placedAt * 1000 : undefined,
lastUpdate: finalizedAt ? finalizedAt * 1000 : undefined,
status,
tokenIn: tokenIdFromMuesliswap(token_id_in),
tokenOut: tokenIdFromMuesliswap(token_id_out),
amountIn: amount_in,
actualAmountOut: actual_out_amount,
expectedAmountOut: expected_out_amount,
txHash: tx_hash,
updateTxHash: update_tx_hash,
customId: _id,
tokenIn: asYoroiTokenId(fromToken),
tokenOut: asYoroiTokenId(toToken),
amountIn: Number(fromAmount),
actualAmountOut: 0,
expectedAmountOut: Number(toAmount),
txHash,
updateTxHash: txHash,
outputIndex: outputIdx ?? 0,
}),
),
},
ordersHistory: {
response: (res: OrdersHistoryResponse): Array<Swap.Order> =>
res.map(
({
_id,
actual_out_amount = 0,
amount_in = 0,
dex = '',
expected_out_amount = 0,
is_dexhunter = false,
last_update = '',
status = '',
submission_time = '',
token_id_in = '',
token_id_out = '',
tx_hash = '',
update_tx_hash = '',
fromToken: {address: fromToken},
toToken: {address: toToken},
placedAt,
finalizedAt,
receivedAmount,
toAmount,
fromAmount,
txHash,
status,
dex = 'muesliswap',
outputIdx,
}) => ({
aggregator: is_dexhunter
? Swap.Aggregator.Muesliswap
: Swap.Aggregator.Muesliswap,
aggregator: Swap.Aggregator.Muesliswap,
dex,
placedAt: submission_time,
lastUpdate: last_update,
placedAt: placedAt ? placedAt * 1000 : undefined,
lastUpdate: finalizedAt ? finalizedAt * 1000 : undefined,
status,
tokenIn: tokenIdFromMuesliswap(token_id_in),
tokenOut: tokenIdFromMuesliswap(token_id_out),
amountIn: amount_in,
actualAmountOut: actual_out_amount,
expectedAmountOut: expected_out_amount,
txHash: tx_hash,
updateTxHash: update_tx_hash,
customId: _id,
tokenIn: asYoroiTokenId(fromToken),
tokenOut: asYoroiTokenId(toToken),
amountIn: Number(fromAmount),
actualAmountOut: Number(receivedAmount),
expectedAmountOut: Number(toAmount),
txHash,
updateTxHash: txHash,
outputIndex: outputIdx ?? 0,
}),
),
},
cancel: {
request: ({order}: Swap.CancelRequest): CancelRequest => ({
address,
order_id: order.customId,
request: ({order, collateral}: Swap.CancelRequest): CancelRequest => ({
wallet: address,
utxo: `${order.txHash ?? ''}#${order.outputIndex}`,
collateralUtxo: collateral ?? '',
}),
response: ({
additional_cancellation_fee,
cbor = '',
}: CancelResponse): Swap.CancelResponse => ({
response: ({cbor = ''}: CancelResponse): Swap.CancelResponse => ({
cbor,
additionalCancellationFee: additional_cancellation_fee,
}),
},
estimate: {
Expand Down
19 changes: 16 additions & 3 deletions packages/swap/src/adapters/muesliswap-api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,27 @@ export type OrdersHistoryResponse = Array<{
finalizedAt: number
fromAmount: string
fromToken: TokensResponse[0]['info']
outputIdx: null
outputIdx: number | null
paidAmount: string
placedAt: number
pubKeyHash: string
receivedAmount: string
scriptVersion: string
receivedAmount: string | number
status: 'matched' | string
toAmount: string
toToken: TokensResponse[0]['info']
txHash: string
scriptVersion?: string
aggregatorPlatform?: string | null
stakeKeyHash?: string
dex?: string
}>

export type CancelRequest = {
utxo: string // order UTxO from the smart contract to cancel. e.g. "txhash#0".
collateralUtxo: string // collateral UTxOs to use for canceling the order in cbor format.
wallet: string // address of the wallet that owns the order in cbor format.
}

export type CancelResponse = {
cbor: string
}
5 changes: 3 additions & 2 deletions packages/types/src/swap/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ export type SwapAggregator =
export type SwapOrder = {
aggregator: SwapAggregator
dex: string
placedAt?: string
lastUpdate?: string
placedAt?: number
lastUpdate?: number
status: string
tokenIn: PortfolioTokenId
tokenOut: PortfolioTokenId
amountIn: number
actualAmountOut: number
expectedAmountOut: number
txHash?: string
outputIndex?: number
updateTxHash?: string
customId?: string
}
Expand Down

0 comments on commit 33689a7

Please sign in to comment.