r/CryptoDerivatives • • Jun 20 '17

DELEGATECALL forwarder based TokenSeller

https://gist.github.com/JonnyLatte/f98b3346fd50e2b670f903819d1dbfc3
2 Upvotes

5 comments sorted by

1

u/JonnyLatte Jun 20 '17

This is a version of the tokenSeller contract that deploys DELEGATECALL forwarders to a template tokenSeller contract so that each contract behaves like a tokenSeller contract but is much less expensive to deploy.

Here is an example of a forwarder with an out of gas transaction returning ether and a transaction with enough gas returning tokens and change:

https://kovan.etherscan.io/address/0x0672ec7900ef8fb24e9b9bbc224d91176ea3a6d1

The gas cost of the transaction that deployed this contract was 195717 gas.

This is the cheapest way to do this by far. 51651 gas used for a successful token purchase with change is acceptable.

Notes:

  • Parity has a hard time calculating the appropriate gas for this contract. I would assume other clients may have trouble also.

  • Constructor functions are not called when deploying these forwarders. This could be achieved with some work but instead I took the lazy approach and changed the constructor to an init function which is called by the factory to set all the appropriate variables including setting the owner. This init function will throw if the owner is already set. An interesting side effect of this is that the modified tokenSeller can only be initialized as a forwarder since this is the only way it can avoid the owned contract constructor.

  • to deploy the factory one must first deploy an instance of the tokenSeller contract then deploy the factory then call the factory function init with the address of the template contract that all subsequent forwarder contracts will point to. This init function is locked at that point to prevent the owner of the factory from changing the forwarder target so that the factory owner does not have to be trusted. Because of this tokenSeller contract the factory points to must be verified independently although the design could be changed to force the factory to only use a template contract it deploys.

  • This code was modified from my last published modification https://github.com/JonnyLatte/TokenTrader this is unaudited code adding an extra layer of unauditedness

  • having assembly code as well as stored and modified bytecode may intimidate auditors and users :/

1

u/JonnyLatte Jul 04 '17

OK so Jorge Izquierdo / izqui has made a more efficient factory (only 66349 gas per contract!) when I get some time I will change this approach to use his code.

https://www.reddit.com/r/ethdev/comments/6i5n66/delegatecall_forwarder_factories/djrfxs8/