diff --git a/scripts/taker-mock/src/begin-auction.js b/scripts/taker-mock/src/begin-auction.js index e0e11ae..d7c4bb3 100644 --- a/scripts/taker-mock/src/begin-auction.js +++ b/scripts/taker-mock/src/begin-auction.js @@ -1,16 +1,57 @@ +function delay(time) { + return new Promise(resolve => setTimeout(resolve, time)); +} + + async function beginAuction(ob) { - const depth = await ob.marketDepth() + // BUY + let depth = await ob.marketDepth() console.log(depth) - //for (const ask of depth.asks) { - const ask = depth.asks[0] - const price = ask[0] - const size = ask[1] - const amountInBToken = price * size - const expectedAmountOut = size - const res = await ob.beginAuction("ETH-USD", "BUY", amountInBToken) - console.log(res) - //} + let sumBToken = 0 + let sumAToken = 0 + for (const ask of depth.asks) { + const price = parseFloat(ask[0]) + const size = parseFloat(ask[1]) + sumAToken += size + sumBToken += size * price + + } + let res = await ob.beginAuction("ETH-USD", "BUY", sumBToken) + if (!res) { + console.error('beginAuction BUY failed') + } + else if (parseFloat(res.amountOut) === sumAToken) { + console.log('SUCCESS BUY amount-out IS equal to sum of size', res.amountOut, sumAToken) + } + else { + console.error('BUY amount out is NOT equal not to sum of size', res.amountOut, sumAToken) + return false; + } + + // SELL (reset) + depth = await ob.marketDepth() + sumAToken = 0 + sumBToken = 0 + for (const bid of depth.bids) { + const price = parseFloat(bid[0]) + const size = parseFloat(bid[1]) + sumAToken += size + sumBToken += size * price + + } + res = await ob.beginAuction("ETH-USD", "SELL", sumAToken) + if (!res) { + console.error('beginAuction SELL failed') + } + else if (parseFloat(res.amountOut) === sumBToken) { + console.log('SUCCESS SELL amount-out IS equal to sum of size', res.amountOut, sumBToken) + return true; + } + else { + console.error('SELL amount out is NOT equal not to sum of size', res.amountOut, sumBToken) + return false; + } return true } diff --git a/scripts/taker-mock/src/order-book.js b/scripts/taker-mock/src/order-book.js index aa53c30..529279a 100644 --- a/scripts/taker-mock/src/order-book.js +++ b/scripts/taker-mock/src/order-book.js @@ -22,6 +22,14 @@ class Orderbook { } const response = await fetch(url, req); + // error + if (response.status >= 400) { + const message = await response.text() + console.error(message) + + return null + } + // Parse and work with the JSON response return await response.json(); }