-
Notifications
You must be signed in to change notification settings - Fork 355
/
client_txs.go
207 lines (177 loc) · 5.57 KB
/
client_txs.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package gnoclient
import (
"github.com/gnolang/gno/gno.land/pkg/sdk/vm"
gno "github.com/gnolang/gno/gnovm/pkg/gnolang"
"github.com/gnolang/gno/tm2/pkg/amino"
ctypes "github.com/gnolang/gno/tm2/pkg/bft/rpc/core/types"
"github.com/gnolang/gno/tm2/pkg/errors"
"github.com/gnolang/gno/tm2/pkg/std"
)
var (
ErrEmptyPkgPath = errors.New("empty pkg path")
ErrEmptyFuncName = errors.New("empty function name")
ErrInvalidGasWanted = errors.New("invalid gas wanted")
ErrInvalidGasFee = errors.New("invalid gas fee")
ErrMissingSigner = errors.New("missing Signer")
ErrMissingRPCClient = errors.New("missing RPCClient")
)
type BaseTxCfg struct {
GasFee string // Gas fee
GasWanted int64 // Gas wanted
AccountNumber uint64 // Account number
SequenceNumber uint64 // Sequence number
Memo string // Memo
}
// MsgCall - syntax sugar for vm.MsgCall
type MsgCall struct {
PkgPath string // Package path
FuncName string // Function name
Args []string // Function arguments
Send string // Send amount
}
// RunCfg contains configuration options for running a temporary package on the blockchain.
type RunCfg struct {
Package *std.MemPackage
GasFee string // Gas fee
GasWanted int64 // Gas wanted
AccountNumber uint64 // Account number
SequenceNumber uint64 // Sequence number
Memo string // Memo
}
// Call executes a contract call on the blockchain.
func (c *Client) Call(cfg BaseTxCfg, msgs ...MsgCall) (*ctypes.ResultBroadcastTxCommit, error) {
// Validate required client fields.
if err := c.validateSigner(); err != nil {
return nil, err
}
if err := c.validateRPCClient(); err != nil {
return nil, err
}
// Validate base transaction config
if err := cfg.validateBaseTxConfig(); err != nil {
return nil, err
}
// Parse MsgCall slice
vmMsgs := make([]vm.MsgCall, 0, len(msgs))
for _, msg := range msgs {
// Validate MsgCall fields
if err := msg.validateMsgCall(); err != nil {
return nil, err
}
// Parse send coins
send, err := std.ParseCoins(msg.Send)
if err != nil {
return nil, err
}
// Unwrap syntax sugar to vm.MsgCall slice
vmMsgs = append(vmMsgs, vm.MsgCall{
Caller: c.Signer.Info().GetAddress(),
PkgPath: msg.PkgPath,
Func: msg.FuncName,
Args: msg.Args,
Send: send,
})
}
// Cast vm.MsgCall back into std.Msg
stdMsgs := make([]std.Msg, len(vmMsgs))
for i, msg := range vmMsgs {
stdMsgs[i] = msg
}
// Parse gas fee
gasFeeCoins, err := std.ParseCoin(cfg.GasFee)
if err != nil {
return nil, err
}
// Pack transaction
tx := std.Tx{
Msgs: stdMsgs,
Fee: std.NewFee(cfg.GasWanted, gasFeeCoins),
Signatures: nil,
Memo: cfg.Memo,
}
return c.signAndBroadcastTxCommit(tx, cfg.AccountNumber, cfg.SequenceNumber)
}
// Temporarily load cfg.Package on the blockchain and run main() which can
// call realm functions and use println() to output to the "console".
// This returns bres where string(bres.DeliverTx.Data) is the "console" output.
func (c *Client) Run(cfg RunCfg) (*ctypes.ResultBroadcastTxCommit, error) {
// Validate required client fields.
if err := c.validateSigner(); err != nil {
return nil, errors.Wrap(err, "validate signer")
}
if err := c.validateRPCClient(); err != nil {
return nil, errors.Wrap(err, "validate RPC client")
}
memPkg := cfg.Package
gasWanted := cfg.GasWanted
gasFee := cfg.GasFee
sequenceNumber := cfg.SequenceNumber
accountNumber := cfg.AccountNumber
memo := cfg.Memo
// Validate config.
if memPkg.IsEmpty() {
return nil, errors.New("found an empty package " + memPkg.Path)
}
// Parse gas wanted & fee.
gasFeeCoins, err := std.ParseCoin(gasFee)
if err != nil {
return nil, errors.Wrap(err, "parsing gas fee coin")
}
caller := c.Signer.Info().GetAddress()
// precompile and validate syntax
err = gno.PrecompileAndCheckMempkg(memPkg)
if err != nil {
return nil, errors.Wrap(err, "precompile and check")
}
memPkg.Name = "main"
memPkg.Path = "gno.land/r/" + caller.String() + "/run"
// Construct message & transaction and marshal.
msg := vm.MsgRun{
Caller: caller,
Package: memPkg,
}
tx := std.Tx{
Msgs: []std.Msg{msg},
Fee: std.NewFee(gasWanted, gasFeeCoins),
Signatures: nil,
Memo: memo,
}
return c.signAndBroadcastTxCommit(tx, accountNumber, sequenceNumber)
}
// signAndBroadcastTxCommit signs a transaction and broadcasts it, returning the result.
func (c Client) signAndBroadcastTxCommit(tx std.Tx, accountNumber, sequenceNumber uint64) (*ctypes.ResultBroadcastTxCommit, error) {
caller := c.Signer.Info().GetAddress()
if sequenceNumber == 0 || accountNumber == 0 {
account, _, err := c.QueryAccount(caller)
if err != nil {
return nil, errors.Wrap(err, "query account")
}
accountNumber = account.AccountNumber
sequenceNumber = account.Sequence
}
signCfg := SignCfg{
UnsignedTX: tx,
SequenceNumber: sequenceNumber,
AccountNumber: accountNumber,
}
signedTx, err := c.Signer.Sign(signCfg)
if err != nil {
return nil, errors.Wrap(err, "sign")
}
bz, err := amino.Marshal(signedTx)
if err != nil {
return nil, errors.Wrap(err, "marshaling tx binary bytes")
}
bres, err := c.RPCClient.BroadcastTxCommit(bz)
if err != nil {
return nil, errors.Wrap(err, "broadcasting bytes")
}
if bres.CheckTx.IsErr() {
return bres, errors.Wrap(bres.CheckTx.Error, "check transaction failed: log:%s", bres.CheckTx.Log)
}
if bres.DeliverTx.IsErr() {
return bres, errors.Wrap(bres.DeliverTx.Error, "deliver transaction failed: log:%s", bres.DeliverTx.Log)
}
return bres, nil
}
// TODO: Add more functionality, examples, and unit tests.