Skip to content

Commit

Permalink
draft dh api and transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
jorbuedo committed Nov 5, 2024
1 parent 938ba1f commit 2ef5000
Show file tree
Hide file tree
Showing 3 changed files with 673 additions and 30 deletions.
317 changes: 315 additions & 2 deletions packages/swap/src/adapters/dexhunter-api/api-maker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import {FetchData, fetchData, isRight} from '@yoroi/common'
import {Chain} from '@yoroi/types'
import {freeze} from 'immer'
import {AveragePriceResponse, EstimateResponse, OrdersResponse} from './types'
import {
AveragePriceResponse,
CancelResponse,
EstimateResponse,
LimitOrderEstimate,
LimitOrderResponse,
OrdersResponse,
ReverseEstimateResponse,
SignResponse,
SwapResponse,
} from './types'
import {transformers} from './transformers'
import {DexhunterApi} from './dexhunter'

Expand Down Expand Up @@ -92,6 +102,7 @@ export const dexhunterApiMaker = ({
true,
)
},

async orders(params: Parameters<typeof transformers.orders.request>[0]) {
const response = await request<OrdersResponse>({
method: 'get',
Expand Down Expand Up @@ -185,7 +196,309 @@ export const dexhunterApiMaker = ({
tag: 'left',
error: {
status: -3,
message: 'Failed to to estimate',
message: 'Failed to fetch estimate',
responseData: response.error.responseData,
},
},
true,
)
},

async reverseEstimate(
body: Parameters<typeof transformers.reverseEstimate.request>[0],
) {
const response = await request<ReverseEstimateResponse>({
method: 'post',
url: `${baseUrl}${apiPaths.estimate}`,
headers,
data: transformers.reverseEstimate.request(body),
})

if (isRight(response)) {
try {
const data = transformers.reverseEstimate.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 reverse estimate',
responseData: response.value.data,
},
},
true,
)
}
}
return freeze(
{
tag: 'left',
error: {
status: -3,
message: 'Failed to fetch reverse estimate',
responseData: response.error.responseData,
},
},
true,
)
},

async swap(body: Parameters<typeof transformers.swap.request>[0]) {
const response = await request<SwapResponse>({
method: 'post',
url: `${baseUrl}${apiPaths.swap}`,
headers,
data: transformers.swap.request(body),
})

if (isRight(response)) {
try {
const data = transformers.swap.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 swap',
responseData: response.value.data,
},
},
true,
)
}
}
return freeze(
{
tag: 'left',
error: {
status: -3,
message: 'Failed to fetch swap',
responseData: response.error.responseData,
},
},
true,
)
},

async sign(body: Parameters<typeof transformers.sign.request>[0]) {
const response = await request<SignResponse>({
method: 'post',
url: `${baseUrl}${apiPaths.sign}`,
headers,
data: transformers.sign.request(body),
})

if (isRight(response)) {
try {
const data = transformers.sign.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 sign',
responseData: response.value.data,
},
},
true,
)
}
}
return freeze(
{
tag: 'left',
error: {
status: -3,
message: 'Failed to fetch sign',
responseData: response.error.responseData,
},
},
true,
)
},

async cancel(body: Parameters<typeof transformers.cancel.request>[0]) {
const response = await request<CancelResponse>({
method: 'post',
url: `${baseUrl}${apiPaths.cancel}`,
headers,
data: transformers.cancel.request(body),
})

if (isRight(response)) {
try {
const data = transformers.cancel.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 cancel',
responseData: response.value.data,
},
},
true,
)
}
}
return freeze(
{
tag: 'left',
error: {
status: -3,
message: 'Failed to fetch cancel',
responseData: response.error.responseData,
},
},
true,
)
},

async limit(body: Parameters<typeof transformers.limit.request>[0]) {
const response = await request<LimitOrderResponse>({
method: 'post',
url: `${baseUrl}${apiPaths.limit}`,
headers,
data: transformers.limit.request(body),
})

if (isRight(response)) {
try {
const data = transformers.limit.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 limit',
responseData: response.value.data,
},
},
true,
)
}
}
return freeze(
{
tag: 'left',
error: {
status: -3,
message: 'Failed to fetch limit',
responseData: response.error.responseData,
},
},
true,
)
},

async limitEstimate(
body: Parameters<typeof transformers.limitEstimate.request>[0],
) {
const response = await request<LimitOrderEstimate>({
method: 'post',
url: `${baseUrl}${apiPaths.limitEstimate}`,
headers,
data: transformers.limitEstimate.request(body),
})

if (isRight(response)) {
try {
const data = transformers.limitEstimate.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 limit estimate',
responseData: response.value.data,
},
},
true,
)
}
}
return freeze(
{
tag: 'left',
error: {
status: -3,
message: 'Failed to fetch limit estimate',
responseData: response.error.responseData,
},
},
Expand Down
Loading

0 comments on commit 2ef5000

Please sign in to comment.