-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeeper_api.py
More file actions
70 lines (54 loc) · 2.65 KB
/
keeper_api.py
File metadata and controls
70 lines (54 loc) · 2.65 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
from deployer import Deployer
import logging
class KeeperAPI(Deployer):
''' A helper class that serves as an API for the smart contract '''
def __init__(self, path, contract_address, infura_provider, private_key):
super().__init__(infura_provider, private_key)
bytecode, abi = self.getCompiledSol(path)
self.contract = self.web3.eth.contract(address=contract_address, abi=abi)
def getLabelsLength(self):
return self.contract.functions.getLabelsLength().call()
def getLabel(self, index):
if index in range(self.getLabelsLength()):
return self.contract.functions.labels(index).call()
else:
logging.error('Operation will revert, index bigger than labels length')
return -1
def getTarget(self, label):
return self.contract.functions.targets(label).call()
def addPassword(self, label, target):
previous_length = self.getLabelsLength()
contract_txn = self.contract.functions.addPassword(label, target).buildTransaction(
{
'from': self.account.address,
'nonce': self.web3.eth.getTransactionCount(self.account.address),
'gas': self.contract.functions.addPassword(label, target).estimateGas(),
'gasPrice': self.web3.eth.gas_price,
'value': self.web3.toWei(0, 'ether')
}
)
signed = self.account.signTransaction(contract_txn)
tx_hash = self.web3.eth.sendRawTransaction(signed.rawTransaction)
logging.info(f'Transaction Hash: {tx_hash.hex()}')
tx_receipt = self.web3.eth.waitForTransactionReceipt(tx_hash)
if tx_receipt['status'] and self.getLabelsLength() > previous_length:
return True
return False
def removeLabel(self, index):
previous_length = self.getLabelsLength()
contract_txn = self.contract.functions.removeLabel(index).buildTransaction(
{
'from': self.account.address,
'nonce': self.web3.eth.getTransactionCount(self.account.address),
'gas': self.contract.functions.removeLabel(index).estimateGas(),
'gasPrice': self.web3.eth.gas_price,
'value': self.web3.toWei(0, 'ether')
}
)
signed = self.account.signTransaction(contract_txn)
tx_hash = self.web3.eth.sendRawTransaction(signed.rawTransaction)
logging.info(f'Transaction Hash: {tx_hash.hex()}')
tx_receipt = self.web3.eth.waitForTransactionReceipt(tx_hash)
if tx_receipt['status'] and self.getLabelsLength() < previous_length:
return True
return False