Skip to content

Commit

Permalink
working test
Browse files Browse the repository at this point in the history
  • Loading branch information
uv-orbs committed Nov 21, 2023
1 parent 7488acb commit 624fbc4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
61 changes: 51 additions & 10 deletions scripts/taker-mock/src/begin-auction.js
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
8 changes: 8 additions & 0 deletions scripts/taker-mock/src/order-book.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 624fbc4

Please sign in to comment.