fix(blocks): Improve paged response
Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
parent
d01fefbfbf
commit
2f0f4a9fde
1 changed files with 22 additions and 10 deletions
32
client.go
32
client.go
|
@ -188,7 +188,8 @@ func (f *FlexpoolClient) MinerWorkers(coin string, address string) (workers []*W
|
||||||
type BlocksResponse struct {
|
type BlocksResponse struct {
|
||||||
Error string `json:"error"`
|
Error string `json:"error"`
|
||||||
Result struct {
|
Result struct {
|
||||||
Data []struct {
|
TotalPages int `json:"totalPages"`
|
||||||
|
Data []struct {
|
||||||
Hash string `json:"hash"`
|
Hash string `json:"hash"`
|
||||||
Number uint64 `json:"number"`
|
Number uint64 `json:"number"`
|
||||||
Reward float64 `json:"reward"`
|
Reward float64 `json:"reward"`
|
||||||
|
@ -199,7 +200,9 @@ type BlocksResponse struct {
|
||||||
// PoolBlocks returns an ordered list of blocks
|
// PoolBlocks returns an ordered list of blocks
|
||||||
func (f *FlexpoolClient) PoolBlocks(coin string, limit int) (blocks []*Block, err error) {
|
func (f *FlexpoolClient) PoolBlocks(coin string, limit int) (blocks []*Block, err error) {
|
||||||
page := 0
|
page := 0
|
||||||
for {
|
totalPages := 0
|
||||||
|
|
||||||
|
for page <= MaxIterations && len(blocks) < limit {
|
||||||
body, err := f.request(fmt.Sprintf("%s/pool/blocks/?coin=%s&page=%d", FlexpoolAPIURL, coin, page))
|
body, err := f.request(fmt.Sprintf("%s/pool/blocks/?coin=%s&page=%d", FlexpoolAPIURL, coin, page))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -208,6 +211,10 @@ func (f *FlexpoolClient) PoolBlocks(coin string, limit int) (blocks []*Block, er
|
||||||
var response BlocksResponse
|
var response BlocksResponse
|
||||||
json.Unmarshal(body, &response)
|
json.Unmarshal(body, &response)
|
||||||
|
|
||||||
|
if totalPages == 0 {
|
||||||
|
totalPages = response.Result.TotalPages
|
||||||
|
}
|
||||||
|
|
||||||
for _, result := range response.Result.Data {
|
for _, result := range response.Result.Data {
|
||||||
block := NewBlock(
|
block := NewBlock(
|
||||||
result.Hash,
|
result.Hash,
|
||||||
|
@ -216,18 +223,23 @@ func (f *FlexpoolClient) PoolBlocks(coin string, limit int) (blocks []*Block, er
|
||||||
)
|
)
|
||||||
blocks = append(blocks, block)
|
blocks = append(blocks, block)
|
||||||
if len(blocks) >= limit {
|
if len(blocks) >= limit {
|
||||||
// sort by number
|
break
|
||||||
sort.Slice(blocks, func(b1, b2 int) bool {
|
|
||||||
return blocks[b1].Number < blocks[b2].Number
|
|
||||||
})
|
|
||||||
return blocks, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
page = page + 1
|
page++
|
||||||
if page > MaxIterations {
|
if page >= totalPages {
|
||||||
return nil, fmt.Errorf("Max iterations of %d reached", MaxIterations)
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if page > MaxIterations {
|
||||||
|
return nil, fmt.Errorf("Max iterations of %d reached", MaxIterations)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort by number
|
||||||
|
sort.Slice(blocks, func(b1, b2 int) bool {
|
||||||
|
return blocks[b1].Number < blocks[b2].Number
|
||||||
|
})
|
||||||
|
return blocks, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// LastPoolBlock return the last discovered block for a given pool
|
// LastPoolBlock return the last discovered block for a given pool
|
||||||
|
|
Reference in a new issue