ERC-20 Token + Crowdsale

Hello im new to coding in solidity and i am having some problems programming a smart contract. I want to create a basic token with name, symbol and an initial amount. This is my code so far for the Token :

/* SPDX-License-Identifier: MIT */

pragma solidity >=0.6.0 <0.8.0;

import ‘https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.4.0/contracts/token/ERC20/ERC20.sol’;

contract ProjectGreenToken is ERC20 {

constructor(
    
    string memory name,
    string memory symbol,
    uint256 initialSupply
    
)  ERC20(name, symbol) {
    _mint(msg.sender, initialSupply);
}

}

The Token seems to work fine I can create it send it etc.
Now i also want to create a crowdsale for this token meaning that other wallets can send ether to a specific account to receive my token. So far my code for the crowdsale looks like this:

pragma solidity ^0.4.23;

import “https://github.com/ConsenSysMesh/openzeppelin-solidity/blob/master/contracts/crowdsale/Crowdsale.sol”;

contract ProjectGreenCrowdsale is Crowdsale {

constructor(
    uint256 rate,
    address wallet,
    ERC20 token
    
) Crowdsale(rate, wallet, token) {}

}

I can deploy the crowdsale contract just fine however i cant buy tokens from the contract. After creating the token i of course sent the created tokens to the crowdsale contract however i was still unable to purchase the error message i am getting is: “Gas price could not be calculated, transaction will most likely fail”
My guess is that my contract doesnt link my created token to the crowdsale. Any help would be greatly appreciated

try sending with 299999 gas limit

Did not work. Also im kinda wondering When i deploy the Crowdsale contract i put in an address which holds token and an adress where the ETH is supposed to be sent to. However once deployed the contract only has the function “buy tokens” where i only need to put in a beneficiary address. How is that supposed to work if i cant even put in an amount to “buy”

Okay so i figured out that the smart contract has its own adress so im guessing to “buy” the crowdsale tokens i have to send ETH to that adress. However this also fails with the Error message: " Transaction reverted smart contract failed to execute" on MyEtherWallet. So i am guessing something is missing from the code.

Because you have to put the amount on the wei section on Remix. Anyway you can modify your code to receive BNB in this way:
User send bnb to the contract address and receives tokens based on a rate you choose

Im sorry i dont get it. What does BNB have to do with this ? Im trying to receive ETH

yes sorry ETH. I said BNB because I am actually working on BSC :sweat_smile:

1 Like

so I have added the buy Tokens function to the code however it is still not accepting any transfer of ETH to the smart contract. my current code is:

pragma solidity ^0.4.23;

import ‘https://github.com/ConsenSysMesh/openzeppelin-solidity/blob/master/contracts/crowdsale/Crowdsale.sol’;

contract ProjectGreenCrowdsale is Crowdsale {

constructor(
uint256 rate,
address wallet,
ERC20 token

) Crowdsale(rate, wallet, token) {}

function () external payable {
buyTokens(msg.sender);
}

function buyTokens(address _beneficiary) public payable {
require(_beneficiary != address(0));
require(msg.value != 0);

uint256 tokens = msg.value.mul(rate);
weiRaised = weiRaised.add(msg.value);

token.transfer(_beneficiary, tokens);
wallet.transfer(msg.value);

}
}

I think i have to transfer ownership of the token to the contract apparently just having the tokens in the wallet specified in the smart contract is not enough. Do you think that could be the issue ?

If you do not want us to know the Token name, it would be useful to follow these steps anyway.
Is code optimization turned on?
How did you compile the code? Please, if you want help to the smallest detail, you can distribute the code in a different name in the test network as you did it. Dude now send some ether to the contract address! Up to 0.001! I assume it exists in the main network. If he sends you tokens in return, we will have to tie a new contract to this contract!
This contract will send directly to the other contract and sell through the other contract. The cons of this situation are that the user pays more gas when buying. Or if you want the cleanest and the coin has not spread yet, I suggest you start over. We can only help that much without knowing the details. Good luck with.

Please use triple backticks (```) surrounding your code in order to properly format it.