0

I am writing a contract where I need to check that if in 5 minutes no new token get purchased then the sale should be ended. What I mean exactly is that, lets say user A has purchased 100 tokens and now I want to see if no one purchase tokens in next 5 minutes then sale will be closed.

I am looking forward to gas efficient and automatic way.

can someone help please?

0

Maybe something like this?

uint256 lastSaleTimestamp;

function isOpen() public view {
    // The sale is open if there haven't been any purchases or if the last
    // purchase was more than 5 minutes ago.
    return lastSaleTimestamp == 0 || now <= lastSaleTimestamp + 5 minutes;
}

function buy(...) external {
   require(isOpen(), "Sale is already closed.");
   lastSaleTimestamp = now;
   ...
| improve this answer | |
  • but this will check only when a new purchaser will come. I need to do some more stuffs on sale closing, so I want the contract to check itself after 5 minutes that if no new purchaser has come then contract should execute saleclose() function and do the needful stuff. – Fariha Abbasi Sep 8 '19 at 21:19
  • Contract code is only executed as part of a transaction made be an externally owned account. It's hard to make a suggestion without knowing what "the needful stuff" is, but you can always have a saleclose() function that you can call after 5 minutes have passed. – user19510 Sep 8 '19 at 21:40
  • in "needful stuff", I need to distribute funds to the existing token holders. so I want to check automatically, if there is a way, that if 5 minutes are passed then contract shall automatically do this? – Fariha Abbasi Sep 8 '19 at 21:56
  • If you want to actually do that stuff in the contract immediately, you'll need to run some code somewhere that monitors the blockchain and calls salesclose() at the right time. It's generally better, though, to delay doing the computation until you need to. (E.g. when existing token holders call collect() or something like that.) – user19510 Sep 9 '19 at 0:06
  • thanks, can u also share your thoughts on this question? ethereum.stackexchange.com/questions/74728/… – Fariha Abbasi Sep 9 '19 at 11:18

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.