pluto/handler/sources/localbitcoins.py

137 lines
3.8 KiB
Python

# Other library imports
import sources.local
import util
from pyotp import TOTP
# Project imports
from settings import settings
class LBTC(sources.local.Local):
"""
LocalBitcoins API handler.
"""
def __init__(self):
"""
Initialise the LocalBitcoins API.
Initialise the last_dash storage for detecting new trades.
"""
self.platform = "lbtc"
super().__init__()
# Cache for detecting new trades
self.last_dash = set()
# Cache for detecting new messages
self.last_messages = {}
# Assets that cheat has been run on
self.cheat_run_on = []
@util.handle_exceptions
def release_funds(self, contact_id):
"""
Release funds for a contact_id.
:param contact_id: trade/contact ID
:type contact_id: string
:return: response dict
:rtype: dict
"""
if self.sets.Dummy == "1":
self.log.error(
f"Running in dummy mode, not releasing funds for {contact_id}"
)
return
payload = {
"tradeId": contact_id,
"password": self.sets.Pass,
}
rtrn = self.api._api_call(
api_method=f"contact_release/{contact_id}",
http_method="POST",
query_values=payload,
)
# Check if we can withdraw funds
self.withdraw_funds()
return rtrn
# TODO: rewrite to handle BTC
@util.handle_exceptions
def withdraw_funds(self):
"""
Withdraw excess funds to our XMR wallets.
"""
totals_all = self.money.get_total()
if totals_all is False:
return False
wallet_xmr, _ = totals_all[2]
# Get the wallet balances in USD
total_usd = totals_all[0][1]
# total_trades_usd = self.tx.get_open_trades_usd()
if not total_usd:
return False
# total_usd += total_trades_usd
profit_usd = total_usd - float(settings.Money.BaseUSD)
# Get the XMR -> USD exchange rate
xmr_usd = self.money.cg.get_price(ids="bitcoin", vs_currencies=["USD"])
# Convert the USD total to XMR
profit_usd_in_xmr = float(profit_usd) / xmr_usd["bitcoin"]["usd"]
# Check profit is above zero
if not profit_usd >= 0:
return
if not float(wallet_xmr) > profit_usd_in_xmr:
# Not enough funds to withdraw
self.log.error(
f"Not enough funds to withdraw {profit_usd_in_xmr}, as wallet only contains {wallet_xmr}"
)
self.irc.sendmsg(
f"Not enough funds to withdraw {profit_usd_in_xmr}, as wallet only contains {wallet_xmr}"
)
self.ux.notify.notify_need_topup(profit_usd_in_xmr)
return
if not profit_usd >= float(settings.Money.WithdrawLimit):
# Not enough profit to withdraw
return
half = profit_usd_in_xmr / 2
half_rounded = round(half, 8)
# Read OTP secret
with open("otp.key", "r") as f:
otp_key = f.read()
f.close()
otp_key = otp_key.replace("\n", "")
# Get OTP code
otp_code = TOTP(otp_key)
# Set up the format for calling wallet_send_xmr
send_cast = {
"address": None,
"amount": half_rounded,
"password": settings.LocalBitcoins.Pass,
"otp": otp_code.now(),
}
send_cast["address"] = settings.XMR.Wallet1
rtrn1 = self.api.wallet_send_xmr(**send_cast)
send_cast["address"] = settings.XMR.Wallet2
rtrn2 = self.api.wallet_send_xmr(**send_cast)
self.irc.sendmsg(f"Withdrawal: {rtrn1['success']} | {rtrn2['success']}")
self.ux.notify.notify_withdrawal(half_rounded)