自动编译和部署以太坊Viper智能合约

时间:2017-11-30 13:55:58

标签: ethereum

有没有办法自动编译和部署Viper Smart Contract到某个自定义链(而不是ethereum.tools的测试链)

根据GitHub issue和我找到的两个帖子(this onethat one),最好的选择是编译合同,然后手动插入geth。

有人可以分享他们的解决方案吗?

1 个答案:

答案 0 :(得分:2)

正如您提供的Github issue中所述 - 您可以使用web3.py库和Viper库本身来实现它。 以下是可能满足您需求的脚本示例:

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()))