-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// description: | ||
// @author renshiwei | ||
// Date: 2023/6/30 | ||
|
||
package beacon | ||
|
||
import ( | ||
"context" | ||
consensusApi "github.com/attestantio/go-eth2-client/api/v1" | ||
"github.com/attestantio/go-eth2-client/spec/phase0" | ||
"github.com/duktig666/ethsuper/eth1" | ||
"github.com/pkg/errors" | ||
"math/big" | ||
"strconv" | ||
) | ||
|
||
// ValidatorIsFullExited Whether the validator has completed the lifecycle of the exit | ||
func ValidatorIsFullExited(validator *consensusApi.Validator) bool { | ||
return validator.Status == consensusApi.ValidatorStateWithdrawalDone | ||
} | ||
|
||
// ValidatorIsRequestExiting Whether the validator request the exit | ||
func ValidatorIsRequestExiting(validator *consensusApi.Validator) bool { | ||
return validator.Status == consensusApi.ValidatorStateActiveExiting || | ||
validator.Status == consensusApi.ValidatorStateActiveSlashed || | ||
validator.Status == consensusApi.ValidatorStateExitedUnslashed || | ||
validator.Status == consensusApi.ValidatorStateExitedSlashed || | ||
validator.Status == consensusApi.ValidatorStateWithdrawalPossible || | ||
validator.Status == consensusApi.ValidatorStateWithdrawalDone | ||
} | ||
|
||
// ValidatorSlashedAmount 32ETh - ValidatorWithdrawAbleBalance | ||
func (b *BeaconService) ValidatorSlashedAmount(ctx context.Context, validator *consensusApi.Validator) (*big.Int, error) { | ||
if !validator.Validator.Slashed { | ||
return nil, errors.New("validator not slashed.") | ||
} | ||
|
||
pubkey := validator.Validator.PublicKey.String() | ||
|
||
withdrawAbleSlot := epochToSlot(validator.Validator.WithdrawableEpoch) | ||
pubkeys := make([]string, 1) | ||
pubkeys[0] = pubkey | ||
slashedValidator, err := b.ValidatorsByPubKey(ctx, strconv.FormatInt(int64(withdrawAbleSlot), 10), pubkeys) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Failed to get validators info.") | ||
} | ||
|
||
slashAmount := new(big.Int).Sub(eth1.ETH32().BigInt(), eth1.GweiToWei(slashedValidator[pubkey].Balance)) | ||
|
||
return slashAmount, nil | ||
} | ||
|
||
func epochToSlot(epoch phase0.Epoch) phase0.Slot { | ||
return phase0.Slot(uint64(epoch) * 12) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters