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} 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) 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 return rtrn
def withdraw_funds(self): def withdraw_funds(self):
""" """
Withdraw excess funds to our XMR/BTC wallets. Withdraw excess funds to our XMR/BTC wallets.
""" """
# wallet_xmr = totals_all = self.tx.get_total()
pass
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, address: str,
amount: float, amount: float,
password: str, password: str,
fee_level: str, # fee_level: str,
otp: Optional[int] = None, otp: Optional[int] = None,
) -> Dict[str, Any]: ) -> Dict[str, Any]:
"""See Agoradesk API. """See Agoradesk API.
@ -1005,7 +1005,7 @@ class AgoraDesk:
"address": address, "address": address,
"amount": amount, "amount": amount,
"password": password, "password": password,
"fee_level": fee_level, # "fee_level": fee_level,
} }
if otp: if otp:
params["otp"] = otp params["otp"] = otp

View File

@ -44,7 +44,13 @@ class Transactions(object):
ts = data["timestamp"] ts = data["timestamp"]
inside = data["data"] inside = data["data"]
txid = inside["id"] 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: if "type" not in inside:
stored_trade = r.hgetall(f"tx.{txid}") stored_trade = r.hgetall(f"tx.{txid}")
print("stored trade", stored_trade) print("stored trade", stored_trade)
@ -74,10 +80,7 @@ class Transactions(object):
return return
# If type not in inside and we haven't hit any more returns # If type not in inside and we haven't hit any more returns
return return
txtype = inside["type"]
if txtype == "card_payment":
self.log.info("Ignoring card payment: {id}", id=txid)
return
state = inside["state"] state = inside["state"]
if "reference" in inside: if "reference" in inside:
reference = inside["reference"] reference = inside["reference"]
@ -414,4 +417,8 @@ class Transactions(object):
price_usd = total_usd price_usd = total_usd
price_gbp = rates["GBP"] * 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