github.com/gagliardetto/solana-go@v1.11.0/programs/token/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 token
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  
    24  	bin "github.com/gagliardetto/binary"
    25  	"github.com/gagliardetto/solana-go/rpc"
    26  )
    27  
    28  const MINT_SIZE = 82
    29  
    30  func (mint *Mint) Decode(data []byte) error {
    31  	mint = new(Mint)
    32  	dec := bin.NewBinDecoder(data)
    33  	if err := dec.Decode(&mint); err != nil {
    34  		return fmt.Errorf("unable to decode mint: %w", err)
    35  	}
    36  	return nil
    37  }
    38  
    39  func FetchMints(ctx context.Context, rpcCli *rpc.Client) (out []*Mint, err error) {
    40  	resp, err := rpcCli.GetProgramAccountsWithOpts(
    41  		ctx,
    42  		ProgramID,
    43  		&rpc.GetProgramAccountsOpts{
    44  			Filters: []rpc.RPCFilter{
    45  				{
    46  					DataSize: MINT_SIZE,
    47  				},
    48  			},
    49  		},
    50  	)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	if resp == nil {
    55  		return nil, fmt.Errorf("resp empty... program account not found")
    56  	}
    57  
    58  	for _, keyedAcct := range resp {
    59  		acct := keyedAcct.Account
    60  
    61  		m := new(Mint)
    62  		if err := m.Decode(acct.Data.GetBinary()); err != nil {
    63  			return nil, fmt.Errorf("unable to decode mint %q: %w", acct.Owner.String(), err)
    64  		}
    65  		out = append(out, m)
    66  
    67  	}
    68  	return
    69  }