-
Notifications
You must be signed in to change notification settings - Fork 55
/
rebalancing.go
47 lines (43 loc) · 1.86 KB
/
rebalancing.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
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
type Redelegation struct {
Delegator sdk.AccAddress
SrcValidator LiquidValidator
DstValidator LiquidValidator
Amount sdk.Int
Last bool
Error error
}
// DivideByWeight divide the input value by the ratio of the param weight of the liquid validator and return it with crumb
// which is may occur while dividing according to the weight of active liquid validators by decimal error.
func DivideByWeight(avs ActiveLiquidValidators, input sdk.Int, whitelistedValsMap WhitelistedValsMap) (outputs []sdk.Int, crumb sdk.Int) {
totalWeight := avs.TotalWeight(whitelistedValsMap)
if !totalWeight.IsPositive() {
return []sdk.Int{}, sdk.ZeroInt()
}
totalOutput := sdk.ZeroInt()
unitInput := input.ToDec().QuoTruncate(totalWeight.ToDec())
for _, val := range avs {
output := unitInput.MulInt(val.GetWeight(whitelistedValsMap, true)).TruncateInt()
totalOutput = totalOutput.Add(output)
outputs = append(outputs, output)
}
return outputs, input.Sub(totalOutput)
}
// DivideByCurrentWeight divide the input value by the ratio of the weight of the liquid validator's liquid token and return it with crumb
// which is may occur while dividing according to the weight of liquid validators by decimal error, outputs is truncated decimal.
func DivideByCurrentWeight(lvs LiquidValidators, input sdk.Dec, totalLiquidTokens sdk.Int, liquidTokenMap map[string]sdk.Int) (outputs []sdk.Dec, crumb sdk.Dec) {
if !totalLiquidTokens.IsPositive() {
return []sdk.Dec{}, sdk.ZeroDec()
}
totalOutput := sdk.ZeroDec()
unitInput := input.QuoTruncate(totalLiquidTokens.ToDec())
for _, val := range lvs {
output := unitInput.MulTruncate(liquidTokenMap[val.OperatorAddress].ToDec()).TruncateDec()
totalOutput = totalOutput.Add(output)
outputs = append(outputs, output)
}
return outputs, input.Sub(totalOutput)
}