|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- package utils
-
- import (
- "errors"
- "strconv"
-
- "github.com/imosed/signet/client"
- "github.com/imosed/signet/data"
- "github.com/stellar/go/clients/horizonclient"
- "github.com/stellar/go/protocols/horizon"
- )
-
- func FindOffer(offerReq horizonclient.OfferRequest, rewardFund *data.RewardFund) (error, bool) {
- op, err := client.SignetClient.Offers(offerReq)
- if err != nil {
- return errors.New("could not get offers"), false
- }
- offers := op.Embedded.Records
- var price float64
- var amt float64
- if len(offers) == 1 {
- price, err = strconv.ParseFloat(op.Embedded.Records[0].Price, 64)
- if err != nil {
- return errors.New("could not parse single offer price to float"), false
- }
- amt, err = strconv.ParseFloat(op.Embedded.Records[0].Amount, 64)
- if err != nil {
- return errors.New("could not parse single offer amount to float"), false
- }
- rewardFund.Price = price
- rewardFund.AmountAvailable = amt
- return nil, true
- } else if len(offers) > 1 {
- var maxOffers float64 = 0
- var correctOffer horizon.Offer
- for _, o := range op.Embedded.Records {
- parsedAmt, err := strconv.ParseFloat(o.Amount, 64)
- if err != nil {
- return errors.New("could not parse amount from offer slice to float"), false
- }
- if parsedAmt > maxOffers {
- correctOffer = o
- maxOffers = parsedAmt
- }
- }
- price, err = strconv.ParseFloat(correctOffer.Price, 64)
- if err != nil {
- return errors.New("could not parse correct offer price to float"), false
- }
- amt, err = strconv.ParseFloat(correctOffer.Amount, 64)
- if err != nil {
- return errors.New("could not parse correct offer amount to float"), false
- }
- rewardFund.Price = price
- rewardFund.AmountAvailable = amt
- return nil, true
- } else {
- return nil, false // no offers shouldn't error
- }
- }
|