Error sending Ether to sales contract

Hi

I have the following case. Create an ERC20 mint token with the following code.

pragma solidity ^0.6.5;

import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyToken is ERC20, AccessControl {

    bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
    bytes32 public constant BURNER_ROLE = keccak256("BURNER_ROLE");

    constructor() public ERC20("MyToken", "TKN") {
        _setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
    }

    function mint(address to, uint256 amount) public {
        require(hasRole(MINTER_ROLE, msg.sender), "Caller is not a minter");
        _mint(to, amount);
    }

    function burn(address from, uint256 amount) public {
        require(hasRole(BURNER_ROLE, msg.sender), "Caller is not a burner");
        _burn(from, amount);
    }
}

Address that takes me in testnet is: 0x7823549A14ab83d50B6654336A42Bb679A695875

Then create a sales contract with the following code:

pragma solidity ^0.5.0;

import "@openzeppelin/contracts/crowdsale/Crowdsale.sol";
import "@openzeppelin/contracts/crowdsale/emission/MintedCrowdsale.sol";
import "@openzeppelin/contracts/crowdsale/validation/CappedCrowdsale.sol";
import "@openzeppelin/contracts/crowdsale/validation/TimedCrowdsale.sol";
import "@openzeppelin/contracts/crowdsale/distribution/PostDeliveryCrowdsale.sol";
import "@openzeppelin/contracts/crowdsale/price/IncreasingPriceCrowdsale.sol";

contract DappTokenCrowdsale is Crowdsale, MintedCrowdsale, CappedCrowdsale, TimedCrowdsale, PostDeliveryCrowdsale, IncreasingPriceCrowdsale {
  mapping(address => uint256) public contributions;
  constructor(
    uint256 _rate,
    address payable _wallet,
    uint256 _cap,
    uint256 _openingTime,
    uint256 _closingTime,
    uint256 _initialRate,
    uint256 _finaleRate,
    IERC20 _token
  )
    Crowdsale(_rate, _wallet, _token)    
    CappedCrowdsale(_cap)
    TimedCrowdsale(_openingTime, _closingTime)
    PostDeliveryCrowdsale()
    IncreasingPriceCrowdsale (_initialRate, _finaleRate)
    public
  {

  }  
  function getUserContribution(address _beneficiary)
    public view returns (uint256)
  {
    return contributions[_beneficiary];
  }
}

The parameters I use are the following:

const DappTokenCrowdsale = artifacts.require("DappTokenCrowdsale");
 let rate = 400;
 let wallet = "0x56679A0bD8a24007aD5cBBd4375179c3A81C9319";
 let cap = 50000000000000000000n;
 let openTime = 1605207911; //    //Epoch Unix Time Stamp 
 let closeTime =  1605218711; //    //Epoch Unix Time Stamp 
 let initialRate = 400;
 let finalRate = 50;
 let toke =  "0x7823549A14ab83d50B6654336A42Bb679A695875"; 
module.exports = function(_deployer) {
     _deployer.deploy(DappTokenCrowdsale, rate, wallet, cap, openTime, closeTime, initialRate, finalRate, toke);
};

Then in the token contract I gave it MINTER_ROLE permissions from the grantRole function so that it can token the sale contract.

Now when I want to test, I know that the sale is open, I try to send 1 ETH to the sale contract from metamak, it generates an error.

Is there anything else I need to do? Could you give me any suggestions please.

Which test net do you use? and what is the error message?

hi @Skype

The test network is ropsten

This is the transaction:

The error message is out of gas, so maybe you can increase the gasLimit and then have a try.

Increase the gas and now I have another error

Hi @jeissoni,

I assume that your timed crowdsale had ended, so you may want to redeploy.

You could interact using the console to check the state: https://docs.openzeppelin.com/learn/deploying-and-interacting#interacting-from-the-console

You could check the values of hasClosed or isOpen.

You may want to call buyTokens rather than just sending Ether to the crowdsale.

Hi @abcoathup

I deployed a new contract, I am sure the purchase is open. But I get the same error referring to @Skyge.

The use of the buyTokens method, I honestly don’t get it. The parameter is only one and it is of type address, so where is the amount of ether sent received?

Could you please verify your contract on the etherscan, so I have a try by myself.

Hi @Skyge

I created a new sales contract, already verified

The address in ropsten is: 0x8e03c66A937EE34da727C0Bf8ceB26254801fd24

Hey, I think at about line 1908:

require(
            ERC20Mintable(address(token())).mint(beneficiary, tokenAmount),
                "MintedCrowdsale: minting failed"
        );

The token is Tress, and at the line 1315 in the Tress token contract:

function mint(address to, uint256 amount) public {
        require(hasRole(MINTER_ROLE, msg.sender), "Caller is not a minter");
        _mint(to, amount);
    }

So you can find out that there is not a return value for mint, so I think the problem is at there.

Hi @jeissoni,

It looks like @Skyge has spotted the issue. :pray:
You can extend the crowdsale to change the minting. I recommend creating unit tests to test this.