|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package endpoints
-
- import (
- "encoding/json"
- "net/http"
- "strconv"
-
- "github.com/imosed/signet/client"
- "github.com/rs/zerolog/log"
- "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 {
- log.Error().Err(err).Msg("Could not decode body in GetBalance call")
- return
- }
-
- 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.SignetClient.AccountDetail(acctReq)
- if err != nil {
- log.Error().Err(err).Msg("Could not get account data from public key")
- return
- }
-
- 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 {
- log.Error().Err(err).Msg("Could not get XLM balance")
- return
- }
-
- var resp GetBalanceResponse
- resp.Balance = blnce
-
- err = json.NewEncoder(w).Encode(resp)
- if err != nil {
- log.Error().Err(err).Msg("Could not deliver response in GetBalance call")
- }
- }
|