通过 web3.js 调用智能合约函数没有响应

Posted

技术标签:

【中文标题】通过 web3.js 调用智能合约函数没有响应【英文标题】:Call smart contract function by web3.js without response 【发布时间】:2019-08-27 08:52:43 【问题描述】:

我正在观看 Youtube 上的教程并按照说明进行操作: https://www.youtube.com/watch?time_continue=1350&v=msT3tpwnyv8

当我尝试使用下面的 js 文件调用该函数时,它在控制台上没有返回任何内容。有谁知道发生了什么?

我正在使用, web3.js 1.0.0 测试版。 52 infura 连接

var Tx = require('ethereumjs-tx')

const Web3 = require('web3');
const web3 = new Web3('https://ropsten.infura.io/v3/project_id')


const contractAddress = '0xd03696B53924972b9903eB17Ac5033928Be7D3Bc'
const contractABI = ["constant":true,"inputs":[],"name":"name","outputs":["name":"","type":"string"],"payable":false,"stateMutability":"view","type":"function","constant":false,"inputs":["name":"_spender","type":"address","name":"_value","type":"uint256"],"name":"approve","outputs":["name":"success","type":"bool"],"payable":false,"stateMutability":"nonpayable","type":"function","constant":true,"inputs":[],"name":"totalSupply","outputs":["name":"","type":"uint256"],"payable":false,"stateMutability":"view","type":"function","constant":false,"inputs":["name":"_from","type":"address","name":"_to","type":"address","name":"_value","type":"uint256"],"name":"transferFrom","outputs":["name":"success","type":"bool"],"payable":false,"stateMutability":"nonpayable","type":"function","constant":true,"inputs":[],"name":"standard","outputs":["name":"","type":"string"],"payable":false,"stateMutability":"view","type":"function","constant":true,"inputs":["name":"","type":"address"],"name":"balanceOf","outputs":["name":"","type":"uint256"],"payable":false,"stateMutability":"view","type":"function","constant":true,"inputs":[],"name":"symbol","outputs":["name":"","type":"string"],"payable":false,"stateMutability":"view","type":"function","constant":false,"inputs":["name":"_to","type":"address","name":"_value","type":"uint256"],"name":"transfer","outputs":["name":"success","type":"bool"],"payable":false,"stateMutability":"nonpayable","type":"function","constant":true,"inputs":["name":"","type":"address","name":"","type":"address"],"name":"allowance","outputs":["name":"","type":"uint256"],"payable":false,"stateMutability":"view","type":"function","inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor","anonymous":false,"inputs":["indexed":true,"name":"_from","type":"address","indexed":true,"name":"_to","type":"address","indexed":false,"name":"_value","type":"uint256"],"name":"Transfer","type":"event","anonymous":false,"inputs":["indexed":true,"name":"_owner","type":"address","indexed":true,"name":"_spender","type":"address","indexed":false,"name":"_value","type":"uint256"],"name":"Approval","type":"event"]

var contract = new web3.eth.Contract(contractABI, contractAddress)


contract.methods.totalSupply().call((err, result)=> 
  console.log(result)
)

【问题讨论】:

嗨@Eric,你能不能多解释一下你的问题,让更多人理解。 @Samer Abu Gahgah 感谢您的回复。当然,我试图与以太坊 Ropsten 网络中的智能合约进行交互。我使用 web3.js 调用该函数,该函数应该回复该合约中的令牌总数。我尝试在控制台中显示响应,但什么也没发生。没有响应也没有错误。 【参考方案1】:

试试:

contract.methods.totalSupply().call().then((result) => console.log(result) );

它应该返回一个 BigNumber。

【讨论】:

以上是关于通过 web3.js 调用智能合约函数没有响应的主要内容,如果未能解决你的问题,请参考以下文章

再深刻理解下web3.js中estimateGas如何计算智能合约消耗的gas量

以太坊Dapp开发通过web3.js调用智能合约

关于web3.js中与交易发送交易签名智能合约函数调用相关api的理解

Web3js - 部署后立即返回智能合约地址

web3使用web3.js发布并执行智能合约

部署智能合约时出现节点错误 - Web3.js