diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index a8fa171..d2c9f39 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -31,6 +31,7 @@ from .util import ( decodeWif, toWIF, getKeyID, + makeInt, ) from .chainparams import ( chainparams, @@ -554,6 +555,7 @@ class BasicSwap(): ro = self.callrpc('smsginbox', ['unread', '', options]) nm = 0 for msg in ro['messages']: + msg['hex'] += '00' # Add nullbtye to match output from 'smsg' cmd - TODO: make consistent self.processMsg(msg) nm += 1 self.log.info('Scanned %d unread messages.', nm) @@ -1580,7 +1582,7 @@ class BasicSwap(): continue # Verify amount if assert_amount: - assert(int(o['amount'] * COIN) == int(assert_amount)), 'Incorrect output amount in txn {}.'.format(assert_txid) + assert(makeInt(o['amount']) == int(assert_amount)), 'Incorrect output amount in txn {}: {} != {}.'.format(assert_txid, makeInt(o['amount']), int(assert_amount)) if not sum_output: if o['height'] > 0: @@ -1623,7 +1625,9 @@ class BasicSwap(): initiate_txn = self.callcoinrpc(coin_from, 'getrawtransaction', [initiate_txnid_hex, True]) # Verify amount vout = getVoutByAddress(initiate_txn, p2sh) - assert(int(initiate_txn['vout'][vout]['value'] * COIN) == int(bid.amount)), 'Incorrect output amount in initiate txn.' + + out_value = makeInt(initiate_txn['vout'][vout]['value']) + assert(out_value == int(bid.amount)), 'Incorrect output amount in initiate txn {}: {} != {}.'.format(initiate_txnid_hex, out_value, int(bid.amount)) bid.initiate_tx.conf = initiate_txn['confirmations'] try: @@ -2122,6 +2126,7 @@ class BasicSwap(): self.swaps_in_progress[bid_id] = (bid, offer) def processMsg(self, msg): + self.log.debug('processMsg %s', msg['hex']) self.mxDB.acquire() try: msg_type = int(msg['hex'][:2], 16) diff --git a/basicswap/http_server.py b/basicswap/http_server.py index 05168cf..fc6a6be 100644 --- a/basicswap/http_server.py +++ b/basicswap/http_server.py @@ -219,6 +219,7 @@ class HttpHandler(BaseHTTPRequestHandler): tx_vsize = swap_client.getContractSpendTxVSize(k) est_fee = (fee_rate * tx_vsize) / 1000 wallets_formatted.append({ + 'name': w['name'], 'cid': str(int(k)), 'fee_rate': format8(fee_rate * COIN), 'est_fee': format8(est_fee * COIN), diff --git a/basicswap/util.py b/basicswap/util.py index c1969d3..fcc1897 100644 --- a/basicswap/util.py +++ b/basicswap/util.py @@ -18,6 +18,11 @@ from xmlrpc.client import ( from .segwit_addr import bech32_decode, convertbits, bech32_encode COIN = 100000000 +DCOIN = decimal.Decimal(COIN) + + +def makeInt(v): + return int(dquantize(decimal.Decimal(v) * DCOIN).quantize(decimal.Decimal(1))) def format8(i): diff --git a/tests/basicswap/test_other.py b/tests/basicswap/test_other.py index d615c9d..21cd0d4 100644 --- a/tests/basicswap/test_other.py +++ b/tests/basicswap/test_other.py @@ -9,6 +9,8 @@ import unittest from basicswap.util import ( SerialiseNum, DeserialiseNum, + makeInt, + format8, ) from basicswap.basicswap import ( Coins, @@ -19,15 +21,13 @@ from basicswap.basicswap import ( ) -def test_case(v, nb=None): - b = SerialiseNum(v) - if nb is not None: - assert(len(b) == nb) - assert(v == DeserialiseNum(b)) - - class Test(unittest.TestCase): def test_serialise_num(self): + def test_case(v, nb=None): + b = SerialiseNum(v) + if nb is not None: + assert(len(b) == nb) + assert(v == DeserialiseNum(b)) test_case(0, 1) test_case(1, 1) test_case(16, 1) @@ -57,6 +57,26 @@ class Test(unittest.TestCase): decoded = decodeSequence(encoded) assert(decoded == blocks_val) + def test_makeInt(self): + def test_case(v): + sv = format8(makeInt(v)) + # Strip + for i in range(7): + if sv[-1] == '0': + sv = sv[:-1] + assert(sv == v) + test_case('0.00899999') + test_case('899999.0') + test_case('899999.00899999') + test_case('1.0') + test_case('1.1') + test_case('1.2') + test_case('0.00899991') + test_case('0.0089999') + test_case('0.0089991') + test_case('0.123') + test_case('123000.000123') + if __name__ == '__main__': unittest.main()