标签: web3js

使用web3以太坊调用智能合约方法

尝试使用以太坊来解决区块链问题,在尝试与已部署的合同进行交互时,我遇到了问题.我想要实现的是调用一种方法来显示添加到使用Geth本地部署的私有区块链的信息.

我无法通过我的智能合约调用任何功能,我一直在想我是否做错了什么......有人能告诉我如何从这个合同中简单地调用其中一个方法吗?让我们说出显示现有的代理商,或者用户所属的代理商名称.

我的合同:agency.sol

pragma solidity ^0.4.18;
// We have to specify what version of compiler this code will compile with

contract Agency {

  event NewAgency(uint agencyId, string name, uint dna);

  uint dnaDigits = 16;
  uint dnaModulus = 10 ** dnaDigits;

  //agency structure
  struct Agency {
    string name;
    uint dna;
  }

  Agency[] public agencies;

  mapping (uint => address) public agencyToOwner;
  mapping (address => uint) ownerAgencyCount;

  function _createAgency(string _name, uint _dna) private {
    uint id = agencies.push(Agency(_name, _dna)) - 1;
    agencyToOwner[id] …
Run Code Online (Sandbox Code Playgroud)

javascript ethereum web3js geth

10
推荐指数
1
解决办法
2万
查看次数

如何计算 RSK 上每笔交易的平均 Gas 成本?

我想计算过去三个月内RSK上所有交易支付的平均汽油费。我尝试使用Web3.js库查询 RSK 区块链。为了获取单个区块高度的交易数据,我必须这样做:

const txsPerBlock = await web3.getBlockTransactionCount(block.height);
const txs = await Promise.all(
    [...Array(txsPerBlock).keys()].map(
        (i) => web3.eth.getTransactionFromBlock(block.height, i))
);
Run Code Online (Sandbox Code Playgroud)

然后计算平均gas费用:

const averageGasFee = txs.reduce(
    (p, c) => p + Number(c.gasPrice) * c.gas,
    0
) / txs.length;
Run Code Online (Sandbox Code Playgroud)

然而,在 3 个月内,RSK 上约有 230,000 个区块,其中每个区块约有 3 笔交易。因此,我必须轮询大约 600,000 笔交易,而这需要很长时间才能完成。我等了一个小时然后取消了剧本。有没有更好的方法来查询 RSK 区块链并计算如此大量的区块和交易的平均交易 Gas 费用?

javascript web3js rsk

10
推荐指数
2
解决办法
1637
查看次数

以太坊Web3.js返回"无法找到模块'web3-requestManager'"

我正在尝试在macosx上开始使用带有节点6.11.1的Ethereum Web3.js

我使用以下命令安装了web3:

npm安装web3

然后我启动这个 - 显然 - 简单的节点命令:

Web3 = require('web3');

好吧,它返回以下错误:

module.js:471
    throw err;
    ^

Error: Cannot find module 'web3-requestManager'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/Users/fremente/Dropbox/Influx Design/Web htdocs/ethereum/node_modules/web3/packages/web3-core/src/index.js:26:22)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
Run Code Online (Sandbox Code Playgroud)

看起来它需要一些未随包安装的模块(例如'web3-requestManager').

这是我的package.json

{
  "name": "ethereum",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "dependencies": {
    "ethereumjs-testrpc": "^4.0.1",
    "solc": "^0.4.13",
    "web3": "^1.0.0-beta2"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo …
Run Code Online (Sandbox Code Playgroud)

javascript node.js ethereum web3js

9
推荐指数
2
解决办法
6263
查看次数

未处理的拒绝错误:无效的JSON RPC响应:""

我试图在我的ERC20令牌合同上调用一个方法.我正在连接到" https://rinkeby.infura.io/"httpProvider.我可以调用()常量方法,但是当我想通过调用send()函数来改变契约的状态时,我得到了这个提到的错误.如果您认为发布ABI JSON或Solidity合同有帮助,我也可以提供.我认为我的问题纯粹与web3有关.我想我需要签署交易(逻辑上),但web3文档没有提到任何内容.http://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-send

这是我得到的错误:

Unhandled rejection Error: Invalid JSON RPC response: ""
at Object.InvalidResponse (/opt/backend/node_modules/web3-core-helpers/src/errors.js:42:16)
at XMLHttpRequest.request.onreadystatechange (/opt/backend/node_modules/web3-providers-http/src/index.js:60:32)
at XMLHttpRequestEventTarget.dispatchEvent (/opt/backend/node_modules/xhr2/lib/xhr2.js:64:18)
at XMLHttpRequest._setReadyState (/opt/backend/node_modules/xhr2/lib/xhr2.js:354:12)
at XMLHttpRequest._onHttpResponseEnd (/opt/backend/node_modules/xhr2/lib/xhr2.js:509:12)
at IncomingMessage.<anonymous> (/opt/backend/node_modules/xhr2/lib/xhr2.js:469:24)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1055:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
From previous event:
at PromiEvent (/opt/backend/node_modules/web3-core-promievent/src/index.js:35:24)
at send (/opt/backend/node_modules/web3-core-method/src/index.js:446:21)
at Object._executeMethod (/opt/backend/node_modules/web3-eth-contract/src/index.js:890:24)
at Object.currencyToken.sendSignUpBonousTokens (/opt/backend/server/common/token/currency-token.js:86:56)
at <anonymous>
Run Code Online (Sandbox Code Playgroud)

从调用此方法:

const Web3 = require('web3');
const Tx = require('ethereumjs-tx');
const …
Run Code Online (Sandbox Code Playgroud)

javascript ethereum solidity web3js web3-donotuse

9
推荐指数
1
解决办法
2438
查看次数

MetaMask 不注入 window.ethereum:未捕获(承诺中)TypeError:无法读取未定义的属性“请求”

首先,让我提一下这是一个浏览器内项目,所以我只能使用

<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

几个月前,我制作了一个 dapp,即使我从未设置过提供程序,它也能正常工作,所以我猜它使用了 MetaMask 提供的提供程序。但是,我在这里使用指南,唯一的问题是以下代码,

<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

此代码用于在元掩码用户在我的 dapp 上批准元掩码后输出元掩码用户的地址,但是我收到了错误

Uncaught (in promise) TypeError: Cannot read property 'request' of undefined
Run Code Online (Sandbox Code Playgroud)

该错误只是说,如果我必须添加提供程序,我的实际目标是使用元掩码中的提供程序,并允许连接到元掩码,其余功能(例如传输或其他我知道如何处理它们),它只是当我尝试加载页面时,似乎我的问题出现了。

我没有可用的以太节点,也不打算使用nodejs,只有一个显示Metamask地址的html文件,因此使用浏览器内的web3js。

我希望这只是我没有意识到一些简单的事情,因为我似乎找不到现在不能使用 web3js 的原因。

javascript browser ethereum web3js metamask

9
推荐指数
2
解决办法
3万
查看次数

有没有办法向Metamask钱包发起断开连接请求?

我正在构建一个分散的应用程序,用户可以在其中将他们的加密货币钱包 (Metamask) 连接到我的网站。

They can initiate a connection request by clicking a button. On success, the wallet is connected and my website can interact with it.

Is there any way to initiate a disconnect request? Similar to a 'Log out' button. Currently, the users have to manually disconnect their wallet within Metamask settings which is not a straightforward process.

blockchain ethereum web3js web3-donotuse metamask

9
推荐指数
1
解决办法
843
查看次数

getTransaction 与 getTransactionReceipt

web3库中的web3.eth.getTransaction两种方法有什么区别?web3.eth.getTransactionReceipt我尝试阅读此文档https://web3js.readthedocs.io/en/v1.2.0/web3-eth.html#gettransactionreceipt但我不清楚其中的区别。

ethereum web3js

9
推荐指数
1
解决办法
2万
查看次数

如何以编程方式将 ethers.js 库与 Rinkeby 连接?

根据ethers.js官方文档,这应该是使用自定义数据连接到特定网络(如Rinkeby -testnet)的方式:

 const provider = ethers.getDefaultProvider(network, {
    etherscan: ETHERSCAN_API_KEY,
    infura: INFURA_API_KEY,
Run Code Online (Sandbox Code Playgroud)

此外,这也是获得签名者来签署交易的方法:

 const signer = provider.getSigner()
Run Code Online (Sandbox Code Playgroud)

但是,现在默认提供程序提供了“getSigner”方法。

类型错误:provider.getSigner 不是函数

如何使用 ethers.js 实现这一点?

ethereum web3js ethers.js

9
推荐指数
3
解决办法
8302
查看次数

BEP-20 代币交易中的交易被低估

我已经在币安智能链的 Binance-Peg BUSD-T 中进行了一些交易,并且成功运行。但5次交易后。我遇到一个问题说Returned error: transaction underpriced!这是我的代码:

const web3 = new Web3('https://bsc-dataseed1.binance.org:443');

const contract = new web3.eth.Contract(abi, usdtContractAddr, {
  from: 'SENDER_ADDRESS', // default from address
  gasPrice: '200000000' // default gas price in wei, 20 gwei in this case
});

web3.eth.accounts.wallet.add('SENDER_PRIVATE_KEY');
const receipt = await contract.methods.transfer('TO_ADDRESS', '1000000000000000000').send({
    from: 'SENDER_ADDRESS',
    gas: 100000
});
Run Code Online (Sandbox Code Playgroud)

我已经将我的 Gas 增加了 10%,并添加了比通过调用 给我的值更多的随机数 web3.eth.getTransactionCount('ADDRESS')。但它们都不起作用。我曾经在 Binance-Peg BUSD-T 上进行过很多交易,所以这对我来说是一个大问题。有办法解决这个问题吗???

node.js ethereum smartcontracts web3js binance-smart-chain

9
推荐指数
2
解决办法
3万
查看次数

Wallet Connect / Web3 Provider 仅通过 imprting 显示大量错误

我想在我的项目中使用“@walletconnect/web3-provider”JS库,但是一旦我将库与Web3模块一起安装(它们将一起使用)

npm install --save web3 @walletconnect/web3-provider
Run Code Online (Sandbox Code Playgroud)

然后将其导入到我的项目中(在 React 项目的 App.js 文件中)

import WalletConnectProvider from '@walletconnect/web3-provider';
Run Code Online (Sandbox Code Playgroud)

它抱怨这么多错误!

19 errors have detailed information that is not shown.
Use 'stats.errorDetails: true' resp. '--stats-error-details' to show it.

webpack 5.68.0 compiled with 19 errors and 110 warnings in 23859 ms
Run Code Online (Sandbox Code Playgroud)

以下是出现在服务器端(在 VS Code 终端中)的错误消息部分:

WARNING in ./node_modules/@walletconnect/jsonrpc-types/dist/esm/index.js
Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map from 'F:\Work\Tellor\testField\test-wallet-connect\node_modules\@walletconnect\jsonrpc-types\src\index.ts' file: Error: ENOENT: no such file or directory, open 'F:\Work\Tellor\testField\test-wallet-connect\node_modules\@walletconnect\jsonrpc-types\src\index.ts'        
 @ ./node_modules/@walletconnect/jsonrpc-utils/dist/esm/types.js 1:0-45 1:0-45
 @ ./node_modules/@walletconnect/jsonrpc-utils/dist/esm/index.js 6:0-24 …
Run Code Online (Sandbox Code Playgroud)

javascript node.js npm web3js

9
推荐指数
2
解决办法
1万
查看次数