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.

127 lignes
3.0 KiB

  1. package endpoints
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/imosed/signet/auth"
  6. . "github.com/imosed/signet/data"
  7. "github.com/stellar/go/clients/horizonclient"
  8. "github.com/stellar/go/protocols/horizon"
  9. "net/http"
  10. "strconv"
  11. )
  12. type SuccessResponse struct {
  13. Success bool `json:"success"`
  14. }
  15. func CreateRewardFund(resp http.ResponseWriter, req *http.Request) {
  16. var fund RewardFund
  17. dec := json.NewDecoder(req.Body)
  18. err := dec.Decode(&fund)
  19. if err != nil {
  20. panic("Could not read submitted reward fund")
  21. }
  22. var bonuses []Bonus
  23. rewardFund := RewardFund{
  24. Asset: fund.Asset,
  25. FundWallet: fund.FundWallet,
  26. SellingWallet: fund.SellingWallet,
  27. IssuerWallet: fund.IssuerWallet,
  28. Memo: fund.Memo,
  29. Price: 0,
  30. AmountAvailable: 0,
  31. MinContribution: fund.MinContribution,
  32. Title: fund.Title,
  33. Description: fund.Description,
  34. Contributions: nil,
  35. QueueID: fund.QueueID,
  36. }
  37. offerReq := horizonclient.OfferRequest{
  38. Seller: rewardFund.SellingWallet,
  39. Selling: fmt.Sprintf("%s:%s", rewardFund.Asset, rewardFund.IssuerWallet),
  40. Order: horizonclient.OrderDesc,
  41. }
  42. url, _ := offerReq.BuildURL()
  43. fmt.Println(url)
  44. op, err := client.Offers(offerReq)
  45. if err != nil {
  46. panic("Could not get offers")
  47. }
  48. offers := op.Embedded.Records
  49. var price float64
  50. var amt float64
  51. if len(offers) == 1 {
  52. price, err = strconv.ParseFloat(op.Embedded.Records[0].Price, 64)
  53. if err != nil {
  54. panic("Could not parse price to float")
  55. }
  56. amt, err = strconv.ParseFloat(op.Embedded.Records[0].Amount, 64)
  57. if err != nil {
  58. panic("Could not parse amount to float")
  59. }
  60. rewardFund.Price = price
  61. rewardFund.AmountAvailable = amt
  62. } else if len(offers) > 1 {
  63. var maxOffers float64 = 0
  64. var correctOffer horizon.Offer
  65. for _, o := range op.Embedded.Records {
  66. parsedAmt, err := strconv.ParseFloat(o.Amount, 64)
  67. if err != nil {
  68. panic("Could not parse amount to float")
  69. }
  70. if parsedAmt > maxOffers {
  71. correctOffer = o
  72. maxOffers = parsedAmt
  73. }
  74. }
  75. price, err = strconv.ParseFloat(correctOffer.Price, 64)
  76. if err != nil {
  77. panic("Could not parse price to float")
  78. }
  79. rewardFund.Price = price
  80. amt, err = strconv.ParseFloat(correctOffer.Amount, 64)
  81. if err != nil {
  82. panic("Could not parse amount to float")
  83. }
  84. rewardFund.AmountAvailable = amt
  85. } else {
  86. err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: false})
  87. return
  88. }
  89. var claims *auth.Claims
  90. claims, err = auth.GetUserClaims(req)
  91. if err != nil {
  92. panic("Could not determine if user is authenticated")
  93. }
  94. if claims.Privileges <= Admin {
  95. Db.Create(&rewardFund)
  96. for _, cancel := range cancellations {
  97. cancel()
  98. }
  99. go InitializeContributionStreams()
  100. for _, bonus := range fund.Bonuses {
  101. bonus.RewardFundID = rewardFund.ID
  102. bonuses = append(bonuses, bonus)
  103. }
  104. Db.Create(&bonuses)
  105. err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: true})
  106. if err != nil {
  107. panic("Could not create response for created reward fund")
  108. }
  109. } else {
  110. resp.WriteHeader(403)
  111. }
  112. }