Rate to use for Crowdsale for ERC20 token not using 18 decimals

i search and i seen ur document all in web but problme is stile in 18 decimals all work fine but in decimal lower 4-5 all number go wrong

why do not list base on other decimals 2-4-5-10 provide examples

1 Like

Hi @TarrahArshad,

Welcome to the community :wave:

To calculate the rate for ERC20 tokens you can use the formula: TKNbits = rate * wei
This applies regardless of what decimals your token uses.

See the documentation for details: https://docs.openzeppelin.com/contracts/2.x/crowdsales#crowdsale-rate

If you need help calculating your rate, feel free to share your decimals and the amount of Ether per token.

Hi @TarrahArshad,

Did you need any more information?

hi andrew
thanks for ur response

my issue is rate .
i have crowdsale on multi stage
stage detail is ( start-price , end-price , cap , sold)
i need calc current base on sold and sold is how many tokens solde before so price increase.

i need know how increase rate and before that i need know formula for calc current rate depend on USD
for ex: $0.01-$0.25 its my range min-max price and i have max 500k token how calc current rate base on sold

and second my problem is i see with example i setn 250 wei and 500 wei crowdsale buytokens send wrong amount if i send 1 eth i receive 250 token only
250 wei rate must send 250 token if user send 1eth ? its wrong i no override ur crowdsale.sol i keeped ur but i create new function name buy and call ur buytokens original

this is my function

for buy and sell i use this

function buy(address beneficiary, address upline) public payable {
    require(
        contributions[upline].buy_amount > 0 || upline == _owner,
        "upline is wrong"
    );

    uint256 weiAmount = msg.value;

    // calculate token amount to be created
    uint256 tokens = _getTokenAmount(weiAmount);

    require(
        CrowdsaleStages[stage].cap >= CrowdsaleStages[stage].sold + tokens,
        "cap hit"
    );

    total_tokenSold += tokens;
    CrowdsaleStages[stage].sold += tokens;

    buyTokens(beneficiary);

    updateUser(beneficiary, upline, tokens);
 }

/**
 * @dev with this function client send request for selling
 * @param amount is how much token selling
 */
function sell(uint256 amount) public returns (uint256 ETHAmount) {
    require(amount > 0, "You need to sell at least some tokens");
    require(
        contributions[msg.sender].buy_amount - amount >= 0,
        "balance is not enoght"
    );
    if (sellBook[msg.sender].sell_time > 0) {
        require(
            sellBook[msg.sender].sell_time + duration_sell <
               uint40(block.timestamp),
            "in this hours u can't sell again"
        );
    }
    // 1. calculate eth amount - 5%
    ETHAmount = _getEthAmount(amount);

    // 2. transfer token from sender to crowdsale address

    /* uint256 allowance = token.allowance(msg.sender, address(this));

    require(allowance >= amount, "Check the token allowance");

    token.transferFrom(msg.sender, address(this), amount);*/

    //msg.sender.transfer(amount);

    // 3. transfer fund
    msg.sender.transfer(ETHAmount);

    // 4. update stage
    contributions[msg.sender].buy_amount -= amount;

    sellBook[msg.sender].id = SellID++;
    sellBook[msg.sender].sell_amount = amount;
    sellBook[msg.sender].sell_time = uint40(block.timestamp);

    // emit event
    emit Sold(msg.sender, amount);

    return ETHAmount;
}
 /**
 * The base rate function is overridden to revert, since this crowdsale doesn't use it, and
 * all calls to it are a mistake.
 */
function rate() public view returns (uint256) {
    return CrowdsaleStages[stage].initialRate;
}

/**
 * @dev Overrides parent method taking into account variable rate.
 * @param weiAmount The value in wei to be converted into tokens
 * @return The number of tokens _weiAmount wei will buy at present time
 */
function _getEthAmount(uint256 weiAmount) internal view returns (uint256) {
    uint256 currentRate = getCurrentRate();
    return weiAmount.div(currentRate).mul(uint256(95).div(uint256(100)));
}
1 Like

Hi @TarrahArshad,

The Crowdsales documentation can be found here: https://docs.openzeppelin.com/contracts/2.x/crowdsales

You could take the ideas from IncreasingPriceCrowdsale, extend Crowdsale and in a getCurrentRate function specify the rate after specific times.

I put together an example of manually setting a crowdsale rate using this. :warning: This code has not been tested or audited. Please appropriately test and audit before using in production.
Setting crowdsale rate manually

If you are selling based on a set fiat value, then you may be better to create a Crowdsale that accepts a stable coin rather than Ether. You could use the ideas from OpenZeppelin Contracts to do this.

Please see the documentation for calculating rate: https://docs.openzeppelin.com/contracts/2.x/crowdsales#crowdsale-rate

I recommend starting with something simple for testing purposes, such as a rate of 1 (assuming decimals of 18).