-
Notifications
You must be signed in to change notification settings - Fork 38
/
static.go
127 lines (105 loc) · 3.17 KB
/
static.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package client
import (
"errors"
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)
// 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 {
tryNotary bool
alpha bool // use client's key to sign notary request's main TX
client *Client // neo-go client instance
scScriptHash util.Uint160 // contract script-hash
fee fixedn.Fixed8 // invocation fee
}
type staticOpts struct {
tryNotary bool
alpha bool
}
// StaticClientOption allows to set an optional
// parameter of StaticClient.
type StaticClientOption func(*staticOpts)
func defaultStaticOpts() *staticOpts {
return new(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.
//
// If provided Client instance is nil, ErrNilClient is returned.
func NewStatic(client *Client, scriptHash util.Uint160, fee fixedn.Fixed8, opts ...StaticClientOption) (*StaticClient, error) {
if client == nil {
return nil, ErrNilClient
}
o := defaultStaticOpts()
for i := range opts {
opts[i](o)
}
return &StaticClient{
tryNotary: o.tryNotary,
alpha: o.alpha,
client: client,
scScriptHash: scriptHash,
fee: fee,
}, nil
}
// Morph return wrapped raw morph client.
func (s StaticClient) Morph() *Client {
return s.client
}
// Invoke calls Invoke method of Client with static internal script hash and fee.
// Supported args types are the same as in Client.
//
// If TryNotary is provided, calls NotaryInvoke on Client.
func (s StaticClient) Invoke(method string, args ...interface{}) error {
if s.tryNotary {
if s.alpha {
// FIXME: do not use constant nonce for alphabet NR: #844
return s.client.NotaryInvoke(s.scScriptHash, s.fee, 1, method, args...)
}
return s.client.NotaryInvokeNotAlpha(s.scScriptHash, s.fee, method, args...)
}
return s.client.Invoke(
s.scScriptHash,
s.fee,
method,
args...,
)
}
// TestInvoke calls TestInvoke method of Client with static internal script hash.
func (s StaticClient) TestInvoke(method string, args ...interface{}) ([]stackitem.Item, error) {
return s.client.TestInvoke(
s.scScriptHash,
method,
args...,
)
}
// ContractAddress returns the address of the associated contract.
func (s StaticClient) ContractAddress() util.Uint160 {
return s.scScriptHash
}
// 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
}
}