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. Get information about our open trades.
Post new trades to IRC and cache trades for the future. Post new trades to IRC and cache trades for the future.
:return: human readable list of strings about our trades :return: human readable list of strings about our trades or False
:rtype: list :rtype: list or bool
""" """
dash = self.agora.dashboard_seller() dash = self.agora.dashboard_seller()
dash_tmp = [] dash_tmp = []
current_trades = [] current_trades = []
if "data" not in dash["response"]:
return False
if dash["response"]["data"]["contact_count"] > 0: if dash["response"]["data"]["contact_count"] > 0:
for contact in dash["response"]["data"]["contact_list"]: for contact in dash["response"]["data"]["contact_list"]:
contact_id = contact["data"]["contact_id"] contact_id = contact["data"]["contact_id"]

View File

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

View File

@ -39,6 +39,9 @@ class IRCBot(irc.IRCClient):
def set_agora(self, agora): def set_agora(self, agora):
self.agora = agora self.agora = agora
def set_revolut(self, revolut):
self.revolut = revolut
def parse(self, user, host, channel, msg): def parse(self, user, host, channel, msg):
""" """
Simple handler for IRC commands. Simple handler for IRC commands.
@ -59,6 +62,8 @@ class IRCBot(irc.IRCClient):
if cmd == "trades" and host in self.admins: if cmd == "trades" and host in self.admins:
# Get details of open trades and post on IRC # Get details of open trades and post on IRC
trades = self.agora.dashboard(send_irc=False) trades = self.agora.dashboard(send_irc=False)
if not trades:
self.msg(channel, "No open trades.")
for trade_id in trades: for trade_id in trades:
self.msg(channel, trade_id) self.msg(channel, trade_id)
@ -104,6 +109,16 @@ class IRCBot(irc.IRCClient):
else: else:
return dumps(rtrn) 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): def signedOn(self):
""" """
Called when we have signed on to IRC. Called when we have signed on to IRC.
@ -173,6 +188,9 @@ class IRCBotFactory(protocol.ClientFactory):
def set_agora(self, agora): def set_agora(self, agora):
self.agora = agora self.agora = agora
def set_revolut(self, revolut):
self.revolut = revolut
def buildProtocol(self, addr): def buildProtocol(self, addr):
""" """
Custom override for the Twisted buildProtocol so we can access the Protocol instance. 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) prcol = IRCBot(self.log)
self.client = prcol self.client = prcol
self.client.set_agora(self.agora) self.client.set_agora(self.agora)
self.client.set_revolut(self.revolut)
return prcol return prcol
def clientConnectionLost(self, connector, reason): def clientConnectionLost(self, connector, reason):

View File

@ -29,6 +29,9 @@ class Revolut(object):
def set_irc(self, irc): def set_irc(self, irc):
self.irc = irc self.irc = irc
def set_agora(self, agora):
self.agora = agora
def setup_auth(self): def setup_auth(self):
""" """
Function to create a new Java Web Token and use it to get a refresh/access token. 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: else:
self.log.error("Cannot get webhook: {content}", r.content) self.log.error("Cannot get webhook: {content}", r.content)
return False 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