Skip to content

Instantly share code, notes, and snippets.

@molekilla
Created June 13, 2019 19:23
Show Gist options
  • Save molekilla/1ececda84b3b8d8753bc05d6fa65bafe to your computer and use it in GitHub Desktop.
Save molekilla/1ececda84b3b8d8753bc05d6fa65bafe to your computer and use it in GitHub Desktop.
better course material
uint buyBookOffersCount;
uint pairsAmountBuyPrices;
// Contains a buy book order offers
// dict[pairIndex, price, offerIndex]
mapping (uint => mapping(uint => mapping(uint => Offer))) buyBookOffers;
// TokenPairInfo eg ETH/PAX, ETH/PAX
mapping (uint => TokenPairInfo) pairs;
// Contains a buy book order prices
mapping (uint => mapping(unint => OrderBookOfferPrice) buyBookPrices;
// TODO... same for sell
addBuyOrder(pairIndex, priceInWei, amount, user) {
buyBookOffersCount++;
buyBookOffers[pairIndex][priceInWei][buyBookOffersCount] = Offer(amount, token, priceInWei, user)
if (buyBookOffersCount == 1) {
buyBookPrices[pairIndex][priceInWei].offersKey = 1;
pairsAmountBuyPrices++;
currentBuyPrice = pairs[pairIndex].currentBuyPrice;
lowestBuyPrice = pairs[pairIndex].lowestBuyPrice;
if (lowestBuyPrice == 0 || lowestBuyPrice > priceInWei) {
if (currentBuyPrice == 0) {
pairs[pairIndex].currentBuyPrice = priceInWei;
buyBookPrices[pairIndex][priceInWei].higherPrice = priceInWei;
buyBookPrices[pairIndex][priceInWei].lowerPrice = 0;
} else {
pairs[pairIndex].currentBuyPrice = priceInWei;
buyBookPrices[pairIndex][priceInWei].higherPrice = lowestBuyPrice;
buyBookPrices[pairIndex][priceInWei].lowerPrice = 0;
}
pairs[pairIndex].lowestBuyPrice = priceInWei;
} else if (currentBuyPrice < priceInWei) {
...
} else {
uint buyPrice = pairs[pairIndex].currentBuyPrice;
book found = false;
while (buyPrice > 0 && !found) {
if (buyPrice < priceInWei && buyBookPrices[pairIndex][priceInWei].higherPrice > priceInWei) {
buyBookPrices[pairIndex][priceInWei].lowerPrice = buyPrice;
buyBookPrices[pairIndex][priceInWei].higherPrice = buyBookPrices[pairIndex][buyPrice].higherPrice;
uint temp = buyBookPrices[pairIndex][buyPrice].higherPrice
buyBookPrices[pairIndex][temp].lowerPrice = priceInWei;
buyBookPrices[pairIndex][temp].higherPrice = priceInWei;
found = true;
}
buyPrice = buyBookPrices[pairIndex][buyPrice].lowerPrice;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment