0

If I'm correct the nonce is responsible for the order transactions are executed. Let assume you have two transactions T1 & T2. T1 depends on T2. So I want to have an order T1 then T2. This is done by giving T1 the nonce and T2 nonce + 1. (please correct me if that is false)

Now in some case, there might be the situation that T2 depends also on the state of a contract. So I want to make sure that T1 is executed only then T2 can be executed. Is there a way to do this?

So actually to make sure T1 & T2 are included in the same block. I think that could be done by a costume contract but not sure if there is a better or kind of standard solution to it.

Thanks a lot for any advice!

PS: Example: I want to buy something. T1 deposit money (or make approval) to a contract. T2 buy something using the contract.

Real Example: I want to buy a token on Ether Delta. T1: deposit. T2: Buy token. It doesn't make sense to deposit if someone was faster and bought that token for the given price already.

  • What do you mean that T1 depends on T2? Like in A has no money, B sends A some tokens in T2 and A sends C in the same block? – meta Feb 14 '18 at 19:05
  • Yes something like this. Simple example. I want to buy something. T1 deposite money (or make approval) to a contract. T2 buy something using the contract. – MichaelRazum Feb 14 '18 at 19:09
  • I don't know the answer, but you may consider updating you question to improve it. I would guess that it's not really doable: Each of the deposit/buy transaction is probably triggered by different entities, so in order to buy you need data in blockchain that someone made a deposit. Only if deposit and buy are triggered in the same method by the same entity you would have data in the same block - but I don't think this is the kind of contract you're developing. – meta Feb 14 '18 at 19:14
  • There's the 'contract constructor trick'. It allows to execute two 'transactions' in a constructor on behalf of the contract and at the end it will selfdestruct. It allows to atomically execute two internal transactions, ie both will execute or a failure in any of them will revert both. It may not be cheaper, and require some preparation, but it will avoid race attacks between both transactions. – Ismael Feb 15 '18 at 17:47
0

So I want to have an order T1 then T2. This is done by giving T1 the nonce and T2 nonce + 1. (please correct me if that is false)

If both T1 and T2 are sent from the same account, then the nonce order will be obeyed, yes.

T2 depends also on the state of a contract. So I want to make sure that T1 is executed only then T2 can be executed. Is there a way to do this?

Scenario 1: If T1 and T2 are from the same account, then this order is already guaranteed by the nonces.

Scenario 2: If they are from different accounts, then T2 will only succeed if it makes a valid change to the state. If it doesn't make a valid change to the state, it will fail and won't be included in a block.

If T1 is required to make the state "valid" before T2 arrives, then you could just ensure that if T2 fails (because T1 hasn't happened yet), then it gets resubmitted.

If T1 isn't required to make the state "valid", and T2 could work without T1 having happened, then you'll need to be careful about ensure the ordering.

So actually to make sure T1 & T2 are included in the same block.

It's not clear why you want them to be included in the same block. As long as T1 is before T2 - even in an earlier block - then the state changes will work.

Further, you can't guarantee their inclusion in the same block. It's up to the miners. You could try to make it happen with gas prices, but again, there's no guarantee.

  • Hi there. Thanks for the explanation. I updated my question. The concrete scenario is to buy something where first a deposit is required. But it can happen that T1 is executed (deposit 1 ETH). But then T2 isn't executed immediately and the item is bought by someone else. – MichaelRazum Feb 14 '18 at 19:21
  • Is it the same person (account) doing T1 and T2, or two different people? – Richard Horrocks Feb 14 '18 at 19:22
  • Yes same person! But the item could be bought by a different person. So T2 can only be executed if nobody did the purchase. – MichaelRazum Feb 14 '18 at 19:25
  • Okay, T2 will always come after T1 if they're from the same person. If I'm understanding you correctly, your contract could detect the first user's deposit (T1) then freeze the bid for everyone else to stop them buying it with their T2. The freeze could time out after a certain amount of time or number of blocks. But it would completely depend on the design of your contract. – Richard Horrocks Feb 14 '18 at 19:34
  • sorry I was not precise enough. I’m interfacing with a foreign contract. So can’t do anything on the contract side. My idea was that I could write my own contract and it would have a function that executes T1 and T2 and interfaces with the foreign contract. – MichaelRazum Feb 14 '18 at 19:47

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.