github.com/gagliardetto/solana-go@v1.11.0/rpc/examples/getTokenAccountsByOwner/getTokenAccountsByOwner.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 main
    16  
    17  import (
    18  	"context"
    19  
    20  	"github.com/davecgh/go-spew/spew"
    21  	bin "github.com/gagliardetto/binary"
    22  	"github.com/gagliardetto/solana-go"
    23  	"github.com/gagliardetto/solana-go/programs/token"
    24  	"github.com/gagliardetto/solana-go/rpc"
    25  )
    26  
    27  func main() {
    28  	endpoint := rpc.TestNet_RPC
    29  	client := rpc.New(endpoint)
    30  
    31  	pubKey := solana.MustPublicKeyFromBase58("7HZaCWazgTuuFuajxaaxGYbGnyVKwxvsJKue1W4Nvyro")
    32  	out, err := client.GetTokenAccountsByOwner(
    33  		context.TODO(),
    34  		pubKey,
    35  		&rpc.GetTokenAccountsConfig{
    36  			Mint: solana.WrappedSol.ToPointer(),
    37  		},
    38  		&rpc.GetTokenAccountsOpts{
    39  			Encoding: solana.EncodingBase64Zstd,
    40  		},
    41  	)
    42  	if err != nil {
    43  		panic(err)
    44  	}
    45  	spew.Dump(out)
    46  
    47  	{
    48  		tokenAccounts := make([]token.Account, 0)
    49  		for _, rawAccount := range out.Value {
    50  			var tokAcc token.Account
    51  
    52  			data := rawAccount.Account.Data.GetBinary()
    53  			dec := bin.NewBinDecoder(data)
    54  			err := dec.Decode(&tokAcc)
    55  			if err != nil {
    56  				panic(err)
    57  			}
    58  			tokenAccounts = append(tokenAccounts, tokAcc)
    59  		}
    60  		spew.Dump(tokenAccounts)
    61  	}
    62  }