Go-Ethereum

通過地址訪問合約,不知道實現

  • October 17, 2016

我想訪問已部署的合約並與之互動。我的限制:了解 API(函式、欄位),但不了解實現。

我正在查看乙太坊文件“訪問契約和交易”(http://ethdocs.org/en/latest/contracts-and-transactions/accessing-contracts-and-transactions.html)。

他們有這個 Web3.js

var source = 'contract Multiply7 { event Print(uint); function multiply(uint input) returns (uint) { Print(input * 7); return input * 7; } }';
var compiled = web3.eth.compile.solidity(source);
var Multiply7 = web3.eth.contract(compiled.Multiply7.info.abiDefinition);
var multi = Multiply7.at("0x0ab60714033847ad7f0677cc7514db48313976e2")
multi.multiply.sendTransaction(6, {from: "0xeb85a5557e5bdc18ee1934a89d8bb402398ee26a"})

但是,這需要知道契約的執行情況,這對我來說沒有意義。

謝謝你的幫助。

您只需要合約的地址和應用程序二進制介面的 JSON 表示。

獲取 ABI:

> var source = 'contract Multiply7 { event Print(uint); function multiply(uint input) returns (uint) { Print(input * 7); return input * 7; } }';
undefined
> var compiled = web3.eth.compile.solidity(source);
undefined
> compiled.Multiply7.info.abiDefinition
[{
   constant: false,
   inputs: [{
       name: "input",
       type: "uint256"
   }],
   name: "multiply",
   outputs: [{
       name: "",
       type: "uint256"
   }],
   payable: false,
   type: "function"
}, {
   anonymous: false,
   inputs: [{
       indexed: false,
       name: "",
       type: "uint256"
   }],
   name: "Print",
   type: "event"
}]

因此,在不知道實現但只知道地址和 ABI 的情況下,您應該能夠執行以下命令:

var Multiply7 = web3.eth.contract([{constant: false, inputs: [{name: "input", type: "uint256"}], name: "multiply", outputs: [{name: "", type: "uint256"}], payable: false, type: "function"}, {anonymous: false, inputs: [{indexed: false, name: "", type: "uint256"}], name: "Print", type: "event"}]);
var multi = Multiply7.at("0x0ab60714033847ad7f0677cc7514db48313976e2")
multi.multiply.sendTransaction(6, {from: "0xeb85a5557e5bdc18ee1934a89d8bb402398ee26a"})

引用自:https://ethereum.stackexchange.com/questions/9359