0

我的代码

https://gist.github.com/tlatkdgus1/4885faa14ca123024fbb3fd194404352

结果

https://gist.github.com/tlatkdgus1/e87c7f247fb3d0376613d25e8286ec80

格思

https://gist.github.com/tlatkdgus1/5939f90a7b5c478f78c8ec0ecca45b5b

抱歉,我不能很好地使用 stackoverflow,所以请在 gist 中上传源代码




那么如何设置solidity变量值呢?

尝试列表

1. solidity setInt (X)
2. gas increase (X)
3. solidity variable name change (X)

我认为这部分是问题,但我不知道:(

while w3.eth.getTransactionReceipt(tx_hash) is None:
    print (w3.eth.getTransactionReceipt(tx_hash))

有什么想法吗?

参考 https://github.com/pipermerriam/web3.py

4

1 回答 1

0

我在回答我认为您要问的问题:为什么不getLog返回前面表达式中设置的值 with setLog

答:在调用setLog时,交易尚未被挖掘。getLog

为了便于阅读,我已经稍微清理了你的代码,并将你的while循环变成了一个有超时的函数。我还冒昧地进行更新以使用 web3.py v4 预发行版。

web3.py 预发行版(连同 eth_tester)可以安装:

$ pip install --pre web3[tester]

import json
import time
from web3 import Web3
from web3.providers.eth_tester import EthereumTesterProvider
from solc import compile_source
from web3.contract import ConciseContract
import time

contract_source_code = '''
pragma solidity ^0.4.11;

contract contract_log {
string name;
string time;
string product;

function contract_log(){
    name='kk';
    }
function setLog(string a, string b, string c) public {
    name = a;
    time = b;
    product = c;
    }


function getLog() constant returns (string, string, string) {
    return (name, time, product);
}

}
'''

def wait_on_tx_receipt(tx_hash):
    start_time = time.time()
    while True:
        if start_time + 60 < time.time():
            raise TimeoutError("Timeout occurred waiting for tx receipt")
        if w3.eth.getTransactionReceipt(tx_hash):
            return w3.eth.getTransactionReceipt(tx_hash)


compiled_sol = compile_source(contract_source_code) # Compiled source code
contract_interface = compiled_sol['<stdin>:contract_log']

w3 = Web3(EthereumTesterProvider())

contract = w3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin'])

deploy_tx_hash = contract.deploy(transaction={'from': w3.eth.coinbase, 'gas':500000})
contract_address = wait_on_tx_receipt(deploy_tx_hash).contractAddress
contract_instance = w3.eth.contract(abi=contract_interface['abi'], address=contract_address)

tx_hash = contract_instance.functions.setLog(
    'tlatk',
    '12',
    'product').transact({'from':w3.eth.coinbase,'gas':500000})

wait_on_tx_receipt(tx_hash)

print(contract_instance.functions.getLog().call())
于 2018-03-27T18:28:21.760 回答