Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add advanced trading features to DEXs, e.g. stop losses/buys #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

quantaf1re
Copy link

Currently on-chain DEXs lack any features other than basic orders. This is a significant source of friction for transitioning users from centralised exchanges to decentralised ones because people can't use their regular trading strategies. Simple things like stop losses are impossible in current DEXs because smart contracts/blockchains are passive (they only change their state when someone interacts with them), so a user needs to be online in order to push an order through.

This solution uses the ASC (Active Smart Contract/Active State Changer) Protocol to make a smart contract (Eth etc), or, in this case, a Cosmos module, active in the sense that a user can set some action to happen in the future then go offline forever and the state change will still happen in the future while the user is offline. This allows users to set up subscription payments that don't require them to manually sign something every month, advanced trading functionality, auto-closing of a user's own MakerDAO CDP that's close to being undercollateralised, investment/insurance funds that automatically pay out to users, and even allows smart contracts/modules to autonomously enact changes in the physical world by using services such as Chainlink on their own. All that is required is the use of the ASC Protocol and tokens to be locked up to fund the contracts' tx fees.

Technically the contract/module still requires txs from people to enact the changes it wants to cause, but those txs can be called by anyone and are incentivised by the contract/module paying out a small reward to whoever calls them and are therefore economically guaranteed. In the same sense that the concept of a Bitcoin is an abstraction of UTXOs, ASCs are an abstraction of incentivised txs.

In this PR I have added a Stop message type to orders. It basically wraps Post but adds a condition that makes it fail if the most recent price of the relevant coin is outside the range of values that the stop should be called in. Before going offline, the user can give this transaction to someone (a relayer) who will profit from broadcasting the transaction at the correct time by giving a specified amount of coins to the relayer upon successful execution of the trade. The user is therefore economically guaranteed that their transaction will execute as soon as the stop condition is met because game theoretically, the relayer's best choice to maximise profit is to execute it as soon as it will succeed because there may never be a time in the future when the same condition will be met again and therefore impossible for the relayer to profit.

Transaction flow:

  • The user finds a relayer, ideally through a web-market of relayers that have each publicly stated they are willing to hold onto transactions for competing fee rates, such as a stop loss, and watch the relevant price data until they are able to fulfill the condition of the trade
  • The user signs a Stop transaction but does not broadcast it to the wider network
  • The user privately sends the signed transaction in raw hex format to the chosen relayer through their chosen channel, who then stores the tx locally
  • The relayer watches the relevant price
  • When the market price goes above or below the stop price (depending on initPrice), the transaction is able to be executed, so the relayer broadcasts the tx to the wider network
  • The tx is executed successfully. The user has bought or sold around their desired price. Cancer is cured. There is peace in the Middle East. World hunger is no more.

Notes:

  • This application of the ASC Protocol is clunky and specific to implementing stop losses here in this DEX and was done in a way that was mainly chosen for ease of use and speed for me as a hackathon project who only started to read any Cosmos code about 14 hours before this PR was submitted. The much more generalised implementation is A) to have a GSN-like network of nodes who basically run a service of having a 2nd mempool (for arbitrarily many blockchains) that is constantly checked to see if the txs can be executed, B) done in a way that the relayer is the one who signs the tx so that it's impossible for the relayed to grief the user by prematurely sending the tx
  • The stop loss is private to everyone except the person who will execute it, like regular centralised exchanges
  • The worst case scenario for the user is that the relayer broadcasts the trade prematurely and therefore the user loses a small tx fee from the tx failing. The user can then blacklist that relayer if they come back online and notice the trade wasn't executed despite the conditions having been fulfilled at some point while the user was offline.
  • It can be used for stop-losses or stop-buys in any direction
  • I didn't use start-block and end-block because there is an attack vector for filling one of those will junk txs that will never execute and forcing them to be checked every block for the rest of time for very little gas cost

@alessio
Copy link
Contributor

alessio commented Nov 3, 2019

Thanks for taking the time to open this PR!
It will be reviewed shortly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants