forked from gagliardetto/solana-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulateTransaction.go
137 lines (119 loc) · 4.09 KB
/
simulateTransaction.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
// Copyright 2021 github.com/gagliardetto
// This file has been modified by github.com/gagliardetto
//
// Copyright 2020 dfuse Platform Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package rpc
import (
"context"
"encoding/base64"
"fmt"
"github.com/shizuautomations/solana-go"
)
type SimulateTransactionResponse struct {
RPCContext
Value *SimulateTransactionResult `json:"value"`
}
type SimulateTransactionResult struct {
// Error if transaction failed, null if transaction succeeded.
Err interface{} `json:"err,omitempty"`
// Array of log messages the transaction instructions output during execution,
// null if simulation failed before the transaction was able to execute
// (for example due to an invalid blockhash or signature verification failure)
Logs []string `json:"logs,omitempty"`
// Array of accounts with the same length as the accounts.addresses array in the request.
Accounts []*Account `json:"accounts"`
// The number of compute budget units consumed during the processing of this transaction.
UnitsConsumed *uint64 `json:"unitsConsumed,omitempty"`
}
// SimulateTransaction simulates sending a transaction.
func (cl *Client) SimulateTransaction(
ctx context.Context,
transaction *solana.Transaction,
) (out *SimulateTransactionResponse, err error) {
return cl.SimulateTransactionWithOpts(
ctx,
transaction,
nil,
)
}
type SimulateTransactionOpts struct {
// If true the transaction signatures will be verified
// (default: false, conflicts with ReplaceRecentBlockhash)
SigVerify bool
// Commitment level to simulate the transaction at.
// (default: "finalized").
Commitment CommitmentType
// If true the transaction recent blockhash will be replaced with the most recent blockhash.
// (default: false, conflicts with SigVerify)
ReplaceRecentBlockhash bool
Accounts *SimulateTransactionAccountsOpts
}
type SimulateTransactionAccountsOpts struct {
// (optional) Encoding for returned Account data,
// either "base64" (default), "base64+zstd" or "jsonParsed".
// - "jsonParsed" encoding attempts to use program-specific state parsers
// to return more human-readable and explicit account state data.
// If "jsonParsed" is requested but a parser cannot be found,
// the field falls back to binary encoding, detectable when
// the data field is type <string>.
Encoding solana.EncodingType
// An array of accounts to return.
Addresses []solana.PublicKey
}
// SimulateTransaction simulates sending a transaction.
func (cl *Client) SimulateTransactionWithOpts(
ctx context.Context,
transaction *solana.Transaction,
opts *SimulateTransactionOpts,
) (out *SimulateTransactionResponse, err error) {
txData, err := transaction.MarshalBinary()
if err != nil {
return nil, fmt.Errorf("send transaction: encode transaction: %w", err)
}
return cl.SimulateRawTransactionWithOpts(ctx, txData, opts)
}
func (cl *Client) SimulateRawTransactionWithOpts(
ctx context.Context,
txData []byte,
opts *SimulateTransactionOpts,
) (out *SimulateTransactionResponse, err error) {
obj := M{
"encoding": "base64",
}
if opts != nil {
if opts.SigVerify {
obj["sigVerify"] = opts.SigVerify
}
if opts.Commitment != "" {
obj["commitment"] = opts.Commitment
}
if opts.ReplaceRecentBlockhash {
obj["replaceRecentBlockhash"] = opts.ReplaceRecentBlockhash
}
if opts.Accounts != nil {
obj["accounts"] = M{
"encoding": opts.Accounts.Encoding,
"addresses": opts.Accounts.Addresses,
}
}
}
b64Data := base64.StdEncoding.EncodeToString(txData)
params := []interface{}{
b64Data,
obj,
}
err = cl.rpcClient.CallForInto(ctx, &out, "simulateTransaction", params)
return
}