Skip to content

Commit

Permalink
add undocumented tokens endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jorbuedo authored and stackchain committed Nov 11, 2024
1 parent 304b280 commit b4a1fb7
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
50 changes: 50 additions & 0 deletions packages/swap/src/adapters/dexhunter-api/api-maker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
ReverseEstimateResponse,
SignResponse,
SwapResponse,
TokensResponse,
} from './types'
import {transformers} from './transformers'
import {DexhunterApi} from './dexhunter'
Expand Down Expand Up @@ -103,6 +104,54 @@ export const dexhunterApiMaker = ({
)
},

async tokens() {
const response = await request<TokensResponse>({
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<typeof transformers.orders.request>[0]) {
const response = await request<OrdersResponse>({
method: 'get',
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions packages/swap/src/adapters/dexhunter-api/dexhunter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export type DexhunterApi = {
averagePrice: (
args: AveragePriceArgs,
) => Promise<Readonly<Api.Response<number>>>
tokens: () => Promise<Readonly<Api.Response<Array<Portfolio.Token.Info>>>>
orders: (args: OrdersArgs) => Promise<Readonly<Api.Response<OrdersResponse>>>
estimate: (
args: EstimateArgs,
Expand Down
42 changes: 42 additions & 0 deletions packages/swap/src/adapters/dexhunter-api/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
SignResponse,
SwapRequest,
SwapResponse,
TokensResponse,
} from './types'
import {isPrimaryToken} from '@yoroi/portfolio'
import {
Expand All @@ -37,13 +38,54 @@ 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
dcaCreate: noTransforms, // unused
dcaEstimate: noTransforms, // unused
dcaByAdress: noTransforms, // unused
markingSubmit: noTransforms, // unused
tokens: {
request: Identity,
response: (res: TokensResponse): Array<Portfolio.Token.Info> =>
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),
Expand Down
16 changes: 14 additions & 2 deletions packages/swap/src/adapters/dexhunter-api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,7 +35,7 @@ export type OrdersResponse = {
update_tx_hash?: string
user_address?: string
user_stake?: string
}
}>

export type Split = {
amount_in?: number
Expand Down Expand Up @@ -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
}>

0 comments on commit b4a1fb7

Please sign in to comment.