Hello there!
This one was a bit tricky but after a while (some days) I finally got this to work!
//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")
}
})
})