-
Notifications
You must be signed in to change notification settings - Fork 40
/
query_unstaked_positions.go
88 lines (73 loc) · 2.87 KB
/
query_unstaked_positions.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
package wasm
import (
"encoding/json"
"fmt"
"math"
errorsmod "cosmossdk.io/errors"
cosmos_sdk_math "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
commitmenttypes "github.com/elys-network/elys/x/commitment/types"
)
func (oq *Querier) queryUnStakedPositions(ctx sdk.Context, query *commitmenttypes.QueryValidatorsRequest) ([]byte, error) {
totalBonded := oq.stakingKeeper.TotalBondedTokens(ctx)
delegatorAddr, err := sdk.AccAddressFromBech32(query.DelegatorAddress)
if err != nil {
return nil, errorsmod.Wrap(err, "invalid delegator address")
}
unbonding_delegations := oq.stakingKeeper.GetUnbondingDelegations(ctx, delegatorAddr, math.MaxInt16)
unstakedPositionsCW := oq.BuildUnStakedPositionResponseCW(ctx, unbonding_delegations, totalBonded, query.DelegatorAddress)
res := commitmenttypes.QueryUnstakedPositionResponse{
UnstakedPosition: unstakedPositionsCW,
}
responseBytes, err := json.Marshal(res)
if err != nil {
return nil, errorsmod.Wrap(err, "failed to get delegation validator response")
}
return responseBytes, nil
}
func (oq *Querier) BuildUnStakedPositionResponseCW(ctx sdk.Context, unbondingDelegations []stakingtypes.UnbondingDelegation, totalBonded cosmos_sdk_math.Int, delegatorAddress string) []commitmenttypes.UnstakedPosition {
edenDenomPrice := sdk.ZeroDec()
baseCurrency, found := oq.assetKeeper.GetUsdcDenom(ctx)
if found {
edenDenomPrice = oq.ammKeeper.GetEdenDenomPrice(ctx, baseCurrency)
}
var unstakedPositions []commitmenttypes.UnstakedPosition
i := 1
for _, undelegation := range unbondingDelegations {
for _, entity := range undelegation.Entries {
var unstakedPosition commitmenttypes.UnstakedPosition
unstakedPosition.Id = fmt.Sprintf("%d", i)
valAddress, err := sdk.ValAddressFromBech32(undelegation.ValidatorAddress)
if err != nil {
continue
}
// Get validator
val := oq.stakingKeeper.Validator(ctx, valAddress).(stakingtypes.Validator)
votingPower := sdk.NewDecFromInt(val.GetBondedTokens()).QuoInt(totalBonded).MulInt(sdk.NewInt(100))
website := val.Description.Website
if len(website) < 1 {
website = " "
}
unstakedPosition.Validator = commitmenttypes.StakingValidator{
Id: val.Description.Identity,
// The validator address.
Address: val.OperatorAddress,
// The validator name.
Name: val.Description.Moniker,
// Voting power percentage for this validator.
VotingPower: votingPower,
// Comission percentage for the validator.
Commission: val.GetCommission(),
}
unstakedPosition.RemainingTime = uint64(entity.CompletionTime.Unix())
unstakedPosition.Unstaked = commitmenttypes.BalanceAvailable{
Amount: entity.Balance,
UsdAmount: edenDenomPrice.MulInt(entity.Balance),
}
unstakedPositions = append(unstakedPositions, unstakedPosition)
i++
}
}
return unstakedPositions
}