Crowdsale: revert when buying tokens

Hi @shellteo

It looks there are not enough tokens to fulfill buyTokens.

Check crowd sale token balance

Does the crowdsale contract have any tokens?
In the code you provided, the MyToken contract mints 10,000,000,000 token (base units) to the deployer (msg.sender) whilst the crowdsale contract has zero tokens.

You could transfer the total supply minted to the crowdsale contract.

In Truffle Console you can do the following:

const myCrowdsale = await MyCrowdsale.deployed()
const myToken = await MyToken.deployed()
await myToken.transfer(myCrowdsale.address, await myToken.totalSupply())

Check rate and available tokens

The second thing to look at is the rate and the available tokens.

The rate is set to 1 which means 1 wei buys 1 token (base unit).
0.5 Ether gets 0.5 * 10**18 token base units, which even once the total supply is transferred to the crowdsale contract is more than the total supply of tokens.

Using a rate of 1, 10 gwei would get 10 * 10**9 token base units (the total supply).

await myCrowdsale.sendTransaction({value: web3.utils.toWei('10', 'gwei'), gas: '220000'})

Rate is covered in the following: