diff --git a/rpc/delegate.go b/rpc/delegate.go index 1a8bdcb..9184e9f 100644 --- a/rpc/delegate.go +++ b/rpc/delegate.go @@ -139,14 +139,14 @@ Function: */ type BakingRightsInput struct { // The hash of block (height) of which you want to make the query. - Blockhash string + BlockHash string `validate:"required"` + + // The block level of which you want to make the query. + Level int // The cycle of which you want to make the query. Cycle int - // The level URL parameter. - Level int - // The delegate public key hash of which you want to make the query. Delegate string @@ -154,21 +154,6 @@ type BakingRightsInput struct { MaxPriority int } -func (b *BakingRightsInput) validate() error { - if b.Blockhash == "" && b.Cycle == 0 { - return errors.New("invalid input: missing key cycle or blockhash") - } else if b.Blockhash != "" && b.Cycle != 0 { - return errors.New("invalid input: cannot have both cycle and blockhash") - } - - err := validator.New().Struct(b) - if err != nil { - return errors.Wrap(err, "invalid input") - } - - return nil -} - /* EndorsingRightsInput is the input for the goTezos.EndorsingRights function. @@ -177,33 +162,18 @@ Function: */ type EndorsingRightsInput struct { // The hash of block (height) of which you want to make the query. - Blockhash string + BlockHash string `validate:"required"` + + // The block level of which you want to make the query. + Level int // The cycle of which you want to make the query. Cycle int - // The level URL parameter. - Level int - // The delegate public key hash of which you want to make the query. Delegate string } -func (e *EndorsingRightsInput) validate() error { - if e.Blockhash == "" && e.Cycle == 0 { - return errors.New("invalid input: missing key cycle or blockhash") - } else if e.Blockhash != "" && e.Cycle != 0 { - return errors.New("invalid input: cannot have both cycle and blockhash") - } - - err := validator.New().Struct(e) - if err != nil { - return errors.Wrap(err, "invalid input") - } - - return nil -} - /* DelegatesInput is the input for the goTezos.Delegates function. @@ -503,17 +473,12 @@ Link: https://tezos.gitlab.io/api/rpc.html#get-block-id-helpers-baking-rights */ func (c *Client) BakingRights(input BakingRightsInput) (*BakingRights, error) { - err := input.validate() - if err != nil { - return &BakingRights{}, errors.Wrapf(err, "could not get delegations for delegate '%s'", input.Delegate) - } - - input.Blockhash, err = c.extractBlockHash(input.Cycle, input.Blockhash) + err := validator.New().Struct(input) if err != nil { - return &BakingRights{}, errors.Wrapf(err, "could not get delegations for delegate '%s'", input.Delegate) + return &BakingRights{}, errors.Wrap(err, "invalid input") } - resp, err := c.get(fmt.Sprintf("/chains/%s/blocks/%s/helpers/baking_rights", c.chain, input.Blockhash), input.contructRPCOptions()...) + resp, err := c.get(fmt.Sprintf("/chains/%s/blocks/%s/helpers/baking_rights", c.chain, input.BlockHash), input.contructRPCOptions()...) if err != nil { return &BakingRights{}, errors.Wrapf(err, "could not get baking rights") } @@ -578,17 +543,12 @@ Link: https://tezos.gitlab.io/api/rpc.html#get-block-id-helpers-endorsing-rights */ func (c *Client) EndorsingRights(input EndorsingRightsInput) (*EndorsingRights, error) { - err := input.validate() - if err != nil { - return &EndorsingRights{}, errors.Wrapf(err, "could not get delegations for delegate '%s'", input.Delegate) - } - - input.Blockhash, err = c.extractBlockHash(input.Cycle, input.Blockhash) + err := validator.New().Struct(input) if err != nil { - return &EndorsingRights{}, errors.Wrapf(err, "could not get delegations for delegate '%s'", input.Delegate) + return &EndorsingRights{}, errors.Wrap(err, "invalid input") } - resp, err := c.get(fmt.Sprintf("/chains/%s/blocks/%s/helpers/endorsing_rights", c.chain, input.Blockhash), input.contructRPCOptions()...) + resp, err := c.get(fmt.Sprintf("/chains/%s/blocks/%s/helpers/endorsing_rights", c.chain, input.BlockHash), input.contructRPCOptions()...) if err != nil { return &EndorsingRights{}, errors.Wrap(err, "could not get endorsing rights") } diff --git a/rpc/delegate_test.go b/rpc/delegate_test.go index 258d2be..ef1c83b 100644 --- a/rpc/delegate_test.go +++ b/rpc/delegate_test.go @@ -433,7 +433,7 @@ func Test_BakingRights(t *testing.T) { assert.Nil(t, err) bakingRights, err := rpc.BakingRights(BakingRightsInput{ - Blockhash: mockBlockHash, + BlockHash: mockBlockHash, }) checkErr(t, tt.wantErr, tt.containsErr, err) @@ -494,7 +494,7 @@ func Test_EndorsingRights(t *testing.T) { assert.Nil(t, err) endorsingRights, err := rpc.EndorsingRights(EndorsingRightsInput{ - Blockhash: mockBlockHash, + BlockHash: mockBlockHash, }) checkErr(t, tt.wantErr, tt.containsErr, err)