The backend for the project formerly known as signet, now known as beignet.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.4 KiB

  1. package endpoints
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strconv"
  6. "github.com/rs/zerolog/log"
  7. "github.com/stellar/go/clients/horizonclient"
  8. "github.com/stellar/go/keypair"
  9. "github.com/stellar/go/protocols/horizon"
  10. )
  11. type GetBalanceRequest struct {
  12. SecretKey string `json:"secretKey"`
  13. }
  14. type GetBalanceResponse struct {
  15. Balance float64 `json:"balance"`
  16. }
  17. func GetBalance(w http.ResponseWriter, r *http.Request) {
  18. var req GetBalanceRequest
  19. err := json.NewDecoder(r.Body).Decode(&req)
  20. if err != nil {
  21. log.Error().Err(err).Msg("Could not decode body in GetBalance call")
  22. return
  23. }
  24. var kp keypair.KP
  25. kp, err = keypair.Parse(req.SecretKey)
  26. if err != nil {
  27. w.WriteHeader(404)
  28. return
  29. }
  30. acctReq := horizonclient.AccountRequest{
  31. AccountID: kp.Address(),
  32. }
  33. var acct horizon.Account
  34. acct, err = client.AccountDetail(acctReq)
  35. if err != nil {
  36. log.Error().Err(err).Msg("Could not get account data from public key")
  37. return
  38. }
  39. var blnce = -1.0
  40. for _, balance := range acct.Balances {
  41. if balance.Asset.Type == "native" {
  42. blnce, err = strconv.ParseFloat(balance.Balance, 64)
  43. }
  44. }
  45. if blnce == -1.0 {
  46. log.Error().Err(err).Msg("Could not get XLM balance")
  47. return
  48. }
  49. var resp GetBalanceResponse
  50. resp.Balance = blnce
  51. err = json.NewEncoder(w).Encode(resp)
  52. if err != nil {
  53. log.Error().Err(err).Msg("Could not deliver response in GetBalance call")
  54. }
  55. }