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.

153 lines
3.9 KiB

  1. package endpoints
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "github.com/imosed/signet/auth"
  8. . "github.com/imosed/signet/data"
  9. "github.com/rs/zerolog/log"
  10. "github.com/stellar/go/clients/horizonclient"
  11. "github.com/stellar/go/protocols/horizon"
  12. )
  13. type CreateRewardFundRequest struct {
  14. Asset string `json:"asset"`
  15. FundWallet string `json:"fundWallet"`
  16. SellingWallet string `json:"sellingWallet"`
  17. IssuerWallet string `json:"issuerWallet"`
  18. Memo string `json:"memo"`
  19. MinContribution float64 `gorm:"type:decimal(19,7)" json:"minContribution"`
  20. TelegramLink string `json:"telegramLink"`
  21. QueueID uint `json:"queueID"`
  22. Bonuses []Bonus `json:"bonuses"`
  23. }
  24. type SuccessResponse struct {
  25. Success bool `json:"success"`
  26. }
  27. func CreateRewardFund(resp http.ResponseWriter, req *http.Request) {
  28. var fund CreateRewardFundRequest
  29. dec := json.NewDecoder(req.Body)
  30. err := dec.Decode(&fund)
  31. if err != nil {
  32. log.Error().Err(err).Msg("Could not read submitted reward fund")
  33. return
  34. }
  35. var bonuses []Bonus
  36. rewardFund := RewardFund{
  37. Asset: fund.Asset,
  38. FundWallet: fund.FundWallet,
  39. SellingWallet: fund.SellingWallet,
  40. IssuerWallet: fund.IssuerWallet,
  41. Memo: fund.Memo,
  42. Price: 0,
  43. AmountAvailable: 0,
  44. MinContribution: fund.MinContribution,
  45. TelegramLink: fund.TelegramLink,
  46. Contributions: nil,
  47. }
  48. var fundsInQueue []RewardFund
  49. Db.Table("queue_reward_funds").Where("queue_id = ?", fund.QueueID).Scan(&fundsInQueue)
  50. next := uint16(len(fundsInQueue))
  51. joinTable := QueueOrder{QueueID: fund.QueueID, RewardFundID: rewardFund.ID, Order: next}
  52. offerReq := horizonclient.OfferRequest{
  53. Seller: rewardFund.SellingWallet,
  54. Selling: fmt.Sprintf("%s:%s", rewardFund.Asset, rewardFund.IssuerWallet),
  55. Order: horizonclient.OrderDesc,
  56. }
  57. url, _ := offerReq.BuildURL()
  58. fmt.Println(url)
  59. op, err := client.Offers(offerReq)
  60. if err != nil {
  61. log.Error().Err(err).Msg("Could not get offers")
  62. return
  63. }
  64. offers := op.Embedded.Records
  65. var price float64
  66. var amt float64
  67. if len(offers) == 1 {
  68. price, err = strconv.ParseFloat(op.Embedded.Records[0].Price, 64)
  69. if err != nil {
  70. log.Error().Err(err).Msg("Could not parse price to float")
  71. return
  72. }
  73. amt, err = strconv.ParseFloat(op.Embedded.Records[0].Amount, 64)
  74. if err != nil {
  75. log.Error().Err(err).Msg("Could not parse amount to float")
  76. return
  77. }
  78. rewardFund.Price = price
  79. rewardFund.AmountAvailable = amt
  80. } else if len(offers) > 1 {
  81. var maxOffers float64 = 0
  82. var correctOffer horizon.Offer
  83. for _, o := range op.Embedded.Records {
  84. parsedAmt, err := strconv.ParseFloat(o.Amount, 64)
  85. if err != nil {
  86. log.Error().Err(err).Msg("Could not parse amount to float")
  87. return
  88. }
  89. if parsedAmt > maxOffers {
  90. correctOffer = o
  91. maxOffers = parsedAmt
  92. }
  93. }
  94. price, err = strconv.ParseFloat(correctOffer.Price, 64)
  95. if err != nil {
  96. log.Error().Err(err).Msg("Could not parse price to float")
  97. return
  98. }
  99. rewardFund.Price = price
  100. amt, err = strconv.ParseFloat(correctOffer.Amount, 64)
  101. if err != nil {
  102. log.Error().Err(err).Msg("Could not parse amount to float")
  103. }
  104. rewardFund.AmountAvailable = amt
  105. } else {
  106. err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: false})
  107. return
  108. }
  109. var claims *auth.Claims
  110. claims, err = auth.GetUserClaims(req)
  111. if err != nil {
  112. log.Error().Err(err).Msg("Could not determine if user is authenticated")
  113. return
  114. }
  115. if claims.Privileges <= Admin {
  116. Db.Create(&rewardFund)
  117. Db.Create(&joinTable)
  118. for _, cancel := range cancellations {
  119. cancel()
  120. }
  121. go InitializeContributionStreams()
  122. for _, bonus := range fund.Bonuses {
  123. bonus.RewardFundID = rewardFund.ID
  124. bonuses = append(bonuses, bonus)
  125. }
  126. Db.Create(&bonuses)
  127. err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: true})
  128. if err != nil {
  129. log.Error().Err(err).Msg("Could not create response for created reward fund")
  130. }
  131. } else {
  132. resp.WriteHeader(403)
  133. }
  134. }