-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
172 lines (152 loc) · 7.01 KB
/
main.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
import random
from algosdk import account
from algosdk.v2client import algod
from numpy.core.defchararray import strip
from constants import Constants, get_env
from helpers import algo_helper
from models.Trip import Trip
from utilities import utils
def read_state(algod_client, app_id, user_private_key=None, show_debug=False):
"""
Get the dApp global state / local state
:param algod_client:
:param app_id:
:param user_private_key:
:param show_debug:
"""
if app_id is None:
utils.console_log("Invalid app_id")
return False
local_state = None
if user_private_key is not None:
# read local state of application
local_state = algo_helper.read_local_state(algod_client,
account.address_from_private_key(user_private_key),
app_id),
# read global state of application
global_state, creator, approval_program, clear_state_program = algo_helper.read_global_state(client=algod_client,
app_id=app_id,
to_array=False,
show=False)
escrow_address = algo_helper.BytesToAddress(global_state.get('escrow_address'))
utils.console_log("App id: {}".format(app_id), 'blue')
utils.console_log("Global State:", 'blue')
print(utils.toArray(global_state))
if local_state is not None:
utils.console_log("Local State:", 'blue')
print(utils.toArray(local_state))
utils.console_log("Approval Program:", 'blue')
print(approval_program)
utils.console_log("Clear State Program:", 'blue')
print(clear_state_program)
utils.console_log("Creator Address:", 'blue')
print(creator)
utils.console_log("Escrow Address:", 'blue')
print(escrow_address)
utils.console_log("Escrow Info:", 'blue')
print(algod_client.account_info(escrow_address))
if show_debug:
utils.console_log("Application Info:", 'blue')
app_info = algod_client.application_info(app_id)
utils.parse_response(app_info)
def get_test_user(user_list, ask_selection=True):
"""
Select a test user account from given user list
:param user_list:
:param ask_selection: if True, ask user for selection, otherwise select the user randomly
"""
if ask_selection:
print('With which user?')
for i in range(0, len(user_list)):
print('{}) {}'.format(i, user_list[i].get('name')))
y = int(strip(input()))
if y <= 0 or y > len(user_list):
y = 0
else:
y = random.randint(0, len(user_list) - 1)
return user_list[y]
def main():
# define private keys
creator_private_key = algo_helper.get_private_key_from_mnemonic(Constants.creator_mnemonic)
algod_client = algod.AlgodClient(Constants.algod_token, Constants.algod_address)
app_id = int(get_env('APP_ID'))
accounts = Constants.accounts
carsharing_trip = Trip(algod_client=algod_client, app_id=app_id)
# ------- trip info ---------
trip_creator_name = "Test"
trip_start_add = "Mestre"
trip_end_add = "Milano"
trip_start_date = "2022-05-10 10:30"
trip_end_date = "2022-05-30 21:00"
trip_cost = 5000
trip_seats = 3
# ---------------------------
color = 'blue'
x = 1
while x != 0:
utils.console_log("--------------------------------------------", color)
utils.console_log('What do you want to do?', color)
utils.console_log('1) Create Trip', color)
utils.console_log('2) Participate', color)
utils.console_log('3) Cancel Participation', color)
utils.console_log('4) Start Trip', color)
utils.console_log('5) Update Trip', color)
utils.console_log('6) Delete Trip', color)
utils.console_log('7) Get Trip State', color)
utils.console_log("--------------------------------------------", color)
x = int(strip(input()))
if x == 1:
carsharing_trip.create_app(creator_private_key=creator_private_key,
trip_creator_name=trip_creator_name,
trip_start_address=trip_start_add,
trip_end_address=trip_end_add,
trip_start_date=trip_start_date,
trip_end_date=trip_end_date,
trip_cost=trip_cost,
trip_available_seats=trip_seats)
carsharing_trip.initialize_escrow(creator_private_key)
carsharing_trip.fund_escrow(creator_private_key)
elif x == 2:
if carsharing_trip.app_id is None:
utils.console_log("Invalid app_id")
continue
test_user = get_test_user(accounts, True)
test_user_pk = algo_helper.get_private_key_from_mnemonic(test_user.get('mnemonic'))
carsharing_trip.participate(test_user_pk, test_user.get('name'))
elif x == 3:
if carsharing_trip.app_id is None:
utils.console_log("Invalid app_id")
test_user = get_test_user(accounts, True)
test_user_pk = algo_helper.get_private_key_from_mnemonic(test_user.get('mnemonic'))
carsharing_trip.cancel_participation(creator_private_key, test_user_pk, test_user.get('name'))
elif x == 4:
if carsharing_trip.app_id is None:
utils.console_log("Invalid app_id")
continue
carsharing_trip.start_trip(creator_private_key)
elif x == 5:
if carsharing_trip.app_id is None:
utils.console_log("Invalid app_id")
continue
carsharing_trip.update_trip_info(creator_private_key=creator_private_key,
trip_creator_name=trip_creator_name,
trip_start_address=trip_start_add,
trip_end_address=trip_end_add,
trip_start_date=trip_start_date,
trip_end_date=trip_end_date,
trip_cost=trip_cost,
trip_available_seats=trip_seats)
elif x == 6:
utils.console_log("Are you sure?", 'red')
y = strip(input())
if y != "y" and y != "yes":
continue
if carsharing_trip.app_id is None:
utils.console_log("Invalid app_id")
carsharing_trip.close_trip(creator_private_key, accounts)
elif x == 7:
read_state(algod_client, carsharing_trip.app_id, show_debug=False)
else:
print("Exiting..")
if __name__ == "__main__":
main()