2

I'm writing a token contract now and I'm trying to only allow a user to purchase tokens before a certain block N. What I thought of doing is checking if block.number >= n and if so you either throw and error or issue a refund.

I'm not entirely sure which one I should do (or if this is even the correct approach).

     if(block.number >= 4370000){
      msg.sender.transfer(msg.value);
     }
2

Since "only allow transactions"

if(block.number > deadline) throw;

(Where deadline is a uint you set elsewhere).

That will invalidate transactions after the deadline, revert any state changes that may have happened and return any ether that was sent to the contract. Your house will be in order.

Hope it helps.

| improve this answer | |
  • 1
    Whats the difference between your approach and mine? Also does 'throw' automatically refund ether or are you telling me to do it myself? – ninesalt Jul 12 '17 at 15:41
  • 1
    It's a little confusing because your question is about users paying the contract but your sample is the contract paying the users. My sample assumes the users paid the contract but it's past the deadline so you have to return the money. Also, it's a best practice to fail early and fail hard. The transaction doesn't pass validation so it should fail completely. Your code continues processing after the if {} block when it should probably stop and revert everything. Makes sense? – Rob Hitchens Jul 12 '17 at 15:47
  • Isn't throw deprecated? – dcorking Jul 12 '17 at 17:46
  • No. You may be thinking of the new revert that is very similar but a little more kind because it returns unused gas instead of destroying it. Throw is still in play. – Rob Hitchens Jul 12 '17 at 18:09
  • 1
    Good one! Truffle is still on 0.4.11 and I didn't realize we're going to be forced to change our style. – Rob Hitchens Jul 13 '17 at 15:04

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.