Error calling function after converting to upgradeable

I just refactored this contract to be upgradeable and it is giving me this error when I send a tx to the newCollection function

:heavy_multiplication_x: Calling: 'newCollection' with:

  • title (string): "test"
    Error while trying to send transaction to 0x03D37AD26961D79E52c3dAEa840C0095Fb4729a7. Error: [object Object]

Here is the contract code

    //SPDX-License-Identifier: Unlicensed

    pragma solidity ^0.6.8;

    //import "@openzeppelin/upgrades/contracts/Initializable.sol";

    import "@openzeppelin/contracts-ethereum-package/contracts/token/ERC721/ERC721.sol";

    import "@openzeppelin/contracts-ethereum-package/contracts/access/Ownable.sol";

    import "@openzeppelin/contracts-ethereum-package/contracts/utils/Counters.sol";

    contract BrightWaves is ERC721UpgradeSafe, OwnableUpgradeSafe {

        using Counters for Counters.Counter;

        Counters.Counter private _tokenIds;

        Counters.Counter public _collectionCount;

        event NewCollection(uint256 id, string title);

        struct Collection {

            string title;

            uint256 count;

        }

        struct Artwork {

            string hash;

            uint256 collection;

        }

        // mapping to store art IPFS hashes

        mapping(uint256 => Artwork) internal artwork;

        mapping(uint256 => Collection) internal collections;

        /**

         * @dev Contract constructor.

         * @notice Constructor inherits ERC721

         */

        // constructor(string memory baseURI) public ERC721("Bright Waves", "~BW~") {

        //     _setBaseURI(baseURI);

        // }

        /**

         * @dev Contract initializer.

         * @notice Constructor inherits ERC721 and Initializable

         */

        function initialize(string memory baseURI) public initializer {

            __ERC721_init_unchained("Bright Waves", "~BW~");

            // ERC721UpgradeSafe.initialize("Bright Waves", "~BW~");

            // OwnableUpgradeSafe.initialize();

            //_setBaseURI(baseURI);

        }

        // create a new collection

        function newCollection(string calldata title)

            external

            onlyOwner()

            returns (uint256)

        {

            _collectionCount.increment();

            uint256 id = _collectionCount.current();

            collections[id] = Collection(title, 0);

            emit NewCollection(id, title);

            return id;

        }

        // mint a piece of artwork

        function mintArtwork(

            string calldata uri,

            string calldata hash,

            uint256 collection

        ) external onlyOwner() returns (uint256) {

            // check the collection exists

            require(

                (collection > 0 && collection <= _collectionCount.current()),

                "Nonexistant Collection ID"

            );

            uint256 id = _tokenIds.current();

            artwork[id] = Artwork(hash, collection);

            _safeMint(msg.sender, id);

            _tokenIds.increment();

            collections[collection].count++;

            _setTokenURI(id, uri);

            return id;

        }

        // get artwork

        function getArtwork(uint256 id)

            external

            view

            returns (string memory, uint256)

        {

            Artwork memory a = artwork[id];

            return (a.hash, a.collection);

        }

        // get collection

        function getCollection(uint256 id)

            external

            view

            returns (string memory, uint256)

        {

            Collection memory c = collections[id];

            return (c.title, c.count);

        }

    }
1 Like

Hi @Skyler_Fly,

It looks like it was your initialization that was the issue. The owner wasn’t initialized.

$ npx oz send-tx
? Pick a network development
? Pick an instance BrightWaves at 0xCfEB869F69431e42cdB54A4F4f105C19C080A601
? Select which function newCollection(title: string)
? title: string: foo
✖ Calling: 'newCollection' with:
- title (string): "foo"
Error while trying to send transaction to 0xCfEB869F69431e42cdB54A4F4f105C19C080A601. 
Error: Returned error: VM Exception while processing transaction: revert Ownable: caller is not the owner

I changed the initialization to:

    function initialize(string memory baseURI) public initializer {
        __Context_init_unchained();
        __ERC165_init_unchained();
        __Ownable_init_unchained();
        __ERC721_init_unchained("Bright Waves", "~BW~");
        _setBaseURI(baseURI);
    }

I was then able to create a new collection

$ npx oz send-tx
? Pick a network development
? Pick an instance BrightWaves at 0xD833215cBcc3f914bD1C9ece3EE7BF8B14f841bb
? Select which function newCollection(title: string)
? title: string: foo
✓ Transaction successful. Transaction hash: 0x827c20786e0214fa56b1797e162e05bbece447b3e430bd3554e212d838fee5cf
Events emitted:
 - NewCollection(1, foo)

If you haven’t already, you may want to look at the preset contract to use Access Control rather than Ownable, and the use of the token URI to reduce gas cost: https://docs.openzeppelin.com/contracts/3.x/api/presets#ERC721PresetMinterPauserAutoId

1 Like