Hi, my new attempt. i used decimals which gives me underflow errors. and i had to approve tokens before deposit in dex.
// the user must have ETH deposited such that eth >= buy order value
// the user must have enough tokens deposited such that token balance > sell order amount
// the BUY order must be ordered on price from the highest to lowest starting at index 0
// the SELL order must be ordered on price from the lowest to highest starting at index 0
// the BUY order must be processed when the prices of BUY and SELL orders matches
// once an order is completed (i.e token amount of order reach zero) it must be delete from the orderbook
const Dex = artifacts.require("Dex")
const Token = artifacts.require("MyToken");
const TruffleAssert = require('truffle-assertions')
contract ("Dex", accounts => {
it("Must have enough ETH",async ()=>{
let token = await Token.deployed()
let dex = await Dex.deployed()
await dex.addToken(web3.utils.fromUtf8("MTK"),token.address)
// have to create a payable deposit function to deposit ether
await dex.depositEther({value: 4500, from: accounts[1]})
await TruffleAssert.reverts(
dex.createLimitOrder(100,55,web3.utils.fromUtf8("MTK"),0,{from: accounts[1]})
)
await dex.depositEther({value:1000, from: accounts[1]})
await TruffleAssert.passes(
dex.createLimitOrder(100,55,web3.utils.fromUtf8("MTK"),0,{from: accounts[1]})
)
})
it("Must have enough Token",async ()=>{
let token = await Token.deployed()
let dex = await Dex.deployed()
await dex.addToken(web3.utils.fromUtf8("MTK"),token.address)
await TruffleAssert.reverts(
dex.createLimitOrder(50,1,web3.utils.fromUtf8("MTK"),1,{from: accounts[2]})
)
await token.transfer(accounts[2],65,{from: accounts[0]})
await token.approve(dex.address,50,{from:accounts[2]})
await dex.deposit(web3.utils.fromUtf8("MTK"),40,{from: accounts[2]})
await TruffleAssert.reverts(
dex.createLimitOrder(50,1,web3.utils.fromUtf8("MTK"),1,{from: accounts[2]})
)
await TruffleAssert.passes(
dex.createLimitOrder(30,1,web3.utils.fromUtf8("MTK"),1,{from: accounts[2]})
)
})
it("the buy orders must be ordered on price from highest to lowest",async ()=>{
let token = await Token.deployed()
let dex = await Dex.deployed()
await dex.addToken(web3.utils.fromUtf8("MTK"),token.address)
await dex.depositEther({value: 50, from: accounts[0]})
await dex.depositEther({value: 300, from: accounts[1]})
await dex.depositEther({value: 800, from: accounts[2]})
await dex.depositEther({value: 550, from: accounts[3]})
await dex.createLimitOrder(50,1,web3.utils.fromUtf8("MTK"),0,{from: accounts[1]})
await dex.createLimitOrder(100,3,web3.utils.fromUtf8("MTK"),0,{from: accounts[2]})
await dex.createLimitOrder(10,4,web3.utils.fromUtf8("MTK"),0,{from: accounts[0]})
await dex.createLimitOrder(110,5,web3.utils.fromUtf8("MTK"),0,{from: accounts[3]})
var BuyOrder = await dex.getOrderBook(web3.utils.fromUtf8("MTK"),0)
assert(BuyOrder.length > 0)
for(let i = 0; i<BuyOrder.length - 1; i++){
assert(BuyOrder[i].price > BuyOrder[i+1].price,"BUY orders not arranged correctly")
}
})
it("the sell orders must be ordered on price from lowest to highest",async ()=>{
let token = await Token.deployed()
let dex = await Dex.deployed()
await dex.addToken(web3.utils.fromUtf8("MTK"),token.address)
await token.transfer(accounts[1],150,{from: accounts[0]})
await token.transfer(accounts[2],250,{from: accounts[0]})
await token.transfer(accounts[3],150,{from: accounts[0]})
await token.approve(dex.address,100,{from: accounts[0]})
await token.approve(dex.address,150,{from: accounts[1]})
await token.approve(dex.address,250,{from: accounts[2]})
await token.approve(dex.address,110,{from: accounts[3]})
await dex.deposit(web3.utils.fromUtf8("MTK"),50,{from: accounts[0]})
await dex.deposit(web3.utils.fromUtf8("MTK"),50,{from: accounts[1]})
await dex.deposit(web3.utils.fromUtf8("MTK"),200,{from: accounts[2]})
await dex.deposit(web3.utils.fromUtf8("MTK"),110,{from: accounts[3]})
await dex.createLimitOrder(50,3,web3.utils.fromUtf8("MTK"),1,{from: accounts[1]})
await dex.createLimitOrder(100,2,web3.utils.fromUtf8("MTK"),1,{from: accounts[2]})
await dex.createLimitOrder(10,5,web3.utils.fromUtf8("MTK"),1,{from: accounts[0]})
await dex.createLimitOrder(110,4,web3.utils.fromUtf8("MTK"),1,{from: accounts[3]})
var SellOrder = await dex.getOrderBook(web3.utils.fromUtf8("MTK"),1)
assert(SellOrder.length > 0)
for(let i = 0; i<SellOrder.length; i++){
console.log(SellOrder[i])
}
})
it("must process matching orders",async ()=>{
let token = await Token.deployed()
let dex = await Dex.deployed()
await dex.addToken(web3.utils.fromUtf8("MTK"),token.address)
await token.approve(dex.address,2500)
await dex.deposit(web3.utils.fromUtf8("MTK"),1000,{from: accounts[0]})
dex.createLimitOrder(500,5,web3.utils.fromUtf8("MTK"),1,{from: accounts[0]})
await dex.depositEther({value:1000, from: accounts[1]})
var before_buyerbalance = await dex.balances(accounts[1],web3.utils.fromUtf8("MTK"))
console.log(before_buyerbalance.toNumber())
var before_sellerbalance = await dex.balances(accounts[0],web3.utils.fromUtf8("MTK"))
console.log(before_sellerbalance.toNumber())
await dex.createLimitOrder(100,5,web3.utils.fromUtf8("MTK"),0,{from: accounts[1]})
var after_buyerbalance = await dex.balances(accounts[1],web3.utils.fromUtf8("MTK"))
var after_sellerbalance = await dex.balances(accounts[0],web3.utils.fromUtf8("MTK"))
console.log(after_sellerbalance.toNumber())
console.log(after_buyerbalance.toNumber())
assert(after_buyerbalance.toNumber() == before_buyerbalance.toNumber() + 100)
})
it("must delete completed Orders",async ()=>{
let token = await Token.deployed()
let dex = await Dex.deployed()
await dex.addToken(web3.utils.fromUtf8("MTK"),token.address)
token.approve(dex.address,2000)
await dex.deposit(web3.utils.fromUtf8("MTK"),1000,{from: accounts[0]})
await dex.createLimitOrder(500,5,web3.utils.fromUtf8("MTK"),1,{from: accounts[0]})
var before_SellOrder = await dex.getOrderBook(web3.utils.fromUtf8("MTK"),1)
await dex.depositEther({value:1000, from: accounts[1]})
await dex.depositEther({value: 1250, from: accounts[2]})
await dex.depositEther({value: 500, from: accounts[3]})
await dex.createLimitOrder(200,5,web3.utils.fromUtf8("MTK"),0,{from: accounts[1]})
await dex.createLimitOrder(250,5,web3.utils.fromUtf8("MTK"),0,{from: accounts[2]})
await dex.createLimitOrder(100,5,web3.utils.fromUtf8("MTK"),0,{from: accounts[3]})
var after_SellOrder = await dex.getOrderBook(web3.utils.fromUtf8("MTK"),1)
assert.equal(after_SellOrder.length, before_SellOrder.length-1)
})
})