我试图使用web3打印智能合同的余额,但它没有显示任何错误,但在客户端应用程序上,它如下所示:
你的余额:
/合同/临时合同:
function () external payable {
// payable fallback to receive and store ETH
if(msg.value < 10 ether){
revert();
}
balanceOf[msg.sender] += msg.value;
}
function getBalance() external view returns (uint) {
return address(this).balance; //this keyword refers to the smart contract address
}/src/js/app.js:
App.contracts.kyc.deployed().then(function(instance) {
kycInstance = instance;
var bal=kycInstance.getBalance();
$("#SmartBalance").html("Your Balance: " + bal);
// #smartbalance is an id for the HTML file
});发布于 2020-05-16 13:59:17
Javascript函数kycInstance.getBalance返回一个需要解析的Promise对象,以便获得从相应的Solidity函数返回的实际值。
例如,要做到这一点,一种方法是改变这种情况:
kycInstance.getBalance();
$("#SmartBalance").html("Your Balance: " + bal);对此:
kycInstance.getBalance().then(function(bal) {
$("#SmartBalance").html("Your Balance: " + bal);
});注意,由于Solidity函数返回一个uint,Javascript中的bal类型取决于您使用的web3.js版本:
typeof bal == BigNumber上typeof bal == String上因此,如果您使用的是web3.jsv0.x,那么最好打印"Your Balance: " + bal.toFixed()。
https://ethereum.stackexchange.com/questions/83467
复制相似问题