forked from gagliardetto/solana-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getBlock.go
163 lines (138 loc) · 4.85 KB
/
getBlock.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
// Copyright 2021 github.com/gagliardetto
//
// 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"
"fmt"
"github.com/rizkypsr/solana-go"
)
type TransactionDetailsType string
const (
TransactionDetailsFull TransactionDetailsType = "full"
TransactionDetailsSignatures TransactionDetailsType = "signatures"
TransactionDetailsNone TransactionDetailsType = "none"
)
type GetBlockOpts struct {
// Encoding for each returned Transaction, either "json", "jsonParsed", "base58" (slow), "base64".
// If parameter not provided, the default encoding is "json".
// - "jsonParsed" encoding attempts to use program-specific instruction parsers to return
// more human-readable and explicit data in the transaction.message.instructions list.
// - If "jsonParsed" is requested but a parser cannot be found, the instruction falls back
// to regular JSON encoding (accounts, data, and programIdIndex fields).
//
// This parameter is optional.
Encoding solana.EncodingType
// Level of transaction detail to return.
// If parameter not provided, the default detail level is "full".
//
// This parameter is optional.
TransactionDetails TransactionDetailsType
// Whether to populate the rewards array.
// If parameter not provided, the default includes rewards.
//
// This parameter is optional.
Rewards *bool
// "processed" is not supported.
// If parameter not provided, the default is "finalized".
//
// This parameter is optional.
Commitment CommitmentType
// Max transaction version to return in responses.
// If the requested block contains a transaction with a higher version, an error will be returned.
MaxSupportedTransactionVersion *uint64
}
// GetBlock returns identity and transaction information about a confirmed block in the ledger.
func (cl *Client) GetBlock(
ctx context.Context,
slot uint64,
) (out *GetBlockResult, err error) {
return cl.GetBlockWithOpts(
ctx,
slot,
nil,
)
}
// GetBlock returns identity and transaction information about a confirmed block in the ledger.
//
// NEW: This method is only available in solana-core v1.7 or newer.
// Please use `getConfirmedBlock` for solana-core v1.6
func (cl *Client) GetBlockWithOpts(
ctx context.Context,
slot uint64,
opts *GetBlockOpts,
) (out *GetBlockResult, err error) {
obj := M{
"encoding": solana.EncodingBase64,
}
if opts != nil {
if opts.TransactionDetails != "" {
obj["transactionDetails"] = opts.TransactionDetails
}
if opts.Rewards != nil {
obj["rewards"] = opts.Rewards
}
if opts.Commitment != "" {
obj["commitment"] = opts.Commitment
}
if opts.Encoding != "" {
if !solana.IsAnyOfEncodingType(
opts.Encoding,
// Valid encodings:
// solana.EncodingJSON, // TODO
// solana.EncodingJSONParsed, // TODO
solana.EncodingBase58,
solana.EncodingBase64,
solana.EncodingBase64Zstd,
) {
return nil, fmt.Errorf("provided encoding is not supported: %s", opts.Encoding)
}
obj["encoding"] = opts.Encoding
}
if opts.MaxSupportedTransactionVersion != nil {
obj["maxSupportedTransactionVersion"] = *opts.MaxSupportedTransactionVersion
}
}
params := []interface{}{slot, obj}
err = cl.rpcClient.CallForInto(ctx, &out, "getBlock", params)
if err != nil {
return nil, err
}
if out == nil {
// Block is not confirmed.
return nil, ErrNotConfirmed
}
return
}
type GetBlockResult struct {
// The blockhash of this block.
Blockhash solana.Hash `json:"blockhash"`
// The blockhash of this block's parent;
// if the parent block is not available due to ledger cleanup,
// this field will return "11111111111111111111111111111111".
PreviousBlockhash solana.Hash `json:"previousBlockhash"`
// The slot index of this block's parent.
ParentSlot uint64 `json:"parentSlot"`
// Present if "full" transaction details are requested.
Transactions []TransactionWithMeta `json:"transactions"`
// Present if "signatures" are requested for transaction details;
// an array of signatures, corresponding to the transaction order in the block.
Signatures []solana.Signature `json:"signatures"`
// Present if rewards are requested.
Rewards []BlockReward `json:"rewards"`
// Estimated production time, as Unix timestamp (seconds since the Unix epoch).
// Nil if not available.
BlockTime *solana.UnixTimeSeconds `json:"blockTime"`
// The number of blocks beneath this block.
BlockHeight *uint64 `json:"blockHeight"`
}