[#801] morph/netmap: Add wrapper options

Add constructor options on wrapper level
of `netmap` contract.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-09-06 15:14:16 +03:00 committed by Pavel Karpy
parent 74051556de
commit 425c02b0ec
2 changed files with 38 additions and 3 deletions

View file

@ -476,7 +476,7 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error
return nil, err return nil, err
} }
server.netmapClient, err = nmWrapper.NewFromMorph(server.morphClient, server.contracts.netmap, fee, client.TryNotary(), client.AsAlphabet()) server.netmapClient, err = nmWrapper.NewFromMorph(server.morphClient, server.contracts.netmap, fee, nmWrapper.TryNotary(), nmWrapper.AsAlphabet())
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -22,9 +22,25 @@ type Wrapper struct {
client *netmap.Client client *netmap.Client
} }
// Option allows to set an optional
// parameter of Wrapper.
type Option func(*opts)
type opts []client.StaticClientOption
func defaultOpts() *opts {
return new(opts)
}
// NewFromMorph returns the wrapper instance from the raw morph client. // NewFromMorph returns the wrapper instance from the raw morph client.
func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8, opts ...client.StaticClientOption) (*Wrapper, error) { func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8, opts ...Option) (*Wrapper, error) {
staticClient, err := client.NewStatic(cli, contract, fee, opts...) o := defaultOpts()
for i := range opts {
opts[i](o)
}
staticClient, err := client.NewStatic(cli, contract, fee, ([]client.StaticClientOption)(*o)...)
if err != nil { if err != nil {
return nil, fmt.Errorf("can't create netmap static client: %w", err) return nil, fmt.Errorf("can't create netmap static client: %w", err)
} }
@ -36,3 +52,22 @@ func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8,
return &Wrapper{client: enhancedNetmapClient}, nil return &Wrapper{client: enhancedNetmapClient}, nil
} }
// TryNotary returns option to enable
// notary invocation tries.
func TryNotary() Option {
return func(o *opts) {
*o = append(*o, client.TryNotary())
}
}
// AsAlphabet returns option to sign main TX
// of notary requests with client's private
// key.
//
// Considered to be used by IR nodes only.
func AsAlphabet() Option {
return func(o *opts) {
*o = append(*o, client.AsAlphabet())
}
}