#### Repository https://github.com/igormuba/DEX/tree/master/Class6 #### What Will I Learn? - Iterate through deployed data structures - Nested loops - Fetching and storing data into arrays using loops #### Requirements - Internet connection. - Code editor. - Browser. #### Difficulty - Advanced. #### Tutorial Contents We have already built the data structures and manipulated data structures on Solidity on this series, now we will visualize and iterate through the data structures we have been building and modifying in the previous tutorials. The function for the exchange implemented here will be used to retrieve data from the buy book. It is a `view` function, which means it does not write data to the blockchain, it just reads. Usually, `view` functions are "cheap", but that is not the case when we are working with complex data structures. Also, the cost of the gas can be high and is not fixed, differently from most functions, because we can't guess the size of the order book. The bigger the book, more nodes it has, and more expensive the transaction cost will be. # The function ``` function getBuyOrders(address _token) public view returns(uint[] memory, uint[] memory){ Token storage loadedToken = tokenList[_token]; } ``` The only argument this function has to receive is the token address. Our exchange works with token addresses, not names. It returns 2 arrays, the first is the array of prices, in order, from the lowest to the highest. The second array is the array of volume, again, following the order. For example: First array: `[1, 4, 5, 10, 13, 15]` Second array: `[10, 4, 24, 3, 4, 9]` That means that the sum of buy orders at the value of 1 each is 10 tokens. The volume of the buy orders at the price of 4 each is 4 tokens. For the price 5 is 24... And there might be many buy orders, for multiple buyers, at the same price, our function will take that into consideration! # The variables we will return This are the variables we will return. We are just declaring them here, we will fill them with values in the future: ``` uint[] memory ordersPrices = new uint[](loadedToken.amountBuyPrices); uint[] memory ordersvolumes = new uint[](loadedToken.amountBuyPrices); ``` We are saying `new uint[](loadedToken.amountBuyPrices)` on both, so that the returned array has the size of the buybook. # The loop to fill the prices Our function will have two while loops, an outer loop, that will jump from one price on the price linked list to another, and an inner loop, that will sum the volumes of the orders on the queue at that price. Right below the arrays, we have declared with the intention to return them, add the code: ``` uint buyPrice = loadedToken.minBuyPrice; uint counter = 0; if (loadedToken.maxBuyPrice>0){ while(buyPrice<loadedToken.maxBuyPrice){ ordersPrices[counter]=buyPrice; } } ``` `buyPrice` is the "current" buy price on this round of the loop. Remember, we are going from the cheapest price to the most expensive. `uint counter = 0;` is the counter to keep track of which "run" of the loop we are at so that the fetched price gets stored in the right position on the array. `if (loadedToken.maxBuyPrice>0)` ensures we won't try to loop on an empty book `while(buyPrice<loadedToken.maxBuyPrice)` will stop running after we process the highest price in the buy book, on the last run. And finally, `ordersPrices[counter]=buyPrice;` stores the price of the current iteration of the loop in the position of the number of the current iteration of the loop. # The loop to fill the volume This is the inner loop, that will check all the orders on the queue at the given price point and sum them. ``` uint priceVolume=0; uint offerPointer=loadedToken.buyBook[buyPrice].offerPointer; while(offerPointer<=loadedToken.buyBook[buyPrice].offerLength){ } ``` Again, we are using variables to store the sum and the "pointer" to keep track at which position of the queue we are. `priceVolume` stores the volume of buy orders at that given price. `offerPointer` goes up one at a time to move the pointer. We also are assigning the `offerPointer` to start at `loadedToken.buyBook[buyPrice].offerPointer`, which is the beginning of the queue. Inside the loop we add the current element of the queue to the sum of volume, and move the pointer up: ``` priceVolume+=loadedToken.buyBook[buyPrice].offers[offerPointer].amount; offerPointer++; ``` # Finishing the loops After closing the inner loop we do: ``` if (buyPrice==loadedToken.buyBook[buyPrice].higherPrice){ break; }else{ buyPrice=loadedToken.buyBook[buyPrice].higherPrice; } counter++; ``` If the "higher price" of the current price equals the current price, that means we have actually reached the end of the linked list, so. we `break` out of it. If we haven't reached the end yet, we jump to the next node, by storing the current nodes `higherPrice` as the `buyPrice`. And, regardless, we move the counter up. # Returning We have already, in the inner and outer loops, filled the arrays we want to return, so, this is step is just closing the outer loop and the if statement, and returning the results we have got: ``` return(ordersPrices, ordersvolumes); ``` # How the DEX looks so far Let us take a review on the logic we have implemented so far, and see how everything connects so far! When a user requests to make a deposit in tokens o the exchange, he is actually calling a function on the exchange that will use the API to call the transaction function on the outside token contract.  With the balance already on the exchange, the user can place an order. The order book is a linked list of prices. Inside of each price, there is a queue of orders, waiting to be filled (first come first serve). <img src="https://steemitimages.com/640x0/https://files.steempeak.com/file/steempeak/igormuba/gRJmkIWO-Captura20de20Tela202019-01-2520aCC80s2021.34.11.png"> If the orders not filled (we haven't implemented the filling functionality yet) the order will be stored in the book, on the top, bottom or somewhere in the middle. <img src="https://steemitimages.com/640x0/https://files.steempeak.com/file/steempeak/igormuba/soaYV6io-Captura20de20Tela202019-01-2720aCC80s2019.24.14.png"> But only if it is the first of its price. <img src="https://steemitimages.com/640x0/https://files.steempeak.com/file/steempeak/igormuba/FUmJU6qE-Captura20de20Tela202019-01-2820aCC80s2019.06.49.png"> If it is not, it will simply be added to the queue of its price, represented. here by the nodes "1st offer", "2nd offer" and "3rd offer"  The path implemented in this tutorial was the reverse, to fetch data on already created and modified data structures. #### Curriculum Latest tutorial of the series: - [(Part 5) Decentralized Exchange - Limit, Security Order Requirements And Buy/Sell Peculiarities(PT 5)](https://steemit.com/utopian-io/@igormuba/part-5-decentralized-exchange-limit-security-order-requirements-and-buy-sell-peculiarities-pt-5) First tutorial of the series: - [(Part 1) Decentralized Exchange - Smart Contract API, Order Book And Orders (PT 1)](https://steemit.com/utopian-io/@igormuba/part-1-decentralized-exchange-smart-contract-api-order-book-and-orders-pt-1) # Beneficiaries This post has as beneficiaries - [@utopian.pay](https://steemit.com/@utopian.pay) with 5% - [@steempeak](https://steemit.com/@steempeak) with 1% using the SteemPeak beneficiary tool 
author | igormuba | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
permlink | part-6-decentralized-exchange-fetching-data-from-book-and-filling-multiple-arrays-using-nested-loops-pt-6 | ||||||||||||
category | utopian-io | ||||||||||||
json_metadata | {"community":"steempeak","app":"steempeak/1.7.0","format":"markdown","tags":["utopian-io","tutorials","ethereum","solidity","remix"],"users":["igormuba","utopian.pay","steempeak"],"links":["https://github.com/igormuba/DEX/tree/master/Class6","https://steemit.com/utopian-io/@igormuba/part-5-decentralized-exchange-limit-security-order-requirements-and-buy-sell-peculiarities-pt-5","https://steemit.com/utopian-io/@igormuba/part-1-decentralized-exchange-smart-contract-api-order-book-and-orders-pt-1","https://steemit.com/@utopian.pay","https://steemit.com/@steempeak"],"image":["https://files.steempeak.com/file/steempeak/igormuba/zLRLnDWW-Captura20de20Tela202019-01-3020aCC80s2018.29.01.png","https://steemitimages.com/640x0/https://files.steempeak.com/file/steempeak/igormuba/gRJmkIWO-Captura20de20Tela202019-01-2520aCC80s2021.34.11.png","https://steemitimages.com/640x0/https://files.steempeak.com/file/steempeak/igormuba/soaYV6io-Captura20de20Tela202019-01-2720aCC80s2019.24.14.png","https://steemitimages.com/640x0/https://files.steempeak.com/file/steempeak/igormuba/FUmJU6qE-Captura20de20Tela202019-01-2820aCC80s2019.06.49.png","https://files.steempeak.com/file/steempeak/igormuba/GU3i7dI3-Captura20de20Tela202019-01-3020aCC80s2018.34.38.png","https://files.steempeak.com/file/steempeak/igormuba/sKoHA6Tn-Captura20de20Tela202019-01-2220aCC80s2016.07.11.png"]} | ||||||||||||
created | 2019-01-30 20:44:36 | ||||||||||||
last_update | 2019-01-30 20:44:36 | ||||||||||||
depth | 0 | ||||||||||||
children | 4 | ||||||||||||
last_payout | 2019-02-06 20:44:36 | ||||||||||||
cashout_time | 1969-12-31 23:59:59 | ||||||||||||
total_payout_value | 20.198 HBD | ||||||||||||
curator_payout_value | 6.865 HBD | ||||||||||||
pending_payout_value | 0.000 HBD | ||||||||||||
promoted | 0.000 HBD | ||||||||||||
body_length | 7,937 | ||||||||||||
author_reputation | 129,528,776,762,516 | ||||||||||||
root_title | "(Part 6) Decentralized Exchange - Fetching Data From Book, And Filling Multiple Arrays Using Nested Loops (PT 6)" | ||||||||||||
beneficiaries |
| ||||||||||||
max_accepted_payout | 1,000,000.000 HBD | ||||||||||||
percent_hbd | 10,000 | ||||||||||||
post_id | 79,180,419 | ||||||||||||
net_rshares | 58,753,594,185,450 | ||||||||||||
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
tombstone | 0 | 3,798,247,501,509 | 16.12% | ||
nascimentoab | 0 | 5,238,177,885 | 100% | ||
snwolak | 0 | 3,604,767,353 | 50% | ||
rufans | 0 | 11,309,145,129 | 100% | ||
elena-singer | 0 | 35,226,656,391 | 100% | ||
eforucom | 0 | 39,239,392,604 | 2% | ||
bukiland | 0 | 4,605,597,120 | 3.14% | ||
miniature-tiger | 0 | 139,259,011,253 | 50% | ||
jga | 0 | 2,428,309,459 | 20.15% | ||
codingdefined | 0 | 23,332,148,736 | 20% | ||
veritasvav | 0 | 96,940,432,324 | 100% | ||
bachuslib | 0 | 20,118,010,641 | 100% | ||
leir | 0 | 2,019,949,372 | 50% | ||
robertoueti | 0 | 9,556,085,006 | 25% | ||
riscoin | 0 | 1,513,401,921 | 35% | ||
mcfarhat | 0 | 20,894,776,616 | 12.57% | ||
flaviusbusck | 0 | 2,119,410,637 | 100% | ||
gathanxz | 0 | 0 | 100% | ||
coyotelation | 0 | 1,293,990,018 | 100% | ||
utopian-io | 0 | 52,987,479,837,692 | 40.3% | ||
naturald | 0 | 5,944,159,061 | 100% | ||
jaff8 | 0 | 51,211,203,585 | 31.44% | ||
ayasha | 0 | 29,065,304,116 | 50% | ||
newsrx | 0 | 84,862,015 | 6.93% | ||
steeminer4up | 0 | 921,082,025 | 100% | ||
shakailove | 0 | 210,356,775 | 2.75% | ||
aiyanna | 0 | 311,737,867 | 2.5% | ||
amosbastian | 0 | 79,400,839,560 | 31.44% | ||
daeshawn | 0 | 364,534,539 | 2.25% | ||
paulomurilo | 0 | 501,492,471 | 100% | ||
osobiggie | 0 | 140,450,211 | 1.2% | ||
peagha | 0 | 503,423,499 | 100% | ||
portugalcoin | 0 | 9,636,540,901 | 15% | ||
tmarisco | 0 | 6,374,413,400 | 100% | ||
casberp | 0 | 289,649,285,665 | 100% | ||
kennybrown | 0 | 79,853,106 | 1% | ||
ignis2 | 0 | 408,044,333 | 100% | ||
amayahaley21 | 0 | 56,911,473 | 1% | ||
alinequeiroz | 0 | 530,318,742 | 100% | ||
barbaralisarti | 0 | 667,091,435 | 100% | ||
sudefteri | 0 | 5,569,421,247 | 100% | ||
fireguardian | 0 | 2,492,288,831 | 100% | ||
rodrigozottis | 0 | 317,438,494 | 100% | ||
aotearoa | 0 | 5,626,028,041 | 62.44% | ||
camillagomes | 0 | 139,964,967 | 20% | ||
fego | 0 | 14,669,770,467 | 31.44% | ||
massola | 0 | 1,105,030,021 | 100% | ||
paulosabaini | 0 | 1,063,790,060 | 100% | ||
erikklok | 0 | 17,722,052,648 | 100% | ||
simplymike | 0 | 63,634,755,463 | 30% | ||
alisonqueiroz | 0 | 502,687,557 | 100% | ||
hakancelik | 0 | 1,963,837,254 | 60% | ||
ronaldoavelino | 0 | 7,434,468,373 | 5% | ||
sweetpee | 0 | 165,725,192 | 1% | ||
mracalf | 0 | 749,404,823 | 100% | ||
danilojjunior | 0 | 625,624,901 | 100% | ||
antenordamazio | 0 | 241,194,381 | 100% | ||
zpedro | 0 | 390,692,293 | 10% | ||
alyaugusto | 0 | 138,772,523 | 20% | ||
hranhuk | 0 | 358,220,174 | 40% | ||
bitcoincwb | 0 | 1,477,231,222 | 50% | ||
handrehermann | 0 | 727,692,035 | 100% | ||
mynotsofitlife | 0 | 1,034,503,199 | 10% | ||
eraizel | 0 | 2,894,753,509 | 100% | ||
rogergalvao | 0 | 193,100,165 | 100% | ||
thefunnyshit | 0 | 424,105,669 | 100% | ||
mestrebtc | 0 | 502,655,149 | 100% | ||
theactuary | 0 | 576,857,007 | 100% | ||
daientei | 0 | 504,079,668 | 100% | ||
siraizel | 0 | 415,594,799 | 100% | ||
biogirl | 0 | 502,022,105 | 100% | ||
ptgram-power | 0 | 1,369,026,998 | 100% | ||
mightypanda | 0 | 46,205,917,902 | 30% | ||
hugosantos | 0 | 502,609,876 | 100% | ||
angelusc | 0 | 70,968,322 | 20% | ||
joaoprobst | 0 | 1,324,100,793 | 100% | ||
juniorfrederico | 0 | 582,032,855 | 100% | ||
ulockblock | 0 | 33,686,844,247 | 12.67% | ||
osave | 0 | 503,894,879 | 100% | ||
menteincrivel | 0 | 501,678,235 | 100% | ||
bielraizel | 0 | 502,023,179 | 100% | ||
blockgatorsarmy | 0 | 76,897,106 | 1.2% | ||
havillafrederico | 0 | 500,924,456 | 100% | ||
caiiiotkn | 0 | 250,867,358 | 50% | ||
lucky222 | 0 | 23,696,769,340 | 100% | ||
pedrocanella | 0 | 12,830,887,967 | 50% | ||
viniciusan | 0 | 495,189,550 | 100% | ||
warriorpower | 0 | 500,921,462 | 100% | ||
lyonms | 0 | 500,850,077 | 100% | ||
bullinachinashop | 0 | 3,586,896,194 | 100% | ||
ogaitbtc | 0 | 502,650,633 | 100% | ||
merlin7 | 0 | 11,959,464,626 | 0.3% | ||
schimiluk | 0 | 494,036,212 | 100% | ||
amandavale | 0 | 502,653,910 | 100% | ||
urubatan | 0 | 502,595,212 | 100% | ||
brunoss | 0 | 502,592,216 | 100% | ||
steem-ua | 0 | 744,072,466,559 | 6.93% | ||
felipefortes | 0 | 521,647,190 | 100% | ||
marianaemilia | 0 | 4,181,909,482 | 99% | ||
brwhale | 0 | 441,296,430 | 100% | ||
kaczynski | 0 | 195,104,186 | 100% | ||
thaynara | 0 | 511,263,426 | 100% | ||
joaohazim | 0 | 3,046,735,185 | 100% | ||
caluk | 0 | 490,829,038 | 100% | ||
bluesniper | 0 | 39,033,027,599 | 5.03% | ||
ascorphat | 0 | 1,861,363,843 | 2.5% | ||
blocosdoigor | 0 | 1,352,456,848 | 100% | ||
mbori | 0 | 552,054,020 | 100% | ||
biologytime | 0 | 528,469,357 | 100% |
Thank you for your contribution. - Again I love the work you do on the visuals in terms of decision charts! - Did you run any tests on those loops with large number of orders? how well do they perform? - The tutorial was a bit too short on content, but still I like the progress being done there. Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category. To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/8/3-1-1-1-1-2-1-3-). ---- Need help? Chat with us on [Discord](https://discord.gg/uTyJkNm). [[utopian-moderator]](https://join.utopian.io/)
author | mcfarhat |
---|---|
permlink | re-igormuba-part-6-decentralized-exchange-fetching-data-from-book-and-filling-multiple-arrays-using-nested-loops-pt-6-20190130t234111874z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/8/3-1-1-1-1-2-1-3-","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"} |
created | 2019-01-30 23:41:24 |
last_update | 2019-01-30 23:41:24 |
depth | 1 |
children | 1 |
last_payout | 2019-02-06 23:41:24 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 4.233 HBD |
curator_payout_value | 1.352 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 750 |
author_reputation | 150,651,671,367,256 |
root_title | "(Part 6) Decentralized Exchange - Fetching Data From Book, And Filling Multiple Arrays Using Nested Loops (PT 6)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 79,186,007 |
net_rshares | 11,585,239,795,375 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
yuxi | 0 | 24,132,727,784 | 100% | ||
codingdefined | 0 | 23,126,264,418 | 20% | ||
espoem | 0 | 26,708,005,062 | 15% | ||
utopian-io | 0 | 11,324,320,389,466 | 8.19% | ||
zapncrap | 0 | 1,328,209,939 | 2% | ||
amosbastian | 0 | 53,182,919,005 | 21.74% | ||
curx | 0 | 1,475,918,854 | 2% | ||
organicgardener | 0 | 4,805,073,910 | 25% | ||
mightypanda | 0 | 99,784,436,135 | 65% | ||
ulockblock | 0 | 14,399,273,234 | 5.34% | ||
fastandcurious | 0 | 2,225,951,545 | 60% | ||
linknotfound | 0 | 1,140,633,989 | 100% | ||
ascorphat | 0 | 1,975,862,320 | 2.5% | ||
monster-inc | 0 | 2,920,759,878 | 100% | ||
yff | 0 | 3,713,369,836 | 100% |
Thank you for your review, @mcfarhat! Keep up the good work!
author | utopian-io |
---|---|
permlink | re-re-igormuba-part-6-decentralized-exchange-fetching-data-from-book-and-filling-multiple-arrays-using-nested-loops-pt-6-20190130t234111874z-20190202t011937z |
category | utopian-io |
json_metadata | "{"app": "beem/0.20.17"}" |
created | 2019-02-02 01:19:39 |
last_update | 2019-02-02 01:19:39 |
depth | 2 |
children | 0 |
last_payout | 2019-02-09 01:19:39 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 60 |
author_reputation | 152,955,367,999,756 |
root_title | "(Part 6) Decentralized Exchange - Fetching Data From Book, And Filling Multiple Arrays Using Nested Loops (PT 6)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 79,279,423 |
net_rshares | 0 |
#### Hi @igormuba! Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation! Your post is eligible for our upvote, thanks to our collaboration with @utopian-io! **Feel free to join our [@steem-ua Discord server](https://discord.gg/KpBNYGz)**
author | steem-ua |
---|---|
permlink | re-part-6-decentralized-exchange-fetching-data-from-book-and-filling-multiple-arrays-using-nested-loops-pt-6-20190131t002140z |
category | utopian-io |
json_metadata | "{"app": "beem/0.20.17"}" |
created | 2019-01-31 00:21:42 |
last_update | 2019-01-31 00:21:42 |
depth | 1 |
children | 0 |
last_payout | 2019-02-07 00:21:42 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 287 |
author_reputation | 23,214,230,978,060 |
root_title | "(Part 6) Decentralized Exchange - Fetching Data From Book, And Filling Multiple Arrays Using Nested Loops (PT 6)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 79,187,219 |
net_rshares | 0 |
Hey, @igormuba! **Thanks for contributing on Utopian**. We’re already looking forward to your next contribution! **Get higher incentives and support Utopian.io!** Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via [SteemPlus](https://chrome.google.com/webstore/detail/steemplus/mjbkjgcplmaneajhcbegoffkedeankaj?hl=en) or [Steeditor](https://steeditor.app)). **Want to chat? Join us on Discord https://discord.gg/h52nFrV.** <a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
author | utopian-io |
---|---|
permlink | re-part-6-decentralized-exchange-fetching-data-from-book-and-filling-multiple-arrays-using-nested-loops-pt-6-20190131t081027z |
category | utopian-io |
json_metadata | "{"app": "beem/0.20.17"}" |
created | 2019-01-31 08:10:30 |
last_update | 2019-01-31 08:10:30 |
depth | 1 |
children | 0 |
last_payout | 2019-02-07 08:10:30 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 590 |
author_reputation | 152,955,367,999,756 |
root_title | "(Part 6) Decentralized Exchange - Fetching Data From Book, And Filling Multiple Arrays Using Nested Loops (PT 6)" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 79,200,689 |
net_rshares | 0 |