Skip to content

Commit

Permalink
wip - reogr
Browse files Browse the repository at this point in the history
  • Loading branch information
stackchain committed Nov 12, 2024
1 parent a644c27 commit ba51818
Show file tree
Hide file tree
Showing 35 changed files with 1,832 additions and 814 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Portfolio, Swap} from '@yoroi/types'

import {swapApiMaker} from './api-maker'
import {openswapMocks} from './openswap.mocks'
import {openswapMocks} from './openswap/openswap.mocks'
import {apiMocks} from './api.mocks'
import {OpenSwapApi} from './api'
import {OpenSwapApi} from './openswap/api'
import {tokenInfoMocks} from '@yoroi/portfolio'

const stakingKey = 'someStakingKey'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {Portfolio, Swap} from '@yoroi/types'

import {transformersMaker} from '../../helpers/transformers'
import {OpenSwapApi} from './api'
import {OpenSwapApi} from './openswap/api'
import {apiMocks} from './api.mocks'
import {CreateOrderRequest} from './types'
import {CreateOrderRequest} from './openswap/types'

export const swapApiMaker = (
{
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,28 +1,12 @@
import {FetchData, fetchData, isLeft} from '@yoroi/common'
import {Chain, Portfolio, Swap} from '@yoroi/types'
import {fetchData, isLeft} from '@yoroi/common'
import {Chain, Swap} from '@yoroi/types'
import {freeze} from 'immer'
import {
CancelResponse,
EstimateResponse,
LimitEstimateResponse,
LimitBuildResponse,
OrdersResponse,
ReverseEstimateResponse,
BuildResponse,
TokensResponse,
} from './types'

import {transformersMaker} from './transformers'
import {DexHunterApi} from './types'

export type DexhunterApiConfig = {
address: string
primaryTokenInfo: Portfolio.Token.Info
partnerId?: string
partnerCode?: string
network: Chain.SupportedNetworks
request?: FetchData
}
export const dexhunterApiMaker = (
config: DexhunterApiConfig,
config: Readonly<DexHunterApi.Config>,
): Readonly<Swap.Api> => {
const {address, partnerId, network, request = fetchData} = config

Expand All @@ -44,22 +28,20 @@ export const dexhunterApiMaker = (
)
},
},
) as Swap.Api
) as Readonly<Swap.Api>

const baseUrl = baseUrls[network]

const headers = {
const transformers = transformersMaker(config)
const headers = freeze({
'Content-Type': 'application/json',
'Accept': 'application/json',
...(partnerId && {'X-Partner-Id': partnerId}),
}

const transformers = transformersMaker(config)
'X-Partner-Id': partnerId,
})

return freeze(
{
async tokens() {
const response = await request<TokensResponse>({
const response = await request<Readonly<DexHunterApi.TokensResponse>>({
method: 'get',
url: `${baseUrl}${apiPaths.tokens}`,
headers,
Expand All @@ -80,7 +62,7 @@ export const dexhunterApiMaker = (
},

async orders() {
const response = await request<OrdersResponse>({
const response = await request<Readonly<DexHunterApi.OrdersResponse>>({
method: 'get',
url: `${baseUrl}${apiPaths.orders({address})}`,
headers,
Expand All @@ -100,7 +82,7 @@ export const dexhunterApiMaker = (
)
},

async estimate(body: Swap.EstimateRequest) {
async estimate(body: Readonly<Swap.EstimateRequest>) {
const kind: 'estimate' | 'reverseEstimate' | 'limitEstimate' =
body.wantedPrice !== undefined
? 'limitEstimate'
Expand All @@ -109,7 +91,11 @@ export const dexhunterApiMaker = (
: 'estimate'

const response = await request<
EstimateResponse | ReverseEstimateResponse | LimitEstimateResponse
Readonly<
| DexHunterApi.EstimateResponse
| DexHunterApi.ReverseEstimateResponse
| DexHunterApi.LimitEstimateResponse
>
>({
method: 'post',
url: `${baseUrl}${apiPaths[kind]}`,
Expand All @@ -131,11 +117,13 @@ export const dexhunterApiMaker = (
)
},

async create(body: Swap.CreateRequest) {
async create(body: Readonly<Swap.CreateRequest>) {
const kind: 'build' | 'limitBuild' =
body.wantedPrice !== undefined ? 'limitBuild' : 'build'

const response = await request<BuildResponse | LimitBuildResponse>({
const response = await request<
Readonly<DexHunterApi.BuildResponse | DexHunterApi.LimitBuildResponse>
>({
method: 'post',
url: `${baseUrl}${apiPaths[kind]}`,
headers,
Expand All @@ -156,8 +144,8 @@ export const dexhunterApiMaker = (
)
},

async cancel(body: Swap.CancelRequest) {
const response = await request<CancelResponse>({
async cancel(body: Readonly<Swap.CancelRequest>) {
const response = await request<Readonly<DexHunterApi.CancelResponse>>({
method: 'post',
url: `${baseUrl}${apiPaths.cancel}`,
headers,
Expand All @@ -182,11 +170,11 @@ export const dexhunterApiMaker = (
)
}

const baseUrls = {
const baseUrls = freeze({
[Chain.Network.Mainnet]: 'https://api-us.dexhunterv3.app',
} as const
} as const)

const apiPaths = {
const apiPaths = freeze({
tokens: '/swap/tokens', // GET
cancel: '/swap/cancel', // POST
estimate: '/swap/estimate', // POST
Expand All @@ -210,4 +198,4 @@ const apiPaths = {
dcaEstimate: '/dca/estimate', // POST
dcaByAdress: ({address}: {address: string}) => `/dca/${address}`, // GET
markingSubmit: '/marking/submit', // POST
} as const
} as const)
223 changes: 223 additions & 0 deletions packages/swap/src/adapters/api/dexhunter/api.mocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
import {freeze} from 'immer'

export const DexHunterApiMocks = freeze(
{
swap: {
success: {
average_price: 1.25,
batcher_fee: 0.1,
cbor: 'some-cbor-data',
communications: ['msg1', 'msg2'],
deposits: 50,
dexhunter_fee: 0.05,
net_price: 1.2,
net_price_reverse: 0.8,
partner_code: 'partner123',
partner_fee: 0.02,
possible_routes: {route1: 60, route2: 40},
splits: [
{
amount_in: 600,
batcher_fee: 0.06,
deposits: 30,
dex: 'DEX1',
expected_output: 750,
expected_output_without_slippage: 740,
fee: 0.05,
final_price: 1.25,
initial_price: 1.2,
pool_fee: 0.01,
pool_id: 'pool123',
price_distortion: 0.02,
price_impact: 0.03,
},
],
total_fee: 0.23,
total_input: 1000,
total_input_without_slippage: 980,
total_output: 1200,
total_output_without_slippage: 1180,
},
},
sign: {
success: {
cbor: 'signed-tx-cbor',
strat_id: 'strategy-123',
},
},
limitEstimate: {
success: {
batcher_fee: 0.15,
blacklisted_dexes: ['DEX6'],
deposits: 100,
dexhunter_fee: 0.07,
net_price: 1.55,
partner: 'partnerXYZ',
partner_fee: 0.03,
possible_routes: {},
splits: [
{
amount_in: 700,
batcher_fee: 0.07,
deposits: 50,
dex: 'DEX6',
expected_output: 1050,
expected_output_without_slippage: 1040,
fee: 0.035,
final_price: 1.55,
initial_price: 1.5,
pool_fee: 0.005,
pool_id: 'poolXYZ',
price_distortion: 0.025,
price_impact: 0.035,
},
],
total_fee: 0.25,
total_input: 1400,
total_output: 2100,
},
},
limit: {
success: {
batcher_fee: 0.15,
cbor: 'some-cbor-data',
deposits: 100,
dexhunter_fee: 0.07,
partner: 'partnerXYZ',
partner_fee: 0.03,
possible_routes: {routeA: 'routeA', routeB: 'routeB'},
splits: [
{
amount_in: 700,
batcher_fee: 0.07,
deposits: 50,
dex: 'DEX6',
expected_output: 1050,
expected_output_without_slippage: 1040,
fee: 0.035,
final_price: 1.55,
initial_price: 1.5,
pool_fee: 0.005,
pool_id: 'poolXYZ',
price_distortion: 0.025,
price_impact: 0.035,
},
],
total_input: 1400,
total_output: 2100,
},
},
reverseEstimate: {
success: {
average_price: 1.25,
batcher_fee: 0.1,
communications: ['msg1', 'msg2'],
deposits: 50,
dexhunter_fee: 0.05,
net_price: 1.2,
net_price_reverse: 0.8,
partner_fee: 0.02,
possible_routes: {route1: 60, route2: 40},
price_ab: 1.3,
price_ba: 0.77,
splits: [
{
amount_in: 600,
batcher_fee: 0.06,
deposits: 30,
dex: 'DEX1',
expected_output: 750,
expected_output_without_slippage: 740,
fee: 0.05,
final_price: 1.25,
initial_price: 1.2,
pool_fee: 0.01,
pool_id: 'pool123',
price_distortion: 0.02,
price_impact: 0.03,
},
],
total_fee: 0.23,
total_input: 1000,
total_input_without_slippage: 980,
total_output: 1200,
},
},
estimate: {
success: {
average_price: 1.35,
batcher_fee: 0.12,
communications: ['info1', 'info2'],
deposits: 60,
dexhunter_fee: 0.06,
net_price: 1.3,
net_price_reverse: 0.75,
partner_code: 'partnerABC',
partner_fee: 0.025,
possible_routes: {routeX: 55, routeY: 45},
splits: [
{
amount_in: 650,
batcher_fee: 0.065,
deposits: 35,
dex: 'DEX5',
expected_output: 877.5,
expected_output_without_slippage: 870,
fee: 0.04,
final_price: 1.35,
initial_price: 1.3,
pool_fee: 0.007,
pool_id: 'poolABC',
price_distortion: 0.03,
price_impact: 0.04,
},
],
total_fee: 0.235,
total_output: 1755,
total_output_without_slippage: 1740,
},
},
cancel: {
success: {
additional_cancellation_fee: 0.05,
cbor: 'cancel-tx-cbor-data',
},
},
averagePrice: {
success: {
averagePrice: 1.25,
price_ab: 1.2,
price_ba: 0.83,
},
},
tokens: {
success: [
{
token_id:
'12345678901234567890123456789012345678901234567890123456tokenA',
token_decimals: 6,
token_policy: 'policy1',
token_ascii: 'Token Alpha',
ticker: 'ALPHA',
is_verified: true,
supply: 1000000,
creation_date: '2023-01-01',
price: 0.5,
},
{
token_id:
'12345678901234567890123456789012345678901234567890123456tokenB',
token_decimals: 8,
token_policy: 'policy2',
token_ascii: 'Token Beta',
ticker: 'BETA',
is_verified: false,
supply: 500000,
creation_date: '2023-02-15',
price: 1.2,
},
],
},
},
true,
)
Loading

0 comments on commit ba51818

Please sign in to comment.