|
@ -24,23 +24,23 @@ type SlotAPI struct { |
|
|
LastItem uint64 `json:"-"` |
|
|
LastItem uint64 `json:"-"` |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func getFirstLastBlock(slotNum int64) (int64, int64) { |
|
|
|
|
|
genesisBlock := cg.AuctionConstants.GenesisBlockNum |
|
|
|
|
|
blocksPerSlot := int64(cg.AuctionConstants.BlocksPerSlot) |
|
|
|
|
|
|
|
|
func (a *API) getFirstLastBlock(slotNum int64) (int64, int64) { |
|
|
|
|
|
genesisBlock := a.cg.AuctionConstants.GenesisBlockNum |
|
|
|
|
|
blocksPerSlot := int64(a.cg.AuctionConstants.BlocksPerSlot) |
|
|
firstBlock := slotNum*blocksPerSlot + genesisBlock |
|
|
firstBlock := slotNum*blocksPerSlot + genesisBlock |
|
|
lastBlock := (slotNum+1)*blocksPerSlot + genesisBlock - 1 |
|
|
lastBlock := (slotNum+1)*blocksPerSlot + genesisBlock - 1 |
|
|
return firstBlock, lastBlock |
|
|
return firstBlock, lastBlock |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func getCurrentSlot(currentBlock int64) int64 { |
|
|
|
|
|
genesisBlock := cg.AuctionConstants.GenesisBlockNum |
|
|
|
|
|
blocksPerSlot := int64(cg.AuctionConstants.BlocksPerSlot) |
|
|
|
|
|
|
|
|
func (a *API) getCurrentSlot(currentBlock int64) int64 { |
|
|
|
|
|
genesisBlock := a.cg.AuctionConstants.GenesisBlockNum |
|
|
|
|
|
blocksPerSlot := int64(a.cg.AuctionConstants.BlocksPerSlot) |
|
|
currentSlot := (currentBlock - genesisBlock) / blocksPerSlot |
|
|
currentSlot := (currentBlock - genesisBlock) / blocksPerSlot |
|
|
return currentSlot |
|
|
return currentSlot |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func isOpenAuction(currentBlock, slotNum int64, auctionVars common.AuctionVariables) bool { |
|
|
|
|
|
currentSlot := getCurrentSlot(currentBlock) |
|
|
|
|
|
|
|
|
func (a *API) isOpenAuction(currentBlock, slotNum int64, auctionVars common.AuctionVariables) bool { |
|
|
|
|
|
currentSlot := a.getCurrentSlot(currentBlock) |
|
|
closedAuctionSlots := currentSlot + int64(auctionVars.ClosedAuctionSlots) |
|
|
closedAuctionSlots := currentSlot + int64(auctionVars.ClosedAuctionSlots) |
|
|
openAuctionSlots := int64(auctionVars.OpenAuctionSlots) |
|
|
openAuctionSlots := int64(auctionVars.OpenAuctionSlots) |
|
|
if slotNum > closedAuctionSlots && slotNum <= (closedAuctionSlots+openAuctionSlots) { |
|
|
if slotNum > closedAuctionSlots && slotNum <= (closedAuctionSlots+openAuctionSlots) { |
|
@ -61,9 +61,9 @@ func getPagination(totalItems uint64, minSlotNum, maxSlotNum *int64) *db.Paginat |
|
|
return pagination |
|
|
return pagination |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func newSlotAPI(slotNum, currentBlockNum int64, bid *historydb.BidAPI, auctionVars *common.AuctionVariables) SlotAPI { |
|
|
|
|
|
firstBlock, lastBlock := getFirstLastBlock(slotNum) |
|
|
|
|
|
openAuction := isOpenAuction(currentBlockNum, slotNum, *auctionVars) |
|
|
|
|
|
|
|
|
func (a *API) newSlotAPI(slotNum, currentBlockNum int64, bid *historydb.BidAPI, auctionVars *common.AuctionVariables) SlotAPI { |
|
|
|
|
|
firstBlock, lastBlock := a.getFirstLastBlock(slotNum) |
|
|
|
|
|
openAuction := a.isOpenAuction(currentBlockNum, slotNum, *auctionVars) |
|
|
slot := SlotAPI{ |
|
|
slot := SlotAPI{ |
|
|
ItemID: uint64(slotNum), |
|
|
ItemID: uint64(slotNum), |
|
|
SlotNum: slotNum, |
|
|
SlotNum: slotNum, |
|
@ -75,10 +75,10 @@ func newSlotAPI(slotNum, currentBlockNum int64, bid *historydb.BidAPI, auctionVa |
|
|
return slot |
|
|
return slot |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func newSlotsAPIFromWinnerBids(fromItem *uint, order string, bids []historydb.BidAPI, currentBlockNum int64, auctionVars *common.AuctionVariables) (slots []SlotAPI) { |
|
|
|
|
|
|
|
|
func (a *API) newSlotsAPIFromWinnerBids(fromItem *uint, order string, bids []historydb.BidAPI, currentBlockNum int64, auctionVars *common.AuctionVariables) (slots []SlotAPI) { |
|
|
for i := range bids { |
|
|
for i := range bids { |
|
|
slotNum := bids[i].SlotNum |
|
|
slotNum := bids[i].SlotNum |
|
|
slot := newSlotAPI(slotNum, currentBlockNum, &bids[i], auctionVars) |
|
|
|
|
|
|
|
|
slot := a.newSlotAPI(slotNum, currentBlockNum, &bids[i], auctionVars) |
|
|
if order == historydb.OrderAsc { |
|
|
if order == historydb.OrderAsc { |
|
|
if slot.ItemID >= uint64(*fromItem) { |
|
|
if slot.ItemID >= uint64(*fromItem) { |
|
|
slots = append(slots, slot) |
|
|
slots = append(slots, slot) |
|
@ -92,8 +92,8 @@ func newSlotsAPIFromWinnerBids(fromItem *uint, order string, bids []historydb.Bi |
|
|
return slots |
|
|
return slots |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func addEmptySlot(slots []SlotAPI, slotNum int64, currentBlockNum int64, auctionVars *common.AuctionVariables, fromItem *uint, order string) ([]SlotAPI, error) { |
|
|
|
|
|
emptySlot := newSlotAPI(slotNum, currentBlockNum, nil, auctionVars) |
|
|
|
|
|
|
|
|
func (a *API) addEmptySlot(slots []SlotAPI, slotNum int64, currentBlockNum int64, auctionVars *common.AuctionVariables, fromItem *uint, order string) ([]SlotAPI, error) { |
|
|
|
|
|
emptySlot := a.newSlotAPI(slotNum, currentBlockNum, nil, auctionVars) |
|
|
if order == historydb.OrderAsc { |
|
|
if order == historydb.OrderAsc { |
|
|
if emptySlot.ItemID >= uint64(*fromItem) { |
|
|
if emptySlot.ItemID >= uint64(*fromItem) { |
|
|
slots = append(slots, emptySlot) |
|
|
slots = append(slots, emptySlot) |
|
@ -106,25 +106,25 @@ func addEmptySlot(slots []SlotAPI, slotNum int64, currentBlockNum int64, auction |
|
|
return slots, nil |
|
|
return slots, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func getSlot(c *gin.Context) { |
|
|
|
|
|
|
|
|
func (a *API) getSlot(c *gin.Context) { |
|
|
slotNumUint, err := parseParamUint("slotNum", nil, 0, maxUint32, c) |
|
|
slotNumUint, err := parseParamUint("slotNum", nil, 0, maxUint32, c) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
retBadReq(err, c) |
|
|
retBadReq(err, c) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
currentBlock, err := h.GetLastBlock() |
|
|
|
|
|
|
|
|
currentBlock, err := a.h.GetLastBlock() |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
retBadReq(err, c) |
|
|
retBadReq(err, c) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
auctionVars, err := h.GetAuctionVars() |
|
|
|
|
|
|
|
|
auctionVars, err := a.h.GetAuctionVars() |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
retBadReq(err, c) |
|
|
retBadReq(err, c) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
slotNum := int64(*slotNumUint) |
|
|
slotNum := int64(*slotNumUint) |
|
|
bid, err := h.GetBestBidAPI(&slotNum) |
|
|
|
|
|
|
|
|
bid, err := a.h.GetBestBidAPI(&slotNum) |
|
|
if err != nil && err != sql.ErrNoRows { |
|
|
if err != nil && err != sql.ErrNoRows { |
|
|
retSQLErr(err, c) |
|
|
retSQLErr(err, c) |
|
|
return |
|
|
return |
|
@ -132,9 +132,9 @@ func getSlot(c *gin.Context) { |
|
|
|
|
|
|
|
|
var slot SlotAPI |
|
|
var slot SlotAPI |
|
|
if err == sql.ErrNoRows { |
|
|
if err == sql.ErrNoRows { |
|
|
slot = newSlotAPI(slotNum, currentBlock.EthBlockNum, nil, auctionVars) |
|
|
|
|
|
|
|
|
slot = a.newSlotAPI(slotNum, currentBlock.EthBlockNum, nil, auctionVars) |
|
|
} else { |
|
|
} else { |
|
|
slot = newSlotAPI(bid.SlotNum, currentBlock.EthBlockNum, &bid, auctionVars) |
|
|
|
|
|
|
|
|
slot = a.newSlotAPI(bid.SlotNum, currentBlock.EthBlockNum, &bid, auctionVars) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// JSON response
|
|
|
// JSON response
|
|
@ -193,7 +193,7 @@ func getLimitsWithAddr(minSlotNum, maxSlotNum *int64, fromItem, limit *uint, ord |
|
|
return minLim, maxLim |
|
|
return minLim, maxLim |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func getSlots(c *gin.Context) { |
|
|
|
|
|
|
|
|
func (a *API) getSlots(c *gin.Context) { |
|
|
var slots []SlotAPI |
|
|
var slots []SlotAPI |
|
|
minSlotNumDflt := int64(0) |
|
|
minSlotNumDflt := int64(0) |
|
|
|
|
|
|
|
@ -211,12 +211,12 @@ func getSlots(c *gin.Context) { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
currentBlock, err := h.GetLastBlock() |
|
|
|
|
|
|
|
|
currentBlock, err := a.h.GetLastBlock() |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
retBadReq(err, c) |
|
|
retBadReq(err, c) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
auctionVars, err := h.GetAuctionVars() |
|
|
|
|
|
|
|
|
auctionVars, err := a.h.GetAuctionVars() |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
retBadReq(err, c) |
|
|
retBadReq(err, c) |
|
|
return |
|
|
return |
|
@ -231,13 +231,13 @@ func getSlots(c *gin.Context) { |
|
|
retBadReq(errors.New("It is necessary to add maxSlotNum filter"), c) |
|
|
retBadReq(errors.New("It is necessary to add maxSlotNum filter"), c) |
|
|
return |
|
|
return |
|
|
} else if *finishedAuction { |
|
|
} else if *finishedAuction { |
|
|
currentBlock, err := h.GetLastBlock() |
|
|
|
|
|
|
|
|
currentBlock, err := a.h.GetLastBlock() |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
retBadReq(err, c) |
|
|
retBadReq(err, c) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
currentSlot := getCurrentSlot(currentBlock.EthBlockNum) |
|
|
|
|
|
auctionVars, err := h.GetAuctionVars() |
|
|
|
|
|
|
|
|
currentSlot := a.getCurrentSlot(currentBlock.EthBlockNum) |
|
|
|
|
|
auctionVars, err := a.h.GetAuctionVars() |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
retBadReq(err, c) |
|
|
retBadReq(err, c) |
|
|
return |
|
|
return |
|
@ -267,7 +267,7 @@ func getSlots(c *gin.Context) { |
|
|
if wonByEthereumAddress == nil { |
|
|
if wonByEthereumAddress == nil { |
|
|
slotMinLim, slotMaxLim = getLimits(minSlotNum, maxSlotNum, fromItem, limit, order) |
|
|
slotMinLim, slotMaxLim = getLimits(minSlotNum, maxSlotNum, fromItem, limit, order) |
|
|
// Get best bids in range maxSlotNum - minSlotNum
|
|
|
// Get best bids in range maxSlotNum - minSlotNum
|
|
|
bids, _, err = h.GetBestBidsAPI(&slotMinLim, &slotMaxLim, wonByEthereumAddress, nil, order) |
|
|
|
|
|
|
|
|
bids, _, err = a.h.GetBestBidsAPI(&slotMinLim, &slotMaxLim, wonByEthereumAddress, nil, order) |
|
|
if err != nil && err != sql.ErrNoRows { |
|
|
if err != nil && err != sql.ErrNoRows { |
|
|
retSQLErr(err, c) |
|
|
retSQLErr(err, c) |
|
|
return |
|
|
return |
|
@ -275,7 +275,7 @@ func getSlots(c *gin.Context) { |
|
|
totalItems = uint64(*maxSlotNum) - uint64(*minSlotNum) + 1 |
|
|
totalItems = uint64(*maxSlotNum) - uint64(*minSlotNum) + 1 |
|
|
} else { |
|
|
} else { |
|
|
slotMinLim, slotMaxLim = getLimitsWithAddr(minSlotNum, maxSlotNum, fromItem, limit, order) |
|
|
slotMinLim, slotMaxLim = getLimitsWithAddr(minSlotNum, maxSlotNum, fromItem, limit, order) |
|
|
bids, pag, err = h.GetBestBidsAPI(&slotMinLim, &slotMaxLim, wonByEthereumAddress, limit, order) |
|
|
|
|
|
|
|
|
bids, pag, err = a.h.GetBestBidsAPI(&slotMinLim, &slotMaxLim, wonByEthereumAddress, limit, order) |
|
|
if err != nil && err != sql.ErrNoRows { |
|
|
if err != nil && err != sql.ErrNoRows { |
|
|
retSQLErr(err, c) |
|
|
retSQLErr(err, c) |
|
|
return |
|
|
return |
|
@ -290,7 +290,7 @@ func getSlots(c *gin.Context) { |
|
|
// Build the slot information with previous bids
|
|
|
// Build the slot information with previous bids
|
|
|
var slotsBids []SlotAPI |
|
|
var slotsBids []SlotAPI |
|
|
if len(bids) > 0 { |
|
|
if len(bids) > 0 { |
|
|
slotsBids = newSlotsAPIFromWinnerBids(fromItem, order, bids, currentBlock.EthBlockNum, auctionVars) |
|
|
|
|
|
|
|
|
slotsBids = a.newSlotsAPIFromWinnerBids(fromItem, order, bids, currentBlock.EthBlockNum, auctionVars) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
retBadReq(err, c) |
|
|
retBadReq(err, c) |
|
|
return |
|
|
return |
|
@ -318,7 +318,7 @@ func getSlots(c *gin.Context) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
if !found { |
|
|
if !found { |
|
|
slots, err = addEmptySlot(slots, i, currentBlock.EthBlockNum, auctionVars, fromItem, order) |
|
|
|
|
|
|
|
|
slots, err = a.addEmptySlot(slots, i, currentBlock.EthBlockNum, auctionVars, fromItem, order) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
retBadReq(err, c) |
|
|
retBadReq(err, c) |
|
|
return |
|
|
return |
|
|