Am I using a while loop correctly in this case?

function createMarketOrder(bytes32 ticker, Side side, uint amount) public{

        uint orderBookSide;

        if(side == Side.BUY){

            orderBookSide = 1;

        }

        else{

            require(balances[msg.sender][ticker] >= amount, "Insufficient balance");

            orderBookSide = 0;

        }

        Order[] storage orders = orderBook[ticker][orderBookSide];

        uint totalFilled;

        for (uint256 i = 0; i < orders.length && totalFilled < amount; i++){

            if(orderBookSide == 1 && orders[i].amount < amount){

                totalFilled = orders[i].amount + totalFilled;

                orders[i].filled = orders[i].amount;

                amount = amount - orders[i].amount;

                _transfer(ticker, msg.sender, orders[i].trader, orders[i].amount, orders[i].price);

            }

            else if(orderBookSide == 1 && orders[i].amount >= amount){

                totalFilled = amount;

                orders[i].filled = amount;

                _transfer(ticker, msg.sender, orders[i].trader, orders[i].filled, orders[i].price);

            }

       

            if(orderBookSide == 0 && orders[i].amount < amount){

                totalFilled = orders[i].amount + totalFilled;

                orders[i].filled = orders[i].amount;

                amount = amount - orders[i].amount;

                _transfer(ticker, orders[i].trader, msg.sender, orders[i].amount, orders[i].price);

            }

            else if(orderBookSide == 0 && orders[i].amount >= amount){

                totalFilled = amount;

                orders[i].filled = amount;

                _transfer(ticker, orders[i].trader, msg.sender, orders[i].filled, orders[i].price);

            }

            if(totalFilled == amount){

                break;

            }

        }

        if(totalFilled != 0){

            _clearOrders(ticker, orderBookSide);

        }

    }

    function _transfer(bytes32 ticker, address buyer, address seller, uint amount, uint price) internal{

        require(balances[buyer]["ETH"] > amount.mul(price));

        balances[buyer][ticker] = amount + balances[buyer][ticker];

        balances[seller][ticker] = balances[seller][ticker] - amount;

        balances[buyer]["ETH"] = balances[buyer]["ETH"] - amount.mul(price);

        balances[seller]["ETH"] = balances[seller]["ETH"] + amount.mul(price);

    }

    function _clearOrders(bytes32 ticker, uint orderBookSide) internal{

        while(orderBook[ticker][orderBookSide][0].filled == orderBook[ticker][orderBookSide][0].amount){         

            if(orderBook[ticker][orderBookSide].length > 1){

                for(uint256 i = 0; i < orderBook[ticker][orderBookSide].length - 1; ++i){                   

                        orderBook[ticker][orderBookSide][i] = orderBook[ticker][orderBookSide][i + 1];

                }

            }    

            orderBook[ticker][orderBookSide].pop();

        }

    }

I keep failing my test due to my clear order function not working properly as my failed test says they sell book isn’t cleared correctly. So I was wondering if in this instance am I using the while loop correctly because it seems to me that it should work. Any bits of help or ideas would be appreciated! Thanks!

function createMarketOrder(bytes32 ticker, Side side, uint amount) public{

        uint orderBookSide;

        if(side == Side.BUY){
            orderBookSide = 1;
        }
        else{
            require(balances[msg.sender][ticker] >= amount, "Insufficient balance");
            orderBookSide = 0;
        }

        Order[] storage orders = orderBook[ticker][orderBookSide];

        uint totalFilled;

        for (uint256 i = 0; i < orders.length && totalFilled < amount; i++){
            if(orderBookSide == 1 && orders[i].amount < amount){
                totalFilled = orders[i].amount + totalFilled;
                orders[i].filled = orders[i].amount;
                amount = amount - orders[i].amount;
                _transfer(ticker, msg.sender, orders[i].trader, orders[i].amount, orders[i].price);
            }
            else if(orderBookSide == 1 && orders[i].amount >= amount){
                totalFilled = amount;
                orders[i].filled = amount;
                _transfer(ticker, msg.sender, orders[i].trader, orders[i].filled, orders[i].price);
            }

            if(orderBookSide == 0 && orders[i].amount < amount){
                totalFilled = orders[i].amount + totalFilled;
                orders[i].filled = orders[i].amount;
                amount = amount - orders[i].amount;
                _transfer(ticker, orders[i].trader, msg.sender, orders[i].amount, orders[i].price);
            }
            else if(orderBookSide == 0 && orders[i].amount >= amount){
                totalFilled = amount;
                orders[i].filled = amount;
                _transfer(ticker, orders[i].trader, msg.sender, orders[i].filled, orders[i].price);
            }

            if(totalFilled == amount){
                break;
            }
        }

        if(totalFilled != 0){
            _clearOrders(ticker, orderBookSide);
        }
    }

    function _transfer(bytes32 ticker, address buyer, address seller, uint amount, uint price) internal{
        require(balances[buyer]["ETH"] > amount.mul(price));

        balances[buyer][ticker] = amount + balances[buyer][ticker];
        balances[seller][ticker] = balances[seller][ticker] - amount;

        balances[buyer]["ETH"] = balances[buyer]["ETH"] - amount.mul(price);
        balances[seller]["ETH"] = balances[seller]["ETH"] + amount.mul(price);

    }

    function _clearOrders(bytes32 ticker, uint orderBookSide) internal{

        while(orderBook[ticker][orderBookSide][0].filled == orderBook[ticker][orderBookSide][0].amount){         
            if(orderBook[ticker][orderBookSide].length > 1){
                for(uint256 i = 0; i < orderBook[ticker][orderBookSide].length - 1; ++i){                   
                        orderBook[ticker][orderBookSide][i] = orderBook[ticker][orderBookSide][i + 1];
                }
            }    
            orderBook[ticker][orderBookSide].pop();
        }
    }

I formatted it a little better here. Dont know why when I copy paste it comes out all spaced out

I’m using Filip’s test he posted on github for reference. Test number 3: Market orders should be filled until the order book is empty or the market order is 100% filled. Tells me the Sell order book should have only one order left.

Also, I will add Safemath to my function just was typing it out my ideas.

Hey @DJaySplash

Push your full project to github and share the link so that we can take a look

2 Likes

I’m not too familiar with github but I made an account and now trying to figure out how to properly put it in as a project so that you can see. How might I be able to get my whole project on github? I created a repository to connect to my smart contract folder except it wont allow me to. I am not sure if that is correct way to go about things so any help would be appreciated.

https://github.com/DjaySplash/DEX-Solidity-Programming-201

I think this is what you are looking for

Were you able to see my project on git hub?