This repository was archived by the owner on May 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
101 lines (77 loc) · 2.7 KB
/
main.py
File metadata and controls
101 lines (77 loc) · 2.7 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
import json
import os
from flask import Flask, request
from flask_cors import CORS
import kin
from kin import errors as kin_errors
from stellar_base.network import NETWORKS
from stellar_base.asset import Asset
app = Flask(__name__)
CORS(app)
@app.route("/status")
def status():
# Return sdk status in json, if fails, returns the exception
try:
return json.dumps(sdk.get_status()), 200
except Exception as e:
response = {'error': 'unexpected error: ' + str(e)}
return json.dumps(response), 500
@app.route("/fund")
def fund():
# Funds an account with <amount> kin , if fails, returns the corresponding error message
destination = request.args.get('account')
amount = request.args.get('amount')
if destination is None:
return make_reply(400, 'Account parameter missing')
if amount is None:
return make_reply(400, 'Amount parameter missing')
# Verify amount is a number
try:
amount = float(amount)
except ValueError:
return make_reply(400, 'Invalid amount')
except Exception as e:
return make_reply(500, 'unexpected error: {}'.format(str(e)))
# Fund the account
try:
sdk.send_kin(destination, amount)
return make_reply(200)
# If the account is not created yet
except kin_errors.AccountNotFoundError:
return make_reply(400, 'Account does not exist')
# If the account has no trustline
except kin_errors.AccountNotActivatedError:
return make_reply(400, 'No KIN trustline established')
except Exception as e:
if 'invalid address' in str(e):
return make_reply(400, 'Invalid address')
# If i get an unexpected error, return it
else:
return make_reply(500, 'unexpected error: {}'.format(str(e)))
def make_reply(status, error=None):
# Build json reply and return it + HTTP code
success = True if status == 200 else False
reply = {'success': success,
'error': error}
return json.dumps(reply), status
def get_seeds():
with open('seeds.txt','r') as myfile:
seeds = myfile.read().splitlines()
channels = None
primary = seeds[0]
if len(seeds) > 1:
channels = []
for i in range(1,len(seeds)):
channels.append(seeds[i])
return primary,channels
def main():
global sdk
primary, channels = get_seeds()
NETWORKS['CUSTOM'] = os.environ['NETWORK_PASSPHRASE']
sdk = kin.SDK(network='CUSTOM',
secret_key=primary,
channel_secret_keys=channels,
horizon_endpoint_uri=os.environ['HORIZON_ENDPOINT'],
kin_asset=Asset('KIN',os.environ['KIN_ISSUER']))
if __name__ == 'main':
main()