-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmoneybot.py
More file actions
executable file
·121 lines (93 loc) · 3.86 KB
/
moneybot.py
File metadata and controls
executable file
·121 lines (93 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#! /Users/joao/testes/virt_env/moneybot/bin/python
# -*- coding: utf-8 -*-
import sys
from functools import reduce
from decimal import Decimal
from datetime import datetime
from time import sleep
from bitreserve import Bitreserve
from config import auth, weights
MINIMUM_TRANSACTION = Decimal('0.001')
DEBUG = False
class MoneyBot(object):
def __init__(self, sandbox=False):
self.normalize_weights()
if DEBUG:
print(self.weights)
self.auth(sandbox)
def normalize_weights(self):
total = 0.0
for value in weights.values():
total += value
self.weights = {}
for key, value in weights.items():
self.weights[key] = value / total
def auth(self, sandbox=False):
if sandbox:
self.api = Bitreserve(host='api-sandbox.bitreserve.org')
else:
self.api = Bitreserve()
pat = auth.get('pat', None)
user = auth.get('user', None)
password = auth.get('password', None)
if pat:
self.api.auth_pat(pat)
elif user and password:
self.api.auth(user, password)
def update_card_information(self):
me = self.api.get_me()
cards = me['cards']
self.cards = {}
for card in cards:
if Decimal(card['balance']) or card['currency'] in self.weights.keys():
self.cards[card['id']] = {
'id': card['id'],
'currency': card['currency'],
'address': card['addresses'][0]['id'],
'balance': Decimal(card['normalized'][0]['balance']),
}
self.currency = card['normalized'][0]['currency']
total = reduce(lambda total, card: total + card['balance'], self.cards.values(), Decimal('0.0'))
if DEBUG:
print('total: {}'.format(total))
for card_id, card in self.cards.items():
target = total * Decimal(self.weights[card['currency']]) # this assumes 1 card per currency
if DEBUG:
print('target: {}'.format(target))
self.cards[card_id]['difference'] = target - card['balance']
if DEBUG:
print(self.cards)
def calculate_next_transaction(self):
def difference(card_id):
return self.cards[card_id]['difference']
potential_sources = list(filter(lambda cid: difference(cid) <= -MINIMUM_TRANSACTION, self.cards.keys()))
potential_destinations = list(filter(lambda cid: difference(cid) >= MINIMUM_TRANSACTION, self.cards.keys()))
if not (len(potential_sources) and len(potential_destinations)):
return None, None, None
potential_sources.sort(key=difference, reverse=True)
potential_destinations.sort(key=difference, reverse=True)
if self.cards[potential_sources[0]] == self.cards[potential_destinations[0]]:
return None, None, None
value = min(
abs(self.cards[potential_sources[0]]['difference']),
abs(self.cards[potential_destinations[0]]['difference'])
)
value = "{0:.3f}".format(value)
return (
self.cards[potential_sources[0]],
self.cards[potential_destinations[0]],
value
)
def run(self):
self.update_card_information()
source, destination, amount = self.calculate_next_transaction()
if not source:
# print('nothing to do')
return
print('Transfer {} {} from {} to {}.'.format(amount, self.currency, source['address'], destination['address']))
trans = self.api.prepare_txn(source['id'], destination['address'], amount, self.currency)
res = self.api.execute_txn(source['id'], trans)
print(res['id'])
if __name__ == '__main__':
scrooge = MoneyBot()
scrooge.run()