The backend for the project formerly known as signet, now known as beignet.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

61 linhas
1.7 KiB

  1. package utils
  2. import (
  3. "errors"
  4. "strconv"
  5. "github.com/imosed/signet/client"
  6. "github.com/imosed/signet/data"
  7. "github.com/stellar/go/clients/horizonclient"
  8. "github.com/stellar/go/protocols/horizon"
  9. )
  10. func FindOffer(offerReq horizonclient.OfferRequest, rewardFund *data.RewardFund) (error, bool) {
  11. op, err := client.SignetClient.Offers(offerReq)
  12. if err != nil {
  13. return errors.New("could not get offers"), false
  14. }
  15. offers := op.Embedded.Records
  16. var price float64
  17. var amt float64
  18. if len(offers) == 1 {
  19. price, err = strconv.ParseFloat(op.Embedded.Records[0].Price, 64)
  20. if err != nil {
  21. return errors.New("could not parse single offer price to float"), false
  22. }
  23. amt, err = strconv.ParseFloat(op.Embedded.Records[0].Amount, 64)
  24. if err != nil {
  25. return errors.New("could not parse single offer amount to float"), false
  26. }
  27. rewardFund.Price = price
  28. rewardFund.AmountAvailable = amt
  29. return nil, true
  30. } else if len(offers) > 1 {
  31. var maxOffers float64 = 0
  32. var correctOffer horizon.Offer
  33. for _, o := range op.Embedded.Records {
  34. parsedAmt, err := strconv.ParseFloat(o.Amount, 64)
  35. if err != nil {
  36. return errors.New("could not parse amount from offer slice to float"), false
  37. }
  38. if parsedAmt > maxOffers {
  39. correctOffer = o
  40. maxOffers = parsedAmt
  41. }
  42. }
  43. price, err = strconv.ParseFloat(correctOffer.Price, 64)
  44. if err != nil {
  45. return errors.New("could not parse correct offer price to float"), false
  46. }
  47. amt, err = strconv.ParseFloat(correctOffer.Amount, 64)
  48. if err != nil {
  49. return errors.New("could not parse correct offer amount to float"), false
  50. }
  51. rewardFund.Price = price
  52. rewardFund.AmountAvailable = amt
  53. return nil, true
  54. } else {
  55. return nil, false // no offers shouldn't error
  56. }
  57. }