Typefully

SwayLang's Predicate: A Low-Cost and Composable Order-Book DEX

Avatar

Share

 • 

3 years ago

 • 

View on X

@fuellabs_ has recently announced their beta-3 testnet, and there is a surge in interest for @SwayLang as we move closer to main net launch. Folks at @fuellabs_ @swaylend @compolabs have been producing awesome tutorials for the Sway language.
One of the features for @SwayLang is predicate, and I will explain how this feature allows developers to build a low-cost, and fully composable order-book DEX.
First,what is predicate? A predicate is a script that can read data from the transaction itself but not the blockchain and evaluate to either true of false. If the predicate evaluates to true, then tokens locked in the predicate can be unlocked and spent.
How does predicate help building a low-cost order-book DEX? A maker is posting OTC offers for takers to trade. The predicate will evaluate to true if the transaction sends X amount of tokens to the maker.
Thus any interested taker can send a tx that sends X amount of tokens to the maker and the predicate in the same tx will evaluate to true. Therefore sending the proceeds to the taker
A order-book DEX built with predicate needs to track all the offer predicates and operate a match engine. The nice thing is that predicates can't be deployed on-chain, thus it does not require a on-chain transaction to place an order.
Alternatively, some order-book DEX are built on their own rollups and thus users don't have to pay a transaction fee for placing orders. However, these DEXes aren't composable with other DeFi applications as they are built on their own rollups.
Therefore, an order-book DEX like @allsparkgg can offer no order placing costs (with predicate), while still remain composable with other DeFi applications launched on Fuel Network.
While @allsparkgg is still under development, @fuellabs_ has provided an example OTC-Swap built with predicate: github.com/FuelLabs/sway-applications/tree/master/OTC-swap-predicate#readme
In this example, a maker is posting OTC offers for takers to trade. The predicate will evaluate to true if the transaction sends X amount of tokens to the maker, here in L50-L57 github.com/FuelLabs/sway-applications/blob/master/OTC-swap-predicate/project/swap-predicate/src/main.sw#L50
The example also included a refund situation where the maker wishes to cancel the order and withdraw funds. The predicate will also evaluate to true if the transaction has an input from the maker, here in L32-L38 github.com/FuelLabs/sway-applications/blob/master/OTC-swap-predicate/project/swap-predicate/src/main.sw#L32
More on predicates being not deployable on-chain nor writing to chain. These are features designed as such, making it impossible for predicate to consume resources.
Predicate not being able to read from on-chain is also a feature, designed to prevent DoS attack. On-chain state might change due to temporary non-consensus.
Thus if the predicate can read from on-chain, it is possible to spam predicates and cause DoS on validators (more on medium.com/blockchain-capital-blog/the-power-of-predicates-73f874cfa9a5)
@dervoiedk @compolabs @SwayLang @IAmNickDodson @ManOfFrogs @Rubenevolent @0xGoldzn @camiinthisthang
Avatar

Dex Chen🦇🔊

@dex_chen_V

cooking @semanticlayer I'm all about MEV and stablecorn we are hiring semanticlayer.io/jobs