【问题标题】:Interacting with a Smart Contract on private network through Web3.js通过 Web3.js 与私有网络上的智能合约交互
【发布时间】:2018-02-07 10:05:08
【问题描述】:

我已经使用mist 和geth 在私有网络上部署了我的智能合约。

现在的困惑是:如何通过 Web3.js 与智能合约交互。

这是我的脚本:

if (typeof web3 !== 'undefined') {
            web3 = new Web3(web3.currentProvider);
        } else {
            // set the provider you want from Web3.providers
            web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
        }
web3.eth.defaultAccount = web3.eth.accounts[0];
var CoursetroContract = web3.eth.contract(YOUR ABI);

var Coursetro = CoursetroContract.at('PASTE CONTRACT ADDRESS HERE');
console.log(Coursetro);

当我尝试以下命令时:

> web3.providers
{
  HttpProvider: function(host, timeout, user, password),
  IpcProvider: function(path, net)
}

【问题讨论】:

  • 我使用infura。 infura.io。设置完成后,您可以使用 infura 链接而不是 localhost 链接。

标签: blockchain ethereum solidity smartcontracts web3js


【解决方案1】:

设置 infura 后,您可以使用他们返回给您的门户 URL 来创建您的提供商。只需将您的脚本编辑成如下所示:

if (typeof web3 !== 'undefined') {
            web3 = new Web3(web3.currentProvider);
        } else {
            // set the provider you want from Web3.providers
            web3 = new Web3(new Web3.providers.HttpProvider(<your infura.io url here>));
        }
web3.eth.defaultAccount = web3.eth.accounts[0];
var CoursetroContract = web3.eth.contract(YOUR ABI);

var Coursetro = CoursetroContract.at('PASTE CONTRACT ADDRESS HERE');
console.log(Coursetro);

【讨论】:

  • 感谢您的回复,我在 infura 上完成了注册,并从 infra 获得了一些网址。接下来我需要做什么?只需要在我的脚本上使用这些 url 或设置我的智能合约和 infura 吗?
  • 如果您查看上面的代码,您可以看到我只是将 infura url 放在我写 的地方,如果用户不这样做,它应该配置您的后备有一个 currentProvider。
  • 不,您不必使用元掩码来使用 infura。如果我之前的回复解决了您的问题,请将其标记为正确。如果不是,请提出澄清问题。 Solidity 开发的资源稀少,所以让我们尽自己的一份力量来获得答案。
猜你喜欢
  • 1970-01-01
  • 2019-08-27
  • 2021-03-12
  • 2020-08-25
  • 2019-01-17
  • 1970-01-01
  • 2019-01-24
  • 1970-01-01
  • 2018-12-20
相关资源
最近更新 更多