| 
                        1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 | 
                        - package endpoints
 - 
 - import (
 - 	"encoding/json"
 - 	"net/http"
 - 	"strconv"
 - 
 - 	"github.com/stellar/go/clients/horizonclient"
 - 	"github.com/stellar/go/keypair"
 - 	"github.com/stellar/go/protocols/horizon"
 - )
 - 
 - type GetBalanceRequest struct {
 - 	SecretKey string `json:"secretKey"`
 - }
 - 
 - type GetBalanceResponse struct {
 - 	Balance float64 `json:"balance"`
 - }
 - 
 - func GetBalance(w http.ResponseWriter, r *http.Request) {
 - 	var req GetBalanceRequest
 - 	err := json.NewDecoder(r.Body).Decode(&req)
 - 	if err != nil {
 - 		panic("Could not decode body")
 - 	}
 - 
 - 	var kp keypair.KP
 - 	kp, err = keypair.Parse(req.SecretKey)
 - 	if err != nil {
 - 		w.WriteHeader(404)
 - 		return
 - 	}
 - 
 - 	acctReq := horizonclient.AccountRequest{
 - 		AccountID: kp.Address(),
 - 	}
 - 	var acct horizon.Account
 - 	acct, err = client.AccountDetail(acctReq)
 - 	if err != nil {
 - 		panic("Could not get account data from public key")
 - 	}
 - 
 - 	var blnce = -1.0
 - 	for _, balance := range acct.Balances {
 - 		if balance.Asset.Type == "native" {
 - 			blnce, err = strconv.ParseFloat(balance.Balance, 64)
 - 		}
 - 	}
 - 
 - 	if blnce == -1.0 {
 - 		panic("Could not get XLM balance")
 - 	}
 - 
 - 	var resp GetBalanceResponse
 - 	resp.Balance = blnce
 - 
 - 	err = json.NewEncoder(w).Encode(resp)
 - 	if err != nil {
 - 		panic("Could not deliver response")
 - 	}
 - }
 
 
  |