【发布时间】:2021-12-28 08:53:33
【问题描述】:
我正在尝试从我创建并在此处的主网上可用的智能合约中读取一些数据: https://snowtrace.io/address/0x98608c1e3ae104e7a11ea2879b44669a1c38b73d#code
当我尝试通过 Web3 库与我的智能合约进行交互时,我能够读取所有公共详细信息,但名为 getCurrentBalance() 的除外
这需要所有者许可(我是)。我三重检查以确保我在 Metamask 中使用正确的所有者帐户。
这是我的 html 文件:
<!DOCTYPE html>
<html>
<head>
<title>Web 3 Demo</title>
<script src='node_modules/web3/dist/web3.min.js'></script>
</head>
<body>
<button onclick="getCurrentBalance();">Get Current Contract Balance</button>
Status: <span id="status">Loading...</span>
<script type="text/javascript" src="index.js"></script>
</body>
</html>
这是 index.js:
// Check metamask installation and connect to site
async function loadWeb3() {
if (window.ethereum) {
window.web3 = new Web3(window.ethereum);
window.ethereum.enable();
}
}
// In the html file there is a span called status this function updates the print out
function updateStatus(status) {
const statusEl = document.getElementById('status');
statusEl.innerHTML = status;
console.log(status);
}
// This function links to the smart contract for interactions
async function loadContract() {
return await new window.web3.eth.Contract([{"inputs": [],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"constant":false,"inputs":[{"internalType":"address payable","name":"_liquitdateTo","type":"address"}],"name":"destroySmartContract","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"fundsReceived","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"getCurrentBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"getFunds","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":false,"inputs":[{"internalType":"bool","name":"_bool","type":"bool"}],"name":"pause","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"smartContractAddress","outputs":[{"internalType":"address","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"tresury","outputs":[{"internalType":"address payable","name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"}], '0x98608C1e3ae104E7A11EA2879b44669a1c38b73D');
}
// This is the main program function that calls all the previous ones
async function load() {
await loadWeb3();
window.contract = await loadContract();
updateStatus('Ready!');
}
async function getCurrentBalance() {
updateStatus('fetching...');
const getCurrentBalance = await window.contract.methods.getCurrentBalance().call();
updateStatus(`Current Contract Balance: ${getCurrentBalance}`);
}
load();
这是我在控制台中遇到的错误:
如何从智能合约中读取 getCurrentBalance()?
【问题讨论】:
标签: ethereum smartcontracts web3 web3js