Skip to content

Commit

Permalink
ConvertAssets - use apidex
Browse files Browse the repository at this point in the history
  • Loading branch information
1aerostorm committed Nov 22, 2023
1 parent 1958a78 commit d9a0c33
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 46 deletions.
61 changes: 15 additions & 46 deletions app/components/modules/ConvertAssets.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import MarketPair from 'app/components/elements/market/MarketPair'
import { normalizeAssets, DEFAULT_EXPIRE, generateOrderID,
calcFeeForSell, calcFeeForBuy } from 'app/utils/market/utils'
import transaction from 'app/redux/Transaction'
import { apidexExchange } from 'app/utils/ApidexApiClient'

class ConvertAssets extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -69,35 +70,21 @@ class ConvertAssets extends React.Component {
return myBalance
}

initOrders = async (sym1, sym2) => {
const now = Date.now()
if (!this.ordersUpdated || now - this.ordersUpdated > 3000) {
this.orders = await api.getOrderBookExtendedAsync(500, [sym1, sym2])
this.ordersUpdated = now
}
}

normalizeOrder = (order, sym1, sym2, prec1, prec2) => {
if (!order.asset1.symbol)
order.asset1 = Asset(order.asset1, prec1, sym1)
if (!order.asset2.symbol)
order.asset2 = Asset(order.asset2, prec1, sym2)
if (!order._price)
order._price = Price(order.order_price)
}

calculate = async (sellAmount, buyAmount, myBalance, isSell = true) => {
let res = isSell ? buyAmount.clone() : sellAmount.clone()
res.amount = 0
sellAmount =sellAmount.mul(10000)
let req = isSell ? sellAmount.clone() : buyAmount.clone()

await this.initOrders(sellAmount.symbol, buyAmount.symbol)

this.limitPrice = null
this.bestPrice = null

let orders = this.orders.bids
if (!orders.length) {
const result = await apidexExchange(req,
(isSell ? buyAmount.symbol : sellAmount.symbol),
isSell ? 'sell' : 'buy'
)

if (result.error) {
this.setState({
warning: tt('convert_assets_jsx.no_orders_DIRECTION', {
DIRECTION: sellAmount.symbol + '/' + buyAmount.symbol
Expand All @@ -113,26 +100,10 @@ class ConvertAssets extends React.Component {
return res
}

for (let i = 0; i < orders.length; ++i) {
const order = orders[i]
this.normalizeOrder(order, sellAmount.symbol, buyAmount.symbol, sellAmount.precision, buyAmount.precision)

const orderAmount = isSell ? order.asset1 : order.asset2

const amount = req.min(orderAmount)
let amount2 = amount.mul(order._price)
res = res.plus(amount2)

this.bestPrice = this.bestPrice || order._price.clone()
this.limitPrice = order._price.clone()

req = req.minus(orderAmount)

if (req.amount <= 0) {
req.amount = 0
break
}
}
this.bestPrice = result.best_price.clone()
this.limitPrice = result.limit_price.clone()
res = result.result.clone()
const remain = result.remain

if (res.amount == 0) {
res.amount = 1
Expand All @@ -141,11 +112,11 @@ class ConvertAssets extends React.Component {
res.amount = myBalance.amount
this.limitPrice = Price(req, res)
warning = tt('convert_assets_jsx.too_big_price')
} else if (req.amount > 0) {
} else if (remain) {
warning = {
a1: (isSell ? sellAmount : buyAmount).minus(req).floatString,
a1: (isSell ? sellAmount : buyAmount).minus(remain).floatString,
a2: res.floatString,
remain: req.floatString,
remain: remain.floatString,
isSell
}
if (!isSell) {
Expand Down Expand Up @@ -175,8 +146,6 @@ class ConvertAssets extends React.Component {

let sellAmount = myBalance.clone()

this.ordersUpdated = null

let fee = buyAmount.clone()
fee.amount = 0
let calcBuy = await this.calculate(sellAmount, buyAmount, myBalance)
Expand Down
26 changes: 26 additions & 0 deletions app/utils/ApidexApiClient.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fetchWithTimeout from 'shared/fetchWithTimeout'
import { Asset, Price } from 'golos-lib-js/lib/utils'

const request_base = {
method: 'get',
Expand Down Expand Up @@ -86,3 +87,28 @@ export async function apidexGetAll() {
return empty
}
}

export async function apidexExchange(sell, buySym, direction = 'sell') {
if (!apidexAvailable()) return null
let request = Object.assign({}, request_base)
try {
let resp = await fetchWithTimeout(apidexUrl(`/api/v1/exchange/` + sell.toString() + '/' + buySym + '/' + direction), 2000, request)
resp = await resp.json()
if (resp.result) {
resp.result = Asset(resp.result)
}
if (resp.best_price) {
resp.best_price = Price(resp.best_price)
}
if (resp.limit_price) {
resp.limit_price = Price(resp.limit_price)
}
if (resp.remain) {
resp.remain = Asset(resp.remain)
}
return resp
} catch (err) {
console.error('apidexExchange', err)
return null
}
}

0 comments on commit d9a0c33

Please sign in to comment.