| @@ -2,6 +2,7 @@ package endpoints | |||||
| import ( | import ( | ||||
| "encoding/json" | "encoding/json" | ||||
| "errors" | |||||
| "fmt" | "fmt" | ||||
| "net/http" | "net/http" | ||||
| "strconv" | "strconv" | ||||
| @@ -57,7 +58,7 @@ func CreateRewardFund(resp http.ResponseWriter, req *http.Request) { | |||||
| Db.Table("queue_reward_funds").Where("queue_id = ?", fund.QueueID).Scan(&fundsInQueue) | Db.Table("queue_reward_funds").Where("queue_id = ?", fund.QueueID).Scan(&fundsInQueue) | ||||
| next := uint16(len(fundsInQueue)) | next := uint16(len(fundsInQueue)) | ||||
| joinTable := QueueOrder{QueueID: fund.QueueID, RewardFundID: rewardFund.ID, Order: next} | |||||
| joinRecord := QueueOrder{QueueID: fund.QueueID, RewardFundID: rewardFund.ID, Order: next} | |||||
| offerReq := horizonclient.OfferRequest{ | offerReq := horizonclient.OfferRequest{ | ||||
| Seller: rewardFund.SellingWallet, | Seller: rewardFund.SellingWallet, | ||||
| @@ -65,38 +66,76 @@ func CreateRewardFund(resp http.ResponseWriter, req *http.Request) { | |||||
| Order: horizonclient.OrderDesc, | Order: horizonclient.OrderDesc, | ||||
| } | } | ||||
| url, _ := offerReq.BuildURL() | |||||
| fmt.Println(url) | |||||
| if err, ok := FindOffer(offerReq, &rewardFund); !ok { | |||||
| err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: ok}) | |||||
| if err != nil { | |||||
| log.Error().Err(err).Msg("Could not deliver response after failing to find issuer offer") | |||||
| } | |||||
| return | |||||
| } | |||||
| if err != nil { | |||||
| log.Error().Err(err).Msg("Could not find issuer offer") | |||||
| return | |||||
| } | |||||
| op, err := client.Offers(offerReq) | |||||
| var claims *auth.Claims | |||||
| claims, err = auth.GetUserClaims(req) | |||||
| if err != nil { | if err != nil { | ||||
| log.Error().Err(err).Msg("Could not get offers") | |||||
| log.Error().Err(err).Msg("Could not determine if user is authenticated") | |||||
| return | return | ||||
| } | } | ||||
| if claims.Privileges <= Admin { | |||||
| Db.Create(&rewardFund) | |||||
| Db.Create(&joinRecord) | |||||
| for _, cancel := range cancellations { | |||||
| cancel() | |||||
| } | |||||
| go InitializeContributionStreams() | |||||
| for _, bonus := range fund.Bonuses { | |||||
| bonus.RewardFundID = rewardFund.ID | |||||
| bonuses = append(bonuses, bonus) | |||||
| } | |||||
| Db.Create(&bonuses) | |||||
| err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: true}) | |||||
| if err != nil { | |||||
| log.Error().Err(err).Msg("Could not create response for created reward fund") | |||||
| } | |||||
| } else { | |||||
| resp.WriteHeader(403) | |||||
| } | |||||
| } | |||||
| func FindOffer(offerReq horizonclient.OfferRequest, rewardFund *RewardFund) (error, bool) { | |||||
| op, err := client.Offers(offerReq) | |||||
| if err != nil { | |||||
| return errors.New("could not get offers"), false | |||||
| } | |||||
| offers := op.Embedded.Records | offers := op.Embedded.Records | ||||
| var price float64 | var price float64 | ||||
| var amt float64 | var amt float64 | ||||
| if len(offers) == 1 { | if len(offers) == 1 { | ||||
| price, err = strconv.ParseFloat(op.Embedded.Records[0].Price, 64) | price, err = strconv.ParseFloat(op.Embedded.Records[0].Price, 64) | ||||
| if err != nil { | if err != nil { | ||||
| log.Error().Err(err).Msg("Could not parse price to float") | |||||
| return | |||||
| return errors.New("could not parse single offer price to float"), false | |||||
| } | } | ||||
| amt, err = strconv.ParseFloat(op.Embedded.Records[0].Amount, 64) | amt, err = strconv.ParseFloat(op.Embedded.Records[0].Amount, 64) | ||||
| if err != nil { | if err != nil { | ||||
| log.Error().Err(err).Msg("Could not parse amount to float") | |||||
| return | |||||
| return errors.New("could not parse single offer amount to float"), false | |||||
| } | } | ||||
| rewardFund.Price = price | rewardFund.Price = price | ||||
| rewardFund.AmountAvailable = amt | rewardFund.AmountAvailable = amt | ||||
| return nil, true | |||||
| } else if len(offers) > 1 { | } else if len(offers) > 1 { | ||||
| var maxOffers float64 = 0 | var maxOffers float64 = 0 | ||||
| var correctOffer horizon.Offer | var correctOffer horizon.Offer | ||||
| for _, o := range op.Embedded.Records { | for _, o := range op.Embedded.Records { | ||||
| parsedAmt, err := strconv.ParseFloat(o.Amount, 64) | parsedAmt, err := strconv.ParseFloat(o.Amount, 64) | ||||
| if err != nil { | if err != nil { | ||||
| log.Error().Err(err).Msg("Could not parse amount to float") | |||||
| return | |||||
| return errors.New("could not parse amount from offer slice to float"), false | |||||
| } | } | ||||
| if parsedAmt > maxOffers { | if parsedAmt > maxOffers { | ||||
| correctOffer = o | correctOffer = o | ||||
| @@ -105,48 +144,17 @@ func CreateRewardFund(resp http.ResponseWriter, req *http.Request) { | |||||
| } | } | ||||
| price, err = strconv.ParseFloat(correctOffer.Price, 64) | price, err = strconv.ParseFloat(correctOffer.Price, 64) | ||||
| if err != nil { | if err != nil { | ||||
| log.Error().Err(err).Msg("Could not parse price to float") | |||||
| return | |||||
| return errors.New("could not parse correct offer price to float"), false | |||||
| } | } | ||||
| rewardFund.Price = price | rewardFund.Price = price | ||||
| amt, err = strconv.ParseFloat(correctOffer.Amount, 64) | amt, err = strconv.ParseFloat(correctOffer.Amount, 64) | ||||
| if err != nil { | if err != nil { | ||||
| log.Error().Err(err).Msg("Could not parse amount to float") | |||||
| return errors.New("could not parse correct offer amount to float"), false | |||||
| } | } | ||||
| rewardFund.AmountAvailable = amt | rewardFund.AmountAvailable = amt | ||||
| return nil, true | |||||
| } else { | } else { | ||||
| err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: false}) | |||||
| return | |||||
| } | |||||
| var claims *auth.Claims | |||||
| claims, err = auth.GetUserClaims(req) | |||||
| if err != nil { | |||||
| log.Error().Err(err).Msg("Could not determine if user is authenticated") | |||||
| return | |||||
| } | |||||
| if claims.Privileges <= Admin { | |||||
| Db.Create(&rewardFund) | |||||
| Db.Create(&joinTable) | |||||
| for _, cancel := range cancellations { | |||||
| cancel() | |||||
| } | |||||
| go InitializeContributionStreams() | |||||
| for _, bonus := range fund.Bonuses { | |||||
| bonus.RewardFundID = rewardFund.ID | |||||
| bonuses = append(bonuses, bonus) | |||||
| } | |||||
| Db.Create(&bonuses) | |||||
| err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: true}) | |||||
| if err != nil { | |||||
| log.Error().Err(err).Msg("Could not create response for created reward fund") | |||||
| } | |||||
| } else { | |||||
| resp.WriteHeader(403) | |||||
| return nil, false // no offers shouldn't error | |||||
| } | } | ||||
| } | } | ||||