首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >被契约API弄糊涂了

被契约API弄糊涂了
EN

Ethereum用户
提问于 2020-02-25 16:52:48
回答 2查看 502关注 0票数 2

我对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控制台上:

代码语言:javascript
复制
> 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 (大写)甚至不存在:

代码语言:javascript
复制
> mySimpleStorage = new web3.eth.Contract(abi)
TypeError: Value is not an object: undefined
        at <eval>:1:19(5)

我必须使用web3.eth.contract (小写):

代码语言:javascript
复制
> 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()

代码语言:javascript
复制
> tx = mySimpleStorage.methods
Undefined
> tx
undefined
EN

回答 2

Ethereum用户

发布于 2020-02-25 17:30:56

代码语言:javascript
复制
const contract = new web3.eth.Contract(abi, address);

我想你忘了第二个参数,那是智能合同地址。

票数 0
EN

Ethereum用户

发布于 2020-02-26 18:19:47

我想我明白这个问题了。Geth支持web3 0.2x.x,而不是1.x。所以没有encodeABI(),您必须使用getData(),而且整个语法是不同的。这个旧接口的文档如下:https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethcontract

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/80087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档