/
supply.go
192 lines (150 loc) · 6.37 KB
/
supply.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
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/anathatech/project-anatha/config"
"github.com/anathatech/project-anatha/x/distribution/internal/types"
)
func (k Keeper) TransferFromDevelopmentFund(ctx sdk.Context, recipient sdk.AccAddress, amount sdk.Coins) error {
err := k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.DevelopmentFundModuleName, recipient , amount)
if err != nil {
return err
}
k.Logger(ctx).Debug(
fmt.Sprintf("development -> %s : %s", recipient, amount),
)
return nil
}
func (k Keeper) TransferFromSecurityTokenFund(ctx sdk.Context, recipients []types.Recipients) error {
for i := 0; i < len(recipients); i++ {
err := k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.SecurityTokenFundModuleName, recipients[i].Recipient , recipients[i].Amount)
if err != nil {
return err
}
k.Logger(ctx).Debug(
fmt.Sprintf("security -> %s : %s", recipients[i].Recipient, recipients[i].Amount),
)
}
return nil
}
func (k Keeper) DistributeNameReward(ctx sdk.Context, recipient sdk.AccAddress, amount sdk.DecCoins) error {
if ctx.BlockTime().Before(k.RewardWithdrawalEnabledTime(ctx)) {
// If we are in the period where withdrawals are disabled, this function only gets executed as a result of a Hook call
// In that case we move all the funds to escrow
escrow := k.GetNameRewardEscrow(ctx, recipient)
escrow = escrow.Add(amount...)
k.SetNameRewardEscrow(ctx, recipient, escrow)
k.Logger(ctx).Debug(
fmt.Sprintf("[V] hhrm -> escrow[%s] : %s ", recipient, escrow),
)
return nil
}
// we add the balance of the escrow account to the current distribution and delete the escrow account
inEscrow := k.GetNameRewardEscrow(ctx, recipient)
amount = amount.Add(inEscrow...)
k.DeleteNameRewardEscrow(ctx, recipient)
k.Logger(ctx).Debug(
fmt.Sprintf("Distributing Name rewards for %s. Total %s, in escrow %s", recipient, amount, inEscrow),
)
// we split the reward in to whole (rewardInt) and fraction (rewardLeftover)
rewardDec := amount.AmountOf(config.DefaultDenom)
rewardInt := rewardDec.TruncateInt()
rewardLeftover := rewardDec.Sub(rewardInt.ToDec())
if ! rewardLeftover.IsZero() {
// Current reward leftover is greater than 0.
// This means that we need to add it to the existing leftovers and send whole pin while persisting fractions
previousLeftover := k.GetNameRewardLeftover(ctx, recipient)
currentLeftover := rewardLeftover.Add(previousLeftover)
// we split the leftover in to whole (currentLeftoverInt) and fraction (currentLeftover)
currentLeftoverInt := currentLeftover.TruncateInt()
currentLeftover = currentLeftover.Sub(currentLeftoverInt.ToDec())
// we add whole pin to the rewardInt variable
rewardInt = rewardInt.Add(currentLeftoverInt)
k.Logger(ctx).Debug(
fmt.Sprintf("Previous leftover: %s, Current leftover: %s", previousLeftover, currentLeftover),
)
// we update the remaining leftover
k.SetNameRewardLeftover(ctx, recipient, currentLeftover)
}
toTransfer := sdk.NewCoins(sdk.NewCoin(config.DefaultDenom, rewardInt))
err := k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.HRAHolderRewardModuleName, recipient , toTransfer)
if err != nil {
return err
}
k.Logger(ctx).Debug(
fmt.Sprintf("hhrm -> %s : %s", recipient, toTransfer),
)
return nil
}
func (k Keeper) DistributeSavingsReward(ctx sdk.Context, recipient sdk.AccAddress, amount sdk.DecCoins) error {
if ctx.BlockTime().Before(k.RewardWithdrawalEnabledTime(ctx)) {
// If we are in the period where withdrawals are disabled, this function only gets executed as a result of
// Withdraw Savings transaction where Funds being taken out of savings and interest is transferred to escrow
// In that case we move all the funds to escrow
escrow := k.GetSavingsRewardEscrow(ctx, recipient)
escrow = escrow.Add(amount...)
k.SetSavingsRewardEscrow(ctx, recipient, escrow)
k.Logger(ctx).Debug(
fmt.Sprintf("[V] savingsdistr -> escrow[%s] : %s ", recipient, escrow),
)
return nil
}
// we add the balance of the escrow account to the current distribution and delete the escrow account
inEscrow := k.GetSavingsRewardEscrow(ctx, recipient)
amount = amount.Add(inEscrow...)
k.DeleteSavingsRewardEscrow(ctx, recipient)
k.Logger(ctx).Debug(
fmt.Sprintf("Distributing Savings rewards for %s. Total %s, in escrow %s", recipient, amount, inEscrow),
)
// we split the reward in to whole (rewardInt) and fraction (rewardLeftover)
rewardDec := amount.AmountOf(config.DefaultDenom)
rewardInt := rewardDec.TruncateInt()
rewardLeftover := rewardDec.Sub(rewardInt.ToDec())
if ! rewardLeftover.IsZero() {
// Current reward leftover is greater than 0.
// This means that we need to add it to the existing leftovers and send whole pin while persisting fractions
previousLeftover := k.GetSavingsRewardLeftover(ctx, recipient)
currentLeftover := rewardLeftover.Add(previousLeftover)
// we split the leftover in to whole (currentLeftoverInt) and fraction (currentLeftover)
currentLeftoverInt := currentLeftover.TruncateInt()
currentLeftover = currentLeftover.Sub(currentLeftoverInt.ToDec())
// we add whole pin to the rewardInt variable
rewardInt = rewardInt.Add(currentLeftoverInt)
k.Logger(ctx).Debug(
fmt.Sprintf("Previous leftover: %s, Current leftover: %s", previousLeftover, currentLeftover),
)
// we update the remaining leftover
k.SetSavingsRewardLeftover(ctx, recipient, currentLeftover)
}
if ! rewardInt.IsZero() {
toTransfer := sdk.NewCoins(sdk.NewCoin(config.DefaultDenom, rewardInt))
err := k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.SavingsDistributionModuleName, recipient , toTransfer)
if err != nil {
return err
}
k.Logger(ctx).Debug(
fmt.Sprintf("savingsdistr -> %s : %s", recipient, toTransfer),
)
}
return nil
}
func (k Keeper) RefundSavingsStake(ctx sdk.Context, recipient sdk.AccAddress, amount sdk.Coins) error {
err := k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.SavingsModuleName, recipient, amount)
if err != nil {
return err
}
k.Logger(ctx).Debug(
fmt.Sprintf("savings -> %s : %s", recipient, amount),
)
return nil
}
func (k Keeper) ClaimSavingsStake(ctx sdk.Context, sender sdk.AccAddress, amount sdk.Coins) error {
err := k.supplyKeeper.SendCoinsFromAccountToModule(ctx, sender, types.SavingsModuleName, amount)
if err != nil {
return err
}
k.Logger(ctx).Debug(
fmt.Sprintf("%s -> savings : %s", sender, amount),
)
return nil
}