-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
178 lines (153 loc) · 5.69 KB
/
client.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
import socket
import json
import requests
from initialize import boot
from pyfiglet import Figlet
from argparse import ArgumentParser
from PyInquirer import Token,prompt,style_from_dict,Separator
f = Figlet(font='slant')
print (f.renderText('Noobcash'))
style = style_from_dict({
Token.Separator: '#CC5454 bold',
Token.QuestionMark: '#673AB7 bold',
Token.Selected: '#CC5454 bold', # default
Token.Pointer: '#673AB7 bold',
Token.Instruction: '', # default
Token.Answer: '#f44336 bold',
Token.Question: '',
})
def application():
port = [{
'type' : 'input',
'name' : 'port',
'message' : "What port of the node is the service using ?"
}]
hostname = socket.gethostname()
ip = socket.gethostbyname(hostname)
port = str(prompt(port,style=style)['port'])
#print(hostname)
#print(ip)
#print(port)
exit = True
while exit:
if not boot:
actions = [{
'type' : 'checkbox',
'message' : 'What do you want to do',
'name' : 'actions',
'choices' : [
Separator('\n<============ Actions ============>\n'),
{
'name':'Create a new transaction'
},
{
'name':'View last transactions'
},
{
'name':'Show wallet balance'
},
{
'name':'Help'
},
{
'name':'Terminate client'
}
]
}]
else:
actions = [{
'type' : 'checkbox',
'message' : 'What do you want to do',
'name' : 'actions',
'choices' : [
Separator('\n<============ Actions ============>\n'),
{
'name':'Create a new transaction'
},
{
'name':'View last transactions'
},
{
'name':'Show wallet balance'
},
{
'name':'Help'
},
{
'name':'5 nodes experiment'
},
{
'name':'10 nodes experiment'
},
{
'name':"Dummy experiment"
},
{
'name':'Terminate client'
}
]
}]
action = prompt(actions,style=style)['actions']
if action[0] == 'Show wallet balance':
address = 'http://' + ip + ':' + port + '/money'
response = requests.get(address)
if response.status_code == 200:
print("Current balance : " + str(response.json()['message']))
else:
print("Something went wrong")
if action[0] == 'View last transactions':
address = 'http://' + ip + ':' + port + "/last_transactions"
response = requests.get(address)
if response.status_code == 200:
print("Successfully retrieved the transactions of the node's most recent block.")
for line in response.json():
print(line)
else:
print("Something went wrong")
if action[0] == 'Create a new transaction':
form = [
{
'type':'input',
'name': 'id',
'message': 'Select id'
},
{
'type':'input',
'name': 'amount',
'message': 'Select amount'
}
]
content = prompt(form)
#print(type(content['id']))
#print(type(content['amount']))
address = 'http://' + ip + ':' + port + '/transaction'
response = requests.post(address,json=content)
if response.status_code == 200:
print('Transaction completed')
if response.status_code == 400:
print("Couldnt create transaction")
if action[0] == 'Help':
print("\n* Creating a new transaction will require from you to set a receiver and the amount to be sent.")
print("\n* Viewing the last transactions will present to you the transactions of the latest block in the chain.")
print("\n* Choosing the show wallet balance option will print how many coins are left in the wallet of the node.")
print("\n* If you wish to terminate the app select the corresponding option.\n")
if action[0] == '5 nodes experiment':
address = 'http://' + ip + ':' + port + '/five_nodes'
response = requests.get(address)
if response.status_code == 200:
print("OK")
if action[0] == '10 nodes experiment':
address = 'http://' + ip + ':' + port + '/ten_nodes'
response = requests.get(address)
if response.status_code == 200:
print("OK")
if action[0] == 'Dummy experiment':
address = 'http://' + ip + ':' + port + '/temp'
response = requests.get(address)
if response.status_code == 200:
print("OK")
if action[0] == 'Terminate client':
exit = False
if __name__ == "__main__":
parser = ArgumentParser(description="cli application")
application()