Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions rpc/examples/getBlock/getBlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,48 @@ package main

import (
"context"
"fmt"

"github.com/davecgh/go-spew/spew"
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc"
)

func main() {
endpoint := rpc.TestNet_RPC
client := rpc.New(endpoint)
ctx := context.Background()
client := rpc.New(rpc.MainNetBeta_RPC)

slot, err := client.GetSlot(context.TODO(), rpc.CommitmentFinalized)
slot, err := client.GetSlot(ctx, rpc.CommitmentFinalized)
if err != nil {
panic(err)
}

// Mainnet blocks contain v0 (versioned) transactions; GetBlock
// requires MaxSupportedTransactionVersion or it errors out.
maxVersion := uint64(0)

{
out, err := client.GetBlock(context.TODO(), slot)
out, err := client.GetBlockWithOpts(ctx, slot, &rpc.GetBlockOpts{
MaxSupportedTransactionVersion: &maxVersion,
})
if err != nil {
panic(err)
}
// spew.Dump(out) // NOTE: This generates a lot of output.
spew.Dump(len(out.Transactions))
// Full block is large; just show the tx count.
fmt.Println("transactions in block:", len(out.Transactions))
}

{
includeRewards := false
out, err := client.GetBlockWithOpts(
context.TODO(),
ctx,
slot,
// You can specify more options here:
&rpc.GetBlockOpts{
Encoding: solana.EncodingBase64,
Commitment: rpc.CommitmentFinalized,
// Get only signatures:
TransactionDetails: rpc.TransactionDetailsSignatures,
// Exclude rewards:
Rewards: &includeRewards,
Encoding: solana.EncodingBase64,
Commitment: rpc.CommitmentFinalized,
TransactionDetails: rpc.TransactionDetailsSignatures,
Rewards: &includeRewards,
MaxSupportedTransactionVersion: &maxVersion,
},
)
if err != nil {
Expand Down
37 changes: 13 additions & 24 deletions rpc/examples/getBlockProduction/getBlockProduction.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,19 @@ import (
)

func main() {
endpoint := rpc.TestNet_RPC
client := rpc.New(endpoint)
ctx := context.Background()
// Testnet has few validators; mainnet-beta has the real schedule.
client := rpc.New(rpc.MainNetBeta_RPC)

{
out, err := client.GetBlockProduction(context.TODO())
if err != nil {
panic(err)
}
spew.Dump(out)
}
{
out, err := client.GetBlockProductionWithOpts(
context.TODO(),
&rpc.GetBlockProductionOpts{
Commitment: rpc.CommitmentFinalized,
// Range: &rpc.SlotRangeRequest{
// FirstSlot: XXXXXX,
// Identity: solana.MustPublicKeyFromBase58("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"),
// },
},
)
if err != nil {
panic(err)
}
spew.Dump(out)
out, err := client.GetBlockProductionWithOpts(
ctx,
&rpc.GetBlockProductionOpts{
Commitment: rpc.CommitmentFinalized,
// Range: &rpc.SlotRangeRequest{ FirstSlot: ..., Identity: ... },
},
)
if err != nil {
panic(err)
}
spew.Dump(out)
}
41 changes: 35 additions & 6 deletions rpc/examples/getFeeForMessage/getFeeForMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,51 @@ package main

import (
"context"
"fmt"

"github.com/davecgh/go-spew/spew"
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/programs/system"
"github.com/gagliardetto/solana-go/rpc"
)

// GetFeeForMessage returns the fee for a base64-encoded Solana Message
// (not a full Transaction). Here we construct a realistic Transfer
// message to show how the base64 argument is produced.
func main() {
endpoint := rpc.TestNet_RPC
client := rpc.New(endpoint)
ctx := context.Background()
client := rpc.New(rpc.MainNetBeta_RPC)

example, err := client.GetFeeForMessage(
context.Background(),
"AQABAgIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEBAQAA",
from := solana.NewWallet().PublicKey()
to := solana.NewWallet().PublicKey()

recent, err := client.GetLatestBlockhash(ctx, rpc.CommitmentFinalized)
if err != nil {
panic(fmt.Errorf("get blockhash: %w", err))
}

tx, err := solana.NewTransaction(
[]solana.Instruction{
system.NewTransferInstruction(
solana.LAMPORTS_PER_SOL/1000, // 0.001 SOL
from,
to,
).Build(),
},
recent.Value.Blockhash,
solana.TransactionPayer(from),
)
if err != nil {
panic(fmt.Errorf("build tx: %w", err))
}

out, err := client.GetFeeForMessage(
ctx,
tx.Message.ToBase64(),
rpc.CommitmentProcessed,
)
if err != nil {
panic(err)
}
spew.Dump(example)
spew.Dump(out)
}
3 changes: 1 addition & 2 deletions rpc/examples/getInflationGovernor/getInflationGovernor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import (
)

func main() {
endpoint := rpc.TestNet_RPC
client := rpc.New(endpoint)
client := rpc.New(rpc.MainNetBeta_RPC)

out, err := client.GetInflationGovernor(
context.TODO(),
Expand Down
3 changes: 1 addition & 2 deletions rpc/examples/getInflationRate/getInflationRate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import (
)

func main() {
endpoint := rpc.TestNet_RPC
client := rpc.New(endpoint)
client := rpc.New(rpc.MainNetBeta_RPC)

out, err := client.GetInflationRate(
context.TODO(),
Expand Down
21 changes: 14 additions & 7 deletions rpc/examples/getInflationReward/getInflationReward.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,23 @@ import (
)

func main() {
endpoint := rpc.TestNet_RPC
client := rpc.New(endpoint)
ctx := context.Background()
client := rpc.New(rpc.MainNetBeta_RPC)

pubKey := solana.MustPublicKeyFromBase58("6dmNQ5jwLeLk5REvio1JcMshcbvkYMwy26sJ8pbkvStu")
// Fetch any currently-voting validator so the example keeps working
// across epochs without a hardcoded pubkey.
voteAccounts, err := client.GetVoteAccounts(ctx, nil)
if err != nil {
panic(err)
}
if len(voteAccounts.Current) == 0 {
panic("no current vote accounts")
}
votePubkey := voteAccounts.Current[0].VotePubkey

out, err := client.GetInflationReward(
context.TODO(),
[]solana.PublicKey{
pubKey,
},
ctx,
[]solana.PublicKey{votePubkey},
&rpc.GetInflationRewardOpts{
Commitment: rpc.CommitmentFinalized,
},
Expand Down
3 changes: 1 addition & 2 deletions rpc/examples/getLargestAccounts/getLargestAccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import (
)

func main() {
endpoint := rpc.TestNet_RPC
client := rpc.New(endpoint)
client := rpc.New(rpc.MainNetBeta_RPC)

out, err := client.GetLargestAccounts(
context.TODO(),
Expand Down
23 changes: 16 additions & 7 deletions rpc/examples/getLeaderSchedule/getLeaderSchedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,29 @@ package main

import (
"context"
"fmt"

"github.com/davecgh/go-spew/spew"
"github.com/gagliardetto/solana-go/rpc"
)

func main() {
endpoint := rpc.TestNet_RPC
client := rpc.New(endpoint)
ctx := context.Background()
client := rpc.New(rpc.MainNetBeta_RPC)

out, err := client.GetLeaderSchedule(
context.TODO(),
)
out, err := client.GetLeaderSchedule(ctx)
if err != nil {
panic(err)
}
spew.Dump(out) // NOTE: this creates a lot of output

// The response maps every validator in the current epoch to its
// scheduled slots; on mainnet this is huge. Only print a summary.
fmt.Println("validators in current epoch:", len(out))
count := 0
for validator, slots := range out {
if count >= 5 {
break
}
fmt.Printf(" %s -> %d slots\n", validator, len(slots))
count++
}
}
36 changes: 28 additions & 8 deletions rpc/examples/getProgramAccounts/getProgramAccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,43 @@ package main

import (
"context"
"fmt"

"github.com/davecgh/go-spew/spew"
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc"
)

// getProgramAccounts returns every account owned by a program. Public
// mainnet RPCs reject it without a restrictive filter because the
// response would be too large. This example filters Token-2022 accounts
// down to just mint accounts (82-byte data) and prints the first few.
func main() {
endpoint := rpc.TestNet_RPC
client := rpc.New(endpoint)
ctx := context.Background()
client := rpc.New(rpc.MainNetBeta_RPC)

out, err := client.GetProgramAccounts(
context.TODO(),
solana.MustPublicKeyFromBase58("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),
out, err := client.GetProgramAccountsWithOpts(
ctx,
solana.Token2022ProgramID,
&rpc.GetProgramAccountsOpts{
Commitment: rpc.CommitmentFinalized,
Filters: []rpc.RPCFilter{
{DataSize: 82}, // SPL Token mint account size
},
// Only fetch the first byte of data so the response stays small.
DataSlice: &rpc.DataSlice{Offset: ptrU64(0), Length: ptrU64(1)},
},
)
if err != nil {
panic(err)
}
spew.Dump(len(out))
spew.Dump(out) // NOTE: this can generate a lot of output

fmt.Println("Token-2022 mints found:", len(out))
for i, acc := range out {
if i >= 5 {
break
}
fmt.Printf(" %d: %s\n", i, acc.Pubkey)
}
}

func ptrU64(v uint64) *uint64 { return &v }
34 changes: 27 additions & 7 deletions rpc/examples/getSignatureStatuses/getSignatureStatuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,42 @@ package main

import (
"context"
"fmt"

"github.com/davecgh/go-spew/spew"
"github.com/gagliardetto/solana-go"
"github.com/gagliardetto/solana-go/rpc"
)

func main() {
endpoint := rpc.TestNet_RPC
client := rpc.New(endpoint)
ctx := context.Background()
client := rpc.New(rpc.MainNetBeta_RPC)

// Fetch fresh signatures so the example keeps working as the
// cluster advances.
limit := 2
sigs, err := client.GetSignaturesForAddressWithOpts(
ctx,
solana.TokenProgramID,
&rpc.GetSignaturesForAddressOpts{Limit: &limit},
)
if err != nil {
panic(fmt.Errorf("getSignaturesForAddress: %w", err))
}
if len(sigs) == 0 {
panic("no recent signatures found")
}

toLookup := make([]solana.Signature, 0, len(sigs))
for _, s := range sigs {
toLookup = append(toLookup, s.Signature)
}
fmt.Println("querying statuses for", len(toLookup), "signatures")

out, err := client.GetSignatureStatuses(
context.TODO(),
true,
// All the transactions you want the get the status for:
solana.MustSignatureFromBase58("2CwH8SqVZWFa1EvsH7vJXGFors1NdCuWJ7Z85F8YqjCLQ2RuSHQyeGKkfo1Tj9HitSTeLoMWnxpjxF2WsCH8nGWh"),
solana.MustSignatureFromBase58("5YJHZPeHZuZjhunBc1CCB1NDRNf2tTJNpdb3azGsR7PfyEncCDhr95wG8EWrvjNXBc4wCKixkheSbCxoC2NCG3X7"),
ctx,
true, // searchTransactionHistory
toLookup...,
)
if err != nil {
panic(err)
Expand Down
Loading
Loading