Skip to content

Commit

Permalink
UIA register - automatic multi-step exchange and fix manual one
Browse files Browse the repository at this point in the history
  • Loading branch information
1aerostorm committed Apr 16, 2024
1 parent 92acbb1 commit 8901797
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 34 deletions.
131 changes: 98 additions & 33 deletions src/pages/api/reg/[...all].js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import tt from 'counterpart'

import nextConnect from '@/server/nextConnect';
import { throwErr, } from '@/server/error';
import { initGolos, } from '@/server/initGolos';
import { initGolos, exchangeNode, } from '@/server/initGolos';
import { getVersion, rateLimitReq, slowDownLimitReq, getRemoteIp,
noBodyParser, bodyParams, } from '@/server/misc';
import passport, { addModalRoutes, checkAlreadyUsed } from '@/server/passport';
Expand Down Expand Up @@ -510,7 +510,7 @@ let handler = nextConnect({ attachParams: true, })
}
})

.get('/api/reg/make_order_receipt/:amount/:submitting', async (req, res) => {
.get('/api/reg/make_order_receipt/:amount/:submitting/:use_new?', async (req, res) => {
await slowDownLimitReq(req)

const clientCfg = getClientCfg(req, req.params)
Expand All @@ -519,38 +519,100 @@ let handler = nextConnect({ attachParams: true, })

const amount = await Asset(req.params.amount.split('%20').join(' '))
let exAmount = amount.clone()
const exchanges = uias[exAmount.symbol]
let error = null
let result = null
let order_receipts = []
for (const ex of exchanges) {
const [ sym1, sym2 ] = ex.split('/')
let resEx

const { dex } = golos.libs
let resMul
const exNode = exchangeNode()
const mulParams = {
node: exNode,
amount: exAmount.toString(),
symbol: 'GOLOS',
direction: 'sell',
}
if (req.params.use_new !== 'false') {
try {
const { dex } = golos.libs
resEx = await dex.apidexExchange({sell: exAmount, buySym: sym2})
resMul = await dex.getExchange(mulParams)
} catch (err) {
console.error(err)
console.error('make_order_receipt getExchange (call 1):', err)
}
if (!resEx) {
error = tt('uia_register_jsx.cannot_check_orders') + exAmount.symbol + '/' + sym2
}

let error = null
let result = null
let order_receipts = []
let usedNode = false

if (resMul) {
const { best } = resMul
if (!best) {
error = tt('uia_register_jsx.no_orders') + exAmount.symbol + '/GOLOS' + tt('uia_register_jsx.cannot_register_with_it')
} else {
mulParams.remain = {
direct: 'ignore',
multi: 'ignore'
}
try {
resMul = await dex.getExchange(mulParams)
} catch (err) {
console.error('make_order_receipt getExchange (call 2):', err)
}
}
if (error) break
if (resEx.remain) {
error = tt('uia_register_jsx.too_low_orders') + exAmount.symbol + '/' + sym2 + tt('uia_register_jsx.cannot_register_with_it')
break
}

if (resMul) {
const { best } = resMul
if (!best) {
error = tt('uia_register_jsx.too_low_orders') + exAmount.symbol + '/GOLOS' + tt('uia_register_jsx.cannot_register_with_it')
} else {
let tx
try {
tx = await dex.makeExchangeTx(best.steps, { owner: 'null' })
if (tx) {
if (!tx.length) throw new Error('makeExchangeTx returned empty array')
for (const op of tx) {
const { amount_to_sell, min_to_receive } = op[1]
order_receipts.push([amount_to_sell, min_to_receive])
}
}
result = best.res
usedNode = true
} catch (err) {
console.error('make_order_receipt makeExchangeTx:', err)
}
}
if (resEx.error === 'no_orders') {
error = tt('uia_register_jsx.no_orders') + exAmount.symbol + '/' + sym2 + tt('uia_register_jsx.cannot_register_with_it')
break
} else if (resEx.error) {
error = resEx.error + tt('uia_register_jsx.unknown_error') + exAmount.symbol + '/' + sym2 + tt('uia_register_jsx.cannot_register_with_it')
break
}

if (!order_receipts.length) {
const exchanges = uias[exAmount.symbol]
for (const ex of exchanges) {
const [ sym1, sym2 ] = ex.split('/')
let resEx
try {
resEx = await dex.apidexExchange({sell: exAmount, buySym: sym2})
} catch (err) {
console.error(err)
}
if (!resEx) {
error = tt('uia_register_jsx.cannot_check_orders') + exAmount.symbol + '/' + sym2
}
if (error) break
if (resEx.remain) {
error = tt('uia_register_jsx.too_low_orders') + exAmount.symbol + '/' + sym2 + tt('uia_register_jsx.cannot_register_with_it')
break
}
if (resEx.error === 'no_orders') {
error = tt('uia_register_jsx.no_orders') + exAmount.symbol + '/' + sym2 + tt('uia_register_jsx.cannot_register_with_it')
break
} else if (resEx.error) {
error = resEx.error + tt('uia_register_jsx.unknown_error') + exAmount.symbol + '/' + sym2 + tt('uia_register_jsx.cannot_register_with_it')
break
}
const min_to_receive = exAmount.mul(resEx.limit_price)
const pair = [exAmount.toString(), min_to_receive]
order_receipts.push(pair)
exAmount = resEx.result.clone()
result = exAmount.clone()
}
const pair = [exAmount.toString(), resEx.result.toString()]
order_receipts.push(pair)
exAmount = resEx.result.clone()
result = exAmount.clone()
}

const deposited = req.session.deposited && req.session.deposited[amount.symbol]
Expand All @@ -562,20 +624,23 @@ let handler = nextConnect({ attachParams: true, })

await req.session.save()
} else {
order_receipts = []
if (req.params.submitting === 'true') {
console.error('make_order_receipt error:', req.session.deposited, amount.symbol)
error = 'wrong deposited'
order_receipts = []
}
}

if (req.params.submitting === 'true' && !order_receipts.length) {
console.error('make_order_receipt error:', req.session.deposited, amount.symbol)
error = 'wrong deposited'
if (error) {
result = null
}

res.json({
status: 'ok',
result: result && result.toString(),
error,
order_receipts
order_receipts,
get_exchange: usedNode && resMul
})
})

Expand Down
12 changes: 11 additions & 1 deletion src/server/initGolos.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ const golos = require('golos-lib-js')
const GolosDexApi = require('golos-dex-lib-js').default
const config = require('config');

function serverNode() {
return config.get('ws_connection_server') || 'https://api.golos.id'
}

function exchangeNode() {
return config.has('ws_connection_exchange') ?
config.get('ws_connection_exchange') : serverNode()
}

function initGolos() {
golos.config.set('websocket', config.get('ws_connection_server') || 'https://api.golos.id');
golos.config.set('websocket', serverNode())
const CHAIN_ID = config.get('chain_id');
if (CHAIN_ID) {
golos.config.set('chain_id', CHAIN_ID);
Expand All @@ -22,5 +31,6 @@ function initGolos() {
}

module.exports = {
exchangeNode,
initGolos,
};

0 comments on commit 8901797

Please sign in to comment.