-
Notifications
You must be signed in to change notification settings - Fork 52
/
keeper_burn_edenB.go
92 lines (75 loc) · 2.98 KB
/
keeper_burn_edenB.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
package keeper
import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
ptypes "github.com/elys-network/elys/x/parameter/types"
)
// Burn EdenBoost from Elys unstaked
func (k Keeper) BurnEdenBFromElysUnstaking(ctx sdk.Context, delegator sdk.AccAddress) {
// Get commitments
delAddr := delegator.String()
commitments := k.commKeeper.GetCommitments(ctx, delAddr)
// Get previous amount
prevElysStaked := k.GetElysStaked(ctx, delAddr)
if prevElysStaked.IsZero() {
return
}
// Calculate current delegated amount of delegator
delAmount := k.CalcDelegationAmount(ctx, delAddr)
// If not unstaked,
// should return nil otherwise it will break staking module
if delAmount.GTE(prevElysStaked) {
return
}
// TODO: Claim all delegation rewards
edenCommitted := commitments.GetCommittedAmountForDenom(ptypes.Eden)
// Total EdenB amount
edenBCommitted := commitments.GetCommittedAmountForDenom(ptypes.EdenB)
edenBClaimed := commitments.GetClaimedForDenom(ptypes.EdenB)
totalEdenB := edenBCommitted.Add(edenBClaimed)
// Unstaked
unstakedElys := prevElysStaked.Sub(delAmount)
unstakedElysDec := sdk.NewDecFromInt(unstakedElys)
edenCommittedAndElysStakedDec := sdk.NewDecFromInt(edenCommitted.Add(prevElysStaked))
edenBToBurn := sdk.ZeroDec()
if edenCommittedAndElysStakedDec.GT(sdk.ZeroDec()) {
edenBToBurn = unstakedElysDec.Quo(edenCommittedAndElysStakedDec).MulInt(totalEdenB)
}
if edenBToBurn.IsZero() {
return
}
// Burn EdenB in commitment module
err := k.commKeeper.BurnEdenBoost(ctx, delAddr, ptypes.EdenB, edenBToBurn.TruncateInt())
if err != nil {
k.Logger(ctx).Error("EdenB burn failure", err)
}
}
// Burn EdenBoost from Eden unclaimed
func (k Keeper) BurnEdenBFromEdenUncommitted(ctx sdk.Context, delegator string, uncommitAmt math.Int) error {
// TODO: Claim all delegation rewards
// Get elys staked amount
elysStaked := k.GetElysStaked(ctx, delegator)
commitments := k.commKeeper.GetCommitments(ctx, delegator)
edenCommitted := commitments.GetCommittedAmountForDenom(ptypes.Eden)
// Total EdenB amount
edenBCommitted := commitments.GetCommittedAmountForDenom(ptypes.EdenB)
edenBClaimed := commitments.GetClaimedForDenom(ptypes.EdenB)
totalEdenB := edenBCommitted.Add(edenBClaimed)
unclaimedAmtDec := sdk.NewDecFromInt(uncommitAmt)
// This formula shud be applied before eden uncommitted or elys staked is removed from eden committed amount and elys staked amount respectively
// So add uncommitted amount to committed eden bucket in calculation.
edenCommittedAndElysStakedDec := sdk.NewDecFromInt(edenCommitted.Add(elysStaked).Add(uncommitAmt))
if edenCommittedAndElysStakedDec.IsZero() {
return nil
}
edenBToBurn := sdk.ZeroDec()
if edenCommittedAndElysStakedDec.GT(sdk.ZeroDec()) {
edenBToBurn = unclaimedAmtDec.Quo(edenCommittedAndElysStakedDec).MulInt(totalEdenB)
}
if edenBToBurn.IsZero() {
return nil
}
// Burn EdenB in commitment module
err := k.commKeeper.BurnEdenBoost(ctx, delegator, ptypes.EdenB, edenBToBurn.TruncateInt())
return err
}