-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
deposit.go
330 lines (288 loc) · 11.1 KB
/
deposit.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package keeper
import (
"context"
"fmt"
"strings"
"cosmossdk.io/collections"
"cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
disttypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
"github.com/cosmos/cosmos-sdk/x/gov/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
)
// SetDeposit sets a Deposit to the gov store
func (keeper Keeper) SetDeposit(ctx context.Context, deposit v1.Deposit) error {
depositor, err := keeper.authKeeper.AddressCodec().StringToBytes(deposit.Depositor)
if err != nil {
return err
}
return keeper.Deposits.Set(ctx, collections.Join(deposit.ProposalId, sdk.AccAddress(depositor)), deposit)
}
// GetDeposits returns all the deposits of a proposal
func (keeper Keeper) GetDeposits(ctx context.Context, proposalID uint64) (deposits v1.Deposits, err error) {
err = keeper.IterateDeposits(ctx, proposalID, func(_ collections.Pair[uint64, sdk.AccAddress], deposit v1.Deposit) (bool, error) {
deposits = append(deposits, &deposit)
return false, nil
})
return deposits, err
}
// DeleteAndBurnDeposits deletes and burns all the deposits on a specific proposal.
func (keeper Keeper) DeleteAndBurnDeposits(ctx context.Context, proposalID uint64) error {
coinsToBurn := sdk.NewCoins()
err := keeper.IterateDeposits(ctx, proposalID, func(key collections.Pair[uint64, sdk.AccAddress], deposit v1.Deposit) (stop bool, err error) {
coinsToBurn = coinsToBurn.Add(deposit.Amount...)
return false, keeper.Deposits.Remove(ctx, key)
})
if err != nil {
return err
}
return keeper.bankKeeper.BurnCoins(ctx, types.ModuleName, coinsToBurn)
}
// IterateDeposits iterates over all the proposals deposits and performs a callback function
func (keeper Keeper) IterateDeposits(ctx context.Context, proposalID uint64, cb func(key collections.Pair[uint64, sdk.AccAddress], value v1.Deposit) (bool, error)) error {
rng := collections.NewPrefixedPairRange[uint64, sdk.AccAddress](proposalID)
err := keeper.Deposits.Walk(ctx, rng, cb)
if err != nil {
return err
}
return nil
}
// AddDeposit adds or updates a deposit of a specific depositor on a specific proposal.
// Activates voting period when appropriate and returns true in that case, else returns false.
func (keeper Keeper) AddDeposit(ctx context.Context, proposalID uint64, depositorAddr sdk.AccAddress, depositAmount sdk.Coins) (bool, error) {
// Checks to see if proposal exists
proposal, err := keeper.Proposals.Get(ctx, proposalID)
if err != nil {
return false, err
}
// Check if proposal is still depositable
if (proposal.Status != v1.StatusDepositPeriod) && (proposal.Status != v1.StatusVotingPeriod) {
return false, errors.Wrapf(types.ErrInactiveProposal, "%d", proposalID)
}
// Check coins to be deposited match the proposal's deposit params
params, err := keeper.Params.Get(ctx)
if err != nil {
return false, err
}
minDepositAmount := proposal.GetMinDepositFromParams(params)
minDepositRatio, err := sdkmath.LegacyNewDecFromStr(params.GetMinDepositRatio())
if err != nil {
return false, err
}
// the deposit must only contain valid denoms (listed in the min deposit param)
if err := keeper.validateDepositDenom(ctx, params, depositAmount); err != nil {
return false, err
}
// If minDepositRatio is set, the deposit must be equal or greater than minDepositAmount*minDepositRatio
// for at least one denom. If minDepositRatio is zero we skip this check.
if !minDepositRatio.IsZero() {
var (
depositThresholdMet bool
thresholds []string
)
for _, minDep := range minDepositAmount {
// calculate the threshold for this denom, and hold a list to later return a useful error message
threshold := sdk.NewCoin(minDep.GetDenom(), minDep.Amount.ToLegacyDec().Mul(minDepositRatio).TruncateInt())
thresholds = append(thresholds, threshold.String())
found, deposit := depositAmount.Find(minDep.Denom)
if !found { // if not found, continue, as we know the deposit contains at least 1 valid denom
continue
}
// Once we know at least one threshold has been met, we can break. The deposit
// might contain other denoms but we don't care.
if deposit.IsGTE(threshold) {
depositThresholdMet = true
break
}
}
// the threshold must be met with at least one denom, if not, return the list of minimum deposits
if !depositThresholdMet {
return false, errors.Wrapf(types.ErrMinDepositTooSmall, "received %s but need at least one of the following: %s", depositAmount, strings.Join(thresholds, ","))
}
}
// update the governance module's account coins pool
err = keeper.bankKeeper.SendCoinsFromAccountToModule(ctx, depositorAddr, types.ModuleName, depositAmount)
if err != nil {
return false, err
}
// Update proposal
proposal.TotalDeposit = sdk.NewCoins(proposal.TotalDeposit...).Add(depositAmount...)
err = keeper.SetProposal(ctx, proposal)
if err != nil {
return false, err
}
// Check if deposit has provided sufficient total funds to transition the proposal into the voting period
activatedVotingPeriod := false
if proposal.Status == v1.StatusDepositPeriod && sdk.NewCoins(proposal.TotalDeposit...).IsAllGTE(minDepositAmount) {
err = keeper.ActivateVotingPeriod(ctx, proposal)
if err != nil {
return false, err
}
activatedVotingPeriod = true
}
// Add or update deposit object
deposit, err := keeper.Deposits.Get(ctx, collections.Join(proposalID, depositorAddr))
switch {
case err == nil:
// deposit exists
deposit.Amount = sdk.NewCoins(deposit.Amount...).Add(depositAmount...)
case errors.IsOf(err, collections.ErrNotFound):
// deposit doesn't exist
deposit = v1.NewDeposit(proposalID, depositorAddr, depositAmount)
default:
// failed to get deposit
return false, err
}
// called when deposit has been added to a proposal, however the proposal may not be active
err = keeper.Hooks().AfterProposalDeposit(ctx, proposalID, depositorAddr)
if err != nil {
return false, err
}
sdkCtx := sdk.UnwrapSDKContext(ctx)
sdkCtx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeProposalDeposit,
sdk.NewAttribute(types.AttributeKeyDepositor, depositorAddr.String()),
sdk.NewAttribute(sdk.AttributeKeyAmount, depositAmount.String()),
sdk.NewAttribute(types.AttributeKeyProposalID, fmt.Sprintf("%d", proposalID)),
),
)
err = keeper.SetDeposit(ctx, deposit)
if err != nil {
return false, err
}
return activatedVotingPeriod, nil
}
// ChargeDeposit will charge proposal cancellation fee (deposits * proposal_cancel_burn_rate) and
// send to a destAddress if defined or burn otherwise.
// Remaining funds are send back to the depositor.
func (keeper Keeper) ChargeDeposit(ctx context.Context, proposalID uint64, destAddress, proposalCancelRate string) error {
rate := sdkmath.LegacyMustNewDecFromStr(proposalCancelRate)
var cancellationCharges sdk.Coins
deposits, err := keeper.GetDeposits(ctx, proposalID)
if err != nil {
return err
}
for _, deposit := range deposits {
depositerAddress, err := keeper.authKeeper.AddressCodec().StringToBytes(deposit.Depositor)
if err != nil {
return err
}
var remainingAmount sdk.Coins
for _, coin := range deposit.Amount {
burnAmount := sdkmath.LegacyNewDecFromInt(coin.Amount).Mul(rate).TruncateInt()
// remaining amount = deposits amount - burn amount
remainingAmount = remainingAmount.Add(
sdk.NewCoin(
coin.Denom,
coin.Amount.Sub(burnAmount),
),
)
cancellationCharges = cancellationCharges.Add(
sdk.NewCoin(
coin.Denom,
burnAmount,
),
)
}
if !remainingAmount.IsZero() {
err := keeper.bankKeeper.SendCoinsFromModuleToAccount(
ctx, types.ModuleName, depositerAddress, remainingAmount,
)
if err != nil {
return err
}
}
err = keeper.Deposits.Remove(ctx, collections.Join(deposit.ProposalId, sdk.AccAddress(depositerAddress)))
if err != nil {
return err
}
}
// burn the cancellation fee or sent the cancellation charges to destination address.
if !cancellationCharges.IsZero() {
// get the distribution module account address
distributionAddress := keeper.authKeeper.GetModuleAddress(disttypes.ModuleName)
switch {
case destAddress == "":
// burn the cancellation charges from deposits
err := keeper.bankKeeper.BurnCoins(ctx, types.ModuleName, cancellationCharges)
if err != nil {
return err
}
case distributionAddress.String() == destAddress:
err := keeper.distrKeeper.FundCommunityPool(ctx, cancellationCharges, keeper.ModuleAccountAddress())
if err != nil {
return err
}
default:
destAccAddress, err := keeper.authKeeper.AddressCodec().StringToBytes(destAddress)
if err != nil {
return err
}
err = keeper.bankKeeper.SendCoinsFromModuleToAccount(
ctx, types.ModuleName, destAccAddress, cancellationCharges,
)
if err != nil {
return err
}
}
}
return nil
}
// RefundAndDeleteDeposits refunds and deletes all the deposits on a specific proposal.
func (keeper Keeper) RefundAndDeleteDeposits(ctx context.Context, proposalID uint64) error {
return keeper.IterateDeposits(ctx, proposalID, func(key collections.Pair[uint64, sdk.AccAddress], deposit v1.Deposit) (bool, error) {
depositor := key.K2()
err := keeper.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, depositor, deposit.Amount)
if err != nil {
return false, err
}
err = keeper.Deposits.Remove(ctx, key)
return false, err
})
}
// validateInitialDeposit validates if initial deposit is greater than or equal to the minimum
// required at the time of proposal submission. This threshold amount is determined by
// the deposit parameters. Returns nil on success, error otherwise.
func (keeper Keeper) validateInitialDeposit(ctx context.Context, params v1.Params, initialDeposit sdk.Coins, expedited bool) error {
if !initialDeposit.IsValid() || initialDeposit.IsAnyNegative() {
return errors.Wrap(sdkerrors.ErrInvalidCoins, initialDeposit.String())
}
minInitialDepositRatio, err := sdkmath.LegacyNewDecFromStr(params.MinInitialDepositRatio)
if err != nil {
return err
}
if minInitialDepositRatio.IsZero() {
return nil
}
var minDepositCoins sdk.Coins
if expedited {
minDepositCoins = params.ExpeditedMinDeposit
} else {
minDepositCoins = params.MinDeposit
}
for i := range minDepositCoins {
minDepositCoins[i].Amount = sdkmath.LegacyNewDecFromInt(minDepositCoins[i].Amount).Mul(minInitialDepositRatio).RoundInt()
}
if !initialDeposit.IsAllGTE(minDepositCoins) {
return errors.Wrapf(types.ErrMinDepositTooSmall, "was (%s), need (%s)", initialDeposit, minDepositCoins)
}
return nil
}
// validateDepositDenom validates if the deposit denom is accepted by the governance module.
func (keeper Keeper) validateDepositDenom(ctx context.Context, params v1.Params, depositAmount sdk.Coins) error {
denoms := []string{}
acceptedDenoms := make(map[string]bool, len(params.MinDeposit))
for _, coin := range params.MinDeposit {
acceptedDenoms[coin.Denom] = true
denoms = append(denoms, coin.Denom)
}
for _, coin := range depositAmount {
if _, ok := acceptedDenoms[coin.Denom]; !ok {
return errors.Wrapf(types.ErrInvalidDepositDenom, "deposited %s, but gov accepts only the following denom(s): %v", depositAmount, denoms)
}
}
return nil
}