我对web3:https://web3js.readthedocs.io/en/v1.2.6/web3-eth-contract.html中的Contract很困惑
myContract.methods在geth控制台中不可用,Geth1.9.11-稳定和1.9.12-在Ubuntu16.0.4上不稳定(快速同步d)
这是版本问题吗?我必须回到以前的版本才有这个特性吗?
更详细地说,我正在尝试从geth控制台中使用这个API来检索要签名和提交的编码事务字符串
当我在Remix (remix.ethereum.org)上使用时,在编译了SimpleStorage契约并部署到Ropsten之后,我可以访问以下方法
在Remix控制台上:
> abi = [{"constant": false,"inputs": [{"name": "x","type": "uint256"}],"name": "set","outputs": [],"payable": false,"stateMutability": "nonpayable","type": "function"},{"constant": true,"inputs": [],"name": "get","outputs": [{"name": "","type": "uint256"}],"payable": false,"stateMutability": "view","type": "function"}]
> mySimpleStorage = new web3.eth.Contract(abi)
> mySimpleStorage.methods
{
"set": "function () { [native code] }",
"0x60fe47b1": "function () { [native code] }",
"set(uint256)": "function () { [native code] }",
"get": "function () { [native code] }",
"0x6d4ce63c": "function () { [native code] }",
"get()": "function () { [native code] }"
}
> tx = mySimpleStorage.methods.set("x")
> tx.encodeABI()
0x60fe47b10000000000000000000000000000000000000000000000000000000000000005这正是我根据文档所期望的。
使用Geth控制台:
web3.eth.Contract (大写)甚至不存在:
> mySimpleStorage = new web3.eth.Contract(abi)
TypeError: Value is not an object: undefined
at <eval>:1:19(5)我必须使用web3.eth.contract (小写):
> mySimpleStorage = new web3.eth.contract(abi)
{
abi: [{
constant: false,
inputs: [{...}],
name: "set",
outputs: [],
payable: false,
stateMutability: "nonpayable",
type: "function"
}, {
constant: true,
inputs: [],
name: "get",
outputs: [{...}],
payable: false,
stateMutability: "view",
type: "function"
}],
eth: {},
at: function(address, callback),
getData: function(),
new: function()
}无论如何,方法属性丢失了,不可能调用encodeABI()
> tx = mySimpleStorage.methods
Undefined
> tx
undefined发布于 2020-02-25 17:30:56
const contract = new web3.eth.Contract(abi, address);我想你忘了第二个参数,那是智能合同地址。
发布于 2020-02-26 18:19:47
我想我明白这个问题了。Geth支持web3 0.2x.x,而不是1.x。所以没有encodeABI(),您必须使用getData(),而且整个语法是不同的。这个旧接口的文档如下:https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethcontract
https://ethereum.stackexchange.com/questions/80087
复制相似问题