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

     1  // Copyright 2021 github.com/gagliardetto
     2  // This file has been modified by github.com/gagliardetto
     3  //
     4  // Copyright 2020 dfuse Platform Inc.
     5  //
     6  // Licensed under the Apache License, Version 2.0 (the "License");
     7  // you may not use this file except in compliance with the License.
     8  // You may obtain a copy of the License at
     9  //
    10  //	http://www.apache.org/licenses/LICENSE-2.0
    11  //
    12  // Unless required by applicable law or agreed to in writing, software
    13  // distributed under the License is distributed on an "AS IS" BASIS,
    14  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    15  // See the License for the specific language governing permissions and
    16  // limitations under the License.
    17  package rpc
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  
    23  	bin "github.com/gagliardetto/binary"
    24  	"github.com/gagliardetto/solana-go"
    25  )
    26  
    27  // GetAccountInfo returns all information associated with the account of provided publicKey.
    28  func (cl *Client) GetAccountInfo(ctx context.Context, account solana.PublicKey) (out *GetAccountInfoResult, err error) {
    29  	return cl.GetAccountInfoWithOpts(
    30  		ctx,
    31  		account,
    32  		&GetAccountInfoOpts{
    33  			Commitment: "",
    34  			DataSlice:  nil,
    35  		},
    36  	)
    37  }
    38  
    39  // GetAccountDataInto decodes the binary data and populates
    40  // the provided `inVar` parameter with all data associated with the account of provided publicKey.
    41  func (cl *Client) GetAccountDataInto(ctx context.Context, account solana.PublicKey, inVar interface{}) (err error) {
    42  	resp, err := cl.GetAccountInfo(ctx, account)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	return bin.NewBinDecoder(resp.Value.Data.GetBinary()).Decode(inVar)
    47  }
    48  
    49  // GetAccountDataBorshInto decodes the borsh binary data and populates
    50  // the provided `inVar` parameter with all data associated with the account of provided publicKey.
    51  func (cl *Client) GetAccountDataBorshInto(ctx context.Context, account solana.PublicKey, inVar interface{}) (err error) {
    52  	resp, err := cl.GetAccountInfo(ctx, account)
    53  	if err != nil {
    54  		return err
    55  	}
    56  	return bin.NewBorshDecoder(resp.Value.Data.GetBinary()).Decode(inVar)
    57  }
    58  
    59  type GetAccountInfoOpts struct {
    60  	// Encoding for Account data.
    61  	// Either "base58" (slow), "base64", "base64+zstd", or "jsonParsed".
    62  	// - "base58" is limited to Account data of less than 129 bytes.
    63  	// - "base64" will return base64 encoded data for Account data of any size.
    64  	// - "base64+zstd" compresses the Account data using Zstandard and base64-encodes the result.
    65  	// - "jsonParsed" encoding attempts to use program-specific state parsers to return more
    66  	// 	 human-readable and explicit account state data. If "jsonParsed" is requested but a parser
    67  	//   cannot be found, the field falls back to "base64" encoding,
    68  	//   detectable when the data field is type <string>.
    69  	//
    70  	// This parameter is optional.
    71  	Encoding solana.EncodingType
    72  
    73  	// Commitment requirement.
    74  	//
    75  	// This parameter is optional.
    76  	Commitment CommitmentType
    77  
    78  	// dataSlice parameters for limiting returned account data:
    79  	// Limits the returned account data using the provided offset and length fields;
    80  	// only available for "base58", "base64" or "base64+zstd" encodings.
    81  	//
    82  	// This parameter is optional.
    83  	DataSlice *DataSlice
    84  
    85  	// The minimum slot that the request can be evaluated at.
    86  	// This parameter is optional.
    87  	MinContextSlot *uint64
    88  }
    89  
    90  // GetAccountInfoWithOpts returns all information associated with the account of provided publicKey.
    91  // You can specify the encoding of the returned data with the encoding parameter.
    92  // You can limit the returned account data with the offset and length parameters.
    93  func (cl *Client) GetAccountInfoWithOpts(
    94  	ctx context.Context,
    95  	account solana.PublicKey,
    96  	opts *GetAccountInfoOpts,
    97  ) (*GetAccountInfoResult, error) {
    98  	out, err := cl.getAccountInfoWithOpts(ctx, account, opts)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  	if out.Value == nil {
   103  		return nil, ErrNotFound
   104  	}
   105  	return out, nil
   106  }
   107  
   108  func (cl *Client) getAccountInfoWithOpts(
   109  	ctx context.Context,
   110  	account solana.PublicKey,
   111  	opts *GetAccountInfoOpts,
   112  ) (out *GetAccountInfoResult, err error) {
   113  
   114  	obj := M{
   115  		// default encoding:
   116  		"encoding": solana.EncodingBase64,
   117  	}
   118  
   119  	if opts != nil {
   120  		if opts.Encoding != "" {
   121  			obj["encoding"] = opts.Encoding
   122  		}
   123  		if opts.Commitment != "" {
   124  			obj["commitment"] = opts.Commitment
   125  		}
   126  		if opts.DataSlice != nil {
   127  			obj["dataSlice"] = M{
   128  				"offset": opts.DataSlice.Offset,
   129  				"length": opts.DataSlice.Length,
   130  			}
   131  			if opts.Encoding == solana.EncodingJSONParsed {
   132  				return nil, errors.New("cannot use dataSlice with EncodingJSONParsed")
   133  			}
   134  		}
   135  		if opts.MinContextSlot != nil {
   136  			obj["minContextSlot"] = *opts.MinContextSlot
   137  		}
   138  	}
   139  
   140  	params := []interface{}{account}
   141  	if len(obj) > 0 {
   142  		params = append(params, obj)
   143  	}
   144  
   145  	err = cl.rpcClient.CallForInto(ctx, &out, "getAccountInfo", params)
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  	if out == nil {
   150  		return nil, errors.New("expected a value, got null result")
   151  	}
   152  	return out, nil
   153  }