github.com/gagliardetto/solana-go@v1.11.0/rpc/examples/getBalance/getBalance.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 "fmt" 20 "math/big" 21 22 "github.com/davecgh/go-spew/spew" 23 "github.com/gagliardetto/solana-go" 24 "github.com/gagliardetto/solana-go/rpc" 25 ) 26 27 func main() { 28 endpoint := rpc.MainNetBeta_RPC 29 client := rpc.New(endpoint) 30 31 pubKey := solana.MustPublicKeyFromBase58("7xLk17EQQ5KLDLDe44wCmupJKJjTGd8hs3eSVVhCx932") 32 out, err := client.GetBalance( 33 context.TODO(), 34 pubKey, 35 rpc.CommitmentFinalized, 36 ) 37 if err != nil { 38 panic(err) 39 } 40 spew.Dump(out) 41 spew.Dump(out.Value) // total lamports on the account; 1 sol = 1000000000 lamports 42 43 var lamportsOnAccount = new(big.Float).SetUint64(uint64(out.Value)) 44 // Convert lamports to sol: 45 var solBalance = new(big.Float).Quo(lamportsOnAccount, new(big.Float).SetUint64(solana.LAMPORTS_PER_SOL)) 46 47 // WARNING: this is not a precise conversion. 48 fmt.Println("◎", solBalance.Text('f', 10)) 49 }