标签: web3js

智能合约方法不是web3中的功能

我正在尝试遵循 web3 上的旧教程,但收到错误,我认为这是由于 Solidity 更新所致。我有如下所示的代码

var express = require("express"),
    Web3 = require("web3"),
    web3;    
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];

//define contract variable using ABI from compiled Remix tab.
var myContract = new web3.eth.Contract([abi_data]);

myContract.options.address = 'contract_address';

myContract.methods.totalSupply(function(err,res){
    if(!err){
        console.log(res);
    } else {
        console.log(err);
    }
})
Run Code Online (Sandbox Code Playgroud)

其中abi_data是我的合约的abi数据,contract_address是我的合约在Roptsen测试网络中的实际地址,其中totalSupply()是Ropsten测试网络上我的solidity智能合约中的方法,该方法返回合约中引用的代币的总供应量。当测试它以node app.js查看是否正确记录时,返回此错误......

/home/ubuntu/workspace/node_modules/web3-eth-contract/src/index.js:693
    throw errors.InvalidNumberOfParams(args.length, this.method.inputs.length, this.method.name); …
Run Code Online (Sandbox Code Playgroud)

javascript ethereum solidity web3js

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

简单的 Solidity 合约和脚本出现无效操作码错误

我是 Solidity 和 web3.js 的新手。我正在遵循这里的教程 -

https://medium.com/@mvmurthy/full-stack-hello-world-voting-ethereum-dapp-tutorial-part-1-40d2d0d807c2

构建一个简单的投票 Dapp。我已经使用 npm 在本地 node_modules 文件夹中安装了 ganache-cli、solc 和 web3 版本 0.20.2。Solidity 中的 Voting.sol 合约是:

pragma solidity ^0.4.18;

contract Voting {

  mapping (bytes32 => uint8) public votesReceived;
  bytes32[] public candidateList;

  function Voting(bytes32[] candidateNames) public {
    candidateList = candidateNames;
  }

  function totalVotesFor(bytes32 candidate) view public returns (uint8) {
    return votesReceived[candidate];
  }
}
Run Code Online (Sandbox Code Playgroud)

使用以下名为 vote_main.js 的脚本:

Web3 = require('web3')
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"))

fs = require('fs')
code = fs.readFileSync('Voting.sol').toString()

solc = require('solc')
compiledCode = solc.compile(code) …
Run Code Online (Sandbox Code Playgroud)

javascript blockchain ethereum solidity web3js

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

web3.eth.abi.decodeLog 返回错误的日志参数值

我有一个以太坊合约,其事件定义如下:

event Apple(address indexed a, address b, address c);
Run Code Online (Sandbox Code Playgroud)

该事件被触发,我可以在交易收据中看到日志。

通过 web3,当我尝试解析收据中的日志时,我能够检索事件参数,但看起来 的值a始终相同。

// compiled is the built contract. address is the contract address
const contract = new web3.eth.Contract(compiled.abi, address)

const eventJsonInterface = _.find(
  contract._jsonInterface,
  o => o.name === 'Apple' && o.type === 'event',
)

const log = _.find(
    receipt.logs,
    l => l.topics.includes(eventJsonInterface.signature)
)

web3.eth.abi.decodeLog(eventJsonInterface.inputs, log.data, log.topics)
Run Code Online (Sandbox Code Playgroud)

我最终得到的是:

Result {
  '0': '0x42087b16F33E688a9e73BFeef94F8F2bd2BfC98f',
  '1': '0xfc36bFe712f30F75DF0BA9A60A109Ad51ac7Ca38',
  '2': '0x6915d2f3D512F7CfEF968f653D1cA3ed4489798C',
  __length__: 3,
  a: '0x42087b16F33E688a9e73BFeef94F8F2bd2BfC98f',
  b: '0xfc36bFe712f30F75DF0BA9A60A109Ad51ac7Ca38',
  c: '0x6915d2f3D512F7CfEF968f653D1cA3ed4489798C' }
Run Code Online (Sandbox Code Playgroud)

其中a触发的事件之间始终是相同的地址。我为每笔交易生成一个新合约,a …

javascript ethereum solidity web3js

5
推荐指数
1
解决办法
4621
查看次数

如何在 web3.js 中使用 toWei() 函数来处理具有 2 位小数的标记?

我正在尝试使用名为 toWei() 的 web3js 函数将金额转换为 wei。

\n\n

这是我的代码:

\n\n
     var etherwithdrawamount1=web3Infura.utils.toWei(etherwithdrawamount, \'wei\'); \n
Run Code Online (Sandbox Code Playgroud)\n\n

现在的问题是代币金额计算不正确,因为代币有 2 位小数。

\n\n

在 web3 文档中:https://web3js.readthedocs.io/en/1.0/web3-utils.html#towei \n没有 2 位小数。

\n\n

number - String|Number|BN:值。

\n\n

unit - 字符串(可选,默认为“ether”):要转换的以太。

\n\n

可能的单位有:

\n\n
\n

诺特: \xe2\x80\x980\xe2\x80\x99
\n 威: \xe2\x80\x981\xe2\x80\x99
\n 奎: \xe2\x80\x981000\xe2\x80\x99
\n 奎: \xe2\x80\x981000\xe2\x80\x99
\n 巴贝奇: \xe2\x80\x981000\xe2\x80\x99
\n 毫微微以太网: \xe2\x80\x981000\xe2\x80\x99
\n mwei: \xe2 \x80\x981000000\xe2\x80\x99
\n Mwei:\xe2\x80\x981000000\xe2\x80\x99
\nlovelace:\xe2\x80\x981000000\xe2\x80\x99
\n 皮以太:\xe2\x80 \x981000000\xe2\x80\x99
\n gwei: \xe2\x80\x981000000000\xe2\x80\x99
\n Gwei: \xe2\x80\x981000000000\xe2\x80\x99
\n 香农:\xe2\x80\x9810000 00000 \xe2\x80\x99
\n 纳米以太:\xe2\x80\x981000000000\xe2\x80\
x99\n 纳米:\xe2\x80\x981000000000\xe2\x80\x99
\n 萨博:\xe2\x80\x981000000000000\xe …

decimal token web3js

5
推荐指数
1
解决办法
5074
查看次数

尝试使用 npm 安装 web3 时找不到 web3/dist/web3.min.js

我尝试通过执行以下操作使用 node.js 安装 web3 npm install web3 --save,但是当我检查node_modules/文件夹时,任何地方都没有dist文件夹及其web3.min.js文件。关于如何或为什么的任何想法?

ethereum web3js

5
推荐指数
1
解决办法
1389
查看次数

运行 truffle migrate 时出现“Web3ProviderEngine 不支持同步请求”

我想使用提供者配置我的 truffle-config.js。当我运行命令“truffle migrate --network ropsten”时,它会抛出以下错误:

错误:Web3ProviderEngine 不支持同步请求。

并告诉了错误详细信息

at Object.run (C:\Users\Bruce\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\truffle-migrate\index.js:92:1)

我对此一无所知。我查找文件 "C:\Users\Bruce\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\truffle-migrate\index.js:92:1"webpack ,但我找不到下面的路径这"build/"。是不是出了什么问题?我使用 global 安装 truffle,它在默认网络 ganache 下运行良好。

ropsten: {
      provider: () => new HDWalletProvider(
        privateKeys.split(','),
        `https://ropsten.infura.io/v3/${process.env.INFURA_API_KEY}`
      ),
      network_id: 3,       // Ropsten's id, mainnet is 1
      gas: 5500000,        // Ropsten has a lower block limit than mainnet
      gasPrice: 2500000000, //2.5 gwei
      confirmations: 2,    // # of confs to wait between deployments. (default: 0)
      timeoutBlocks: 200,  // # of blocks before a deployment times out  (minimum/default: 50) …
Run Code Online (Sandbox Code Playgroud)

ethereum solidity truffle web3js

5
推荐指数
1
解决办法
2873
查看次数

如何使用 web3.js 购买或出售 erc20 代币?

我将在 web3.js 中使用哪个函数从我的网站购买 erc20 令牌?

ico web3js erc20

5
推荐指数
1
解决办法
3617
查看次数

未捕获(承诺中)错误:给定选项和默认选项中均未指定“发件人”地址

我想知道是否有人可以指导我关于我正在从事的项目的正确方向,该项目是 Udemy 课程的一部分(以太坊和 Solidity 完整的开发人员指南)

因此,我目前正在致力于整合 Kickstarter 替代方案的前端。我面临的问题在于该new.js文件作为代表新页面的JS文件,其中包含一个按钮,使用户能够创建新的活动(通过metamask进行实际交易)。

import React, { Component } from 'react';
import { Form, Button, Input } from 'semantic-ui-react';
import Layout from '../../components/Layout';
import factory from '../../ethereum/factory';
import web3 from '../../ethereum/web3';
require('babel-polyfill');

class CampaignNew extends Component {
  state = {
    minimumContribution: ''
  };

  //to record and track user changes and inputs
  onSubmit = async (event) => {
    event.preventDefault();

    const accounts = await web3.eth.getAccounts();
    await factory.methods
      .createCampaign(this.state.minimumContribution)
      .send({ from: accounts[0] });
  }; …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs blockchain ethereum web3js

5
推荐指数
1
解决办法
4930
查看次数

如何使用 ethers.utils.defaultAbiCoder 解码交易输入数据

我正在使用 Etherscan API 获取交易数据。这是我得到的示例结果:

{
  blockNumber: '7409930',
  timeStamp: '1639151980',
  hash: '...',
  nonce: '4124',
  ...
  input: '0x9d90e4c8000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000002000000000000000000000000093238bb66b5d15b4152c5db574e3397ff1b1a450',
  contractAddress: '',
  cumulativeGasUsed: '403775',
  gasUsed: '1162315',
  confirmations: '191308'
}
Run Code Online (Sandbox Code Playgroud)

我现在需要找出该交易的事件类型(合约方法,例如,,...)TransferOwnershipstakeTokens该数据存储在input该对象的属性中。

我设法使用abi-decoder库来完成此任务,但我想使用 ethers 的实用方法(以哪种方式)来完成同样的事情。

我当前的实现:

const abiDecoder = require("abi-decoder");
abiDecoder.addABI(contractAbi);
// "item" is transaction data, input property is encoded stuff from which I want to get the contract method used by this transaction
const decodedInput = abiDecoder.decodeMethod(item.input);

// contract method
console.log(decodedInput.name);
Run Code Online (Sandbox Code Playgroud)

我正在阅读以太坊的文档(https://docs.ethers.io/v5/api/utils/abi/coder/),但我无法弄清楚。

javascript ethereum web3js etherscan ethers.js

5
推荐指数
1
解决办法
4220
查看次数

无法访问Vue3JS Vite项目中的流程变量

我正在创建一个 vue3 应用程序(使用 Vite 创建),它与用 Solidity 编写并存储在 Ropsten 上的智能合约进行交互。因此,我使用 web3js 与我的智能合约以及 web3.storage 进行交互,以便在 IPFS 上存储一些图像。我的项目根目录中有一个.env文件,用于存储 web3.storage 的 API 密钥:

VUE_APP_API_TOKEN=VALUE
VITE_API_TOKEN=VALUE
Run Code Online (Sandbox Code Playgroud)

问题是显然 web3.storage 期望 API 令牌存储在 process.env 中,而我无法process从我的应用程序访问全局变量。我总是收到错误Uncaught ReferenceError: process is not defined

我认为,这与我使用 Vite 而不是纯粹的 Vue3 有关。我尝试vite.config.ts使用该代码导出文件中的进程环境,但它不起作用:

export default ({ mode }) => {
   process.env = { ...process.env, ...loadEnv(mode, process.cwd(), '') };

   console.log(process.env.VITE_API_TOKEN)         //Works fine: VALUE is logged
   console.log(process.env.VUE_APP_API_TOKEN)      //Works fine: VALUE is logged

   return defineConfig({
       plugins: [vue()]
   });
}
Run Code Online (Sandbox Code Playgroud)

如何 …

javascript vue.js ipfs web3js vite

5
推荐指数
1
解决办法
7743
查看次数