forked from irinazheltisheva/powergate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasks.go
More file actions
61 lines (55 loc) · 1.57 KB
/
asks.go
File metadata and controls
61 lines (55 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package client
import (
"context"
"time"
"github.com/textileio/powergate/index/ask"
"github.com/textileio/powergate/index/ask/rpc"
)
// Asks provides an API for viewing asks data.
type Asks struct {
client rpc.RPCServiceClient
}
// Get returns the current index of available asks.
func (a *Asks) Get(ctx context.Context) (*ask.Index, error) {
reply, err := a.client.Get(ctx, &rpc.GetRequest{})
if err != nil {
return nil, err
}
lastUpdated := time.Unix(reply.GetIndex().GetLastUpdated(), 0)
storage := make(map[string]ask.StorageAsk, len(reply.GetIndex().GetStorage()))
for key, val := range reply.GetIndex().GetStorage() {
storage[key] = askFromPbAsk(val)
}
return &ask.Index{
LastUpdated: lastUpdated,
StorageMedianPrice: reply.GetIndex().StorageMedianPrice,
Storage: storage,
}, nil
}
// Query executes a query to retrieve active Asks.
func (a *Asks) Query(ctx context.Context, query ask.Query) ([]ask.StorageAsk, error) {
q := &rpc.Query{
MaxPrice: query.MaxPrice,
PieceSize: query.PieceSize,
Limit: int32(query.Limit),
Offset: int32(query.Offset),
}
reply, err := a.client.Query(ctx, &rpc.QueryRequest{Query: q})
if err != nil {
return nil, err
}
asks := make([]ask.StorageAsk, len(reply.GetAsks()))
for i, a := range reply.GetAsks() {
asks[i] = askFromPbAsk(a)
}
return asks, nil
}
func askFromPbAsk(a *rpc.StorageAsk) ask.StorageAsk {
return ask.StorageAsk{
Price: a.GetPrice(),
MinPieceSize: a.GetMinPieceSize(),
Miner: a.GetMiner(),
Timestamp: a.GetTimestamp(),
Expiry: a.GetExpiry(),
}
}