1.定义abi
> mshk_abi=JSON.parse('[{\"constant\":true,\"inputs\":[{\"name\":\"name\",\"type\":\"string\"}],\"name\":\"print\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"say\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"}]')
输出:
[{
constant: true,
inputs: [{
name: "name",
type: "string"
}],
name: "print",
outputs: [{
name: "",
type: "string"
}],
payable: false,
stateMutability: "pure",
type: "function"
}, {
constant: true,
inputs: [],
name: "say",
outputs: [{
name: "",
type: "string"
}],
payable: false,
stateMutability: "pure",
type: "function"
}]
2.加载合约地址
var address = "0x668efc5325df2511e0cc9284ae3d51cbfb63b88a";
输出结果:
undefined
3.生成合约对象
> var metacoin = web3.eth.contract(mshk_abi).at(address);
undefined
4.调用合约方法
> metacoin.print("t")
"t"