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 + +

+

+ + TX on token 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 ( <> - + - Cancel + Cancel Counter Offer - {/** ... */} + + {!hideSpinner && ( + + Canceling Counter Offer... + + + )} + +
+ {statusMsg && ( + + {statusMsg} + + )}
diff --git a/src/components/app-body/counter-offers/counter-offer-card.js b/src/components/app-body/counter-offers/counter-offer-card.js index d72f573..325c108 100644 --- a/src/components/app-body/counter-offers/counter-offer-card.js +++ b/src/components/app-body/counter-offers/counter-offer-card.js @@ -12,7 +12,7 @@ import InfoButton from './info-button' import CancelCounterOfferBtn from './cancel-counter-offer-btn' function CounterOfferCard (props) { - const { token } = props + const { token, appData, refreshTokens } = props const [icon, setIcon] = useState(token.icon) // Update icon state every token.icon changes @@ -71,7 +71,11 @@ function CounterOfferCard (props) { - + diff --git a/src/components/app-body/sweep/index.js b/src/components/app-body/sweep/index.js index e160482..ef8d275 100644 --- a/src/components/app-body/sweep/index.js +++ b/src/components/app-body/sweep/index.js @@ -105,30 +105,6 @@ const SweepWif = (props) => { } } - const handleCancelCounterOffers = async () => { - try { - console.log('Executing handleCancelCounterOffers()') - - // Set modal initial state - // setShowModal(true) - // setHideSpinner(false) - // setStatusMsg('') - - // Get the keypair that holds Counter Offer UTXOs. - const bchDexLib = appData.dexLib - const keyPair = await bchDexLib.take.util.getKeyPair(1) - const wif = keyPair.wif - // console.log(`WIF: ${wif}`) - - // Sweep the private key holding the Counter Offer UTXOs. - setWifToSweep(wif) - - await handleSweep() - } catch (err) { - console.error('Error in handleCancelCounterOffers(): ', err) - } - } - // Modal component const getModal = () => ( setShowModal(false)}> @@ -207,28 +183,6 @@ const SweepWif = (props) => { -
-
-
- - - -

- 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. -

- -
- - - - - - {showModal && getModal()}