-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_a_withdrawal.py
637 lines (485 loc) · 24.4 KB
/
make_a_withdrawal.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
from brownie import accounts, config
from web3 import Web3
from decimal import Decimal
w3 = Web3(Web3.WebsocketProvider(config["provider"]["websockets"]))
with open("./contract_data/goerli-eth/address.txt") as file_a:
contract_address = file_a.read()
with open("./contract_data/goerli-eth/abi.json") as file_b:
contract_abi = file_b.read()
# initialising the contract instance
contract_instance = w3.eth.contract(address=contract_address, abi=contract_abi)
def main():
# instantiating the accounts
admin = config["wallets"]["from_key"]["admin"]
admin_address = config["addresses"]["admin_address"]
# family members to add
family_member_2 = config["wallets"]["from_key"]["family_member_2"]
family_member_2_address = config["addresses"]["family_member_2_address"]
family_member_3 = config["wallets"]["from_key"]["family_member_3"]
family_member_3_address = config["addresses"]["family_member_3_address"]
family_member_4 = config["wallets"]["from_key"]["family_member_4"]
family_member_4_address = config["addresses"]["family_member_4_address"]
## PART 1
print("\n------------------------------------------------------------------\n")
print("------------------------------------------------------------------\n")
print("\nSTARTING THE SCRIPT MAKE_A_WITHDRAWAL.PY...\n")
print("------------------------------------------------------------------\n")
print("------------------------------------------------------------------\n")
# admin will add these accounts to the family contract addresses
# getting the current count of family members
num_of_family = contract_instance.functions.numOfFamilyMembers().call()
print(f"\nThere are currently {num_of_family} family members in this contract.\n")
print("------------------------------------------------------------------\n")
####################################################################################
# adding family member 2
print(f"Adding family member 2 '{family_member_2_address}'...\n")
# getting the nonce of the admin that will be sending the transaction
nonce = w3.eth.get_transaction_count(admin_address)
# building the transaction
tx = contract_instance.functions.addFamilyMember(
family_member_2_address
).buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": 5,
"from": admin_address,
"nonce": nonce,
}
)
# signing and sending the transaction
signed_tx = w3.eth.account.sign_transaction(tx, admin)
send_tx = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
print("Waiting for the transaction to go through...\n")
tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx)
print("--------------------------------------------")
print(f"Transaction Receipt:\n{tx_receipt}")
print("--------------------------------------------\n")
print(f"Family member 2 '{family_member_2_address}' has been added.\n")
# checking the new number of family members
num_of_family = contract_instance.functions.numOfFamilyMembers().call()
print(f"There are now {num_of_family} family members in this contract.\n")
print("------------------------------------------------------------------\n")
####################################################################################
# adding family member 3
print(f"Adding family member 3 '{family_member_3_address}'...\n")
# getting the nonce of the admin that will be sending the transaction
nonce = w3.eth.get_transaction_count(admin_address)
# building the transaction
tx = contract_instance.functions.addFamilyMember(
family_member_3_address
).buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": 5,
"from": admin_address,
"nonce": nonce,
}
)
# signing and sending the transaction
signed_tx = w3.eth.account.sign_transaction(tx, admin)
send_tx = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
print("Waiting for the transaction to go through...\n")
tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx)
print("--------------------------------------------")
print(f"Transaction Receipt:\n{tx_receipt}")
print("--------------------------------------------\n")
print(f"Family member 3 '{family_member_3_address}' has been added.\n")
# checking the new number of family members
num_of_family = contract_instance.functions.numOfFamilyMembers().call()
print(f"There are now {num_of_family} family members in this contract.\n")
print("------------------------------------------------------------------\n")
####################################################################################
# adding family member 4
print(f"Adding family member 4 '{family_member_4_address}'...\n")
# getting the nonce of the admin that will be sending the transaction
nonce = w3.eth.get_transaction_count(admin_address)
# building the transaction
tx = contract_instance.functions.addFamilyMember(
family_member_4_address
).buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": 5,
"from": admin_address,
"nonce": nonce,
}
)
# signing and sending the transaction
signed_tx = w3.eth.account.sign_transaction(tx, admin)
send_tx = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
print("Waiting for the transaction to go through...\n")
tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx)
print("--------------------------------------------")
print(f"Transaction Receipt:\n{tx_receipt}")
print("--------------------------------------------\n")
print(f"Family member 4 '{family_member_4_address}' has been added.\n")
# checking the new number of family members
num_of_family = contract_instance.functions.numOfFamilyMembers().call()
print(f"There are now {num_of_family} family members in this contract.\n")
print("------------------------------------------------------------------\n")
####################################################################################
####################################################################################
####################################################################################
## PART 2
## each member will now add call easySaveETH
####################################################################################
####################################################################################
####################################################################################
## ADMIN ADDING SAVINGS
# checking the current family holdings
print("\nChecking the current family savings...\n")
current_holdings = contract_instance.functions.viewFamilyHoldings().call(
{"from": admin_address}
)
# holdings converted
holdings_converted = w3.fromWei(current_holdings, "ether")
print(f"The current savings in aWETH are: {holdings_converted} aWETH\n")
# initialising the amount of ETH to send
amount_to_send = w3.toWei(Decimal("0.003"), "ether") # 0.003 ETH
# converting to ether
amount_converted = w3.fromWei(amount_to_send, "ether")
print(
f"The admin / 'family member 1 ({admin_address})' is sending {amount_converted} ETH to the family savings account...\n"
)
# depositing some eth into the savings - this tx will go to Aave V3
nonce = w3.eth.get_transaction_count(admin_address)
tx = contract_instance.functions.easySaveETH().buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": 5,
"from": admin_address,
"value": amount_to_send,
"nonce": nonce,
}
)
# signing and sending the transaction
signed_tx = w3.eth.account.sign_transaction(tx, admin)
send_tx = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
print("Waiting for the transaction to go through...\n")
tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx)
print("--------------------------------------------")
print(f"Transaction Receipt:\n{tx_receipt}")
print("--------------------------------------------\n")
print(
f"The admin / 'family member 1 ({admin_address})' has successfully added {amount_converted} ETH to the defi family savings account.\n"
)
# checking the current family holdings
current_holdings = contract_instance.functions.viewFamilyHoldings().call(
{"from": admin_address}
)
# holdings converted
holdings_converted = w3.fromWei(current_holdings, "ether")
print(f"The current savings in aWETH are: {holdings_converted} aWETH :) \n")
print("------------------------------------------------------------------\n")
####################################################################################
## FAMILY MEMBER 2 ADDING SAVINGS
# checking the current family holdings
print("\nChecking the current family savings...\n")
current_holdings = contract_instance.functions.viewFamilyHoldings().call(
{"from": family_member_2_address}
)
# holdings converted
holdings_converted = w3.fromWei(current_holdings, "ether")
print(f"The current savings in aWETH are: {holdings_converted} aWETH\n")
# initialising the amount of ETH to send
amount_to_send = w3.toWei(Decimal("0.003"), "ether") # 0.003 ETH
# converting to ether
amount_converted = w3.fromWei(amount_to_send, "ether")
print(
f"Family member 2 ({family_member_2_address}) is sending {amount_converted} ETH to the family savings account...\n"
)
# depositing some eth into the savings - this tx will go to Aave V3
nonce = w3.eth.get_transaction_count(family_member_2_address)
tx = contract_instance.functions.easySaveETH().buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": 5,
"from": family_member_2_address,
"value": amount_to_send,
"nonce": nonce,
}
)
# signing and sending the transaction
signed_tx = w3.eth.account.sign_transaction(tx, family_member_2)
send_tx = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
print("Waiting for the transaction to go through...\n")
tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx)
print("--------------------------------------------")
print(f"Transaction Receipt:\n{tx_receipt}")
print("--------------------------------------------\n")
print(
f"Family member 2 ({family_member_2_address}) has successfully added {amount_converted} ETH to the defi family savings account.\n"
)
# checking the current family holdings
current_holdings = contract_instance.functions.viewFamilyHoldings().call(
{"from": family_member_2_address}
)
# holdings converted
holdings_converted = w3.fromWei(current_holdings, "ether")
print(f"The current savings in aWETH are: {holdings_converted} aWETH :) \n")
print("------------------------------------------------------------------\n")
####################################################################################
## FAMILY MEMBER 3 ADDING SAVINGS
# checking the current family holdings
print("\nChecking the current family savings...\n")
current_holdings = contract_instance.functions.viewFamilyHoldings().call(
{"from": family_member_3_address}
)
# holdings converted
holdings_converted = w3.fromWei(current_holdings, "ether")
print(f"The current family savings in aWETH are: {holdings_converted} aWETH\n")
# initialising the amount of ETH to send
amount_to_send = w3.toWei(Decimal("0.003"), "ether") # 0.003 ETH
# converting to ether
amount_converted = w3.fromWei(amount_to_send, "ether")
print(
f"Family member 3 ({family_member_3_address}) is sending {amount_converted} ETH to the family savings account...\n"
)
# depositing some eth into the savings - this tx will go to Aave V3
nonce = w3.eth.get_transaction_count(family_member_3_address)
tx = contract_instance.functions.easySaveETH().buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": 5,
"from": family_member_3_address,
"value": amount_to_send,
"nonce": nonce,
}
)
# signing and sending the transaction
signed_tx = w3.eth.account.sign_transaction(tx, family_member_3)
send_tx = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
print("Waiting for the transaction to go through...\n")
tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx)
print("--------------------------------------------")
print(f"Transaction Receipt:\n{tx_receipt}")
print("--------------------------------------------\n")
print(
f"Family member 3 {family_member_3_address} has successfully added {amount_converted} ETH to the defi family savings account.\n"
)
# checking the current family holdings
current_holdings = contract_instance.functions.viewFamilyHoldings().call(
{"from": family_member_3_address}
)
# holdings converted
holdings_converted = w3.fromWei(current_holdings, "ether")
print(f"The current savings in aWETH are: {holdings_converted} aWETH :) \n")
print("------------------------------------------------------------------\n")
####################################################################################
####################################################################################
####################################################################################
## PART 3
## MEMBER 2 WILL REQUEST A WITHDRAWAL
####################################################################################
####################################################################################
####################################################################################
## FAMILY MEMBER 2 REQUESTING A WITHDRAWAL
# checking the current family holdings
print("\nChecking the current family holdings...\n")
current_holdings = contract_instance.functions.viewFamilyHoldings().call(
{"from": family_member_2_address}
)
# holdings converted
holdings_converted = w3.fromWei(current_holdings, "ether")
print(f"The current savings in aWETH are: {holdings_converted} aWETH\n")
# initialising the amount of ETH to withdraw
amount_to_request = w3.toWei(Decimal("0.003"), "ether") # 0.003 ETH
# converting to ether
amount_converted = w3.fromWei(amount_to_request, "ether")
# reason to withdraw
reason_to_withdraw = "Buying a new house."
print(
f"Family member 2 ({family_member_2_address}) is requesting a withdrawal of {amount_converted} ETH from the family savings account, with the reason '{reason_to_withdraw}'...\n"
)
# sending the request - this will use request ID = 1
nonce = w3.eth.get_transaction_count(family_member_2_address)
tx = contract_instance.functions.requestWithdrawal(
amount_to_request, reason_to_withdraw
).buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": 5,
"from": family_member_2_address,
"nonce": nonce,
}
)
# signing and sending the transaction
signed_tx = w3.eth.account.sign_transaction(tx, family_member_2)
send_tx = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
print("Waiting for the transaction to go through...\n")
tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx)
print("--------------------------------------------")
print(f"Transaction Receipt:\n{tx_receipt}")
print("--------------------------------------------\n")
# checking the current request ID
request_id = contract_instance.functions.requestID().call(
{"from": family_member_2_address}
)
print(f"Your request ID is: {request_id} :) \n")
print(
f"Successfully made a request for {amount_converted} ETH from the defi family savings account, with the requestID of {request_id}. Please wait for the others to approve your request...\n"
)
print("------------------------------------------------------------------\n")
####################################################################################
####################################################################################
####################################################################################
## PART 4
## 2 FAMILY MEMBERS WILL APPROVE THE REQUEST OF MEMBER 2 USING THE REQUEST ID
# - since 2 is the current approval number
####################################################################################
####################################################################################
####################################################################################
# finding out the current number of approvals
current_num_of_approvals = contract_instance.functions.requestIDToApprovals(
request_id
).call({"from": family_member_3_address})
print(
f"The current number of approvals for requestID {request_id} is {current_num_of_approvals}\n"
)
# getting the reason to the request
reason_for_request = contract_instance.functions.requestIDToReason(request_id).call(
{"from": family_member_3_address}
)
print(f"The reason for requestID {request_id} is: '{reason_for_request}' \n")
# family member 3 will approve the request
print(
f"Family member 3 ({family_member_3_address}) is approving requestID: {request_id}...\n"
)
# approving the request
nonce = w3.eth.get_transaction_count(family_member_3_address)
tx = contract_instance.functions.approveRequest(request_id).buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": 5,
"from": family_member_3_address,
"nonce": nonce,
}
)
# signing and sending the transaction
signed_tx = w3.eth.account.sign_transaction(tx, family_member_3)
send_tx = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
print("Waiting for the transaction to go through...\n")
tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx)
print("--------------------------------------------")
print(f"Transaction Receipt:\n{tx_receipt}")
print("--------------------------------------------\n")
print(
f"Family member 3 ({family_member_3_address}) has successfully approved requestID: {request_id}.\n"
)
# checking the new number of approvals
current_num_of_approvals = contract_instance.functions.requestIDToApprovals(
request_id
).call({"from": family_member_3_address})
print(
f"The current number of approvals for requestID {request_id} is {current_num_of_approvals}\n"
)
####################################################################################
# family member 4 will approve the request
print(
f"Family member 4 ({family_member_4_address}) is approving request ID: {request_id}...\n"
)
# approving the request
nonce = w3.eth.get_transaction_count(family_member_4_address)
tx = contract_instance.functions.approveRequest(request_id).buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": 5,
"from": family_member_4_address,
"nonce": nonce,
}
)
# signing and sending the transaction
signed_tx = w3.eth.account.sign_transaction(tx, family_member_4)
send_tx = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
print("Waiting for the transaction to go through...\n")
tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx)
print("--------------------------------------------")
print(f"Transaction Receipt:\n{tx_receipt}")
print("--------------------------------------------\n")
print(
f"Family member 4 ({family_member_4_address}) has successfully approved requestID: {request_id}.\n"
)
# checking the new number of approvals
current_num_of_approvals = contract_instance.functions.requestIDToApprovals(
request_id
).call({"from": family_member_3_address})
print(
f"The current number of approvals for requestID {request_id} is {current_num_of_approvals}\n"
)
# checking the required approvals
required_approvals = contract_instance.functions.requiredApprovals().call()
print(
f"The required number of approvals for a withdrawal to go through is: {required_approvals} approvals\n"
)
####################################################################################
####################################################################################
####################################################################################
## PART 5
## FAMILY MEMBER 2 WILL NOW EXECUTE THEIR WITHDRAWAL NOW THAT IT HAS BEEN APPROVED
####################################################################################
####################################################################################
####################################################################################
# checking the individual's balance before execution of the withdrawal
print(f"Checking the current balance of {family_member_2_address}...\n")
family_member_2_balance = w3.eth.get_balance(family_member_2_address)
family_member_2_balance_converted = w3.fromWei(family_member_2_balance, "ether")
print(
f"The current balance of family member 2 ({family_member_2_address}) is {family_member_2_balance_converted} ETH.\n"
)
# checking the current family holdings
print("Checking the current family savings...\n")
current_holdings = contract_instance.functions.viewFamilyHoldings().call(
{"from": admin_address}
)
# holdings converted
holdings_converted = w3.fromWei(current_holdings, "ether")
print(f"The current family savings in aWETH are: {holdings_converted} aWETH\n")
#########################
# executing the withdrawal
print(
f"Family member 2 ({family_member_2_address}) is now executing their withdrawal...\n"
)
nonce = w3.eth.get_transaction_count(family_member_2_address)
tx = contract_instance.functions.withdrawSavedETH(request_id).buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": 5,
"from": family_member_2_address,
"nonce": nonce,
}
)
# signing and sending the transaction
signed_tx = w3.eth.account.sign_transaction(tx, family_member_2)
send_tx = w3.eth.send_raw_transaction(signed_tx.rawTransaction)
print("Waiting for the transaction to go through...\n")
tx_receipt = w3.eth.wait_for_transaction_receipt(send_tx)
print("--------------------------------------------")
print(f"Transaction Receipt:\n{tx_receipt}")
print("--------------------------------------------\n")
print(
f"Family member 2 ({family_member_2}) has now successfully withdrawn from the family defi savings.\n"
)
# checking the updated balance before after the withdrawal
print(
f"Checking the current balance of family member 2 ({family_member_2_address})...\n"
)
family_member_2_balance = w3.eth.get_balance(family_member_2_address)
family_member_2_balance_converted = w3.fromWei(family_member_2_balance, "ether")
print(
f"The current balance of family member 2 ({family_member_2_address}) is now {family_member_2_balance_converted} ETH.\n"
)
# checking the updated family holdings
print("Checking the new amount in family savings...\n")
current_holdings = contract_instance.functions.viewFamilyHoldings().call(
{"from": admin_address}
)
# holdings converted
holdings_converted = w3.fromWei(current_holdings, "ether")
print(f"The current updated savings in aWETH are: {holdings_converted} aWETH\n")
print("-----------------------------------------------------------------")
print("-----------------------------------------------------------------")
print("-----------------------------------------------------------------")
print("\nMAKE_A_WITHDRAWAL.PY SCRIPT IS NOW COMPLETE.\n")
print("-----------------------------------------------------------------")
print("-----------------------------------------------------------------")
print("-----------------------------------------------------------------")