-
Notifications
You must be signed in to change notification settings - Fork 0
/
claims.go
137 lines (115 loc) · 3.81 KB
/
claims.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
package keeper
import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/furyfoundation/fury-blockchain/x/claims/types"
)
func (k Keeper) SetCollection(ctx sdk.Context, data types.Collection) {
k.Set(ctx, []byte(data.Id), types.CollectionKey, data, k.Marshal)
}
func (k Keeper) GetCollection(ctx sdk.Context, id string) (types.Collection, error) {
val, found := k.Get(ctx, []byte(id), types.CollectionKey, k.UnmarshalCollection)
if !found {
return types.Collection{}, sdkerrors.Wrapf(types.ErrCollectionNotFound, "for %s", id)
}
return val.(types.Collection), nil
}
func (k Keeper) UnmarshalCollection(value []byte) (interface{}, bool) {
data := types.Collection{}
k.Unmarshal(value, &data)
return data, types.IsValidCollection(&data)
}
func (k Keeper) Marshal(value interface{}) (bytes []byte) {
switch value := value.(type) {
case types.Collection:
bytes = k.cdc.MustMarshal(&value)
case types.Claim:
bytes = k.cdc.MustMarshal(&value)
case types.Dispute:
bytes = k.cdc.MustMarshal(&value)
}
return
}
// Unmarshal unmarshal a byte slice to a struct, return false in case of errors
func (k Keeper) Unmarshal(data []byte, val codec.ProtoMarshaler) bool {
if len(data) == 0 {
return false
}
if err := k.cdc.Unmarshal(data, val); err != nil {
return false
}
return true
}
func (k Keeper) SetClaim(ctx sdk.Context, data types.Claim) {
k.Set(ctx, []byte(data.ClaimId), types.ClaimKey, data, k.Marshal)
}
func (k Keeper) GetClaim(ctx sdk.Context, id string) (types.Claim, error) {
val, found := k.Get(ctx, []byte(id), types.ClaimKey, k.UnmarshalClaim)
if !found {
return types.Claim{}, sdkerrors.Wrapf(types.ErrClaimNotFound, "for %s", id)
}
return val.(types.Claim), nil
}
func (k Keeper) UnmarshalClaim(value []byte) (interface{}, bool) {
data := types.Claim{}
k.Unmarshal(value, &data)
return data, types.IsValidClaim(&data)
}
func (k Keeper) SetDispute(ctx sdk.Context, data types.Dispute) {
k.Set(ctx, []byte(data.Data.Proof), types.DisputeKey, data, k.Marshal)
}
func (k Keeper) GetDispute(ctx sdk.Context, proof string) (types.Dispute, error) {
val, found := k.Get(ctx, []byte(proof), types.DisputeKey, k.UnmarshalDispute)
if !found {
return types.Dispute{}, sdkerrors.Wrapf(types.ErrDisputeNotFound, "for proof %s", proof)
}
return val.(types.Dispute), nil
}
func (k Keeper) UnmarshalDispute(value []byte) (interface{}, bool) {
data := types.Dispute{}
k.Unmarshal(value, &data)
return data, types.IsValidDispute(&data)
}
func (k Keeper) GetCollectionsIterator(ctx sdk.Context) sdk.Iterator {
return k.GetAll(ctx, types.CollectionKey)
}
func (k Keeper) GetCollections(ctx sdk.Context) []types.Collection {
iterator := k.GetCollectionsIterator(ctx)
collections := []types.Collection{}
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var c types.Collection
k.cdc.MustUnmarshal(iterator.Value(), &c)
collections = append(collections, c)
}
return collections
}
func (k Keeper) GetClaimsIterator(ctx sdk.Context) sdk.Iterator {
return k.GetAll(ctx, types.ClaimKey)
}
func (k Keeper) GetClaims(ctx sdk.Context) []types.Claim {
iterator := k.GetClaimsIterator(ctx)
claims := []types.Claim{}
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var c types.Claim
k.cdc.MustUnmarshal(iterator.Value(), &c)
claims = append(claims, c)
}
return claims
}
func (k Keeper) GetDisputesIterator(ctx sdk.Context) sdk.Iterator {
return k.GetAll(ctx, types.DisputeKey)
}
func (k Keeper) GetDisputes(ctx sdk.Context) []types.Dispute {
iterator := k.GetDisputesIterator(ctx)
disputes := []types.Dispute{}
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var d types.Dispute
k.cdc.MustUnmarshal(iterator.Value(), &d)
disputes = append(disputes, d)
}
return disputes
}