-
Notifications
You must be signed in to change notification settings - Fork 12
/
delegate.go
31 lines (27 loc) · 1.16 KB
/
delegate.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/functionx/fx-core/v3/x/crosschain/types"
)
func (k Keeper) GetOracleDelegateToken(ctx sdk.Context, delegateAddr sdk.AccAddress, valAddr sdk.ValAddress) (sdk.Int, error) {
delegation, found := k.stakingKeeper.GetDelegation(ctx, delegateAddr, valAddr)
if !found {
return sdk.ZeroInt(), sdkerrors.Wrap(types.ErrInvalid, "no delegation for (address, validator) tuple")
}
validator, found := k.stakingKeeper.GetValidator(ctx, valAddr)
if !found {
return sdk.ZeroInt(), stakingtypes.ErrNoValidatorFound
}
delegateToken := validator.TokensFromSharesTruncated(delegation.GetShares()).TruncateInt()
sharesTruncated, err := validator.SharesFromTokensTruncated(delegateToken)
if err != nil {
return sdk.ZeroInt(), sdkerrors.Wrapf(types.ErrInvalid, "shares from tokens:%v", delegateToken)
}
delShares := delegation.GetShares()
if sharesTruncated.GT(delShares) {
delegateToken = validator.TokensFromSharesTruncated(sharesTruncated).TruncateInt()
}
return delegateToken, nil
}