-
Notifications
You must be signed in to change notification settings - Fork 0
/
proposals.go
130 lines (113 loc) · 3.56 KB
/
proposals.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"
"github.com/hardiksa/ambinet/v4/x/incentives/types"
)
// RegisterIncentive creates an incentive for a contract
func (k Keeper) RegisterIncentive(
ctx sdk.Context,
contract common.Address,
allocations sdk.DecCoins,
epochs uint32,
) (*types.Incentive, error) {
// Check if the Incentives are globally enabled
params := k.GetParams(ctx)
if !params.EnableIncentives {
return nil, sdkerrors.Wrap(
types.ErrInternalIncentive,
"incentives are currently disabled by governance",
)
}
// Check if contract exists
acc := k.evmKeeper.GetAccountWithoutBalance(ctx, contract)
if acc == nil || !acc.IsContract() {
return nil, sdkerrors.Wrapf(
types.ErrInternalIncentive,
"contract doesn't exist: %s", contract,
)
}
// Check if the incentive is already registered
if k.IsIncentiveRegistered(ctx, contract) {
return nil, sdkerrors.Wrapf(
types.ErrInternalIncentive,
"incentive already registered: %s", contract,
)
}
// Check if the balance is > 0 for coins other than the mint denomination
mintDenom := k.evmKeeper.GetParams(ctx).EvmDenom
moduleAddr := k.accountKeeper.GetModuleAddress(types.ModuleName)
for _, al := range allocations {
if al.Denom != mintDenom && k.bankKeeper.GetBalance(ctx, moduleAddr, al.Denom).IsZero() {
return nil, sdkerrors.Wrapf(
sdkerrors.ErrInvalidCoins,
"base denomination '%s' cannot have a supply of 0", al.Denom,
)
}
// Check if each allocation is below the allocation limit
if al.Amount.GT(params.AllocationLimit) {
return nil, sdkerrors.Wrapf(
types.ErrInternalIncentive,
"allocation for denom '%s' (%s) cannot be above allocation limit (%s)", al.Denom, al.Amount, params.AllocationLimit,
)
}
}
// Iterate over allocations to update allocation meters
allocationMeters := []sdk.DecCoin{}
for _, al := range allocations {
allocationMeter, _ := k.GetAllocationMeter(ctx, al.Denom)
// Check if the sum of all allocations (current + proposed) exceeds 100%
allocationSum := allocationMeter.Amount.Add(al.Amount)
if allocationSum.GT(sdk.OneDec()) {
return nil, sdkerrors.Wrapf(
types.ErrInternalIncentive,
"allocation for denom %s is larger than 100 percent: %v",
al.Denom, allocationSum,
)
}
// build new allocation meter
newAllocationMeter := sdk.DecCoin{
Denom: al.Denom,
Amount: allocationSum,
}
allocationMeters = append(allocationMeters, newAllocationMeter)
}
// create incentive and set to store
incentive := types.NewIncentive(contract, allocations, epochs)
incentive.StartTime = ctx.BlockTime()
k.SetIncentive(ctx, incentive)
// Update allocation meters
for _, am := range allocationMeters {
k.SetAllocationMeter(ctx, am)
}
return &incentive, nil
}
// RegisterIncentive deletes the incentive for a contract
func (k Keeper) CancelIncentive(
ctx sdk.Context,
contract common.Address,
) error {
// Check if the Incentives are globally enabled
params := k.GetParams(ctx)
if !params.EnableIncentives {
return sdkerrors.Wrap(
types.ErrInternalIncentive,
"incentives are currently disabled by governance",
)
}
incentive, found := k.GetIncentive(ctx, contract)
if !found {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidAddress,
"unmatching contract '%s' ", contract,
)
}
k.DeleteIncentiveAndUpdateAllocationMeters(ctx, incentive)
// Delete incentive's gas meters
gms := k.GetIncentiveGasMeters(ctx, contract)
for _, gm := range gms {
k.DeleteGasMeter(ctx, gm)
}
return nil
}