-
Notifications
You must be signed in to change notification settings - Fork 64
/
resolver_funds.go
134 lines (115 loc) · 3.1 KB
/
resolver_funds.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package gql
import (
"context"
"fmt"
"time"
gqltypes "github.com/filecoin-project/boost/gql/types"
smfunds "github.com/filecoin-project/boost/storagemarket/funds"
"github.com/graph-gophers/graphql-go"
)
type fundsEscrow struct {
Available gqltypes.BigInt
Locked gqltypes.BigInt
Tagged gqltypes.BigInt
}
type fundsWallet struct {
Address string
Balance gqltypes.BigInt
Tagged gqltypes.BigInt
}
type funds struct {
Escrow fundsEscrow
Collateral fundsWallet
PubMsg fundsWallet
}
// query: funds: Funds
func (r *resolver) Funds(ctx context.Context) (*funds, error) {
fnds, err := smfunds.GetStatus(ctx, r.fundMgr)
if err != nil {
return nil, err
}
return &funds{
Escrow: fundsEscrow{
Available: gqltypes.BigInt{Int: fnds.Escrow.Available},
Locked: gqltypes.BigInt{Int: fnds.Escrow.Locked},
Tagged: gqltypes.BigInt{Int: fnds.Escrow.Tagged},
},
Collateral: fundsWallet{
Address: fnds.Collateral.Address,
Balance: gqltypes.BigInt{Int: fnds.Collateral.Balance},
},
PubMsg: fundsWallet{
Address: fnds.PubMsg.Address,
Balance: gqltypes.BigInt{Int: fnds.PubMsg.Balance},
Tagged: gqltypes.BigInt{Int: fnds.PubMsg.Tagged},
},
}, nil
}
type fundsLogList struct {
TotalCount int32
Logs []*fundsLogResolver
More bool
}
type fundsLogResolver struct {
CreatedAt graphql.Time
DealUUID graphql.ID
Amount gqltypes.BigInt
Text string
}
type fundsLogsArgs struct {
Cursor *gqltypes.BigInt // CreatedAt in milli-seconds
Offset graphql.NullInt
Limit graphql.NullInt
}
// query: fundsLogs: FundsLogList
func (r *resolver) FundsLogs(ctx context.Context, args fundsLogsArgs) (*fundsLogList, error) {
offset := 0
if args.Offset.Set && args.Offset.Value != nil && *args.Offset.Value > 0 {
offset = int(*args.Offset.Value)
}
limit := 10
if args.Limit.Set && args.Limit.Value != nil && *args.Limit.Value > 0 {
limit = int(*args.Limit.Value)
}
var cursor *time.Time
if args.Cursor != nil {
val := (*args.Cursor).Int64()
asTime := time.Unix(val/1000, (val%1000)*1e6)
cursor = &asTime
}
// Fetch one extra log so that we can check if there are more logs
// beyond the limit
logs, err := r.fundsDB.Logs(ctx, cursor, offset, limit+1)
if err != nil {
return nil, fmt.Errorf("getting funds logs: %w", err)
}
more := len(logs) > limit
if more {
// Truncate log list to limit
logs = logs[:limit]
}
// Get the total log count
count, err := r.fundsDB.LogsCount(ctx)
if err != nil {
return nil, err
}
fundsLogs := make([]*fundsLogResolver, 0, len(logs))
for _, l := range logs {
fundsLogs = append(fundsLogs, &fundsLogResolver{
CreatedAt: graphql.Time{Time: l.CreatedAt},
DealUUID: graphql.ID(l.DealUUID.String()),
Amount: gqltypes.BigInt{Int: l.Amount},
Text: l.Text,
})
}
return &fundsLogList{
Logs: fundsLogs,
TotalCount: int32(count),
More: more,
}, nil
}
// mutation: moveFundsToEscrow(amount): Boolean
func (r *resolver) FundsMoveToEscrow(ctx context.Context, args struct{ Amount gqltypes.BigInt }) (bool, error) {
_, err := r.fundMgr.MoveFundsToEscrow(ctx, args.Amount.Int)
return true, err
}