Skip to content

Commit

Permalink
Multi-step exchange - fixes (looks like all, but not tested)
Browse files Browse the repository at this point in the history
  • Loading branch information
1aerostorm committed May 27, 2024
1 parent ae1191b commit ea450ab
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 33 deletions.
37 changes: 21 additions & 16 deletions app/components/modules/ConvertAssets.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ class ConvertAssets extends React.Component {
let res = await getExchange(sellAmount, buyAmount, myBalance,
(data) => {
fee = data.fee
const { warning, altBanner, mainBanner, newSelected, bestType, bestPrice, limitPrice } = data
const { warning, altBanner, mainBanner, newSelected, bestType, bestPrice, limitPrice, chain, isSell } = data
this.setState({
warning,
altBanner,
mainBanner,
bestType,
exType: newSelected || this.state.exType
exType: newSelected || this.state.exType,
chain, isSell
})
this.bestPrice = bestPrice
this.limitPrice = limitPrice
Expand All @@ -130,9 +131,9 @@ class ConvertAssets extends React.Component {
let res = await getExchange(sellAmount.asset, buyAmount.asset, myBalance,
(data) => {
fee = data.fee
const { warning, altBanner, mainBanner, newSelected, bestType, bestPrice, limitPrice } = data
const { warning, altBanner, mainBanner, newSelected, bestType, bestPrice, limitPrice, chain, isSell } = data
this.setState({ warning, altBanner, mainBanner, bestType,
exType: newSelected || this.state.exType })
exType: newSelected || this.state.exType, chain, isSell })
this.bestPrice = bestPrice
this.limitPrice = limitPrice
}, exType)
Expand Down Expand Up @@ -191,9 +192,9 @@ class ConvertAssets extends React.Component {
let res = await getExchange(sellAmount.asset, newAmount.asset, myBalance,
(data) => {
fee = data.fee
const { warning, altBanner, mainBanner, newSelected, bestType, bestPrice, limitPrice } = data
const { warning, altBanner, mainBanner, newSelected, bestType, bestPrice, limitPrice, chain, isSell } = data
this.setState({ warning, altBanner, mainBanner, bestType,
exType: newSelected || this.state.exType })
exType: newSelected || this.state.exType, chain, isSell })
this.bestPrice = bestPrice
this.limitPrice = limitPrice
}, exType, false)
Expand Down Expand Up @@ -221,13 +222,16 @@ class ConvertAssets extends React.Component {
let confirm
if (this.limitPrice) {
minToReceive = sellAmount.mul(this.limitPrice)
if (minToReceive.eq(0) && buyAmount.ne(0)) {
minToReceive = buyAmount.clone()
}

let possible = sellAmount.mul(this.bestPrice)
if (possible.minus(possible.mul(3000).div(10000)).gt(buyAmount)) {
confirm = tt('convert_assets_jsx.price_warning')
}
} else {
minToReceive = this.state.buyAmount.asset
minToReceive = buyAmount.clone()
}

this.setState({ loading: true })
Expand Down Expand Up @@ -439,13 +443,13 @@ class ConvertAssets extends React.Component {
}

_renderWarning() {
const { warning } = this.state
const { warning, isSell } = this.state
if (!warning) {
return null
}
let children = warning
if (warning.a1) {
const { a1, a2, remain, isSell } = warning
const { a1, a2, remain } = warning
children = [
<span>{tt('convert_assets_jsx.too_much_amount1')}</span>,
<b>{a1}</b>,
Expand All @@ -464,10 +468,11 @@ class ConvertAssets extends React.Component {
}

_onRadioChange = async (id) => {
const { chain, isSell } = this.state.altBanner
const { chain, } = this.state.altBanner

const { sellAmount, buyAmount, myBalance, assets, } = this.state
let exType = this.state.exType === ExchangeTypes.direct ? ExchangeTypes.multi : ExchangeTypes.direct
const { sellAmount, buyAmount, myBalance, assets, isSell } = this.state
let exType = id === ExchangeTypes.direct ?
ExchangeTypes.direct : ExchangeTypes.multi

let fee
let calc
Expand All @@ -478,9 +483,9 @@ class ConvertAssets extends React.Component {
let res = await getExchange(sellAmount.asset, buyAmount.asset, myBalance,
(data) => {
fee = data.fee
const { warning, altBanner, mainBanner, newSelected, bestType, bestPrice, limitPrice } = data
const { warning, altBanner, mainBanner, newSelected, bestType, bestPrice, limitPrice, chain, isSell } = data
exType = newSelected || exType
this.setState({ warning, altBanner, mainBanner, bestType })
this.setState({ warning, altBanner, mainBanner, bestType, chain, isSell })
this.bestPrice = bestPrice
this.limitPrice = limitPrice
}, exType, isSell)
Expand All @@ -496,7 +501,6 @@ class ConvertAssets extends React.Component {
}
const newState = {
fee,
chain,
exType
}
if (isSell) {
Expand Down Expand Up @@ -540,7 +544,8 @@ class ConvertAssets extends React.Component {
const spans = []
let title
if (banner) {
const { msg, chain, sell, buy, req, isSell } = banner
const { isSell } = this.state
const { msg, chain, sell, buy, req } = banner

if (chain) {
title = chain.syms.join(' > ')
Expand Down
36 changes: 19 additions & 17 deletions app/utils/market/exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export async function getExchange(sellAmount, buyAmount, myBalance,
let bestPrice = null
let fee

let warning = ''
let amDir, amMul
let warDir, warMul
let chain
let directErr, multiErr
let altBanner = null, mainBanner = null
Expand Down Expand Up @@ -86,9 +86,12 @@ export async function getExchange(sellAmount, buyAmount, myBalance,
if (resDir) {
if (resDir.error) {
if (isDir) {
warning = tt('convert_assets_jsx.no_orders_DIRECTION', {
warDir = tt('convert_assets_jsx.no_orders_DIRECTION', {
DIRECTION: sellAmount.symbol + '/' + buyAmount.symbol
})
isDir = false
selected = ExchangeTypes.multi
newSelected = selected
}
} else {
const bp = wrapPrice(resDir.best_price)
Expand All @@ -97,26 +100,25 @@ export async function getExchange(sellAmount, buyAmount, myBalance,
limitPrice = wrapPrice(resDir.limit_price)
}
amDir = wrapAsset(resDir.result)
const remain = resDir.remain
const remain = resDir.remain && wrapAsset(resDir.remain)

if (amDir.amount == 0) {
amDir.amount = 1
if (isDir) {
warning = tt('convert_assets_jsx.too_low_amount')
warDir = tt('convert_assets_jsx.too_low_amount')
}
} else if (!isSell && amDir.gt(myBalance)) {
amDir.amount = myBalance.amount
if (isDir) {
limitPrice = Price(req, res)
warning = tt('convert_assets_jsx.too_big_price')
warDir = tt('convert_assets_jsx.too_big_price')
}
} else if (remain) {
if (isDir) {
warning = {
warDir = {
a1: (isSell ? sellAmount : buyAmount).minus(remain).floatString,
a2: amDir.floatString,
remain: remain.floatString,
isSell
remain: remain.floatString
}
}
if (!isSell) {
Expand Down Expand Up @@ -144,7 +146,7 @@ export async function getExchange(sellAmount, buyAmount, myBalance,

if (!chain) {
if (!isDir) {
warning = tt('convert_assets_jsx.no_orders_DIRECTION', {
warMul = tt('convert_assets_jsx.no_orders_DIRECTION', {
DIRECTION: sellAmount.symbol + '/' + buyAmount.symbol
})
}
Expand All @@ -164,21 +166,20 @@ export async function getExchange(sellAmount, buyAmount, myBalance,
if (amMul.amount == 0) {
amMul.amount = 1
if (!isDir) {
warning = tt('convert_assets_jsx.too_low_amount')
warnMul = tt('convert_assets_jsx.too_low_amount')
}
} else if (!isSell && amMul.gt(myBalance)) {
amMul.amount = myBalance.amount
if (!isDir) {
limitPrice = Price(req, amMul)
warning = tt('convert_assets_jsx.too_big_price')
warMul = tt('convert_assets_jsx.too_big_price')
}
} else if (remain) {
if (isDir) {
warning = {
warMul = {
a1: (isSell ? sellAmount : buyAmount).minus(remain).floatString,
a2: amMul.floatString,
remain: remain.floatString,
isSell
remain: remain.floatString
}
}
if (!isSell) {
Expand Down Expand Up @@ -233,7 +234,7 @@ export async function getExchange(sellAmount, buyAmount, myBalance,
msg: multiErr ? ExchangeErrors.unavailable : ExchangeErrors.no_way
}
}
return { isSell, chain,
return { chain,
sell: (isSell ? req : amMul),
buy: (isSell ? amMul : reqFixed),
req
Expand All @@ -246,7 +247,7 @@ export async function getExchange(sellAmount, buyAmount, myBalance,
msg: directErr ? ExchangeErrors.unavailable : ExchangeErrors.no_way
}
}
return { isSell, direct: true,
return { direct: true,
sell: (isSell ? req : amDir),
buy: (isSell ? amDir : req),
req
Expand All @@ -268,7 +269,7 @@ export async function getExchange(sellAmount, buyAmount, myBalance,

if (onData) {
onData({
warning,
warning: isDir ? warDir : warMul,

bestPrice,
limitPrice,
Expand All @@ -277,6 +278,7 @@ export async function getExchange(sellAmount, buyAmount, myBalance,
fee,
reqFixed,

isSell,
altBanner,
mainBanner,
newSelected,
Expand Down

0 comments on commit ea450ab

Please sign in to comment.