why the onlyowner function type of solidity is showing result by all the wallet adress?

Viewed 32

I am using this solidity onlywoner code

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract vote{
    address public  participant1 = 0xF6C486B8A4e67b8eff4d5045C804E9be4ed39FF9;
    address public  participant2 = 0xBBDb2A08711D7b2b9c15318E77B6e026eD8fA278;
    mapping(address=>uint) user;
    mapping(address=>bool) chek;

    address public owner;
    constructor(){
        owner = msg.sender;
    }

    modifier onlyonwner(){
        require(owner == msg.sender,"You are not allowed to declare the result");
        _;
    }

    function participant1_vote()  public {
        require(msg.sender !=participant1,"You can not vote to yourself");
        require(chek[msg.sender]!=true,"You have already voted");
        user[participant1]++;
        chek[msg.sender] = true;
    }
        function participant2_vote()  public {
        require(msg.sender !=participant2,"You can not vote to yourself");
        require(chek[msg.sender]!=true,"You have already voted");
        user[participant2]+=1;
        chek[msg.sender] = true;
    }

    function pati1_cnt_VOTE() view public onlyonwner returns(uint){
        return user[participant1];

    }

    function pati2_cnt_VOTE() view public onlyonwner returns(uint){
        return user[participant2];

    }

    function declare_winner() view public  onlyonwner returns(string memory){
        if(user[participant1]>user[participant2]){
            return ("Partcipant1 is wiiner !!");
        }
        else if(user[participant1]==user[participant2]){
            return("Both the participant are equal");
        }
        else{
            return ("Participant 2 is winenr");
        }
    }

}

In this the function which are the onlywoner is only acessed by the owner but this is being acessed by all the wallet adrress .

You can see my frontend code ->

if(typeof window.ethereum =="undefined"){
        console.log("PLease install the metamask");
    }
  var accounts;
    let web3 = new Web3(window.ethereum);
   let contract = new web3.eth.Contract(
    [
  {
    "inputs": [],
    "name": "participant1_vote",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "participant2_vote",
    "outputs": [],
    "stateMutability": "nonpayable",
    "type": "function"
  },
  {
    "inputs": [],
    "stateMutability": "nonpayable",
    "type": "constructor"
  },
  {
    "inputs": [],
    "name": "declare_winner",
    "outputs": [
      {
        "internalType": "string",
        "name": "",
        "type": "string"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "owner",
    "outputs": [
      {
        "internalType": "address",
        "name": "",
        "type": "address"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "participant1",
    "outputs": [
      {
        "internalType": "address",
        "name": "",
        "type": "address"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "participant2",
    "outputs": [
      {
        "internalType": "address",
        "name": "",
        "type": "address"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "pati1_cnt_VOTE",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "pati2_cnt_VOTE",
    "outputs": [
      {
        "internalType": "uint256",
        "name": "",
        "type": "uint256"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  }
],
  "0x78AC9c94Bfd5F64b14515F575791d1822B194E48"
  );
    async function  account(){
        accounts = await web3.eth.requestAccounts();
       contract.methods.owner().call().then(function (resp){
        const owner_address = resp;
        $("#btn").click(async function(){
            accounts1 = await web3.eth.requestAccounts();
            contract.methods.participant1_vote().send({from:String(accounts1)},function(err,res){
                if(err){
                    console.log("error"+err);
                }
                else{
                    console.log("Voted to the participant1 sucessfully");
                }
            });
        });

        $("#btn1").click(async function(){
            accounts2 = await web3.eth.requestAccounts();
      alert(accounts2);
            contract.methods.participant2_vote().send({from: String(accounts2)},function(err,res){
                if(err){
                    console.log("error"+err);
                }
                else{
                    console.log("Voted to the participant2 sucessfully");
                }
            });
        });

        $("#res").click(async function() {
            contract.methods.declare_winner().call(function(err,res){
                if(err){
                    alert("U r not the owner");
                }
                else{
              contract.methods.declare_winner().call().then(function (resp){
           alert(resp);
        })
                }
        });

      // const res = await contract.methods.declare_winner().call({from:owner_address})
      // console.log(res);
        });

      $("#show-vote1").click(async function(){
        // calling the return method of the solidity fucntion

        contract.methods.pati1_cnt_VOTE().call().then(function (resp){
           alert("The vote for the candidate 1 is ->"+resp);
        })
      });
      $("#show-vote2").click(async function(){
        // calling the return method of the solidity fucntion
          contract.methods.pati2_cnt_VOTE().call().then(function (resp){
           alert("The vote for the candidate 2 is ->"+resp);
        });

      });
    });
   }

  account();

plsease give a solution that only owner of the contract is being able to get the onlyowner restricted address .

1 Answers
// instead of public, private is better, noone can get from outside
address payable private owner;

constructor(){
        // type of msg.sender is payable
        owner = payable(msg.sender);
    }
Related