-
-
Save amitkumarj441/6ff230cd9c3973de7730adb03c1ebd24 to your computer and use it in GitHub Desktop.
Make vyper easy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from web3 import Web3, HTTPProvider | |
from viper import compiler | |
from web3.contract import ConciseContract | |
from time import sleep | |
example_contract = open('./path/to/contract.v.py', 'r') | |
contract_code = example_contract.read() | |
example_contract.close() | |
cmp = compiler.Compiler() | |
contract_bytecode = cmp.compile(contract_code).hex() | |
contract_abi = cmp.mk_full_signature(contract_code) | |
web3 = Web3(HTTPProvider('http://localhost:8545')) | |
web3.personal.unlockAccount('account_addr', 'account_pwd', 120) | |
# Instantiate and deploy contract | |
contract_bytecode = web3.eth.contract(contract_abi, bytecode=contract_bytecode) | |
# Get transaction hash from deployed contract | |
tx_hash = contract_bytecode.deploy(transaction={'from': 'account_addr', 'gas': 410000}) | |
# Waiting for contract to be delpoyed | |
i = 0 | |
while i < 5: | |
try: | |
# Get tx receipt to get contract address | |
tx_receipt = web3.eth.getTransactionReceipt(tx_hash) | |
contract_address = tx_receipt['contractAddress'] | |
break # if success, then exit the loop | |
except: | |
print("Reading failure for {} time(s)".format(i + 1)) | |
sleep(5+i) | |
i = i + 1 | |
if i >= 5: | |
raise Exception("Cannot wait for contract to be deployed") | |
# Contract instance in concise mode | |
contract_instance = web3.eth.contract(contract_abi, contract_address, ContractFactoryClass=ConciseContract) | |
# Calling contract method | |
print('Contract value: {}'.format(contract_instance.some_method())) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment