Assignment - Limit Order Test

Thank you for the answer but honestly no DeFI project stores the buy/sell orders in the blockchain (or am I wrong?)
The velocity that orders are added/filled is key. The DEX would need to work in miliseconds and in parallel and being able to scale if demand increases.

Cannot imagine to wait until the miners are done and blocks confirmed before placing an order :roll_eyes:

Hey @danielmain

Uniswap does not really have an orderbook and works with liquidity providers so that you basically trade against the contract directly.
Every time you interact with Uniswap you use the smart contract not a centralised database.

Cheers,
Dani

2 Likes

Do I get the chance to have a short screen sharing conference If I get stuck without knowing what am I doing wrong?

Here is the problem

CleanShot 2021-04-15 at 15.28.04@2x

The whole source code is here: https://github.com/danielmain/dex-example

1- Clone it
2- yarn install
3- truffle migrate --reset
4- yarn run test

Thanks in advance!

SOLUTION:

Only the account[0] (owner) can deposit tokens. Adding the { from: accounts[0] } solved the problem

    it("should have enough balance before selling the token", async () => {
        await dex.addToken(Tokens.LINK, link.address, { from: accounts[0] });
        await truffleAssert.reverts(
            dex.createLimitOrder(Side.SELL, Tokens.LINK, 100, 100)
        );
        await link.approve(dex.address, 100, { from: accounts[0] });
        await dex.deposit(10, Tokens.LINK, { from: accounts[0] });
        truffleAssert.passes(
            await dex.createLimitOrder(Side.SELL, Tokens.LINK, 2, 51, { from: accounts[0] })
        );

    });
1 Like

I’m posting my unfinished assignment here for Market Order Tests since it doesn’t have its own forum. I’m pretty sure there are lots of mistakes here, If someone can just point them out, that would be great!

I would like to learn how to think and write a structured code that makes sense by knowing what mistakes I’m doing. Here’s the faulty code :sweat_smile:

const Dex = artifacts.require("Dex")
const Xyz = artifacts.require("Xyz")
const truffleAssert = require('truffle-assertions')

contract ("Dex", accounts =>{
//When creating a SELL market order, the seller needs to have enough tokens for the trade
    it("Seller needs enough tokens to create a SELL market order", async () =>{
        let dex = await Dex.deployed()
        let xyz = await Xyz.deployed()
        await dex.addToken(web3.utils.fromUtf8("XYZ"), xyz.address, {from: accounts[0]})
        
        await truffleAssert.reverts(1, dex.createMarketOrder(web3.utils.fromUtf8("XYZ")), 10);

        await xyz.approve(dex.address, 50);
        await dex.deposit(10, web3.utils.fromUtf8("XYZ"));

        await truffleAssert.passes(1, dex.createMarketOrder(web3.utils.fromUtf8("XYZ")), 10);
    })
    
//When creating a BUY market order, the buyer needs to have enough ETH for the trade
//Note: Market orders take the best price possibles
    it("Buyer musy have enough ETH balance to create a BUY market order", async () =>{
        let dex = Dex.deployed()
        let marketPrice = 100        

        await truffleAssert.reverts(0, dex.createMarketOrder(web3.utils.fromUtf8("XYZ")), 1, marketPrice);

        dex.depositEth({value: 100})

        await truffleAssert.passes(0, dex.createMarketOrder(web3.utils.fromUtf8("XYZ")), 1, marketPrice);
    })

//Market order can be submitted even if the order book is empty
    it("Market order can be submitted even if the limit order book is empty", async () => {
        let dex = Dex.deployed()
        let xyz = Xyz.deployed()

        let buyOrderBook = await dex.getOrderBook(0, web3.utils.fromUtf8("XYZ"));
        assert (buyOrderBook.length >= 0)

        let sellOrderBook = await dex.getOrderBook(1, web3.utils.fromUtf8("XYZ"));
        assert (sellOrderBook.length >= 0)

        await truffleAssert.passes(0, dex.createMarketOrder(web3.utils.fromUtf8("XYZ")), 1, 100);
        await truffleAssert.passes(1, dex.createMarketOrder(web3.utils.fromUtf8("XYZ")), 1, 100);
    })

//Market orders should be filled until the orderbook is empty or the market order is 100% filled
    it("Market orders should be filled until the orderbook is empty or the market order is 100% filled", async () => {
        let dex = Dex.deployed()
        let xyz = Xyz.deployed()

    //Market SELL orders
        await xyz.approve(dex.address, 500);
        await dex.createMarketOrder(1, web3.utils.fromUtf8("XYZ"), 10)
        await dex.createMarketOrder(1, web3.utils.fromUtf8("XYZ"), 5)
        assert (sellOrderBook.length > 0);
        
    //Market BUY orders
        dex.depositEth({value: 500})
        await dex.createMarketOrder(0, web3.utils.fromUtf8("XYZ"), 10)
        await dex.createMarketOrder(0, web3.utils.fromUtf8("XYZ"), 5)
        assert (buyOrderBook.length > 0);

        for(j = 0; j < buyOrderBook.length; j++){
            if(buyOrderBook[j].amount > 0){
                for(i = 0; i < sellOrderBook.length; i++){
                    if(sellOrderBook[i].amount > 0 && buyOrderBook[j].price == sellOrderBook[i].price){
                        buyOrderBook[j].amount = buyOrderBook[j].amount.sub(sellOrderBook[i].amount)
                    }
                    if (sellOrderBook[i].amount == 0 || sellOrderBook.amount[i] > buyOrderBook.amount[j]){
                        break;
                    }
                }
            }
        }
    })
    //The token balances of the limit order sellers should decrease with the filled amounts
    it("The token balances of the limit order sellers should decrease with the filled amounts", async () => {
        let dex = Dex.deployed()
        let xyz = Xyz.deployed()
        
        let balance = await dex.balances(msg.sender, web3.utils.fromUtf8("XYZ"))

        for(i = 0; i < buyOrderBook.length; i++){
            if (msg.sender(sellOrderBook.amount) == buyOrderBook[i].amount){
                balance = balance.sub(sellOrderBook.amount);
            }
        }
    })

    //Filled limit orders should be removed from the order book
    it("Filled limit orders should be removed from the order book", async () => {

    })
})

Many thanks!!

1 Like

Hello I have a question about Filip’s solution.
I don’t understand why this is possible (line43) :

 let orderbook = await dex.getOrderBook(web3.utils.fromUtf8("LINK"), 0);

If you look at the solidity file, getOrderBook function returns Order[] which is an array of Order struct. However, in the JS file, it receives as type let. How is this possible?

tokens.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract Eth is ERC20 {
  constructor() ERC20("Ether", "ETH") {
    _mint(msg.sender, 10000);
  }
}

contract Link is ERC20 {
  constructor() ERC20("Chainlink", "LINK") {
    _mint(msg.sender, 10000);
  }
}

3_tokens_migration.js

const Link = artifacts.require("Link");
const Eth = artifacts.require("Eth");

module.exports = async function (deployer) {
  await deployer.deploy(Link);
  await deployer.deploy(Eth);
};

dexTest.js

const Dex = artifacts.require("Dex");
const Link = artifacts.require("Link");
const Eth = artifacts.require("Eth");
const truffleAssert = require('truffle-assertions');

contract("Dex", accounts => {
  let dex;
  let eth;
  let link;

  before(async function(){
    dex = await Dex.deployed();
    eth = await Eth.deployed();
    link = await Link.deployed();
    await dex.addToken(web3.utils.fromUtf8("LINK"), link.address, {from: accounts[0]});
    await dex.addToken(web3.utils.fromUtf8("ETH"), eth.address, {from: accounts[0]});
    await eth.approve(dex.address, 5000)
    await link.approve(dex.address, 5000);
  });

  //The user must have ETH deposited such that deposited eth >= buy order value
  it("should throw an error if ETH balance is too low when creating BUY limit order", async () => {
    await truffleAssert.reverts(
      dex.createLimitOrder(0, web3.utils.fromUtf8("LINK"), 10, 1)
    );
    await dex.deposit(10, web3.utils.fromUtf8("ETH")); // await dex.depositEth({value: 10});
    await truffleAssert.passes(
      dex.createLimitOrder(0, web3.utils.fromUtf8("LINK"), 10, 1)
    );
  });
  //The user must have enough tokens deposited such that token balance >= sell order amount
  it("should throw an error if token balance is too low when creating SELL limit order", async () => {
    await truffleAssert.reverts(
      dex.createLimitOrder(1, web3.utils.fromUtf8("LINK"), 10, 1)
    );
    await dex.deposit(10, web3.utils.fromUtf8("LINK"));
    await truffleAssert.passes(
      dex.createLimitOrder(1, web3.utils.fromUtf8("LINK"), 10, 1)
    );
  });
  //The BUY order book should be ordered on price from highest to lowest starting at index 0
  it("The BUY order book should be ordered on price from highest to lowest starting at index 0", async () => {
    await dex.deposit(1000, web3.utils.fromUtf8("ETH"));
    await dex.createLimitOrder(0, web3.utils.fromUtf8("LINK"), 1, 300);
    await dex.createLimitOrder(0, web3.utils.fromUtf8("LINK"), 1, 100);
    await dex.createLimitOrder(0, web3.utils.fromUtf8("LINK"), 1, 200);

    let orderbook = await dex.getOrderBook(web3.utils.fromUtf8("LINK"), 0);
    assert(orderbook.length > 0);
    for (let i = 0; i < orderbook.length - 1; i++) {
      assert(orderbook[i].price >= orderbook[i+1].price, "not right order in buy book");
    }
  });
  //The SELL order book should be ordered on price from lowest to highest starting at index 0
  it("The SELL order book should be ordered on price from lowest to highest starting at index 0", async () => {
    await dex.deposit(1000, web3.utils.fromUtf8("LINK"));
    await dex.createLimitOrder(1, web3.utils.fromUtf8("LINK"), 1, 300);
    await dex.createLimitOrder(1, web3.utils.fromUtf8("LINK"), 1, 100);
    await dex.createLimitOrder(1, web3.utils.fromUtf8("LINK"), 1, 200);

    let orderbook = await dex.getOrderBook(web3.utils.fromUtf8("LINK"), 1);
    assert(orderbook.length > 0);
    for (let i = 0; i < orderbook.length - 1; i++) {
      assert(orderbook[i].price <= orderbook[i+1].price, "not right order in sell book");
    }
  });
});

let is not a type. Do not take it personal, but I suggest you to learn javascript first before doing this course (var/let/const are ways to declare variables or constants)

1 Like

Thanks, now I understand; got a bit confused, going back and forth between different languages…

Here are just the should statements

// arguments
// (address trader, bytes32 ticker, uint256 amount, uint256 price, Side side)

// balances
// should allow BUY Limit order if balance is >= buy order value
// should allow SELL Limit order if token balance >= sell order amount

// price
// should allow BUY Limit order if price has been met
// should not allow BUY Limit order if price has not been met

// should allow SELL Limit order if price has been met
// should not allow SELL Limit order if price has not been met

// price arrangement
// should arrange BUY Order Book prices from highest to lowest starting at index 0
// should arrange SELL Order Book prices from lowest to highest starting at index 0

// slippage
// should allow BUY Limit order within slippage value settings
// should allow BUY Limit order within slippage time settings

@dan-i do you know how to make one of the accounts array in truffle to have ETH balance 0 ? I’m writing a negative test that checks before buying a token if the eth balance is not zero.

Thank you!

Hey @danielmain

There might be a functionality in ganache, I am sure you can increase / decrease the amount of all accounts, not sure if you can do it by specifying one single account.
I would just send all the ethers to another account.

Cheers,
Dani

Thank you @dan-i but how should I connect @openzeppelin/test-environment’ with ganache?

Ganache is running and I don’t know where in the code or in the config I can tell @openzeppelin/test-environment that should connect to my running ganache. My test-environment.config.js look like this:

module.exports = {
    accounts: {
      amount: 3, // Number of unlocked accounts
      ether: 0, // Initial balance of unlocked accounts (in ether)
    },
  
    contracts: {
      type: 'truffle', // Contract abstraction to use: 'truffle' for @truffle/contract or 'web3' for web3-eth-contract
      defaultGas: 6e6, // Maximum gas for contract calls (when unspecified)
  
      // Options available since v0.1.2
      defaultGasPrice: 20e9, // Gas price for contract calls (when unspecified)
      artifactsDir: 'build/contracts', // Directory where contract artifacts are stored
    },
  
    node: { // Options passed directly to Ganache client
      gasLimit: 8e6, // Maximum gas per block
      gasPrice: 20e9 // Sets the default gas price for transactions if not otherwise specified.
    },
  };

even though the address I get running jest are not the same I see in ganache :frowning:

CleanShot 2021-04-21 at 15.44.16@2x

Hi All!

I have made this far yet:

const Dex = artifacts.require("Dex")

const Link = artifacts.require("Link")

const truffleAssert = require('truffle-assertions');

// The User must have ETH deposited such that deposited ETH >= buy order value

// The user must have enough tokens deposited such that token balance >= sell order amount 

// The BUY order book should be ordered on price from highest to lowest starting at index 0 

contract("Dex", accounts =>{

    //The user must have ETH deposited such that deposited ETH >= buy order value

    it("The user cannot create the limit order as the amount is less than the balance", async() =>{

    let Dex = await Dex.deployed()

    let Link = await Link.deployed()

    await truffleAssert.reverts(

        dex.createLimitOrder(web3.utils.fromUtf8("LINK"), 5, 3))

    

})

})

When I run the test I am getting this error:

" ReferenceError: Cannot access ‘Dex’ before initialization"

Any idea how can I fix?

Thanks!

Dex is a constant you cannot overwrite it
You should rename your let Dex in line 10 to somethink like let dexDeployed.

@dan-i or @filip is there any objection if for this course I use hardhat instead of truffle ? Seems that I have more options in my unit tests and the possibility to connect to my local Ganacha.

Here is my proposed solution:

//The user must have ETH deposited such that eth >= buy order value
//The user must have enough tokens deposited such that token balnce >= sell order amount
//The BUY order book should be ordered on price from highest to lowest starting at index 0
//The SELL order book should be ordered on price from lowest to highest starting at index 0?

const Dex = artifacts.require("Dex");
const Link = artifacts.require("Link")
//const Eth = artifacts.require("Eth")
const truffleAssert = require('truffle-assertions');
//Side side, bytes32 ticker, uint amount, uint price

contract("Dex", accounts => {
    it ("should only be possible for trader to put in a BUY order up to the amount of eth he has", async () => {
        let dex = await Dex.deployed();
        let link = await Link.deployed();
        //let eth = await Eth.deployed();
        
        await truffleAssert.reverts(
            dex.createLimitOrder(0, web3.utils.fromUtf8("LINK"), 10, 20 ) //10 @ price 20 = 200 eth
        )
        //await dex.addToken(web3.utils.fromUtf8("LINK"), link.address);
        //await dex.addToken(web3.utils.fromUtf8("ETH"), eth.address);
        dex.depositEth({value:100}) //dex.deposit(100, web3.utils.fromUtf8("ETH")); //why not await on this one?
        await truffleAssert.passes(
            dex.createLimitOrder(0, web3.utils.fromUtf8("LINK"), 10, 10 ) //10 @ price 10 = 100 eth
        )
    })

    it ("user should have enough tokens deposited s t token balance >= sell order amount", async () => {
        let dex = await Dex.deployed()
        let link = await Link.deployed()
        await truffleAssert.reverts(
            dex.createLimitOrder(1, web3.utils.fromUtf8("LINK"), 10, 10) //on selling, it's only the amount that is important here
        )
        await link.approve(dex.address,500);
        await dex.addToken(web3.utils.fromUtf8("LINK"), link.address, {from: accounts[0]});
        await dex.deposit(100, web3.utils.fromUtf8("LINK"), {from: accounts[0]}); //why throwing an error?
        //console.log(dex.balances)
        await truffleAssert.passes(
            dex.createLimitOrder(1, web3.utils.fromUtf8("LINK"), 10, 10) //on selling, it's only the amount that is important here
        )
        
    })

    it ("should have a BUY order book ordered on price from highest to lowest starting at index 0", async () => { //before and after creating a limit order
        let dex = await Dex.deployed() //what if orderbook is empty??
        let link = await Link.deployed()
        await link.approve(dex.address, 100);
        await dex.depositEth({value: 3000});
        //let eth = await Eth.deployed()
        //await dex.addToken(web3.utils.fromUtf8("LINK"), link.address)
        //await dex.addToken(web3.utils.fromUtf8("ETH"), eth.address)
        //await dex.deposit(100, web3.utils.fromUtf8("ETH"));
        
        await dex.createLimitOrder(0, web3.utils.fromUtf8("LINK"), 10, 10);
        await dex.createLimitOrder(0, web3.utils.fromUtf8("LINK"), 10, 30);
        await dex.createLimitOrder(0, web3.utils.fromUtf8("LINK"), 10, 20);
        await dex.createLimitOrder(1, web3.utils.fromUtf8("LINK"), 10, 50);
        await dex.createLimitOrder(1, web3.utils.fromUtf8("LINK"), 10, 40);
        
        let orderbook = await dex.getOrderBook(web3.utils.fromUtf8("LINK"), 0);
        assert(orderbook.length > 0);
        console.log(orderbook);
        for (let i = 0; i < orderbook.length - 1; i++) {
            assert(orderbook[i].price >= orderbook[i+1].price, "not right order in buy book");
        }
    })

    it ("should have a SELL order book ordered on price from lowest to highest starting at index 0", async () => { //is this right?
        let dex = await Dex.deployed();
        let link = await Link.deployed();
        //await dex.addToken(web3.utils.fromUtf8("LINK"), link.address);
        //await dex.deposit(100, web3.utils.fromUtf8("LINK")); //why does he not have this line in his code??
        await link.approve(dex.address, 1000);

        await dex.createLimitOrder(1, web3.utils.fromUtf8("LINK"), 10, 10);
        await dex.createLimitOrder(1, web3.utils.fromUtf8("LINK"), 10, 30);
        await dex.createLimitOrder(1, web3.utils.fromUtf8("LINK"), 10, 20);
        await dex.createLimitOrder(1, web3.utils.fromUtf8("LINK"), 10, 50);
        await dex.createLimitOrder(1, web3.utils.fromUtf8("LINK"), 10, 40);

        let orderbook = await dex.getOrderBook(web3.utils.fromUtf8("LINK"), 1);
        assert(orderbook.length > 0);
        console.log(orderbook);
        for (let i = 0; i < orderbook.length - 1; i++) {
            assert(orderbook[i].price <= orderbook[i+1].price, "not right order in sell book");
        }
    })
})
2 Likes

Hey @Riki

dex.createLimitOrder

The variable dex does not exists in your code as far as I see.

Double check that and include a screenshot of the error.

Cheers,
Dani

Hi @danielmain

This course uses Truffle to deploy and test, we don’t have one for Hardhat yet.

Cheers,
Dani

Hello.

Here is my code. Not 100% here as we did not have functions yet.

//The user must have ETH deposited such that  deposited ETH >= buy order value

//The user must have enough tokens deposited such that token balance > sell order amount

//The BUY order book should be ordered on price from highest to lowest starting at index 0

//User must have a minimum amount in order to create buy or sell order

//User must be the owner in order to create bus or sell order

const Dex = artifacts.require("Dex")

const Link = artifacts.require("LINK")

const truffleAssert = require('truffle-assertions');

contract("Dex", accounts => {

    it("Not enough Ether balance to create buy order value", async() =>{

        let dex = await Dex.deployed()

        let link = await Link.deployed()

        await truffleAssert.reverts(

            await dex.createLimitOrder(0, web3.utils.fromUtf8("LINK"), 9, 5)

        )

        dex.depositETH({value:10})

        await dex.createLimitOrder(0,web3.utils.fromUtf8("LINK", 9, 5)) //this will pass as this is having the enough deposited ether

       

    })

    it("Not enough token balance to start sell value", async() =>{

        let dex = await Dex.deployed()

        let link = await Link.deployed()

        await dex.addToken(web3.utils.fromUtf8("LINK"), link.address, {from: accounts[0]})

        await truffleAssert.reverts(

            await dex.createLimitOrder(0, web3.utils.fromUtf8("LINK"), 600, 5)) // I think this should fail because there is no deposit made

        await dex.deposit(500, web3.utils.fromUtf8("LINK"))

        await truffleAssert.passes(

            await dex.createLimitOrder(0, web3.utils.fromutf8("LINK"), 300, 5) // This should go in now as there are deposited amounts in 

        )

    })

    it("They buy order book need to be ordered starting from highest price to lowest ", async() =>{

        let dex = await Dex.deployed()

        let link = await Link.deployed()

        await link.approve(dex.address, 5000)

        await dex.depositEth({value:850})

        await dex.createLimitOrder(0, web3.utils.fromUtf8("LINK"), 100, 3)

        await dex.createLimitOrder(1, web3.utils.fromUtf8("LINK"), 300, 6)

        await dex.createLimitOrder(2, web3.utils.fromUtf8("LINK"), 250, 10)

        await dex.createLimitOrder(3, web3.utils.fromUtf8("LINK"), 200, 2)

        

        let Orderbook = await dex.getOrderBook(web3.utils.fromUtf8("LINK"), 0)

        assert(orderbook.length >0);

        for (let i =0; i<orderbook.length-1, i++;){

            assert(orderbook[i].price >= orderbook[i+1].price) //In this case the orderBook is not sorted

        }

        }

    )

})
1 Like

Hello there!

This one was a bit tricky but after a while (some days) I finally got this to work! :smiley:


    //Should handle ETH deposits correctly
    //The user must have ETH deposited such that deposited eth >= buy order value
    //The user must have enough tokens deposited such that token balance >= sell order amount
    //The BUY order book should be ordered on price from highest to lowest starting at index 0
    //The SELL order book should be ordered on price from lowest to highest starting at index 0

    //createLimitOrder(bytes32 ticker, Side side, uint amount, uint ethPrice)
const Dex = artifacts.require("Dex");
const Link = artifacts.require("Link");
const truffleAssert = require('truffle-assertions');

contract("Dex", accounts => {
    it("should handle ETH deposits correctly", async () => {
        let dex = await Dex.deployed()
        await dex.depositEth({from: accounts[0], value: web3.utils.toWei('2', 'ether')})
        let balance = await dex.balances(accounts[0], web3.utils.fromUtf8("ETH"))
        assert.equal( balance, web3.utils.toWei('2', 'ether') )
    })
    
    it("user must have more eth deposited than the buy order value", async () => {
        let dex = await Dex.deployed()
        
        await truffleAssert.passes(
            dex.createLimitOrder(web3.utils.fromUtf8("LINK"), 0, 2, web3.utils.toWei('500', 'finney'))
        )
        await truffleAssert.reverts(
            dex.createLimitOrder(web3.utils.fromUtf8("LINK"), 0, 200, web3.utils.toWei('500', 'finney'))
        )
    })
    
    it("user must have more or equal tokens deposited than the sell order amount", async () => {
        let dex = await Dex.deployed()
        let link = await Link.deployed()
        
        await truffleAssert.reverts(
            await dex.createLimitOrder(web3.utils.fromUtf8("LINK"), 1, 500, web3.utils.toWei('1', 'finney'))
        )

        await dex.addToken(web3.utils.fromUtf8("LINK"), link.address)
        await link.approve(dex.address, 500, {from: accounts[0]})
        await dex.deposit(500, web3.utils.fromUtf8("LINK"))

        await truffleAssert.passes(
            await dex.createLimitOrder(web3.utils.fromUtf8("LINK"), 1, 500, web3.utils.toWei('1', 'finney'))
        )
        
        
    })
    
    it("buy order book is ordered on price from highest to lowest starting at index 0", async () => {
        let dex = await Dex.deployed()
        let link = await Link.deployed()

        await dex.depositEth({from: accounts[1], value: web3.utils.toWei('2', 'ether')})
        await dex.depositEth({from: accounts[2], value: web3.utils.toWei('2', 'ether')})
        
        await dex.createLimitOrder(web3.utils.fromUtf8("LINK"), 0, 500, web3.utils.toWei('1', 'finney'), {from: accounts[0]})
        await dex.createLimitOrder(web3.utils.fromUtf8("LINK"), 0, 200, web3.utils.toWei('1', 'finney'), {from: accounts[1]})
        await dex.createLimitOrder(web3.utils.fromUtf8("LINK"), 0, 400, web3.utils.toWei('1', 'finney'), {from: accounts[2]})
        let orderbook = await dex.getOrderBook(web3.utils.fromUtf8("LINK"), 0)

        for(let i=0;i<orderbook.length;i++){
            assert(orderbook[i].price >= orderbook[i+1].price, "Buy orderbook is not sorted correctly")
        }
    })
    
    it("sell order book is ordered on price from highest to lowest starting at index 0", async () => {
        let dex = await Dex.deployed()
        let link = await Link.deployed()

        await link.approve(dex.address, 200, {from: accounts[1]})
        await link.approve(dex.address, 400, {from: accounts[2]})

        await dex.deposit(200, web3.utils.fromUtf8("LINK"), {from: accounts[1]})
        await dex.deposit(400, web3.utils.fromUtf8("LINK"), {from: accounts[2]})

        await dex.createLimitOrder(web3.utils.fromUtf8("LINK"), 1, 500, web3.utils.toWei('1', 'finney'), {from: accounts[0]})
        await dex.createLimitOrder(web3.utils.fromUtf8("LINK"), 1, 200, web3.utils.toWei('1', 'finney'), {from: accounts[1]})
        await dex.createLimitOrder(web3.utils.fromUtf8("LINK"), 1, 400, web3.utils.toWei('1', 'finney'), {from: accounts[2]})

        let orderbook = await dex.getOrderBook(web3.utils.fromUtf8("LINK"), 0)

        for(let i=0;i<orderbook.length;i++){
            assert(orderbook[i].price <= orderbook[i+1].price, "Sell orderbook is not sorted correctly")
        }
    })
    
})
1 Like