github.com/gagliardetto/solana-go@v1.11.0/programs/tokenregistry/rpc.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  
    18  package tokenregistry
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  
    24  	"github.com/gagliardetto/solana-go"
    25  	"github.com/gagliardetto/solana-go/rpc"
    26  )
    27  
    28  func GetTokenRegistryEntry(ctx context.Context, rpcCli *rpc.Client, mintAddress solana.PublicKey) (*TokenMeta, error) {
    29  	resp, err := rpcCli.GetProgramAccountsWithOpts(
    30  		ctx,
    31  		ProgramID(),
    32  		&rpc.GetProgramAccountsOpts{
    33  			Filters: []rpc.RPCFilter{
    34  				{
    35  					Memcmp: &rpc.RPCFilterMemcmp{
    36  						Offset: 5,
    37  						Bytes:  mintAddress[:], // hackey to convert [32]byte to []byte
    38  					},
    39  				},
    40  			},
    41  		},
    42  	)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	if resp == nil {
    47  		return nil, fmt.Errorf("resp empty... cannot find account")
    48  	}
    49  
    50  	for _, keyedAcct := range resp {
    51  		acct := keyedAcct.Account
    52  		t, err := DecodeTokenMeta(acct.Data.GetBinary())
    53  		if err != nil {
    54  			return nil, fmt.Errorf("unable to decode token meta %q: %w", acct.Owner.String(), err)
    55  		}
    56  		return t, nil
    57  	}
    58  	return nil, rpc.ErrNotFound
    59  }
    60  
    61  func GetEntries(ctx context.Context, rpcCli *rpc.Client) (out []*TokenMeta, err error) {
    62  	resp, err := rpcCli.GetProgramAccountsWithOpts(
    63  		ctx,
    64  		ProgramID(),
    65  		&rpc.GetProgramAccountsOpts{
    66  			Filters: []rpc.RPCFilter{
    67  				{
    68  					DataSize: TOKEN_META_SIZE,
    69  				},
    70  			},
    71  		},
    72  	)
    73  	if err != nil {
    74  		return nil, err
    75  	}
    76  	if resp == nil {
    77  		return nil, fmt.Errorf("resp empty... cannot find accounts")
    78  	}
    79  
    80  	for _, keyedAcct := range resp {
    81  		acct := keyedAcct.Account
    82  		t, err := DecodeTokenMeta(acct.Data.GetBinary())
    83  		if err != nil {
    84  			return nil, fmt.Errorf("unable to decode token meta %q: %w", acct.Owner.String(), err)
    85  		}
    86  		out = append(out, t)
    87  	}
    88  	return out, nil
    89  }