-
Notifications
You must be signed in to change notification settings - Fork 16
/
getters_funding.go
153 lines (126 loc) · 4.68 KB
/
getters_funding.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package keeper
import (
"github.com/KYVENetwork/chain/x/funders/types"
queryTypes "github.com/KYVENetwork/chain/x/query/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// DoesFundingExist checks if the funding exists
func (k Keeper) DoesFundingExist(ctx sdk.Context, funderAddress string, poolId uint64) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.FundingKeyPrefixByFunder)
return store.Has(types.FundingKeyByFunder(funderAddress, poolId))
}
// GetFunding returns the funding
func (k Keeper) GetFunding(ctx sdk.Context, funderAddress string, poolId uint64) (funding types.Funding, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.FundingKeyPrefixByFunder)
b := store.Get(types.FundingKeyByFunder(
funderAddress,
poolId,
))
if b == nil {
return funding, false
}
k.cdc.MustUnmarshal(b, &funding)
return funding, true
}
// GetFundingsOfFunder returns all fundings of a funder
func (k Keeper) GetFundingsOfFunder(ctx sdk.Context, funderAddress string) (fundings []types.Funding) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.FundingKeyPrefixByFunder)
iterator := sdk.KVStorePrefixIterator(store, types.FundingKeyByFunderIter(funderAddress))
//goland:noinspection GoUnhandledErrorResult
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var funding types.Funding
k.cdc.MustUnmarshal(iterator.Value(), &funding)
fundings = append(fundings, funding)
}
return fundings
}
// GetFundingsOfPool returns all fundings of a pool
func (k Keeper) GetFundingsOfPool(ctx sdk.Context, poolId uint64) (fundings []types.Funding) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.FundingKeyPrefixByPool)
iterator := sdk.KVStorePrefixIterator(store, types.FundingKeyByPoolIter(poolId))
//goland:noinspection GoUnhandledErrorResult
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var funding types.Funding
k.cdc.MustUnmarshal(iterator.Value(), &funding)
fundings = append(fundings, funding)
}
return fundings
}
// GetAllFundings returns all fundings
func (k Keeper) GetAllFundings(ctx sdk.Context) (fundings []types.Funding) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.FundingKeyPrefixByFunder)
iterator := sdk.KVStorePrefixIterator(store, []byte{})
//goland:noinspection GoUnhandledErrorResult
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.Funding
k.cdc.MustUnmarshal(iterator.Value(), &val)
fundings = append(fundings, val)
}
return fundings
}
// SetFunding sets a specific funding in the store from its index
func (k Keeper) SetFunding(ctx sdk.Context, funding *types.Funding) {
b := k.cdc.MustMarshal(funding)
storeByFunder := prefix.NewStore(ctx.KVStore(k.storeKey), types.FundingKeyPrefixByFunder)
storeByFunder.Set(types.FundingKeyByFunder(
funding.FunderAddress,
funding.PoolId,
), b)
storeByPool := prefix.NewStore(ctx.KVStore(k.storeKey), types.FundingKeyPrefixByPool)
storeByPool.Set(types.FundingKeyByPool(
funding.FunderAddress,
funding.PoolId,
), b)
}
// GetPaginatedFundingQuery performs a full search on all fundings with the given parameters.
// Requires either funderAddress or poolId to be provided.
func (k Keeper) GetPaginatedFundingQuery(
ctx sdk.Context,
pagination *query.PageRequest,
funderAddress *string,
poolId *uint64,
fundingStatus queryTypes.FundingStatus,
) ([]types.Funding, *query.PageResponse, error) {
if funderAddress == nil && poolId == nil {
return nil, nil, status.Error(codes.InvalidArgument, "either funderAddress or poolId must be provided")
}
keyPrefix := types.FundingKeyPrefixByFunder
if funderAddress == nil {
keyPrefix = types.FundingKeyPrefixByPool
}
var fundings []types.Funding
store := prefix.NewStore(ctx.KVStore(k.storeKey), keyPrefix)
pageRes, err := query.FilteredPaginate(store, pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
var funding types.Funding
if err := k.cdc.Unmarshal(value, &funding); err != nil {
return false, err
}
if funderAddress != nil && *funderAddress != funding.FunderAddress {
return false, nil
}
if poolId != nil && *poolId != funding.PoolId {
return false, nil
}
if fundingStatus == queryTypes.FUNDING_STATUS_ACTIVE && funding.IsInactive() {
return false, nil
}
if fundingStatus == queryTypes.FUNDING_STATUS_INACTIVE && funding.IsActive() {
return false, nil
}
if accumulate {
fundings = append(fundings, funding)
}
return true, nil
})
if err != nil {
return nil, nil, status.Error(codes.Internal, err.Error())
}
return fundings, pageRes, nil
}