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.

68 regels
1.4 KiB

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