1
1
package keeper
2
2
3
3
import (
4
+ sdkmath "cosmossdk.io/math"
4
5
sdk "github.com/cosmos/cosmos-sdk/types"
5
6
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
6
7
7
8
"github.com/bandprotocol/chain/v2/x/restake/types"
8
9
)
9
10
11
+ func (k Keeper ) GetOrCreateKey (ctx sdk.Context , keyName string ) (types.Key , error ) {
12
+ key , err := k .GetKey (ctx , keyName )
13
+ if err != nil {
14
+ keyAccAddr , err := k .createKeyAccount (ctx , keyName )
15
+ if err != nil {
16
+ return types.Key {}, err
17
+ }
18
+
19
+ key = types.Key {
20
+ Name : keyName ,
21
+ PoolAddress : keyAccAddr .String (),
22
+ IsActive : true ,
23
+ TotalPower : sdkmath .NewInt (0 ),
24
+ RewardPerPowers : sdk .NewDecCoins (),
25
+ Remainders : sdk .NewDecCoins (),
26
+ }
27
+
28
+ k .SetKey (ctx , key )
29
+ }
30
+
31
+ return key , nil
32
+ }
33
+
10
34
// AddRewards adds rewards to the pool address and re-calculate reward_per_share of the key
11
35
func (k Keeper ) AddRewards (ctx sdk.Context , sender sdk.AccAddress , keyName string , rewards sdk.Coins ) error {
12
36
key , err := k .GetKey (ctx , keyName )
@@ -28,7 +52,7 @@ func (k Keeper) AddRewards(ctx sdk.Context, sender sdk.AccAddress, keyName strin
28
52
}
29
53
30
54
decRewards := sdk .NewDecCoinsFromCoins (rewards .Sort ()... )
31
- totalPower := sdk . NewDecFromInt (key .TotalPower )
55
+ totalPower := sdkmath . LegacyNewDecFromInt (key .TotalPower )
32
56
rewardPerPowers := decRewards .QuoDecTruncate (totalPower )
33
57
truncatedRewards := decRewards .Sub (rewardPerPowers .MulDecTruncate (totalPower ))
34
58
@@ -50,29 +74,39 @@ func (k Keeper) AddRewards(ctx sdk.Context, sender sdk.AccAddress, keyName strin
50
74
return nil
51
75
}
52
76
53
- func (k Keeper ) GetOrCreateKey (ctx sdk.Context , keyName string ) (types. Key , error ) {
77
+ func (k Keeper ) IsActiveKey (ctx sdk.Context , keyName string ) bool {
54
78
key , err := k .GetKey (ctx , keyName )
55
79
if err != nil {
56
- keyAccAddr , err := k .CreateKeyAccount (ctx , keyName )
57
- if err != nil {
58
- return types.Key {}, err
59
- }
80
+ return false
81
+ }
60
82
61
- key = types.Key {
62
- Name : keyName ,
63
- PoolAddress : keyAccAddr .String (),
64
- IsActive : true ,
65
- TotalPower : sdk .NewInt (0 ),
66
- RewardPerPowers : sdk .NewDecCoins (),
67
- }
83
+ return key .IsActive
84
+ }
68
85
69
- k .SetKey (ctx , key )
86
+ func (k Keeper ) DeactivateKey (ctx sdk.Context , keyName string ) error {
87
+ key , err := k .GetKey (ctx , keyName )
88
+ if err != nil {
89
+ return err
70
90
}
71
91
72
- return key , nil
92
+ if ! key .IsActive {
93
+ return types .ErrKeyAlreadyDeactivated
94
+ }
95
+
96
+ key .IsActive = false
97
+ k .SetKey (ctx , key )
98
+
99
+ ctx .EventManager ().EmitEvent (
100
+ sdk .NewEvent (
101
+ types .EventTypeDeactivateKey ,
102
+ sdk .NewAttribute (types .AttributeKeyKey , keyName ),
103
+ ),
104
+ )
105
+
106
+ return nil
73
107
}
74
108
75
- func (k Keeper ) CreateKeyAccount (ctx sdk.Context , key string ) (sdk.AccAddress , error ) {
109
+ func (k Keeper ) createKeyAccount (ctx sdk.Context , key string ) (sdk.AccAddress , error ) {
76
110
header := ctx .BlockHeader ()
77
111
78
112
buf := []byte (key )
@@ -105,38 +139,6 @@ func (k Keeper) CreateKeyAccount(ctx sdk.Context, key string) (sdk.AccAddress, e
105
139
return keyAccAddr , nil
106
140
}
107
141
108
- func (k Keeper ) IsActiveKey (ctx sdk.Context , keyName string ) bool {
109
- key , err := k .GetKey (ctx , keyName )
110
- if err != nil {
111
- return false
112
- }
113
-
114
- return key .IsActive
115
- }
116
-
117
- func (k Keeper ) DeactivateKey (ctx sdk.Context , keyName string ) error {
118
- key , err := k .GetKey (ctx , keyName )
119
- if err != nil {
120
- return err
121
- }
122
-
123
- if ! key .IsActive {
124
- return types .ErrKeyAlreadyDeactivated
125
- }
126
-
127
- key .IsActive = false
128
- k .SetKey (ctx , key )
129
-
130
- ctx .EventManager ().EmitEvent (
131
- sdk .NewEvent (
132
- types .EventTypeDeactivateKey ,
133
- sdk .NewAttribute (types .AttributeKeyKey , keyName ),
134
- ),
135
- )
136
-
137
- return nil
138
- }
139
-
140
142
// -------------------------------
141
143
// store part
142
144
// -------------------------------
0 commit comments