-
Notifications
You must be signed in to change notification settings - Fork 1
/
app_route.py
226 lines (195 loc) · 8.44 KB
/
app_route.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
from flask import request, make_response
from dotenv import load_dotenv
from datetime import datetime
from datetime import timezone
from icecream import ic
from os import getenv
from optimization.performance import *
from feedback.message import *
from validator_api.coinmarketcap import *
from validator_api.coinbase import *
from validator_api.plaid import *
from support.score import *
from app import *
load_dotenv()
def create_feedback_plaid():
return {'fetch': {}, 'credit': {}, 'velocity': {}, 'stability': {}, 'diversity': {}}
def create_feedback_coinbase():
return {'kyc': {}, 'history': {}, 'liquidity': {}, 'activity': {}}
# @measure_time_and_memory
@app.route('/credit_score/plaid', methods=['POST'])
def credit_score_plaid():
if request.method == 'POST':
try:
keplr_token = request.json.get('keplr_token', None)
plaid_token = request.json.get('plaid_token', None)
plaid_client_id = request.json.get('plaid_client_id', None)
plaid_client_secret = request.json.get('plaid_client_secret', None)
coinmarketcap_key = request.json.get('coinmarketcap_key', None)
except Exception as e:
timestamp = datetime.now(timezone.utc).strftime(
'%m-%d-%Y %H:%M:%S GMT')
output = {
'endpoint': '/credit_score/plaid',
'title': 'Credit Score',
'status_code': 400,
'status': 'error',
'timestamp': timestamp,
'message': str(e)
}
ic(output)
return make_response(output, output['status_code'])
try:
# client connection
client = plaid_client(
getenv('ENV'), plaid_client_id, plaid_client_secret)
ic(client)
# data fetching and formatting
plaid_txn = plaid_transactions(plaid_token, client, 360)
if 'error' in plaid_txn:
raise Exception(plaid_txn['error']['message'])
plaid_txn = {k: v for k, v in plaid_txn.items(
) if k in ['accounts', 'item', 'transactions']}
plaid_txn['transactions'] = [
t for t in plaid_txn['transactions'] if not t['pending']]
# compute score
feedback = create_feedback_plaid()
feedback = plaid_bank_name(
client, plaid_txn['item']['institution_id'], feedback)
score, feedback = plaid_score(plaid_txn, feedback)
message = qualitative_feedback_plaid(
score, feedback, coinmarketcap_key)
feedback = interpret_score_plaid(score, feedback)
status_code = 200
status = 'success'
except Exception as e:
status_code = 400
status = 'error'
score = 0
feedback = {}
message = str(e)
finally:
timestamp = datetime.now(timezone.utc).strftime(
'%m-%d-%Y %H:%M:%S GMT')
output = {
'endpoint': '/credit_score/plaid',
'title': 'Credit Score',
'status_code': status_code,
'status': status,
'timestamp': timestamp,
'score': int(score),
'feedback': feedback,
'message': message
}
if score == 0:
output.pop('score', None)
output.pop('feedback', None)
ic(output)
return make_response(output, output['status_code'])
# @measure_time_and_memory
@app.route('/credit_score/coinbase', methods=['POST'])
def credit_score_coinbase():
if request.method == 'POST':
try:
keplr_token = request.json.get('keplr_token', None)
coinbase_access_token = request.json.get(
'coinbase_access_token', None)
coinbase_refresh_token = request.json.get(
'coinbase_refresh_token', None)
coinmarketcap_key = request.json.get('coinmarketcap_key', None)
except Exception as e:
timestamp = datetime.now(timezone.utc).strftime(
'%m-%d-%Y %H:%M:%S GMT')
output = {
'endpoint': '/credit_score/coinbase',
'title': 'Credit Score',
'status_code': 400,
'status': 'error',
'timestamp': timestamp,
'message': str(e)
}
ic(output)
return make_response(output, output['status_code'])
try:
# client connection
client = coinbase_client(
coinbase_access_token, coinbase_refresh_token)
ic(client)
# coinmarketcap
# fetch top X cryptos from coinmarketcap API
top_coins = coinmarketcap_coins(coinmarketcap_key, 25)
ic(top_coins)
currencies = coinbase_currencies(client)
ic(currencies)
if 'error' in currencies:
raise Exception(currencies['error']['message'])
odd_fiats = ['BHD', 'BIF', 'BYR', 'CLP', 'DJF', 'GNF', 'HUF', 'IQD', 'ISK', 'JOD', 'JPY', 'KMF', 'KRW',
'KWD', 'LYD', 'MGA', 'MRO', 'OMR', 'PYG', 'RWF', 'TND', 'UGX', 'VND', 'VUV', 'XAF', 'XOF', 'XPF']
currencies = {k: 1 for (k, v) in currencies.items()
if v == 0.01 or k in odd_fiats}
top_coins.update(currencies)
coins = list(top_coins.keys())
# change coinbase native currency to USD
native = coinbase_native_currency(client)
ic(native)
if 'error' in native:
raise Exception(native['error']['message'])
if native != 'USD':
set_native = coinbase_set_native_currency(client, 'USD')
ic(set_native)
# fetch and format data from user's Coinbase account
coinbase_acc = coinbase_accounts(client)
if 'error' in coinbase_acc:
raise Exception(coinbase_acc['error']['message'])
coinbase_acc = [n for n in coinbase_acc if n['currency'] in coins]
coinbase_txn = [coinbase_transactions(
client, n['id']) for n in coinbase_acc]
coinbase_txn = [x for n in coinbase_txn for x in n]
# keep only certain transaction types
txn_types = ['fiat_deposit', 'request', 'buy',
'fiat_withdrawal', 'vault_withdrawal', 'sell', 'send']
coinbase_txn = [n for n in coinbase_txn if n['status']
== 'completed' and n['type'] in txn_types]
for d in coinbase_txn:
# If the txn is of 'send' type and is a credit, then relabel its type to 'send_credit'
if d['type'] == 'send' and np.sign(float(d['amount']['amount'])) == 1:
d['type'] = 'send_credit'
# If the txn is of 'send' type and is a debit, then relabel its type to 'send_debit'
elif d['type'] == 'send' and np.sign(float(d['amount']['amount'])) == -1:
d['type'] = 'send_debit'
# reset native currency
set_native = coinbase_set_native_currency(client, native)
ic(set_native)
# compute score
feedback = create_feedback_coinbase()
score, feedback = coinbase_score(
coinbase_acc, coinbase_txn, feedback)
message = qualitative_feedback_coinbase(
score, feedback, coinmarketcap_key)
feedback = interpret_score_coinbase(score, feedback)
status_code = 200
status = 'success'
except Exception as e:
status_code = 400
status = 'error'
score = 0
feedback = {}
message = str(e)
finally:
timestamp = datetime.now(timezone.utc).strftime(
'%m-%d-%Y %H:%M:%S GMT')
output = {
'endpoint': '/credit_score/coinbase',
'title': 'Credit Score',
'status_code': status_code,
'status': status,
'timestamp': timestamp,
'score': int(score),
'feedback': feedback,
'message': message
}
if score == 0:
output.pop('score', None)
output.pop('feedback', None)
ic(output)
return make_response(output, output['status_code'])