The drawbacks of today’s centralized financial system have led to the emergence of Decentralized Finance, or “DeFi” for short. DeFi applications are built on blockchains to increase transparency in financial markets by removing the control of a central authority. One such type of DeFi applications is decentralized cryptocurrency exchanges (DEXs) like Switcheo.

Decentralized exchanges enable peer-to-peer (p2p) trades on the blockchain using smart contracts, allowing users to trade cryptocurrencies without being exposed to custodial risks inherent with centralized exchanges (e.g. the infamous Mt Gox hack where over 850,000 bitcoins, valued at US$3 billion today, had been stolen, also Binance’s loss of US$40 million from a large security breach earlier this year). Truly decentralized exchanges are therefore non-custodial such that even the exchange owners (or any other 3rd-party) have no access to users’ tokens.

However, first-generation decentralized exchanges like EtherDelta turned out to be slow and cumbersome to use. Since everything from order matching and trade settlement are performed fully on the blockchain, users of EtherDelta had to wait for their trade transactions to be mined and confirmed on the blockchain (which could take a few seconds to several minutes) before making their next order. This performance-limiting dependency resulted in traders being incapable of executing swing trades rapidly, ultimately giving a poor user experience.

At Switcheo, our users’ trading experience is very important to us. This is why Switcheo Exchange is built upon a unique infrastructure that supports instant trade confirmations, freeing our users from waiting for transactions to be mined.

In this two-part series, we will first explore how Switcheo Exchange works on the NEO and Ethereum blockchains, such that it supports instant trading while remaining non-custodial. In the next part, we will continue by examining how Switcheo works on the EOS blockchain.

Switcheo Exchange Infrastructure

A first-generation decentralized exchange is usually made up of just two components: the first being its smart contract that is deployed on the blockchain to process trades, and the second being the user interface, which acts as a direct gateway between the trader and the smart contract.

Unlike first-generation decentralized exchanges, the infrastructure of Switcheo Exchange comprises of three layers which include a user interface, a smart contract, and the Switcheo Matching Engine.

The 3 layers to Switcheo Exchange

The interaction between these three components allows Switcheo Exchange to be performant while still being non-custodial.

The Switcheo Smart Contract is a self-executing computer program deployed on the blockchain. It automatically tracks data such as the amount of each users’ digital assets that are deposited into Switcheo Exchange. But distinct from depositing into a centralized exchange, users retain sole ownership of their funds on Switcheo Exchange.

This is because tokens are deposited directly into the smart contract rather than the exchange’s wallet, and the smart contract only allows tokens to be traded or withdrawn only with the correct authorization from the user’s private keys. Since Switcheo does not acquire or store users’ passwords or private keys, the exchange is never able to access users’ funds directly. This is similar to how a first-generation DEX would work.

However, the reason why users can trade instantly and continuously on Switcheo Exchange lies within the Switcheo’s Matching Engine.

The Switcheo Matching Engine — is the key component that grants Switcheo Exchange its robustness and performance. The matching engine is an off-chain order matching system that accepts user trade requests. The matching engine then matches the order against the current order book on a price-time priority basis, and responds with the appropriate messages for the user to authorize their trade. Once authorized, these trades are then broadcasted to the blockchain for settlement. We will examine further how this is done safely in a short while.

The key benefit for executing order matching off-chain allows the order book on Switcheo Exchange to be updated in real-time instead of only after each block is produced. This allows users to have their orders instantaneously confirmed in the same way as a centralized exchange, allowing users to make a large number of orders rapidly. Of course, this also means that order cancellations are instantaneous on Switcheo Exchange, and it is impossible to have failed transactions wasting network fees due to being front-runned.

Below, we illustrate what happens within Switcheo Exchange when a user makes an order, and when the order is matched.

• • •

A. Making an Order

In this example, a user, Bob, wants to sell 1 ETH for 200 DAI on Switcheo’s ETH/DAI market.

Step 1:
On the ETH/DAI market, Bob makes an order to sell 1 ETH for 200 DAI on the exchange user interface.

Step 2:
Bob’s order is sent from the exchange user interface to the Switcheo Matching Engine, where it is encoded into a message that the smart contract can understand, as shown below.

The message tells the smart contract to create such an order and later reduce Bob’s ETH balance by 1 (to pay for the order when matched).

Step 3:
The message is then sent to the exchange interface for Bob to authorize by signing the message with his private key (e.g. through MetaMask).

Doing so generates a digital signature that represents Bob’s authorization for his order (sell 1 ETH for 200 DAI). Bob’s digital signature is then sent back to the Switcheo Matching Engine for verification.

Step 4:
The Switcheo Matching Engine verifies that Bob’s digital signature is validand adds his order on the exchange order book immediately.

Switcheo’s user interface also updates his balance even before the smart contract is made aware of the order so that Bob will not make any invalid trades by mistake.

Both the message and Bob’s digital signature is sent to our Ethereum smart contract via an Ethereum transaction for the order to be executed (a.k.a balance settlement). It may take some time before the transaction is mined.

Step 5:
The Ethereum smart contract verifies Bob’s digital signature against the message containing Bob’s order upon receiving the order transaction.

This will never fail as the Switcheo Matching Engine has already verifiedthe digital signature, and is built to mirror the smart contract, maintaining a copy of the user’s balance and makes identical trade verifications to ensure that a valid trade is executed (e.g. sufficient balance).

Bob’s order will then be reflected on the blockchain (settled), reducing 1 ETH from Bob’s balance on the smart contract for making this order.

Bob will only be credited the 200 DAI once someone else matches his order to sell him DAI for ETH at the same or better price, and making that corresponding order on Switcheo. We will see that in the next section. Alternatively, he may also cancel the order to get back 1 ETH.

• • •

B. Order Matching and Trade Settlement

Next, a second user named Mary wishes to buy 0.75 ETH with 150 DAI on Switcheo Exchange.

Step 1:
On the ETH/DAI market, Mary makes an order to buy 0.75 ETH with 150 DAI via the exchange user interface.

Step 2:
Assuming Bob’s order is the best order to match, Mary’s order is sent from the exchange user interface to the Switcheo Matching Engine, where it is encoded into another message.

This message is different from the previous example, as it tells the smart contract that Mary wishes to fill Bob’s order, rather than to create a new order.

“150” refers to how much of Bob’s order to fill

Step 3:
The message containing Mary’s order is sent back to the user interface for Mary to sign.

Once again, doing so generates a digital signature that represents Mary’s authorization for filling Bob’s order (but only for 0.75 ETH worth). Mary’s digital signature is then sent back to the Switcheo Matching Engine for verification.

Step 4:
Again, the Switcheo Matching Engine verifies that Mary’s digital signature is valid. At this point, once a trade is considered executed, it will be pushed onto the exchange’s trade history immediately. All balances are updated even before the smart contract is made aware of the order, and the exchange order book is appropriately updated.

Both the message and digital signature are now sent to our Ethereum smart contract via an Ethereum transaction for the order to actually be executed (a.k.a balance settlement).

Step 5:
Switcheo smart contract verifies Mary’s digital signature against the message to fill Bob’s order.

Once verified, the trade is considered settled on the Ethereum blockchain. Mary’s balance on the smart contract is updated by having 0.75 ETH added and 150 DAI deducted, while Bob has 150 DAI added.

Since Bob already has 1 ETH reduced before, there is no need for Bob to interact in order to authorize the reduction of 1 ETH. Instead, his order is simply updated to reduce 0.75 ETH.

Step 6:
Since Mary only filled three-quarter of Bob’s order, Bob’s order remains open. The remainder of 0.25 ETH to be sold for 50 DAI is still displayed on Switcheo Exchange order book for more users to fill.

• • •

Since both the Switcheo Matching Engine and the immutable smart contract has to verify the same user digital signature, it is impossible for Switcheo off-chain matching engine to broadcast unauthorized trades. At the same time, it allows Switcheo’s order book to reflect orders and trades that have not yet settled on the blockchain since Switcheo is able to guarantee the validity of the signatures before the miners even receive the transactions.

In the steps above, we have shown how Switcheo keeps the entire trading process non-custodial, while allowing users to trade without caring about blockchain confirmations.

Using a modern trading interface, trading on Switcheo Exchange is just as easy as using a centralized exchange, but with additional security and transparency.

You Are In Control

Switcheo is committed to being non-custodial and giving users the financial freedom taken away by centralized entities. To ensure users have access to their funds when they trade on Switcheo, our open-source smart contracts also have functions that users can interact with to withdraw their funds from the blockchain directly.

• • •

We hope now you have a deeper understanding of how Switcheo Exchange is able to deliver a smooth trading experience on the NEO and Ethereum markets. Stay tuned for Part 2 as we will explore how Switcheo Exchange works on the EOS blockchain!

Visit Switcheo Exchange today to start trading crypto without KYC.

• • •

Like what you read? Check out these articles that may interest you:

The Ultimate Beginner’s Guide To Switcheo Exchange

Switcheo Discovery: How Switcheo Account Actually Works

Switcheo Discovery: How Atomic Swaps Work

• • •

For more information on Switcheo: