parity-bridge-research/erc20/utils/web3.py

46 lines
1.0 KiB
Python
Raw Normal View History

2018-05-25 08:06:23 -07:00
# This module is wrapper for web3.py module
from web3 import Web3, Account
from json import load as jload
2018-05-25 08:06:23 -07:00
def toChecksumAddress(_addr):
return Web3.toChecksumAddress(_addr)
def connectionToRPCProvider(_rpc_link):
ch = None
try:
ch = Web3(Web3.HTTPProvider(_rpc_link))
except e:
pass
return ch
def decryptPrivateKey(_file, _pwd):
with open(_file) as keyfile:
encrypted_key = keyfile.read()
keyfile.close()
ac_obj = Account()
decrypted_key = ac_obj.decrypt(encrypted_key, _pwd)
return ac_obj.privateKeyToAccount(decrypted_key)
def initContractAtAddress(_web3, _file, _address):
with open(_file) as f:
contract_abi = jload(f)
f.close()
if type(contract_abi) == dict:
contract_abi = contract_abi['abi']
contract_factory = _web3.eth.contract(
abi = contract_abi,
)
return contract_factory(_address)
def fromEther(_value):
return Web3.toWei(_value, 'ether')
def toEther(_value):
return Web3.fromWei(_value, 'ether')