The backend for the project formerly known as signet, now known as beignet.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

116 lignes
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. FundSecret: fund.FundSecret,
  40. SellingWallet: fund.SellingWallet,
  41. IssuerWallet: fund.IssuerWallet,
  42. Memo: fund.Memo,
  43. Price: 0,
  44. AmountAvailable: 0,
  45. MinContribution: fund.MinContribution,
  46. TelegramLink: fund.TelegramLink,
  47. Contributions: nil,
  48. }
  49. var fundsInQueue []RewardFund
  50. Db.Table("queue_reward_funds").Where("queue_id = ?", fund.QueueID).Scan(&fundsInQueue)
  51. next := uint16(len(fundsInQueue))
  52. joinRecord := QueueOrder{QueueID: fund.QueueID, RewardFundID: rewardFund.ID, Order: next}
  53. offerReq := horizonclient.OfferRequest{
  54. Seller: rewardFund.SellingWallet,
  55. Selling: fmt.Sprintf("%s:%s", rewardFund.Asset, rewardFund.IssuerWallet),
  56. Order: horizonclient.OrderDesc,
  57. }
  58. if err, ok := utils.FindOffer(offerReq, &rewardFund); !ok {
  59. err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: ok})
  60. if err != nil {
  61. log.Error().Err(err).Msg("Could not deliver response after failing to find issuer offer")
  62. }
  63. return
  64. }
  65. if err != nil {
  66. log.Error().Err(err).Msg("Could not find issuer offer")
  67. return
  68. }
  69. var claims *auth.Claims
  70. claims, err = auth.GetUserClaims(req)
  71. if err != nil {
  72. log.Error().Err(err).Msg("Could not determine if user is authenticated")
  73. return
  74. }
  75. if claims == nil {
  76. resp.WriteHeader(401)
  77. return
  78. }
  79. if claims.Privileges > Admin {
  80. resp.WriteHeader(403)
  81. return
  82. }
  83. Db.Create(&rewardFund)
  84. Db.Create(&joinRecord)
  85. for _, cancel := range cancellations {
  86. cancel()
  87. }
  88. go InitializeContributionStreams()
  89. for _, bonus := range fund.Bonuses {
  90. bonus.RewardFundID = rewardFund.ID
  91. bonuses = append(bonuses, bonus)
  92. }
  93. Db.Create(&bonuses)
  94. err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: true})
  95. if err != nil {
  96. log.Error().Err(err).Msg("Could not create response for created reward fund")
  97. }
  98. }