The backend for the project formerly known as signet, now known as beignet.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

111 rader
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.Privileges <= Admin {
  76. Db.Create(&rewardFund)
  77. Db.Create(&joinRecord)
  78. for _, cancel := range cancellations {
  79. cancel()
  80. }
  81. go InitializeContributionStreams()
  82. for _, bonus := range fund.Bonuses {
  83. bonus.RewardFundID = rewardFund.ID
  84. bonuses = append(bonuses, bonus)
  85. }
  86. Db.Create(&bonuses)
  87. err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: true})
  88. if err != nil {
  89. log.Error().Err(err).Msg("Could not create response for created reward fund")
  90. }
  91. } else {
  92. resp.WriteHeader(403)
  93. }
  94. }