github.com/gagliardetto/solana-go@v1.11.0/rpc/getMultipleAccounts.go (about)

     1  // Copyright 2021 github.com/gagliardetto
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package rpc
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  
    21  	"github.com/gagliardetto/solana-go"
    22  )
    23  
    24  type GetMultipleAccountsResult struct {
    25  	RPCContext
    26  	Value []*Account `json:"value"`
    27  }
    28  
    29  // GetMultipleAccounts returns the account information for a list of Pubkeys.
    30  func (cl *Client) GetMultipleAccounts(
    31  	ctx context.Context,
    32  	accounts ...solana.PublicKey, // An array of Pubkeys to query
    33  ) (out *GetMultipleAccountsResult, err error) {
    34  	return cl.GetMultipleAccountsWithOpts(
    35  		ctx,
    36  		accounts,
    37  		nil,
    38  	)
    39  }
    40  
    41  type GetMultipleAccountsOpts GetAccountInfoOpts
    42  
    43  // GetMultipleAccountsWithOpts returns the account information for a list of Pubkeys.
    44  func (cl *Client) GetMultipleAccountsWithOpts(
    45  	ctx context.Context,
    46  	accounts []solana.PublicKey,
    47  	opts *GetMultipleAccountsOpts,
    48  ) (out *GetMultipleAccountsResult, err error) {
    49  	params := []interface{}{accounts}
    50  
    51  	if opts != nil {
    52  		obj := M{}
    53  		if opts.Encoding != "" {
    54  			obj["encoding"] = opts.Encoding
    55  		}
    56  		if opts.Commitment != "" {
    57  			obj["commitment"] = opts.Commitment
    58  		}
    59  		if opts.DataSlice != nil {
    60  			obj["dataSlice"] = M{
    61  				"offset": opts.DataSlice.Offset,
    62  				"length": opts.DataSlice.Length,
    63  			}
    64  			if opts.Encoding == solana.EncodingJSONParsed {
    65  				return nil, errors.New("cannot use dataSlice with EncodingJSONParsed")
    66  			}
    67  		}
    68  		if len(obj) > 0 {
    69  			params = append(params, obj)
    70  		}
    71  	}
    72  
    73  	err = cl.rpcClient.CallForInto(ctx, &out, "getMultipleAccounts", params)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  	if out.Value == nil {
    78  		return nil, ErrNotFound
    79  	}
    80  	return
    81  }