-
Notifications
You must be signed in to change notification settings - Fork 80
/
simulate_transaction.go
53 lines (45 loc) · 1.97 KB
/
simulate_transaction.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
package rpc
import (
"context"
"errors"
)
type SimulateTransactionEncoding string
const (
SimulateTransactionEncodingBase58 SimulateTransactionEncoding = "base58" // (slow, DEPRECATED)
SimulateTransactionEncodingBase64 SimulateTransactionEncoding = "base64"
)
type SimulateTransactionConfig struct {
SigVerify bool `json:"sigVerify,omitempty"` // default: false, conflicts with replace blockhash
PreflightCommitment Commitment `json:"preflightCommitment,omitempty"` // default: finalized
Encoding SimulateTransactionEncoding `json:"encoding,omitempty"` // default: "base58", either "base58" or "base64"
ReplaceRecentBlockhash bool `json:"replaceRecentBlockhash,omitempty"` // default: false, conflicts with sigVerify
Accounts *SimulateTransactionConfigAccounts `json:"accounts,omitempty"`
}
type SimulateTransactionConfigAccountsEncoding string
type SimulateTransactionConfigAccounts struct {
Encoding string `json:"encoding,omitempty"`
Addresses []string `json:"addresses"`
}
type SimulateTransactionResponse struct {
Err interface{} `json:"err"`
Logs []string `json:"logs"`
Accounts []GetAccountInfoResponse `json:"accounts"`
}
// SimulateTransaction simulate sending a transaction
func (s *RpcClient) SimulateTransaction(ctx context.Context, rawTx string, cfg SimulateTransactionConfig) (SimulateTransactionResponse, error) {
res := struct {
GeneralResponse
Result struct {
Context Context `json:"context"`
Value SimulateTransactionResponse `json:"value"`
} `json:"result"`
}{}
err := s.request(ctx, "simulateTransaction", []interface{}{rawTx, cfg}, &res)
if err != nil {
return SimulateTransactionResponse{}, err
}
if res.Error != nil {
return SimulateTransactionResponse{}, errors.New(res.Error.Message)
}
return res.Result.Value, nil
}