Implement getting total USD value of Revolut account

This commit is contained in:
Mark Veidemanis 2021-12-28 11:26:51 +00:00
parent 45abcfc7dc
commit 2f274390a8
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
4 changed files with 53 additions and 3 deletions

View File

@ -59,12 +59,14 @@ class Agora(object):
"""
Get information about our open trades.
Post new trades to IRC and cache trades for the future.
:return: human readable list of strings about our trades
:rtype: list
:return: human readable list of strings about our trades or False
:rtype: list or bool
"""
dash = self.agora.dashboard_seller()
dash_tmp = []
current_trades = []
if "data" not in dash["response"]:
return False
if dash["response"]["data"]["contact_count"] > 0:
for contact in dash["response"]["data"]["contact_list"]:
contact_id = contact["data"]["contact_id"]

View File

@ -62,8 +62,10 @@ if __name__ == "__main__":
# Define Revolut
revolut = Revolut()
# Pass IRC to Revolut
# Pass IRC to Revolut and Revolut to IRC
revolut.set_irc(irc)
irc.set_revolut(revolut)
revolut.set_agora(agora)
# Define Transactions
tx = Transactions()

View File

@ -39,6 +39,9 @@ class IRCBot(irc.IRCClient):
def set_agora(self, agora):
self.agora = agora
def set_revolut(self, revolut):
self.revolut = revolut
def parse(self, user, host, channel, msg):
"""
Simple handler for IRC commands.
@ -59,6 +62,8 @@ class IRCBot(irc.IRCClient):
if cmd == "trades" and host in self.admins:
# Get details of open trades and post on IRC
trades = self.agora.dashboard(send_irc=False)
if not trades:
self.msg(channel, "No open trades.")
for trade_id in trades:
self.msg(channel, trade_id)
@ -104,6 +109,16 @@ class IRCBot(irc.IRCClient):
else:
return dumps(rtrn)
elif cmd == "accounts" and host in self.admins:
accounts = self.revolut.accounts()
for account in accounts:
if account["balance"] > 0:
self.msg(channel, f"{account['name']} {account['currency']}: {account['balance']}")
elif cmd == "total" and host in self.admins:
total_usd = self.revolut.get_total_usd()
self.msg(channel, f"Total: {round(total_usd, 2)}USD")
def signedOn(self):
"""
Called when we have signed on to IRC.
@ -173,6 +188,9 @@ class IRCBotFactory(protocol.ClientFactory):
def set_agora(self, agora):
self.agora = agora
def set_revolut(self, revolut):
self.revolut = revolut
def buildProtocol(self, addr):
"""
Custom override for the Twisted buildProtocol so we can access the Protocol instance.
@ -182,6 +200,7 @@ class IRCBotFactory(protocol.ClientFactory):
prcol = IRCBot(self.log)
self.client = prcol
self.client.set_agora(self.agora)
self.client.set_revolut(self.revolut)
return prcol
def clientConnectionLost(self, connector, reason):

View File

@ -29,6 +29,9 @@ class Revolut(object):
def set_irc(self, irc):
self.irc = irc
def set_agora(self, agora):
self.agora = agora
def setup_auth(self):
"""
Function to create a new Java Web Token and use it to get a refresh/access token.
@ -169,3 +172,27 @@ class Revolut(object):
else:
self.log.error("Cannot get webhook: {content}", r.content)
return False
def accounts(self):
"""
Get the details and balances of all Revolut accounts.
:return: account details
:rtype: dict
"""
headers = {"Authorization": f"Bearer {self.token}"}
r = requests.get(f"{settings.Revolut.Base}/accounts", headers=headers)
if r.status_code == 200:
return r.json()
else:
return False
def get_total_usd(self):
rates = self.agora.get_rates_all()
accounts = self.accounts()
total_usd = 0
for account in accounts:
if account["currency"] == "USD":
total_usd += account["balance"]
else:
total_usd += account["balance"] / rates[account["currency"]]
return total_usd