简体   繁体   English

返回错误:VM Exception while processing transaction: revert only owner can call this function

[英]Returned error: VM Exception while processing transaction: revert only owner can call this function

I'm trying to test this contract using "truffle test" but it show the following error: Error: Returned error: VM Exception while processing transaction: revert only owner can call this function - - Reason given: Only owner can call this function.我正在尝试使用“松露测试”测试此合同,但它显示以下错误:错误:返回错误:处理事务时的 VM 异常:还原只有所有者可以调用此 function - - 给出的原因:只有所有者可以调用此 function。

Question Open the TestGaming.sol contract in the test folder.问题 打开 test 文件夹中的 TestGaming.sol 合约。 Write a test for the function withdrawFunds.为 function 提取资金编写一个测试。 Your test should check the balance of the owner who called the withdrawFunds function.您的测试应该检查调用withdrawFunds function 的所有者的余额。 Your test should also verify if the owner's balance has increased by 10 ether.您的测试还应该验证所有者的余额是否增加了 10 以太币。

Gaming.sol游戏.sol

pragma solidity ^0.5.0;

contract Gaming {
    /* Our Online gaming contract */
    address public owner;
    bool public online;

    struct Player {
        uint wins;
        uint losses;
    }

    mapping (address => Player) public players;

    constructor() public payable {
        owner = msg.sender;
        online = true;
    }

    modifier isOwner() {
        require(msg.sender == owner, "Only owner can call this function");
        _;
    }

event GameFunded(address funder, uint amount);
    event PlayerWon(address player, uint amount, uint mysteryNumber, uint displayedNumber);
    event PlayerLost(address player, uint amount, uint mysteryNumber, uint displayedNumber);

    function mysteryNumber() internal view returns (uint) {
        uint randomNumber = uint(blockhash(block.number-1))%10 + 1;
        return randomNumber;
    }

    function determineWinner(uint number, uint display, bool guess) public pure returns (bool) {
        if (guess == true && number > display) {
            return true;
        } else if (guess == true && number < display) {
            return false;
        } else if (guess == false && number > display) {
            return false;
        } else if (guess == false && number < display) {
            return true;
        }
    }

    function winOrLose(uint display, bool guess) external payable returns (bool, uint) {
        /* Use true for a higher guess, false for a lower guess */
        require(online == true, "The game is online");
        require(msg.sender.balance > msg.value, "Insufficient funds");
        uint mysteryNumber_ = mysteryNumber();
        bool isWinner = determineWinner(mysteryNumber_, display, guess);
        Player storage player = players[msg.sender];
        /* Player Won */
        if (isWinner == true) {
            player.wins += 1;
            msg.sender.transfer(msg.value * 2);
            emit PlayerWon(msg.sender, msg.value, mysteryNumber_, display);
            return (true, mysteryNumber_);
          /* Player Lost */
        } else if (isWinner == false) {
            player.losses += 1;
            emit PlayerLost(msg.sender, msg.value, mysteryNumber_, display);
            return (false, mysteryNumber_);
        }
    }
}

TestGaming.sol测试游戏.sol

pragma solidity ^0.5.0;

import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/Gaming.sol";

contract TestGaming {
    uint public initialBalance = 10 ether;
    Gaming gaming;
    address owner;

    function beforeAll() public {
        gaming = Gaming(DeployedAddresses.Gaming());
        owner = gaming.owner();

    }

    function testWithdrawFunds() public {
        uint ownerBalanceBefore = owner.balance;
        gaming.withdrawFunds();
        uint ownerBalanceAfter = owner.balance;

        Assert.equal (initialBalance, ownerBalanceAfter - ownerBalanceBefore, "The owner's balance should have increased by 10 ether");
}
function testPlayerWonGuessHigher() public {
        bool expected = true;
        bool result = gaming.determineWinner(5, 4, true);

        Assert.equal(expected, result, "The player should have won by guessing the mystery number was higher than their number");
    }

    function testPlayerWonGuessLower() public {
        bool expected = true;
        bool result = gaming.determineWinner(5, 6, false);

        Assert.equal(expected, result, "The player should have won by guessing the mystery number was lower than their number");
    }

    function testPlayerLostGuessLower() public {
        bool expected = false;
        bool result = gaming.determineWinner(5, 4, false);

        Assert.equal(expected, result, "The player should have lost by guessing the mystery number was lower than their number");
    }

    function testPlayerLostGuessHigher() public {
        bool expected = false;
        bool result = gaming.determineWinner(5, 6, true);

        Assert.equal(expected, result, "The player should have lost by guessing the mystery number was higher than their number");
    }
}

Error错误

在此处输入图像描述

The problem is that you are testing your contract through another contract.问题是您正在通过另一个合同测试您的合同。 Since the function withdraw requires the caller to be the owner which is the deployer of the contract, the check fails since TestGaming.sol is calling the function;由于 function withdraw要求调用者是合约的owner ,即合约的部署者,因此由于TestGaming.sol正在调用 function,因此检查失败; TestGaming.sol is not the deployer of the Gaming contract. TestGaming.sol不是Gaming合约的部署者。

You have to instantiate the Gaming contract from inside the TestGaming contract.您必须从TestGaming合约内部实例化Gaming合约。

pragma solidity ^0.5.0;

import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/Gaming.sol";

contract TestGaming {
    uint public initialBalance = 10 ether;
    Gaming gaming;
    address owner;

    function beforeAll() public {
    //---Instead of grabbing Gaming address, deploy it fresh for the test
        gaming = new Gaming();
        owner = gaming.owner();

    }

    function testWithdrawFunds() public {
        uint ownerBalanceBefore = owner.balance;
        gaming.withdrawFunds();
        uint ownerBalanceAfter = owner.balance;

        Assert.equal (initialBalance, ownerBalanceAfter - ownerBalanceBefore, "The owner's balance should have increased by 10 ether");
}

.
.
.

}

This way, TestGaming will be the deployer and thus owner for Gaming and the test will pass.这样, TestGaming将成为Gaming的部署者和owner ,测试将通过。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:返回错误:处理事务时 VM 异常:还原只有所有者可以调用此 function - Error: Returned error: VM Exception while processing transaction: revert only owner can call this function Chainlink 返回错误:处理事务时出现 VM 异常:还原 - Chainlink Returned error: VM Exception while processing transaction: revert 错误:返回错误:处理事务时出现 VM 异常:使用 New 创建时恢复 - Error: Returned error: VM Exception while processing transaction: revert when creating with New 松露错误:错误:处理事务时VM异常:恢复 - Truffle error: Error: VM Exception while processing transaction: revert 处理事务时出现 VM 异常:还原 - VM Exception while processing transaction: revert 错误:返回错误:处理事务时出现 VM 异常:气体不足 - Error: Returned error: VM Exception while processing transaction: out of gas brownie:ValueError: execution reverted: VM Exception while processing transaction: revert - brownie:ValueError: execution reverted: VM Exception while processing transaction: revert Openzepplin众包合同获得:处理事务时VM异常:恢复错误 - Openzepplin crowdsale contract got: VM Exception while processing transaction: revert error Solidity - Truffle:错误:处理事务时出现 VM 异常:无效的操作码不是因为恢复 - Solidity - Truffle: Error: VM Exception while processing transaction: invalid opcode NOT because of revert 在松露控制台中使用OpenZeppelin ERC721 Mint时,“处理事务时发生VM异常:还原” - “VM Exception while processing transaction: revert” when using OpenZeppelin ERC721 mint in truffle console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM