The backend for the project formerly known as signet, now known as beignet.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

62 rader
1.2 KiB

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