mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
API add bids endpoint
This commit is contained in:
@@ -339,16 +339,89 @@ func (hdb *HistoryDB) addBids(d meddler.DB, bids []common.Bid) error {
|
||||
)
|
||||
}
|
||||
|
||||
// GetBids return the bids
|
||||
func (hdb *HistoryDB) GetBids() ([]common.Bid, error) {
|
||||
// GetAllBids retrieve all bids from the DB
|
||||
func (hdb *HistoryDB) GetAllBids() ([]common.Bid, error) {
|
||||
var bids []*common.Bid
|
||||
err := meddler.QueryAll(
|
||||
hdb.db, &bids,
|
||||
"SELECT * FROM bid;",
|
||||
`SELECT bid.slot_num, bid.bid_value, bid.eth_block_num, bid.bidder_addr FROM bid;`,
|
||||
)
|
||||
return db.SlicePtrsToSlice(bids).([]common.Bid), err
|
||||
}
|
||||
|
||||
// GetBidsAPI return the bids applying the given filters
|
||||
func (hdb *HistoryDB) GetBidsAPI(slotNum *uint, forgerAddr *ethCommon.Address, fromItem, limit *uint, order string) ([]BidAPI, *db.Pagination, error) {
|
||||
var query string
|
||||
var args []interface{}
|
||||
queryStr := `SELECT bid.*, block.timestamp, coordinator.forger_addr, coordinator.url,
|
||||
COUNT(*) OVER() AS total_items, MIN(bid.item_id) OVER() AS first_item,
|
||||
MAX(bid.item_id) OVER() AS last_item FROM bid
|
||||
INNER JOIN block ON bid.eth_block_num = block.eth_block_num
|
||||
INNER JOIN coordinator ON bid.bidder_addr = coordinator.bidder_addr `
|
||||
// Apply filters
|
||||
nextIsAnd := false
|
||||
// slotNum filter
|
||||
if slotNum != nil {
|
||||
if nextIsAnd {
|
||||
queryStr += "AND "
|
||||
} else {
|
||||
queryStr += "WHERE "
|
||||
}
|
||||
queryStr += "bid.slot_num = ? "
|
||||
args = append(args, slotNum)
|
||||
nextIsAnd = true
|
||||
}
|
||||
// slotNum filter
|
||||
if forgerAddr != nil {
|
||||
if nextIsAnd {
|
||||
queryStr += "AND "
|
||||
} else {
|
||||
queryStr += "WHERE "
|
||||
}
|
||||
queryStr += "bid.bidder_addr = ? "
|
||||
args = append(args, forgerAddr)
|
||||
nextIsAnd = true
|
||||
}
|
||||
if fromItem != nil {
|
||||
if nextIsAnd {
|
||||
queryStr += "AND "
|
||||
} else {
|
||||
queryStr += "WHERE "
|
||||
}
|
||||
if order == OrderAsc {
|
||||
queryStr += "bid.item_id >= ? "
|
||||
} else {
|
||||
queryStr += "bid.item_id <= ? "
|
||||
}
|
||||
args = append(args, fromItem)
|
||||
}
|
||||
// pagination
|
||||
queryStr += "ORDER BY bid.item_id "
|
||||
if order == OrderAsc {
|
||||
queryStr += "ASC "
|
||||
} else {
|
||||
queryStr += "DESC "
|
||||
}
|
||||
queryStr += fmt.Sprintf("LIMIT %d;", *limit)
|
||||
query, argsQ, err := sqlx.In(queryStr, args...)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
query = hdb.db.Rebind(query)
|
||||
bids := []*BidAPI{}
|
||||
if err := meddler.QueryAll(hdb.db, &bids, query, argsQ...); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if len(bids) == 0 {
|
||||
return nil, nil, sql.ErrNoRows
|
||||
}
|
||||
return db.SlicePtrsToSlice(bids).([]BidAPI), &db.Pagination{
|
||||
TotalItems: bids[0].TotalItems,
|
||||
FirstItem: bids[0].FirstItem,
|
||||
LastItem: bids[0].LastItem,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// AddCoordinators insert Coordinators into the DB
|
||||
func (hdb *HistoryDB) AddCoordinators(coordinators []common.Coordinator) error {
|
||||
return hdb.addCoordinators(hdb.db, coordinators)
|
||||
|
||||
@@ -159,7 +159,7 @@ func TestBids(t *testing.T) {
|
||||
err = historyDB.AddBids(bids)
|
||||
assert.NoError(t, err)
|
||||
// Fetch bids
|
||||
fetchedBids, err := historyDB.GetBids()
|
||||
fetchedBids, err := historyDB.GetAllBids()
|
||||
assert.NoError(t, err)
|
||||
// Compare fetched bids vs generated bids
|
||||
for i, bid := range fetchedBids {
|
||||
|
||||
@@ -227,3 +227,19 @@ type Metrics struct {
|
||||
TotalBJJs int64 `json:"totalBJJs"`
|
||||
AvgTransactionFee float64 `json:"avgTransactionFee"`
|
||||
}
|
||||
|
||||
// BidAPI is a representation of a bid with additional information
|
||||
// required by the API
|
||||
type BidAPI struct {
|
||||
ItemID int `json:"itemId" meddler:"item_id"`
|
||||
SlotNum int64 `json:"slotNum" meddler:"slot_num"`
|
||||
BidValue apitypes.BigIntStr `json:"bidValue" meddler:"bid_value"`
|
||||
EthBlockNum int64 `json:"ethereumBlockNum" meddler:"eth_block_num"`
|
||||
Bidder ethCommon.Address `json:"bidderAddr" meddler:"bidder_addr"`
|
||||
Forger ethCommon.Address `json:"forgerAddr" meddler:"forger_addr"`
|
||||
URL string `json:"URL" meddler:"url"`
|
||||
Timestamp time.Time `json:"timestamp" meddler:"timestamp,utctime"`
|
||||
TotalItems int `json:"-" meddler:"total_items"`
|
||||
FirstItem int `json:"-" meddler:"first_item"`
|
||||
LastItem int `json:"-" meddler:"last_item"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user