The backend for the project formerly known as signet, now known as beignet.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

110 rindas
2.9 KiB

  1. package endpoints
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "github.com/imosed/signet/auth"
  7. . "github.com/imosed/signet/data"
  8. "github.com/imosed/signet/utils"
  9. "github.com/rs/zerolog/log"
  10. "github.com/stellar/go/clients/horizonclient"
  11. )
  12. type CreateRewardFundRequest struct {
  13. Asset string `json:"asset"`
  14. FundWallet string `json:"fundWallet"`
  15. FundSecret string `json:"fundSecret"`
  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. joinRecord := 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. if err, ok := utils.FindOffer(offerReq, &rewardFund); !ok {
  58. err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: ok})
  59. if err != nil {
  60. log.Error().Err(err).Msg("Could not deliver response after failing to find issuer offer")
  61. }
  62. return
  63. }
  64. if err != nil {
  65. log.Error().Err(err).Msg("Could not find issuer offer")
  66. return
  67. }
  68. var claims *auth.Claims
  69. claims, err = auth.GetUserClaims(req)
  70. if err != nil {
  71. log.Error().Err(err).Msg("Could not determine if user is authenticated")
  72. return
  73. }
  74. if claims.Privileges <= Admin {
  75. Db.Create(&rewardFund)
  76. Db.Create(&joinRecord)
  77. for _, cancel := range cancellations {
  78. cancel()
  79. }
  80. go InitializeContributionStreams()
  81. for _, bonus := range fund.Bonuses {
  82. bonus.RewardFundID = rewardFund.ID
  83. bonuses = append(bonuses, bonus)
  84. }
  85. Db.Create(&bonuses)
  86. err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: true})
  87. if err != nil {
  88. log.Error().Err(err).Msg("Could not create response for created reward fund")
  89. }
  90. } else {
  91. resp.WriteHeader(403)
  92. }
  93. }