From 92cbc23d291cadbaad5613abacf94250600aaf3a Mon Sep 17 00:00:00 2001 From: 1aerostorm Date: Tue, 9 Apr 2024 16:59:37 +0300 Subject: [PATCH] Multi-step register - golos-dex-lib-js, small fix --- package.json | 3 +- src/locales/en.json | 3 +- src/locales/ru-RU.json | 3 +- src/modules/register/UIARegister.jsx | 13 +++- src/pages/api/reg/[...all].js | 4 +- src/server/initGolos.js | 12 +++- src/utils/ApidexApiClient.js | 101 --------------------------- yarn.lock | 15 ++-- 8 files changed, 41 insertions(+), 113 deletions(-) delete mode 100644 src/utils/ApidexApiClient.js diff --git a/package.json b/package.json index 2ba8e4d..a1bc470 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ "formik": "^2.2.9", "git-rev-sync": "^3.0.1", "gmail-send": "^1.8.10", - "golos-lib-js": "^0.9.62", + "golos-dex-lib-js": "^1.0.2", + "golos-lib-js": "^0.9.68", "iron-session": "6.0.4", "jspdf": "^2.3.0", "lodash": "^4.17.11", diff --git a/src/locales/en.json b/src/locales/en.json index 7e2dd78..a738990 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -118,7 +118,8 @@ "cannot_register_with_it": ", cannot register.", "too_low_orders": "Too low orders in ", "unknown_error": " - error in ", - "too_low_amount_to_register": "Too low amount to register." + "too_low_amount_to_register": "Too low amount to register.", + "no_assets_available": "No UIA assets available now." }, "invites_jsx": { "claim_wrong_secret": "Wrong secret", diff --git a/src/locales/ru-RU.json b/src/locales/ru-RU.json index c549bae..ac7ee9d 100644 --- a/src/locales/ru-RU.json +++ b/src/locales/ru-RU.json @@ -118,7 +118,8 @@ "cannot_register_with_it": ", не можем зарегистрировать за такую сумму.", "too_low_orders": "Слишком мало ордеров по направлению ", "unknown_error": " - ошибка по направлению ", - "too_low_amount_to_register": "Слишком малая сумма для регистрации." + "too_low_amount_to_register": "Слишком малая сумма для регистрации.", + "no_assets_available": "Регистрация через криптовалюту сейчас не работает." }, "invites_jsx": { "claim_wrong_secret": "Неверно указан ключ", diff --git a/src/modules/register/UIARegister.jsx b/src/modules/register/UIARegister.jsx index c0beb34..414ad50 100644 --- a/src/modules/register/UIARegister.jsx +++ b/src/modules/register/UIARegister.jsx @@ -5,6 +5,7 @@ import cn from 'classnames' import golos, { api, } from 'golos-lib-js' import { Asset, AssetEditor } from 'golos-lib-js/lib/utils'; import { key_utils, PrivateKey } from 'golos-lib-js/lib/auth/ecc' +import GolosDexApi from 'golos-dex-lib-js' import Link from 'next/link' import { Formik, ErrorMessage, } from 'formik' @@ -13,7 +14,6 @@ import AmountField from '@/elements/forms/AmountField' import AccountName from '@/elements/register/AccountName' import VerifyWayTabs from '@/elements/register/VerifyWayTabs' import TransferWaiter from '@/modules/register/TransferWaiter' -import { apidexGetPrices } from '@/utils/ApidexApiClient' import KeyFile from '@/utils/KeyFile' import { delay, } from '@/utils/misc' import { emptyAuthority } from '@/utils/RecoveryUtils' @@ -69,6 +69,14 @@ class UIARegister extends React.Component { golos.config.set('chain_id', clientCfg.config.chain_id) const { apidex_service } = clientCfg.config + try { + new GolosDexApi(golos, { + host: apidex_service.host + }) + } catch (err) { + console.error('GolosDexApi init error:', err) + } + const { uias } = clientCfg.config.registrar const syms = uias.assets @@ -113,7 +121,7 @@ class UIARegister extends React.Component { let cmc try { - cmc = await apidexGetPrices(apidex_service, sym) + cmc = await golos.libs.dex.apidexGetPrices({ sym }) if (!cmc.price_usd) { console.error('Cannot obtain price_usd', cmc) throw Error('Cannot obtain price_usd') @@ -618,6 +626,7 @@ class UIARegister extends React.Component { {syms} {this.state.sym &&
} {form} + {!syms.length && !form ?
{tt('uia_register_jsx.no_assets_available')}
: null} } diff --git a/src/pages/api/reg/[...all].js b/src/pages/api/reg/[...all].js index 049d330..ddc5b7f 100644 --- a/src/pages/api/reg/[...all].js +++ b/src/pages/api/reg/[...all].js @@ -16,7 +16,6 @@ import { getDailyLimit, obtainUid, getClientCfg, } from '@/server/reg'; import { regSessionMiddleware, } from '@/server/regSession'; import Tarantool from '@/server/tarantool'; import { delay, } from '@/utils/misc' -import { apidexExchange } from '@/utils/ApidexApiClient' initGolos(); @@ -526,7 +525,8 @@ let handler = nextConnect({ attachParams: true, }) const [ sym1, sym2 ] = ex.split('/') let resEx try { - resEx = await apidexExchange(apidex_service, exAmount, sym2) + const { dex } = golos.libs + resEx = await dex.apidexExchange({sell: exAmount, buySym: sym2}) } catch (err) { console.error(err) } diff --git a/src/server/initGolos.js b/src/server/initGolos.js index e1cb334..425baff 100644 --- a/src/server/initGolos.js +++ b/src/server/initGolos.js @@ -1,4 +1,5 @@ -const golos = require('golos-lib-js'); +const golos = require('golos-lib-js') +const GolosDexApi = require('golos-dex-lib-js').default const config = require('config'); function initGolos() { @@ -9,6 +10,15 @@ function initGolos() { } golos.config.set('broadcast_transaction_with_callback', true); + + const apidex_service = config.get('apidex_service') + try { + new GolosDexApi(golos, { + host: apidex_service.host + }) + } catch (err) { + console.error('GolosDexApi init error:', err) + } } module.exports = { diff --git a/src/utils/ApidexApiClient.js b/src/utils/ApidexApiClient.js deleted file mode 100644 index f8bedc3..0000000 --- a/src/utils/ApidexApiClient.js +++ /dev/null @@ -1,101 +0,0 @@ -import { fetchEx, Asset } from 'golos-lib-js/lib/utils' - -const request_base = { - timeout: 2000, - method: 'get', - headers: { - Accept: 'application/json', - 'Content-Type': 'application/json' - } -} - -const pageBaseURL = 'https://coinmarketcap.com/currencies/' - -const getPageURL = (slug) => { - return new URL(slug + '/', pageBaseURL).toString() -} - -export const apidexUrl = (apidex_service, pathname) => { - try { - return new URL(pathname, apidex_service.host).toString(); - } catch (err) { - console.error('apidexUrl', err) - return '' - } -} - -let cached = {} - -export async function apidexGetPrices(apidex_service, sym) { - const empty = { - price_usd: null, - price_rub: null, - page_url: null - } - if (!apidex_service || !apidex_service.host) return empty - let request = Object.assign({}, request_base) - try { - const now = new Date() - const cache = cached[sym] - if (cache && (now - cache.time) < 60000) { - return cache.resp - } else { - let resp = await fetchEx(apidexUrl(apidex_service, `/api/v1/cmc/${sym}`), request) - resp = await resp.json() - if (resp.data && resp.data.slug) - resp['page_url'] = getPageURL(resp.data.slug) - else - resp['page_url'] = null - cached[sym] = { - resp, time: now - } - return resp - } - } catch (err) { - console.error('apidexGetPrices', err) - return empty - } -} - -let cachedAll = {} - -export async function apidexGetAll(apidex_service) { - const empty = { - data: {} - } - if (!apidex_service || !apidex_service.host) return empty - let request = Object.assign({}, request_base) - try { - const now = new Date() - if (cachedAll && (now - cachedAll.time) < 60000) { - return cachedAll.resp - } else { - let resp = await fetchEx(apidexUrl(apidex_service, `/api/v1/cmc`), request) - resp = await resp.json() - cachedAll = { - resp, time: now - } - return resp - } - } catch (err) { - console.error('apidexGetAll', err) - return empty - } -} - -export async function apidexExchange(apidex_service, sell, buySym) { - if (!apidex_service || !apidex_service.host) return empty - let request = Object.assign({}, request_base) - try { - let resp = await fetchEx(apidexUrl(apidex_service, `/api/v1/exchange/` + sell.toString() + '/' + buySym), request) - resp = await resp.json() - return { - result: resp.result ? await Asset(resp.result) : null, - remain: resp.remain ? await Asset(resp.remain) : null, - error: resp.error - } - } catch (err) { - console.error('apidexExchange', err) - return null - } -} diff --git a/yarn.lock b/yarn.lock index 0c8feb7..6e8c6ed 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2968,10 +2968,17 @@ gmail-send@^1.8.10: lodash "^4.17.15" nodemailer "^6.3.0" -golos-lib-js@^0.9.62: - version "0.9.62" - resolved "https://registry.yarnpkg.com/golos-lib-js/-/golos-lib-js-0.9.62.tgz#d3c48db7cb808b59158f2c878ff59936c24d88e3" - integrity sha512-npQBa353iju1vsL6z8u8ZnluzRhavCElG23OFMFHAyapLuy/1TyYU10aCAyrRc+bJro7QR91tKedmPLgFPBa8Q== +golos-dex-lib-js@^1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/golos-dex-lib-js/-/golos-dex-lib-js-1.0.2.tgz#e0fe5d29781da8d0830ccb6bcfa5386bc73a2c5d" + integrity sha512-wrTzbMdoXlarjFu15liuTdcZjHlPXbdGyMouvCVlMKVqcldUbvi5SMR5egtLBjMKZfmFnFxbFrv798ukGqqSUg== + dependencies: + golos-lib-js "^0.9.64" + +golos-lib-js@^0.9.64, golos-lib-js@^0.9.68: + version "0.9.68" + resolved "https://registry.yarnpkg.com/golos-lib-js/-/golos-lib-js-0.9.68.tgz#62f3c4a7755eacfbe6dc2f6ebc3111f2ff74e6dd" + integrity sha512-KoQMFHtINYdM0hhe9dKPpWXZLo3MkFXyRxY/zQq0kbBjAwJBU4P1WEMwmPS+MPujjizfvU6nerUOLI4Lj7ChFw== dependencies: abort-controller "^3.0.0" assert "^2.0.0"