0

issue: Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending? gas required exceeds allowance or always failing transaction.

already pass the Compiler, ran and deployed contract, however, when i click at address button secondly to deploy the same contract by calling buytoken function, the issue appeared.

i use openzeppelin contracts, following is my coding:

pragma solidity ^0.5.0;

//import "./ERC20Detailed.sol";
//import "./ERC20.sol";
//import "./ERC20Mintable.sol";
import "./Crowdsale.sol";
import "./TimedCrowdsale.sol";
import "./CappedCrowdsale.sol";
import "./IndividuallyCappedCrowdsale.sol";
import "./MintedCrowdsale.sol";
import "./CapperRoleMock.sol";
/**
 * @title NewsToken
 * @dev Very simple ERC20 Token with individually CappedCrowdsale
 * It is meant to be used in a crowdsale contract.
 */
//contract NewsToken is ERC20Mintable,ERC20Detailed {
  //  constructor () public ERC20Detailed("NewsToken", "News", 18) 
    //{
    //_mint(msg.sender, 1000000000 * (10 ** uint256(decimals())));
       // solhint-disable-previous-line no-empty-blocks
//    }
//}

/**
 * @title Crowdsale
 * @dev This is an example of a fully fledged crowdsale.
 * The way to add new features to a base crowdsale is by multiple inheritance.
 * In this example we are providing following extensions:
 * individuallyCappedCrowdsale - sets a max boundary per users for raised funds.
 * After adding multiple features it's good practice to run integration tests
 * to ensure that subcontracts works together as intended.
 */
contract NewsTokenCrowdsale is IndividuallyCappedCrowdsale,CappedCrowdsale,MintedCrowdsale,TimedCrowdsale,CapperRoleMock {
    constructor (
        uint256 openingTime,
        uint256 closingTime,
        uint256 rate,
        address payable wallet,
        uint256 cap,
        ERC20Mintable token,
        uint256 goal
    )
        public
        Crowdsale(rate, wallet, token)
        CappedCrowdsale(cap)
        TimedCrowdsale(openingTime, closingTime)
        //RefundableCrowdsale(goal)
    {
        //As goal needs to be met for a successful crowdsale
        //the value needs to less or equal than a cap which is limit for accepted funds
        //require(goal <= cap, "NewsCrowdSale: goal is greater than cap");

    }
}
  • 1
    Buying tokens should not deploy a new contract. So I'm afraid I don't understand what you are trying to do and when exactly do you get that error message. – Lauri Peltonen Aug 15 at 5:20
0

You appear to have created local copies of the OpenZeppelin Contracts. It is recommended to install npm install @openzeppelin/contracts and then import.

Your Token is commented out, and whilst it is mintable, an initial supply is also minted. When using with a MintedCrowdsale you don't need to pre-mint tokens as tokens will be minted as they are sold.

Your Crowdsale inherits from CapperRoleMock which is a Mock contract used for testing. You have a goal parameter in your constructor that can be removed as you have commented out RefundableCrowdsale. (You should remove commented out code)

For a MintedCrowdsale once you have deployed the token and the crowdsale you need to give the crowdsale the MinterRole.

For a TimedCrowdsale you can only buyTokens once the crowdsale isOpen

For an IndividuallyCappedCrowdsale you need to set a cap for each purchaser.

If you haven't done all of the above, then buyTokens will fail.

Below is a sample token, crowdsale, migrations script and manual test in truffle console.

See the OpenZeppelin Contracts Crowdsale documentation for details: https://docs.openzeppelin.com/contracts/2.x/crowdsales

If you have questions on using OpenZeppelin you can ask in the Community Forum: https://forum.openzeppelin.com/

Disclosure: I am the Community Manager at OpenZeppelin

MyToken.sol

pragma solidity ^0.5.0;

import "@openzeppelin/contracts/token/ERC20/ERC20Mintable.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol";

/**
 * @title MyToken
 * @dev Very simple ERC20 Token that can be minted.
 * It is meant to be used in a crowdsale contract.
 */
contract MyToken is ERC20Mintable, ERC20Detailed {
    constructor () public ERC20Detailed("My Token", "TKN", 18) {
        // solhint-disable-previous-line no-empty-blocks
    }
}

MyTokenCrowdsale.sol

pragma solidity ^0.5.0;

import "@openzeppelin/contracts/crowdsale/validation/CappedCrowdsale.sol";
import "@openzeppelin/contracts/crowdsale/validation/TimedCrowdsale.sol";
import "@openzeppelin/contracts/crowdsale/emission/MintedCrowdsale.sol";
import "@openzeppelin/contracts/crowdsale/validation/IndividuallyCappedCrowdsale.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Mintable.sol";

contract MyTokenCrowdsale is IndividuallyCappedCrowdsale, CappedCrowdsale, MintedCrowdsale, TimedCrowdsale {
    constructor (
        uint256 openingTime,
        uint256 closingTime,
        uint256 rate,
        address payable wallet,
        uint256 cap,
        ERC20Mintable token
    )
        public
        Crowdsale(rate, wallet, token)
        CappedCrowdsale(cap)
        TimedCrowdsale(openingTime, closingTime)
    {
    }
}

2_deploy.js

const MyToken = artifacts.require('MyToken');
const MyTokenCrowdsale = artifacts.require('MyTokenCrowdsale');

module.exports = async function (deployer, network, accounts) {
  await deployer.deploy(MyToken);
  const token = await MyToken.deployed();

  var date = new Date();
  const startTime = Math.floor((date.getTime() - date.getTimezoneOffset())/1000);
  const openingTime = startTime + 5;
  const closingTime = startTime + 7 * 24 * 60 * 60;
  const rate = 1;
  const wallet = accounts[2];
  const cap = "1000000000000000000000000";

  await deployer.deploy(MyTokenCrowdsale, openingTime, closingTime, rate, wallet, cap, token.address);

  const crowdsale = await MyTokenCrowdsale.deployed();

  await token.addMinter(crowdsale.address);
  await token.renounceMinter();

  await crowdsale.setCap(accounts[1], "100000000000000000000")
};

Test in truffle console

$ npx truffle console
truffle(development)> token = await MyToken.deployed()
truffle(development)> crowdsale = await MyTokenCrowdsale.deployed()
truffle(development)> accounts = await web3.eth.getAccounts()
truffle(development)> crowdsale.buyTokens(accounts[1], {value:"1000000000000000000"})
truffle(development)> (await token.balanceOf(accounts[1])).toString()
'1000000000000000000'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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