在以太坊中,我如何与区块链上的合同进行交互?

时间:2015-11-21 02:33:39

标签: ethereum

当我使用编译器的输出发布 new 合约时,我会得到一个javascript对象" somecontract"我可以像这样调用方法:" somecontract.doSomething();"

但是,我如何与过去某个时间点发布到区块链的合同互动?

1 个答案:

答案 0 :(得分:7)

如果您已经拥有合同地址,请跳至下面的代码段。如果没有,你需要找到它。

  1. 在etherchain.org上查找合同发布的帐户
  2. 查找用于创建合同的交易。它将成为“新合同”行之一。
  3. 最后,在geth内,获取交易收据,合同地址将是收据的“contractAddress”属性。
  4. 要获取abi,请重新编译合同(通常在此处https://chriseth.github.io/browser-solidity/)。现在您已获得abi和合同地址,您已准备好重新获得合同。

    var abi = <cut and paste the contract abi here>;
    var contract = web3.eth.contract(abi).at(<contract address>);
    contract.functionname();
    contract.functionname.sendTransaction(...);
    

    这是来自https://github.com/fivedogit/solidity-baby-steps/blob/master/contracts/05_greeter.sol

    的样本abi
    [{"constant":false,"inputs":[],"name":"kill","outputs":[],"type":"function"},
        {"constant":true,"inputs":[],"name":"getBlockNumber","outputs":[{"name":"","type":"uint256"}],"type":"function"},
        {"constant":false,"inputs":[{"name":"_newgreeting","type":"string"}],"name":"setGreeting","outputs":[],"type":"function"},
        {"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"type":"constructor"}]