Build a minimal viable Decentralized Exchange (DEX) on Ethereum and a simple (minimally styled) front-end website, which supports listing of available asset tokens on the marketplace, submission of trading order, matching and execution of orders (i.e. swapping/exchanging/trading assets), and most importantly, in our DEX, users have the ultimate control of his/her own digital assets.
Traditional trading sites and centralized exchanges allow users to purchase and trade different digital assets on their platforms. For example, your first time purchasing an Ethereum token or Bitcoin was probably through a centralized exchange like Coinbase, Binance, Bittrex, Huobi and alike. The central thesis for these exchanges is “key management is hard for average users”, thereby they will manage cryptographic keys for you and provide more friendly interface as a service, and all you, the user, need to do is authenticate yourself to these platforms using traditional login username and passwords. The actual private keys that control and “own” the assets/cryptocurrencies are in the database of these companies. Evidentially, all these platforms claim that they have best security team in the world to secure your keys and therefore your assets from stolen or lost.
Unfortunately, there are no lack of examples of those platforms mishandling or losing users' assets, either by internal corruption, or by external hacks. The most infamous Mt.Gox, a centralized bitcoin exchange, which at one time is handling 70% of all bitcoin trading – the largest in the world, until they got hacked and lost majority of its bitcoin, or rather its customers’ bitcoin and filed bankruptcy.
Fundamentally, the problem is that your cryptocurrencies and digital assets are as secure as your key management. Regardless of all the cryptographic protections and techniques we cover previously in our course, and how unhackable the underlying math is, if you don’t own your keys, then you don’t really own your asset. And asking a centralized exchange to hold your keys for you is a huge trust assumption and liability. With this problem in mind, here comes a promising solution – decentralized exchanges or DEX for short. What distinguish DEX is that, users would need to manage their keys in their digital wallet locally (e.g. MetaMask). Now, no one else would have control over your money – there isn’t a honeypot of millions of bitcoin sitting in certain company’s database waiting for hackers to get a shot, you don’t have to trust any third party that your assets are safe.
In a DEX system, the exchange only manages buy/sell orders from users – exactly like a marketplace where sellers put out the prices range and amount to exchange, and later on some buyers will fulfill that requests. In simpler term, DEX are places where you bring your own key/wallet, to trade your digital assets (e.g. USD token) with other assets (e.g. Gold Token, ETH token) -- and this process is referred as token exchange, or token trading, or token swap. Here are some top tokens/digital assets on Ethereum blockchain.
It is assumed that the following pre-requisits are met:
- Node (v12.16.1) installed
- npm (v6.13.4) installed
- MetaMask Extension and created at least 1 account
- Truffle v5.1.45 (core: 5.1.45) installed
- Infura account setup with at least one set of endpoints
- Ganache (for running local blockchain)
Steps to install:
- Clone this repository
- Run
npm install
- Run
cd webapp && npm install
- Run
trufle migrate
to deploy your contract to your blockchain network
- Create a file called
secret.js
in the root directoryconst secrets = { infuraKey: "xxx" // this should be your infuraKey };
-
Go to
Exchange.js
and update thetrue
/false
value// set true for testnet / false for development if (true) { // testnet var infuraWSS = `wss://ropsten.infura.io/ws/v3/6dbaa0a0eed5453ab2d7e585a6ff39b6`; var exchangeContractAddress = "0x641Ad5725E9C2AFb7f7936c3E45711E5dc08D3b5"; } else { // development - to update this var infuraWSS = `ws://localhost:7545`; var exchangeContractAddress = "0xB4C0ed7Ad8616c450A688218eaFABE5f0a45Cdf8"; }
-
Go to
Index.js
and update thetrue
/false
value// set true for testnet / false for development if (true) { // testnet var infuraHttp = `https://ropsten.infura.io/v3/6dbaa0a0eed5453ab2d7e585a6ff39b6`; var exchangeContractAddress = "0x641Ad5725E9C2AFb7f7936c3E45711E5dc08D3b5"; } else { // development - to update this var infuraHttp = `http://localhost:7545`; var exchangeContractAddress = "0xB4C0ed7Ad8616c450A688218eaFABE5f0a45Cdf8"; }
-
Add your local wallet to your metamask
-
Add the 3 ERC20tokens to your Metamask wallet (Reference: Adding custom tokens to Metamask)
Token Name Contract Address Symbol Name Decimals AdrianCoin (address of Token contract deployed) ADC 8 Bitconnect (address of Token2 contract deployed) BCC 8 One Coin (address of Token3 contract deployed) ONE 8 -
Run
npm start
to get the server running -
Add the tokens to the exchange by calling these APIs in your console
- Note: To replace
XXX_CONTRACT_ADDRESS
andWALLET_ADDRESS
with the respective addresses - ADC:
curl -X POST localhost:3000/exchange/addToken?ecr20TokenAddress=ADRIANCOIN_CONTRACT_ADDRESS&symbolName=ADC&addr=WALLET_ADDRESS
- BCC:
curl -X POST localhost:3000/exchange/addToken?ecr20TokenAddress=BITCONNECT_CONTRACT_ADDRESS&symbolName=BCC&addr=WALLET_ADDRESS
- ONE:
curl -X POST localhost:3000/exchange/addToken?ecr20TokenAddress=ONECOIN_CONTRACT_ADDRESS&symbolName=ONE&addr=WALLET_ADDRESS
- Note: To replace
-
Go to localhost:3000
NOTE: Turns out the code faces some last minute problem because Infura doesn't support sending non-raw transactions.
-
Add tokens to your Metamask wallet (Reference: Adding custom tokens to Metamask)
Token Name Contract Address Symbol Name Decimals AdrianCoin 0xd3F1b9650d4E07e6d2B053efAFFfa60d569CDb8B ADC 8 Bitconnect 0xE5C41188462E63c602Bb831AfBA62C11c794De36 BCC 8 One Coin 0xc21192efb4dfB112Eb69a036DbFE4BC263CA52Cd ONE 8 -
Reach out to me with your address to get some ERC20 tokens for testing
-
Run
npm start
to get the server and go to localhost:3000
Adrian Goh Jun Wei - GitHub