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.

109 lignes
2.8 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. SellingWallet string `json:"sellingWallet"`
  16. IssuerWallet string `json:"issuerWallet"`
  17. Memo string `json:"memo"`
  18. MinContribution float64 `gorm:"type:decimal(19,7)" json:"minContribution"`
  19. TelegramLink string `json:"telegramLink"`
  20. QueueID uint `json:"queueID"`
  21. Bonuses []Bonus `json:"bonuses"`
  22. }
  23. type SuccessResponse struct {
  24. Success bool `json:"success"`
  25. }
  26. func CreateRewardFund(resp http.ResponseWriter, req *http.Request) {
  27. var fund CreateRewardFundRequest
  28. dec := json.NewDecoder(req.Body)
  29. err := dec.Decode(&fund)
  30. if err != nil {
  31. log.Error().Err(err).Msg("Could not read submitted reward fund")
  32. return
  33. }
  34. var bonuses []Bonus
  35. rewardFund := RewardFund{
  36. Asset: fund.Asset,
  37. FundWallet: fund.FundWallet,
  38. SellingWallet: fund.SellingWallet,
  39. IssuerWallet: fund.IssuerWallet,
  40. Memo: fund.Memo,
  41. Price: 0,
  42. AmountAvailable: 0,
  43. MinContribution: fund.MinContribution,
  44. TelegramLink: fund.TelegramLink,
  45. Contributions: nil,
  46. }
  47. var fundsInQueue []RewardFund
  48. Db.Table("queue_reward_funds").Where("queue_id = ?", fund.QueueID).Scan(&fundsInQueue)
  49. next := uint16(len(fundsInQueue))
  50. joinRecord := QueueOrder{QueueID: fund.QueueID, RewardFundID: rewardFund.ID, Order: next}
  51. offerReq := horizonclient.OfferRequest{
  52. Seller: rewardFund.SellingWallet,
  53. Selling: fmt.Sprintf("%s:%s", rewardFund.Asset, rewardFund.IssuerWallet),
  54. Order: horizonclient.OrderDesc,
  55. }
  56. if err, ok := utils.FindOffer(offerReq, &rewardFund); !ok {
  57. err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: ok})
  58. if err != nil {
  59. log.Error().Err(err).Msg("Could not deliver response after failing to find issuer offer")
  60. }
  61. return
  62. }
  63. if err != nil {
  64. log.Error().Err(err).Msg("Could not find issuer offer")
  65. return
  66. }
  67. var claims *auth.Claims
  68. claims, err = auth.GetUserClaims(req)
  69. if err != nil {
  70. log.Error().Err(err).Msg("Could not determine if user is authenticated")
  71. return
  72. }
  73. if claims.Privileges <= Admin {
  74. Db.Create(&rewardFund)
  75. Db.Create(&joinRecord)
  76. for _, cancel := range cancellations {
  77. cancel()
  78. }
  79. go InitializeContributionStreams()
  80. for _, bonus := range fund.Bonuses {
  81. bonus.RewardFundID = rewardFund.ID
  82. bonuses = append(bonuses, bonus)
  83. }
  84. Db.Create(&bonuses)
  85. err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: true})
  86. if err != nil {
  87. log.Error().Err(err).Msg("Could not create response for created reward fund")
  88. }
  89. } else {
  90. resp.WriteHeader(403)
  91. }
  92. }