【问题标题】:How to make sure Web3 / Metamask recognize that I am the smart contract owner?如何确保 Web3 / Metamask 识别我是智能合约所有者?
【发布时间】: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


    【解决方案1】:

    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'});
    

    但是,由于该函数实际上只是返回合约地址余额,您也可以执行变通方法,直接从区块链查询合约地址余额(无需调用合约函数):

    await web3.eth.getBalance('0x98608C1e3ae104E7A11EA2879b44669a1c38b73D');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-20
      • 2021-11-28
      • 1970-01-01
      • 2019-03-01
      • 2018-07-10
      • 1970-01-01
      • 2022-09-28
      • 2019-09-12
      相关资源
      最近更新 更多