-
Notifications
You must be signed in to change notification settings - Fork 11
/
feeshare.go
155 lines (134 loc) · 4.38 KB
/
feeshare.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
154
155
package keeper
import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/Source-Protocol-Cosmos/source/v3/x/feeshare/types"
)
// GetFeeShares returns all registered FeeShares.
func (k Keeper) GetFeeShares(ctx sdk.Context) []types.FeeShare {
feeshares := []types.FeeShare{}
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixFeeShare)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var feeshare types.FeeShare
k.cdc.MustUnmarshal(iterator.Value(), &feeshare)
feeshares = append(feeshares, feeshare)
}
return feeshares
}
// IterateFeeShares iterates over all registered contracts and performs a
// callback with the corresponding FeeShare.
func (k Keeper) IterateFeeShares(
ctx sdk.Context,
handlerFn func(fee types.FeeShare) (stop bool),
) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixFeeShare)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var feeshare types.FeeShare
k.cdc.MustUnmarshal(iterator.Value(), &feeshare)
if handlerFn(feeshare) {
break
}
}
}
// GetFeeShare returns the FeeShare for a registered contract
func (k Keeper) GetFeeShare(
ctx sdk.Context,
contract sdk.Address,
) (types.FeeShare, bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixFeeShare)
bz := store.Get(contract.Bytes())
if len(bz) == 0 {
return types.FeeShare{}, false
}
var feeshare types.FeeShare
k.cdc.MustUnmarshal(bz, &feeshare)
return feeshare, true
}
// SetFeeShare stores the FeeShare for a registered contract.
func (k Keeper) SetFeeShare(ctx sdk.Context, feeshare types.FeeShare) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixFeeShare)
key := feeshare.GetContractAddr()
bz := k.cdc.MustMarshal(&feeshare)
store.Set(key.Bytes(), bz)
}
// DeleteFeeShare deletes a FeeShare of a registered contract.
func (k Keeper) DeleteFeeShare(ctx sdk.Context, fee types.FeeShare) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixFeeShare)
key := fee.GetContractAddr()
store.Delete(key.Bytes())
}
// SetDeployerMap stores a contract-by-deployer mapping
func (k Keeper) SetDeployerMap(
ctx sdk.Context,
deployer sdk.AccAddress,
contract sdk.Address,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixDeployer)
key := append(deployer.Bytes(), contract.Bytes()...)
store.Set(key, []byte{1})
}
// DeleteDeployerMap deletes a contract-by-deployer mapping
func (k Keeper) DeleteDeployerMap(
ctx sdk.Context,
deployer sdk.AccAddress,
contract sdk.Address,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixDeployer)
key := append(deployer.Bytes(), contract.Bytes()...)
store.Delete(key)
}
// SetWithdrawerMap stores a contract-by-withdrawer mapping
func (k Keeper) SetWithdrawerMap(
ctx sdk.Context,
withdrawer sdk.AccAddress,
contract sdk.Address,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixWithdrawer)
key := append(withdrawer.Bytes(), contract.Bytes()...)
store.Set(key, []byte{1})
}
// DeleteWithdrawMap deletes a contract-by-withdrawer mapping
func (k Keeper) DeleteWithdrawerMap(
ctx sdk.Context,
withdrawer sdk.AccAddress,
contract sdk.Address,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixWithdrawer)
key := append(withdrawer.Bytes(), contract.Bytes()...)
store.Delete(key)
}
// IsFeeShareRegistered checks if a contract was registered for receiving
// transaction fees
func (k Keeper) IsFeeShareRegistered(
ctx sdk.Context,
contract sdk.Address,
) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixFeeShare)
return store.Has(contract.Bytes())
}
// IsDeployerMapSet checks if a given contract-by-withdrawer mapping is set in
// store
func (k Keeper) IsDeployerMapSet(
ctx sdk.Context,
deployer sdk.AccAddress,
contract sdk.Address,
) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixDeployer)
key := append(deployer.Bytes(), contract.Bytes()...)
return store.Has(key)
}
// IsWithdrawerMapSet checks if a give contract-by-withdrawer mapping is set in
// store
func (k Keeper) IsWithdrawerMapSet(
ctx sdk.Context,
withdrawer sdk.AccAddress,
contract sdk.Address,
) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixWithdrawer)
key := append(withdrawer.Bytes(), contract.Bytes()...)
return store.Has(key)
}