-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path03_booking_and_completion_simulation.py
305 lines (250 loc) · 11.1 KB
/
03_booking_and_completion_simulation.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
from brownie import accounts, config
from web3 import Web3
from decimal import Decimal
# web3 connection
# w3 = Web3(Web3.WebsocketProvider(config["provider"]["websockets"]))
w3 = Web3(Web3.HTTPProvider(config["provider"]["http"]))
# initialising the contract instance
with open("./contract_data/address.txt") as file_a:
contract_address = file_a.read()
with open("./contract_data/abi.json") as file_b:
contract_abi = file_b.read()
contract_instance = w3.eth.contract(address=contract_address, abi=contract_abi)
# initialising the amWMATIC erc20 token
with open("./contract_data/erc20_abi.json") as file_c:
a_dai_abi = file_c.read()
am_wmatic_token = w3.eth.contract(
address="0xF45444171435d0aCB08a8af493837eF18e86EE27", abi=a_dai_abi
)
# initialising regular WMATIC erc20 token
with open("./contract_data/erc20_abi.json") as file_d:
erc20_abi = file_d.read()
wmatic_erc20_token = w3.eth.contract(
address="0x9c3C9283D3e44854697Cd22D3Faa240Cfb032889", abi=erc20_abi
)
# main script
def main():
print("\n--------------------------------------------- \n")
print("STARTING THE 03_MAKE_A_BOOKING.PY SCRIPT\n")
print("---------------------------------------------\n")
# initialising the accounts
print("Initialising the accoounts...\n")
barber = config["wallets"]["from_key"]["main"]
barber_address = config["addresses"]["main_address"]
customer_1 = config["wallets"]["from_key"]["dev_2"]
customer_1_address = config["addresses"]["dev_2_address"]
arbiter = config["wallets"]["from_key"]["dev_3"]
arbiter_address = config["addresses"]["dev_3_address"]
print("Accounts initialised.\n")
# barber needs to open up bookings
print("Getting the current booking state... \n")
current_booking_state = contract_instance.functions.bookingState().call()
print(f"The current booking state: {current_booking_state}\n")
if current_booking_state != 1:
print("Barber is opening up the bookings...\n")
nonce = w3.eth.get_transaction_count(barber_address)
tx = contract_instance.functions.openUpBookings().buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": 80001,
"from": barber_address,
"nonce": nonce,
}
)
signed_tx = w3.eth.account.sign_transaction(tx, barber)
send_tx = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
print("Waiting for the transaction receipt...\n")
tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx)
print("--------------------------------------------")
print(f"Transaction Receipt:\n{tx_receipt}")
print("--------------------------------------------\n")
current_booking_state = contract_instance.functions.bookingState().call()
print(f"The current booking state: {current_booking_state}\n")
print("Bookings are now open.\n")
# customer checks the haircut price of a fade
print("Customer is checking the price of a fade...\n")
# haircut_price = contract_instance.functions.viewHairCutPrices(1).call()
haircut_price = contract_instance.functions.FadePrice().call()
converted_haircut_price = w3.fromWei(haircut_price, "ether")
print(f"Haircut price of a fade is: {converted_haircut_price} WMATIC\n")
print("Customer is fine with the price and is now proceeding to book...\n")
# check customer balance before booking
customer_wmatic_balance = wmatic_erc20_token.functions.balanceOf(
customer_1_address
).call()
converted_customer_balance = w3.fromWei(customer_wmatic_balance, "ether")
print(
f"The customer's current WMATIC balance is: {converted_customer_balance} WMATIC \n"
)
# check contract balance before booking
contract_wmatic_balance = wmatic_erc20_token.functions.balanceOf(
contract_address
).call()
converted_contract_balance = w3.fromWei(contract_wmatic_balance, "ether")
print(
f"The contract's current WMATIC balance is: {converted_contract_balance} WMATIC \n"
)
# customer books a haircut
print("Customer is booking a haircut...\n")
# customer is approving the contract to spend the wmatic
print("Customer is approving the booking contract to spend the WMATIC... \n ")
print("Getting the nonce...")
nonce = w3.eth.get_transaction_count(customer_1_address)
print("Building the transaction...")
tx = wmatic_erc20_token.functions.approve(
contract_address, haircut_price
).buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"gas": 10_000_000,
"chainId": 80001,
"from": customer_1_address,
"nonce": nonce,
}
)
print("Signing the transaction...")
signed_tx = w3.eth.account.sign_transaction(tx, customer_1)
print("Sending the transaction...")
send_tx = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
print("Waiting for the transaction receipt of the approval...")
tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx)
print("--------------------------------------------")
print(f"Transaction Receipt of approval: \n{tx_receipt}")
print("--------------------------------------------\n")
# HAIRCUT BOOKING
print("Now calling the booking function for the haircut..\n")
print("Getting the nonce...\n")
nonce = w3.eth.get_transaction_count(customer_1_address)
print("Got the nonce.\n")
print("Building the transaction...\n")
name = "Jeff"
start_time = 1400
end_time = 1500
tx = contract_instance.functions.bookAFade(
name,
start_time,
end_time,
).buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"gas": 10_000_000,
"chainId": 80001,
"from": customer_1_address,
"nonce": nonce,
}
)
print("Transaction built.\n")
print("Signing the transaction...\n")
signed_tx = w3.eth.account.sign_transaction(tx, customer_1)
print("Sending the transaction...\n")
send_tx = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
print("Waiting for the transaction receipt...\n")
tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx)
print("--------------------------------------------")
print(f"Transaction Receipt:\n{tx_receipt}")
print("--------------------------------------------\n")
print(f"Customer 1 ({customer_1_address}) has now made a booking!\n")
# checking the booking ID
current_booking_id = contract_instance.functions.currentBookingID().call()
print(f"The booking ID for customer 1 is: {current_booking_id} \n")
# check contract balance after booking
contract_wmatic_balance = wmatic_erc20_token.functions.balanceOf(
contract_address
).call()
converted_contract_balance = w3.fromWei(contract_wmatic_balance, "ether")
print(
f"The contract's current WMATIC balance is: {converted_contract_balance} WMATIC \n"
)
# check new contract balance in Aave
contract_am_wmatic_token_balance = am_wmatic_token.functions.balanceOf(
contract_address
).call()
converted_balance = w3.fromWei(contract_am_wmatic_token_balance, "ether")
print(
f"The contract's current amWMATIC balance is: {converted_balance} amWMATIC \n"
)
# check barber balance before completion of haircut
barber_wmatic_balance = wmatic_erc20_token.functions.balanceOf(
barber_address
).call()
converted_barber_balance = w3.fromWei(barber_wmatic_balance, "ether")
print(
f"The barber's current WMATIC balance is: {converted_barber_balance} WMATIC \n"
)
# check customer balance before completion of haircut
customer_wmatic_balance = wmatic_erc20_token.functions.balanceOf(
customer_1_address
).call()
converted_customer_balance = w3.fromWei(customer_wmatic_balance, "ether")
print(f"The customer's WMATIC balance is: {converted_customer_balance} WMATIC \n")
#
print("--------------------\n")
print("The date and time of the haircut has now arrived.\n")
print("--------------------\n")
print("The haircut is currently in progress...\n")
# arbiter approves the haircut is complete
print("The haircut is now complete and was done to a good standard.\n")
print("The arbiter will now confirm this on the contract...\n")
print("Getting the nonce...\n")
nonce = w3.eth.get_transaction_count(arbiter_address)
print("Got the nonce.\n")
print("Building the transaction...\n")
tx = contract_instance.functions.completed(current_booking_id).buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"gas": 10_000_000,
"chainId": 80001,
"from": arbiter_address,
"nonce": nonce,
}
)
print("Transaction built.\n")
print("Signing the transaction...\n")
signed_tx = w3.eth.account.sign_transaction(tx, arbiter)
print("Transaction signed.\n")
print("Sending the transaction...\n")
send_tx = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
print("Transaction sent.\n")
print("Waiting for the transaction receipt...\n")
tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx)
print("--------------------------------------------")
print(f"Transaction Receipt:\n{tx_receipt}")
print("--------------------------------------------\n")
print("The arbiter has marked the haircut as complete. \n")
# check barber's new balance - barber's balance should go up
barber_new_wmatic_balance = wmatic_erc20_token.functions.balanceOf(
barber_address
).call()
converted_barber_balance = w3.fromWei(barber_new_wmatic_balance, "ether")
print(
f"The barber's new WMATIC balance after the haircut is: {converted_barber_balance} WMATIC "
)
# sanity check
if barber_new_wmatic_balance > barber_wmatic_balance:
print("This is higher than before the haircut as expected.\n")
# check customer's new balance - balance should go up
customer_new_wmatic_balance = wmatic_erc20_token.functions.balanceOf(
customer_1_address
).call()
converted_customer_balance = w3.fromWei(customer_new_wmatic_balance, "ether")
print(
f"\nThe customer's new WMATIC balance after the haircut is: {converted_customer_balance} WMATIC "
)
# sanity check
if customer_new_wmatic_balance > customer_wmatic_balance:
print("This is higher than before the haircut as expected.\n")
# check the contract's new balance on Aave - should have gone down
contract_new_am_wmatic_token_balance = am_wmatic_token.functions.balanceOf(
contract_address
).call()
converted_balance = w3.fromWei(contract_new_am_wmatic_token_balance, "ether")
print(
f"\nThe contract's new amWMATIC balance after the haircut is: {converted_balance} amWMATIC "
)
# sanity check
if contract_new_am_wmatic_token_balance < contract_am_wmatic_token_balance:
print("This is lower than before the haircut as expected.\n")
# End of script
print("---------------------------------------------\n")
print("03_MAKE_A_BOOKING.PY SCRIPT IS COMPLETE \n")
print("---------------------------------------------\n")