0

I’m trying to use the renounceOwnership() function from the Ownable.sol contract and it doesn’t seem to be working.

I’ve got a simple 2-contract set-up, as follows:

  1. The first contract is a basic ERC721 Contract - which imports and uses Ownable.sol:

    import "../node_modules/@openzeppelin/contracts/access/Ownable.sol";
    
    contract myERC721Contract is Context, Ownable, ERC165, IERC721, IERC721Metadata, IERC721Enumerable {
    
      …code…
    
    }
    

  1. The second contract imports the first one (“myERC721Contract”) and has a function called cutCord() which calls the renounceOwnership() function that it inherits from Ownable.sol:

    import "./myERC721Contract.sol";
    
    contract MyERCMarketplace is myERC721Contract {
    
       … code…
    
    
    
       function cutCord() external {
          renounceOwnership();
       }   
    
    }
    

As a reminder, this **renounceOwnership()** method - which is from the **Ownable** contract - emits an event called `OwnershipTransferred`, declared as follows:
  event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

And here’s how the renounceOwnership() is declared in Ownable:

 function renounceOwnership() public virtual onlyOwner {
        emit OwnershipTransferred(_owner, address(0));
        _owner = address(0);
    }
  1. Finally, I set up my front-end JS app to listen for the OwnershipTransferred event:

    let contractOwnerChanged =  myContractInstance.events.OwnershipTransferred({ fromBlock: "latest", toBlock: "pending" })
    .on("data", async function(event) {
        console.log("\n==================\n--->OwnershipTransferred 'data' event = " + event);
        let eventKeys = Object.keys(event);
        let eventValues = Object.values(event);
        console.log("'OwnershipTransferred' eventKeys = " + eventKeys);
        console.log("'OwnershipTransferred' eventValues = " + eventValues);
    
        let returnValues = event["returnValues"];
        let returnValuesKEYS = Object.keys(returnValues);
        let returnValuesVALUES = Object.values(returnValues);
    
      })
      .on('changed', async function(event) {
         console.log("OwnershipTransferred 'changed' event = " + event);
      })
      .on('error', async function(event) {
         console.log("--'OwnershipTransferred' - ERROR!" + error);
      });
    

I trigger my cutCord() function via a simple button-press, as follows:

function cutCord() {
    console.log("\n\n=================\n==> in 'cutCord()'!");
  
    var cordCutter = myContractInstance.methods.cutCord().call({ from: activeAccount }, 
      function(error, result) {
        if (!error) {
          console.log("\n\n==>Back in 'cutCord()'!!!  No ERRORS!!!");
          console.log(">'result' is:");
          console.log(result);
        }
        else {
          console.log("ERROR!!! from 'cutCord()' -->\n" + error);
        }
      });
  
    console.log("\n\n=================\n==>EXITING 'cutCord()'!");
  }
  

The end result is that nothing is happening: on the one hand I don’t get any errors, but on the other hand, the result that comes back simply returns this in the console:

enter image description here

So what am I doing wrong?

1

Since you are using the call functionality, you are not sending a real transaction (https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#methods-mymethod-call). A call can't alter contract state and can't emit events.

You have to send a real transaction with send (https://web3js.readthedocs.io/en/v1.2.11/web3-eth-contract.html#methods-mymethod-send). This will give you a transaction hash a result, then you need to wait for the transaction to be mined and you'll see the emitted event.

1
  • 1
    WELL. I'm having one of the biggest "DUH" moments of my life right now 😎 (Or at least one of the bigger DUH moments of the WEEK 😇) Anyway, thanks so much - I appreciate it!
    – Mark55
    Apr 22 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.