forked from gagliardetto/solana-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
553 lines (438 loc) · 16.8 KB
/
types.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// 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 (
"encoding/base64"
stdjson "encoding/json"
"fmt"
"math/big"
bin "github.com/gagliardetto/binary"
"github.com/gagliardetto/solana-go"
)
type Context struct {
Slot uint64 `json:"slot"`
}
type RPCContext struct {
Context Context `json:"context,omitempty"`
}
type GetBalanceResult struct {
RPCContext
Value uint64 `json:"value"`
}
type GetRecentBlockhashResult struct {
RPCContext
Value *BlockhashResult `json:"value"`
}
type BlockhashResult struct {
Blockhash solana.Hash `json:"blockhash"`
FeeCalculator FeeCalculator `json:"feeCalculator"`
}
type FeeCalculator struct {
LamportsPerSignature uint64 `json:"lamportsPerSignature"`
}
type GetConfirmedBlockResult struct {
Blockhash solana.Hash `json:"blockhash"`
// could be zeroes if ledger was clean-up and this is unavailable
PreviousBlockhash solana.Hash `json:"previousBlockhash"`
ParentSlot uint64 `json:"parentSlot"`
Transactions []TransactionWithMeta `json:"transactions"`
Signatures []solana.Signature `json:"signatures"`
Rewards []BlockReward `json:"rewards"`
BlockTime *solana.UnixTimeSeconds `json:"blockTime,omitempty"`
}
type BlockReward struct {
// The public key of the account that received the reward.
Pubkey solana.PublicKey `json:"pubkey"`
// Number of reward lamports credited or debited by the account, as a i64.
Lamports int64 `json:"lamports"`
// Account balance in lamports after the reward was applied.
PostBalance uint64 `json:"postBalance"`
// Type of reward: "Fee", "Rent", "Voting", "Staking".
RewardType RewardType `json:"rewardType"`
// Vote account commission when the reward was credited,
// only present for voting and staking rewards.
Commission *uint8 `json:"commission,omitempty"`
}
type RewardType string
const (
RewardTypeFee RewardType = "Fee"
RewardTypeRent RewardType = "Rent"
RewardTypeVoting RewardType = "Voting"
RewardTypeStaking RewardType = "Staking"
)
type TransactionWithMeta struct {
// The slot this transaction was processed in.
Slot uint64 `json:"slot"`
// Estimated production time, as Unix timestamp (seconds since the Unix epoch)
// of when the transaction was processed.
// Nil if not available.
BlockTime *solana.UnixTimeSeconds `json:"blockTime" bin:"optional"`
Transaction *DataBytesOrJSON `json:"transaction"`
// Transaction status metadata object
Meta *TransactionMeta `json:"meta,omitempty"`
Version TransactionVersion `json:"version"`
}
func (dt TransactionWithMeta) GetParsedTransaction() (*solana.Transaction, error) {
if dt.Transaction == nil {
return nil, fmt.Errorf("transaction is nil")
}
if dt.Transaction.rawDataEncoding != solana.EncodingJSONParsed {
return nil, fmt.Errorf("data is not in JSONParsed encoding")
}
var parsedTransaction solana.Transaction
if err := json.Unmarshal(dt.Transaction.asJSON, &parsedTransaction); err != nil {
return nil, err
}
return &parsedTransaction, nil
}
func (twm TransactionWithMeta) MustGetTransaction() *solana.Transaction {
tx, err := twm.GetTransaction()
if err != nil {
panic(err)
}
return tx
}
func (twm TransactionWithMeta) GetTransaction() (*solana.Transaction, error) {
tx := new(solana.Transaction)
err := tx.UnmarshalWithDecoder(bin.NewBinDecoder(twm.Transaction.GetBinary()))
if err != nil {
return nil, err
}
return tx, nil
}
type TransactionParsed struct {
Meta *TransactionMeta `json:"meta,omitempty"`
Transaction *solana.Transaction `json:"transaction"`
}
type TokenBalance struct {
// Index of the account in which the token balance is provided for.
AccountIndex uint16 `json:"accountIndex"`
// Pubkey of token balance's owner.
Owner *solana.PublicKey `json:"owner,omitempty"`
// Pubkey of the token's mint.
Mint solana.PublicKey `json:"mint"`
UiTokenAmount *UiTokenAmount `json:"uiTokenAmount"`
}
type UiTokenAmount struct {
// Raw amount of tokens as a string, ignoring decimals.
Amount string `json:"amount"`
// TODO: <number> == int64 ???
// Number of decimals configured for token's mint.
Decimals uint8 `json:"decimals"`
// DEPRECATED: Token amount as a float, accounting for decimals.
UiAmount *float64 `json:"uiAmount"`
// Token amount as a string, accounting for decimals.
UiAmountString string `json:"uiAmountString"`
}
type LoadedAddresses struct {
ReadOnly solana.PublicKeySlice `json:"readonly"`
Writable solana.PublicKeySlice `json:"writable"`
}
type TransactionMeta struct {
// Error if transaction failed, null if transaction succeeded.
// https://github.com/solana-labs/solana/blob/master/sdk/src/transaction.rs#L24
Err interface{} `json:"err"`
// Fee this transaction was charged
Fee uint64 `json:"fee"`
// Array of u64 account balances from before the transaction was processed
PreBalances []uint64 `json:"preBalances"`
// Array of u64 account balances after the transaction was processed
PostBalances []uint64 `json:"postBalances"`
// List of inner instructions or omitted if inner instruction recording
// was not yet enabled during this transaction
InnerInstructions []InnerInstruction `json:"innerInstructions"`
// List of token balances from before the transaction was processed
// or omitted if token balance recording was not yet enabled during this transaction
PreTokenBalances []TokenBalance `json:"preTokenBalances"`
// List of token balances from after the transaction was processed
// or omitted if token balance recording was not yet enabled during this transaction
PostTokenBalances []TokenBalance `json:"postTokenBalances"`
// Array of string log messages or omitted if log message
// recording was not yet enabled during this transaction
LogMessages []string `json:"logMessages"`
// DEPRECATED: Transaction status.
Status DeprecatedTransactionMetaStatus `json:"status"`
Rewards []BlockReward `json:"rewards"`
LoadedAddresses LoadedAddresses `json:"loadedAddresses"`
ComputeUnitsConsumed *uint64 `json:"computeUnitsConsumed"`
}
type InnerInstruction struct {
// TODO: <number> == int64 ???
// Index of the transaction instruction from which the inner instruction(s) originated
Index uint16 `json:"index"`
// Ordered list of inner program instructions that were invoked during a single transaction instruction.
Instructions []solana.CompiledInstruction `json:"instructions"`
}
// Ok interface{} `json:"Ok"` // <null> Transaction was successful
// Err interface{} `json:"Err"` // Transaction failed with TransactionError
type DeprecatedTransactionMetaStatus M
type TransactionSignature struct {
// Error if transaction failed, nil if transaction succeeded.
Err interface{} `json:"err"`
// Memo associated with the transaction, nil if no memo is present.
Memo *string `json:"memo"`
// Transaction signature.
Signature solana.Signature `json:"signature"`
// The slot that contains the block with the transaction.
Slot uint64 `json:"slot,omitempty"`
// Estimated production time, as Unix timestamp (seconds since the Unix epoch)
// of when transaction was processed. Nil if not available.
BlockTime *solana.UnixTimeSeconds `json:"blockTime,omitempty"`
ConfirmationStatus ConfirmationStatusType `json:"confirmationStatus,omitempty"`
}
type GetAccountInfoResult struct {
RPCContext
Value *Account `json:"value"`
}
// GetBinary returns the binary representation of the account data.
func (a *GetAccountInfoResult) GetBinary() []byte {
if a == nil {
return nil
}
if a.Value == nil {
return nil
}
if a.Value.Data == nil {
return nil
}
return a.Value.Data.GetBinary()
}
// Bytes returns the binary representation of the account data.
func (a *GetAccountInfoResult) Bytes() []byte {
return a.GetBinary()
}
type IsValidBlockhashResult struct {
RPCContext
Value bool `json:"value"` // True if the blockhash is still valid.
}
type Account struct {
// Number of lamports assigned to this account
Lamports uint64 `json:"lamports"`
// Pubkey of the program this account has been assigned to
Owner solana.PublicKey `json:"owner"`
// Data associated with the account, either as encoded binary data or JSON format {<program>: <state>}, depending on encoding parameter
Data *DataBytesOrJSON `json:"data"`
// Boolean indicating if the account contains a program (and is strictly read-only)
Executable bool `json:"executable"`
// The epoch at which this account will next owe rent
RentEpoch *big.Int `json:"rentEpoch"`
}
type DataBytesOrJSON struct {
rawDataEncoding solana.EncodingType
asDecodedBinary solana.Data
asJSON stdjson.RawMessage
}
func DataBytesOrJSONFromBase64(stringBase64 string) (*DataBytesOrJSON, error) {
decodedData, err := base64.StdEncoding.DecodeString(stringBase64)
if err != nil {
return nil, err
}
return DataBytesOrJSONFromBytes(decodedData), nil
}
// DataBytesOrJSONFromBytes creates a new `DataBytesOrJSON` from the provided bytes.
func DataBytesOrJSONFromBytes(data []byte) *DataBytesOrJSON {
return &DataBytesOrJSON{
rawDataEncoding: solana.EncodingBase64,
asDecodedBinary: solana.Data{
Encoding: solana.EncodingBase64,
Content: data,
},
}
}
func (dt DataBytesOrJSON) MarshalJSON() ([]byte, error) {
if dt.rawDataEncoding == solana.EncodingJSONParsed || dt.rawDataEncoding == solana.EncodingJSON {
return json.Marshal(dt.asJSON)
}
return json.Marshal(dt.asDecodedBinary)
}
func (wrap *DataBytesOrJSON) UnmarshalJSON(data []byte) error {
if len(data) == 0 || (len(data) == 4 && string(data) == "null") {
// TODO: is this an error?
return nil
}
firstChar := data[0]
switch firstChar {
// Check if first character is `[`, standing for a JSON array.
case '[':
// It's base64 (or similar)
{
err := wrap.asDecodedBinary.UnmarshalJSON(data)
if err != nil {
return err
}
wrap.rawDataEncoding = wrap.asDecodedBinary.Encoding
}
case '{':
// It's JSON, most likely.
// TODO: is it always JSON???
{
// Store raw bytes, and unmarshal on request.
wrap.asJSON = data
wrap.rawDataEncoding = solana.EncodingJSONParsed
}
default:
return fmt.Errorf("unknown kind: %v", data)
}
return nil
}
// GetBinary returns the decoded bytes if the encoding is
// "base58", "base64", or "base64+zstd".
func (dt *DataBytesOrJSON) GetBinary() []byte {
return dt.asDecodedBinary.Content
}
// GetRawJSON returns a stdjson.RawMessage when the data
// encoding is "jsonParsed".
func (dt *DataBytesOrJSON) GetRawJSON() stdjson.RawMessage {
return dt.asJSON
}
type DataSlice struct {
Offset *uint64 `json:"offset,omitempty"`
Length *uint64 `json:"length,omitempty"`
}
type GetProgramAccountsOpts struct {
Commitment CommitmentType `json:"commitment,omitempty"`
Encoding solana.EncodingType `json:"encoding,omitempty"`
// Limit the returned account data
DataSlice *DataSlice `json:"dataSlice,omitempty"`
// Filter on accounts, implicit AND between filters.
// Filter results using various filter objects;
// account must meet all filter criteria to be included in results.
Filters []RPCFilter `json:"filters,omitempty"`
}
type GetProgramAccountsResult []*KeyedAccount
type KeyedAccount struct {
Pubkey solana.PublicKey `json:"pubkey"`
Account *Account `json:"account"`
}
type GetConfirmedSignaturesForAddress2Opts struct {
Limit *uint64 `json:"limit,omitempty"`
Before solana.Signature `json:"before,omitempty"`
Until solana.Signature `json:"until,omitempty"`
Commitment CommitmentType `json:"commitment,omitempty"`
}
type GetConfirmedSignaturesForAddress2Result []*TransactionSignature
type RPCFilter struct {
Memcmp *RPCFilterMemcmp `json:"memcmp,omitempty"`
DataSize uint64 `json:"dataSize,omitempty"`
}
type RPCFilterMemcmp struct {
Offset uint64 `json:"offset"`
Bytes solana.Base58 `json:"bytes"`
}
type CommitmentType string
const (
CommitmentMax CommitmentType = "max" // Deprecated as of v1.5.5
CommitmentRecent CommitmentType = "recent" // Deprecated as of v1.5.5
CommitmentRoot CommitmentType = "root" // Deprecated as of v1.5.5
CommitmentSingle CommitmentType = "single" // Deprecated as of v1.5.5
CommitmentSingleGossip CommitmentType = "singleGossip" // Deprecated as of v1.5.5
// The node will query the most recent block confirmed by supermajority
// of the cluster as having reached maximum lockout,
// meaning the cluster has recognized this block as finalized.
CommitmentFinalized CommitmentType = "finalized"
// The node will query the most recent block that has been voted on by supermajority of the cluster.
// - It incorporates votes from gossip and replay.
// - It does not count votes on descendants of a block, only direct votes on that block.
// - This confirmation level also upholds "optimistic confirmation" guarantees in release 1.3 and onwards.
CommitmentConfirmed CommitmentType = "confirmed"
// The node will query its most recent block. Note that the block may still be skipped by the cluster.
CommitmentProcessed CommitmentType = "processed"
)
type ParsedTransaction struct {
Signatures []solana.Signature `json:"signatures"`
Message ParsedMessage `json:"message"`
}
type ParsedTransactionMeta struct {
// Error if transaction failed, null if transaction succeeded.
// https://github.com/solana-labs/solana/blob/master/sdk/src/transaction.rs#L24
Err interface{} `json:"err"`
// Fee this transaction was charged
Fee uint64 `json:"fee"`
// Array of u64 account balances from before the transaction was processed
PreBalances []uint64 `json:"preBalances"`
// Array of u64 account balances after the transaction was processed
PostBalances []uint64 `json:"postBalances"`
// List of inner instructions or omitted if inner instruction recording
// was not yet enabled during this transaction
InnerInstructions []ParsedInnerInstruction `json:"innerInstructions"`
// List of token balances from before the transaction was processed
// or omitted if token balance recording was not yet enabled during this transaction
PreTokenBalances []TokenBalance `json:"preTokenBalances"`
// List of token balances from after the transaction was processed
// or omitted if token balance recording was not yet enabled during this transaction
PostTokenBalances []TokenBalance `json:"postTokenBalances"`
// Array of string log messages or omitted if log message
// recording was not yet enabled during this transaction
LogMessages []string `json:"logMessages"`
}
type ParsedInnerInstruction struct {
Index uint64 `json:"index"`
Instructions []*ParsedInstruction `json:"instructions"`
}
type ParsedMessageAccount struct {
PublicKey solana.PublicKey `json:"pubkey"`
Signer bool `json:"signer"`
Writable bool `json:"writable"`
}
type ParsedMessage struct {
AccountKeys []ParsedMessageAccount `json:"accountKeys"`
Instructions []*ParsedInstruction `json:"instructions"`
RecentBlockHash string `json:"recentBlockhash"`
}
type ParsedInstruction struct {
Program string `json:"program,omitempty"`
ProgramId solana.PublicKey `json:"programId,omitempty"`
Parsed *InstructionInfoEnvelope `json:"parsed,omitempty"`
Data solana.Base58 `json:"data,omitempty"`
Accounts []solana.PublicKey `json:"accounts,omitempty"`
}
type InstructionInfoEnvelope struct {
asString string
asInstructionInfo *InstructionInfo
}
type InstructionInfo struct {
Info map[string]interface{} `json:"info"`
InstructionType string `json:"type"`
}
type TransactionOpts struct {
Encoding solana.EncodingType `json:"encoding,omitempty"`
SkipPreflight bool `json:"skipPreflight,omitempty"`
PreflightCommitment CommitmentType `json:"preflightCommitment,omitempty"`
MaxRetries *uint `json:"maxRetries"`
MinContextSlot *uint64 `json:"minContextSlot"`
}
func (opts *TransactionOpts) ToMap() M {
obj := M{}
if opts.Encoding == "" {
// default to base64 encoding
obj["encoding"] = "base64"
} else {
obj["encoding"] = opts.Encoding
}
obj["skipPreflight"] = opts.SkipPreflight
if opts.PreflightCommitment != "" {
obj["preflightCommitment"] = opts.PreflightCommitment
}
if opts.MaxRetries != nil {
obj["maxRetries"] = *opts.MaxRetries
}
if opts.MinContextSlot != nil {
obj["minContextSlot"] = *opts.MinContextSlot
}
return obj
}
type M map[string]interface{}