mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Coordinators methods for API
This commit is contained in:
@@ -915,3 +915,53 @@ func (hdb *HistoryDB) AddBlockSCData(blockData *BlockData) (err error) {
|
||||
|
||||
return txn.Commit()
|
||||
}
|
||||
|
||||
// GetCoordinator returns a coordinator by its bidderAddr
|
||||
func (hdb *HistoryDB) GetCoordinator(bidderAddr ethCommon.Address) (*HistoryCoordinator, error) {
|
||||
coordinator := &HistoryCoordinator{}
|
||||
err := meddler.QueryRow(
|
||||
hdb.db, coordinator, `SELECT * FROM coordinator WHERE bidder_addr = $1;`, bidderAddr,
|
||||
)
|
||||
return coordinator, err
|
||||
}
|
||||
|
||||
// GetCoordinators returns a list of coordinators from the DB and pagination info
|
||||
func (hdb *HistoryDB) GetCoordinators(fromItem, limit *uint, order string) ([]HistoryCoordinator, *db.Pagination, error) {
|
||||
var query string
|
||||
var args []interface{}
|
||||
queryStr := `SELECT coordinator.*,
|
||||
COUNT(*) OVER() AS total_items, MIN(coordinator.item_id) OVER() AS first_item, MAX(coordinator.item_id) OVER() AS last_item
|
||||
FROM coordinator `
|
||||
// Apply filters
|
||||
if fromItem != nil {
|
||||
queryStr += "WHERE "
|
||||
if order == OrderAsc {
|
||||
queryStr += "coordinator.item_id >= ? "
|
||||
} else {
|
||||
queryStr += "coordinator.item_id <= ? "
|
||||
}
|
||||
args = append(args, fromItem)
|
||||
}
|
||||
// pagination
|
||||
queryStr += "ORDER BY coordinator.item_id "
|
||||
if order == OrderAsc {
|
||||
queryStr += " ASC "
|
||||
} else {
|
||||
queryStr += " DESC "
|
||||
}
|
||||
queryStr += fmt.Sprintf("LIMIT %d;", *limit)
|
||||
query = hdb.db.Rebind(queryStr)
|
||||
|
||||
coordinators := []*HistoryCoordinator{}
|
||||
if err := meddler.QueryAll(hdb.db, &coordinators, query, args...); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if len(coordinators) == 0 {
|
||||
return nil, nil, sql.ErrNoRows
|
||||
}
|
||||
return db.SlicePtrsToSlice(coordinators).([]HistoryCoordinator), &db.Pagination{
|
||||
TotalItems: coordinators[0].TotalItems,
|
||||
FirstItem: coordinators[0].FirstItem,
|
||||
LastItem: coordinators[0].LastItem,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -118,3 +118,16 @@ type HistoryExit struct {
|
||||
TokenUSD *float64 `meddler:"usd"`
|
||||
TokenUSDUpdate *time.Time `meddler:"usd_update"`
|
||||
}
|
||||
|
||||
// HistoryCoordinator is a representation of a coordinator with additional information
|
||||
// required by the API
|
||||
type HistoryCoordinator struct {
|
||||
ItemID int `meddler:"item_id"`
|
||||
Bidder ethCommon.Address `meddler:"bidder_addr"`
|
||||
Forger ethCommon.Address `meddler:"forger_addr"`
|
||||
EthBlockNum int64 `meddler:"eth_block_num"`
|
||||
URL string `meddler:"url"`
|
||||
TotalItems int `meddler:"total_items"`
|
||||
FirstItem int `meddler:"first_item"`
|
||||
LastItem int `meddler:"last_item"`
|
||||
}
|
||||
|
||||
@@ -8,11 +8,11 @@ CREATE TABLE block (
|
||||
);
|
||||
|
||||
CREATE TABLE coordinator (
|
||||
item_id SERIAL PRIMARY KEY,
|
||||
bidder_addr BYTEA NOT NULL,
|
||||
forger_addr BYTEA NOT NULL,
|
||||
eth_block_num BIGINT NOT NULL REFERENCES block (eth_block_num) ON DELETE CASCADE,
|
||||
url VARCHAR(200) NOT NULL,
|
||||
PRIMARY KEY (bidder_addr, eth_block_num)
|
||||
url VARCHAR(200) NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE batch (
|
||||
|
||||
Reference in New Issue
Block a user