-
Notifications
You must be signed in to change notification settings - Fork 4
/
reward.go
94 lines (73 loc) · 2.54 KB
/
reward.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
package keeper
import (
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
sdk "github.com/cosmos/cosmos-sdk/types"
oracletypes "github.com/ODIN-PROTOCOL/odin-core/x/oracle/types"
)
func (k Keeper) SetDataProviderAccumulatedReward(ctx sdk.Context, acc sdk.AccAddress, reward sdk.Coins) error {
hasReward, err := k.HasDataProviderReward(ctx, acc)
if err != nil {
return err
}
if !hasReward {
return k.DataProviderAccumulatedRewards.Set(ctx, acc.Bytes(), oracletypes.NewDataProviderAccumulatedReward(acc, reward))
}
oldReward, err := k.GetDataProviderAccumulatedReward(ctx, acc.Bytes())
if err != nil {
return err
}
newReward := oldReward.Add(reward...)
return k.DataProviderAccumulatedRewards.Set(ctx, acc.Bytes(), oracletypes.NewDataProviderAccumulatedReward(acc, newReward))
}
func (k Keeper) ClearDataProviderAccumulatedReward(ctx sdk.Context, acc sdk.AccAddress) error {
return k.DataProviderAccumulatedRewards.Remove(ctx, acc.Bytes())
}
func (k Keeper) GetDataProviderAccumulatedReward(ctx sdk.Context, acc sdk.AccAddress) (sdk.Coins, error) {
rewards, err := k.DataProviderAccumulatedRewards.Get(ctx, acc.Bytes())
if err != nil {
return nil, err
}
return rewards.DataProviderReward, nil
}
func (k Keeper) HasDataProviderReward(ctx sdk.Context, acc sdk.AccAddress) (bool, error) {
return k.DataProviderAccumulatedRewards.Has(ctx, acc.Bytes())
}
// AllocateRewardsToDataProviders sends rewards from fee pool to data providers, that have given data for the passed request
func (k Keeper) AllocateRewardsToDataProviders(ctx sdk.Context, rid oracletypes.RequestID) error {
request, err := k.GetRequest(ctx, rid)
if err != nil {
return err
}
for _, rawReq := range request.RawRequests {
ds := k.MustGetDataSource(ctx, rawReq.GetDataSourceID())
ownerAccAddr, err := sdk.AccAddressFromBech32(ds.Owner)
if err != nil {
return err
}
hasReward, err := k.HasDataProviderReward(ctx, ownerAccAddr)
if err != nil {
return err
}
if !hasReward {
continue
}
reward, err := k.GetDataProviderAccumulatedReward(ctx, ownerAccAddr)
if err != nil {
return err
}
err = k.distrKeeper.DistributeFromFeePool(ctx, reward, ownerAccAddr)
if err != nil {
if err == distrtypes.ErrBadDistribution {
k.Logger(ctx).Error("oracle pool does not have enough coins to reward data providers")
continue
}
return err
}
// we are sure to have paid the reward to the provider, we can remove him now
err = k.ClearDataProviderAccumulatedReward(ctx, ownerAccAddr)
if err != nil {
return err
}
}
return nil
}