Solution:
const Dex = artifacts.require("Dex")
const Link = artifacts.require("Link")
const truffleAssert = require('truffle-assertions');
contract("Dex", accounts => {
it("the user have ETH deposited such that deposited eth >= buy order value", async () =>{
let dex = await Dex.deployed();
let link = await Link.deployed();
await dex.addToken(web3.utils.fromUtf8("LINK"), link.address, {from: accounts[0]})
//createLimitOrder should take care of approve and deposit
truffleAssert.passes(
dex.createLimitOrder("LINK", 500, 50, 0, {from: accounts[0]})
)
})
it("the user have ETH deposited such that deposited eth < buy order value, should revert", async () =>{
let dex = await Dex.deployed();
let link = await Link.deployed();
await dex.addToken(web3.utils.fromUtf8("LINK"), link.address, {from: accounts[0]})
//createLimitOrder should take care of approve and deposit
truffleAssert.reverts(
dex.createLimitOrder("LINK", 5000000, 50000000000, 0, {from: accounts[0]})
)
})
it("the user have enough tokens deposited such that token balance >= sell order amount", async () =>{
let dex = await Dex.deployed();
let link = await Link.deployed();
//createLimitOrder should take care of approve and deposit
truffleAssert.passes(
dex.createLimitOrder("LINK", 500, 50, 1, {from: accounts[0]})
)
})
it("the user have tokens deposited such that token balance < sell order amount", async () =>{
let dex = await Dex.deployed();
let link = await Link.deployed();
truffleAssert.reverts(
dex.createLimitOrder("LINK", 500000, 50, 1, {from: accounts[0]})
)
})
it("order book BUY side should be sorted from highest to lowest", async () =>{
let dex = await Dex.deployed();
let link = await Link.deployed();
await dex.createLimitOrder(web3.utils.fromUtf8("LINK"), 2, 1, 0, {from: accounts[0]});
await dex.createLimitOrder(web3.utils.fromUtf8("LINK"), 3, 2, 0, {from: accounts[1]});
await dex.createLimitOrder(web3.utils.fromUtf8("LINK"), 1, 3, 0, {from: accounts[2]});
await dex.createLimitOrder(web3.utils.fromUtf8("LINK"), 4, 4, 0, {from: accounts[3]});
var p1 = 1;
var p2 = 2;
let orderBook = await dex.getOrderBook;
for(let i = 0; i < orderBook.length-1; i++){
p1 = orderBook[i].price;
p2 = orderBook[i+1].price;
truffleAssert(p1 >= p2);
}
})
it("order book SELL side should be sorted from lowest to highest", async () =>{
let dex = await Dex.deployed();
let link = await Link.deployed();
await dex.createLimitOrder(web3.utils.fromUtf8("LINK"), 2, 1, 0, {from: accounts[0]});
await dex.createLimitOrder(web3.utils.fromUtf8("LINK"), 3, 2, 0, {from: accounts[1]});
await dex.createLimitOrder(web3.utils.fromUtf8("LINK"), 1, 3, 0, {from: accounts[2]});
await dex.createLimitOrder(web3.utils.fromUtf8("LINK"), 4, 4, 0, {from: accounts[3]});
var p1 = 1;
var p2 = 2;
let orderBook = await dex.getOrderBook;
for(let i = 0; i < orderBook.length-1; i++){
p1 = orderBook[i].price;
p2 = orderBook[i+1].price;
assert(p1 <= p2);
}
})
})