The backend for the project formerly known as signet, now known as beignet.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

createrewardfund.go 2.9 KiB

há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
há 2 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 && 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. }