Skip to content

Commit

Permalink
Fix register
Browse files Browse the repository at this point in the history
  • Loading branch information
1aerostorm committed Nov 3, 2023
1 parent 0a1bdf2 commit e1f723b
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 29 deletions.
49 changes: 34 additions & 15 deletions db/reg_pollers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function reg_pollers_bootstrap()
{name = 'sym', type = 'STR'},
{name = 'amount', type = 'number'},
{name = 'uid', type = 'STR'},
{name = 'created', type = 'number'},
{name = 'created', type = 'unsigned'},
{name = 'init_balance', type = 'number'},
}
})
Expand All @@ -21,44 +21,63 @@ function reg_pollers_bootstrap()
'amount'
}, unique = true
})
reg_pollers:create_index('by_created', {
type = 'tree', parts = {
'created'
}, unique = false
})
end

local function wrap_rp(rp)
return {
id = rp[1],
sym = rp[2],
amount = rp[3],
uid = rp[4],
created = rp[5],
init_bal = rp[6],
}
end

function get_free_reg_poller(amount, sym)
local rps = nil
repeat
if rps ~= nil then
local now = fiber.clock64()
local rp = wrap_rp(rps[1])
if (now - rp['created']) >= 20*60*1000000 then
box.space.reg_pollers:delete(rp['id'])
break
end
amount = amount + 1
end
rps = box.space.reg_pollers.index.by_amount:select{sym, amount}
until #rps == 0
return amount
end

local function wrap_rp(rp)
return {
id = rp[1],
sym = rp[2],
amount = rp[3],
uid = rp[4],
created = rp[5],
init_bal = rp[6],
}
local function clean_reg_pollers(now)
for i,rp in box.space.reg_pollers.index.by_created:pairs(0, {iterator = 'GT', limit = 100}) do
local rp = wrap_rp(rp)
if (now - rp['created']) > 20*1000000 then
box.space.reg_pollers:delete(rp['id'])
else
break
end
end
end

function upsert_reg_poller(amount, sym, uid, init_bal)
local now = fiber.clock64()
clean_reg_pollers(now)
local rps = box.space.reg_pollers.index.by_amount:select{sym, amount}
if #rps ~= 0 then
local rp = rps[1]
rp = wrap_rp(rp)
if rp['uid'] ~= uid then
return { err = 'Someone already waits for such transfer.', res = nil }
end
if (now - rp['created']) >= 15*60*1000 then
box.space.reg_pollers:delete(rp['id'])
else
return { err = nil, res = rp }
end
return { err = nil, res = rp }
end
local rp = box.space.reg_pollers:insert{nil, sym, amount, uid, now, init_bal}
return { err = nil, res = wrap_rp(rp) }
Expand Down
3 changes: 2 additions & 1 deletion src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@
"memo_fixed": "Memo",
"to": "Send tokens to address/account",
"api_error": "Cannot get address. Try again later. If problem still occurs, contact the issuer of ",
"api_error_details": "and send the error details:"
"api_error_details": "and send the error details:",
"free_poller": "Cannot calculate minimal amount. Try again later, please. Or try another currency."
},
"invites_jsx": {
"claim_wrong_secret": "Wrong secret",
Expand Down
3 changes: 2 additions & 1 deletion src/locales/ru-RU.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@
"memo_fixed": "Заметка/memo",
"to": "Отправьте токены на адрес/аккаунт",
"api_error": "Не удается получить адрес. Попробуйте позднее. Если проблема сохраняется, свяжитесь с эмитентом ",
"api_error_details": "и сообщите подробности ошибки:"
"api_error_details": "и сообщите подробности ошибки:",
"free_poller": "Не удается рассчитать минимальную сумму. Попробуйте позже, или другую валюту."
},
"invites_jsx": {
"claim_wrong_secret": "Неверно указан ключ",
Expand Down
5 changes: 3 additions & 2 deletions src/modules/register/TransferWaiter.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import tt from 'counterpart'
import { Asset } from 'golos-lib-js/lib/utils';

import LoadingIndicator from '@/elements/LoadingIndicator'
import { delay, } from '@/utils/misc'
import { callApi, } from '@/utils/RegApiClient'

class TransferWaiter extends React.Component {
Expand All @@ -15,7 +16,7 @@ class TransferWaiter extends React.Component {

poll = async (minAmount) => {
const retry = async () => {
await new Promise(resolve => setTimeout(resolve, 1000))
await delay(1000)
if (!this.state.stopped)
this.poll(minAmount)
}
Expand All @@ -37,7 +38,7 @@ class TransferWaiter extends React.Component {

start = async () => {
this.setState({
seconds: 30*60,
seconds: 15*60,
stopped: false
})

Expand Down
26 changes: 24 additions & 2 deletions src/modules/register/UIARegister.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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'
import { callApi, } from '@/utils/RegApiClient'
import { withRouterHelpers, } from '@/utils/routing'
Expand Down Expand Up @@ -91,7 +92,11 @@ class UIARegister extends React.Component {
let registrar = await golos.api.getAccounts([accName])
registrar = registrar[0]

const { to_type, to_api, } = deposit
const { to_type, to_api, fee, } = deposit
if (fee && (isNaN(fee) || parseFloat(fee) !== 0)) {
error = sym + tt('uia_register_jsx.transfer_not_supported')
break
}
if (to_type === 'transfer') {
error = sym + tt('uia_register_jsx.transfer_not_supported')
break
Expand Down Expand Up @@ -128,6 +133,23 @@ class UIARegister extends React.Component {
}
}

for (let i = 0; i < 3; ++i) {
try {
let fp = await callApi('/api/reg/get_free_poller/' + minAmount.toString())
fp = await fp.json()
if (fp.error) {
throw new Error(fp.error)
}
minAmount = Asset(fp.amount)
error = null
break
} catch (err) {
console.error('get_free_poller', err)
error = tt('uia_register_jsx.free_poller')
}
await delay(2000)
}

this.setState({
rules: { ...deposit, creator: asset.creator, telegram },
minAmount,
Expand Down Expand Up @@ -195,7 +217,7 @@ class UIARegister extends React.Component {
|| res.error === 'cannot_connect_gateway')) {
console.error('Repeating /uia_address', res)
++retried
await new Promise(resolve => setTimeout(resolve, 1100))
await delay(1100)
await retryReq()
return
}
Expand Down
88 changes: 81 additions & 7 deletions src/pages/api/reg/[...all].js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import config from 'config';
import gmailSend from 'gmail-send';
import golos, { api } from 'golos-lib-js'
import golos, { api, broadcast } from 'golos-lib-js'
import { hash, } from 'golos-lib-js/lib/auth/ecc';
import { Asset } from 'golos-lib-js/lib/utils'
import secureRandom from 'secure-random';

import nextConnect from '@/server/nextConnect';
import { throwErr, } from '@/server/error';
import { initGolos, } from '@/server/initGolos';
Expand All @@ -13,6 +14,7 @@ import passport, { addModalRoutes, checkAlreadyUsed } from '@/server/passport';
import { getDailyLimit, obtainUid, getClientCfg, } from '@/server/reg';
import { regSessionMiddleware, } from '@/server/regSession';
import Tarantool from '@/server/tarantool';
import { delay, } from '@/utils/misc'

initGolos();

Expand Down Expand Up @@ -306,6 +308,8 @@ let handler = nextConnect({ attachParams: true, })
})

.get('/api/reg/get_free_poller/:amount', async (req, res) => {
rateLimitReq(req)

const amountStr = req.params.amount.split('%20').join(' ')
let amount
try {
Expand All @@ -331,7 +335,7 @@ let handler = nextConnect({ attachParams: true, })
}

if (freeAmount) {
const ret = Asset(0, amount.precision, amount.symbol)
const ret = await Asset(0, amount.precision, amount.symbol)
ret.amountFloat = freeAmount.toString()
res.json({
status: 'ok',
Expand All @@ -347,10 +351,12 @@ let handler = nextConnect({ attachParams: true, })
})

.get('/api/reg/wait_for_transfer/:amount', async (req, res) => {
rateLimitReq(req)

const amountStr = req.params.amount.split('%20').join(' ')
let amount
try {
amount = Asset(amountStr)
amount = await Asset(amountStr)
} catch (err) {
res.json({
status: 'err',
Expand All @@ -373,11 +379,11 @@ let handler = nextConnect({ attachParams: true, })
let bal = balances[0][amount.symbol]
if (bal) {
bal = bal.balance
return Asset(bal)
return await Asset(bal)
} else {
const assets = await api.getAssetsAsync('', [amount.symbol])
if (!assets[0]) throwErr(req, 400, ['No such asset'])
bal = Asset(assets[0].supply)
bal = await Asset(assets[0].supply)
bal.amount = 0
return bal
}
Expand All @@ -400,6 +406,7 @@ let handler = nextConnect({ attachParams: true, })
try {
initBal = await getBalance()
} catch (err) {
console.error('wait_for_transfer getBalance', initBal)
throwErr(req, 400, ['Blockchain unavailable'])
}

Expand Down Expand Up @@ -438,7 +445,7 @@ let handler = nextConnect({ attachParams: true, })
}

console.log('wait_for_transfer', initBal.toString(), bal.toString())
const delta = bal.amount.minus(initBal.amount)
const delta = bal.minus(initBal.amount)
if (delta.gte(amount)) {
let stopMe = false

Expand Down Expand Up @@ -480,10 +487,77 @@ let handler = nextConnect({ attachParams: true, })
return
}

await new Promise(resolve => setTimeout(resolve, pollMsec))
await delay(pollMsec)
}
})

.post('/api/reg/place_order/:amount', async (req, res) => {
rateLimitReq(req)

const amountStr = req.params.amount.split('%20').join(' ')
let amount
try {
amount = await Asset(amountStr)
} catch (err) {
res.json({
status: 'err',
error: 'Asset parse error'
})
return
}

if (!req.session.deposited) {
throwErr(req, 400, ['You have no deposited'])
}
const deposited = req.session.deposited[amount.symbol]
if (!deposited) {
throwErr(req, 400, ['You have no deposited in ' + amount.symbol])
}
if (deposited !== amountStr) {
throwErr(req, 400, ['You have wrong deposited in ' + amount.symbol])
}

let chainProps
for (let i = 0; i < 3; ++i) {
try {
chainProps = await api.getChainPropertiesAsync()
break
} catch (err) {
console.error('/api/reg/place_order - getChainPropertiesAsync', err)
await delay(3000)
}
}
if (!chainProps) {
throwErr(req, 503, ['/api/reg/place_order - Blockchain node unavailable - cannot getChainPropertiesAsync'])
}
if (!chainProps.chain_status) {
throwErr(req, 503, ['/api/reg/place_order - Blockchain node is stopped - chain_status is false'])
}

const signingKey = config.get('registrar.signing_key')
const orderid = Math.floor(Date.now() / 1000)
let operations = [['limit_order_create', {
owner: config.get('registrar.account'),
orderid,
amount_to_sell: amountStr,
min_to_receive: chainProps.create_account_min_golos_fee,
fill_or_kill: true,
expiration: null
}]]
try {
await broadcast.sendAsync({
extensions: [],
operations
}, [signingKey])

delete req.session.deposited[amount.symbol]
await req.session.save()
} catch (err) {
console.error('/api/reg/place_order - Cannot sell tokens')
throwErr(req, 400, ['Cannot sell tokens'])
}
}

handler = addModalRoutes(handler);

export default handler;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/sign/delegate_vs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import LoginForm from '@/modules/LoginForm';
import { getOAuthCfg, getChainData, } from '@/server/oauth';
import { getOAuthSession, } from '@/server/oauthSession';
import { withSecureHeadersSSR, } from '@/server/security';
import { steemToVests, } from '@/utils/misc'
import { callApi, } from '@/utils/OAuthClient';
import { steemToVests, } from '@/utils/State';
import validate_account_name from '@/utils/validate_account_name';

function calcMaxInterest(cprops) {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/State.js → src/utils/misc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export async function delay(msec) {
await new Promise(resolve => setTimeout(resolve, msec))
}

export const toAsset = (value) => {
const [ amount, symbol ] = value.split(' ')
return { amount: parseFloat(amount), symbol }
Expand Down

0 comments on commit e1f723b

Please sign in to comment.