Add advanced trading features to DEXs, e.g. stop losses/buys #4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 wrapsPost
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 (arelayer
) 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:
Stop
transaction but does not broadcast it to the wider networkinitPrice
), the transaction is able to be executed, so the relayer broadcasts the tx to the wider networkNotes:
start-block
andend-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