forked from evmos/evmos
-
Notifications
You must be signed in to change notification settings - Fork 3
/
distribution.go
252 lines (215 loc) · 7.24 KB
/
distribution.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
package keeper
import (
"math/big"
"strconv"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/math"
"github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"
"github.com/EscanBE/evermint/v12/x/incentives/types"
)
// DistributeRewards transfers the allocated rewards to the participants of a given
// incentive.
// - allocates the amount to be distributed from the inflation pool
// - distributes the rewards to all participants
// - deletes all gas meters
// - updates the remaining epochs of each incentive
// - sets the cumulative totalGas to zero
func (k Keeper) DistributeRewards(ctx sdk.Context) error {
logger := k.Logger(ctx)
rewardAllocations, totalRewards, err := k.rewardAllocations(ctx)
if err != nil {
return err
}
k.IterateIncentives(ctx, func(incentive types.Incentive) (stop bool) {
_, _ = k.rewardParticipants(ctx, incentive, rewardAllocations)
incentive.Epochs--
// Update Incentive and reset its total gas count. Remove incentive if it
// has no remaining epochs left.
if incentive.IsActive() {
k.SetIncentive(ctx, incentive)
k.SetIncentiveTotalGas(ctx, incentive, 0)
} else {
k.DeleteIncentiveAndUpdateAllocationMeters(ctx, incentive)
logger.Info(
"incentive finalized",
"contract", incentive.Contract,
)
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeDistributeIncentives,
sdk.NewAttribute(types.AttributeKeyContract, incentive.Contract),
sdk.NewAttribute(
types.AttributeKeyEpochs,
strconv.FormatUint(uint64(incentive.Epochs), 10),
),
),
)
return false
})
defer func() {
for _, r := range totalRewards {
if r.Amount.IsInt64() {
telemetry.IncrCounterWithLabels(
[]string{types.ModuleName, "distribute", "reward", "total"},
float32(r.Amount.Int64()),
[]metrics.Label{telemetry.NewLabel("denom", r.Denom)},
)
}
}
}()
return nil
}
// rewardAllocations returns a map of each incentive's reward allocation
// - Iterate over all the registered and active incentives
// - create an allocation (module account) from escrow balance to be distributed to the contract address
// - check that escrow balance is sufficient
func (k Keeper) rewardAllocations(
ctx sdk.Context,
) (map[common.Address]sdk.Coins, sdk.Coins, error) {
// Get balances on incentive module account
denomBalances := make(map[string]math.Int)
moduleAddr := k.accountKeeper.GetModuleAddress(types.ModuleName)
escrow := sdk.Coins{}
// iterate over the module account balance insert elements to the denom -> amount
// lookup map
k.bankKeeper.IterateAccountBalances(ctx, moduleAddr, func(coin sdk.Coin) bool {
denomBalances[coin.Denom] = coin.Amount
// NOTE: all coins have different denomination so we can safely append instead
// of using Add
escrow = append(escrow, coin)
return false
})
rewardAllocations := make(map[common.Address]sdk.Coins)
rewards := sdk.Coins{}
// iterate over all the incentives to define the allocation
// amount for each contract
k.IterateIncentives(
ctx,
func(incentive types.Incentive) (stop bool) {
coins := sdk.Coins{}
contract := common.HexToAddress(incentive.Contract)
// calculate allocation for the incentivized contract
for _, al := range incentive.Allocations {
// Check if a balance to allocate exists
if _, ok := denomBalances[al.Denom]; !ok {
continue
}
// allocation for the contract is the amount escrowed * the allocation %
coinAllocated := sdk.NewDecFromInt(denomBalances[al.Denom]).Mul(al.Amount).TruncateInt()
amount := coinAllocated
// NOTE: safety check, shouldn't occur since the allocation and balance
// are > 0
if !amount.IsPositive() {
continue
}
coin := sdk.Coin{Denom: al.Denom, Amount: amount}
coins = coins.Add(coin)
}
rewardAllocations[contract] = coins
rewards = rewards.Add(coins...)
return false
},
)
// checks if module account has sufficient balance for allocation
if rewards.IsAnyGT(escrow) {
return nil, nil, errorsmod.Wrapf(
errortypes.ErrInsufficientFunds,
"escrowed balance < total coins allocated (%s < %s)",
escrow, rewards,
)
}
return rewardAllocations, rewards, nil
}
// rewardParticipants reward participants of a given Incentive, delete their gas
// meters and returns a count of all gas meters
// - Check if participants spent gas on interacting with incentive
// - Iterate over the incentive participants' gas meters
// - Allocate rewards according to participants gasRatio and cap them at 100% of their gas spent on interaction with incentive
// - Send rewards to participants
// - Delete gas meter
func (k Keeper) rewardParticipants(
ctx sdk.Context,
incentive types.Incentive,
coinsAllocated map[common.Address]sdk.Coins,
) (rewards sdk.Coins, count uint64) {
logger := k.Logger(ctx)
// Check if coin allocation was successful
contract := common.HexToAddress(incentive.Contract)
contractAllocation, ok := coinsAllocated[contract]
if !ok {
logger.Debug(
"contract allocation coins not found",
"contract", incentive.Contract,
)
return sdk.Coins{}, 0
}
// Check if participants spent gas on interacting with incentive
totalGas := incentive.TotalGas
if totalGas == 0 {
logger.Debug(
"no gas spent on incentive during epoch",
"contract", incentive.Contract,
)
return sdk.Coins{}, 0
}
totalGasDec := sdk.NewDecFromBigInt(new(big.Int).SetUint64(totalGas))
mintDenom := k.evmKeeper.GetParams(ctx).EvmDenom
rewardScaler := k.GetParams(ctx).RewardScaler
// Iterate over the incentive's gas meters and distribute rewards
k.IterateIncentiveGasMeters(
ctx,
contract,
func(gm types.GasMeter) (stop bool) {
// Get participant's ratio of `gas spent / total gas spent`
cumulativeGas := sdk.NewDecFromBigInt(new(big.Int).SetUint64(gm.CumulativeGas))
gasRatio := cumulativeGas.Quo(totalGasDec)
coins := sdk.Coins{}
// Allocate rewards according to gasRatio
for _, allocation := range incentive.Allocations {
coinAllocated := contractAllocation.AmountOf(allocation.Denom)
reward := gasRatio.MulInt(coinAllocated)
if !reward.IsPositive() {
continue
}
// Cap rewards in mint denom to receive only up to 100% of
// the participant's gas spent and prevent gaming
if mintDenom == allocation.Denom {
rewardCap := cumulativeGas.Mul(rewardScaler)
reward = sdk.MinDec(reward, rewardCap)
}
// NOTE: ignore denom validation
coin := sdk.Coin{Denom: allocation.Denom, Amount: reward.TruncateInt()}
coins = coins.Add(coin)
}
rewards = rewards.Add(coins...)
// Send rewards to participant
participant := common.HexToAddress(gm.Participant)
err := k.bankKeeper.SendCoinsFromModuleToAccount(
ctx,
types.ModuleName,
sdk.AccAddress(participant.Bytes()),
coins,
)
if err != nil {
logger.Debug(
"failed to distribute incentive",
"address", gm.Participant,
"allocation", coins.String(),
"incentive", gm.Contract,
"error", err.Error(),
)
return true // break iteration
}
// Remove gas meter once the rewards are distributed
k.DeleteGasMeter(ctx, gm)
count++
return false
},
)
return rewards, count
}