Ethereum for Web Developers Book - ERC721 sample app - Error when running deploy.js

Hi!

I’m a beginner developing simple apps using smart contracts following the book “Ethereum for web developers”. by @spalladino
I’m getting this error when running the deploy.js script of the ERC721 sample app:

Error: The contract code couldn’t be stored, please check your gas limit.

I’ve read different posts and found this error may actually be unrelated to the gas amount.

If any extra information is needed please let me know.

Any help would be greatly appreciated!
Thanks!

:computer: Environment
ganache-cli -d
web3@1.2.0
openzeppelin-solidity@2.1
bignumber.js@8.0
axios@0.18.0
@0x/sol-compiler@2.0.2

:memo:Details

:1234: Code to reproduce

// scripts/deploy.js
const Web3 = require('web3');
const fs = require('fs');
const path = require('path');

// Deploys any artifact from the default account

async function deploy(artifact, arguments, opts) {
  const providerUrl = process.env.PROVIDER_URL || 'http://localhost:8545';
  const web3 = new Web3(providerUrl);
  const from = (await web3.eth.getAccounts())[0];
  const data = artifact.compilerOutput.evm.bytecode.object;
  const abi = artifact.compilerOutput.abi;
  const Contract = new web3.eth.Contract(abi, null, { data });
  const gasPrice = 1e9;
  const instance = await Contract.deploy({ arguments }).send({ from, gasPrice, ...opts });
  const address = instance.options.address;
  const network = await web3.eth.net.getId();
  save(network, address);
  console.log(address);
}

// Saves deployment address to a Deploys.json file

function save(network, address) {
  const file = path.join(__dirname, '..', 'artifacts', 'Deploys.json' );
  const deployments = fs.existsSync(file) ? JSON.parse(fs.readFileSync(file)) : {};
  deployments[network] = address;
  const content = JSON.stringify(deployments, null, 2);
  fs.writeFileSync(file, content);
}

// Deploys our ERC721PayPerMint contract

function main() {
  const artifactPath = '../artifacts/ERC721PayPerMint.json';
  const artifact = require(artifactPath);
  return deploy(artifact, [], { gas: 5e6, gasPrice: 1e9 });
}
main();
2 Likes

Hi @sanvaldi,

Welcome to the community :wave:

I am sorry that you are having an issue. I was able to run the sample code using the following steps:

$ git clone https://github.com/spalladino/ethereum-samples.git
$ cd ethereum-samples/05-sending-txs/02-sample-app/
$ npm install
$ node scripts/deploy.js
0xe78A0F7E598Cc8b0Bb87894B0F60dD2a88d6a8Ab

I am using Windows Subsystem for Linux (WSL2 - Ubuntu 18.04) with node v10.22.1

What operating system and version of node do you have?
Can you try the steps above (if you haven’t already)?

1 Like

Hey @sanvaldi! As you say, that error is pretty generic, and may or may not be related to gas. Have you modified the ERC721PayPerMint? If not, can you check what happens if you try to deploy another contract, the simpler the better?

1 Like

Hi @spalladino!

I have modified the ERC721PayPerMint contract as follows:

// contracts/ER721PayPerMint.sol
// SPDX-License-Identifier: MIT


pragma solidity >=0.4.21 <0.7.0;

import "openzeppelin-solidity/contracts/math/SafeMath.sol";
import "openzeppelin-solidity/contracts/ownership/Ownable.sol";
import "openzeppelin-solidity/contracts/token/ERC721/ERC721.sol";
import "openzeppelin-solidity/contracts/token/ERC721/ERC721Enumerable.sol";


contract ER721PayPerMint is ERC721, ERC721Enumerable, Ownable {
    using SafeMath for uint256;

    function exists(uint256 tokenId)
	    public view returns (bool) {
		    return _exists(tokenId);
	    }
    function mint(address to, uint256 tokenId)
	    public payable returns (bool) {
		    require(msg.value >= tokenId.mul(1e12), "Insufficient payment");
		    _mint(to, tokenId);
		    return true;
	    }
    function withdraw()
	    public onlyOwner {
		    msg.sender.transfer(address(this).balance);
	    }
}

I tried running the deploy.js with the Box.sol contract and it worked just fine, outputting this result:
{ “1601044864793”: “0x5b1869D9A4C187F2EAa108f3062412ecf0526b24” }

Box.sol contract:

// contracts/Box.sol
// SPDX-License-Identifier: MIT

pragma solidity >=0.4.21 <0.7.0;


contract Box {
    uint256 private value;

    // Emitted when the stored value changes
    event ValueChanged(uint256 newValue);

    // Stores a new value in the contract
    function store(uint256 newValue) public {
        value = newValue;
        emit ValueChanged(newValue);
    }

    // Reads the last stored value
    function retrieve() public view returns (uint256) {
        return value;
    }
}

I’ll check the reply @abcoathup sent me earlier and post the result.

Thanks a lot!!!

1 Like

OK, I found the mistake! It was just a typo on the contract name. I misspelled ERC721PayPerMint.

It works fine now.

Sorry about that!!

Thanks so much for your help!!

2 Likes

To be fair, if you have a typo in the contract name and the error is “check your gas limit”, there’s something really wrong with the tooling. Anyway, happy to hear you resolved it!

2 Likes

Yeah, I had another mistake besides the typo in the name of the contract. I was Importing IERC721Enumerable.sol instead of ERC721Enumerable.sol. So when I tried running the deploy.js with this mistake, the error I received was “check your gas limit”.

After fixing this, and trying to compile the contract I received the error: Contract ERC721PayPerMint not found in ERC721PayPerMint.sol. Please make sure your contract has the same name as it’s file name

So finally I found the typo in the contract name and then compiled the contract and run the depoy.js successfully.

1 Like

Hi @sanvaldi,

Glad you resolved your issue. I hope that you are enjoying working through the book.

1 Like