diff --git a/src/components/app-body/counter-offers/cancel-counter-offer-btn.js b/src/components/app-body/counter-offers/cancel-counter-offer-btn.js index 4724a28..66730ad 100644 --- a/src/components/app-body/counter-offers/cancel-counter-offer-btn.js +++ b/src/components/app-body/counter-offers/cancel-counter-offer-btn.js @@ -7,30 +7,129 @@ // Global npm libraries import React, { useState } from 'react' -import { Button, Modal, Container } from 'react-bootstrap' +import { Button, Modal, Container, Row, Col, Spinner } from 'react-bootstrap' +import Sweeper from 'bch-token-sweep' function CancelCounterOfferBtn (props) { const [show, setShow] = useState(false) + const [statusMsg, setStatusMsg] = useState('') + const [hideSpinner, setHideSpinner] = useState(false) + const [isProcessing, setIsProcessing] = useState(false) + + // Update wallet state function + const updateWalletState = async () => { + const wallet = props.appData.wallet + const bchBalance = await wallet.getBalance({ bchAddress: wallet.walletInfo.cashAddress }) + await wallet.initialize() + const slpTokens = await wallet.listTokens(wallet.walletInfo.cashAddress) + props.appData.updateBchWalletState({ walletObj: { bchBalance, slpTokens }, appData: props.appData }) + } + + // Handle cancel/sweep function + const handleCancel = async () => { + try { + console.log('Canceling Counter Offer') + + // Set modal initial state + setShow(true) + setHideSpinner(false) + setStatusMsg('') + setIsProcessing(true) + + // Get the keypair that holds Counter Offer UTXOs. + // This uses the same derivation path as used in the counter offers page (index 1) + const bchDexLib = props.appData.dexLib + const keyPair = await bchDexLib.take.util.getKeyPair(1) + const wif = keyPair.wif + + // Get wallet info for sweeping + const walletWif = props.appData.wallet.walletInfo.privateKey + const toAddr = props.appData.wallet.slpAddress + + // Instance the Sweep library and populate UTXOs from network + const sweep = new Sweeper(wif, walletWif, props.appData.wallet) + await sweep.populateObjectFromNetwork() + + // Constructing the sweep transaction + const hex = await sweep.sweepTo(toAddr) + const txid = await props.appData.wallet.ar.sendTx(hex) + + // Generate success status message + const newStatusMsg = ( + <> +
Sweep succeeded!
+Counter Offer has been canceled.
+Transaction ID: {txid}
++ + TX on Blockchair BCH Block Explorer + +
+ + > + ) + + setHideSpinner(true) + setStatusMsg(newStatusMsg) + setIsProcessing(false) + + // Update wallet state to reflect the changes + await updateWalletState() + + // Refresh the counter offers list if refreshTokens function is provided + if (props.refreshTokens) { + // Small delay to ensure blockchain state is updated + setTimeout(() => { + props.refreshTokens() + }, 2000) + } + } catch (err) { + console.error('Error in handleCancel(): ', err) + setHideSpinner(true) + setIsProcessing(false) + setStatusMsg({`Error: ${err.message}`}) + } + } const handleClose = () => { setShow(false) - // props.instance.setState({ showModal: false }) + setStatusMsg('') + setHideSpinner(false) + setIsProcessing(false) } const handleOpen = () => { setShow(true) + handleCancel() // Automatically start the cancel process when modal opens } return ( <> - +- When a Buy order is created, the coins (UTXO) to pay for it are - moved to a secondary address. Clicking the button below will - sweep those funds back into this main wallet. This will also - cancel/invalidate all open Counter Offers that you've created. -
- -