Implement withdrawing to wallets

This commit is contained in:
Mark Veidemanis 2022-01-25 18:39:35 +00:00
parent 49e02ce549
commit 7b5290a1e0
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
3 changed files with 66 additions and 9 deletions

View File

@ -620,11 +620,61 @@ class Agora(object):
"""
payload = {"tradeId": contact_id, "password": settings.Agora.Pass}
rtrn = self.agora._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
def withdraw_funds(self):
"""
Withdraw excess funds to our XMR/BTC wallets.
"""
# wallet_xmr =
pass
totals_all = self.tx.get_total()
wallet_xmr, _ = totals_all[2]
# Get the wallet balances in USD
total_usd = totals_all[0][1]
profit_usd = total_usd - float(settings.Money.BaseUSD)
# Get the XMR -> USD exchange rate
xmr_usd = self.cg.get_price(ids="monero", vs_currencies=["USD"])
# Convert the USD total to XMR
profit_usd_in_xmr = float(profit_usd) / xmr_usd["monero"]["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(
"Not enough funds to withdraw {profit}, as wallet only contains {wallet}", profit=profit_usd_in_xmr, wallet=wallet_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)
# Set up the format for calling wallet_send_xmr
send_cast = {
"address": None,
"amount": half_rounded,
"password": settings.Agora.Pass,
}
send_cast["address"] = settings.XMR.Wallet1
rtrn1 = self.agora.wallet_send_xmr(**send_cast)
send_cast["address"] = settings.XMR.Wallet2
rtrn2 = self.agora.wallet_send_xmr(**send_cast)
return (rtrn1["success"], rtrn2["success"])

View File

@ -992,7 +992,7 @@ class AgoraDesk:
address: str,
amount: float,
password: str,
fee_level: str,
# fee_level: str,
otp: Optional[int] = None,
) -> Dict[str, Any]:
"""See Agoradesk API.
@ -1005,7 +1005,7 @@ class AgoraDesk:
"address": address,
"amount": amount,
"password": password,
"fee_level": fee_level,
# "fee_level": fee_level,
}
if otp:
params["otp"] = otp

View File

@ -44,7 +44,13 @@ class Transactions(object):
ts = data["timestamp"]
inside = data["data"]
txid = inside["id"]
txtype = inside["type"]
if txtype == "card_payment":
self.log.info("Ignoring card payment: {id}", id=txid)
return
if "type" not in inside:
stored_trade = r.hgetall(f"tx.{txid}")
print("stored trade", stored_trade)
@ -74,10 +80,7 @@ class Transactions(object):
return
# If type not in inside and we haven't hit any more returns
return
txtype = inside["type"]
if txtype == "card_payment":
self.log.info("Ignoring card payment: {id}", id=txid)
return
state = inside["state"]
if "reference" in inside:
reference = inside["reference"]
@ -414,4 +417,8 @@ class Transactions(object):
price_usd = total_usd
price_gbp = rates["GBP"] * total_usd
return ((price_sek, price_usd, price_gbp), (total_usd_agora_xmr, total_usd_agora_btc))
return (
(price_sek, price_usd, price_gbp), # Total prices in our 3 favourite currencies
(total_usd_agora_xmr, total_usd_agora_btc), # Total USD balance in only Agora
(total_xmr_agora, total_btc_agora),
) # Total XMR and BTC balance in Agora