如何确保 Web3 / Metamask 识别出我是智能合约所有者?
How to make sure Web3 / Metamask recognize that I am the smart contract owner?
我正在尝试从我创建并在主网上可用的智能合约中读取一些数据:
https://snowtrace.io/address/0x98608c1e3ae104e7a11ea2879b44669a1c38b73d#code
当我尝试通过 Web3 库与我的智能合约交互时,我能够读取所有 public 详细信息,除了一个名为: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()?
smart的getCurrentBalance()
函数定义为:
function getCurrentBalance() public view returns(uint) {
require(owner == msg.sender, 'You are not the owner!!!');
require(paused == false, 'Contract Status: Paused!!!');
return address(this).balance;
}
在您的情况下,第一个 require()
条件失败并显示“您不是所有者!!!”错误消息,因为调用函数的地址不是 owner
变量中存储的预期值。
系统的解决方案是从 owner
地址调用 getCurrentBalance()
函数:
// where `0x123` is the `owner` address
await window.contract.methods.getCurrentBalance().call({from: '0x123'});
但是,由于该函数实际上只是 returns 合约地址余额,您还可以执行变通方法,直接从区块链查询合约地址余额(无需调用合约函数):
await web3.eth.getBalance('0x98608C1e3ae104E7A11EA2879b44669a1c38b73D');
我正在尝试从我创建并在主网上可用的智能合约中读取一些数据: https://snowtrace.io/address/0x98608c1e3ae104e7a11ea2879b44669a1c38b73d#code
当我尝试通过 Web3 库与我的智能合约交互时,我能够读取所有 public 详细信息,除了一个名为: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()?
smart的getCurrentBalance()
函数定义为:
function getCurrentBalance() public view returns(uint) {
require(owner == msg.sender, 'You are not the owner!!!');
require(paused == false, 'Contract Status: Paused!!!');
return address(this).balance;
}
在您的情况下,第一个 require()
条件失败并显示“您不是所有者!!!”错误消息,因为调用函数的地址不是 owner
变量中存储的预期值。
系统的解决方案是从 owner
地址调用 getCurrentBalance()
函数:
// where `0x123` is the `owner` address
await window.contract.methods.getCurrentBalance().call({from: '0x123'});
但是,由于该函数实际上只是 returns 合约地址余额,您还可以执行变通方法,直接从区块链查询合约地址余额(无需调用合约函数):
await web3.eth.getBalance('0x98608C1e3ae104E7A11EA2879b44669a1c38b73D');