From 00bebfa371f0f0bc0289e139a581dff538ad944c Mon Sep 17 00:00:00 2001 From: tecnovert Date: Wed, 28 Jun 2023 19:27:44 +0200 Subject: [PATCH] ui: Fix active sent bids count. --- basicswap/basicswap.py | 7 +++--- basicswap/basicswap_util.py | 49 +++++++++++++++++++++++++++---------- basicswap/db.py | 7 ++++-- basicswap/db_upgrades.py | 22 +++++++++++++++-- doc/api.md | 5 ++++ 5 files changed, 70 insertions(+), 20 deletions(-) diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index f198701..fff7ba4 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -1051,7 +1051,7 @@ class BasicSwap(BaseApp): self.removeWatchedOutput(Coins(offer.coin_from), bid.bid_id, None) self.removeWatchedOutput(Coins(offer.coin_to), bid.bid_id, None) - if bid.state == BidStates.BID_ABANDONED or bid.state == BidStates.SWAP_COMPLETED: + if bid.state in (BidStates.BID_ABANDONED, BidStates.SWAP_COMPLETED): # Return unused addrs to pool itx_state = bid.getITxState() ptx_state = bid.getPTxState() @@ -5824,12 +5824,13 @@ class BasicSwap(BaseApp): now: int = self.getTime() q_str = '''SELECT COUNT(CASE WHEN b.was_sent THEN 1 ELSE NULL END) AS count_sent, - COUNT(CASE WHEN b.was_sent AND b.state = {} AND b.expire_at > {} AND o.expire_at > {} THEN 1 ELSE NULL END) AS count_sent_active, + COUNT(CASE WHEN b.was_sent AND s.swap_ended = 0 AND b.expire_at > {} AND o.expire_at > {} THEN 1 ELSE NULL END) AS count_sent_active, COUNT(CASE WHEN b.was_received THEN 1 ELSE NULL END) AS count_received, COUNT(CASE WHEN b.was_received AND b.state = {} AND b.expire_at > {} AND o.expire_at > {} THEN 1 ELSE NULL END) AS count_available FROM bids b JOIN offers o ON b.offer_id = o.offer_id - WHERE b.active_ind = 1'''.format(BidStates.BID_RECEIVED, now, now, BidStates.BID_RECEIVED, now, now) + JOIN bidstates s ON b.state = s.state_id + WHERE b.active_ind = 1'''.format(now, now, BidStates.BID_RECEIVED, now, now) q = self.engine.execute(q_str).first() bids_sent = q[0] bids_sent_active = q[1] diff --git a/basicswap/basicswap_util.py b/basicswap/basicswap_util.py index cecabdb..0bbed94 100644 --- a/basicswap/basicswap_util.py +++ b/basicswap/basicswap_util.py @@ -464,6 +464,25 @@ def getLastBidState(packed_states): return BidStates.BID_STATE_UNKNOWN +def strSwapType(swap_type): + if swap_type == SwapTypes.SELLER_FIRST: + return 'seller_first' + if swap_type == SwapTypes.XMR_SWAP: + return 'xmr_swap' + return None + + +def strSwapDesc(swap_type): + if swap_type == SwapTypes.SELLER_FIRST: + return 'Secret Hash' + if swap_type == SwapTypes.XMR_SWAP: + return 'Adaptor Sig' + return None + + +inactive_states = [BidStates.SWAP_COMPLETED, BidStates.BID_ERROR, BidStates.BID_REJECTED, BidStates.SWAP_TIMEDOUT, BidStates.BID_ABANDONED] + + def isActiveBidState(state): if state >= BidStates.BID_ACCEPTED and state < BidStates.SWAP_COMPLETED: return True @@ -492,20 +511,24 @@ def isActiveBidState(state): return False -def strSwapType(swap_type): - if swap_type == SwapTypes.SELLER_FIRST: - return 'seller_first' - if swap_type == SwapTypes.XMR_SWAP: - return 'xmr_swap' - return None +def isErrorBidState(state): + return state in ( + BidStates.BID_STALLED_FOR_TEST, + BidStates.BID_ERROR, + ) -def strSwapDesc(swap_type): - if swap_type == SwapTypes.SELLER_FIRST: - return 'Secret Hash' - if swap_type == SwapTypes.XMR_SWAP: - return 'Adaptor Sig' - return None +def isFailingBidState(state): + return state in ( + BidStates.BID_STALLED_FOR_TEST, + BidStates.BID_ERROR, + BidStates.XMR_SWAP_SCRIPT_TX_PREREFUND, + BidStates.XMR_SWAP_NOSCRIPT_TX_RECOVERED, + BidStates.XMR_SWAP_FAILED_REFUNDED, + BidStates.XMR_SWAP_FAILED_SWIPED, + BidStates.XMR_SWAP_FAILED, + ) -inactive_states = [BidStates.SWAP_COMPLETED, BidStates.BID_ERROR, BidStates.BID_REJECTED, BidStates.SWAP_TIMEDOUT, BidStates.BID_ABANDONED] +def isFinalBidState(state): + return state in inactive_states diff --git a/basicswap/db.py b/basicswap/db.py index b1d8247..ed5cb4f 100644 --- a/basicswap/db.py +++ b/basicswap/db.py @@ -12,8 +12,8 @@ from enum import IntEnum, auto from sqlalchemy.ext.declarative import declarative_base -CURRENT_DB_VERSION = 19 -CURRENT_DB_DATA_VERSION = 2 +CURRENT_DB_VERSION = 20 +CURRENT_DB_DATA_VERSION = 3 Base = declarative_base() @@ -487,6 +487,9 @@ class BidState(Base): state_id = sa.Column(sa.Integer) label = sa.Column(sa.String) in_progress = sa.Column(sa.Integer) + in_error = sa.Column(sa.Integer) + swap_failed = sa.Column(sa.Integer) + swap_ended = sa.Column(sa.Integer) note = sa.Column(sa.String) created_at = sa.Column(sa.BigInteger) diff --git a/basicswap/db_upgrades.py b/basicswap/db_upgrades.py index e866c7d..bfaaddf 100644 --- a/basicswap/db_upgrades.py +++ b/basicswap/db_upgrades.py @@ -18,7 +18,11 @@ from .db import ( from .basicswap_util import ( BidStates, strBidState, - isActiveBidState) + isActiveBidState, + isErrorBidState, + isFailingBidState, + isFinalBidState, +) def upgradeDatabaseData(self, data_version): @@ -56,10 +60,13 @@ def upgradeDatabaseData(self, data_version): active_ind=1, state_id=int(state), in_progress=isActiveBidState(state), + in_error=isErrorBidState(state), + swap_failed=isFailingBidState(state), + swap_ended=isFinalBidState(state), label=strBidState(state), created_at=now)) - if data_version < 2: + if data_version > 0 and data_version < 2: for state in (BidStates.XMR_SWAP_MSG_SCRIPT_LOCK_TX_SIGS, BidStates.XMR_SWAP_MSG_SCRIPT_LOCK_SPEND_TX): session.add(BidState( active_ind=1, @@ -67,6 +74,12 @@ def upgradeDatabaseData(self, data_version): in_progress=isActiveBidState(state), label=strBidState(state), created_at=now)) + if data_version > 0 and data_version < 3: + for state in BidStates: + in_error = isErrorBidState(state) + swap_failed = isFailingBidState(state) + swap_ended = isFinalBidState(state) + session.execute('UPDATE bidstates SET in_error = :in_error, swap_failed = :swap_failed, swap_ended = :swap_ended WHERE state_id = :state_id', {'in_error': in_error, 'swap_failed': swap_failed, 'swap_ended': swap_ended, 'state_id': int(state)}) self.db_data_version = CURRENT_DB_DATA_VERSION self.setIntKVInSession('db_data_version', self.db_data_version, session) @@ -248,6 +261,11 @@ def upgradeDatabase(self, db_version): db_version += 1 session.execute('ALTER TABLE xmr_split_data ADD COLUMN addr_from STRING') session.execute('ALTER TABLE xmr_split_data ADD COLUMN addr_to STRING') + elif current_version == 19: + db_version += 1 + session.execute('ALTER TABLE bidstates ADD COLUMN in_error INTEGER') + session.execute('ALTER TABLE bidstates ADD COLUMN swap_failed INTEGER') + session.execute('ALTER TABLE bidstates ADD COLUMN swap_ended INTEGER') if current_version != db_version: self.db_version = db_version diff --git a/doc/api.md b/doc/api.md index f79c3d5..d403695 100644 --- a/doc/api.md +++ b/doc/api.md @@ -2,6 +2,11 @@ Examples: + curl --header "Content-Type: application/json" \ + --request POST \ + --data '{"coin_from":"btc","coin_to":"xmr","amt_from":10,"amt_to":10,"lockseconds":1200}' \ + http://localhost:12701/json/offers/new + curl --header "Content-Type: application/json" \ --request POST \ --data '{"show_extra":true}' \