[#625] morph/client: make method names constant

We don't use custom names and the only place where custom method option
is used it provides the default name and can be omitted.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-01-29 16:06:36 +03:00 committed by LeL
parent 35dec2f494
commit 3c5b62d839
44 changed files with 249 additions and 1097 deletions

View file

@ -1,8 +1,6 @@
package balance
import (
"errors"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
)
@ -16,113 +14,24 @@ import (
// and can lead to panic.
type Client struct {
client *client.StaticClient // static Balance contract client
*cfg // contract method names
}
// ErrNilClient is returned by functions that expect
// a non-nil Client pointer, but received nil.
var ErrNilClient = errors.New("balance contract client is nil")
// Option is a client configuration change function.
type Option func(*cfg)
type cfg struct {
transferXMethod, // transferX method name for invocation
mintMethod,
burnMethod,
lockMethod,
balanceOfMethod, // balanceOf method name for invocation
decimalsMethod string // decimals method name for invocation
}
const (
defaultTransferXMethod = "transferX" // default "transferX" method name
defaultMintMethod = "mint" // default "mint" method name
defaultBurnMethod = "burn" // default "burn" method name
defaultLockMethod = "lock" // default "lock" method name
defaultBalanceOfMethod = "balanceOf" // default "balance of" method name
defaultDecimalsMethod = "decimals" // default decimals method name
transferXMethod = "transferX"
mintMethod = "mint"
burnMethod = "burn"
lockMethod = "lock"
balanceOfMethod = "balanceOf"
decimalsMethod = "decimals"
)
func defaultConfig() *cfg {
return &cfg{
transferXMethod: defaultTransferXMethod,
mintMethod: defaultMintMethod,
burnMethod: defaultBurnMethod,
lockMethod: defaultLockMethod,
balanceOfMethod: defaultBalanceOfMethod,
decimalsMethod: defaultDecimalsMethod,
}
}
// New creates, initializes and returns the Client instance.
//
// If StaticClient is nil, client.ErrNilStaticClient is returned.
//
// Other values are set according to provided options, or by default:
// * "balance of" method name: balanceOf;
// * decimals method name: decimals.
//
// If desired option satisfies the default value, it can be omitted.
// If multiple options of the same config value are supplied,
// the option with the highest index in the arguments will be used.
func New(c *client.StaticClient, opts ...Option) (*Client, error) {
func New(c *client.StaticClient) (*Client, error) {
if c == nil {
return nil, client.ErrNilStaticClient
}
res := &Client{
client: c,
cfg: defaultConfig(), // build default configuration
}
// apply options
for _, opt := range opts {
opt(res.cfg)
}
return res, nil
}
// WithBalanceOfMethod returns a client constructor option that
// specifies the "balance of" method name.
//
// Ignores empty value.
//
// If option not provided, "balanceOf" is used.
func WithBalanceOfMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.balanceOfMethod = n
}
}
}
// WithDecimalsMethod returns a client constructor option that
// specifies the method name of decimals receiving operation.
//
// Ignores empty value.
//
// If option not provided, "decimals" is used.
func WithDecimalsMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.decimalsMethod = n
}
}
}
// WithTransferXMethod returns a client constructor option that
// specifies the "transferX" method name.
//
// Ignores empty value.
//
// If option not provided, "transferX" is used.
func WithTransferXMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.transferXMethod = n
}
}
return &Client{client: c}, nil
}