|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package endpoints
-
- import (
- "encoding/json"
- "fmt"
- "github.com/imosed/signet/auth"
- . "github.com/imosed/signet/data"
- "github.com/stellar/go/clients/horizonclient"
- "github.com/stellar/go/protocols/horizon"
- "net/http"
- "strconv"
- )
-
- type CreateRewardFundRequest struct {
- Asset string `json:"asset"`
- FundWallet string `json:"fundWallet"`
- SellingWallet string `json:"sellingWallet"`
- IssuerWallet string `json:"issuerWallet"`
- Memo string `json:"memo"`
- MinContribution float64 `gorm:"type:decimal(19,7)" json:"minContribution"`
- Title string `gorm:"type:varchar(50)" json:"title"`
- Description string `gorm:"type:text" json:"description"`
- QueueID uint `json:"queueID"`
- Bonuses []Bonus `json:"bonuses"`
- }
-
- type SuccessResponse struct {
- Success bool `json:"success"`
- }
-
- func CreateRewardFund(resp http.ResponseWriter, req *http.Request) {
- var fund CreateRewardFundRequest
- dec := json.NewDecoder(req.Body)
- err := dec.Decode(&fund)
- if err != nil {
- panic("Could not read submitted reward fund")
- }
-
- var bonuses []Bonus
-
- rewardFund := RewardFund{
- Asset: fund.Asset,
- FundWallet: fund.FundWallet,
- SellingWallet: fund.SellingWallet,
- IssuerWallet: fund.IssuerWallet,
- Memo: fund.Memo,
- Price: 0,
- AmountAvailable: 0,
- MinContribution: fund.MinContribution,
- Title: fund.Title,
- Description: fund.Description,
- Contributions: nil,
- }
-
- var fundsInQueue []RewardFund
- Db.Table("queue_reward_funds").Where("queue_id = ?", fund.QueueID).Scan(&fundsInQueue)
- next := uint16(len(fundsInQueue))
-
- joinTable := QueueOrder{QueueID: fund.QueueID, RewardFundID: rewardFund.ID, Order: next}
-
- offerReq := horizonclient.OfferRequest{
- Seller: rewardFund.SellingWallet,
- Selling: fmt.Sprintf("%s:%s", rewardFund.Asset, rewardFund.IssuerWallet),
- Order: horizonclient.OrderDesc,
- }
-
- url, _ := offerReq.BuildURL()
- fmt.Println(url)
-
- op, err := client.Offers(offerReq)
- if err != nil {
- panic("Could not get offers")
- }
- offers := op.Embedded.Records
- var price float64
- var amt float64
- if len(offers) == 1 {
- price, err = strconv.ParseFloat(op.Embedded.Records[0].Price, 64)
- if err != nil {
- panic("Could not parse price to float")
- }
- amt, err = strconv.ParseFloat(op.Embedded.Records[0].Amount, 64)
- if err != nil {
- panic("Could not parse amount to float")
- }
- rewardFund.Price = price
- rewardFund.AmountAvailable = amt
- } else if len(offers) > 1 {
- var maxOffers float64 = 0
- var correctOffer horizon.Offer
- for _, o := range op.Embedded.Records {
- parsedAmt, err := strconv.ParseFloat(o.Amount, 64)
- if err != nil {
- panic("Could not parse amount to float")
- }
- if parsedAmt > maxOffers {
- correctOffer = o
- maxOffers = parsedAmt
- }
- }
- price, err = strconv.ParseFloat(correctOffer.Price, 64)
- if err != nil {
- panic("Could not parse price to float")
- }
- rewardFund.Price = price
-
- amt, err = strconv.ParseFloat(correctOffer.Amount, 64)
- if err != nil {
- panic("Could not parse amount to float")
- }
- rewardFund.AmountAvailable = amt
- } else {
- err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: false})
- return
- }
-
- var claims *auth.Claims
- claims, err = auth.GetUserClaims(req)
- if err != nil {
- panic("Could not determine if user is authenticated")
- }
-
- if claims.Privileges <= Admin {
- Db.Create(&rewardFund)
- Db.Create(&joinTable)
-
- for _, cancel := range cancellations {
- cancel()
- }
- go InitializeContributionStreams()
-
- for _, bonus := range fund.Bonuses {
- bonus.RewardFundID = rewardFund.ID
- bonuses = append(bonuses, bonus)
- }
- Db.Create(&bonuses)
-
- err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: true})
- if err != nil {
- panic("Could not create response for created reward fund")
- }
- } else {
- resp.WriteHeader(403)
- }
- }
|