Skip to content

Commit

Permalink
UIA register
Browse files Browse the repository at this point in the history
  • Loading branch information
1aerostorm committed Nov 1, 2023
1 parent 4684f47 commit 06a6dd7
Show file tree
Hide file tree
Showing 11 changed files with 348 additions and 149 deletions.
7 changes: 5 additions & 2 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"signing_key": "5K67PNheLkmxkgJ5UjvR8Nyt3GVPoLEN1dMZjFuNETzrNyMecPG",
"delegation": "75000.000000 GESTS",
"free_regs_per_day": 10,
"uias": ["YMUSDT", "YMPZM", "YMHIVE"]
"uias": ["YMUSDT", "YMPZM", "YMHIVE", "TESTF"]
},
"server_session_secret": "exiKdyF+IwRIXJDmtGIl4vWUz4i3eVSISpfZoeYc0s4=",
"session_cookie_key": "X-Reg-ISession",
Expand All @@ -39,7 +39,7 @@
"styleSrc": "'self' 'unsafe-inline' fonts.googleapis.com",
"imgSrc": "* data:",
"fontSrc": "data: fonts.gstatic.com",
"connectSrc": "'self' apibeta.golos.today wss://apibeta.golos.today",
"connectSrc": "'self' apibeta.golos.today wss://apibeta.golos.today *.golos.app",
"frameAncestors": "'none'"
}
},
Expand Down Expand Up @@ -76,6 +76,9 @@
"bot_token": "6390290192:AAEGipeCa4P0V4HZzoXvITCij8on6OItsGM"
}
},
"apidex_service": {
"host": "https://api-dex.golos.app"
},
"default_client": "blogs",
"clients": {
"blogs": {
Expand Down
2 changes: 2 additions & 0 deletions db/app.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require 'guid'
require 'oauth'
require 'server_tokens'
require 'cryptostore'
require 'reg_pollers'

io.output():setvbuf('no')

Expand Down Expand Up @@ -91,4 +92,5 @@ box.once('bootstrap', function()
oauth_bootstrap()
server_tokens_bootstrap()
cryptostore_bootstrap()
reg_pollers_bootstrap()
end)
63 changes: 63 additions & 0 deletions db/reg_pollers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
fiber = require 'fiber'

function reg_pollers_bootstrap()
box.schema.sequence.create('reg_pollers')
reg_pollers = box.schema.create_space('reg_pollers', {
format = {
{name = 'id', type = 'unsigned'},
{name = 'sym', type = 'STR'},
{name = 'amount', type = 'number'},
{name = 'uid', type = 'STR'},
{name = 'created', type = 'unsigned'},
{name = 'init_balance', type = 'number'},
}
})
reg_pollers:create_index('primary', {
sequence='reg_pollers'
})
reg_pollers:create_index('by_amount', {
type = 'tree', parts = {
'sym',
'amount'
}, unique = true
})
end

function get_free_reg_poller(amount, sym)
local rps = nil
repeat
if rps ~= nil then
amount = amount + 1
end
rps = box.space.reg_pollers.index.by_amount:select{sym, amount}
until #rps == 0
return amount
end

function upsert_reg_poller(amount, sym, uid, init_bal)
local now = fiber.clock()
local rps = box.space.reg_pollers.index.by_amount:select{sym, amount}
if #rps ~= 0 then
local rp = rps[1]
if rp['uid'] ~= uid then
return { err = 'Someone already waits for such transfer.', res = nil }
end
if (now - rp['created']) >= 15*60 then
box.space.reg_pollers:delete(rp['id'])
else
return { err = nil, res = rp }
end
end
local rp = box.space.reg_pollers:insert(nil, sym, amount, uid, now, init_bal)
return { err = nil, res = rp }
end

function delete_reg_poller(amount, sym)
local rps = box.space.reg_pollers.index.by_amount:select{sym, amount}
if #rps ~= 0 then
local rp = rps[1]
box.space.reg_pollers:delete(rp['id'])
return true
end
return false
end
3 changes: 3 additions & 0 deletions public/icons/copy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions public/icons/copy_ok.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
"register_with": "Register with...",
"no_such_asset": " - no such UIA.",
"deposit_unavailable": " - deposit temporarily unavailable.",
"cmc_error": " - cannot get min amount from CoinMarketCap.",
"transfer_not_supported": " - not yet supported.",
"min_amount": "Min amount",
"fee": "Fee",
"memo_fixed": "Memo",
Expand Down
2 changes: 2 additions & 0 deletions src/locales/ru-RU.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
"register_with": "Регистрация с помощью...",
"no_such_asset": " - такого UIA не существует.",
"deposit_unavailable": " - депозит временно недоступен.",
"cmc_error": " - не удается получить минимальную сумму с CoinMarketCap.",
"transfer_not_supported": " - пока не поддерживается.",
"min_amount": "Минимальная сумма",
"fee": "Комиссия",
"memo_fixed": "Заметка/memo",
Expand Down
165 changes: 36 additions & 129 deletions src/modules/register/UIARegister.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import CopyToClipboard from 'react-copy-to-clipboard'
import tt from 'counterpart'
import cn from 'classnames'
import golos, { api, broadcast } from 'golos-lib-js'
import golos, { api, } from 'golos-lib-js'
import { Asset } from 'golos-lib-js/lib/utils';
import { key_utils, PrivateKey } from 'golos-lib-js/lib/auth/ecc'
import Link from 'next/link'
Expand All @@ -11,6 +11,7 @@ import LoadingIndicator from '@/elements/LoadingIndicator'
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 { emptyAuthority } from '@/utils/RecoveryUtils'
import { callApi, } from '@/utils/RegApiClient'
Expand Down Expand Up @@ -39,14 +40,6 @@ function getAssetMeta(asset) {
return res
}

const TransferState = {
initial: 0,
transferring: 1,
waiting: 2,
received: 3,
timeouted: 4,
};

class APIError extends Error {
constructor(errReason, errData) {
super('API Error')
Expand All @@ -68,6 +61,7 @@ class UIARegister extends React.Component {
if (clientCfg.config.chain_id)
golos.config.set('chain_id', clientCfg.config.chain_id)

const { apidex_service } = clientCfg.config
const { uias } = clientCfg.config.registrar

const path = this.getPath()
Expand All @@ -83,7 +77,8 @@ class UIARegister extends React.Component {
error = sym + tt('uia_register_jsx.no_such_asset')
} else {
for (const asset of assets) {
const symbol = Asset(asset.supply).symbol
const supply = Asset(asset.supply)
const symbol = supply.symbol
if (sym === symbol) {
const meta = getAssetMeta(asset)
const { deposit, telegram } = meta
Expand All @@ -98,21 +93,35 @@ class UIARegister extends React.Component {

const { to_type, to_api, } = deposit
if (to_type === 'transfer') {
/*clearOldAddresses();
const addr = loadAddress(sym, asset.creator);
if (addr) {
this.setState({
transferState: TransferState.received,
receivedTransfer: {
memo: addr,
},
});
}*/
error = sym + tt('uia_register_jsx.transfer_not_supported')
break
}

let minAmount = Asset(0, supply.precision, supply.symbol)

let cmc
try {
cmc = await apidexGetPrices(apidex_service, sym)
if (!cmc.price_usd) {
console.error('Cannot obtain price_usd', cmc)
throw Error('Cannot obtain price_usd')
}
const priceUsd = parseFloat(cmc.price_usd)
let minFloat = 1 / priceUsd
if (minFloat >= 0.99 && minFloat < 1) {
minFloat = 1
}

minAmount.amountFloat = minFloat.toString()
} catch (err) {
console.error(err)
error = sym + tt('uia_register_jsx.cmc_error')
break
}

this.setState({
transferState: TransferState.initial,
rules: { ...deposit, creator: asset.creator, telegram },
minAmount,
registrar,
sym,
copied_addr: false,
Expand Down Expand Up @@ -222,55 +231,6 @@ class UIARegister extends React.Component {
return Asset(this.balanceValue()).gte(Asset('0.001 GOLOS'));
}

transfer = async () => {
this.setState({
transferState: TransferState.transferring,
}, () => {
this.transferAndWait()
})
}

waitingTimeout = (10 + 1) * 60 * 1000

transferAndWait = async () => {
const { sym, rules, registrar } = this.state
const { to_transfer, memo_transfer, } = rules
let stopper
let stopStream = api.streamOperations((err, op) => {
if (op[0] === 'transfer' && op[1].from === to_transfer
&& op[1].to === registrar.name) {
stopStream();
clearTimeout(stopper);
saveAddress(sym, rules.creator, op[1].memo);
this.setState({
transferState: TransferState.received,
receivedTransfer: op[1],
});
}
})

try {
const res = await broadcast.transferAsync(registrar.name, to_transfer, '0.001 GOLOS', memo_transfer)
} catch (err) {
console.error(err)
this.setState({
transferState: TransferState.initial,
})
stopStream()
return
}

this.setState({
transferState: TransferState.waiting,
});
stopper = setTimeout(() => {
if (stopStream) stopStream();
this.setState({
transferState: TransferState.timeouted,
})
}, this.waitingTimeout)
}

_renderTo = (to, to_fixed, username) => {
let addr = to || to_fixed;
if (username)
Expand Down Expand Up @@ -298,13 +258,16 @@ class UIARegister extends React.Component {
if (memo_fixed) {
details = details.split('<account>').join(username)
}
let minAmount = parseFloat(min_amount)
const minAmountForUsd = parseFloat(this.state.minAmount.floatString)
minAmount = Math.max(minAmount, minAmountForUsd)
return <div style={{fontSize: "90%"}}>
<hr />
{details && <div style={{ whiteSpace: 'pre-line', }}>
{details}
<br/><br/></div>}
{min_amount && <div>
{tt('uia_register_jsx.min_amount')} <b>{min_amount} {sym || ''}</b></div>}
{minAmount && <div>
{tt('uia_register_jsx.min_amount')} <b>{minAmount} {sym || ''}</b></div>}
{fee && <div>
{tt('uia_register_jsx.fee') + ': '}<b>{fee} {sym || ''}</b></div>}
</div>;
Expand Down Expand Up @@ -351,60 +314,6 @@ class UIARegister extends React.Component {
</div>)
}

_renderTransfer = () => {
const { rules, sym, transferState, receivedTransfer, } = this.state
const { to_transfer, memo_transfer, } = rules

const transferring = transferState === TransferState.transferring

const enough = this.enoughBalance()

if (transferState === TransferState.received) {
const { registrar, } = this.state
const { memo, } = receivedTransfer;
return (<div>
{this._renderTo(receivedTransfer.memo, null, registrar.name)}
{this._renderParams(false)}
</div>);
}

if (transferState === TransferState.timeouted) {
return (<div>
{tt('asset_edit_deposit_jsx.timeouted')}
{sym || ''}
.
</div>);
}

if (transferState === TransferState.waiting) {
return (<div>
{tt('asset_edit_deposit_jsx.waiting')}
<br />
<br />
<center>
<LoadingIndicator type='circle' size='70px' />
</center>
<br />
</div>);
}

return (<div>
{tt('uia_register_jsx.transfer_desc')}
<b>{to_transfer || ''}</b>
{tt('uia_register_jsx.transfer_desc_2')}
<b>{memo_transfer || ''}</b>
{transferring ?
<span><LoadingIndicator type='circle' /></span> : null}
<button type='submit' disabled={!enough || transferring} className='button float-center' onClick={this.transfer}>
{tt('g.submit')}
</button>
{!enough ? <div className='error'>
{tt('transfer_jsx.insufficient_funds')}
</div> : null}
{this._renderParams()}
</div>);
}

_renderWaiter = () => {
const { sym, registrar, onTransfer } = this.state
if (!onTransfer) {
Expand Down Expand Up @@ -465,12 +374,10 @@ class UIARegister extends React.Component {
</div>
} else {
const { rules, registrar, } = this.state
const { to, to_type, to_fixed, to_transfer,
const { to, to_type, to_fixed,
min_amount, fee, details, } = rules
if (to_type === 'api') {
form = this._renderApi()
} else if (to_type === 'transfer') {
form = this._renderTransfer()
} else {
let memo_fixed = rules.memo_fixed
if (memo_fixed) {
Expand Down
Loading

0 comments on commit 06a6dd7

Please sign in to comment.