Check available balance before sending xmr bid

2024-05-20_merge
tecnovert 3 years ago
parent 65951220b8
commit 1476ad3cec
No known key found for this signature in database
GPG Key ID: 8ED6D8750C4E3F93
  1. 14
      basicswap/basicswap.py
  2. 3
      basicswap/interface_btc.py
  3. 6
      basicswap/interface_part.py
  4. 13
      basicswap/interface_xmr.py
  5. 2
      basicswap/rpc.py

@ -1667,17 +1667,16 @@ class BasicSwap(BaseApp):
self.checkSynced(coin_from, coin_to)
contract_count = self.getNewContractId()
amount_to = int((msg_buf.amount * bid_rate) // self.ci(coin_from).COIN())
amount_to = int((msg_buf.amount * bid_rate) // ci_from.COIN())
now = int(time.time())
if offer.swap_type == SwapTypes.SELLER_FIRST:
msg_buf.pkhash_buyer = getKeyID(self.getContractPubkey(dt.datetime.fromtimestamp(now).date(), contract_count))
proof_addr, proof_sig = self.getProofOfFunds(coin_to, amount_to, offer_id)
msg_buf.proof_address = proof_addr
msg_buf.proof_signature = proof_sig
contract_count = self.getNewContractId()
msg_buf.pkhash_buyer = getKeyID(self.getContractPubkey(dt.datetime.fromtimestamp(now).date(), contract_count))
else:
raise ValueError('TODO')
@ -2011,6 +2010,11 @@ class BasicSwap(BaseApp):
self.checkSynced(coin_from, coin_to)
amount_to = int((int(amount) * bid_rate) // ci_from.COIN())
balance_to = ci_to.getSpendableBalance()
ensure(balance_to > amount_to, '{} spendable balance is too low: {}'.format(ci_to.coin_name(), ci_to.format_amount(balance_to)))
msg_buf = XmrBidMessage()
msg_buf.protocol_version = 1
msg_buf.offer_msg_id = offer_id

@ -1096,6 +1096,9 @@ class BTCInterface(CoinInterface):
def describeTx(self, tx_hex):
return self.rpc_callback('decoderawtransaction', [tx_hex])
def getSpendableBalance(self):
return self.make_int(self.rpc_callback('getbalances')['mine']['trusted'])
def testBTCInterface():
print('testBTCInterface')

@ -601,6 +601,9 @@ class PARTInterfaceBlind(PARTInterface):
return bytes.fromhex(lock_refund_swipe_tx_hex)
def getSpendableBalance(self):
return self.make_int(self.rpc_callback('getbalances')['mine']['blind_trusted'])
class PARTInterfaceAnon(PARTInterface):
@staticmethod
@ -712,3 +715,6 @@ class PARTInterfaceAnon(PARTInterface):
return {'txid': txid_hex, 'amount': 0, 'height': rv['height']}
return None
def getSpendableBalance(self):
return self.make_int(self.rpc_callback('getbalances')['mine']['anon_trusted'])

@ -26,7 +26,6 @@ from .util import (
ensure,
dumpj,
make_int,
format_amount,
TemporaryError)
from .rpc_xmr import (
make_xmr_rpc_func,
@ -142,8 +141,8 @@ class XMRInterface(CoinInterface):
rv = {}
self.rpc_wallet_cb('refresh')
balance_info = self.rpc_wallet_cb('get_balance')
rv['balance'] = format_amount(balance_info['unlocked_balance'], XMRInterface.exp())
rv['unconfirmed_balance'] = format_amount(balance_info['balance'] - balance_info['unlocked_balance'], XMRInterface.exp())
rv['balance'] = self.format_amount(balance_info['unlocked_balance'])
rv['unconfirmed_balance'] = self.format_amount(balance_info['balance'] - balance_info['unlocked_balance'])
return rv
def walletRestoreHeight(self):
@ -477,3 +476,11 @@ class XMRInterface(CoinInterface):
return rv
except Exception as e:
return {'error': str(e)}
def getSpendableBalance(self):
with self._mx_wallet:
self.rpc_wallet_cb('open_wallet', {'filename': self._wallet_filename})
self.rpc_wallet_cb('refresh')
balance_info = self.rpc_wallet_cb('get_balance')
return balance_info['unlocked_balance']

@ -107,7 +107,7 @@ def callrpc(rpc_port, auth, method, params=[], wallet=None, host='127.0.0.1'):
r = json.loads(v.decode('utf-8'))
except Exception as ex:
traceback.print_exc()
raise ValueError('RPC Server Error')
raise ValueError('RPC Server Error ' + str(ex))
if 'error' in r and r['error'] is not None:
raise ValueError('RPC error ' + str(r['error']))

Loading…
Cancel
Save