etherdelta / etherdelta.github.io

https://etherdelta.com
554 stars 689 forks source link

Why do some trades point to a different contract? (not etherdelta_2) #236

Closed MeoMix closed 6 years ago

MeoMix commented 6 years ago

Hey there,

So, I've got the basics of my bot up and running, but I notice that it's sometimes up to two blocks behind other bots that are trying to snipe orders mine is interested in. I've been doing my best to figure out ways to speed things up to hit an earlier block and, in doing so, I came across something interesting.

When I look at EtherScan entries for a normal trade, https://etherscan.io/tx/0xf43794b1505f8d53b9116be8800d4d8b7e8e3a16f3249cd778cbcbb41210c4ee, it has a couple of key features:

By contrast, a trades that I suspect is automated: https://etherscan.io/tx/0x0b4a8885086b6885116fd3d1d1c6b68915129b795b94c59afe81e43239b406c0

Could you shed some light on how this works?

Presumably the input data is a more raw format which means they're able to transmit less over the wire...?

How on earth does sending to another contract work, though? Have they written their own smart contract which is internally interface with EtherDelta? I'm not sure how that would be beneficial unless they've managed to do something malicious. If this is not what's occurring... an enlightening explanation would be nice!

Thanks! :D

zackcoburn commented 6 years ago

Yep, this is someone who has written a smart contract that trades with EtherDelta. It does two trades at once, locking in an arbitrage, and either fails on both or succeeds on both. The code isn’t verified, but such a contract would be relatively simple to write. We can tell what it does because it outputs two EtherDelta trade events. Whoever is doing this is running a script that then submits his trades through this smart contract.

On Sun, Nov 5, 2017 at 4:31 AM Sean Anderson notifications@github.com wrote:

Hey there,

So, I've got the basics of my bot up and running, but I notice that it's sometimes up to two blocks behind other bots that are trying to snipe orders mine is interested in. I've been doing my best to figure out ways to speed things up to hit an earlier block and, in doing so, I came across something interesting.

When I look at EtherScan entries for a normal trade, https://etherscan.io/tx/0xf43794b1505f8d53b9116be8800d4d8b7e8e3a16f3249cd778cbcbb41210c4ee, it has a couple of key features:

  • To:Contract 0x8d12a197cb00d4747a1fe03395095ce2a5cc6819 (etherdelta_2)
  • Input data is partially human readable

By contrast, a trades that I suspect is automated: https://etherscan.io/tx/0x0b4a8885086b6885116fd3d1d1c6b68915129b795b94c59afe81e43239b406c0

  • Contract 0xf44072eda87ede9613b6ae1b812de8a60ade27a2
  • Input data is hex buffer.

Could you shed some light on how this works?

Presumably the input data is a more raw format which means they're able to transmit less over the wire...?

How on earth does sending to another contract work, though? Have they written their own smart contract which is internally interface with EtherDelta? I'm not sure how that would be beneficial unless they've managed to do something malicious. If this is not what's occurring... an enlightening explanation would be nice!

Thanks! :D

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/etherdelta/etherdelta.github.io/issues/236, or mute the thread https://github.com/notifications/unsubscribe-auth/APEqPQzIEq8WNWUZKeHIm75Mnp34DFRqks5szYCHgaJpZM4QSUNy .

-- Zack Coburn

NOTICE: This e-mail transmission (and/or the attachments accompanying it) may contain confidential information belonging to the sender which is protected by law. The information is intended only for the use of the intended recipient. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or the taking of any action in reliance on the contents of this information is strictly prohibited. Any unauthorized interception of this transmission is illegal. If you have received this transmission in error, please promptly notify the sender by reply e-mail, and then destroy all copies of the transmission.

MeoMix commented 6 years ago

@zackcoburn

Thanks again for the quick reply. I don't know much about Solidity development and I will spend my day brushing up on that to try and better understand why someone might choose to do this over simply interacting with the contract.

Would you be able to elaborate on some benefits of using a second contract to interact with yours?

You note that they can 'lock in' an arbitrage - basically by doing the work on-chain they don't run the risk of having their trades in separate blocks and unable to control the success of one/failure of another?

Are there other benefits? I was thinking maybe if they did the derivation of v/r/s in the contract then they'd be able to shave off some processing time before being on-chain (at the cost of a more expensive contract execution?)

Anything else? :) Sorry for bothering you. I'm sure you get it all the time, but am having a lot of fun trying to make a few pennies off your EtherDelta! 👍

zackcoburn commented 6 years ago

There are two main benefits I can think of. One is what you mentioned, that you can have your function call EtherDelta’s trade function twice, and throw an error (reversing everything but still spending gas) if either trade fails. The other benefit is you should be able to save a little bit of gas. V, r, and s aren’t part of the savings, since you still have to submit both original orders’ v, r, and s parameters — they can’t be calculated on chain as they are calculated by the people who placed the orders in the first place.

On Sun, Nov 5, 2017 at 12:59 PM Sean Anderson notifications@github.com wrote:

@zackcoburn https://github.com/zackcoburn

Thanks again for the quick reply. I don't know much about Solidity development and I will spend my day brushing up on that to try and better understand why someone might choose to do this over simply interacting with the contract.

Would you be able to elaborate on some benefits of using the contract?

You note that they can 'lock in' an arbitrage - basically by doing the work on-chain they don't run the risk of having their trades in separate blocks and unable to control the success of one/failure of another?

Are there other benefits? I was thinking maybe if they did the derivation of v/r/s in the contract then they'd be able to shave off some processing time before being on-chain (at the cost of a more expensive contract execution?)

Anything else? :) Sorry for bothering you. I'm sure you get it all the time, but am having a lot of fun trying to make a few pennies off your EtherDelta! 👍

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/etherdelta/etherdelta.github.io/issues/236#issuecomment-341992215, or mute the thread https://github.com/notifications/unsubscribe-auth/APEqPTFqmc2AyR_Z0aPomAeoKSv93UmTks5szfdzgaJpZM4QSUNy .

-- Zack Coburn

NOTICE: This e-mail transmission (and/or the attachments accompanying it) may contain confidential information belonging to the sender which is protected by law. The information is intended only for the use of the intended recipient. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or the taking of any action in reliance on the contents of this information is strictly prohibited. Any unauthorized interception of this transmission is illegal. If you have received this transmission in error, please promptly notify the sender by reply e-mail, and then destroy all copies of the transmission.

MeoMix commented 6 years ago

Alrighty! Neither of those actually seem to be what's most interesting to me - being beaten out by two blocks!

I'm running my own Ethereum node at home. I live in SF, have a 1GB line, and it's on a SSD. The code I run calls web3.sendSignedTransaction within milliseconds of receiving the orders event from the socket. It's in Node... could be ran lower level.. but that seems like a premature optimization.

Since orders are able to be placed off-chain -- I believe I have an absolute need to interact with the EtherDelta socket to be notified of orders being added. Right?

And yet, when I view transactions I'm beat out on, I see this code sometimes getting its transaction a full two blocks ahead of me. Not even like, the last transaction in one block vs first in another. Solid placements in the transactions.

I played around a bit with trying to tracert the socket server, but it all just runs into CloudFlare. Thought I might be able to spin up something in the cloud and place it nearer the ED server.

Is it just pure, dumb luck that they sometimes win out? I would expect to be competing on gas price, in the same block, more often than not. I would then expect sometimes to be a block ahead, or block behind, others competing for the same transaction - just due to network variance. Two blocks behind would seem to imply I'm ~12s slower? None of the numbers I'm considering are anywhere close to accounting for something like that.

If you felt like sharing other tips I'd happily take them! If not, fully understand, I'm definitely picking your brain more than I should be in a GitHub issue.

zackcoburn commented 6 years ago

I actually can’t think of a good answer for being two blocks behind.

On Sun, Nov 5, 2017 at 1:12 PM Sean Anderson notifications@github.com wrote:

Alrighty! Neither of those actually seem to be what's most interesting to me - being beaten out by two blocks!

I'm running my own Ethereum node at home. I live in SF, having a 1GB line, and it's on a SSD. The code I run calls web3.sendSignedTransaction within milliseconds of receiving the orders event from the socket.

And yet, when I view transactions I'm beat out on, I see this code sometimes getting its transaction a full two blocks ahead of me. Not even like, the last transaction in one block vs first in another. Solid placements in the transactions.

I played around a bit with trying to tracert the socket server, but it all just runs into CloudFlare.

Is it just pure, dumb luck that they sometimes win out? I would expect to be competing on gas price, in the same block, more often than not. I would then expect sometimes to be a block ahead, or block behind, others competing for the same transaction - just due to network variance. Two blocks behind would seem to imply I'm ~12s slower? None of the numbers I'm considering are anywhere close to accounting for something like that.

If you felt like sharing other tips I'd happily take them! If not, fully understand, I'm definitely picking your brain more than I should be in a GitHub issue.

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/etherdelta/etherdelta.github.io/issues/236#issuecomment-341993024, or mute the thread https://github.com/notifications/unsubscribe-auth/APEqPV0tKhwo9OETNh_IkBf4PZQ-IPmIks5szfp-gaJpZM4QSUNy .

-- Zack Coburn

NOTICE: This e-mail transmission (and/or the attachments accompanying it) may contain confidential information belonging to the sender which is protected by law. The information is intended only for the use of the intended recipient. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or the taking of any action in reliance on the contents of this information is strictly prohibited. Any unauthorized interception of this transmission is illegal. If you have received this transmission in error, please promptly notify the sender by reply e-mail, and then destroy all copies of the transmission.

MeoMix commented 6 years ago

Alrighty! I'll look into it a bit more today and see if I can reproduce it. Thanks for taking so much time to answer. See you around.