github.com/gagliardetto/solana-go@v1.11.0/rpc/examples/getAccountInfo/getAccountInfo.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  	solana "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.MainNetBeta_RPC
    29  	client := rpc.New(endpoint)
    30  
    31  	{
    32  		pubKey := solana.MustPublicKeyFromBase58("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt") // serum token
    33  		// basic usage
    34  		resp, err := client.GetAccountInfo(
    35  			context.TODO(),
    36  			pubKey,
    37  		)
    38  		if err != nil {
    39  			panic(err)
    40  		}
    41  		spew.Dump(resp)
    42  
    43  		var mint token.Mint
    44  		// Account{}.Data.GetBinary() returns the *decoded* binary data
    45  		// regardless the original encoding (it can handle them all).
    46  		err = bin.NewBinDecoder(resp.Value.Data.GetBinary()).Decode(&mint)
    47  		if err != nil {
    48  			panic(err)
    49  		}
    50  		spew.Dump(mint)
    51  		// NOTE: The supply is mint.Supply, with the mint.Decimals:
    52  		// mint.Supply = 9998022451607088
    53  		// mint.Decimals = 6
    54  		// ... which means that the supply is 9998022451.607088
    55  	}
    56  	{
    57  		// Or you can use `GetAccountDataIn` which does all of the above in one call:
    58  		pubKey := solana.MustPublicKeyFromBase58("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt") // serum token
    59  		var mint token.Mint
    60  		// Get the account, and decode its data into the provided mint object:
    61  		err := client.GetAccountDataInto(
    62  			context.TODO(),
    63  			pubKey,
    64  			&mint,
    65  		)
    66  		if err != nil {
    67  			panic(err)
    68  		}
    69  		spew.Dump(mint)
    70  	}
    71  	{
    72  		// // Or you can use `GetAccountDataBorsh` which does all of the above in one call but for borsh-encoded data:
    73  		// var metadata token_metadata.Metadata
    74  		// // Get the account, and decode its data into the provided metadata object:
    75  		// err := client.GetAccountDataBorsh(
    76  		//   context.TODO(),
    77  		//   pubKey,
    78  		//   &metadata,
    79  		// )
    80  		// if err != nil {
    81  		//   panic(err)
    82  		// }
    83  		// spew.Dump(metadata)
    84  	}
    85  	{
    86  		pubKey := solana.MustPublicKeyFromBase58("4k3Dyjzvzp8eMZWUXbBCjEvwSkkk59S5iCNLY3QrkX6R") // raydium token
    87  		// advanced usage
    88  		resp, err := client.GetAccountInfoWithOpts(
    89  			context.TODO(),
    90  			pubKey,
    91  			// You can specify more options here:
    92  			&rpc.GetAccountInfoOpts{
    93  				Encoding:   solana.EncodingBase64Zstd,
    94  				Commitment: rpc.CommitmentFinalized,
    95  				// You can get just a part of the account data by specify a DataSlice:
    96  				// DataSlice: &rpc.DataSlice{
    97  				// 	Offset: pointer.ToUint64(0),
    98  				// 	Length: pointer.ToUint64(1024),
    99  				// },
   100  			},
   101  		)
   102  		if err != nil {
   103  			panic(err)
   104  		}
   105  		spew.Dump(resp)
   106  
   107  		var mint token.Mint
   108  		err = bin.NewBinDecoder(resp.Value.Data.GetBinary()).Decode(&mint)
   109  		if err != nil {
   110  			panic(err)
   111  		}
   112  		spew.Dump(mint)
   113  	}
   114  }