Rarible EP7 Error while minting NFT

Hi,

Could you please help - why this error below happens?
error

main.js 95

await nftFile.saveIPFS();

main.js 115

const nftId = await mintNft(nftFileMetadataFilePath);

main.js 137

    const receipt = await tokenContract.methods.createItem(metadataUrl).send({from: ethereum.selectedAddress});
    console.log(receipt);
    return receipt.events.Transfer.returnValues.tokenId;

Thank you in advance,
Piotr

Hey Hip!

My first guess is that you are missing the ABI for the token contract.
In the Init function at the top of the file you should define it something like this:

 window.tokenContract = new web3.eth.Contract(tokenContractAbi, TOKEN_CONTRACT_ADDRESS);

In the tutorial series, we define tokenContractAbi in a separate file (abi.js).
Make sure that it is references in the HTML file before main.js

    <script src="https://unpkg.com/moralis/dist/moralis.js"></script>
    <script src="abi.js"></script>
    <script src="main.js"></script>

Let me know if your problen persists.

/Capplequoppe

Hey Capplequoppe,

Thank you so much for responding so quickly.

Yes, I had these references before - a bit different - as one line below

<script src="https://unpkg.com/[email protected]/dist/moralis.js"></script>

now it looks like here:

But, the result and error info is exactly the same.

Thank you,
Hip

Can you post the contents of abi.js?

Here it is:

var tokenContractAbi = [
    {
      "inputs": [],
      "name": "last_completed_migration",
      "outputs": [
        {
          "internalType": "uint256",
          "name": "",
          "type": "uint256"
        }
      ],
      "stateMutability": "view",
      "type": "function",
      "constant": true
    },
    {
      "inputs": [],
      "name": "owner",
      "outputs": [
        {
          "internalType": "address",
          "name": "",
          "type": "address"
        }
      ],
      "stateMutability": "view",
      "type": "function",
      "constant": true
    },
    {
      "inputs": [
        {
          "internalType": "uint256",
          "name": "completed",
          "type": "uint256"
        }
      ],
      "name": "setCompleted",
      "outputs": [],
      "stateMutability": "nonpayable",
      "type": "function"
    }
  ];

Hip

It seems you have the wrong ABI here, looks like the migration ABI.
You should find the correct one in build\contracts\nameOfToken.json

Great, thank you so much! Wll look for the right one and keep you informed :slight_smile:

Hip

I do not have this token (HipToken.sol) at build\contracts\nameOfToken.sol

although I have the address of the contract that has been created.


const TOKEN_CONTRACT_ADDRESS = "0x216897B03725CF9912EC763f61E08E3463a82728";
pragma solidity ^0.8.0;

import "../node_modules/@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "../node_modules/@openzeppelin/contracts/utils/Counters.sol";

contract HipToken is ERC721 {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    constructor () ERC721("HipToken", "HIP"){}

    struct Item {
        uint256 id;
        address creator;
        string uri;
    }

    mapping (uint256 => Item) public Items;

    function createItem(string memory uri) public returns(uint256){
        _tokenIds.increment();
        uint256 newItemId = _tokenIds.current();
        _safeMint(msg.sender, newItemId);

        Items[newItemId] = Item(newItemId, msg.sender, uri);

        return newItemId;
    }

    //function tokenURI(uint256 tokenId) public view returns (string memory) {
    //    require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");




    function tokenURI(uint256 tokenId) public view override returns (string memory) {
        require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");   

        return Items[tokenId].uri;
    }
}

Hip

I believe that occured because I have missed “override” here:

function tokenURI(uint256 tokenId) public view override returns (string memory) {
        require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");   

        return Items[tokenId].uri;
    }

Hip

Sorry, I made a mistake in my previous answer.
The file containing your ABI in the build\contracts folder should be HipToken.json after you build/migrate.

Thank you so much for coming back to me.

The key is that my migration is unsuccessful that is why - I assume - the HipToken is not there.

2_token_migration.js
====================

C:\Users\Admin\AppData\Roaming\npm\node_modules\truffle\build\459.bundled.js:25500
        throw new Error("Could not find artifacts for " + import_path + " from any sources");
        ^

Error: Could not find artifacts for HipToken from any sources
    at Resolver.require (C:\Users\Admin\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\resolver\dist\lib\resolver.js:61:1)
    at Object.require (C:\Users\Admin\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:172:1)
    at ResolverIntercept.require (C:\Users\Admin\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\ResolverIntercept.js:22:1)
    at C:\Users\Admin\Morales.HIP\contracts\migrations\2_token_migration.js:1:28
    at Script.runInContext (vm.js:143:18)
    at Script.runInNewContext (vm.js:148:17)
    at Object.file (C:\Users\Admin\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\require\require.js:94:1)
    at Migration._load (C:\Users\Admin\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\Migration.js:49:1)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at Migration.run (C:\Users\Admin\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\Migration.js:212:1)
    at Object.runMigrations (C:\Users\Admin\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:150:1)
    at Object.runFrom (C:\Users\Admin\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:110:1)
    at Object.runAll (C:\Users\Admin\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:114:1)
    at Object.run (C:\Users\Admin\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\migrate\index.js:79:1)
    at runMigrations (C:\Users\Admin\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\commands\migrate.js:263:1)
    at Object.run (C:\Users\Admin\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\core\lib\commands\migrate.js:228:1)
Truffle v5.3.4 (core: 5.3.4)
Node v14.16.1

To tell the truth I do not know why :slight_smile:

Hy @Capplequoppe , I also get an error when trying to mint, I thought this thread might fit:

NFTMintRPCRevert

I can’t find the mistake. Ganache is connected correctly, shows that is connected with moralis as well. Metamask pops up as should, but the this revert with RPC error :thinking:
Any clues?
Thanks a lot :pray:

truffleConfig
/**
 * Use this file to configure your truffle project. It's seeded with some
 * common settings for different networks and features like migrations,
 * compilation and testing. Uncomment the ones you need or modify
 * them to suit your project as necessary.
 *
 * More information about configuration can be found at:
 *
 * trufflesuite.com/docs/advanced/configuration
 *
 * To deploy via Infura you'll need a wallet provider (like @truffle/hdwallet-provider)
 * to sign your transactions before they're sent to a remote public node. Infura accounts
 * are available for free at: infura.io/register.
 *
 * You'll also need a mnemonic - the twelve word phrase the wallet uses to generate
 * public/private key pairs. If you're publishing your code to GitHub make sure you load this
 * phrase from a file you've .gitignored so it doesn't accidentally become public.
 *
 */

// const HDWalletProvider = require('@truffle/hdwallet-provider');
// const infuraKey = "fj4jll3k.....";
//
// const fs = require('fs');
// const mnemonic = fs.readFileSync(".secret").toString().trim();

module.exports = {
  /**
   * Networks define how you connect to your ethereum client and let you set the
   * defaults web3 uses to send transactions. If you don't specify one truffle
   * will spin up a development blockchain for you on port 9545 when you
   * run `develop` or `test`. You can ask a truffle command to use a specific
   * network from the command line, e.g
   *
   * $ truffle test --network <network-name>
   */

  networks: {
    // Useful for testing. The `development` name is special - truffle uses it by default
    // if it's defined here and no other network is specified at the command line.
    // You should run a client (like ganache-cli, geth or parity) in a separate terminal
    // tab if you use this network and you must also set the `host`, `port` and `network_id`
    // options below to some value.
    //
    // development: {
    //  host: "127.0.0.1",     // Localhost (default: none)
    //  port: 8545,            // Standard Ethereum port (default: none)
    //  network_id: "*",       // Any network (default: none)
    // },
    // Another network with more advanced options...
    // advanced: {
    // port: 8777,             // Custom port
    // network_id: 1342,       // Custom network
    // gas: 8500000,           // Gas sent with each transaction (default: ~6700000)
    // gasPrice: 20000000000,  // 20 gwei (in wei) (default: 100 gwei)
    // from: <address>,        // Account to send txs from (default: accounts[0])
    // websocket: true        // Enable EventEmitter interface for web3 (default: false)
    // },
    // Useful for deploying to a public network.
    // NB: It's important to wrap the provider as a function.
    // ropsten: {
    // provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/v3/YOUR-PROJECT-ID`),
    // network_id: 3,       // Ropsten's id
    // gas: 5500000,        // Ropsten has a lower block limit than mainnet
    // confirmations: 2,    // # of confs to wait between deployments. (default: 0)
    // timeoutBlocks: 200,  // # of blocks before a deployment times out  (minimum/default: 50)
    // skipDryRun: true     // Skip dry run before migrations? (default: false for public nets )
    // },
    // Useful for private networks
    // private: {
    // provider: () => new HDWalletProvider(mnemonic, `https://network.io`),
    // network_id: 2111,   // This network is yours, in the cloud.
    // production: true    // Treats this network as if it was a public net. (default: false)
    // }
  },

  // Set default mocha options here, use special reporters etc.
  mocha: {
    // timeout: 100000
  },

  // Configure your compilers
  compilers: {
    solc: {
      version: "^0.8.0",    // Fetch exact version from solc-bin (default: truffle's version)
      // docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)
      // settings: {          // See the solidity docs for advice about optimization and evmVersion
      //  optimizer: {
      //    enabled: false,
      //    runs: 200
      //  },
      //  evmVersion: "byzantium"
      // }
    }
  },

  // Truffle DB is currently disabled by default; to enable it, change enabled: false to enabled: true
  //
  // Note: if you migrated your contracts prior to enabling this field in your Truffle project and want
  // those previously migrated contracts available in the .db directory, you will need to run the following:
  // $ truffle migrate --reset --compile-all

  db: {
    enabled: false
  }
};

JSCode
Moralis.initialize("VyXM3keXqHlBnaH5FUv8rT6Y9DDMCBBreVc2kyJK");
Moralis.serverURL = 'https://m0e24kwy0shp.moralis.io:2053/server';
const TOKEN_CONTRACT_ADDRESS = "0x7a6642fa4Faec85cD3a73c167Cc226fA13BcE6Af";




init = async () => {
 hideElement(userInfo);
 hideElement(createItemForm);
 window.web3 = await Moralis.Web3.enable();
 window.tokenContract = new web3.eth.Contract(tokenContractAbi, TOKEN_CONTRACT_ADDRESS);
 initUser();
}

initUser = async () => {
    if(await Moralis.User.current()){
        hideElement(userConnectButton);
        showElement(userProfileButton);
        showElement(openCreateItemButton);
    } else {
        showElement(userConnectButton);
        hideElement(userProfileButton);
        hideElement(openCreateItemButton);
    }
}

login = async () => {
    try {
        Moralis.Web3.authenticate();
        initUser();
    } catch (error) {
        alert(error);
    }
}

logout = async () => {
    await Moralis.User.logOut();
    hideElement(userInfo);
    init();
}

openUserInfo = async () => {
    user = await Moralis.User.current();
    if (user){
        const email = user.get('email');
        if(email) {
            userEmailField.value = email;
        } else {
            userEmailField.value = "";
        }

        userUsernameField.value = user.get('username');

        const userAvatar = user.get('avatar');
        if(userAvatar) {
            userAvatarImg.src = userAvatar.url();
            showElement(userAvatarImg);
        } else {
            hideElement(userAvatarImg);
        }

        showElement(userInfo);
    }else{
        login();
    }


} 

saveUserInfo = async () => {
    user.set('email', userEmailField.value);
    user.set('username', userUsernameField.value); //There is a check behind the scenes for username duplicate in moralis backend, but is not deep dived here in tutorial!

    if (userAvatarFile.files.length > 0) {
        const avatar = new Moralis.File("avatar.jpg", userAvatarFile.files[0]);
        user.set('avatar', avatar);
      }

    await user.save();
    alert("User info saved successfully!");
    openUserInfo();

}

createItem = async () => {
    if (createItemFile.files.length == 0){
        alert("Please select a file!");
        return;
    } else if (createItemNameField.value.length == 0) {
        alert("Please give the item a name!");
        return;
    }

    const nftFile = new Moralis.File("nftFile.jpg", createItemFile.files[0]);
    await nftFile.saveIPFS();

    const nftFilePath = nftFile.ipfs();
    const nftFileHash = nftFile.hash();

    const metadata = {
        name: createItemNameField.value,
        description: createItemDescriptionField.value,
        image: nftFilePath,
    };

    const nftFileMetadataFile = new Moralis.File("metadata.json", {base64 : btoa(JSON.stringify(metadata))});
    await nftFileMetadataFile.saveIPFS();

    const nftFileMetadataFilePath = nftFileMetadataFile.ipfs();
    const nftFileMetadataFileHash = nftFileMetadataFile.hash();

    const nftId = await mintNft(nftFileMetadataFilePath);

    const Item = Moralis.Object.extend("Item");

    //create new Instance of Item Class
    const item = new Item();
    item.set('name', createItemNameField.value);
    item.set('description', createItemDescriptionField.value);
    item.set('nftFilePath', nftFilePath);
    item.set('nftFileHash', nftFileHash);
    item.set('metadataFilePath', nftFileMetadataFilePath);
    item.set('metadataFileHash', nftFileMetadataFileHash);
    item.set('nftId', nftId);
    item.set('nftContractAddress', TOKEN_CONTRACT_ADDRESS);

    await item.save();
    console.log(item);
}

mintNft = async (metaDataUrl) => {
    const receipt = await tokenContract.methods.createItem(metaDataUrl).send({from: ethereum.selectedAddress});
    console.log(receipt);
    return receipt.events.Transfer.returnValues.tokenId;
}

hideElement = (element) => element.style.display = "none";
showElement = (element) => element.style.display = "block";

// NAVBAR ------------------------------------------------------------
const userConnectButton = document.getElementById("btnConnect");
userConnectButton.onclick = login;

const userProfileButton = document.getElementById("btnUserInfo");
userProfileButton.onclick = openUserInfo;

const openCreateItemButton = document.getElementById("btnOpenCreateItem");
openCreateItemButton.onclick = () => showElement(createItemForm);

//USER PROFILE ------------------------------------------------------------
const userInfo = document.getElementById("userInfo");
const userUsernameField = document.getElementById("txtUserName");
const userEmailField = document.getElementById("txtEmail");
const userAvatarImg = document.getElementById("imgAvatar");
const userAvatarFile = document.getElementById("fileAvatar");

document.getElementById('btnCloseUserInfo').onclick = () => hideElement(userInfo);
document.getElementById('btnLogout').onclick = logout;
document.getElementById('btnSaveUserInfo').onclick = saveUserInfo;

//ITEM CREATION ------------------------------------------------------------
const createItemForm = document.getElementById("createItem");

const createItemNameField = document.getElementById("txtCreateItemName");
const createItemDescriptionField = document.getElementById("txtCreateItemDescription");
const createItemPriceField = document.getElementById("numberCreateItemPrice");
const createItemStatusField = document.getElementById("selectCreateItemStatus");
const createItemFile = document.getElementById("fileCreateItemFile");

document.getElementById('btnCloseCreateItem').onclick = () => hideElement(createItemForm);
document.getElementById('btnCreateItem').onclick = createItem;

init();
contractCode
pragma solidity ^0.8.0;

import "../node_modules/@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "../node_modules/@openzeppelin/contracts/utils/Counters.sol";

contract MyRaribleToken is ERC721 {
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIds;

    constructor () ERC721("MyRarible", "MYRA"){

    }

    struct Item {
        uint256 id;
        address creator;
        string uri;
    }

    mapping (uint256 => Item) public Items;

    function createItem(string memory uri) public returns(uint256) {
        _tokenIds.increment();
        uint256 newItemId = _tokenIds.current();
        _safeMint(msg.sender, newItemId);

        Items[newItemId] = Item(newItemId, msg.sender, uri);

        return newItemId;
    }

     function tokenURI(uint256 tokenId) public view override returns (string memory) {
        require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");

        return Items[tokenId].uri;
    }



}

Hi @Capplequoppe.
I am using angular 11, I get the following error " Cannot find name ‘ethereum’ "

In the following screen shot you can see all my imports in my custom service called “MoralisService.ts”, in this service I handle all communication with Moralis.
Capture2

  1. Do I neeed to create a new variable with the name “ethereum” and if so what do I assign to the variable instance?
  2. Or am I missing an import and if so what do I need to import?

You could change that to user.get(“ethAddress”)

Thank you, I will give it a go.

YIHA!!
Peristance Prevails. PERIOD!
Happy to let you know that after redoing the whole Tutorial, I could successfully mint! Yiha!
FirstSuccessfullMint
As a little celebration I proudly share this first NFT Transaction Receipt with you.

Never forget to celebrate once you hit a milestone! For future readers:

Make sure your dev environment is clean, simple and consistent! not configuring my truffle.config and not using the same Ganache Workspace caused me alot of headache. You don’t have to go down that road, mark my words: KEEP IT CLEAN! Haha.

Thanks to Niklas and the whole Moralis Team! This journey is amazing!
Cheerz,
:partying_face:

3 Likes

Hey @Hip were you ever able to over come this road block? Im having the same issue when minting. Although my I was able to compile and migrate contract

Hi ibn5x,

Than you for reaching out to me - no, I was not able to finish it. I think that the problem was that I had no general idea what things are needed for and I kept copying blindly. Will come back to it in time :slight_smile: