[#625] morph/client: make all constructors return no error

In some places we panic, in some we return error, in some (audit) just return a client.
However in all of the places static client is created immediately before
the sugared-client creation.

This commit makes all constructors to just return a client for the sake
of code simplification and unification.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-01-29 16:21:49 +03:00 committed by LeL
parent 3c5b62d839
commit 0decb95591
11 changed files with 12 additions and 73 deletions

View file

@ -26,12 +26,6 @@ const (
) )
// New creates, initializes and returns the Client instance. // New creates, initializes and returns the Client instance.
// func New(c *client.StaticClient) *Client {
// If StaticClient is nil, client.ErrNilStaticClient is returned. return &Client{client: c}
func New(c *client.StaticClient) (*Client, error) {
if c == nil {
return nil, client.ErrNilStaticClient
}
return &Client{client: c}, nil
} }

View file

@ -67,13 +67,8 @@ func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8,
return nil, fmt.Errorf("could not create static client of Balance contract: %w", err) return nil, fmt.Errorf("could not create static client of Balance contract: %w", err)
} }
enhancedBalanceClient, err := balance.New(staticClient)
if err != nil {
return nil, fmt.Errorf("could not create Balance contract client: %w", err)
}
return &Wrapper{ return &Wrapper{
StaticClient: staticClient, StaticClient: staticClient,
client: enhancedBalanceClient, client: balance.New(staticClient),
}, nil }, nil
} }

View file

@ -36,14 +36,8 @@ const (
) )
// New creates, initializes and returns the Client instance. // New creates, initializes and returns the Client instance.
// func New(c *client.StaticClient) *Client {
// If StaticClient is nil, client.ErrNilStaticClient is returned. return &Client{client: c}
func New(c *client.StaticClient) (*Client, error) {
if c == nil {
return nil, client.ErrNilStaticClient
}
return &Client{client: c}, nil
} }
// Morph returns raw morph client. // Morph returns raw morph client.

View file

@ -93,13 +93,8 @@ func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8,
return nil, fmt.Errorf("can't create container static client: %w", err) return nil, fmt.Errorf("can't create container static client: %w", err)
} }
enhancedContainerClient, err := container.New(staticClient)
if err != nil {
return nil, fmt.Errorf("can't create container morph client: %w", err)
}
return &Wrapper{ return &Wrapper{
StaticClient: staticClient, StaticClient: staticClient,
client: enhancedContainerClient, client: container.New(staticClient),
}, nil }, nil
} }

View file

@ -24,12 +24,6 @@ const (
) )
// New creates, initializes and returns the Client instance. // New creates, initializes and returns the Client instance.
//
// If StaticClient is nil, panic occurs.
func New(c *client.StaticClient) *Client { func New(c *client.StaticClient) *Client {
if c == nil {
panic("static client is nil")
}
return &Client{client: c} return &Client{client: c}
} }

View file

@ -23,12 +23,6 @@ const (
) )
// New creates, initializes and returns the Client instance. // New creates, initializes and returns the Client instance.
//
// If StaticClient is nil, panic occurs.
func New(c *client.StaticClient) *Client { func New(c *client.StaticClient) *Client {
if c == nil {
panic("static client is nil")
}
return &Client{client: c} return &Client{client: c}
} }

View file

@ -39,14 +39,8 @@ const (
) )
// New creates, initializes and returns the Client instance. // New creates, initializes and returns the Client instance.
// func New(c *client.StaticClient) *Client {
// If StaticClient is nil, client.ErrNilStaticClient is returned. return &Client{client: c}
func New(c *client.StaticClient) (*Client, error) {
if c == nil {
return nil, client.ErrNilStaticClient
}
return &Client{client: c}, nil
} }
// Morph returns raw morph client. // Morph returns raw morph client.

View file

@ -48,14 +48,9 @@ func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8,
return nil, fmt.Errorf("can't create netmap static client: %w", err) return nil, fmt.Errorf("can't create netmap static client: %w", err)
} }
enhancedNetmapClient, err := netmap.New(staticClient)
if err != nil {
return nil, fmt.Errorf("can't create netmap morph client: %w", err)
}
return &Wrapper{ return &Wrapper{
StaticClient: staticClient, StaticClient: staticClient,
client: enhancedNetmapClient, client: netmap.New(staticClient),
}, nil }, nil
} }

View file

@ -24,14 +24,8 @@ const (
) )
// New creates, initializes and returns the Client instance. // New creates, initializes and returns the Client instance.
// func New(c *client.StaticClient) *Client {
// If StaticClient is nil, client.ErrNilStaticClient is returned. return &Client{client: c}
func New(c *client.StaticClient) (*Client, error) {
if c == nil {
return nil, client.ErrNilStaticClient
}
return &Client{client: c}, nil
} }
// Morph returns raw morph client. // Morph returns raw morph client.

View file

@ -65,13 +65,8 @@ func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8,
return nil, fmt.Errorf("could not create static client of reputation contract: %w", err) return nil, fmt.Errorf("could not create static client of reputation contract: %w", err)
} }
enhancedRepurationClient, err := reputation.New(staticClient)
if err != nil {
return nil, fmt.Errorf("could not create reputation contract client: %w", err)
}
return &ClientWrapper{ return &ClientWrapper{
StaticClient: staticClient, StaticClient: staticClient,
client: enhancedRepurationClient, client: reputation.New(staticClient),
}, nil }, nil
} }

View file

@ -1,7 +1,6 @@
package client package client
import ( import (
"errors"
"fmt" "fmt"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
@ -35,10 +34,6 @@ type staticOpts struct {
// parameter of StaticClient. // parameter of StaticClient.
type StaticClientOption func(*staticOpts) type StaticClientOption func(*staticOpts)
// ErrNilStaticClient is returned by functions that expect
// a non-nil StaticClient pointer, but received nil.
var ErrNilStaticClient = errors.New("static client is nil")
// NewStatic creates, initializes and returns the StaticClient instance. // NewStatic creates, initializes and returns the StaticClient instance.
// //
// If provided Client instance is nil, ErrNilClient is returned. // If provided Client instance is nil, ErrNilClient is returned.