0
uint256 public max_contribution = 50 ether; 
uint256 public min_contribution = 0.1 ether; 

function transfer(address to, uint tokens) public payable returns (bool success) {
    require(msg.value >= min_contribution);
    require(msg.value <= max_contribution);
    balances[msg.sender] = safeSub(balances[msg.sender], tokens);
    balances[to] = safeAdd(balances[to], tokens);
    emit Transfer(msg.sender, to, tokens);
    return true;
}

So i used this to set the minimal investement, still had no luck, transaction succeed even if i send less than 0.1 ether. Can you guys check what is wrong with it? Also is it possible to set a return if someone sents less than a min.contribution?

1

It seems to work just fine. Try this in Remix:

pragma solidity ^0.4.22;

contract Test {

    uint256 public max_contribution = 50 ether; 
    uint256 public min_contribution = 0.1 ether; 

    function transfer() public payable returns (bool success) {
        require(msg.value >= min_contribution);
        require(msg.value <= max_contribution);
        return true;
    }
}

If you call the transfer function with 1 Ether, it returns true and everything works. If you call it with 0 or 60 Ether, the transaction fails and it is reverted.

  • it is sooooo weird, because everything works in remix, but when i am deploying it in the testnet, i can do the transactions lower than 0.1 – Daniel Apr 25 '18 at 9:08
  • It's probably not about where you test it but how you test it. My guess is that you are testing it in some wrong way in the testnet. – Lauri Peltonen Apr 25 '18 at 11:12
  • maybe... i am not too good at it. I am sending amount of tokens on contract adress from MEW that is lower than min_contribution and still receiving tokens, that`s it... – Daniel Apr 25 '18 at 11:21
  • msg.value is the amount of Ethers, not the amount of tokens. In your question you are asking about restricting amount of Ether. If you mean to restrict the amount of tokens, use tokens in your example instead of msg.value – Lauri Peltonen Apr 25 '18 at 11:23
  • sorry, that was a typo. I am sending ethers, not tokens – Daniel Apr 25 '18 at 11:27

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.