2020-07-24 13:54:03 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2020-12-11 08:33:27 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2020-07-24 13:54:03 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2020-08-27 11:57:13 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// StaticClient is a wrapper over Neo:Morph client
|
|
|
|
// that invokes single smart contract methods with fixed fee.
|
|
|
|
//
|
|
|
|
// Working static client must be created via constructor NewStatic.
|
|
|
|
// Using the StaticClient that has been created with new(StaticClient)
|
|
|
|
// expression (or just declaring a StaticClient variable) is unsafe
|
|
|
|
// and can lead to panic.
|
|
|
|
type StaticClient struct {
|
2021-05-25 12:07:57 +00:00
|
|
|
tryNotary bool
|
2021-08-25 11:05:07 +00:00
|
|
|
alpha bool // use client's key to sign notary request's main TX
|
2021-05-25 12:07:57 +00:00
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
client *Client // neo-go client instance
|
|
|
|
|
|
|
|
scScriptHash util.Uint160 // contract script-hash
|
|
|
|
|
2020-12-11 08:33:27 +00:00
|
|
|
fee fixedn.Fixed8 // invocation fee
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 12:07:57 +00:00
|
|
|
type staticOpts struct {
|
|
|
|
tryNotary bool
|
2021-08-25 11:05:07 +00:00
|
|
|
alpha bool
|
2021-05-25 12:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StaticClientOption allows to set an optional
|
|
|
|
// parameter of StaticClient.
|
|
|
|
type StaticClientOption func(*staticOpts)
|
|
|
|
|
|
|
|
func defaultStaticOpts() *staticOpts {
|
|
|
|
return new(staticOpts)
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// If provided Client instance is nil, ErrNilClient is returned.
|
2021-05-25 12:07:57 +00:00
|
|
|
func NewStatic(client *Client, scriptHash util.Uint160, fee fixedn.Fixed8, opts ...StaticClientOption) (*StaticClient, error) {
|
2020-07-24 13:54:03 +00:00
|
|
|
if client == nil {
|
|
|
|
return nil, ErrNilClient
|
|
|
|
}
|
|
|
|
|
2021-05-25 12:07:57 +00:00
|
|
|
o := defaultStaticOpts()
|
|
|
|
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](o)
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
return &StaticClient{
|
2021-05-25 12:07:57 +00:00
|
|
|
tryNotary: o.tryNotary,
|
2021-08-25 11:05:07 +00:00
|
|
|
alpha: o.alpha,
|
2020-07-24 13:54:03 +00:00
|
|
|
client: client,
|
|
|
|
scScriptHash: scriptHash,
|
|
|
|
fee: fee,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-09-03 17:19:16 +00:00
|
|
|
// Morph return wrapped raw morph client.
|
|
|
|
func (s StaticClient) Morph() *Client {
|
|
|
|
return s.client
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// Invoke calls Invoke method of Client with static internal script hash and fee.
|
|
|
|
// Supported args types are the same as in Client.
|
2021-05-25 12:07:57 +00:00
|
|
|
//
|
|
|
|
// If TryNotary is provided, calls NotaryInvoke on Client.
|
2020-07-24 13:54:03 +00:00
|
|
|
func (s StaticClient) Invoke(method string, args ...interface{}) error {
|
2021-05-25 12:07:57 +00:00
|
|
|
if s.tryNotary {
|
2021-08-25 11:05:07 +00:00
|
|
|
if s.alpha {
|
2021-09-23 16:56:13 +00:00
|
|
|
// FIXME: do not use constant nonce for alphabet NR: #844
|
|
|
|
return s.client.NotaryInvoke(s.scScriptHash, s.fee, 1, method, args...)
|
2021-08-25 11:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s.client.NotaryInvokeNotAlpha(s.scScriptHash, s.fee, method, args...)
|
2021-05-25 12:07:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
return s.client.Invoke(
|
|
|
|
s.scScriptHash,
|
|
|
|
s.fee,
|
|
|
|
method,
|
|
|
|
args...,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestInvoke calls TestInvoke method of Client with static internal script hash.
|
2020-08-27 11:57:13 +00:00
|
|
|
func (s StaticClient) TestInvoke(method string, args ...interface{}) ([]stackitem.Item, error) {
|
2020-07-24 13:54:03 +00:00
|
|
|
return s.client.TestInvoke(
|
|
|
|
s.scScriptHash,
|
|
|
|
method,
|
|
|
|
args...,
|
|
|
|
)
|
|
|
|
}
|
2021-08-25 11:05:07 +00:00
|
|
|
|
2021-09-15 10:45:28 +00:00
|
|
|
// ContractAddress returns the address of the associated contract.
|
|
|
|
func (s StaticClient) ContractAddress() util.Uint160 {
|
|
|
|
return s.scScriptHash
|
|
|
|
}
|
|
|
|
|
2021-08-25 11:05:07 +00:00
|
|
|
// TryNotary returns option to enable
|
|
|
|
// notary invocation tries.
|
|
|
|
func TryNotary() StaticClientOption {
|
|
|
|
return func(o *staticOpts) {
|
|
|
|
o.tryNotary = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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() StaticClientOption {
|
|
|
|
return func(o *staticOpts) {
|
|
|
|
o.alpha = true
|
|
|
|
}
|
|
|
|
}
|