github.com/gagliardetto/solana-go@v1.11.0/README.md (about) 1 # Solana SDK library for Go 2 3 [](https://pkg.go.dev/github.com/gagliardetto/solana-go@v1.11.0?tab=doc) 4 [](https://github.com/gagliardetto/solana-go/releases) 5 [](https://github.com/gagliardetto/solana-go/actions?query=branch%3Amain) 6 [](https://www.tickgit.com/browse?repo=github.com/gagliardetto/solana-go&branch=main) 7 [](https://goreportcard.com/report/github.com/gagliardetto/solana-go) 8 9 Go library to interface with Solana JSON RPC and WebSocket interfaces. 10 11 More contracts to come. 12 13 **If you're using/developing Solana programs written in [Anchor Framework](https://github.com/project-serum/anchor), you can use [anchor-go](https://github.com/gagliardetto/anchor-go) to generate Golang clients** 14 15 **If you're looking for a SERUM library, you can check out [gagliardetto/serum-go](https://github.com/gagliardetto/serum-go) ([./programs/serum](https://github.com/gagliardetto/solana-go/tree/main/programs/serum) is deprecated.**) 16 17 <div align="center"> 18 <img src="https://user-images.githubusercontent.com/15271561/128235229-1d2d9116-23bb-464e-b2cc-8fb6355e3b55.png" margin="auto" height="175"/> 19 </div> 20 21 ## Future Development 22 23 `solana-go` is exclusively supported by my own time (which is money). 24 25 If my work has been useful in building your for-profit services/infra/bots/etc., consider donating at 8tTwBazKr2ST1b2kNrM7JMXwixRTvZicn7eRBihThymm (solana) to support future development. 26 27 Thanks! 28 29 ## Contents 30 31 - [Solana SDK library for Go](#solana-sdk-library-for-go) 32 - [Contents' Index](#contents) 33 - [Features](#features) 34 - [Current development status](#current-development-status) 35 - [Requirements](#requirements) 36 - [Installation](#installation) 37 - [Pretty-Print transactions/instructions](#pretty-print-transactionsinstructions) 38 - [SendAndConfirmTransaction](#sendandconfirmtransaction) 39 - [Address Lookup Tables](#address-lookup-tables) 40 - [Decode an instruction data](#parsedecode-an-instruction-from-a-transaction) 41 - [Borsh encoding/decoding](#borsh-encodingdecoding) 42 - [ZSTD account data encoding](#zstd-account-data-encoding) 43 - [Custom Headers for authenticating with RPC providers](#custom-headers-for-authenticating-with-rpc-providers) 44 - [Working with rate-limited RPC providers](#working-with-rate-limited-rpc-providers) 45 - [Timeouts and Custom HTTP Clients](#timeouts-and-custom-http-clients) 46 - [Examples](#examples) 47 - [Create Account/Wallet](#create-account-wallet) 48 - [Load/parse keys](#loadparse-private-and-public-keys) 49 - [Transfer SOL from a wallet to another](#transfer-sol-from-one-wallet-to-another-wallet) 50 - [RPC (index)](#rpc-usage-examples) 51 - [RPC examples](#rpc-methods) 52 - [Websocket Subscription examples](#websocket-subscriptions) 53 - [Contributing](#contributing) 54 - [License](#license) 55 - [Credits](#credits) 56 57 ## Features 58 59 - [x] [Full JSON RPC API](#rpc-usage-examples) 60 - [x] Full WebSocket JSON streaming API 61 - [ ] Wallet, account, and keys management 62 - [ ] Clients for native programs 63 - [x] [system](/programs/system) 64 - [ ] config 65 - [ ] stake 66 - [ ] vote 67 - [x] BPF Loader 68 - [ ] Secp256k1 69 - [ ] Clients for Solana Program Library (SPL) 70 - [x] [SPL token](/programs/token) 71 - [x] [associated-token-account](/programs/associated-token-account) 72 - [ ] memo 73 - [ ] name-service 74 - [ ] ... 75 - [ ] Client for Serum 76 - [x] [Metaplex](https://github.com/gagliardetto/metaplex-go): 77 - [x] auction 78 - [x] metaplex 79 - [x] token-metadata 80 - [x] token-vault 81 - [x] nft-candy-machine 82 - [ ] More programs 83 84 ## Current development status 85 86 There is currently **no stable release**. The SDK is actively developed and latest is `v1.11.0` which is an `alpha` release. 87 88 The RPC and WS client implementation is based on [this RPC spec](https://github.com/solana-labs/solana/blob/c2435363f39723cef59b91322f3b6a815008af29/docs/src/developing/clients/jsonrpc-api.md). 89 90 ## Note 91 92 - solana-go is in active development, so all APIs are subject to change. 93 - This code is unaudited. Use at your own risk. 94 95 ## Requirements 96 97 - Go 1.18 or later 98 99 ## Installation 100 101 ```bash 102 $ cd my-project 103 $ go get github.com/gagliardetto/solana-go@v1.11.0 104 ``` 105 106 ## Pretty-Print transactions/instructions 107 108  109 110 Instructions can be pretty-printed with the `String()` method on a `Transaction`: 111 112 ```go 113 tx, err := solana.NewTransaction( 114 []solana.Instruction{ 115 system.NewTransferInstruction( 116 amount, 117 accountFrom.PublicKey(), 118 accountTo, 119 ).Build(), 120 }, 121 recent.Value.Blockhash, 122 solana.TransactionPayer(accountFrom.PublicKey()), 123 ) 124 125 ... 126 127 // Pretty print the transaction: 128 fmt.Println(tx.String()) 129 // OR you can choose a destination and a title: 130 // tx.EncodeTree(text.NewTreeEncoder(os.Stdout, "Transfer SOL")) 131 ``` 132 133 ## SendAndConfirmTransaction 134 135 You can wait for a transaction confirmation using the `github.com/gagliardetto/solana-go/rpc/sendAndConfirmTransaction` package tools (for a complete example: [see here](#transfer-sol-from-one-wallet-to-another-wallet)) 136 137 ```go 138 // Send transaction, and wait for confirmation: 139 sig, err := confirm.SendAndConfirmTransaction( 140 context.TODO(), 141 rpcClient, 142 wsClient, 143 tx, 144 ) 145 if err != nil { 146 panic(err) 147 } 148 spew.Dump(sig) 149 ``` 150 151 The above command will send the transaction, and wait for its confirmation. 152 153 ## Address Lookup Tables 154 155 Resolve lookups for a transaction: 156 157 ```go 158 package main 159 160 import ( 161 "context" 162 "fmt" 163 "time" 164 165 "github.com/davecgh/go-spew/spew" 166 "github.com/gagliardetto/solana-go" 167 lookup "github.com/gagliardetto/solana-go/programs/address-lookup-table" 168 "github.com/gagliardetto/solana-go/rpc" 169 "golang.org/x/time/rate" 170 ) 171 172 func main() { 173 cluster := rpc.MainNetBeta 174 175 rpcClient := rpc.NewWithCustomRPCClient(rpc.NewWithLimiter( 176 cluster.RPC, 177 rate.Every(time.Second), // time frame 178 5, // limit of requests per time frame 179 )) 180 181 version := uint64(0) 182 tx, err := rpcClient.GetTransaction( 183 context.Background(), 184 solana.MustSignatureFromBase58("24jRjMP3medE9iMqVSPRbkwfe9GdPmLfeftKPuwRHZdYTZJ6UyzNMGGKo4BHrTu2zVj4CgFF3CEuzS79QXUo2CMC"), 185 &rpc.GetTransactionOpts{ 186 MaxSupportedTransactionVersion: &version, 187 Encoding: solana.EncodingBase64, 188 }, 189 ) 190 if err != nil { 191 panic(err) 192 } 193 parsed, err := tx.Transaction.GetTransaction() 194 if err != nil { 195 panic(err) 196 } 197 processTransactionWithAddressLookups(*parsed, rpcClient) 198 } 199 200 func processTransactionWithAddressLookups(txx solana.Transaction, rpcClient *rpc.Client) { 201 if !txx.Message.IsVersioned() { 202 fmt.Println("tx is not versioned; only versioned transactions can contain lookups") 203 return 204 } 205 tblKeys := txx.Message.GetAddressTableLookups().GetTableIDs() 206 if len(tblKeys) == 0 { 207 fmt.Println("no lookup tables in versioned transaction") 208 return 209 } 210 numLookups := txx.Message.GetAddressTableLookups().NumLookups() 211 if numLookups == 0 { 212 fmt.Println("no lookups in versioned transaction") 213 return 214 } 215 fmt.Println("num lookups:", numLookups) 216 fmt.Println("num tbl keys:", len(tblKeys)) 217 resolutions := make(map[solana.PublicKey]solana.PublicKeySlice) 218 for _, key := range tblKeys { 219 fmt.Println("Getting table", key) 220 221 info, err := rpcClient.GetAccountInfo( 222 context.Background(), 223 key, 224 ) 225 if err != nil { 226 panic(err) 227 } 228 fmt.Println("got table "+key.String()) 229 230 tableContent, err := lookup.DecodeAddressLookupTableState(info.GetBinary()) 231 if err != nil { 232 panic(err) 233 } 234 235 fmt.Println("table content:", spew.Sdump(tableContent)) 236 fmt.Println("isActive", tableContent.IsActive()) 237 238 resolutions[key] = tableContent.Addresses 239 } 240 241 err := txx.Message.SetAddressTables(resolutions) 242 if err != nil { 243 panic(err) 244 } 245 246 err = txx.Message.ResolveLookups() 247 if err != nil { 248 panic(err) 249 } 250 fmt.Println(txx.String()) 251 } 252 ``` 253 254 255 ## Parse/decode an instruction from a transaction 256 257 ```go 258 package main 259 260 import ( 261 "context" 262 "encoding/base64" 263 "os" 264 "reflect" 265 266 "github.com/davecgh/go-spew/spew" 267 bin "github.com/gagliardetto/binary" 268 "github.com/gagliardetto/solana-go" 269 "github.com/gagliardetto/solana-go/programs/system" 270 "github.com/gagliardetto/solana-go/rpc" 271 "github.com/gagliardetto/solana-go/text" 272 ) 273 274 func main() { 275 exampleFromGetTransaction() 276 } 277 278 func exampleFromBase64() { 279 encoded := "AfjEs3XhTc3hrxEvlnMPkm/cocvAUbFNbCl00qKnrFue6J53AhEqIFmcJJlJW3EDP5RmcMz+cNTTcZHW/WJYwAcBAAEDO8hh4VddzfcO5jbCt95jryl6y8ff65UcgukHNLWH+UQGgxCGGpgyfQVQV02EQYqm4QwzUt2qf9f1gVLM7rI4hwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA6ANIF55zOZWROWRkeh+lExxZBnKFqbvIxZDLE7EijjoBAgIAAQwCAAAAOTAAAAAAAAA=" 280 281 data, err := base64.StdEncoding.DecodeString(encoded) 282 if err != nil { 283 panic(err) 284 } 285 286 // parse transaction: 287 tx, err := solana.TransactionFromDecoder(bin.NewBinDecoder(data)) 288 if err != nil { 289 panic(err) 290 } 291 292 decodeSystemTransfer(tx) 293 } 294 295 func exampleFromGetTransaction() { 296 endpoint := rpc.TestNet_RPC 297 client := rpc.New(endpoint) 298 299 txSig := solana.MustSignatureFromBase58("3hZorctJtD3QLCRV3zF6JM6FDbFR5kAvsuKEG1RH9rWdz8YgnDzAvMWZFjdJgoL8KSNzZnx7aiExm1JEMC8KHfyy") 300 { 301 out, err := client.GetTransaction( 302 context.TODO(), 303 txSig, 304 &rpc.GetTransactionOpts{ 305 Encoding: solana.EncodingBase64, 306 }, 307 ) 308 if err != nil { 309 panic(err) 310 } 311 312 tx, err := solana.TransactionFromDecoder(bin.NewBinDecoder(out.Transaction.GetBinary())) 313 if err != nil { 314 panic(err) 315 } 316 317 decodeSystemTransfer(tx) 318 } 319 } 320 321 func decodeSystemTransfer(tx *solana.Transaction) { 322 spew.Dump(tx) 323 324 // Get (for example) the first instruction of this transaction 325 // which we know is a `system` program instruction: 326 i0 := tx.Message.Instructions[0] 327 328 // Find the program address of this instruction: 329 progKey, err := tx.ResolveProgramIDIndex(i0.ProgramIDIndex) 330 if err != nil { 331 panic(err) 332 } 333 334 // Find the accounts of this instruction: 335 accounts, err := i0.ResolveInstructionAccounts(&tx.Message) 336 if err != nil { 337 panic(err) 338 } 339 340 // Feed the accounts and data to the system program parser 341 // OR see below for alternative parsing when you DON'T know 342 // what program the instruction is for / you don't have a parser. 343 inst, err := system.DecodeInstruction(accounts, i0.Data) 344 if err != nil { 345 panic(err) 346 } 347 348 // inst.Impl contains the specific instruction type (in this case, `inst.Impl` is a `*system.Transfer`) 349 spew.Dump(inst) 350 if _, ok := inst.Impl.(*system.Transfer); !ok { 351 panic("the instruction is not a *system.Transfer") 352 } 353 354 // OR 355 { 356 // There is a more general instruction decoder: `solana.DecodeInstruction`. 357 // But before you can use `solana.DecodeInstruction`, 358 // you must register a decoder for each program ID beforehand 359 // by using `solana.RegisterInstructionDecoder` (all solana-go program clients do it automatically with the default program IDs). 360 decodedInstruction, err := solana.DecodeInstruction( 361 progKey, 362 accounts, 363 i0.Data, 364 ) 365 if err != nil { 366 panic(err) 367 } 368 // The returned `decodedInstruction` is the decoded instruction. 369 spew.Dump(decodedInstruction) 370 371 // decodedInstruction == inst 372 if !reflect.DeepEqual(inst, decodedInstruction) { 373 panic("they are NOT equal (this would never happen)") 374 } 375 376 // To register other (not yet registered decoders), you can add them with 377 // `solana.RegisterInstructionDecoder` function. 378 } 379 380 { 381 // pretty-print whole transaction: 382 _, err := tx.EncodeTree(text.NewTreeEncoder(os.Stdout, text.Bold("TEST TRANSACTION"))) 383 if err != nil { 384 panic(err) 385 } 386 } 387 } 388 389 ``` 390 391 ## Borsh encoding/decoding 392 393 You can use the `github.com/gagliardetto/binary` package for encoding/decoding borsh-encoded data: 394 395 Decoder: 396 397 ```go 398 resp, err := client.GetAccountInfo( 399 context.TODO(), 400 pubKey, 401 ) 402 if err != nil { 403 panic(err) 404 } 405 406 borshDec := bin.NewBorshDecoder(resp.GetBinary()) 407 var meta token_metadata.Metadata 408 err = borshDec.Decode(&meta) 409 if err != nil { 410 panic(err) 411 } 412 ``` 413 414 Encoder: 415 416 ```go 417 buf := new(bytes.Buffer) 418 borshEncoder := bin.NewBorshEncoder(buf) 419 err := borshEncoder.Encode(meta) 420 if err != nil { 421 panic(err) 422 } 423 // fmt.Print(buf.Bytes()) 424 ``` 425 426 ## ZSTD account data encoding 427 428 You can request account data to be encoded with base64+zstd in the `Encoding` parameter: 429 430 ```go 431 resp, err := client.GetAccountInfoWithOpts( 432 context.TODO(), 433 pubKey, 434 &rpc.GetAccountInfoOpts{ 435 Encoding: solana.EncodingBase64Zstd, 436 Commitment: rpc.CommitmentFinalized, 437 }, 438 ) 439 if err != nil { 440 panic(err) 441 } 442 spew.Dump(resp) 443 444 var mint token.Mint 445 err = bin.NewDecoder(resp.GetBinary()).Decode(&mint) 446 if err != nil { 447 panic(err) 448 } 449 spew.Dump(mint) 450 ``` 451 452 ## Working with rate-limited RPC providers 453 454 ```go 455 package main 456 457 import ( 458 "context" 459 460 "golang.org/x/time/rate" 461 "github.com/davecgh/go-spew/spew" 462 "github.com/gagliardetto/solana-go/rpc" 463 ) 464 465 func main() { 466 cluster := rpc.MainNetBeta 467 client := rpc.NewWithCustomRPCClient(rpc.NewWithLimiter( 468 cluster.RPC, 469 rate.Every(time.Second), // time frame 470 5, // limit of requests per time frame 471 )) 472 473 out, err := client.GetVersion( 474 context.TODO(), 475 ) 476 if err != nil { 477 panic(err) 478 } 479 spew.Dump(out) 480 } 481 ``` 482 483 ## Custom Headers for authenticating with RPC providers 484 485 ```go 486 package main 487 488 import ( 489 "context" 490 491 "golang.org/x/time/rate" 492 "github.com/davecgh/go-spew/spew" 493 "github.com/gagliardetto/solana-go/rpc" 494 ) 495 496 func main() { 497 cluster := rpc.MainNetBeta 498 client := rpc.NewWithHeaders( 499 cluster.RPC, 500 map[string]string{ 501 "x-api-key": "...", 502 }, 503 ) 504 505 out, err := client.GetVersion( 506 context.TODO(), 507 ) 508 if err != nil { 509 panic(err) 510 } 511 spew.Dump(out) 512 } 513 ``` 514 515 The data will **AUTOMATICALLY get decoded** and returned (**the right decoder will be used**) when you call the `resp.GetBinary()` method. 516 517 ## Timeouts and Custom HTTP Clients 518 519 You can use a timeout context: 520 521 ```go 522 ctx, cancel := context.WithTimeout(context.Background(), time.Second*3) 523 defer cancel() 524 acc, err := rpcClient.GetAccountInfoWithOpts( 525 ctx, 526 accountID, 527 &rpc.GetAccountInfoOpts{ 528 Commitment: rpc.CommitmentProcessed, 529 }, 530 ) 531 ``` 532 533 Or you can initialize the RPC client using a custom HTTP client using `rpc.NewWithCustomRPCClient`: 534 535 ```go 536 import ( 537 "net" 538 "net/http" 539 "time" 540 541 "github.com/gagliardetto/solana-go/rpc" 542 "github.com/gagliardetto/solana-go/rpc/jsonrpc" 543 ) 544 545 func NewHTTPTransport( 546 timeout time.Duration, 547 maxIdleConnsPerHost int, 548 keepAlive time.Duration, 549 ) *http.Transport { 550 return &http.Transport{ 551 IdleConnTimeout: timeout, 552 MaxIdleConnsPerHost: maxIdleConnsPerHost, 553 Proxy: http.ProxyFromEnvironment, 554 Dial: (&net.Dialer{ 555 Timeout: timeout, 556 KeepAlive: keepAlive, 557 }).Dial, 558 } 559 } 560 561 // NewHTTP returns a new Client from the provided config. 562 func NewHTTP( 563 timeout time.Duration, 564 maxIdleConnsPerHost int, 565 keepAlive time.Duration, 566 ) *http.Client { 567 tr := NewHTTPTransport( 568 timeout, 569 maxIdleConnsPerHost, 570 keepAlive, 571 ) 572 573 return &http.Client{ 574 Timeout: timeout, 575 Transport: tr, 576 } 577 } 578 579 // NewRPC creates a new Solana JSON RPC client. 580 func NewRPC(rpcEndpoint string) *rpc.Client { 581 var ( 582 defaultMaxIdleConnsPerHost = 10 583 defaultTimeout = 25 * time.Second 584 defaultKeepAlive = 180 * time.Second 585 ) 586 opts := &jsonrpc.RPCClientOpts{ 587 HTTPClient: NewHTTP( 588 defaultTimeout, 589 defaultMaxIdleConnsPerHost, 590 defaultKeepAlive, 591 ), 592 } 593 rpcClient := jsonrpc.NewClientWithOpts(rpcEndpoint, opts) 594 return rpc.NewWithCustomRPCClient(rpcClient) 595 } 596 ``` 597 598 ## Examples 599 600 ### Create account (wallet) 601 602 ```go 603 package main 604 605 import ( 606 "context" 607 "fmt" 608 609 "github.com/gagliardetto/solana-go" 610 "github.com/gagliardetto/solana-go/rpc" 611 ) 612 613 func main() { 614 // Create a new account: 615 account := solana.NewWallet() 616 fmt.Println("account private key:", account.PrivateKey) 617 fmt.Println("account public key:", account.PublicKey()) 618 619 // Create a new RPC client: 620 client := rpc.New(rpc.TestNet_RPC) 621 622 // Airdrop 1 SOL to the new account: 623 out, err := client.RequestAirdrop( 624 context.TODO(), 625 account.PublicKey(), 626 solana.LAMPORTS_PER_SOL*1, 627 rpc.CommitmentFinalized, 628 ) 629 if err != nil { 630 panic(err) 631 } 632 fmt.Println("airdrop transaction signature:", out) 633 } 634 ``` 635 636 ### Load/parse private and public keys 637 638 ```go 639 { 640 // Load private key from a json file generated with 641 // $ solana-keygen new --outfile=standard.solana-keygen.json 642 privateKey, err := solana.PrivateKeyFromSolanaKeygenFile("/path/to/standard.solana-keygen.json") 643 if err != nil { 644 panic(err) 645 } 646 fmt.Println("private key:", privateKey.String()) 647 // To get the public key, you need to call the `PublicKey()` method: 648 publicKey := privateKey.PublicKey() 649 // To get the base58 string of a public key, you can call the `String()` method: 650 fmt.Println("public key:", publicKey.String()) 651 } 652 653 { 654 // Load private key from base58: 655 { 656 privateKey, err := solana.PrivateKeyFromBase58("66cDvko73yAf8LYvFMM3r8vF5vJtkk7JKMgEKwkmBC86oHdq41C7i1a2vS3zE1yCcdLLk6VUatUb32ZzVjSBXtRs") 657 if err != nil { 658 panic(err) 659 } 660 fmt.Println("private key:", privateKey.String()) 661 fmt.Println("public key:", privateKey.PublicKey().String()) 662 } 663 // OR: 664 { 665 privateKey := solana.MustPrivateKeyFromBase58("66cDvko73yAf8LYvFMM3r8vF5vJtkk7JKMgEKwkmBC86oHdq41C7i1a2vS3zE1yCcdLLk6VUatUb32ZzVjSBXtRs") 666 _ = privateKey 667 } 668 } 669 670 { 671 // Generate a new key pair: 672 { 673 privateKey, err := solana.NewRandomPrivateKey() 674 if err != nil { 675 panic(err) 676 } 677 _ = privateKey 678 } 679 { 680 { // Generate a new private key (a Wallet struct is just a wrapper around a private key) 681 account := solana.NewWallet() 682 _ = account 683 } 684 } 685 } 686 687 { 688 // Parse a public key from a base58 string: 689 { 690 publicKey, err := solana.PublicKeyFromBase58("F8UvVsKnzWyp2nF8aDcqvQ2GVcRpqT91WDsAtvBKCMt9") 691 if err != nil { 692 panic(err) 693 } 694 _ = publicKey 695 } 696 // OR: 697 { 698 publicKey := solana.MustPublicKeyFromBase58("F8UvVsKnzWyp2nF8aDcqvQ2GVcRpqT91WDsAtvBKCMt9") 699 _ = publicKey 700 } 701 } 702 ``` 703 704 ### Transfer Sol from one wallet to another wallet 705 706 ```go 707 package main 708 709 import ( 710 "context" 711 "fmt" 712 "os" 713 "time" 714 715 "github.com/davecgh/go-spew/spew" 716 "github.com/gagliardetto/solana-go" 717 "github.com/gagliardetto/solana-go/programs/system" 718 "github.com/gagliardetto/solana-go/rpc" 719 confirm "github.com/gagliardetto/solana-go/rpc/sendAndConfirmTransaction" 720 "github.com/gagliardetto/solana-go/rpc/jsonrpc" 721 "github.com/gagliardetto/solana-go/rpc/ws" 722 "github.com/gagliardetto/solana-go/text" 723 ) 724 725 func main() { 726 // Create a new RPC client: 727 rpcClient := rpc.New(rpc.DevNet_RPC) 728 729 // Create a new WS client (used for confirming transactions) 730 wsClient, err := ws.Connect(context.Background(), rpc.DevNet_WS) 731 if err != nil { 732 panic(err) 733 } 734 735 // Load the account that you will send funds FROM: 736 accountFrom, err := solana.PrivateKeyFromSolanaKeygenFile("/path/to/.config/solana/id.json") 737 if err != nil { 738 panic(err) 739 } 740 fmt.Println("accountFrom private key:", accountFrom) 741 fmt.Println("accountFrom public key:", accountFrom.PublicKey()) 742 743 // The public key of the account that you will send sol TO: 744 accountTo := solana.MustPublicKeyFromBase58("TODO") 745 // The amount to send (in lamports); 746 // 1 sol = 1000000000 lamports 747 amount := uint64(3333) 748 749 if true { 750 // Airdrop 1 sol to the account so it will have something to transfer: 751 out, err := rpcClient.RequestAirdrop( 752 context.TODO(), 753 accountFrom.PublicKey(), 754 solana.LAMPORTS_PER_SOL*1, 755 rpc.CommitmentFinalized, 756 ) 757 if err != nil { 758 panic(err) 759 } 760 fmt.Println("airdrop transaction signature:", out) 761 time.Sleep(time.Second * 5) 762 } 763 //--------------- 764 765 recent, err := rpcClient.GetRecentBlockhash(context.TODO(), rpc.CommitmentFinalized) 766 if err != nil { 767 panic(err) 768 } 769 770 tx, err := solana.NewTransaction( 771 []solana.Instruction{ 772 system.NewTransferInstruction( 773 amount, 774 accountFrom.PublicKey(), 775 accountTo, 776 ).Build(), 777 }, 778 recent.Value.Blockhash, 779 solana.TransactionPayer(accountFrom.PublicKey()), 780 ) 781 if err != nil { 782 panic(err) 783 } 784 785 _, err = tx.Sign( 786 func(key solana.PublicKey) *solana.PrivateKey { 787 if accountFrom.PublicKey().Equals(key) { 788 return &accountFrom 789 } 790 return nil 791 }, 792 ) 793 if err != nil { 794 panic(fmt.Errorf("unable to sign transaction: %w", err)) 795 } 796 spew.Dump(tx) 797 // Pretty print the transaction: 798 tx.EncodeTree(text.NewTreeEncoder(os.Stdout, "Transfer SOL")) 799 800 // Send transaction, and wait for confirmation: 801 sig, err := confirm.SendAndConfirmTransaction( 802 context.TODO(), 803 rpcClient, 804 wsClient, 805 tx, 806 ) 807 if err != nil { 808 panic(err) 809 } 810 spew.Dump(sig) 811 812 // Or just send the transaction WITHOUT waiting for confirmation: 813 // sig, err := rpcClient.SendTransactionWithOpts( 814 // context.TODO(), 815 // tx, 816 // false, 817 // rpc.CommitmentFinalized, 818 // ) 819 // if err != nil { 820 // panic(err) 821 // } 822 // spew.Dump(sig) 823 } 824 825 ``` 826 827 ## RPC usage examples 828 829 - [RPC Methods](#rpc-methods) 830 - [GetAccountInfo](#index--rpc--getaccountinfo) 831 - [GetBalance](#index--rpc--getbalance) 832 - [GetBlock](#index--rpc--getblock) 833 - [GetBlockCommitment](#index--rpc--getblockcommitment) 834 - [GetBlockHeight](#index--rpc--getblockheight) 835 - [GetBlockProduction](#index--rpc--getblockproduction) 836 - [GetBlockTime](#index--rpc--getblocktime) 837 - [GetBlocks](#index--rpc--getblocks) 838 - [GetBlocksWithLimit](#index--rpc--getblockswithlimit) 839 - [GetClusterNodes](#index--rpc--getclusternodes) 840 - [GetConfirmedBlock](#index--rpc--getconfirmedblock) 841 - **DEPRECATED: Please use [GetBlock](#index--rpc--getblock) instead** (This method is expected to be removed in **solana-core v2.0**) 842 - [GetConfirmedBlocks](#index--rpc--getconfirmedblocks) 843 - **DEPRECATED: Please use [GetBlocks](#index--rpc--getblocks) instead** (This method is expected to be removed in **solana-core v2.0**) 844 - [GetConfirmedBlocksWithLimit](#index--rpc--getconfirmedblockswithlimit) 845 - **DEPRECATED: Please use [GetBlocksWithLimit](#index--rpc--getblockswithlimit) instead** (This method is expected to be removed in **solana-core v2.0**) 846 - [GetConfirmedSignaturesForAddress2](#index--rpc--getconfirmedsignaturesforaddress2) 847 - **DEPRECATED: Please use [GetSignaturesForAddress](#index--rpc--getsignaturesforaddress) instead** (This method is expected to be removed in **solana-core v2.0**) 848 - [GetConfirmedTransaction](#index--rpc--getconfirmedtransaction) 849 - **DEPRECATED: Please use [GetTransaction](#index--rpc--gettransaction) instead** (This method is expected to be removed in **solana-core v2.0**) 850 - [GetEpochInfo](#index--rpc--getepochinfo) 851 - [GetEpochSchedule](#index--rpc--getepochschedule) 852 - [GetFeeCalculatorForBlockhash](#index--rpc--getfeecalculatorforblockhash) 853 - **DEPRECATED: Please use [IsBlockhashValid](#index--rpc--isblockhashvalid) or [GetFeeForMessage](#index--rpc--getfeeformessage) instead** (This method is expected to be removed in **solana-core v2.0**) 854 - [GetFeeRateGovernor](#index--rpc--getfeerategovernor) **DEPRECATED** 855 - [GetFees](#index--rpc--getfees) 856 - **DEPRECATED: Please use [GetFeeForMessage](#index--rpc--getfeeformessage) instead** (This method is expected to be removed in **solana-core v2.0**) 857 - [GetFeeForMessage](#index--rpc--getfeeformessage) 858 - [GetFirstAvailableBlock](#index--rpc--getfirstavailableblock) 859 - [GetGenesisHash](#index--rpc--getgenesishash) 860 - [GetHealth](#index--rpc--gethealth) 861 - [GetHighestSnapshotSlot](#index--rpc--gethighestsnapshotslot) 862 - [GetLatestBlockhash](#index--rpc--getlatestblockhash) 863 - [GetIdentity](#index--rpc--getidentity) 864 - [GetInflationGovernor](#index--rpc--getinflationgovernor) 865 - [GetInflationRate](#index--rpc--getinflationrate) 866 - [GetInflationReward](#index--rpc--getinflationreward) 867 - [GetLargestAccounts](#index--rpc--getlargestaccounts) 868 - [GetLeaderSchedule](#index--rpc--getleaderschedule) 869 - [GetMaxRetransmitSlot](#index--rpc--getmaxretransmitslot) 870 - [GetMaxShredInsertSlot](#index--rpc--getmaxshredinsertslot) 871 - [GetMinimumBalanceForRentExemption](#index--rpc--getminimumbalanceforrentexemption) 872 - [GetMultipleAccounts](#index--rpc--getmultipleaccounts) 873 - [GetProgramAccounts](#index--rpc--getprogramaccounts) 874 - [GetRecentBlockhash](#index--rpc--getrecentblockhash) 875 - To be used with **solana v1.8** 876 - For solana v1.9 or newer: **DEPRECATED: Please use [GetLatestBlockhash](#index--rpc--getlatestblockhash) instead** (This method is expected to be removed in **solana-core v2.0**) 877 - [GetRecentPerformanceSamples](#index--rpc--getrecentperformancesamples) 878 - [GetRecentPrioritizationFees](#index--rpc--getrecentprioritizationfees) 879 - [GetSignatureStatuses](#index--rpc--getsignaturestatuses) 880 - [GetSignaturesForAddress](#index--rpc--getsignaturesforaddress) 881 - [GetSlot](#index--rpc--getslot) 882 - [GetSlotLeader](#index--rpc--getslotleader) 883 - [GetSlotLeaders](#index--rpc--getslotleaders) 884 - [GetSnapshotSlot](#index--rpc--getsnapshotslot) 885 - **DEPRECATED: Please use [GetHighestSnapshotSlot](#index--rpc--gethighestsnapshotslot) instead** (This method is expected to be removed in **solana-core v2.0**) 886 - [GetStakeActivation](#index--rpc--getstakeactivation) 887 - [GetSupply](#index--rpc--getsupply) 888 - [GetTokenAccountBalance](#index--rpc--gettokenaccountbalance) 889 - [GetTokenAccountsByDelegate](#index--rpc--gettokenaccountsbydelegate) 890 - [GetTokenAccountsByOwner](#index--rpc--gettokenaccountsbyowner) 891 - [GetTokenLargestAccounts](#index--rpc--gettokenlargestaccounts) 892 - [GetTokenSupply](#index--rpc--gettokensupply) 893 - [GetTransaction](#index--rpc--gettransaction) 894 - [GetTransactionCount](#index--rpc--gettransactioncount) 895 - [GetVersion](#index--rpc--getversion) 896 - [GetVoteAccounts](#index--rpc--getvoteaccounts) 897 - [IsBlockhashValid](#index--rpc--isblockhashvalid) 898 - [MinimumLedgerSlot](#index--rpc--minimumledgerslot) 899 - [RequestAirdrop](#index--rpc--requestairdrop) 900 - [SendTransaction](#index--rpc--sendtransaction) 901 - [SimulateTransaction](#index--rpc--simulatetransaction) 902 - [Websocket Subscriptions](#websocket-subscriptions) 903 - [AccountSubscribe](#index--ws-subscriptions--accountsubscribe) 904 - [LogsSubscribe](#index--ws-subscriptions--logssubscribe) 905 - [ProgramSubscribe](#index--ws-subscriptions--programsubscribe) 906 - [RootSubscribe](#index--ws-subscriptions--rootsubscribe) 907 - [SignatureSubscribe](#index--ws-subscriptions--signaturesubscribe) 908 - [SlotSubscribe](#index--ws-subscriptions--slotsubscribe) 909 - [VoteSubscribe](#index--ws-subscriptions--votesubscribe) 910 911 ### RPC Methods 912 913 #### [index](#contents) > [RPC](#rpc-methods) > GetAccountInfo 914 915 ```go 916 package main 917 918 import ( 919 "context" 920 921 "github.com/davecgh/go-spew/spew" 922 bin "github.com/gagliardetto/binary" 923 solana "github.com/gagliardetto/solana-go" 924 "github.com/gagliardetto/solana-go/programs/token" 925 "github.com/gagliardetto/solana-go/rpc" 926 ) 927 928 func main() { 929 endpoint := rpc.MainNetBeta_RPC 930 client := rpc.New(endpoint) 931 932 { 933 pubKey := solana.MustPublicKeyFromBase58("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt") // serum token 934 // basic usage 935 resp, err := client.GetAccountInfo( 936 context.TODO(), 937 pubKey, 938 ) 939 if err != nil { 940 panic(err) 941 } 942 spew.Dump(resp) 943 944 var mint token.Mint 945 // Account{}.Data.GetBinary() returns the *decoded* binary data 946 // regardless the original encoding (it can handle them all). 947 err = bin.NewDecoder(resp.GetBinary()).Decode(&mint) 948 if err != nil { 949 panic(err) 950 } 951 spew.Dump(mint) 952 // NOTE: The supply is mint.Supply, with the mint.Decimals: 953 // mint.Supply = 9998022451607088 954 // mint.Decimals = 6 955 // ... which means that the supply is 9998022451.607088 956 } 957 { 958 // Or you can use `GetAccountDataInto` which does all of the above in one call: 959 pubKey := solana.MustPublicKeyFromBase58("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt") // serum token 960 var mint token.Mint 961 // Get the account, and decode its data into the provided mint object: 962 err := client.GetAccountDataInto( 963 context.TODO(), 964 pubKey, 965 &mint, 966 ) 967 if err != nil { 968 panic(err) 969 } 970 spew.Dump(mint) 971 } 972 { 973 // // Or you can use `GetAccountDataBorshInto` which does all of the above in one call but for borsh-encoded data: 974 // var metadata token_metadata.Metadata 975 // // Get the account, and decode its data into the provided metadata object: 976 // err := client.GetAccountDataBorshInto( 977 // context.TODO(), 978 // pubKey, 979 // &metadata, 980 // ) 981 // if err != nil { 982 // panic(err) 983 // } 984 // spew.Dump(metadata) 985 } 986 { 987 pubKey := solana.MustPublicKeyFromBase58("4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R") // raydium token 988 // advanced usage 989 resp, err := client.GetAccountInfoWithOpts( 990 context.TODO(), 991 pubKey, 992 // You can specify more options here: 993 &rpc.GetAccountInfoOpts{ 994 Encoding: solana.EncodingBase64Zstd, 995 Commitment: rpc.CommitmentFinalized, 996 // You can get just a part of the account data by specify a DataSlice: 997 // DataSlice: &rpc.DataSlice{ 998 // Offset: pointer.ToUint64(0), 999 // Length: pointer.ToUint64(1024), 1000 // }, 1001 }, 1002 ) 1003 if err != nil { 1004 panic(err) 1005 } 1006 spew.Dump(resp) 1007 1008 var mint token.Mint 1009 err = bin.NewDecoder(resp.GetBinary()).Decode(&mint) 1010 if err != nil { 1011 panic(err) 1012 } 1013 spew.Dump(mint) 1014 } 1015 } 1016 ``` 1017 1018 #### [index](#contents) > [RPC](#rpc-methods) > GetBalance 1019 1020 ```go 1021 package main 1022 1023 import ( 1024 "context" 1025 "fmt" 1026 "math/big" 1027 1028 "github.com/davecgh/go-spew/spew" 1029 "github.com/gagliardetto/solana-go" 1030 "github.com/gagliardetto/solana-go/rpc" 1031 ) 1032 1033 func main() { 1034 endpoint := rpc.MainNetBeta_RPC 1035 client := rpc.New(endpoint) 1036 1037 pubKey := solana.MustPublicKeyFromBase58("7xLk17EQQ5KLDLDe44wCmupJKJjTGd8hs3eSVVhCx932") 1038 out, err := client.GetBalance( 1039 context.TODO(), 1040 pubKey, 1041 rpc.CommitmentFinalized, 1042 ) 1043 if err != nil { 1044 panic(err) 1045 } 1046 spew.Dump(out) 1047 spew.Dump(out.Value) // total lamports on the account; 1 sol = 1000000000 lamports 1048 1049 var lamportsOnAccount = new(big.Float).SetUint64(uint64(out.Value)) 1050 // Convert lamports to sol: 1051 var solBalance = new(big.Float).Quo(lamportsOnAccount, new(big.Float).SetUint64(solana.LAMPORTS_PER_SOL)) 1052 1053 // WARNING: this is not a precise conversion. 1054 fmt.Println("◎", solBalance.Text('f', 10)) 1055 } 1056 ``` 1057 1058 #### [index](#contents) > [RPC](#rpc-methods) > GetBlock 1059 1060 ```go 1061 package main 1062 1063 import ( 1064 "context" 1065 1066 "github.com/davecgh/go-spew/spew" 1067 "github.com/gagliardetto/solana-go" 1068 "github.com/gagliardetto/solana-go/rpc" 1069 ) 1070 1071 func main() { 1072 endpoint := rpc.TestNet_RPC 1073 client := rpc.New(endpoint) 1074 1075 example, err := client.GetRecentBlockhash(context.TODO(), rpc.CommitmentFinalized) 1076 if err != nil { 1077 panic(err) 1078 } 1079 1080 { 1081 out, err := client.GetBlock(context.TODO(), uint64(example.Context.Slot)) 1082 if err != nil { 1083 panic(err) 1084 } 1085 // spew.Dump(out) // NOTE: This generates a lot of output. 1086 spew.Dump(len(out.Transactions)) 1087 } 1088 1089 { 1090 includeRewards := false 1091 out, err := client.GetBlockWithOpts( 1092 context.TODO(), 1093 uint64(example.Context.Slot), 1094 // You can specify more options here: 1095 &rpc.GetBlockOpts{ 1096 Encoding: solana.EncodingBase64, 1097 Commitment: rpc.CommitmentFinalized, 1098 // Get only signatures: 1099 TransactionDetails: rpc.TransactionDetailsSignatures, 1100 // Exclude rewards: 1101 Rewards: &includeRewards, 1102 }, 1103 ) 1104 if err != nil { 1105 panic(err) 1106 } 1107 spew.Dump(out) 1108 } 1109 } 1110 ``` 1111 1112 #### [index](#contents) > [RPC](#rpc-methods) > GetBlockCommitment 1113 1114 ```go 1115 package main 1116 1117 import ( 1118 "context" 1119 1120 "github.com/davecgh/go-spew/spew" 1121 "github.com/gagliardetto/solana-go/rpc" 1122 ) 1123 1124 func main() { 1125 endpoint := rpc.TestNet_RPC 1126 client := rpc.New(endpoint) 1127 1128 example, err := client.GetRecentBlockhash(context.TODO(), rpc.CommitmentFinalized) 1129 if err != nil { 1130 panic(err) 1131 } 1132 1133 out, err := client.GetBlockCommitment( 1134 context.TODO(), 1135 uint64(example.Context.Slot), 1136 ) 1137 if err != nil { 1138 panic(err) 1139 } 1140 spew.Dump(out) 1141 } 1142 ``` 1143 1144 #### [index](#contents) > [RPC](#rpc-methods) > GetBlockHeight 1145 1146 ```go 1147 package main 1148 1149 import ( 1150 "context" 1151 1152 "github.com/davecgh/go-spew/spew" 1153 "github.com/gagliardetto/solana-go/rpc" 1154 ) 1155 1156 func main() { 1157 endpoint := rpc.TestNet_RPC 1158 client := rpc.New(endpoint) 1159 1160 out, err := client.GetBlockHeight( 1161 context.TODO(), 1162 rpc.CommitmentFinalized, 1163 ) 1164 if err != nil { 1165 panic(err) 1166 } 1167 spew.Dump(out) 1168 } 1169 ``` 1170 1171 #### [index](#contents) > [RPC](#rpc-methods) > GetBlockProduction 1172 1173 ```go 1174 package main 1175 1176 import ( 1177 "context" 1178 1179 "github.com/davecgh/go-spew/spew" 1180 "github.com/gagliardetto/solana-go/rpc" 1181 ) 1182 1183 func main() { 1184 endpoint := rpc.TestNet_RPC 1185 client := rpc.New(endpoint) 1186 1187 { 1188 out, err := client.GetBlockProduction(context.TODO()) 1189 if err != nil { 1190 panic(err) 1191 } 1192 spew.Dump(out) 1193 } 1194 { 1195 out, err := client.GetBlockProductionWithOpts( 1196 context.TODO(), 1197 &rpc.GetBlockProductionOpts{ 1198 Commitment: rpc.CommitmentFinalized, 1199 // Range: &rpc.SlotRangeRequest{ 1200 // FirstSlot: XXXXXX, 1201 // Identity: solana.MustPublicKeyFromBase58("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"), 1202 // }, 1203 }, 1204 ) 1205 if err != nil { 1206 panic(err) 1207 } 1208 spew.Dump(out) 1209 } 1210 } 1211 ``` 1212 1213 #### [index](#contents) > [RPC](#rpc-methods) > GetBlockTime 1214 1215 ```go 1216 package main 1217 1218 import ( 1219 "context" 1220 "time" 1221 1222 "github.com/davecgh/go-spew/spew" 1223 "github.com/gagliardetto/solana-go/rpc" 1224 ) 1225 1226 func main() { 1227 endpoint := rpc.TestNet_RPC 1228 client := rpc.New(endpoint) 1229 1230 example, err := client.GetRecentBlockhash( 1231 context.TODO(), 1232 rpc.CommitmentFinalized, 1233 ) 1234 if err != nil { 1235 panic(err) 1236 } 1237 1238 out, err := client.GetBlockTime( 1239 context.TODO(), 1240 uint64(example.Context.Slot), 1241 ) 1242 if err != nil { 1243 panic(err) 1244 } 1245 spew.Dump(out) 1246 spew.Dump(out.Time().Format(time.RFC1123)) 1247 } 1248 ``` 1249 1250 #### [index](#contents) > [RPC](#rpc-methods) > GetBlocks 1251 1252 ```go 1253 package main 1254 1255 import ( 1256 "context" 1257 1258 "github.com/davecgh/go-spew/spew" 1259 "github.com/gagliardetto/solana-go/rpc" 1260 ) 1261 1262 func main() { 1263 endpoint := rpc.TestNet_RPC 1264 client := rpc.New(endpoint) 1265 1266 example, err := client.GetRecentBlockhash( 1267 context.TODO(), 1268 rpc.CommitmentFinalized, 1269 ) 1270 if err != nil { 1271 panic(err) 1272 } 1273 1274 endSlot := uint64(example.Context.Slot) 1275 out, err := client.GetBlocks( 1276 context.TODO(), 1277 uint64(example.Context.Slot-3), 1278 &endSlot, 1279 rpc.CommitmentFinalized, 1280 ) 1281 if err != nil { 1282 panic(err) 1283 } 1284 spew.Dump(out) 1285 } 1286 ``` 1287 1288 #### [index](#contents) > [RPC](#rpc-methods) > GetBlocksWithLimit 1289 1290 ```go 1291 package main 1292 1293 import ( 1294 "context" 1295 1296 "github.com/davecgh/go-spew/spew" 1297 "github.com/gagliardetto/solana-go/rpc" 1298 ) 1299 1300 func main() { 1301 endpoint := rpc.TestNet_RPC 1302 client := rpc.New(endpoint) 1303 1304 example, err := client.GetRecentBlockhash( 1305 context.TODO(), 1306 rpc.CommitmentFinalized, 1307 ) 1308 if err != nil { 1309 panic(err) 1310 } 1311 1312 limit := uint64(4) 1313 out, err := client.GetBlocksWithLimit( 1314 context.TODO(), 1315 uint64(example.Context.Slot-10), 1316 limit, 1317 rpc.CommitmentFinalized, 1318 ) 1319 if err != nil { 1320 panic(err) 1321 } 1322 spew.Dump(out) 1323 } 1324 ``` 1325 1326 #### [index](#contents) > [RPC](#rpc-methods) > GetClusterNodes 1327 1328 ```go 1329 package main 1330 1331 import ( 1332 "context" 1333 1334 "github.com/davecgh/go-spew/spew" 1335 "github.com/gagliardetto/solana-go/rpc" 1336 ) 1337 1338 func main() { 1339 endpoint := rpc.TestNet_RPC 1340 client := rpc.New(endpoint) 1341 1342 out, err := client.GetClusterNodes( 1343 context.TODO(), 1344 ) 1345 if err != nil { 1346 panic(err) 1347 } 1348 spew.Dump(out) 1349 } 1350 ``` 1351 1352 #### [index](#contents) > [RPC](#rpc-methods) > GetConfirmedBlock 1353 1354 ```go 1355 package main 1356 1357 import ( 1358 "context" 1359 1360 "github.com/AlekSi/pointer" 1361 "github.com/davecgh/go-spew/spew" 1362 "github.com/gagliardetto/solana-go" 1363 "github.com/gagliardetto/solana-go/rpc" 1364 ) 1365 1366 func main() { 1367 endpoint := rpc.TestNet_RPC 1368 client := rpc.New(endpoint) 1369 1370 example, err := client.GetRecentBlockhash( 1371 context.TODO(), 1372 rpc.CommitmentFinalized, 1373 ) 1374 if err != nil { 1375 panic(err) 1376 } 1377 1378 { // deprecated and is going to be removed in solana-core v1.8 1379 out, err := client.GetConfirmedBlock( 1380 context.TODO(), 1381 uint64(example.Context.Slot), 1382 ) 1383 if err != nil { 1384 panic(err) 1385 } 1386 spew.Dump(out) 1387 } 1388 { 1389 slot := uint64(example.Context.Slot) 1390 out, err := client.GetConfirmedBlockWithOpts( 1391 context.TODO(), 1392 slot, 1393 // You can specify more options here: 1394 &rpc.GetConfirmedBlockOpts{ 1395 Encoding: solana.EncodingBase64, 1396 Commitment: rpc.CommitmentFinalized, 1397 // Get only signatures: 1398 TransactionDetails: rpc.TransactionDetailsSignatures, 1399 // Exclude rewards: 1400 Rewards: pointer.ToBool(false), 1401 }, 1402 ) 1403 if err != nil { 1404 panic(err) 1405 } 1406 spew.Dump(out) 1407 } 1408 } 1409 ``` 1410 1411 #### [index](#contents) > [RPC](#rpc-methods) > GetConfirmedBlocks 1412 1413 ```go 1414 package main 1415 1416 import ( 1417 "context" 1418 1419 "github.com/davecgh/go-spew/spew" 1420 "github.com/gagliardetto/solana-go/rpc" 1421 ) 1422 1423 func main() { 1424 endpoint := rpc.TestNet_RPC 1425 client := rpc.New(endpoint) 1426 1427 example, err := client.GetRecentBlockhash( 1428 context.TODO(), 1429 rpc.CommitmentFinalized, 1430 ) 1431 if err != nil { 1432 panic(err) 1433 } 1434 1435 { 1436 endSlot := uint64(example.Context.Slot) 1437 // deprecated and is going to be removed in solana-core v1.8 1438 out, err := client.GetConfirmedBlocks( 1439 context.TODO(), 1440 uint64(example.Context.Slot-3), 1441 &endSlot, 1442 rpc.CommitmentFinalized, 1443 ) 1444 if err != nil { 1445 panic(err) 1446 } 1447 spew.Dump(out) 1448 } 1449 } 1450 ``` 1451 1452 #### [index](#contents) > [RPC](#rpc-methods) > GetConfirmedBlocksWithLimit 1453 1454 ```go 1455 package main 1456 1457 import ( 1458 "context" 1459 1460 "github.com/davecgh/go-spew/spew" 1461 "github.com/gagliardetto/solana-go/rpc" 1462 ) 1463 1464 func main() { 1465 endpoint := rpc.TestNet_RPC 1466 client := rpc.New(endpoint) 1467 1468 example, err := client.GetRecentBlockhash( 1469 context.TODO(), 1470 rpc.CommitmentFinalized, 1471 ) 1472 if err != nil { 1473 panic(err) 1474 } 1475 1476 limit := uint64(3) 1477 { // deprecated and is going to be removed in solana-core v1.8 1478 out, err := client.GetConfirmedBlocksWithLimit( 1479 context.TODO(), 1480 uint64(example.Context.Slot-10), 1481 limit, 1482 rpc.CommitmentFinalized, 1483 ) 1484 if err != nil { 1485 panic(err) 1486 } 1487 spew.Dump(out) 1488 } 1489 } 1490 ``` 1491 1492 #### [index](#contents) > [RPC](#rpc-methods) > GetConfirmedSignaturesForAddress2 1493 1494 ```go 1495 package main 1496 1497 import ( 1498 "context" 1499 1500 "github.com/davecgh/go-spew/spew" 1501 "github.com/gagliardetto/solana-go" 1502 "github.com/gagliardetto/solana-go/rpc" 1503 ) 1504 1505 func main() { 1506 endpoint := rpc.TestNet_RPC 1507 client := rpc.New(endpoint) 1508 1509 pubKey := solana.MustPublicKeyFromBase58("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt") // serum token 1510 { 1511 // deprecated and is going to be removed in solana-core v1.8 1512 out, err := client.GetConfirmedSignaturesForAddress2( 1513 context.TODO(), 1514 pubKey, 1515 // TODO: 1516 nil, 1517 ) 1518 if err != nil { 1519 panic(err) 1520 } 1521 spew.Dump(out) 1522 } 1523 } 1524 ``` 1525 1526 #### [index](#contents) > [RPC](#rpc-methods) > GetConfirmedTransaction 1527 1528 ```go 1529 package main 1530 1531 import ( 1532 "context" 1533 1534 "github.com/davecgh/go-spew/spew" 1535 "github.com/gagliardetto/solana-go" 1536 "github.com/gagliardetto/solana-go/rpc" 1537 ) 1538 1539 func main() { 1540 endpoint := rpc.TestNet_RPC 1541 client := rpc.New(endpoint) 1542 1543 pubKey := solana.MustPublicKeyFromBase58("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt") // serum token 1544 // Let's get a valid transaction to use in the example: 1545 example, err := client.GetConfirmedSignaturesForAddress2( 1546 context.TODO(), 1547 pubKey, 1548 nil, 1549 ) 1550 if err != nil { 1551 panic(err) 1552 } 1553 1554 out, err := client.GetConfirmedTransaction( 1555 context.TODO(), 1556 example[0].Signature, 1557 ) 1558 if err != nil { 1559 panic(err) 1560 } 1561 spew.Dump(out) 1562 } 1563 ``` 1564 1565 #### [index](#contents) > [RPC](#rpc-methods) > GetEpochInfo 1566 1567 ```go 1568 package main 1569 1570 import ( 1571 "context" 1572 1573 "github.com/davecgh/go-spew/spew" 1574 "github.com/gagliardetto/solana-go/rpc" 1575 ) 1576 1577 func main() { 1578 endpoint := rpc.TestNet_RPC 1579 client := rpc.New(endpoint) 1580 1581 out, err := client.GetEpochInfo( 1582 context.TODO(), 1583 rpc.CommitmentFinalized, 1584 ) 1585 if err != nil { 1586 panic(err) 1587 } 1588 spew.Dump(out) 1589 } 1590 ``` 1591 1592 #### [index](#contents) > [RPC](#rpc-methods) > GetEpochSchedule 1593 1594 ```go 1595 package main 1596 1597 import ( 1598 "context" 1599 1600 "github.com/davecgh/go-spew/spew" 1601 "github.com/gagliardetto/solana-go/rpc" 1602 ) 1603 1604 func main() { 1605 endpoint := rpc.TestNet_RPC 1606 client := rpc.New(endpoint) 1607 1608 out, err := client.GetEpochSchedule( 1609 context.TODO(), 1610 ) 1611 if err != nil { 1612 panic(err) 1613 } 1614 spew.Dump(out) 1615 } 1616 ``` 1617 1618 #### [index](#contents) > [RPC](#rpc-methods) > GetFeeCalculatorForBlockhash 1619 1620 ```go 1621 package main 1622 1623 import ( 1624 "context" 1625 1626 "github.com/davecgh/go-spew/spew" 1627 "github.com/gagliardetto/solana-go/rpc" 1628 ) 1629 1630 func main() { 1631 endpoint := rpc.TestNet_RPC 1632 client := rpc.New(endpoint) 1633 1634 example, err := client.GetRecentBlockhash( 1635 context.TODO(), 1636 rpc.CommitmentFinalized, 1637 ) 1638 if err != nil { 1639 panic(err) 1640 } 1641 1642 out, err := client.GetFeeCalculatorForBlockhash( 1643 context.TODO(), 1644 example.Value.Blockhash, 1645 rpc.CommitmentFinalized, 1646 ) 1647 if err != nil { 1648 panic(err) 1649 } 1650 spew.Dump(out) 1651 } 1652 ``` 1653 1654 #### [index](#contents) > [RPC](#rpc-methods) > GetFeeRateGovernor 1655 1656 ```go 1657 package main 1658 1659 import ( 1660 "context" 1661 1662 "github.com/davecgh/go-spew/spew" 1663 "github.com/gagliardetto/solana-go/rpc" 1664 ) 1665 1666 func main() { 1667 endpoint := rpc.TestNet_RPC 1668 client := rpc.New(endpoint) 1669 1670 out, err := client.GetFeeRateGovernor( 1671 context.TODO(), 1672 ) 1673 if err != nil { 1674 panic(err) 1675 } 1676 spew.Dump(out) 1677 } 1678 ``` 1679 1680 #### [index](#contents) > [RPC](#rpc-methods) > GetFees 1681 1682 ```go 1683 package main 1684 1685 import ( 1686 "context" 1687 1688 "github.com/davecgh/go-spew/spew" 1689 "github.com/gagliardetto/solana-go/rpc" 1690 ) 1691 1692 func main() { 1693 endpoint := rpc.TestNet_RPC 1694 client := rpc.New(endpoint) 1695 1696 out, err := client.GetFees( 1697 context.TODO(), 1698 rpc.CommitmentFinalized, 1699 ) 1700 if err != nil { 1701 panic(err) 1702 } 1703 spew.Dump(out) 1704 } 1705 ``` 1706 1707 #### [index](#contents) > [RPC](#rpc-methods) > GetFeeForMessage 1708 1709 ```go 1710 package main 1711 1712 import ( 1713 "context" 1714 1715 "github.com/davecgh/go-spew/spew" 1716 "github.com/gagliardetto/solana-go/rpc" 1717 ) 1718 1719 func main() { 1720 endpoint := rpc.TestNet_RPC 1721 client := rpc.New(endpoint) 1722 1723 example, err := client.GetFeeForMessage( 1724 context.Background(), 1725 "AQABAgIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEBAQAA", 1726 rpc.CommitmentProcessed, 1727 ) 1728 if err != nil { 1729 panic(err) 1730 } 1731 spew.Dump(example) 1732 } 1733 ``` 1734 1735 #### [index](#contents) > [RPC](#rpc-methods) > GetFirstAvailableBlock 1736 1737 ```go 1738 package main 1739 1740 import ( 1741 "context" 1742 1743 "github.com/davecgh/go-spew/spew" 1744 "github.com/gagliardetto/solana-go/rpc" 1745 ) 1746 1747 func main() { 1748 endpoint := rpc.TestNet_RPC 1749 client := rpc.New(endpoint) 1750 1751 out, err := client.GetFirstAvailableBlock( 1752 context.TODO(), 1753 ) 1754 if err != nil { 1755 panic(err) 1756 } 1757 spew.Dump(out) 1758 } 1759 ``` 1760 1761 #### [index](#contents) > [RPC](#rpc-methods) > GetGenesisHash 1762 1763 ```go 1764 package main 1765 1766 import ( 1767 "context" 1768 1769 "github.com/davecgh/go-spew/spew" 1770 "github.com/gagliardetto/solana-go/rpc" 1771 ) 1772 1773 func main() { 1774 endpoint := rpc.TestNet_RPC 1775 client := rpc.New(endpoint) 1776 1777 out, err := client.GetGenesisHash( 1778 context.TODO(), 1779 ) 1780 if err != nil { 1781 panic(err) 1782 } 1783 spew.Dump(out) 1784 } 1785 ``` 1786 1787 #### [index](#contents) > [RPC](#rpc-methods) > GetHealth 1788 1789 ```go 1790 package main 1791 1792 import ( 1793 "context" 1794 1795 "github.com/davecgh/go-spew/spew" 1796 "github.com/gagliardetto/solana-go/rpc" 1797 ) 1798 1799 func main() { 1800 endpoint := rpc.TestNet_RPC 1801 client := rpc.New(endpoint) 1802 1803 out, err := client.GetHealth( 1804 context.TODO(), 1805 ) 1806 if err != nil { 1807 panic(err) 1808 } 1809 spew.Dump(out) 1810 spew.Dump(out == rpc.HealthOk) 1811 } 1812 ``` 1813 1814 #### [index](#contents) > [RPC](#rpc-methods) > GetHighestSnapshotSlot 1815 1816 ```go 1817 package main 1818 1819 import ( 1820 "context" 1821 1822 "github.com/davecgh/go-spew/spew" 1823 "github.com/gagliardetto/solana-go/rpc" 1824 ) 1825 1826 func main() { 1827 endpoint := rpc.TestNet_RPC 1828 client := rpc.New(endpoint) 1829 1830 example, err := client.GetHighestSnapshotSlot( 1831 context.Background(), 1832 ) 1833 if err != nil { 1834 panic(err) 1835 } 1836 spew.Dump(example) 1837 } 1838 ``` 1839 1840 #### [index](#contents) > [RPC](#rpc-methods) > GetLatestBlockhash 1841 1842 NEW: This method is only available in solana-core v1.9 or newer. Please use getRecentBlockhash for solana-core v1.8 1843 1844 ```go 1845 package main 1846 1847 import ( 1848 "context" 1849 1850 "github.com/davecgh/go-spew/spew" 1851 "github.com/gagliardetto/solana-go/rpc" 1852 ) 1853 1854 func main() { 1855 endpoint := rpc.TestNet_RPC 1856 client := rpc.New(endpoint) 1857 1858 example, err := client.GetLatestBlockhash( 1859 context.Background(), 1860 rpc.CommitmentFinalized, 1861 ) 1862 if err != nil { 1863 panic(err) 1864 } 1865 spew.Dump(example) 1866 } 1867 ``` 1868 1869 #### [index](#contents) > [RPC](#rpc-methods) > GetIdentity 1870 1871 ```go 1872 package main 1873 1874 import ( 1875 "context" 1876 1877 "github.com/davecgh/go-spew/spew" 1878 "github.com/gagliardetto/solana-go/rpc" 1879 ) 1880 1881 func main() { 1882 endpoint := rpc.TestNet_RPC 1883 client := rpc.New(endpoint) 1884 1885 out, err := client.GetIdentity( 1886 context.TODO(), 1887 ) 1888 if err != nil { 1889 panic(err) 1890 } 1891 spew.Dump(out) 1892 } 1893 ``` 1894 1895 #### [index](#contents) > [RPC](#rpc-methods) > GetInflationGovernor 1896 1897 ```go 1898 package main 1899 1900 import ( 1901 "context" 1902 1903 "github.com/davecgh/go-spew/spew" 1904 "github.com/gagliardetto/solana-go/rpc" 1905 ) 1906 1907 func main() { 1908 endpoint := rpc.TestNet_RPC 1909 client := rpc.New(endpoint) 1910 1911 out, err := client.GetInflationGovernor( 1912 context.TODO(), 1913 rpc.CommitmentFinalized, 1914 ) 1915 if err != nil { 1916 panic(err) 1917 } 1918 spew.Dump(out) 1919 } 1920 ``` 1921 1922 #### [index](#contents) > [RPC](#rpc-methods) > GetInflationRate 1923 1924 ```go 1925 package main 1926 1927 import ( 1928 "context" 1929 1930 "github.com/davecgh/go-spew/spew" 1931 "github.com/gagliardetto/solana-go/rpc" 1932 ) 1933 1934 func main() { 1935 endpoint := rpc.TestNet_RPC 1936 client := rpc.New(endpoint) 1937 1938 out, err := client.GetInflationRate( 1939 context.TODO(), 1940 ) 1941 if err != nil { 1942 panic(err) 1943 } 1944 spew.Dump(out) 1945 } 1946 ``` 1947 1948 #### [index](#contents) > [RPC](#rpc-methods) > GetInflationReward 1949 1950 ```go 1951 package main 1952 1953 import ( 1954 "context" 1955 1956 "github.com/davecgh/go-spew/spew" 1957 "github.com/gagliardetto/solana-go" 1958 "github.com/gagliardetto/solana-go/rpc" 1959 ) 1960 1961 func main() { 1962 endpoint := rpc.TestNet_RPC 1963 client := rpc.New(endpoint) 1964 1965 pubKey := solana.MustPublicKeyFromBase58("6dmNQ5jwLeLk5REvio1JcMshcbvkYMwy26sJ8pbkvStu") 1966 1967 out, err := client.GetInflationReward( 1968 context.TODO(), 1969 []solana.PublicKey{ 1970 pubKey, 1971 }, 1972 &rpc.GetInflationRewardOpts{ 1973 Commitment: rpc.CommitmentFinalized, 1974 }, 1975 ) 1976 if err != nil { 1977 panic(err) 1978 } 1979 spew.Dump(out) 1980 } 1981 ``` 1982 1983 #### [index](#contents) > [RPC](#rpc-methods) > GetLargestAccounts 1984 1985 ```go 1986 package main 1987 1988 import ( 1989 "context" 1990 1991 "github.com/davecgh/go-spew/spew" 1992 "github.com/gagliardetto/solana-go/rpc" 1993 ) 1994 1995 func main() { 1996 endpoint := rpc.TestNet_RPC 1997 client := rpc.New(endpoint) 1998 1999 out, err := client.GetLargestAccounts( 2000 context.TODO(), 2001 rpc.CommitmentFinalized, 2002 rpc.LargestAccountsFilterCirculating, 2003 ) 2004 if err != nil { 2005 panic(err) 2006 } 2007 spew.Dump(out) 2008 } 2009 ``` 2010 2011 #### [index](#contents) > [RPC](#rpc-methods) > GetLeaderSchedule 2012 2013 ```go 2014 package main 2015 2016 import ( 2017 "context" 2018 2019 "github.com/davecgh/go-spew/spew" 2020 "github.com/gagliardetto/solana-go/rpc" 2021 ) 2022 2023 func main() { 2024 endpoint := rpc.TestNet_RPC 2025 client := rpc.New(endpoint) 2026 2027 out, err := client.GetLeaderSchedule( 2028 context.TODO(), 2029 ) 2030 if err != nil { 2031 panic(err) 2032 } 2033 spew.Dump(out) // NOTE: this creates a lot of output 2034 } 2035 ``` 2036 2037 #### [index](#contents) > [RPC](#rpc-methods) > GetMaxRetransmitSlot 2038 2039 ```go 2040 package main 2041 2042 import ( 2043 "context" 2044 2045 "github.com/davecgh/go-spew/spew" 2046 "github.com/gagliardetto/solana-go/rpc" 2047 ) 2048 2049 func main() { 2050 endpoint := rpc.TestNet_RPC 2051 client := rpc.New(endpoint) 2052 2053 out, err := client.GetMaxRetransmitSlot( 2054 context.TODO(), 2055 ) 2056 if err != nil { 2057 panic(err) 2058 } 2059 spew.Dump(out) 2060 } 2061 ``` 2062 2063 #### [index](#contents) > [RPC](#rpc-methods) > GetMaxShredInsertSlot 2064 2065 ```go 2066 package main 2067 2068 import ( 2069 "context" 2070 2071 "github.com/davecgh/go-spew/spew" 2072 "github.com/gagliardetto/solana-go/rpc" 2073 ) 2074 2075 func main() { 2076 endpoint := rpc.TestNet_RPC 2077 client := rpc.New(endpoint) 2078 2079 out, err := client.GetMaxShredInsertSlot( 2080 context.TODO(), 2081 ) 2082 if err != nil { 2083 panic(err) 2084 } 2085 spew.Dump(out) 2086 } 2087 ``` 2088 2089 #### [index](#contents) > [RPC](#rpc-methods) > GetMinimumBalanceForRentExemption 2090 2091 ```go 2092 package main 2093 2094 import ( 2095 "context" 2096 2097 "github.com/davecgh/go-spew/spew" 2098 "github.com/gagliardetto/solana-go/rpc" 2099 ) 2100 2101 func main() { 2102 endpoint := rpc.TestNet_RPC 2103 client := rpc.New(endpoint) 2104 2105 dataSize := uint64(1024 * 9) 2106 out, err := client.GetMinimumBalanceForRentExemption( 2107 context.TODO(), 2108 dataSize, 2109 rpc.CommitmentFinalized, 2110 ) 2111 if err != nil { 2112 panic(err) 2113 } 2114 spew.Dump(out) 2115 } 2116 ``` 2117 2118 #### [index](#contents) > [RPC](#rpc-methods) > GetMultipleAccounts 2119 2120 ```go 2121 package main 2122 2123 import ( 2124 "context" 2125 2126 "github.com/davecgh/go-spew/spew" 2127 "github.com/gagliardetto/solana-go" 2128 "github.com/gagliardetto/solana-go/rpc" 2129 ) 2130 2131 func main() { 2132 endpoint := rpc.MainNetBeta_RPC 2133 client := rpc.New(endpoint) 2134 2135 { 2136 out, err := client.GetMultipleAccounts( 2137 context.TODO(), 2138 solana.MustPublicKeyFromBase58("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt"), // serum token 2139 solana.MustPublicKeyFromBase58("4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R"), // raydium token 2140 ) 2141 if err != nil { 2142 panic(err) 2143 } 2144 spew.Dump(out) 2145 } 2146 { 2147 out, err := client.GetMultipleAccountsWithOpts( 2148 context.TODO(), 2149 []solana.PublicKey{solana.MustPublicKeyFromBase58("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt"), // serum token 2150 solana.MustPublicKeyFromBase58("4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R"), // raydium token 2151 }, 2152 &rpc.GetMultipleAccountsOpts{ 2153 Encoding: solana.EncodingBase64Zstd, 2154 Commitment: rpc.CommitmentFinalized, 2155 // You can get just a part of the account data by specify a DataSlice: 2156 // DataSlice: &rpc.DataSlice{ 2157 // Offset: pointer.ToUint64(0), 2158 // Length: pointer.ToUint64(1024), 2159 // }, 2160 }, 2161 ) 2162 if err != nil { 2163 panic(err) 2164 } 2165 spew.Dump(out) 2166 } 2167 } 2168 ``` 2169 2170 #### [index](#contents) > [RPC](#rpc-methods) > GetProgramAccounts 2171 2172 ```go 2173 package main 2174 2175 import ( 2176 "context" 2177 2178 "github.com/davecgh/go-spew/spew" 2179 "github.com/gagliardetto/solana-go" 2180 "github.com/gagliardetto/solana-go/rpc" 2181 ) 2182 2183 func main() { 2184 endpoint := rpc.TestNet_RPC 2185 client := rpc.New(endpoint) 2186 2187 out, err := client.GetProgramAccounts( 2188 context.TODO(), 2189 solana.MustPublicKeyFromBase58("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"), 2190 ) 2191 if err != nil { 2192 panic(err) 2193 } 2194 spew.Dump(len(out)) 2195 spew.Dump(out) // NOTE: this can generate a lot of output 2196 } 2197 ``` 2198 2199 #### [index](#contents) > [RPC](#rpc-methods) > GetRecentBlockhash 2200 2201 ```go 2202 package main 2203 2204 import ( 2205 "context" 2206 2207 "github.com/davecgh/go-spew/spew" 2208 "github.com/gagliardetto/solana-go/rpc" 2209 ) 2210 2211 func main() { 2212 endpoint := rpc.TestNet_RPC 2213 client := rpc.New(endpoint) 2214 2215 recent, err := client.GetRecentBlockhash( 2216 context.TODO(), 2217 rpc.CommitmentFinalized, 2218 ) 2219 if err != nil { 2220 panic(err) 2221 } 2222 spew.Dump(recent) 2223 } 2224 ``` 2225 #### [index](#contents) > [RPC](#rpc-methods) > GetRecentPerformanceSamples 2226 2227 ```go 2228 package main 2229 2230 import ( 2231 "context" 2232 2233 "github.com/davecgh/go-spew/spew" 2234 "github.com/gagliardetto/solana-go/rpc" 2235 ) 2236 2237 func main() { 2238 endpoint := rpc.TestNet_RPC 2239 client := rpc.New(endpoint) 2240 2241 limit := uint(3) 2242 out, err := client.GetRecentPerformanceSamples( 2243 context.TODO(), 2244 &limit, 2245 ) 2246 if err != nil { 2247 panic(err) 2248 } 2249 spew.Dump(out) 2250 } 2251 ``` 2252 2253 #### [index](#contents) > [RPC](#rpc-methods) > GetRecentPrioritizationFees 2254 2255 ```go 2256 package main 2257 2258 import ( 2259 "context" 2260 2261 "github.com/davecgh/go-spew/spew" 2262 "github.com/gagliardetto/solana-go" 2263 "github.com/gagliardetto/solana-go/rpc" 2264 ) 2265 2266 func main() { 2267 endpoint := rpc.TestNet_RPC 2268 client := rpc.New(endpoint) 2269 2270 out, err := client.GetRecentPrioritizationFees( 2271 context.TODO(), 2272 []solana.PublicKey{ 2273 solana.MustPublicKeyFromBase58("q5BgreVhTyBH1QCeriVb7kQYEPneanFXPLjvyjdf8M3"), 2274 }, 2275 ) 2276 if err != nil { 2277 panic(err) 2278 } 2279 spew.Dump(out) 2280 } 2281 ``` 2282 2283 #### [index](#contents) > [RPC](#rpc-methods) > GetSignatureStatuses 2284 2285 ```go 2286 package main 2287 2288 import ( 2289 "context" 2290 2291 "github.com/davecgh/go-spew/spew" 2292 "github.com/gagliardetto/solana-go" 2293 "github.com/gagliardetto/solana-go/rpc" 2294 ) 2295 2296 func main() { 2297 endpoint := rpc.TestNet_RPC 2298 client := rpc.New(endpoint) 2299 2300 out, err := client.GetSignatureStatuses( 2301 context.TODO(), 2302 true, 2303 // All the transactions you want the get the status for: 2304 solana.MustSignatureFromBase58("2CwH8SqVZWFa1EvsH7vJXGFors1NdCuWJ7Z85F8YqjCLQ2RuSHQyeGKkfo1Tj9HitSTeLoMWnxpjxF2WsCH8nGWh"), 2305 solana.MustSignatureFromBase58("5YJHZPeHZuZjhunBc1CCB1NDRNf2tTJNpdb3azGsR7PfyEncCDhr95wG8EWrvjNXBc4wCKixkheSbCxoC2NCG3X7"), 2306 ) 2307 if err != nil { 2308 panic(err) 2309 } 2310 spew.Dump(out) 2311 } 2312 ``` 2313 2314 #### [index](#contents) > [RPC](#rpc-methods) > GetSignaturesForAddress 2315 2316 ```go 2317 package main 2318 2319 import ( 2320 "context" 2321 2322 "github.com/davecgh/go-spew/spew" 2323 "github.com/gagliardetto/solana-go" 2324 "github.com/gagliardetto/solana-go/rpc" 2325 ) 2326 2327 func main() { 2328 endpoint := rpc.TestNet_RPC 2329 client := rpc.New(endpoint) 2330 2331 out, err := client.GetSignaturesForAddress( 2332 context.TODO(), 2333 solana.MustPublicKeyFromBase58("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"), 2334 ) 2335 if err != nil { 2336 panic(err) 2337 } 2338 spew.Dump(out) 2339 } 2340 ``` 2341 2342 #### [index](#contents) > [RPC](#rpc-methods) > GetSlot 2343 2344 ```go 2345 package main 2346 2347 import ( 2348 "context" 2349 2350 "github.com/davecgh/go-spew/spew" 2351 "github.com/gagliardetto/solana-go/rpc" 2352 ) 2353 2354 func main() { 2355 endpoint := rpc.TestNet_RPC 2356 client := rpc.New(endpoint) 2357 2358 out, err := client.GetSlot( 2359 context.TODO(), 2360 rpc.CommitmentFinalized, 2361 ) 2362 if err != nil { 2363 panic(err) 2364 } 2365 spew.Dump(out) 2366 } 2367 ``` 2368 2369 #### [index](#contents) > [RPC](#rpc-methods) > GetSlotLeader 2370 2371 ```go 2372 package main 2373 2374 import ( 2375 "context" 2376 2377 "github.com/davecgh/go-spew/spew" 2378 "github.com/gagliardetto/solana-go/rpc" 2379 ) 2380 2381 func main() { 2382 endpoint := rpc.TestNet_RPC 2383 client := rpc.New(endpoint) 2384 2385 out, err := client.GetSlotLeader( 2386 context.TODO(), 2387 rpc.CommitmentFinalized, 2388 ) 2389 if err != nil { 2390 panic(err) 2391 } 2392 spew.Dump(out) 2393 } 2394 ``` 2395 2396 #### [index](#contents) > [RPC](#rpc-methods) > GetSlotLeaders 2397 2398 ```go 2399 package main 2400 2401 import ( 2402 "context" 2403 2404 "github.com/davecgh/go-spew/spew" 2405 "github.com/gagliardetto/solana-go/rpc" 2406 ) 2407 2408 func main() { 2409 endpoint := rpc.TestNet_RPC 2410 client := rpc.New(endpoint) 2411 2412 recent, err := client.GetRecentBlockhash( 2413 context.TODO(), 2414 rpc.CommitmentFinalized, 2415 ) 2416 if err != nil { 2417 panic(err) 2418 } 2419 2420 out, err := client.GetSlotLeaders( 2421 context.TODO(), 2422 uint64(recent.Context.Slot), 2423 10, 2424 ) 2425 if err != nil { 2426 panic(err) 2427 } 2428 spew.Dump(out) 2429 } 2430 ``` 2431 2432 #### [index](#contents) > [RPC](#rpc-methods) > GetSnapshotSlot 2433 2434 ```go 2435 package main 2436 2437 import ( 2438 "context" 2439 2440 "github.com/davecgh/go-spew/spew" 2441 "github.com/gagliardetto/solana-go/rpc" 2442 ) 2443 2444 func main() { 2445 endpoint := rpc.TestNet_RPC 2446 client := rpc.New(endpoint) 2447 2448 out, err := client.GetSnapshotSlot( 2449 context.TODO(), 2450 ) 2451 if err != nil { 2452 panic(err) 2453 } 2454 spew.Dump(out) 2455 } 2456 ``` 2457 2458 #### [index](#contents) > [RPC](#rpc-methods) > GetStakeActivation 2459 2460 ```go 2461 package main 2462 2463 import ( 2464 "context" 2465 2466 "github.com/davecgh/go-spew/spew" 2467 "github.com/gagliardetto/solana-go" 2468 "github.com/gagliardetto/solana-go/rpc" 2469 ) 2470 2471 func main() { 2472 endpoint := rpc.TestNet_RPC 2473 client := rpc.New(endpoint) 2474 2475 pubKey := solana.MustPublicKeyFromBase58("EW2p7QCJNHMVj5nQCcW7Q2BDETtNBXn68FyucU4RCjvb") 2476 out, err := client.GetStakeActivation( 2477 context.TODO(), 2478 pubKey, 2479 rpc.CommitmentFinalized, 2480 nil, 2481 ) 2482 if err != nil { 2483 panic(err) 2484 } 2485 spew.Dump(out) 2486 } 2487 ``` 2488 2489 #### [index](#contents) > [RPC](#rpc-methods) > GetSupply 2490 2491 ```go 2492 package main 2493 2494 import ( 2495 "context" 2496 2497 "github.com/davecgh/go-spew/spew" 2498 "github.com/gagliardetto/solana-go/rpc" 2499 ) 2500 2501 func main() { 2502 endpoint := rpc.TestNet_RPC 2503 client := rpc.New(endpoint) 2504 2505 out, err := client.GetSupply( 2506 context.TODO(), 2507 rpc.CommitmentFinalized, 2508 ) 2509 if err != nil { 2510 panic(err) 2511 } 2512 spew.Dump(out) 2513 } 2514 ``` 2515 2516 #### [index](#contents) > [RPC](#rpc-methods) > GetTokenAccountBalance 2517 2518 ```go 2519 package main 2520 2521 import ( 2522 "context" 2523 2524 "github.com/davecgh/go-spew/spew" 2525 "github.com/gagliardetto/solana-go" 2526 "github.com/gagliardetto/solana-go/rpc" 2527 ) 2528 2529 func main() { 2530 endpoint := rpc.TestNet_RPC 2531 client := rpc.New(endpoint) 2532 2533 pubKey := solana.MustPublicKeyFromBase58("EzK5qLWhftu8Z2znVa5fozVtobbjhd8Gdu9hQHpC8bec") 2534 out, err := client.GetTokenAccountBalance( 2535 context.TODO(), 2536 pubKey, 2537 rpc.CommitmentFinalized, 2538 ) 2539 if err != nil { 2540 panic(err) 2541 } 2542 spew.Dump(out) 2543 } 2544 ``` 2545 2546 #### [index](#contents) > [RPC](#rpc-methods) > GetTokenAccountsByDelegate 2547 2548 ```go 2549 package main 2550 2551 import ( 2552 "context" 2553 2554 "github.com/davecgh/go-spew/spew" 2555 "github.com/gagliardetto/solana-go" 2556 "github.com/gagliardetto/solana-go/rpc" 2557 ) 2558 2559 func main() { 2560 endpoint := rpc.TestNet_RPC 2561 client := rpc.New(endpoint) 2562 2563 pubKey := solana.MustPublicKeyFromBase58("AfkALUPjQp8R1rUwE6KhT38NuTYWCncwwHwcJu7UtAfV") 2564 out, err := client.GetTokenAccountsByDelegate( 2565 context.TODO(), 2566 pubKey, 2567 &rpc.GetTokenAccountsConfig{ 2568 Mint: solana.MustPublicKeyFromBase58("So11111111111111111111111111111111111111112"), 2569 }, 2570 nil, 2571 ) 2572 if err != nil { 2573 panic(err) 2574 } 2575 spew.Dump(out) 2576 } 2577 ``` 2578 2579 #### [index](#contents) > [RPC](#rpc-methods) > GetTokenAccountsByOwner 2580 2581 ```go 2582 package main 2583 2584 import ( 2585 "context" 2586 2587 "github.com/davecgh/go-spew/spew" 2588 bin "github.com/gagliardetto/binary" 2589 "github.com/gagliardetto/solana-go" 2590 "github.com/gagliardetto/solana-go/programs/token" 2591 "github.com/gagliardetto/solana-go/rpc" 2592 ) 2593 2594 func main() { 2595 endpoint := rpc.TestNet_RPC 2596 client := rpc.New(endpoint) 2597 2598 pubKey := solana.MustPublicKeyFromBase58("7HZaCWazgTuuFuajxaaxGYbGnyVKwxvsJKue1W4Nvyro") 2599 out, err := client.GetTokenAccountsByOwner( 2600 context.TODO(), 2601 pubKey, 2602 &rpc.GetTokenAccountsConfig{ 2603 Mint: solana.WrappedSol.ToPointer(), 2604 }, 2605 &rpc.GetTokenAccountsOpts{ 2606 Encoding: solana.EncodingBase64Zstd, 2607 }, 2608 ) 2609 if err != nil { 2610 panic(err) 2611 } 2612 spew.Dump(out) 2613 2614 { 2615 tokenAccounts := make([]token.Account, 0) 2616 for _, rawAccount := range out.Value { 2617 var tokAcc token.Account 2618 2619 data := rawAccount.Account.Data.GetBinary() 2620 dec := bin.NewBinDecoder(data) 2621 err := dec.Decode(&tokAcc) 2622 if err != nil { 2623 panic(err) 2624 } 2625 tokenAccounts = append(tokenAccounts, tokAcc) 2626 } 2627 spew.Dump(tokenAccounts) 2628 } 2629 } 2630 2631 ``` 2632 2633 #### [index](#contents) > [RPC](#rpc-methods) > GetTokenLargestAccounts 2634 2635 ```go 2636 package main 2637 2638 import ( 2639 "context" 2640 2641 "github.com/davecgh/go-spew/spew" 2642 "github.com/gagliardetto/solana-go" 2643 "github.com/gagliardetto/solana-go/rpc" 2644 ) 2645 2646 func main() { 2647 endpoint := rpc.MainNetBeta_RPC 2648 client := rpc.New(endpoint) 2649 2650 pubKey := solana.MustPublicKeyFromBase58("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt") // serum token 2651 out, err := client.GetTokenLargestAccounts( 2652 context.TODO(), 2653 pubKey, 2654 rpc.CommitmentFinalized, 2655 ) 2656 if err != nil { 2657 panic(err) 2658 } 2659 spew.Dump(out) 2660 } 2661 ``` 2662 2663 #### [index](#contents) > [RPC](#rpc-methods) > GetTokenSupply 2664 2665 ```go 2666 package main 2667 2668 import ( 2669 "context" 2670 2671 "github.com/davecgh/go-spew/spew" 2672 "github.com/gagliardetto/solana-go" 2673 "github.com/gagliardetto/solana-go/rpc" 2674 ) 2675 2676 func main() { 2677 endpoint := rpc.MainNetBeta_RPC 2678 client := rpc.New(endpoint) 2679 2680 pubKey := solana.MustPublicKeyFromBase58("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt") // serum token 2681 out, err := client.GetTokenSupply( 2682 context.TODO(), 2683 pubKey, 2684 rpc.CommitmentFinalized, 2685 ) 2686 if err != nil { 2687 panic(err) 2688 } 2689 spew.Dump(out) 2690 } 2691 ``` 2692 2693 #### [index](#contents) > [RPC](#rpc-methods) > GetTransaction 2694 2695 ```go 2696 package main 2697 2698 import ( 2699 "context" 2700 2701 "github.com/davecgh/go-spew/spew" 2702 "github.com/gagliardetto/solana-go" 2703 "github.com/gagliardetto/solana-go/rpc" 2704 ) 2705 2706 func main() { 2707 endpoint := rpc.TestNet_RPC 2708 client := rpc.New(endpoint) 2709 2710 txSig := solana.MustSignatureFromBase58("4bjVLV1g9SAfv7BSAdNnuSPRbSscADHFe4HegL6YVcuEBMY83edLEvtfjE4jfr6rwdLwKBQbaFiGgoLGtVicDzHq") 2711 { 2712 out, err := client.GetTransaction( 2713 context.TODO(), 2714 txSig, 2715 &rpc.GetTransactionOpts{ 2716 Encoding: solana.EncodingBase64, 2717 }, 2718 ) 2719 if err != nil { 2720 panic(err) 2721 } 2722 spew.Dump(out) 2723 spew.Dump(out.Transaction.GetBinary()) 2724 2725 decodedTx, err := solana.TransactionFromDecoder(bin.NewBinDecoder(out.Transaction.GetBinary())) 2726 if err != nil { 2727 panic(err) 2728 } 2729 spew.Dump(decodedTx) 2730 } 2731 { 2732 out, err := client.GetTransaction( 2733 context.TODO(), 2734 txSig, 2735 nil, 2736 ) 2737 if err != nil { 2738 panic(err) 2739 } 2740 spew.Dump(out) 2741 spew.Dump(out.Transaction.GetParsedTransaction()) 2742 } 2743 } 2744 ``` 2745 2746 #### [index](#contents) > [RPC](#rpc-methods) > GetTransactionCount 2747 2748 ```go 2749 package main 2750 2751 import ( 2752 "context" 2753 2754 "github.com/davecgh/go-spew/spew" 2755 "github.com/gagliardetto/solana-go/rpc" 2756 ) 2757 2758 func main() { 2759 endpoint := rpc.TestNet_RPC 2760 client := rpc.New(endpoint) 2761 2762 out, err := client.GetTransactionCount( 2763 context.TODO(), 2764 rpc.CommitmentFinalized, 2765 ) 2766 if err != nil { 2767 panic(err) 2768 } 2769 spew.Dump(out) 2770 } 2771 ``` 2772 2773 #### [index](#contents) > [RPC](#rpc-methods) > GetVersion 2774 2775 ```go 2776 package main 2777 2778 import ( 2779 "context" 2780 2781 "github.com/davecgh/go-spew/spew" 2782 "github.com/gagliardetto/solana-go/rpc" 2783 ) 2784 2785 func main() { 2786 endpoint := rpc.TestNet_RPC 2787 client := rpc.New(endpoint) 2788 2789 out, err := client.GetVersion( 2790 context.TODO(), 2791 ) 2792 if err != nil { 2793 panic(err) 2794 } 2795 spew.Dump(out) 2796 } 2797 ``` 2798 2799 #### [index](#contents) > [RPC](#rpc-methods) > GetVoteAccounts 2800 2801 ```go 2802 package main 2803 2804 import ( 2805 "context" 2806 2807 "github.com/davecgh/go-spew/spew" 2808 "github.com/gagliardetto/solana-go" 2809 "github.com/gagliardetto/solana-go/rpc" 2810 ) 2811 2812 func main() { 2813 endpoint := rpc.TestNet_RPC 2814 client := rpc.New(endpoint) 2815 2816 out, err := client.GetVoteAccounts( 2817 context.TODO(), 2818 &rpc.GetVoteAccountsOpts{ 2819 VotePubkey: solana.MustPublicKeyFromBase58("vot33MHDqT6nSwubGzqtc6m16ChcUywxV7tNULF19Vu"), 2820 }, 2821 ) 2822 if err != nil { 2823 panic(err) 2824 } 2825 spew.Dump(out) 2826 } 2827 ``` 2828 2829 #### [index](#contents) > [RPC](#rpc-methods) > IsBlockhashValid 2830 2831 ```go 2832 package main 2833 2834 import ( 2835 "context" 2836 "fmt" 2837 "github.com/davecgh/go-spew/spew" 2838 "github.com/gagliardetto/solana-go" 2839 "github.com/gagliardetto/solana-go/rpc" 2840 ) 2841 2842 func main() { 2843 endpoint := rpc.MainNetBeta_RPC 2844 client := rpc.New(endpoint) 2845 2846 blockHash := solana.MustHashFromBase58("J7rBdM6AecPDEZp8aPq5iPSNKVkU5Q76F3oAV4eW5wsW") 2847 out, err := client.IsBlockhashValid( 2848 context.TODO(), 2849 blockHash, 2850 rpc.CommitmentFinalized, 2851 ) 2852 if err != nil { 2853 panic(err) 2854 } 2855 spew.Dump(out) 2856 spew.Dump(out.Value) // true or false 2857 2858 fmt.Println("is blockhash valid:", out.Value) 2859 } 2860 ``` 2861 2862 #### [index](#contents) > [RPC](#rpc-methods) > MinimumLedgerSlot 2863 2864 ```go 2865 package main 2866 2867 import ( 2868 "context" 2869 2870 "github.com/davecgh/go-spew/spew" 2871 "github.com/gagliardetto/solana-go/rpc" 2872 ) 2873 2874 func main() { 2875 endpoint := rpc.TestNet_RPC 2876 client := rpc.New(endpoint) 2877 2878 out, err := client.MinimumLedgerSlot( 2879 context.TODO(), 2880 ) 2881 if err != nil { 2882 panic(err) 2883 } 2884 spew.Dump(out) 2885 } 2886 ``` 2887 2888 #### [index](#contents) > [RPC](#rpc-methods) > RequestAirdrop 2889 2890 ```go 2891 package main 2892 2893 import ( 2894 "context" 2895 2896 "github.com/davecgh/go-spew/spew" 2897 "github.com/gagliardetto/solana-go" 2898 "github.com/gagliardetto/solana-go/rpc" 2899 ) 2900 2901 func main() { 2902 endpoint := rpc.TestNet_RPC 2903 client := rpc.New(endpoint) 2904 2905 amount := solana.LAMPORTS_PER_SOL // 1 sol 2906 pubKey := solana.MustPublicKeyFromBase58("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") 2907 out, err := client.RequestAirdrop( 2908 context.TODO(), 2909 pubKey, 2910 amount, 2911 "", 2912 ) 2913 if err != nil { 2914 panic(err) 2915 } 2916 spew.Dump(out) 2917 } 2918 ``` 2919 2920 #### [index](#contents) > [RPC](#rpc-methods) > SendTransaction 2921 2922 ```go 2923 package main 2924 2925 func main() { 2926 2927 } 2928 ``` 2929 2930 #### [index](#contents) > [RPC](#rpc-methods) > SimulateTransaction 2931 2932 ```go 2933 package main 2934 2935 func main() { 2936 2937 } 2938 ``` 2939 2940 ### Websocket Subscriptions 2941 2942 #### [index](#contents) > [WS Subscriptions](#websocket-subscriptions) > AccountSubscribe 2943 2944 ```go 2945 package main 2946 2947 import ( 2948 "context" 2949 2950 "github.com/davecgh/go-spew/spew" 2951 "github.com/gagliardetto/solana-go" 2952 "github.com/gagliardetto/solana-go/rpc" 2953 "github.com/gagliardetto/solana-go/rpc/ws" 2954 ) 2955 2956 func main() { 2957 client, err := ws.Connect(context.Background(), rpc.MainNetBeta_WS) 2958 if err != nil { 2959 panic(err) 2960 } 2961 program := solana.MustPublicKeyFromBase58("9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin") // serum 2962 2963 { 2964 sub, err := client.AccountSubscribe( 2965 program, 2966 "", 2967 ) 2968 if err != nil { 2969 panic(err) 2970 } 2971 defer sub.Unsubscribe() 2972 2973 for { 2974 got, err := sub.Recv() 2975 if err != nil { 2976 panic(err) 2977 } 2978 spew.Dump(got) 2979 } 2980 } 2981 if false { 2982 sub, err := client.AccountSubscribeWithOpts( 2983 program, 2984 "", 2985 // You can specify the data encoding of the returned accounts: 2986 solana.EncodingBase64, 2987 ) 2988 if err != nil { 2989 panic(err) 2990 } 2991 defer sub.Unsubscribe() 2992 2993 for { 2994 got, err := sub.Recv() 2995 if err != nil { 2996 panic(err) 2997 } 2998 spew.Dump(got) 2999 } 3000 } 3001 } 3002 ``` 3003 3004 #### [index](#contents) > [WS Subscriptions](#websocket-subscriptions) > LogsSubscribe 3005 3006 ```go 3007 package main 3008 3009 import ( 3010 "context" 3011 3012 "github.com/davecgh/go-spew/spew" 3013 "github.com/gagliardetto/solana-go" 3014 "github.com/gagliardetto/solana-go/rpc" 3015 "github.com/gagliardetto/solana-go/rpc/ws" 3016 ) 3017 3018 func main() { 3019 client, err := ws.Connect(context.Background(), rpc.MainNetBeta_WS) 3020 if err != nil { 3021 panic(err) 3022 } 3023 program := solana.MustPublicKeyFromBase58("9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin") // serum 3024 3025 { 3026 // Subscribe to log events that mention the provided pubkey: 3027 sub, err := client.LogsSubscribeMentions( 3028 program, 3029 rpc.CommitmentRecent, 3030 ) 3031 if err != nil { 3032 panic(err) 3033 } 3034 defer sub.Unsubscribe() 3035 3036 for { 3037 got, err := sub.Recv() 3038 if err != nil { 3039 panic(err) 3040 } 3041 spew.Dump(got) 3042 } 3043 } 3044 if false { 3045 // Subscribe to all log events: 3046 sub, err := client.LogsSubscribe( 3047 ws.LogsSubscribeFilterAll, 3048 rpc.CommitmentRecent, 3049 ) 3050 if err != nil { 3051 panic(err) 3052 } 3053 defer sub.Unsubscribe() 3054 3055 for { 3056 got, err := sub.Recv() 3057 if err != nil { 3058 panic(err) 3059 } 3060 spew.Dump(got) 3061 } 3062 } 3063 } 3064 ``` 3065 3066 #### [index](#contents) > [WS Subscriptions](#websocket-subscriptions) > ProgramSubscribe 3067 3068 ```go 3069 package main 3070 3071 import ( 3072 "context" 3073 3074 "github.com/davecgh/go-spew/spew" 3075 "github.com/gagliardetto/solana-go" 3076 "github.com/gagliardetto/solana-go/rpc" 3077 "github.com/gagliardetto/solana-go/rpc/ws" 3078 ) 3079 3080 func main() { 3081 client, err := ws.Connect(context.Background(), rpc.MainNetBeta_WS) 3082 if err != nil { 3083 panic(err) 3084 } 3085 program := solana.MustPublicKeyFromBase58("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA") // token 3086 3087 sub, err := client.ProgramSubscribeWithOpts( 3088 program, 3089 rpc.CommitmentRecent, 3090 solana.EncodingBase64Zstd, 3091 nil, 3092 ) 3093 if err != nil { 3094 panic(err) 3095 } 3096 defer sub.Unsubscribe() 3097 3098 for { 3099 got, err := sub.Recv() 3100 if err != nil { 3101 panic(err) 3102 } 3103 spew.Dump(got) 3104 3105 decodedBinary := got.Value.Account.Data.GetBinary() 3106 if decodedBinary != nil { 3107 // spew.Dump(decodedBinary) 3108 } 3109 3110 // or if you requested solana.EncodingJSONParsed and it is supported: 3111 rawJSON := got.Value.Account.Data.GetRawJSON() 3112 if rawJSON != nil { 3113 // spew.Dump(rawJSON) 3114 } 3115 } 3116 } 3117 ``` 3118 3119 #### [index](#contents) > [WS Subscriptions](#websocket-subscriptions) > RootSubscribe 3120 3121 ```go 3122 package main 3123 3124 import ( 3125 "context" 3126 3127 "github.com/davecgh/go-spew/spew" 3128 "github.com/gagliardetto/solana-go/rpc" 3129 "github.com/gagliardetto/solana-go/rpc/ws" 3130 ) 3131 3132 func main() { 3133 client, err := ws.Connect(context.Background(), rpc.TestNet_WS) 3134 if err != nil { 3135 panic(err) 3136 } 3137 3138 sub, err := client.RootSubscribe() 3139 if err != nil { 3140 panic(err) 3141 } 3142 3143 for { 3144 got, err := sub.Recv() 3145 if err != nil { 3146 panic(err) 3147 } 3148 spew.Dump(got) 3149 } 3150 } 3151 ``` 3152 3153 #### [index](#contents) > [WS Subscriptions](#websocket-subscriptions) > SignatureSubscribe 3154 3155 ```go 3156 package main 3157 3158 import ( 3159 "context" 3160 3161 "github.com/davecgh/go-spew/spew" 3162 "github.com/gagliardetto/solana-go" 3163 "github.com/gagliardetto/solana-go/rpc" 3164 "github.com/gagliardetto/solana-go/rpc/ws" 3165 ) 3166 3167 func main() { 3168 client, err := ws.Connect(context.Background(), rpc.TestNet_WS) 3169 if err != nil { 3170 panic(err) 3171 } 3172 3173 txSig := solana.MustSignatureFromBase58("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") 3174 3175 sub, err := client.SignatureSubscribe( 3176 txSig, 3177 "", 3178 ) 3179 if err != nil { 3180 panic(err) 3181 } 3182 defer sub.Unsubscribe() 3183 3184 for { 3185 got, err := sub.Recv() 3186 if err != nil { 3187 panic(err) 3188 } 3189 spew.Dump(got) 3190 } 3191 } 3192 ``` 3193 3194 #### [index](#contents) > [WS Subscriptions](#websocket-subscriptions) > SlotSubscribe 3195 3196 ```go 3197 package main 3198 3199 import ( 3200 "context" 3201 3202 "github.com/davecgh/go-spew/spew" 3203 "github.com/gagliardetto/solana-go/rpc" 3204 "github.com/gagliardetto/solana-go/rpc/ws" 3205 ) 3206 3207 func main() { 3208 client, err := ws.Connect(context.Background(), rpc.TestNet_WS) 3209 if err != nil { 3210 panic(err) 3211 } 3212 3213 sub, err := client.SlotSubscribe() 3214 if err != nil { 3215 panic(err) 3216 } 3217 defer sub.Unsubscribe() 3218 3219 for { 3220 got, err := sub.Recv() 3221 if err != nil { 3222 panic(err) 3223 } 3224 spew.Dump(got) 3225 } 3226 } 3227 ``` 3228 3229 #### [index](#contents) > [WS Subscriptions](#websocket-subscriptions) > VoteSubscribe 3230 3231 ```go 3232 package main 3233 3234 import ( 3235 "context" 3236 3237 "github.com/davecgh/go-spew/spew" 3238 "github.com/gagliardetto/solana-go/rpc" 3239 "github.com/gagliardetto/solana-go/rpc/ws" 3240 ) 3241 3242 func main() { 3243 client, err := ws.Connect(context.Background(), rpc.MainNetBeta_WS) 3244 if err != nil { 3245 panic(err) 3246 } 3247 3248 // NOTE: this subscription must be enabled by the node you're connecting to. 3249 // This subscription is disabled by default. 3250 sub, err := client.VoteSubscribe() 3251 if err != nil { 3252 panic(err) 3253 } 3254 defer sub.Unsubscribe() 3255 3256 for { 3257 got, err := sub.Recv() 3258 if err != nil { 3259 panic(err) 3260 } 3261 spew.Dump(got) 3262 } 3263 } 3264 ``` 3265 3266 ## Contributing 3267 3268 We encourage everyone to contribute, submit issues, PRs, discuss. Every kind of help is welcome. 3269 3270 ## License 3271 3272 [Apache 2.0](LICENSE) 3273 3274 ## Credits 3275 3276 - Gopher logo was originally created by Takuya Ueda (https://twitter.com/tenntenn). Licensed under the Creative Commons 3.0 Attributions license.