diff --git a/packages/swap/src/adapters/dexhunter-api/api-maker.ts b/packages/swap/src/adapters/dexhunter-api/api-maker.ts index faa95498ad..cf4909d9b8 100644 --- a/packages/swap/src/adapters/dexhunter-api/api-maker.ts +++ b/packages/swap/src/adapters/dexhunter-api/api-maker.ts @@ -11,6 +11,7 @@ import { ReverseEstimateResponse, SignResponse, SwapResponse, + TokensResponse, } from './types' import {transformers} from './transformers' import {DexhunterApi} from './dexhunter' @@ -103,6 +104,54 @@ export const dexhunterApiMaker = ({ ) }, + async tokens() { + const response = await request({ + method: 'get', + url: `${baseUrl}${apiPaths.tokens}`, + headers, + }) + + if (isRight(response)) { + try { + const data = transformers.tokens.response(response.value.data) + + return freeze( + { + tag: 'right', + value: { + status: response.value.status, + data, + }, + }, + true, + ) + } catch (e) { + return freeze( + { + tag: 'left', + error: { + status: -3, + message: 'Failed to transform tokens', + responseData: response.value.data, + }, + }, + true, + ) + } + } + return freeze( + { + tag: 'left', + error: { + status: -3, + message: 'Failed to fetch tokens', + responseData: response.error.responseData, + }, + }, + true, + ) + }, + async orders(params: Parameters[0]) { const response = await request({ method: 'get', @@ -521,6 +570,7 @@ const apiPaths = { dcaEstimate: '/dca/estimate', // POST dcaByAdress: ({address}: {address: string}) => `/dca/${address}`, // GET markingSubmit: '/marking/submit', // POST + tokens: '/swap/tokens', // GET averagePrice: ({ tokenInId, tokenOutId, diff --git a/packages/swap/src/adapters/dexhunter-api/dexhunter.ts b/packages/swap/src/adapters/dexhunter-api/dexhunter.ts index 856882c826..2215efd242 100644 --- a/packages/swap/src/adapters/dexhunter-api/dexhunter.ts +++ b/packages/swap/src/adapters/dexhunter-api/dexhunter.ts @@ -71,6 +71,7 @@ export type DexhunterApi = { averagePrice: ( args: AveragePriceArgs, ) => Promise>> + tokens: () => Promise>>> orders: (args: OrdersArgs) => Promise>> estimate: ( args: EstimateArgs, diff --git a/packages/swap/src/adapters/dexhunter-api/transformers.ts b/packages/swap/src/adapters/dexhunter-api/transformers.ts index 1abc9f8754..601fd970fd 100644 --- a/packages/swap/src/adapters/dexhunter-api/transformers.ts +++ b/packages/swap/src/adapters/dexhunter-api/transformers.ts @@ -14,6 +14,7 @@ import { SignResponse, SwapRequest, SwapResponse, + TokensResponse, } from './types' import {isPrimaryToken} from '@yoroi/portfolio' import { @@ -37,6 +38,12 @@ const noTransforms = { const tokenIdToDexhunter = (tokenId: Portfolio.Token.Id) => isPrimaryToken(tokenId) ? 'ADA' : tokenId.replace('.', '') +const tokenIdFromDexhunter = ( + tokenId: string, + tokenPolicy: string, +): Portfolio.Token.Id => + `${tokenPolicy}.${tokenId?.slice(tokenPolicy.length) ?? ''}` + export const transformers = { charts: noTransforms, // unused dcaCancel: noTransforms, // unused @@ -44,6 +51,41 @@ export const transformers = { dcaEstimate: noTransforms, // unused dcaByAdress: noTransforms, // unused markingSubmit: noTransforms, // unused + tokens: { + request: Identity, + response: (res: TokensResponse): Array => + res.map( + ({ + token_id, + token_decimals, + token_policy, + token_ascii, + ticker, + is_verified, + supply, + creation_date, + price, + }) => ({ + id: tokenIdFromDexhunter(token_id, token_policy), + type: Portfolio.Token.Type.FT, + nature: Portfolio.Token.Nature.Secondary, + decimals: token_decimals ?? 0, + ticker: ticker ?? '', + name: token_ascii ?? '', + symbol: ticker ?? '', + status: is_verified + ? Portfolio.Token.Status.Valid + : Portfolio.Token.Status.Unknown, + application: Portfolio.Token.Application.General, + tag: '', + reference: '', + fingerprint: '', + description: `${price}, ${supply}, ${creation_date}`, + website: '', + originalImage: '', + }), + ), + }, averagePrice: { request: ({tokenInId, tokenOutId}: AveragePriceArgs) => ({ tokenInId: tokenIdToDexhunter(tokenInId), diff --git a/packages/swap/src/adapters/dexhunter-api/types.ts b/packages/swap/src/adapters/dexhunter-api/types.ts index ef74305e59..9da6285a21 100644 --- a/packages/swap/src/adapters/dexhunter-api/types.ts +++ b/packages/swap/src/adapters/dexhunter-api/types.ts @@ -14,7 +14,7 @@ export type CancelResponse = { cbor?: string } -export type OrdersResponse = { +export type OrdersResponse = Array<{ _id?: string actual_out_amount?: number amount_in?: number @@ -35,7 +35,7 @@ export type OrdersResponse = { update_tx_hash?: string user_address?: string user_stake?: string -} +}> export type Split = { amount_in?: number @@ -197,3 +197,15 @@ export type SwapResponse = { total_output?: number total_output_without_slippage?: number } + +export type TokensResponse = Array<{ + token_id: string + token_decimals: number + token_policy: string + token_ascii: string + ticker: string + is_verified: boolean + supply: number + creation_date: string + price: number +}>