The backend for the project formerly known as signet, now known as beignet.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

102 Zeilen
2.4 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. IssuerWallet: fund.IssuerWallet,
  27. Memo: fund.Memo,
  28. Price: 0,
  29. AmountGoal: fund.AmountGoal,
  30. MinContribution: fund.MinContribution,
  31. Title: fund.Title,
  32. Description: fund.Description,
  33. Contributions: nil,
  34. }
  35. offerReq := horizonclient.OfferRequest{
  36. Seller: rewardFund.IssuerWallet,
  37. Selling: fmt.Sprintf("%s:%s", rewardFund.Asset, rewardFund.IssuerWallet),
  38. Buying: fmt.Sprintf("XLM:%s", rewardFund.IssuerWallet),
  39. Order: horizonclient.OrderDesc,
  40. }
  41. op, err := client.Offers(offerReq)
  42. if err != nil {
  43. panic("Could not get offers")
  44. }
  45. offers := op.Embedded.Records
  46. var price float64
  47. if len(offers) == 1 {
  48. price, err = strconv.ParseFloat(op.Embedded.Records[0].Price, 64)
  49. if err != nil {
  50. panic("Could not parse price to float")
  51. }
  52. rewardFund.Price = price
  53. } else {
  54. var maxOffers float64 = 0
  55. var correctOffer horizon.Offer
  56. for _, o := range op.Embedded.Records {
  57. parsedAmt, err := strconv.ParseFloat(o.Amount, 64)
  58. if err != nil {
  59. panic("Could not parse amount to float")
  60. }
  61. if parsedAmt > maxOffers {
  62. correctOffer = o
  63. maxOffers = parsedAmt
  64. }
  65. }
  66. price, err = strconv.ParseFloat(correctOffer.Price, 64)
  67. if err != nil {
  68. panic("Could not parse price to float")
  69. }
  70. rewardFund.Price = price
  71. }
  72. var claims *auth.Claims
  73. claims, err = auth.GetUserClaims(req)
  74. if err != nil {
  75. panic("Could not determine if user is authenticated")
  76. }
  77. if claims.Privileges <= Admin {
  78. Db.Create(&rewardFund)
  79. for _, bonus := range fund.Bonuses {
  80. bonus.RewardFundID = rewardFund.ID
  81. bonuses = append(bonuses, bonus)
  82. }
  83. Db.Create(&bonuses)
  84. err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: true})
  85. if err != nil {
  86. panic("Could not create response for created reward fund")
  87. }
  88. } else {
  89. resp.WriteHeader(403)
  90. }
  91. }