-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdeposit_collateral.py
More file actions
executable file
·141 lines (114 loc) · 4.31 KB
/
deposit_collateral.py
File metadata and controls
executable file
·141 lines (114 loc) · 4.31 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
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
#!/usr/bin/env python3
"""
Collateral Deposit Script
This script allows users to deposit collateral into the Collateral smart contract.
It handles validation of minimum collateral amounts, trustee verification, and
executes the deposit transaction on the blockchain.
"""
import sys
import argparse
from web3 import Web3
import bittensor.utils
from common import (
load_contract_abi,
get_web3_connection,
get_account,
validate_address_format,
build_and_send_transaction,
wait_for_receipt,
get_revert_reason,
)
class DepositCollateralError(Exception):
"""Custom exception for collateral deposit related errors."""
pass
def check_minimum_collateral(contract, amount_wei):
"""Check if the amount meets minimum collateral requirement."""
min_collateral = contract.functions.MIN_COLLATERAL_INCREASE().call()
if amount_wei < min_collateral:
raise ValueError(
f"Error: Amount {Web3.from_wei(amount_wei, 'ether')} TAO is less than "
f"minimum required {Web3.from_wei(min_collateral, 'ether')} TAO"
)
return min_collateral
def verify_trustee(contract, expected_trustee):
"""Verify if the provided trustee address matches the contract's trustee."""
trustee = contract.functions.TRUSTEE().call()
if trustee.lower() != expected_trustee.lower():
raise ValueError(
f"Error: Trustee address mismatch. Expected: {expected_trustee}, "
f"Got: {trustee}"
)
def deposit_collateral(w3, account, amount_tao,
contract_address, trustee_address):
"""Deposit collateral into the contract.
Args:
w3: Web3 instance
account: Account to use for the transaction
amount_tao: Amount to deposit in TAO
contract_address: Address of the contract
trustee_address: Trustee address to verify
Returns:
tuple: (deposit_event, receipt)
"""
validate_address_format(contract_address)
validate_address_format(trustee_address)
contract_abi = load_contract_abi()
contract = w3.eth.contract(address=contract_address, abi=contract_abi)
verify_trustee(contract, trustee_address)
amount_wei = w3.to_wei(amount_tao, "ether")
check_minimum_collateral(contract, amount_wei)
tx_hash = build_and_send_transaction(
w3, contract.functions.deposit(), account, value=amount_wei
)
receipt = wait_for_receipt(w3, tx_hash)
if receipt['status'] == 0:
revert_reason = get_revert_reason(w3, tx_hash, receipt['blockNumber'])
raise DepositCollateralError(f"Transaction failed for depositing collateral. Revert reason: {revert_reason}")
deposit_event = contract.events.Deposit().process_receipt(receipt)[0]
return deposit_event, receipt
def main():
"""Handle command line arguments and execute deposit."""
parser = argparse.ArgumentParser(
description="Deposit collateral into the Collateral smart contract"
)
parser.add_argument(
"--contract-address",
required=True,
help="Address of the Collateral contract"
)
parser.add_argument(
"--amount-tao",
required=True,
type=float,
help="Amount of TAO to deposit"
)
parser.add_argument(
"--trustee-address",
required=True,
help="Expected trustee address to verify"
)
parser.add_argument("--keyfile", help="Path to keypair file")
parser.add_argument("--network", default="finney", help="The Subtensor Network to connect to.")
args = parser.parse_args()
w3 = get_web3_connection(args.network)
account = get_account(args.keyfile)
deposit_event, receipt = deposit_collateral(
w3=w3,
account=account,
amount_tao=args.amount_tao,
contract_address=args.contract_address,
trustee_address=args.trustee_address,
)
print(f"Successfully deposited {args.amount_tao} TAO")
print("Event details:")
print(f" Account: {deposit_event['args']['account']}")
print(
f" Amount: {w3.from_wei(deposit_event['args']['amount'], 'ether')} TAO")
print(f" Transaction hash: {receipt['transactionHash'].hex()}")
print(f" Block number: {receipt['blockNumber']}")
if __name__ == "__main__":
try:
main()
except Exception as e:
print(f"Error: {str(e)}", file=sys.stderr)
sys.exit(1)