Skip to content
This repository was archived by the owner on Dec 5, 2021. It is now read-only.
10 changes: 9 additions & 1 deletion packages/omgx/wallet-frontend/src/actions/networkAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,15 @@ export function depositL2LP(token, value) {
)
}

//DEPOSIT ETH
//SWAP RELATED - Depositing into the L2LP triggers the swap-exit - variant of depositL2LP
//that handles Exit All
export function fastExitAll(token, value) {
return createAction('EXIT/CREATE', () =>
networkService.fastExitAll(token, value)
)
}

//CLASSICAL DEPOSIT ETH
export function depositETHL2(value) {
return createAction('DEPOSIT/CREATE', () => {
return networkService.depositETHL2(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import React, { useState, useEffect } from 'react'

import { useDispatch, useSelector } from 'react-redux'

import { depositL2LP } from 'actions/networkAction'
import { depositL2LP, fastExitAll } from 'actions/networkAction'
import { openAlert } from 'actions/uiAction'

import { selectLoading } from 'selectors/loadingSelector'
Expand Down Expand Up @@ -101,6 +101,28 @@ function DoExitStepFast({ handleClose, token }) {

}

async function doExitAll() {

console.log("Amount to exit:", token.balance.toString())

let res = await dispatch(
fastExitAll(
token.address
)
)

if (res) {
dispatch(
openAlert(
`${token.symbol} was bridged. You will receive
${receivableAmount(value)} ${token.symbol} minus gas fees (if bridging ETH) on L1.`
)
)
handleClose()
}

}

useEffect(() => {
if (typeof(token) !== 'undefined') {
networkService.L1LPBalance(token.addressL1).then((res) => {
Expand All @@ -109,6 +131,9 @@ function DoExitStepFast({ handleClose, token }) {
networkService.getTotalFeeRate().then((feeRate) => {
setFeeRate(feeRate)
})
// networkService.getFastExitAllCost(token.address).then((fee) => {
// console.log("fee:",fee)
// })
}
// to clean up state and fix the
// error in console for max state update.
Expand Down Expand Up @@ -189,6 +214,20 @@ function DoExitStepFast({ handleClose, token }) {
)}
</Box>

<Button
onClick={doExitAll}
color='primary'
variant='contained'
loading={loading}
tooltip={loading ? "Your transaction is still pending. Please wait for confirmation." : "Click here to bridge your funds to L1"}
disabled={false}
triggerTime={new Date()}
fullWidth={isMobile}
size='large'
>
Bridge All to L1
</Button>

<WrapperActionsModal>
<Button
onClick={handleClose}
Expand All @@ -210,6 +249,7 @@ function DoExitStepFast({ handleClose, token }) {
>
Bridge to L1
</Button>

</WrapperActionsModal>
</>
)
Expand Down
103 changes: 101 additions & 2 deletions packages/omgx/wallet-frontend/src/services/networkService.js
Original file line number Diff line number Diff line change
Expand Up @@ -1394,7 +1394,8 @@ class NetworkService {
this.L2LPAddress
)

let depositAmount_BN = new BN(value_Wei_String)
//let depositAmount_BN = new BN(value_Wei_String)
let depositAmount_BN = BigNumber.from(value_Wei_String)

if (depositAmount_BN.gt(allowance_BN)) {
const approveStatus = await L2ERC20Contract.approve(
Expand Down Expand Up @@ -2052,6 +2053,103 @@ class NetworkService {
return balance.toString()
}

/**************************************************************/
/***** SWAP OFF from BOBA by depositing funds to the L2LP *****/
/**************************************************************/
async fastExitAll(currencyAddress) {

updateSignatureStatus_exitLP(false)

let costToExit_BN = BigNumber.from("0")
let approvalGas_BN = BigNumber.from("0")
let approvalCost_BN = BigNumber.from("0")

const L2ERC20Contract = new ethers.Contract(
currencyAddress,
L2ERC20Json.abi,
this.provider.getSigner()
)

let balance_BN = await L2ERC20Contract.balanceOf(
this.account
)
console.log("Initial Balance (ETH)", utils.formatEther(balance_BN))

let allowance_BN = await L2ERC20Contract.allowance(
this.account,
this.L2LPAddress
)
//console.log("Allowance:",allowance_BN.toString())

if (balance_BN.gt(allowance_BN)) {

//Estimate gas
const tx = await L2ERC20Contract.populateTransaction.approve(
this.L2LPAddress,
balance_BN
)

approvalGas_BN = await this.L2Provider.estimateGas(tx)
approvalCost_BN = approvalGas_BN.mul('15000000')

console.log("Cost to Approve", utils.formatEther(approvalCost_BN))

const approveStatus = await L2ERC20Contract.approve(
this.L2LPAddress,
balance_BN
)
await approveStatus.wait()

if (!approveStatus)
return false

} else {
//console.log("Balance:",balance_BN)
console.log("Allowance is already suitable:", utils.formatEther(allowance_BN))
}

balance_BN = balance_BN.sub(approvalCost_BN)
console.log("Balance after approval", utils.formatEther(balance_BN))

const tx2 = await this.L2LPContract.populateTransaction.clientDepositL2(
balance_BN,
currencyAddress
)
//console.log("tx2",tx2)

const despositGas_BN = await this.L2Provider.estimateGas(tx2)

let despositCost_BN = despositGas_BN.mul('15000000')
console.log("Deposit gas cost (ETH)", utils.formatEther(despositCost_BN))

let amount_BN = balance_BN.sub(despositCost_BN)
console.log("Amount to exit (ETH)", utils.formatEther(amount_BN))

const depositTX = await this.L2LPContract.clientDepositL2(
amount_BN.toString(),
currencyAddress
)

//at this point the tx has been submitted, and we are waiting...
await depositTX.wait()

//closes the modal
updateSignatureStatus_exitLP(true)

// Waiting for the response from L1
const [L2ToL1msgHash] = await this.fastWatcher.getMessageHashesFromL2Tx(
depositTX.hash
)
console.log(' got L2->L1 message hash', L2ToL1msgHash)

const L1Receipt = await this.fastWatcher.getL1TransactionReceipt(
L2ToL1msgHash
)
console.log(' completed Deposit! L1 tx hash:', L1Receipt.transactionHash)

return L1Receipt
}

/**************************************************************/
/***** SWAP OFF from BOBA by depositing funds to the L2LP *****/
/**************************************************************/
Expand All @@ -2070,7 +2168,8 @@ class NetworkService {
this.L2LPAddress
)

let depositAmount_BN = new BN(value_Wei_String)
//let depositAmount_BN = new BN(value_Wei_String)
let depositAmount_BN = BigNumber.from(value_Wei_String)

if (depositAmount_BN.gt(allowance_BN)) {
const approveStatus = await L2ERC20Contract.approve(
Expand Down