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

1 Answer 1

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.

6
  • 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
    Commented Jul 12, 2017 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? Commented Jul 12, 2017 at 15:47
  • Isn't throw deprecated?
    – dcorking
    Commented Jul 12, 2017 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. Commented Jul 12, 2017 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. Commented Jul 13, 2017 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.