0

Tried compiling this code on Remix. Would be thankful for your help.

Getting the error below: Reason provided by the contract: "Ownable: caller is not the owner". Debug the transaction to get more information.

https://mumbai.polygonscan.com/address/0xeb22a05c1c3ca55aec8be98e2a005dffe7099876

Please find the contract address

//Contract based on [https://docs.openzeppelin.com/contracts/3.x/erc721](https://docs.openzeppelin.com/contracts/3.x/erc721)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/release-v4.4/contracts/token/ERC721/ERC721Upgradeable.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/release-v4.4/contracts/utils/CountersUpgradeable.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/release-v4.4/contracts/access/OwnableUpgradeable.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/release-v4.4/contracts/token/ERC721/extensions/ERC721URIStorageUpgradeable.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/release-v4.4/contracts/proxy/utils/Initializable.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/release-v4.4/contracts/utils/ContextUpgradeable.sol";

//import "https://github.com/OpenZeppelin/openzeppelin-contracts-upgradeable/blob/release-v4.4/contracts/token/ERC20/ERC20Upgradeable.sol";

contract MintDropsNFT is
    Initializable,
    ContextUpgradeable,
    OwnableUpgradeable,
    ERC721URIStorageUpgradeable
{

    using CountersUpgradeable for CountersUpgradeable.Counter;
    CountersUpgradeable.Counter private _tokenIds;

    function initialize(address owner) initializer public {
        __ERC721_init_unchained("MintDrops", "MDROPS");
        __Ownable_init_unchained();
        __Context_init_unchained();
    }

    function send(address recipient, uint256 tokenId) public  onlyOwner {
        safeTransferFrom(address (this),recipient,tokenId);
    }

    function mintNFT(address recipient, string memory tokenURI)
        public onlyOwner
        returns (uint256)
    {
        _tokenIds.increment();

        uint256 newItemId = _tokenIds.current();
        _mint(recipient, newItemId);
        _setTokenURI(newItemId, tokenURI);

        return newItemId;
    }

    function airDrop(address recipient, string memory tokenURI) 
        public onlyOwner
        returns (uint256)
    {
        uint256 tokenId = mintNFT(recipient, tokenURI);
        send(recipient, tokenId);

        return tokenId;
    }
}
2

1 Answer 1

1

The onlyOwner modifier reverts the transaction if it's not sent by the current owner. (see the code)

So you need to call the mintNFT() function from the same address that deployed the contract because that's the current owner. Or you can change the owner first by calling transferOwnership() (from the current owner address).

However, you can also remove the onlyOwner modifier from the mintNFT() function but that will allow anyone to call the function and mint NFT.

OpenZeppelin's Ownable.sol by default defines the owner to user who deploys it and therefore it mintNFT needs to be called by that user (address). However, you can also transfer the ownership by calling transferOwnership() or renounce the owner (i.e. set to 0x0) by calling renounceOwnership().

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.