This repository has been archived by the owner on Dec 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.py
231 lines (185 loc) · 7.11 KB
/
app.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
227
228
229
230
231
from flask import Flask, jsonify, request
from flask_cors import CORS
from core.acedb import AceDB
from core.pythaces import Pythaces
from core.contracts import Contract
from core.conversion import Conversion
from core.util import parse_config, get_network
import requests
import time
import os.path
atomic = 100000000
app = Flask(__name__)
CORS(app)
@app.route("/api/<convCoin>", methods=['POST'])
def crypto(convCoin):
acesdb = AceDB(coin['channel']['dbusername'])
try:
# get send/receive addresses and amouunt
req_data = request.get_json()
send = req_data['send']
receive = req_data["receive"]
amount = req_data["amount"]*atomic
# do validations
c1, c1_msg = validate_addresses(convCoin,send,receive)
c2, c2_msg = validate_amount(convCoin,amount)
a_check = all([c1, c2])
if a_check == True:
channel = coin['channel']['channel']
# calculate value
f = {'flatFee': coin['channel']['flat_fee'],
'pctFee': coin['channel']['pct_fee']}
c = Contract()
send_amount, total_fee = c.pricing(channel,convCoin, amount, f)
ts = int(time.time())
c.create(ts, send, send_amount, receive, amount, total_fee, channel, convCoin)
new_contract = (c.contract,)
acesdb.storeContracts(new_contract)
address = coin['channel']['service_acct']
amt = send_amount / atomic
vendorfield = c.uid
qr_dict={"success":True,"channel":channel,"address":address,"amount":amt, "vendorField":vendorfield, "receive":(amount/atomic)}
#return json to client (address, amount, vendorfield)
return jsonify(qr_dict)
else:
if c1:
msg = c2_msg
else:
msg = c1_msg
return jsonify(msg)
except Exception as e:
print(e)
error ={"success":False, "msg":"Incorrect Entry"}
return jsonify(Error=error)
#display all prices
@app.route("/api/prices")
def prices():
try:
conversion_rates = {}
channel = coin['channel']['channel']
# get conversion rates
for key in coin:
if key != "channel":
cnv = Conversion(coin['channel']['channel'], key)
conversion_rates[key] = cnv.conversion_rate()
# get fees
feeDict = {
"flatFee": coin['channel']['flat_fee'],
"percentFee": coin['channel']['pct_fee']*100
}
priceDict = {
"channel": channel,
"prices": conversion_rates,
"fees": feeDict}
return jsonify(priceDict)
except Exception as e:
print(e)
error ={"success":False, "msg":"Prices not available"}
return jsonify(Error=error)
def contract_to_json(c):
convert={"contract":c[0],
"createdOnTimestamp":c[1],
"sendingAddress":c[2],
"sendingAmount":c[3]/atomic,
"receivingAddress":c[4],
"receivingAmount":c[5]/atomic,
"totalFees":c[6]/atomic,
"contractStatus":c[7],
"processedOnTimestamp":c[10],
"channel": c[8],
"coin": c[9],
"serviceAccount": coin['channel']['service_acct']}
return convert
#Change this to just send contracts
@app.route("/api/contracts")
def contracts():
acesdb = AceDB(coin['channel']['dbusername'])
filtered_contracts=[]
all_contracts = acesdb.contracts().fetchall()
for i in all_contracts:
if i[7] != "Expired":
filter_tmp = contract_to_json(i)
filtered_contracts.append(filter_tmp)
return jsonify(filtered_contracts)
@app.route("/api/history/<id>")
def get_contract(id):
acesdb = AceDB(coin['channel']['dbusername'])
#contract_id = acesdb.singleContract(id).fetchall()
all_contracts = acesdb.contracts().fetchall()
for i in all_contracts:
if i[0] == id:
contract_id = i
jsoned = contract_to_json(contract_id)
return jsonify(jsoned)
#get all capacity
@app.route("/api/capacity")
def capacity():
acesdb = AceDB(coin['channel']['dbusername'])
try:
service_availability = {}
contracts = acesdb.unprocessedContracts().fetchall()
for key in coin:
if key != 'channel':
pythaces = Pythaces(fx_coins[key], atomic)
# total capacity
capacity = pythaces.service_capacity(coin[key]['service_acct'])
# reserve capacity
reserved = pythaces.reserve_capacity(contracts, coin[key]['service_acct'])
#available capacity
available = pythaces.available_capacity()
service_availability[key] = {"totalCapacity": capacity,
"reservedCapacity": reserved,
"availableCapacity": available}
return jsonify(service_availability)
except Exception as e:
print(e)
error ={"success":False, "msg":"Capacity is not available"}
return jsonify(Error=error)
def validate_amount(c,amount):
# check against limit
url = "http://"+coin['channel']['channel_ip']+"/api/capacity"
r = requests.get(url)
avail_cap = r.json()[c]["availableCapacity"] - atomic
limit = 100*atomic
if amount <= limit:
if amount < avail_cap:
flag = True
m = amount
else:
flag = False
m = "amount is currently over capacity"
else:
flag = False
m = "amount is currently over capacity or limit"
msg = {"success":flag, "msg":m}
return flag, msg
def validate_addresses(c, a_addr, b_addr):
a_len=len(a_addr)
b_len=len(b_addr)
# set response to invalid
flag = False
msg = {"success":flag, "msg":"one or both addresses invalid, please double check"}
# ark check
if any([a_addr[0].isdigit(),b_addr[0].isdigit()]):
# temp disable checks for non ark dpos coins
flag = True
msg = {"success": flag}
else:
# check that address is valid address length and network
a_check = coin['channel']["service_acct"][0]
b_check = coin[c]["addr_start"]
all_check= all([a_len==34,a_addr[0]==a_check,b_len==34,b_addr[0]==b_check])
if all_check:
# both addresses are good, flip flag and message
flag = True
msg = {"success": flag}
return flag, msg
if __name__ == "__main__":
# get config data
network, coin = parse_config()
#initialize park objects for use
fx_coins = {}
for key in coin:
if key != "channel":
fx_coins[key] = get_network(key, network, coin[key]['relay_ip'])
app.run(threaded=True)