2020-07-24 13:54:03 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2021-11-09 14:52:48 +00:00
|
|
|
"fmt"
|
2020-07-24 13:54:03 +00:00
|
|
|
|
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-12-07 09:54:21 +00:00
|
|
|
staticOpts
|
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
|
|
|
|
}
|
|
|
|
|
2021-05-25 12:07:57 +00:00
|
|
|
type staticOpts struct {
|
|
|
|
tryNotary bool
|
2021-12-07 09:54:21 +00:00
|
|
|
alpha bool // use client's key to sign notary request's main TX
|
|
|
|
|
2023-05-17 14:41:28 +00:00
|
|
|
fee fixedn.Fixed8
|
2021-05-25 12:07:57 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 18:14:38 +00:00
|
|
|
// WithNotary returns notary status of the client.
|
|
|
|
//
|
|
|
|
// See also TryNotary.
|
|
|
|
func (s *StaticClient) WithNotary() bool {
|
2022-08-31 11:09:55 +00:00
|
|
|
return s.client.IsNotaryEnabled()
|
2022-05-13 18:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsAlpha returns Alphabet status of the client.
|
|
|
|
//
|
|
|
|
// See also AsAlphabet.
|
|
|
|
func (s *StaticClient) IsAlpha() bool {
|
|
|
|
return s.alpha
|
|
|
|
}
|
|
|
|
|
2021-05-25 12:07:57 +00:00
|
|
|
// StaticClientOption allows to set an optional
|
|
|
|
// parameter of StaticClient.
|
|
|
|
type StaticClientOption func(*staticOpts)
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// NewStatic creates, initializes and returns the StaticClient instance.
|
|
|
|
//
|
|
|
|
// If provided Client instance is nil, ErrNilClient is returned.
|
2021-12-07 09:54:21 +00:00
|
|
|
//
|
|
|
|
// Specified fee is used by default. Per-operation fees can be customized via WithCustomFee option.
|
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-12-07 09:54:21 +00:00
|
|
|
c := &StaticClient{
|
|
|
|
client: client,
|
|
|
|
scScriptHash: scriptHash,
|
|
|
|
}
|
|
|
|
|
2023-05-17 14:41:28 +00:00
|
|
|
c.fee = fee
|
2021-05-25 12:07:57 +00:00
|
|
|
|
|
|
|
for i := range opts {
|
2021-12-07 09:54:21 +00:00
|
|
|
opts[i](&c.staticOpts)
|
2021-05-25 12:07:57 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 09:54:21 +00:00
|
|
|
return c, nil
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 17:19:16 +00:00
|
|
|
// Morph return wrapped raw morph client.
|
|
|
|
func (s StaticClient) Morph() *Client {
|
|
|
|
return s.client
|
|
|
|
}
|
|
|
|
|
2021-11-09 14:52:48 +00:00
|
|
|
// InvokePrm groups parameters of the Invoke operation.
|
|
|
|
type InvokePrm struct {
|
2022-01-29 11:21:57 +00:00
|
|
|
TestInvokePrm
|
2021-11-09 14:52:48 +00:00
|
|
|
|
|
|
|
// optional parameters
|
|
|
|
InvokePrmOptional
|
|
|
|
}
|
|
|
|
|
|
|
|
// InvokePrmOptional groups optional parameters of the Invoke operation.
|
|
|
|
type InvokePrmOptional struct {
|
|
|
|
// hash is an optional hash of the transaction
|
|
|
|
// that generated the notification that required
|
|
|
|
// to invoke notary request.
|
|
|
|
// It is used to generate same but unique nonce and
|
|
|
|
// `validUntilBlock` values by all notification
|
|
|
|
// receivers.
|
|
|
|
hash *util.Uint256
|
2023-04-25 12:09:20 +00:00
|
|
|
// controlTX controls whether the invoke method will use a rounded
|
|
|
|
// block height value, which is useful for control transactions which
|
|
|
|
// are required to be produced by all nodes with very high probability.
|
|
|
|
// It's only used by notary transactions and it affects only the
|
|
|
|
// computation of `validUntilBlock` values.
|
|
|
|
controlTX bool
|
2021-11-09 14:52:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetHash sets optional hash of the transaction.
|
|
|
|
// If hash is set and notary is enabled, StaticClient
|
|
|
|
// uses it for notary nonce and `validUntilBlock`
|
|
|
|
// calculation.
|
|
|
|
func (i *InvokePrmOptional) SetHash(hash util.Uint256) {
|
|
|
|
i.hash = &hash
|
|
|
|
}
|
|
|
|
|
2023-04-25 12:09:20 +00:00
|
|
|
// SetControlTX sets whether a control transaction will be used.
|
|
|
|
func (i *InvokePrmOptional) SetControlTX(b bool) {
|
|
|
|
i.controlTX = b
|
|
|
|
}
|
|
|
|
|
2023-10-16 15:15:04 +00:00
|
|
|
// IsControl gets whether a control transaction will be used.
|
|
|
|
func (i *InvokePrmOptional) IsControl() bool {
|
|
|
|
return i.controlTX
|
|
|
|
}
|
|
|
|
|
2023-11-07 15:13:26 +00:00
|
|
|
type InvokeRes struct {
|
|
|
|
VUB uint32
|
|
|
|
}
|
|
|
|
|
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
|
|
|
//
|
2021-11-09 14:52:48 +00:00
|
|
|
// If TryNotary is provided:
|
2022-08-15 16:20:20 +00:00
|
|
|
// - if AsAlphabet is provided, calls NotaryInvoke;
|
|
|
|
// - otherwise, calls NotaryInvokeNotAlpha.
|
2021-12-07 09:54:21 +00:00
|
|
|
//
|
|
|
|
// If fee for the operation executed using specified method is customized, then StaticClient uses it.
|
|
|
|
// Otherwise, default fee is used.
|
2023-11-07 15:13:26 +00:00
|
|
|
func (s StaticClient) Invoke(prm InvokePrm) (InvokeRes, error) {
|
|
|
|
var res InvokeRes
|
|
|
|
var err error
|
2021-05-25 12:07:57 +00:00
|
|
|
if s.tryNotary {
|
2021-08-25 11:05:07 +00:00
|
|
|
if s.alpha {
|
2021-11-09 14:52:48 +00:00
|
|
|
var (
|
|
|
|
nonce uint32 = 1
|
|
|
|
vubP *uint32
|
|
|
|
vub uint32
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if prm.hash != nil {
|
2023-04-25 12:09:20 +00:00
|
|
|
if prm.controlTX {
|
|
|
|
nonce, vub, err = s.client.CalculateNonceAndVUBControl(*prm.hash)
|
|
|
|
} else {
|
|
|
|
nonce, vub, err = s.client.CalculateNonceAndVUB(*prm.hash)
|
|
|
|
}
|
2021-11-09 14:52:48 +00:00
|
|
|
if err != nil {
|
2023-11-07 15:13:26 +00:00
|
|
|
return InvokeRes{}, fmt.Errorf("could not calculate nonce and VUB for notary alphabet invoke: %w", err)
|
2021-11-09 14:52:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vubP = &vub
|
|
|
|
}
|
|
|
|
|
2023-11-07 15:13:26 +00:00
|
|
|
res.VUB, err = s.client.NotaryInvoke(s.scScriptHash, s.fee, nonce, vubP, prm.method, prm.args...)
|
|
|
|
return res, err
|
2021-08-25 11:05:07 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 15:13:26 +00:00
|
|
|
res.VUB, err = s.client.NotaryInvokeNotAlpha(s.scScriptHash, s.fee, prm.method, prm.args...)
|
|
|
|
return res, err
|
2021-05-25 12:07:57 +00:00
|
|
|
}
|
|
|
|
|
2023-11-07 15:13:26 +00:00
|
|
|
res.VUB, err = s.client.Invoke(
|
2020-07-24 13:54:03 +00:00
|
|
|
s.scScriptHash,
|
2023-05-17 14:41:28 +00:00
|
|
|
s.fee,
|
2021-11-09 14:52:48 +00:00
|
|
|
prm.method,
|
|
|
|
prm.args...,
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
2023-11-07 15:13:26 +00:00
|
|
|
return res, err
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2021-11-09 14:52:48 +00:00
|
|
|
// TestInvokePrm groups parameters of the TestInvoke operation.
|
|
|
|
type TestInvokePrm struct {
|
|
|
|
method string
|
2023-02-21 11:42:45 +00:00
|
|
|
args []any
|
2021-11-09 14:52:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetMethod sets method of the contract to call.
|
|
|
|
func (ti *TestInvokePrm) SetMethod(method string) {
|
|
|
|
ti.method = method
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetArgs sets arguments of the contact call.
|
2023-02-21 11:42:45 +00:00
|
|
|
func (ti *TestInvokePrm) SetArgs(args ...any) {
|
2021-11-09 14:52:48 +00:00
|
|
|
ti.args = args
|
|
|
|
}
|
|
|
|
|
2020-07-24 13:54:03 +00:00
|
|
|
// TestInvoke calls TestInvoke method of Client with static internal script hash.
|
2021-11-09 14:52:48 +00:00
|
|
|
func (s StaticClient) TestInvoke(prm TestInvokePrm) ([]stackitem.Item, error) {
|
2020-07-24 13:54:03 +00:00
|
|
|
return s.client.TestInvoke(
|
|
|
|
s.scScriptHash,
|
2021-11-09 14:52:48 +00:00
|
|
|
prm.method,
|
|
|
|
prm.args...,
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|