0

I tried to deploy my contracts but every time I get this error. The first two contracts were deployed successfully, but the third contract, "PostFactory.sol" is facing this issue in loops. I am using Infura and I am deploying it on the rinkeby test network.

Codes

This is my truffle-config.js file

const path = require("path");

const HDWalletProvider = require("truffle-hdwallet-provider");

const initializedProvider = new HDWalletProvider(
  "<SEED_PHRASE>", 
  "https://rinkeby.infura.io/v3/<INFURA_KEY>", 

initializedProvider.engine.start();
module.exports = {
  networks:{
    development: {
      host: "127.0.0.1",   
      port: 8545,          
      network_id: "*"      
     },
     rinkeby: {
      provider: initializedProvider,
      network_id: 4,
      skipDryRun: true,
    }
    },
    solc: {
      optimizer: {
        enabled: true,
        runs: 200
      }
    },

    contracts_build_directory: path.join(__dirname, "client/src/contracts")
};


This is the deployment file of migrations.

var UserFactory = artifacts.require("./UserFactory.sol");
var PostFactory = artifacts.require("./PostFactory.sol");

module.exports = function (deployer) {
  deployer.deploy(PostFactory, UserFactory.address);
};

This is the smart contract "PostFactory.sol" which is causing this error!


'''
    pragma solidity ^0.5.0;
import "./Post.sol";
import "./UserFactory.sol";
import "./OriginalPost.sol";
import "./SharedPost.sol";
contract PostFactory {
    
    //user contract address => user's posts contract addresses
    mapping(address => address[]) public posts;
    // need userfactory to check if user exits or not before create post
    UserFactory private userFactory;
    
    constructor(address user_factory_address)public{
        userFactory=UserFactory(user_factory_address);
    }

    function createPost(address user_contract_address,string memory post_text, string memory imageUrl) public {
        
        require(userFactory.checkUserExists(msg.sender));
        require(auth(user_contract_address,msg.sender),"user contract address is not yours");
        
        Post post = new OriginalPost(user_contract_address,post_text,imageUrl);
        posts[user_contract_address].push(address(post));
    }

    function getPostsOfUser(address user_contract_address) public view returns(address[] memory){
        return posts[user_contract_address];
    }
    
    function sharePost(address user_contract_address,address original_post_address, string memory post_text) public {

        require(userFactory.checkUserExists(msg.sender));
        require(auth(user_contract_address,msg.sender),"user contract address is not yours");
        
        OriginalPost originalPost = OriginalPost(original_post_address);
        originalPost.increamentShares();
        Post post = new SharedPost(user_contract_address,original_post_address,post_text);
        posts[user_contract_address].push(address(post));
    }

    function auth(address user_contract_address,address msg_sender) public view returns(bool){
            User user = User(user_contract_address);
            if(user.isOwner(msg_sender))
                return true;
            else
                return false;
        }
    
}
'''

Enviornment

Node js - 14.17.0

truffle- 5.1.39

npm- 6.14.13

1 Answer 1

0

There are two reasons for this error :

  1. You are not passing enough gas for your contract, to be sure of this when deploying, use the maximum gasAmount allocation possible and the max gasPRice allowed by rinkeby.

  2. This error stems from the contract code being very big in size. for exact measurement its bytecode is bigger than 24Kb in size. the error message here is for sure misleading. to resolve this either reduce the contract code or put it in libraries not contracts. Libraries are .sol files that use the keyword library instead of contract.

here is a python code that you can run to find out which contract is the culprit and how much their actual size is:

import os
import json

# https://github.com/ethereum/EIPs/blob/master/EIPS/eip-170.md
MAX_CONTRACT_BYTECODE_SIZE = 24 * 1024


for fn in os.listdir(f"{os.path.dirname(os.path.realpath(__file__))}/build/contracts"):
    fn = os.path.join('build/contracts', fn)
    with open(fn, 'rb') as f:
        data = f.read()
        obj = json.loads(data)
        bytecode = int((len(obj.get('bytecode', '0x')) - 2) / 2)
        deployedBytecode = int((len(obj.get('deployedBytecode', '0x')) - 2) / 2)
        if deployedBytecode > MAX_CONTRACT_BYTECODE_SIZE:
            warn = True
        else:
            warn = False
        print('ABI file "{:<30}" bytecode= {:<6} bytes, deployedBytecode= {:<6} bytes {}'.format(
            fn, bytecode, deployedBytecode, '  WARNING - maximum deployed contract size of 24kB exceeded' if warn else ''))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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