Skip to content

Commit

Permalink
Add extra support for more getters for the Validator class
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-cb committed Dec 17, 2024
1 parent 88b4973 commit d461ef5
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## Unreleased

### Added

- Add getters for `Validator` object to expose more data to users.
- Add test cases for `Validator` object.



## [0.0.15] - 2024-12-02

### Added
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/gagliardetto/binary v0.8.0 h1:U9ahc45v9HW0d15LoN++vIXSJyqR/pWw8DDlhd7zvxg=
github.com/gagliardetto/binary v0.8.0/go.mod h1:2tfj51g5o9dnvsc+fL3Jxr22MuWzYXwx9wEoN0XQ7/c=
github.com/gagliardetto/gofuzz v1.2.2 h1:XL/8qDMzcgvR4+CyRQW9UGdwPRPMHVJfqQ/uMvSUuQw=
github.com/gagliardetto/gofuzz v1.2.2/go.mod h1:bkH/3hYLZrMLbfYWA0pWzXmi5TTRZnu4pMGZBkqMKvY=
github.com/gagliardetto/solana-go v1.10.0 h1:lDuHGC+XLxw9j8fCHBZM9tv4trI0PVhev1m9NAMaIdM=
github.com/gagliardetto/solana-go v1.10.0/go.mod h1:afBEcIRrDLJst3lvAahTr63m6W2Ns6dajZxe2irF7Jg=
github.com/gagliardetto/treeout v0.1.4 h1:ozeYerrLCmCubo1TcIjFiOWTTGteOOHND1twdFpgwaw=
Expand Down
89 changes: 89 additions & 0 deletions pkg/coinbase/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package coinbase

import (
"context"
"encoding/json"
"fmt"

"github.com/coinbase/coinbase-sdk-go/gen/client"
Expand Down Expand Up @@ -94,6 +95,84 @@ func (v Validator) Status() client.ValidatorStatus {
return v.model.Status
}

func (v Validator) NetworkID() string {
return v.model.NetworkId
}

func (v Validator) AssetID() string {
return v.model.AssetId
}

func (v Validator) Index() string {
if !v.hasEthereumMetadata() {
return ""
}
return v.model.Details.EthereumValidatorMetadata.GetIndex()
}

func (v Validator) PublicKey() string {
if !v.hasEthereumMetadata() {
return ""
}
return v.model.Details.EthereumValidatorMetadata.GetPublicKey()
}

func (v Validator) WithdrawalAddress() string {
if !v.hasEthereumMetadata() {
return ""
}
return v.model.Details.EthereumValidatorMetadata.GetWithdrawalAddress()
}

func (v Validator) Slashed() bool {
if !v.hasEthereumMetadata() {
return false
}
return v.model.Details.EthereumValidatorMetadata.GetSlashed()
}

func (v Validator) ActivationEpoch() string {
if !v.hasEthereumMetadata() {
return ""
}
return v.model.Details.EthereumValidatorMetadata.GetActivationEpoch()
}

func (v Validator) ExitEpoch() string {
if !v.hasEthereumMetadata() {
return ""
}
return v.model.Details.EthereumValidatorMetadata.GetExitEpoch()
}

func (v Validator) WithdrawableEpoch() string {
if !v.hasEthereumMetadata() {
return ""
}
return v.model.Details.EthereumValidatorMetadata.GetWithdrawableEpoch()
}

func (v Validator) Balance() client.Balance {
if !v.hasEthereumMetadata() {
return client.Balance{}
}
return v.model.Details.EthereumValidatorMetadata.GetBalance()
}

func (v Validator) EffectiveBalance() client.Balance {
if !v.hasEthereumMetadata() {
return client.Balance{}
}
return v.model.Details.EthereumValidatorMetadata.GetEffectiveBalance()
}

func (v Validator) hasEthereumMetadata() bool {
if v.model.Details == nil || v.model.Details.EthereumValidatorMetadata == nil {
return false
}
return true
}

func (v Validator) ToString() string {
return fmt.Sprintf(
"Validator { Id: '%s' Status: '%s' }",
Expand All @@ -102,6 +181,16 @@ func (v Validator) ToString() string {
)
}

func (v Validator) ToJSON() (string, error) {
jsonData, err := json.Marshal(v.model)
if err != nil {
return "", err
}

// Print JSON as string
return string(jsonData), nil
}

func (c *Client) ListValidators(
ctx context.Context,
networkId string,
Expand Down
52 changes: 52 additions & 0 deletions pkg/coinbase/validators_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,58 @@ func (s *ValidatorSuite) TestGetValidator_Failure() {
s.EqualError(err, "APIError{HttpStatusCode: 500, Code: unknown, Message: some error calling api}")
}

func (s *ValidatorSuite) TestGetters() {
validator := NewValidator(api.Validator{
ValidatorId: "validator-1",
NetworkId: EthereumHolesky,
AssetId: Eth,
Status: api.VALIDATORSTATUS_ACTIVE,
Details: &api.ValidatorDetails{
EthereumValidatorMetadata: &api.EthereumValidatorMetadata{
Index: "0",
PublicKey: "public-key-1",
WithdrawalAddress: "withdrawal-address-1",
Slashed: false,
ActivationEpoch: "epoch-1",
ExitEpoch: "exit-epoch-2",
WithdrawableEpoch: "withdrawable-epoch-3",
Balance: api.Balance{
Amount: "100",
Asset: api.Asset{
NetworkId: EthereumHolesky,
AssetId: Eth,
},
},
EffectiveBalance: api.Balance{
Amount: "200",
Asset: api.Asset{
NetworkId: EthereumHolesky,
AssetId: Eth,
},
},
},
},
})

s.Assert().Equal("validator-1", validator.ID())
s.Assert().Equal(EthereumHolesky, validator.NetworkID())
s.Assert().Equal(Eth, validator.AssetID())
s.Assert().Equal(api.VALIDATORSTATUS_ACTIVE, validator.Status())
s.Assert().Equal("0", validator.Index())
s.Assert().Equal("public-key-1", validator.PublicKey())
s.Assert().Equal("withdrawal-address-1", validator.WithdrawalAddress())
s.Assert().Equal(false, validator.Slashed())
s.Assert().Equal("epoch-1", validator.ActivationEpoch())
s.Assert().Equal("exit-epoch-2", validator.ExitEpoch())
s.Assert().Equal("withdrawable-epoch-3", validator.WithdrawableEpoch())
s.Assert().Equal("100", validator.Balance().Amount)
s.Assert().Equal(EthereumHolesky, validator.Balance().Asset.NetworkId)
s.Assert().Equal(Eth, validator.Balance().Asset.AssetId)
s.Assert().Equal("200", validator.EffectiveBalance().Amount)
s.Assert().Equal(EthereumHolesky, validator.EffectiveBalance().Asset.NetworkId)
s.Assert().Equal(Eth, validator.EffectiveBalance().Asset.AssetId)
}

func (s *ValidatorSuite) mockSuccessfulListValidators(ctx context.Context, networkId string, assetId string, mockValidators *api.ValidatorList) {
s.T().Helper()

Expand Down

0 comments on commit d461ef5

Please sign in to comment.