1
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;
}

using this to set the minimal ETH investement, still receiving tokens when sending 0.1> amount of ETH. Testing on the testnet. Any ideas? Contract: 0xb1a89c746ecec28bfdf7f06f44b603bf9c035be8 with verified code.

2

The confusion has been created because you posted the code for transfer but not the code for the fallback function, just as @mafrasi2 points out.

Now, the fallback function determines the number of tokens and update the balances. Here you can put the restrictions on the amount of ether, like this:

function () public payable {
        require(now >= startDate && now <= endDate);
        require(msg.value >= min_contribution);
        require(msg.value <= max_contribution);
        uint tokens;
        if (now <= bonusEnds) {
            tokens = msg.value * 12000;
        } else {
            tokens = msg.value * 10000;
        }
        balances[msg.sender] = safeAdd(balances[msg.sender], tokens);
        _totalSupply = safeAdd(_totalSupply, tokens);
        emit Transfer(address(0), msg.sender, tokens);
        owner.transfer(msg.value);
    }

But this is not all, you must remove the limits in the transfer function because in the current state the users of your token will never be able to transfer it unless they send ether, which is odd. So transfer should be like this:

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

hope this helps.

1
  • Sir, you`ve just made my week! Thank you very much. Everything is working even if i am sending ehter from MyEtherWallet. Realy appreciate it!
    – Daniel
    May 24 '18 at 17:28
2

As far as I know, the ether values that you have on:

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

Solidity gets it as Wei by default, not as ether. Since a Wei is an indivisible unit, you can't have 0,X Wei.

Try putting the ether values on Wei knowing that 1 ether = 1E18 Wei.

Maybe this is faster: https://etherconverter.online/

The function seems to be Ok written, so I don't think it's the problem.

Hope it helps!

14
  • ive change it to weis, but still receiving tokens when i am sending less then 0.1 ether
    – Daniel
    May 23 '18 at 9:22
  • 2
    The correction described by @Cpereez19 works, I implemented this in remix and it prevent deposits smaller than 0.1 ether. Be sure that you are compiling and deploying again after the changes.
    – Jaime
    May 23 '18 at 9:41
  • Same here, tested the @CPereez19 advice and all works fine on remix.
    – Kevin J.
    May 23 '18 at 10:25
  • But look, ive just did the transaction with 0.01 ether and still the same... ropsten.etherscan.io/tx/… .Maybe its about the way ive tested it? Ive deployed the contract in remix and sent 0.01 from MyEtherWallet to contract address. @Jaime
    – Daniel
    May 23 '18 at 18:25
  • 1
    Did you just send ether or did you use the transfer function?. In your contract sending ether will just work but sending ether through the transfer function you show in the question will revert if the amount is smaller than 0.1. Let me know.
    – Jaime
    May 24 '18 at 8:49
1

@Jaime hinted at it: you don't call the transfer() function. Instead you are calling this function:

function () public payable {
    require(now >= startDate && now <= endDate);
    uint tokens;
    if (now <= bonusEnds) {
        tokens = msg.value * 12000;
    } else {
        tokens = msg.value * 10000;
    }
    balances[msg.sender] = safeAdd(balances[msg.sender], tokens);
    _totalSupply = safeAdd(_totalSupply, tokens);
    emit Transfer(address(0), msg.sender, tokens);
    owner.transfer(msg.value);
}

Your transfer() function is correct without any modifications. Solidity will convert 0.x ether to wei just fine.

3
  • Everything i need is minimal investement amount for anyone who sending ether to contract address. So for example: Person sending from his ethereum wallet less then 0.1 ether and not receiving any tokens and getting his tranastion back.
    – Daniel
    May 24 '18 at 15:05
  • You didn't modify the correct function then. Both requires(...) should go into the function I posted, not into transfer(). Also, transfer() should definitely not be payable.
    – mafrasi2
    May 24 '18 at 15:45
  • ropsten.etherscan.io/address/… still can receive tokens after less then 0.01 transaction. Also can`t deploy the code when remove payable
    – Daniel
    May 24 '18 at 16:36

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.