Skip to content

Commit

Permalink
improve exit value checking and simplify messages (#110)
Browse files Browse the repository at this point in the history
(cherry picked from commit 721e481)
  • Loading branch information
CAPtheorem authored and InoMurko committed Jun 17, 2022
1 parent 622b8ee commit 6df2016
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 115 deletions.
26 changes: 6 additions & 20 deletions packages/boba/gateway/src/components/input/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ function Input({
unit,
value,
onChange,
onUseMax,
onSelect,
sx,
paste,
Expand All @@ -46,8 +45,7 @@ function Input({
variant,
newStyle = false,
allowUseAll = false,
allowExitAll = false,
onExitAll,
onUseMax,
loading,
maxLength,
selectOptions,
Expand Down Expand Up @@ -160,27 +158,15 @@ function Input({
{unit && (
<S.ActionsWrapper>
<Typography variant="body2" component="p" sx={{opacity: 0.7, textAlign: "end", mb: 2}}>
Max Amount: {Number(maxValue).toFixed(5)}
Max Amount<br/>{Number(maxValue).toFixed(5)}
</Typography>
{allowUseAll && (
<Box>
<Button onClick={handleClickMax} variant="small" >
Use All
</Button>
</Box>
)}
{allowExitAll && (
<Box>
<Button
onClick={onExitAll}
variant="small"
size="small"
sx={{margin: '10px 0px'}}
loading={loading}
triggerTime={new Date()}
disabled={disabledExitAll}
<Button
onClick={handleClickMax}
variant="small"
>
Bridge All
Use All
</Button>
</Box>
)}
Expand Down
118 changes: 57 additions & 61 deletions packages/boba/gateway/src/containers/modals/exit/steps/DoExitStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,73 +95,64 @@ function DoExitStep({ handleClose, token }) {

setErrorString('')

if (tooSmall || tooBig) {
setErrorString('Warning: Value out of bounds')
if (value <= 0) {
setValidValue(false)
setValue(value)
return false
}
else if (
!feeUseBoba && // check is needed regardless of fee token choice
token.symbol === 'ETH' &&
(Number(value) + feeETH) > balance)
{
// insufficient ETH to cover the ETH amount plus gas
// due to MetaMask issue, this is needed even if you are paying in ETH
setErrorString('Warning: ETH amount + fees > balance')
else if (tooSmall) {
setValidValue(false)
setValue(value)
return false
}
else if (tooBig) {
setValidValue(false)
setValue(value)
return false
}
else if (
feeUseBoba && // check is needed regardless of fee token choice
token.symbol === 'ETH' &&
(Number(value) + feeETH) > balance)
{
// insufficient ETH to cover the ETH amount plus gas
// due to MetaMask issue, this is needed even if you are paying in ETH
setErrorString('Warning: ETH balance too low. Even if you pay in BOBA, you still need to maintain a minimum ETH balance in your wallet.')
(Number(value) + feeETH) > balance) {
if(feeUseBoba)
setErrorString('Warning: ETH amount + fees > balance. Even if you pay in BOBA, you still need to maintain a minimum ETH balance in your wallet')
else
setErrorString('Warning: ETH amount + fees > balance')
setValidValue(false)
setValue(value)
return false
}
else if (
//pay BOBA, exit BOBA - check BOBA amount
feeUseBoba &&
token.symbol === 'BOBA' &&
(Number(value) + feeBOBA) > balance)
(Number(value) + feeBOBA + exitFee) > balance)
{
// insufficient BOBA to cover the BOBA amount plus gas
// insufficient BOBA to cover the BOBA amount plus gas plus exitFee
setErrorString('Warning: BOBA amount + fees > balance')
setValidValue(false)
setValue(value)
return false
}
else if (
!feeUseBoba &&
feeETH > Number(feeBalanceETH))
{
// insufficient ETH to cover exit fees
setErrorString('Warning: ETH balance too low.')
setValidValue(false)
setValue(value)
return false
}
else if (
feeUseBoba &&
// it does not matter if you are paying in ETH or BOBA
feeETH > Number(feeBalanceETH))
{
// insufficient ETH to cover exit fees
setErrorString('Warning: ETH balance too low. Even if you pay in BOBA, you still need to maintain a minimum ETH balance in your wallet.')
if(feeUseBoba)
setErrorString('Warning: ETH balance too low. Even if you pay in BOBA, you still need to maintain a minimum ETH balance in your wallet')
else
setErrorString('Warning: ETH balance too low to cover gas')
setValidValue(false)
setValue(value)
return false
}
else if (
// insufficient BOBA to cover exit fees
feeUseBoba &&
feeBOBA > Number(feeBalanceBOBA))
(feeBOBA + exitFee) > Number(feeBalanceBOBA))
{
// insufficient BOBA to cover exit fees
setErrorString('Warning: BOBA balance too low')
setErrorString('Warning: BOBA balance too low to cover gas/fees')
setValidValue(false)
setValue(value)
return false
Expand All @@ -176,16 +167,18 @@ function DoExitStep({ handleClose, token }) {

async function doExit() {

console.log("Amount to exit:", value_Wei_String)

let res = await dispatch(exitBOBA(token.address, value_Wei_String))
let res = await dispatch(
exitBOBA(
token.address,
value_Wei_String
)
)

if (res) {
dispatch(
openAlert(
`${token.symbol} was bridged to L1. You will receive
${Number(value).toFixed(3)} ${token.symbol}
on L1 in 7 days.`
${Number(value).toFixed(3)} ${token.symbol} on L1 in 7 days.`
)
)
handleClose()
Expand All @@ -208,7 +201,6 @@ function DoExitStep({ handleClose, token }) {

useEffect(() => {
if (typeof(token) !== 'undefined') {
console.log("Token:",token)
dispatch(fetchClassicExitCost(token.address))
dispatch(fetchL2BalanceETH())
dispatch(fetchL2BalanceBOBA())
Expand All @@ -221,8 +213,6 @@ function DoExitStep({ handleClose, token }) {
function estimateMax() {

const safeCost = Number(cost) * 1.04 // 1.04 = safety margin on the cost
console.log("ETH fees:", safeCost)
console.log("BOBA fees:", safeCost * feePriceRatio)

setFeeETH(safeCost)
setFeeBOBA(safeCost * feePriceRatio)
Expand All @@ -237,8 +227,14 @@ function DoExitStep({ handleClose, token }) {
setMax_Float(0.0)
}
else if (token.symbol === 'BOBA' && feeUseBoba) {
if(balance - safeCost > 0.0)
setMax_Float(balance - safeCost)
if(balance - (safeCost * feePriceRatio) - exitFee > 0.0)
setMax_Float(balance - (safeCost * feePriceRatio) - exitFee)
else
setMax_Float(0.0)
}
else if (token.symbol === 'BOBA' && !feeUseBoba) {
if(balance - exitFee > 0.0)
setMax_Float(balance - exitFee)
else
setMax_Float(0.0)
}
Expand All @@ -253,21 +249,25 @@ function DoExitStep({ handleClose, token }) {

if(feeETH && Number(feeETH) > 0) {
if(feeUseBoba) {
ETHstring = `Estimated gas (approval + exit): ${Number(feeBOBA).toFixed(4)} BOBA`
ETHstring = `Estimated gas: ${Number(feeBOBA).toFixed(4)} BOBA`
} else {
ETHstring = `Estimated gas (approval + exit): ${Number(feeETH).toFixed(4)} ETH`
ETHstring = `Estimated gas: ${Number(feeETH).toFixed(4)} ETH`
}
}

// prohibit ExitAll when paying with the token that is to be exited
let allowExitall = true
let allowUseAll = true
if(token.symbol === 'ETH') {
allowExitall = false
allowUseAll = false
}
else if (token.symbol === 'BOBA' && feeUseBoba) {
allowExitall = false
allowUseAll = false
}

let receiveL1 = `You will receive ${Number(value).toFixed(3)} ${token.symbol}
${!!amountToUsd(value, lookupPrice, token) ? `($${amountToUsd(value, lookupPrice, token).toFixed(2)})`: ''}
on L1. Your funds will be available on L1 in 7 days.`

return (
<>
<Box>
Expand All @@ -285,12 +285,11 @@ function DoExitStep({ handleClose, token }) {
setAmount(i.target.value)
setValue_Wei_String(toWei_String(i.target.value, token.decimals))
}}
onUseMax={(i)=>{//they want to use the maximum
console.log(i)
onUseMax={(i)=>{ //they want to use the maximum
setAmount(max_Float) //so the display value updates for the user
setValue_Wei_String(token.balance.toString())
}}
allowUseAll={allowExitall}
allowUseAll={allowUseAll}
unit={token.symbol}
maxValue={max_Float}
variant="standard"
Expand All @@ -302,22 +301,18 @@ function DoExitStep({ handleClose, token }) {
Loading...
</Typography>
}
{validValue && token && (
<Typography variant="body2" sx={{mt: 2}}>
{value &&
`You will receive ${Number(value).toFixed(3)} ${token.symbol}
${!!amountToUsd(value, lookupPrice, token) ? `($${amountToUsd(value, lookupPrice, token).toFixed(2)})`: ''}
on L1. Your funds will be available on L1 in 7 days.`}
</Typography>
)}

<Typography variant="body2" sx={{mt: 2}}>
{parse(`Message Relay Fee: ${exitFee} BOBA`)}
<br/>
{parse(ETHstring)}
</Typography>

<Typography variant="body2" sx={{mt: 2}}>
{parse(`Exit Fee: ${exitFee} BOBA`)}
</Typography>
{validValue && token && value &&
<Typography variant="body2" sx={{mt: 2}}>
{receiveL1}
</Typography>
}

{errorString !== '' &&
<Typography variant="body2" sx={{mt: 2, color: 'red'}}>
Expand Down Expand Up @@ -356,6 +351,7 @@ function DoExitStep({ handleClose, token }) {
</Button>
)}
</WrapperActionsModal>

</>
)
}
Expand Down
Loading

0 comments on commit 6df2016

Please sign in to comment.