github.com/gagliardetto/solana-go@v1.11.0/rpc/getTokenAccountsByDelegate.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 GetTokenAccountsConfig struct {
    25  	// Pubkey of the specific token Mint to limit accounts to.
    26  	Mint *solana.PublicKey `json:"mint"`
    27  
    28  	// OR:
    29  
    30  	// Pubkey of the Token program ID that owns the accounts.
    31  	ProgramId *solana.PublicKey `json:"programId"`
    32  }
    33  
    34  type GetTokenAccountsOpts struct {
    35  	Commitment CommitmentType `json:"commitment,omitempty"`
    36  
    37  	Encoding solana.EncodingType `json:"encoding,omitempty"`
    38  
    39  	DataSlice *DataSlice `json:"dataSlice,omitempty"`
    40  }
    41  
    42  // GetTokenAccountsByDelegate returns all SPL Token accounts by approved Delegate.
    43  func (cl *Client) GetTokenAccountsByDelegate(
    44  	ctx context.Context,
    45  	account solana.PublicKey, // Pubkey of account delegate to query
    46  	conf *GetTokenAccountsConfig,
    47  	opts *GetTokenAccountsOpts,
    48  ) (out *GetTokenAccountsResult, err error) {
    49  	params := []interface{}{account}
    50  	if conf == nil {
    51  		return nil, errors.New("conf is nil")
    52  	}
    53  	if conf.Mint != nil && conf.ProgramId != nil {
    54  		return nil, errors.New("conf.Mint and conf.ProgramId are both set; must be just one of them")
    55  	}
    56  
    57  	{
    58  		confObj := M{}
    59  		if conf.Mint != nil {
    60  			confObj["mint"] = conf.Mint
    61  		}
    62  		if conf.ProgramId != nil {
    63  			confObj["programId"] = conf.ProgramId
    64  		}
    65  		if len(confObj) > 0 {
    66  			params = append(params, confObj)
    67  		}
    68  	}
    69  	defaultEncoding := solana.EncodingBase64
    70  	{
    71  		optsObj := M{}
    72  		if opts != nil {
    73  			if opts.Commitment != "" {
    74  				optsObj["commitment"] = opts.Commitment
    75  			}
    76  			if opts.Encoding != "" {
    77  				optsObj["encoding"] = opts.Encoding
    78  			} else {
    79  				optsObj["encoding"] = defaultEncoding
    80  			}
    81  			if opts.DataSlice != nil {
    82  				optsObj["dataSlice"] = M{
    83  					"offset": opts.DataSlice.Offset,
    84  					"length": opts.DataSlice.Length,
    85  				}
    86  				if opts.Encoding == solana.EncodingJSONParsed {
    87  					return nil, errors.New("cannot use dataSlice with EncodingJSONParsed")
    88  				}
    89  			}
    90  			if len(optsObj) > 0 {
    91  				params = append(params, optsObj)
    92  			}
    93  		} else {
    94  			params = append(params, M{"encoding": defaultEncoding})
    95  		}
    96  	}
    97  
    98  	err = cl.rpcClient.CallForInto(ctx, &out, "getTokenAccountsByDelegate", params)
    99  	return
   100  }
   101  
   102  type GetTokenAccountsResult struct {
   103  	RPCContext
   104  	Value []*TokenAccount `json:"value"`
   105  }
   106  
   107  type TokenAccount struct {
   108  	Pubkey  solana.PublicKey `json:"pubkey"`
   109  	Account Account          `json:"account"`
   110  }