diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index fc90688..e072859 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -43,6 +43,17 @@ from .messages_pb2 import ( BidMessage, BidAcceptMessage, ) +from .db import ( + CURRENT_DB_VERSION, + Base, + DBKVInt, + DBKVString, + Offer, + Bid, + SwapTx, + PooledAddress, + SentOffer, +) import basicswap.config as cfg import basicswap.segwit_addr as segwit_addr @@ -50,8 +61,6 @@ import basicswap.segwit_addr as segwit_addr DEBUG = True SMSG_SECONDS_IN_DAY = 86400 -CURRENT_DB_VERSION = 1 - MIN_OFFER_VALID_TIME = 60 * 10 MAX_OFFER_VALID_TIME = 60 * 60 * 48 @@ -115,7 +124,9 @@ class OpCodes(IntEnum): OP_CHECKSEQUENCEVERIFY = 0xb2, -class TxTypes(IntEnum): # For PooledAddress +class TxTypes(IntEnum): + ITX = auto() + PTX = auto() ITX_REDEEM = auto() ITX_REFUND = auto() PTX_REDEEM = auto() @@ -280,168 +291,6 @@ def replaceAddrPrefix(addr, coin_type, chain_name, addr_type='pubkey_address'): return encodeAddress(bytes((chainparams[coin_type][chain_name][addr_type],)) + decodeAddress(addr)[1:]) -Base = declarative_base() - - -class DBKVInt(Base): - __tablename__ = 'kv_int' - key = sa.Column(sa.String, primary_key=True) - value = sa.Column(sa.Integer) - - -class DBKVString(Base): - __tablename__ = 'kv_string' - key = sa.Column(sa.String, primary_key=True) - value = sa.Column(sa.String) - - -class Offer(Base): - __tablename__ = 'offers' - - offer_id = sa.Column(sa.LargeBinary, primary_key=True) - - coin_from = sa.Column(sa.Integer) - coin_to = sa.Column(sa.Integer) - amount_from = sa.Column(sa.BigInteger) - rate = sa.Column(sa.BigInteger) - min_bid_amount = sa.Column(sa.BigInteger) - time_valid = sa.Column(sa.BigInteger) - lock_type = sa.Column(sa.Integer) - lock_value = sa.Column(sa.Integer) - swap_type = sa.Column(sa.Integer) - - proof_address = sa.Column(sa.String) - proof_signature = sa.Column(sa.LargeBinary) - pkhash_seller = sa.Column(sa.LargeBinary) - secret_hash = sa.Column(sa.LargeBinary) - - addr_from = sa.Column(sa.String) - created_at = sa.Column(sa.BigInteger) - expire_at = sa.Column(sa.BigInteger) - was_sent = sa.Column(sa.Boolean) - - auto_accept_bids = sa.Column(sa.Boolean) - - state = sa.Column(sa.Integer) - states = sa.Column(sa.LargeBinary) # Packed states and times - - def setState(self, new_state): - now = int(time.time()) - self.state = new_state - if self.states is None: - self.states = struct.pack('