0

I'm new to blockchain. I'm trying to deploying a contract through web3 on ganache (the npm version) for testing purpose with mocha.

During the deployment attempt, ganache returns with the following error:

"Runtime error: code size to deposit exceeds maximum code size"

If i deploy the same smart contract with truffle on ganache (the application), everything goes fine. My implementation of the contract uses some openzeppelin library. Here is my code:

Test script

const assert = require('assert');
const ganache = require('ganache');

const Web3 = require('web3');
const web3 = new Web3(ganache.provider());

const { abi, bytecode } = require('../build/contracts/LotteryNftAward.json');

let lottery;
let accounts;

const TOTAL_SUPPLY = 5;

beforeEach(async () => {
    accounts = await web3.eth.getAccounts();

    lottery = await new web3.eth.Contract(abi)
    .deploy({ data: bytecode, arguments: [TOTAL_SUPPLY]})
    .send({ from: accounts[0], gas: 1000000 });    
});

describe('Lottery Contract', () => {
    it('deploys a contract', () => {
        assert.ok(lottery.options.address);
    });
});

Contract

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

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


contract LotteryNftAward is ERC721, Ownable {
    using Counters for Counters.Counter;

    Counters.Counter private _tokenIdCounter;

    struct Player {
        address playerAddress;
        bool hasWon;
    }

    event Subscription(address newPlayer, uint256 totalSubscription);
    
    uint256 immutable TOTAL_SUPPLY;

    Player[] private players;

    mapping(address => bool) private claimTracker;

    constructor(uint256 totalSupply) ERC721("LotteryNftAward", "LNA") {
        TOTAL_SUPPLY = totalSupply;
    }

    function getPlayersAddresses() public view returns (address[] memory) {
        address[] memory playersAddresses = new address[](players.length);
        
        for(uint i = 0; i < players.length; i++) {
            playersAddresses[i] = players[i].playerAddress;
        }

        return playersAddresses;
    }

    function subscribe(address player) external onlyOwner {
        Player memory newPlayer = Player({
            playerAddress: player,
            hasWon: false
        });
        
        players.push(newPlayer);

        emit Subscription(player, players.length);
        
    }

    function random() private view returns (uint) {
        return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, getPlayersAddresses())));
    }

    function pickWinners() external view onlyOwner returns (address [] memory) {
        address[] memory winners;
        if(players.length <= TOTAL_SUPPLY) {
            winners = new address[](players.length);
            for(uint i = 0; i < players.length; i++) {
                Player memory winner = players[i];
                winner.hasWon = true;
                winners[i] = winner.playerAddress;
            }
        } else {
            winners = new address[](TOTAL_SUPPLY);
            for(uint i = 0; i < TOTAL_SUPPLY; i++) {
                uint winnerIndex = random() % players.length;
                Player memory candidatePlayer = players[winnerIndex];
                
                if(candidatePlayer.hasWon){
                    continue;
                }
                
                candidatePlayer.hasWon = true;
                winners[i] = candidatePlayer.playerAddress;
            }
        }

        return winners;
    }

    function claim() external {
        uint256 currentTokenId = _tokenIdCounter.current();

        require(currentTokenId < TOTAL_SUPPLY, "No more tokens to claim");
        require(!claimTracker[msg.sender], "This address has already claimed");
        
        _tokenIdCounter.increment();

        claimTracker[msg.sender] = true;
        
        _safeMint(msg.sender, _tokenIdCounter.current());
        
    }

}
2
  • 1
    compile after enabling optimization 200 and send more gas. those contracts are big , gas: 1000000 is not enough
    – Majd TL
    Jan 24, 2023 at 8:29
  • with 10000000 and optimization 200 it works, thanks!!
    – yanosh
    Jan 24, 2023 at 10:02

1 Answer 1

0

As Majd TL suggested in his answer, with 10000000 gas and optimization 200 it worked

1
  • 1
    you don't need 10000000, just deploy it once on a test net using remix and check how much it really consumes
    – Majd TL
    Jan 24, 2023 at 10:48

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.