create account

How to create a crypto exchange on the Ethereum blockchain using Node.js/React.js/Solidity - Part 3 by gotgame

View this thread on: hive.blogpeakd.comecency.com
· @gotgame ·
$0.49
How to create a crypto exchange on the Ethereum blockchain using Node.js/React.js/Solidity - Part 3
This is the third post in a series of tutorials on how to create a crypto exchange on the Ethereum blockchain. 

I'll leave links to previous posts at the end of this post.

This series was inspired by a similar series by  [Dapp University](https://www.youtube.com/channel/UCY0xL8V6NzzFcwzHCgB8orQ).

The crypto exchange will contain a  `buy/sell`  feature and will have Ethereum and ERC20 token pairs.

In the last post we started creating the smart contracts for our exchange, first we created the exchange smart contract and deployed it to the blockchain, though we have not done much in that respect. 

Also we created the smart contract for the token we will be trading against Ethereum and also deployed it to the blockchain.

In this post we are going to continue with the smart contracts we created, we'll fine tune them till we get them to our desired states.


#### More on the token smart contract

After creating the token smart contract and deploying it to the blockchain, we need to run the command `truffle console` in our project terminal in  order to retrieve certain information about the token we just created.

Once the truffle development prompt comes on in the terminal, type following code to initialize the token as a variable.

```
 token = await Token.deployed()
```

In order to view the full scope of the token in the console, type in the following code in your terminal and press `Enter`

```
token
```

You should get something identical to this image in the console.

![enter image description here](https://i.ibb.co/F3QLxHc/new.png)

Naturally, when creating the token smart contract all available tokens will be assigned to the account of the user who created and deployed the smart contract to the blockchain.

We don't want that, so we are going to transfer the token from the previous user to the `Exchange` account which is where the tokens will be bought from.

In the file `migrations/2_deploy_contracts.js`, replace the contents of the file with the following

```
const  Token = artifacts.require("Token");

const  Exchange = artifacts.require("Exchange");

  

module.exports = async function(deployer) {
	await deployer.deploy(Token);
	const  token = Token.deployed()

	await deployer.deploy(Exchange);
	const  exchange = Exchange.deployed()

	// transfer tokens to exchange account  
	await  token.transfer(exchange.address, '1000000000000000000000000')
};
```

Save the file and in the terminal, run the command

```
truffle migrate --reset
```

Once the migration is complete you should see a result like this in your console

![enter image description here](https://i.ibb.co/mN3558V/new.png)

We can confirm that our token has been truly transferred, in the terminal run the command 

```
truffle console
```

In the resulting prompt type in the following code 

```
exchange = await Exchange.deployed()
```

then, type in

```
exchange.address
```

You should get a result that displays the address of the Exchange smart contract in the terminal

Next, type in

```
token = await Token.deployed()
```
In order to get the token balance of the exchange address, first type in the following code 

```
balance = await token.balanceOf(exchange.address)
```
You can now check the balance by running the following code

```
balance.toString()
``` 

In your terminal you should see something like this

![enter image description here](https://i.ibb.co/477yPmm/new.png)

We have finally been able to transfer the tokens to the exchange account so that people can buy the tokens from the exchange when they create buy orders.

Basically, our coins are ready to be purchased.

In the next post we are going to be diving more into the inner working s of our exchange.

#### Links to previous posts in this series

-   [Part 1](https://hive.blog/hive-175254/@gotgame/how-to-create-a-crypto-exchange-on-the-ethereum-blockchain-using-node-js-react-js-solidity-part-1)
-   [Part 2](https://hive.blog/hive-175254/@gotgame/how-to-create-a-crypto-exchange-on-the-ethereum-blockchain-using-node-js-react-js-solidity-part-2)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 13 others
properties (23)
authorgotgame
permlinkhow-to-create-a-crypto-exchange-on-the-ethereum-blockchain-using-node-js-react-js-solidity-part-3
categoryhive-175254
json_metadata{"tags":["neoxian","palnet","qurator","ocd","gems","gitplait","dblog"],"image":["https://i.ibb.co/F3QLxHc/new.png","https://i.ibb.co/mN3558V/new.png","https://i.ibb.co/477yPmm/new.png"],"links":["https://www.youtube.com/channel/UCY0xL8V6NzzFcwzHCgB8orQ","https://hive.blog/hive-175254/@gotgame/how-to-create-a-crypto-exchange-on-the-ethereum-blockchain-using-node-js-react-js-solidity-part-1","https://hive.blog/hive-175254/@gotgame/how-to-create-a-crypto-exchange-on-the-ethereum-blockchain-using-node-js-react-js-solidity-part-2"],"app":"hiveblog/0.1","format":"markdown"}
created2020-08-06 13:36:27
last_update2020-08-06 13:36:27
depth0
children2
last_payout2020-08-13 13:36:27
cashout_time1969-12-31 23:59:59
total_payout_value0.214 HBD
curator_payout_value0.274 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,052
author_reputation23,969,707,386,372
root_title"How to create a crypto exchange on the Ethereum blockchain using Node.js/React.js/Solidity - Part 3"
beneficiaries
0.
accountph-fund
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,925,847
net_rshares1,973,422,361,167
author_curate_reward""
vote details (77)
@hivebootcamphub ·
<div class="text-justify">
This is a very useful post for developers on the platform. We are sure it would go a long way to help them learn. Thanks for sharing. 


<center>![hive dividers03.png](https://files.peakd.com/file/peakd-hive/hivebootcamphub/drNY6o85-hive20dividers-03.png)</center>

[**The HiveBootcampHub Community**]( https://peakd.com/c/hive-109221) just selected and supported your article!!!

**To get a stronger support from us, you can post your articles in [**our Community**]( https://peakd.com/c/hive-109221)** 

Feel free to [**Subscribe to our Community**](https://peakd.com/c/hive-109221/created) and [**Join our Discord Server**](https://discord.gg/t5YzPNg) as we’d be happy to have you with us! Cheers!


<sub><center>![WhatsApp Image 2020-07-19 at 23.30.37.jpeg](https://images.hive.blog/DQmSeMVtJrNePq2EtcCrnihHMQkMWRpj492h5bVHScH3qjw/WhatsApp%20Image%202020-07-19%20at%2023.30.37.jpeg)</sub></center>

</div>
properties (22)
authorhivebootcamphub
permlinkre-gotgame-qepimo
categoryhive-175254
json_metadata{"tags":["hive-175254"],"app":"peakd/2020.08.1"}
created2020-08-07 18:42:27
last_update2020-08-07 18:42:27
depth1
children0
last_payout2020-08-14 18:42:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length936
author_reputation9,144,995,094,149
root_title"How to create a crypto exchange on the Ethereum blockchain using Node.js/React.js/Solidity - Part 3"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id98,948,800
net_rshares0
@hivebuzz ·
Congratulations @gotgame! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@gotgame/upvotes.png?202008121902"></td><td>You distributed more than 61000 upvotes. Your next target is to reach 62000 upvotes.</td></tr>
</table>

<sub>_You can view [your badges on your board](https://hivebuzz.me/@gotgame) And compare to others on the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>

properties (22)
authorhivebuzz
permlinkhivebuzz-notify-gotgame-20200812t192105000z
categoryhive-175254
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2020-08-12 19:21:06
last_update2020-08-12 19:21:06
depth1
children0
last_payout2020-08-19 19:21:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length619
author_reputation370,793,562,899,017
root_title"How to create a crypto exchange on the Ethereum blockchain using Node.js/React.js/Solidity - Part 3"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,036,603
net_rshares0