当我使用编译器的输出发布新合同时,我得到一个 javascript 对象“somecontract”,我可以像这样调用方法:“somecontract.doSomething();”
但是我如何与过去某个时间发布到区块链的合约进行交互呢?
当我使用编译器的输出发布新合同时,我得到一个 javascript 对象“somecontract”,我可以像这样调用方法:“somecontract.doSomething();”
但是我如何与过去某个时间发布到区块链的合约进行交互呢?
如果您已经有了合约地址,请跳到下面的代码片段。如果没有,你需要找到它。
要获得 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"}]