Multichain Decentralized Exchange Script
Decentralized exchanges (DEX) acts a highly tremendous roll in the crypto space world, DEX fixes the trust and security issues that have always been associated with third-party services like cryptocurrency exchanges. Given the initial crypto ethos of decentralization, traditional exchanges have become the single point of failure that the creation of blockchain technology itself aimed to fix.
Thanks to the ingenuity and hard work of the cryptocurrency development community, we now have tools to trade cryptos in the same way we send and receive them: Without the need to trust or rely on any middlemen.
Although decentralized exchanges are a fairly new concept, there are multiple alternatives out there, so it’s important to choose the best DEX crypto can offer.
What are Decentralized exchanges? (DEX)
Decentralized exchanges do not require the user to entrust their funds to a third party, thus fixing the problem associated with hacks, scams, and bankruptcy.
A decentralized exchange is an exchange market that does not rely on a third-party service to hold the customer's funds. Instead, trades occur directly between users (peer-to-peer) through an automated process.
Such a system can be established by creating proxy tokens (cryptoassets that represent a certain fiat or cryptocurrency) or assets (that can represent shares in a company for example) or through a decentralized multi-signature escrow system, among other solutions.
Smart contract based DEX:
DEX is a smart contract based, a decentralized cryptocurrency exchange which classified as two categories
Decoupling assets of the user from crypto exchange
The user's assets can be never in the control of DEX and therefore it cannot be lost even the exchange is hacked.
Instant trading experience
Orders are instantly placed, exchanged, matched and executed
A is the core strength of traditional decentralized exchanges, while B is the core strength of centralized exchanges. DEX achieves its both way by designing and driving smart contracts. It is called ROC (Replayed on chain)
Technical Backgrounds of two-ledgers
Dex has two trading ledgers: an off-chain ledger responsible for providing instant trading experience.
Smartcontract based on-chain ledger to ensure the traders asset are safe.
The two ledgers are closely synchronized, where are all the trading activities are first registered on offchain ledger and later gets exactly replayed and confirmed on the onchain ledger. The onchain ledger can be interpreted as a delayed version of off-chain ledger.
The off-chain ledger is a virtual trading floor with instant trading settlement.
Off-chain trading: Each trader has an offchain trading balance reflecting in the onchain trading balance. Traders can place buy and sell orders, when orders are matched, the off-chain trading balances are updated immediately. However, the balances on the offchain ledger are simply phantom balances and there is no actual transfer of tokens taking place off-chain. All the activity registered on the off-chain ledger must be confirmed on the onchain ledger where the actual transfer of takes place.
Private key signature: To verify the identity of traders and to ensure the offchain trading activity reflects the trader’s will, each other has to sign all off chain trading activity with the private key of the trader’s onchain account.
To avoid the mismatch between the off-chain and on-chain trading balances and to ensure that traders cannot trade-off chain with funds that they have withdrawn from the on-chain trading balance, the on-chain smartcontracts holds the funds of each trader to match off the trader’s off-chain trading allowances.
The onchain smart contract is the key element for the decentralized custody and settlement of assets. The smart contract safeguards the traders funds on-chain and has a very limited set of functions.
Receipt of tokens/custody:
A trader is required to transfer the tokens the trader wishes to trade to the smart contract, which will be added into an account identified by the trader’s public key in the smart contract.
Update of trading balance settlement:
When the offchain ledger communicates with the pair of matching orders of smartcontract and both the orders are correctly signed with the private keys of traders, the trading balances for the both traders are updated in the smartcontract.
Release of funds:
Upon request of the trader and if there are no orders are outstanding on the offchain ledger, the tokens held on the smartcontract will be released to the token account of the trader.
Provides more security
It’s technically impossible for the smart contracts to release the assets to any account than the traders account. It is therefore impossible fpr hackers or creditors of DEx to gain control over the traders asset.
An additional safety feature to prevent users assets from being locked in the smart contract, the smart contract has a “release all” function which stops trading indefinitely and releases all assets of all traders accounts.
As the smart contract is the key to the trustless trading model.
Immutable Once deployed, the smart contract is fully onchain and cannot be changed, neither by DEX nor by anyone else
Opensource: The traders have to trust DEX ad DEX has no custody of or access to their assets. The traders simply have to verify that the smartcontract is coded correctly. Therefore the smartcontract will be fully opensource and viewable to the public in the beginning.
Simple: Verify the correctness of code must be easy. Therefore smartcontract is ket as simple as possible.
The trader's tokens are never held by DEx, only by the smart contract.
The tokens can only be traded directly from trader to trader. Order must be correctly signed by the private key to the trader, which will be verified by the smart contract when executing the order. DEX will not collect and store any private key of the traders.
If you have an idea to build such an multi-chain decentralized exchange using EOS and ETH based one?