The backend for the project formerly known as signet, now known as beignet.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

createrewardfund.go 3.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. }
  36. offerReq := horizonclient.OfferRequest{
  37. Seller: rewardFund.SellingWallet,
  38. Selling: fmt.Sprintf("%s:%s", rewardFund.Asset, rewardFund.IssuerWallet),
  39. Order: horizonclient.OrderDesc,
  40. }
  41. url, _ := offerReq.BuildURL()
  42. fmt.Println(url)
  43. op, err := client.Offers(offerReq)
  44. if err != nil {
  45. panic("Could not get offers")
  46. }
  47. offers := op.Embedded.Records
  48. var price float64
  49. var amt float64
  50. if len(offers) == 1 {
  51. price, err = strconv.ParseFloat(op.Embedded.Records[0].Price, 64)
  52. if err != nil {
  53. panic("Could not parse price to float")
  54. }
  55. amt, err = strconv.ParseFloat(op.Embedded.Records[0].Amount, 64)
  56. if err != nil {
  57. panic("Could not parse amount to float")
  58. }
  59. rewardFund.Price = price
  60. rewardFund.AmountAvailable = amt
  61. } else if len(offers) > 1 {
  62. var maxOffers float64 = 0
  63. var correctOffer horizon.Offer
  64. for _, o := range op.Embedded.Records {
  65. parsedAmt, err := strconv.ParseFloat(o.Amount, 64)
  66. if err != nil {
  67. panic("Could not parse amount to float")
  68. }
  69. if parsedAmt > maxOffers {
  70. correctOffer = o
  71. maxOffers = parsedAmt
  72. }
  73. }
  74. price, err = strconv.ParseFloat(correctOffer.Price, 64)
  75. if err != nil {
  76. panic("Could not parse price to float")
  77. }
  78. rewardFund.Price = price
  79. amt, err = strconv.ParseFloat(correctOffer.Amount, 64)
  80. if err != nil {
  81. panic("Could not parse amount to float")
  82. }
  83. rewardFund.AmountAvailable = amt
  84. } else {
  85. err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: false})
  86. return
  87. }
  88. var claims *auth.Claims
  89. claims, err = auth.GetUserClaims(req)
  90. if err != nil {
  91. panic("Could not determine if user is authenticated")
  92. }
  93. if claims.Privileges <= Admin {
  94. Db.Create(&rewardFund)
  95. for _, cancel := range cancellations {
  96. cancel()
  97. }
  98. go InitializeContributionStreams()
  99. for _, bonus := range fund.Bonuses {
  100. bonus.RewardFundID = rewardFund.ID
  101. bonuses = append(bonuses, bonus)
  102. }
  103. Db.Create(&bonuses)
  104. err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: true})
  105. if err != nil {
  106. panic("Could not create response for created reward fund")
  107. }
  108. } else {
  109. resp.WriteHeader(403)
  110. }
  111. }