Skip to content

Commit

Permalink
add more rpc support (#8)
Browse files Browse the repository at this point in the history
* feat(rpc): add Accounts config support to SimulateTransaction

* feat(rpc): add new rpc getSignaturesForAddress

* feat(rpc): add new rpc getBlocksWithLimit

* feat(rpc): add new rpc getTransaction

* feat(rpc): add new rpc getBlocks

* feat(rpc): add new rpc getBlock

* feat(rpc): update get epoch info response type

* feat(rpc): add new rpc getBlockHeight

* feat(rpc): add new rpc getFirstAvailableBlock

* feat(rpc): add new rpc getGenesisHash

* feat(rpc): add new rpc get identity

* feat(rpc): add brief desc to rpc functions
  • Loading branch information
yihau authored Jul 10, 2021
1 parent 0cbce4a commit c9f2599
Show file tree
Hide file tree
Showing 28 changed files with 298 additions and 10 deletions.
1 change: 1 addition & 0 deletions client/get_account_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type GetAccountInfoResponse struct {
Data interface{} `json:"data"`
}

// GetAccountInfo returns all information associated with the account of provided Pubkey
func (s *Client) GetAccountInfo(ctx context.Context, account string, cfg GetAccountInfoConfig) (GetAccountInfoResponse, error) {
res := struct {
GeneralResponse
Expand Down
1 change: 1 addition & 0 deletions client/get_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
)

// GetBalance returns the balance of the account of provided Pubkey
func (s *Client) GetBalance(ctx context.Context, base58Addr string) (uint64, error) {
res := struct {
GeneralResponse
Expand Down
41 changes: 41 additions & 0 deletions client/get_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package client

import "context"

type GetBlockConfig struct {
// TODO custom
// Encoding string `json:"encoding"` // default: "json", either "json", "jsonParsed", "base58" (slow), "base64"
// TransactionDetails string `json:"transactionDetails"` // default: "full", either "full", "signatures", "none"
Commitment Commitment `json:"commitment,omitempty"` // "processed" is not supported. If parameter not provided, the default is "finalized".
}

type GetBlockResponse struct {
Blockhash string `json:"blockhash"`
PreviousBlockhash string `json:"previousBlockhash"`
ParentSLot uint64 `json:"parentSlot"`
BlockTime int64 `json:"blockTime"`
Transactions []struct {
Meta TransactionMeta `json:"meta"`
Transaction Transaction `json:"transaction"`
} `json:"transactions"`
Rewards []struct {
Pubkey string `json:"pubkey"`
Lamports int64 `json:"lamports"`
PostBalance uint64 `json:"postBalance"`
RewardType string `json:"rewardType"` // type of reward: "fee", "rent", "voting", "staking"
} `json:"rewards"`
}

// NEW: This method is only available in solana-core v1.7 or newer. Please use getConfirmedBlock for solana-core v1.6
// GetBlock returns identity and transaction information about a confirmed block in the ledger
func (s *Client) GetBlock(ctx context.Context, slot uint64, cfg GetBlockConfig) (GetBlockResponse, error) {
res := struct {
GeneralResponse
Result GetBlockResponse `json:"result"`
}{}
err := s.request(ctx, "getBlock", []interface{}{slot, cfg}, &res)
if err != nil {
return GetBlockResponse{}, err
}
return res.Result, nil
}
1 change: 1 addition & 0 deletions client/get_block_commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type GetBlockCommitmentResponse struct {
TotalStake uint64 `json:"totalStake"`
}

// GetBlockCommitment returns commitment for particular block
func (s *Client) GetBlockCommitment(ctx context.Context, slot uint64) (GetBlockCommitmentResponse, error) {
res := struct {
GeneralResponse
Expand Down
26 changes: 26 additions & 0 deletions client/get_block_height.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package client

import (
"context"
"errors"
)

type GetBlockHeightConfig struct {
Commitment Commitment `json:"commitment,omitempty"`
}

// GetBlockHeight returns the current block height of the node
func (s *Client) GetBlockHeight(ctx context.Context, cfg GetBlockHeightConfig) (uint64, error) {
res := struct {
GeneralResponse
Result uint64 `json:"result"`
}{}
err := s.request(ctx, "getBlockHeight", []interface{}{cfg}, &res)
if err != nil {
return 0, err
}
if res.Error != (ErrorResponse{}) {
return 0, errors.New(res.Error.Message)
}
return res.Result, nil
}
1 change: 1 addition & 0 deletions client/get_block_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
)

// GetBlockTime returns the estimated production time of a block.
func (s *Client) GetBlockTime(ctx context.Context, slot uint64) (int64, error) {
res := struct {
GeneralResponse
Expand Down
22 changes: 22 additions & 0 deletions client/get_blocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package client

import "context"

type GetBlocksConfig struct {
Commitment Commitment `json:"commitment,omitempty"`
}

// NEW: This method is only available in solana-core v1.7 or newer. Please use getConfirmedBlocks for solana-core v1.6
// GetBlocks returns a list of confirmed blocks between two slots
// Max range allowed is 500,000 slots
func (s *Client) GetBlocks(ctx context.Context, startSlot uint64, endSlot uint64, cfg GetBlocksConfig) ([]uint64, error) {
res := struct {
GeneralResponse
Result []uint64 `json:"result"`
}{}
err := s.request(ctx, "getBlocks", []interface{}{startSlot, endSlot, cfg}, &res)
if err != nil {
return nil, err
}
return res.Result, nil
}
21 changes: 21 additions & 0 deletions client/get_blocks_with_limit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package client

import "context"

type GetBlocksWithLimitConfig struct {
Commitment Commitment `json:"commitment,omitempty"`
}

// NEW: This method is only available in solana-core v1.7 or newer. Please use getConfirmedBlocksWithLimit for solana-core v1.6
// GetBlocksWithLimit returns a list of confirmed blocks starting at the given slot
func (s *Client) GetBlocksWithLimit(ctx context.Context, startSlot uint64, limit uint64, cfg GetBlocksWithLimitConfig) ([]uint64, error) {
res := struct {
GeneralResponse
Result []uint64 `json:"result"`
}{}
err := s.request(ctx, "getBlocksWithLimit", []interface{}{startSlot, limit, cfg}, &res)
if err != nil {
return nil, err
}
return res.Result, nil
}
1 change: 1 addition & 0 deletions client/get_cluster_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type GetClusterNodesResponse struct {
Version *string `json:"version"`
}

// GetClusterNodes returns information about all the nodes participating in the cluster
func (s *Client) GetClusterNodes(ctx context.Context) ([]GetClusterNodesResponse, error) {
res := struct {
GeneralResponse
Expand Down
2 changes: 2 additions & 0 deletions client/get_confirmed_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type GetConfirmBlockResponse struct {
} `json:"rewards"`
}

// DEPRECATED: Please use getBlock instead This method is expected to be removed in solana-core v1.8
// GetConfirmedBlock returns identity and transaction information about a confirmed block in the ledger
func (s *Client) GetConfirmedBlock(ctx context.Context, slot uint64) (GetConfirmBlockResponse, error) {
res := struct {
GeneralResponse
Expand Down
2 changes: 2 additions & 0 deletions client/get_confirmed_block_with_limit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package client

import "context"

// DEPRECATED: Please use getBlocksWithLimit instead This method is expected to be removed in solana-core v1.8
// GetConfirmedBlocksWithLimit returns a list of confirmed blocks starting at the given slot
func (s *Client) GetConfirmedBlocksWithLimit(ctx context.Context, startSlot uint64, limit uint64) ([]uint64, error) {
res := struct {
GeneralResponse
Expand Down
2 changes: 2 additions & 0 deletions client/get_confirmed_blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package client

import "context"

// DEPRECATED: Please use getBlocks instead This method is expected to be removed in solana-core v1.8
// GetConfirmedBlocks returns a list of confirmed blocks between two slots
func (s *Client) GetConfirmedBlocks(ctx context.Context, startSlot uint64, endSlot uint64) ([]uint64, error) {
res := struct {
GeneralResponse
Expand Down
1 change: 1 addition & 0 deletions client/get_confirmed_signatures_for_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type GetConfirmedSignaturesForAddressConfig struct {
Commitment Commitment `json:"commitment,omitempty"`
}

// DEPRECATED: Please use getSignaturesForAddress instead This method is expected to be removed in solana-core v1.8
// GetConfirmedSignaturesForAddress returns confirmed signatures for transactions involving an address
// backwards in time from the provided signature or most recent confirmed block
func (s *Client) GetConfirmedSignaturesForAddress(ctx context.Context, base58Addr string, config GetConfirmedSignaturesForAddressConfig) ([]GetConfirmedSignaturesForAddress, error) {
Expand Down
2 changes: 2 additions & 0 deletions client/get_confirmed_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type GetConfirmedTransactionResponse struct {
Transaction Transaction `json:"transaction"`
}

// DEPRECATED: Please use getTransaction instead This method is expected to be removed in solana-core v1.8
// GetConfirmedTransaction returns transaction details for a confirmed transaction
func (s *Client) GetConfirmedTransaction(ctx context.Context, txhash string) (GetConfirmedTransactionResponse, error) {
res := struct {
GeneralResponse
Expand Down
11 changes: 6 additions & 5 deletions client/get_epoch_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package client
import "context"

type GetEpochInfoResponse struct {
AbsoluteSlot int `json:"absoluteSlot"`
BlockHeight int `json:"blockHeight"`
Epoch int `json:"epoch"`
SlotIndex int `json:"slotIndex"`
SlotsInEpoch int `json:"slotsInEpoch"`
AbsoluteSlot uint64 `json:"absoluteSlot"`
BlockHeight uint64 `json:"blockHeight"`
Epoch uint64 `json:"epoch"`
SlotIndex uint64 `json:"slotIndex"`
SlotsInEpoch uint64 `json:"slotsInEpoch"`
}

// GetEpochInfo returns information about the current epoch
func (s *Client) GetEpochInfo(ctx context.Context, commitment Commitment) (GetEpochInfoResponse, error) {
res := struct {
GeneralResponse
Expand Down
22 changes: 22 additions & 0 deletions client/get_first_available_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package client

import (
"context"
"errors"
)

// GetFirstAvailableBlock returns the slot of the lowest confirmed block that has not been purged from the ledger
func (s *Client) GetFirstAvailableBlock(ctx context.Context) (uint64, error) {
res := struct {
GeneralResponse
Result uint64 `json:"result"`
}{}
err := s.request(ctx, "getFirstAvailableBlock", []interface{}{}, &res)
if err != nil {
return 0, err
}
if res.Error != (ErrorResponse{}) {
return 0, errors.New(res.Error.Message)
}
return res.Result, nil
}
22 changes: 22 additions & 0 deletions client/get_genesis_hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package client

import (
"context"
"errors"
)

// GetGenesisHash returns the genesis hash
func (s *Client) GetGenesisHash(ctx context.Context) (string, error) {
res := struct {
GeneralResponse
Result string `json:"result"`
}{}
err := s.request(ctx, "getGenesisHash", []interface{}{}, &res)
if err != nil {
return "", err
}
if res.Error != (ErrorResponse{}) {
return "", errors.New(res.Error.Message)
}
return res.Result, nil
}
24 changes: 24 additions & 0 deletions client/get_identity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package client

import (
"context"
"errors"
)

// GetIdentity returns the identity pubkey for the current node
func (s *Client) GetIdentity(ctx context.Context) (string, error) {
res := struct {
GeneralResponse
Result struct {
Identity string `json:"identity"`
} `json:"result"`
}{}
err := s.request(ctx, "getIdentity", []interface{}{}, &res)
if err != nil {
return "", err
}
if res.Error != (ErrorResponse{}) {
return "", errors.New(res.Error.Message)
}
return res.Result.Identity, nil
}
1 change: 1 addition & 0 deletions client/get_min_balance_for_rent_free.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
)

// GetMinimumBalanceForRentExemption returns minimum balance required to make account rent exempt.
func (s *Client) GetMinimumBalanceForRentExemption(ctx context.Context, accountDataLen uint64) (uint64, error) {
res := struct {
GeneralResponse
Expand Down
2 changes: 2 additions & 0 deletions client/get_recent_block_hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type GetRecentBlockHashResponse struct {
} `json:"feeCalculator"`
}

// getRecentBlockhash returns a recent block hash from the ledger, and a fee schedule that can be used to compute
// the cost of submitting a transaction using it.
func (s *Client) GetRecentBlockhash(ctx context.Context) (GetRecentBlockHashResponse, error) {
res := struct {
GeneralResponse
Expand Down
39 changes: 39 additions & 0 deletions client/get_signatures_for_address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package client

import (
"context"
"errors"
)

type GetSignaturesForAddress struct {
Signature string `json:"signature"`
Slot uint64 `json:"slot"`
BlockTime *int64 `json:"blockTime"`
Err interface{} `json:"err"`
Memo *string `json:"memo"`
}

type GetSignaturesForAddressConfig struct {
Limit int `json:"limit,omitempty"` // between 1 and 1000, default: 1000
Before string `json:"before,omitempty"`
Until string `json:"until,omitempty"`
Commitment Commitment `json:"commitment,omitempty"` // "processed" is not supported, default is "finalized"
}

// NEW: This method is only available in solana-core v1.7 or newer. Please use "getConfirmedSignaturesForAddress2" for solana-core v1.6
// GetSignaturesForAddress Returns confirmed signatures for transactions involving an address backwards
// in time from the provided signature or most recent confirmed block
func (s *Client) GetSignaturesForAddress(ctx context.Context, base58Addr string, config GetConfirmedSignaturesForAddressConfig) ([]GetConfirmedSignaturesForAddress, error) {
res := struct {
GeneralResponse
Result []GetConfirmedSignaturesForAddress `json:"result"`
}{}
err := s.request(ctx, "getSignaturesForAddress", []interface{}{base58Addr, config}, &res)
if err != nil {
return []GetConfirmedSignaturesForAddress{}, err
}
if res.Error != (ErrorResponse{}) {
return []GetConfirmedSignaturesForAddress{}, errors.New(res.Error.Message)
}
return res.Result, nil
}
1 change: 1 addition & 0 deletions client/get_stake_activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type GetStakeActivationResponse struct {
Inactive uint64 `json:"inactive"`
}

// GetStakeActivation returns epoch activation information for a stake account
func (s *Client) GetStakeActivation(ctx context.Context, address string, cfg GetStakeActivationConfig) (GetStakeActivationResponse, error) {
res := struct {
GeneralResponse
Expand Down
1 change: 1 addition & 0 deletions client/get_token_account_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type GetTokenAccountBalance struct {
UIAmountString string `json:"uiAmountString"`
}

// GetTokenAccountBalance returns the token balance of an SPL Token account.
func (s *Client) GetTokenAccountBalance(ctx context.Context, base58Addr string, commitment Commitment) (GetTokenAccountBalance, error) {
res := struct {
GeneralResponse
Expand Down
29 changes: 29 additions & 0 deletions client/get_transaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package client

import "context"

type GetTransactionWithLimitConfig struct {
// TODO custom encoding
// Encoding string `json:"encoding"` // either "json", "jsonParsed", "base58" (slow), "base64", default: json
Commitment Commitment `json:"commitment,omitempty"` // "processed" is not supported. If parameter not provided, the default is "finalized".
}

type GetTransaction struct {
Slot uint64 `json:"slot"`
Meta TransactionMeta `json:"meta"`
Transaction Transaction `json:"transaction"`
}

// NEW: This method is only available in solana-core v1.7 or newer. Please use getConfirmedTransaction for solana-core v1.6
// GetConfirmedTransaction returns transaction details for a confirmed transaction
func (s *Client) GetTransaction(ctx context.Context, txhash string, cfg GetTransactionWithLimitConfig) (GetConfirmedTransactionResponse, error) {
res := struct {
GeneralResponse
Result GetConfirmedTransactionResponse `json:"result"`
}{}
err := s.request(ctx, "getTransaction", []interface{}{txhash, cfg}, &res)
if err != nil {
return GetConfirmedTransactionResponse{}, err
}
return res.Result, nil
}
1 change: 1 addition & 0 deletions client/get_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type GetVersionResponse struct {
FeatureSet uint64 `json:"feature-set"`
}

// GetVersion returns the current solana versions running on the node
func (s *Client) GetVersion(ctx context.Context) (GetVersionResponse, error) {
res := struct {
GeneralResponse
Expand Down
Loading

0 comments on commit c9f2599

Please sign in to comment.