# -*- coding: utf-8 -*- # Copyright (c) 2019-2023 tecnovert # Distributed under the MIT software license, see the accompanying # file LICENSE or http://www.opensource.org/licenses/mit-license.php. import time import struct import sqlalchemy as sa from enum import IntEnum, auto from sqlalchemy.ext.declarative import declarative_base CURRENT_DB_VERSION = 22 CURRENT_DB_DATA_VERSION = 4 Base = declarative_base() class Concepts(IntEnum): OFFER = auto() BID = auto() NETWORK_MESSAGE = auto() AUTOMATION = auto() def strConcepts(state): if state == Concepts.OFFER: return 'Offer' if state == Concepts.BID: return 'Bid' if state == Concepts.NETWORK_MESSAGE: return 'Network Message' return 'Unknown' 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) active_ind = sa.Column(sa.Integer) protocol_version = sa.Column(sa.Integer) 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) proof_utxos = sa.Column(sa.LargeBinary) pkhash_seller = sa.Column(sa.LargeBinary) secret_hash = sa.Column(sa.LargeBinary) addr_from = sa.Column(sa.String) addr_to = sa.Column(sa.String) created_at = sa.Column(sa.BigInteger) expire_at = sa.Column(sa.BigInteger) was_sent = sa.Column(sa.Boolean) # Sent by node from_feerate = sa.Column(sa.BigInteger) to_feerate = sa.Column(sa.BigInteger) amount_negotiable = sa.Column(sa.Boolean) rate_negotiable = sa.Column(sa.Boolean) # Local fields auto_accept_bids = sa.Column(sa.Boolean) withdraw_to_addr = sa.Column(sa.String) # Address to spend lock tx to - address from wallet if empty TODO security_token = sa.Column(sa.LargeBinary) bid_reversed = 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('