From 33689a7bf069bfe04a97f8526fa0127e81b8b5e2 Mon Sep 17 00:00:00 2001 From: jorbuedo Date: Mon, 11 Nov 2024 18:12:54 +0100 Subject: [PATCH] WIP --- .../adapters/dexhunter-api/transformers.ts | 10 +- .../swap/src/adapters/dexhunter-api/types.ts | 4 +- .../src/adapters/muesliswap-api/api-maker.ts | 23 ++-- .../adapters/muesliswap-api/transformers.ts | 112 ++++++++---------- .../swap/src/adapters/muesliswap-api/types.ts | 19 ++- packages/types/src/swap/api.ts | 5 +- 6 files changed, 93 insertions(+), 80 deletions(-) diff --git a/packages/swap/src/adapters/dexhunter-api/transformers.ts b/packages/swap/src/adapters/dexhunter-api/transformers.ts index 8858ed93c8..3a29d90b34 100644 --- a/packages/swap/src/adapters/dexhunter-api/transformers.ts +++ b/packages/swap/src/adapters/dexhunter-api/transformers.ts @@ -114,20 +114,21 @@ 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), @@ -135,6 +136,7 @@ export const transformersMaker = ({ actualAmountOut: actual_out_amount, expectedAmountOut: expected_out_amount, txHash: tx_hash, + outputIndex: output_index, updateTxHash: update_tx_hash, customId: _id, }), diff --git a/packages/swap/src/adapters/dexhunter-api/types.ts b/packages/swap/src/adapters/dexhunter-api/types.ts index 0adbc1b1fd..65894b4ec2 100644 --- a/packages/swap/src/adapters/dexhunter-api/types.ts +++ b/packages/swap/src/adapters/dexhunter-api/types.ts @@ -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 diff --git a/packages/swap/src/adapters/muesliswap-api/api-maker.ts b/packages/swap/src/adapters/muesliswap-api/api-maker.ts index af20374b30..f4074a0b44 100644 --- a/packages/swap/src/adapters/muesliswap-api/api-maker.ts +++ b/packages/swap/src/adapters/muesliswap-api/api-maker.ts @@ -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, @@ -177,12 +179,18 @@ export const muesliswapApiMaker = ( }, async cancel(body: Swap.CancelRequest) { - const response = await request({ - method: 'post', - url: `${baseUrl}${apiPaths.cancel}`, - headers, - data: transformers.cancel.request(body, config), - }) + const params: CancelRequest = transformers.cancel.request(body) + + const response = await request( + { + method: 'post', + url: apiUrls.cancel, + headers, + }, + { + params, + }, + ) if (isLeft(response)) return response @@ -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 diff --git a/packages/swap/src/adapters/muesliswap-api/transformers.ts b/packages/swap/src/adapters/muesliswap-api/transformers.ts index 8bc87d0929..75f2b90e57 100644 --- a/packages/swap/src/adapters/muesliswap-api/transformers.ts +++ b/packages/swap/src/adapters/muesliswap-api/transformers.ts @@ -1,5 +1,7 @@ import {Portfolio, Swap} from '@yoroi/types' import { + CancelRequest, + CancelResponse, OrdersAggregatorResponse, OrdersHistoryResponse, TokensResponse, @@ -65,35 +67,30 @@ export const transformersMaker = ({ response: (res: OrdersAggregatorResponse): Array => 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, }), ), }, @@ -101,49 +98,42 @@ export const transformersMaker = ({ response: (res: OrdersHistoryResponse): Array => 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: { diff --git a/packages/swap/src/adapters/muesliswap-api/types.ts b/packages/swap/src/adapters/muesliswap-api/types.ts index 286972e66d..eb0e75945d 100644 --- a/packages/swap/src/adapters/muesliswap-api/types.ts +++ b/packages/swap/src/adapters/muesliswap-api/types.ts @@ -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 +} diff --git a/packages/types/src/swap/api.ts b/packages/types/src/swap/api.ts index 4c3a4de736..805283cbf8 100644 --- a/packages/types/src/swap/api.ts +++ b/packages/types/src/swap/api.ts @@ -12,8 +12,8 @@ export type SwapAggregator = export type SwapOrder = { aggregator: SwapAggregator dex: string - placedAt?: string - lastUpdate?: string + placedAt?: number + lastUpdate?: number status: string tokenIn: PortfolioTokenId tokenOut: PortfolioTokenId @@ -21,6 +21,7 @@ export type SwapOrder = { actualAmountOut: number expectedAmountOut: number txHash?: string + outputIndex?: number updateTxHash?: string customId?: string }