-
Notifications
You must be signed in to change notification settings - Fork 12
/
delegation.go
34 lines (28 loc) · 1.09 KB
/
delegation.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
package staking
import (
"fmt"
"math/big"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/functionx/fx-core/v5/x/evm/types"
)
func (c *Contract) Delegation(ctx sdk.Context, _ *vm.EVM, contract *vm.Contract, _ bool) ([]byte, error) {
cacheCtx, _ := ctx.CacheContext()
// parse args
var args DelegationArgs
if err := types.ParseMethodArgs(DelegationMethod, &args, contract.Input[4:]); err != nil {
return nil, err
}
valAddr := args.GetValidator()
validator, found := c.stakingKeeper.GetValidator(cacheCtx, valAddr)
if !found {
return nil, fmt.Errorf("validator not found: %s", valAddr.String())
}
delegation, found := c.stakingKeeper.GetDelegation(cacheCtx, args.Delegator.Bytes(), valAddr)
if !found {
return DelegationMethod.Outputs.Pack(big.NewInt(0), big.NewInt(0))
}
delegationAmt := delegation.GetShares().MulInt(validator.GetTokens()).Quo(validator.GetDelegatorShares())
// TODO truncate shares, decimal 18
return DelegationMethod.Outputs.Pack(delegation.GetShares().TruncateInt().BigInt(), delegationAmt.TruncateInt().BigInt())
}