| @@ -5,65 +5,63 @@ import ( | |||||
| "github.com/spf13/viper" | "github.com/spf13/viper" | ||||
| "gorm.io/driver/postgres" | "gorm.io/driver/postgres" | ||||
| "gorm.io/gorm" | "gorm.io/gorm" | ||||
| "time" | |||||
| ) | ) | ||||
| type ModelBase struct { | |||||
| ID uint `gorm:"primarykey" json:"id"` | |||||
| CreatedAt time.Time `json:"createdAt"` | |||||
| UpdatedAt time.Time `json:"updatedAt"` | |||||
| DeletedAt gorm.DeletedAt `gorm:"index" json:"deletedAt"` | |||||
| } | |||||
| type Bonus struct { | type Bonus struct { | ||||
| gorm.Model | |||||
| ModelBase | |||||
| Goal float64 `json:"goal"` | Goal float64 `json:"goal"` | ||||
| Percent float64 `json:"percent"` | Percent float64 `json:"percent"` | ||||
| RewardFundID uint `json:"rewardFundID"` | RewardFundID uint `json:"rewardFundID"` | ||||
| } | } | ||||
| type Queue struct { | type Queue struct { | ||||
| gorm.Model | |||||
| Name string `json:"name"` | |||||
| Funds []RewardFund `json:"funds"` | |||||
| ModelBase | |||||
| Name string `gorm:"type:varchar(50)" json:"name"` | |||||
| Orders []QueueOrder `json:"order"` | |||||
| } | } | ||||
| type RewardFund struct { | type RewardFund struct { | ||||
| gorm.Model | |||||
| ModelBase | |||||
| Asset string `json:"asset"` | Asset string `json:"asset"` | ||||
| FundWallet string `json:"fundWallet"` | FundWallet string `json:"fundWallet"` | ||||
| SellingWallet string `json:"sellingWallet"` | SellingWallet string `json:"sellingWallet"` | ||||
| IssuerWallet string `json:"issuerWallet"` | IssuerWallet string `json:"issuerWallet"` | ||||
| Memo string `json:"memo"` | Memo string `json:"memo"` | ||||
| Price float64 `json:"price"` | |||||
| Price float64 `gorm:"type:decimal(19,7)" json:"price"` | |||||
| AmountAvailable float64 `gorm:"type:decimal(19,7)" json:"amountAvailable"` | AmountAvailable float64 `gorm:"type:decimal(19,7)" json:"amountAvailable"` | ||||
| MinContribution float64 `gorm:"type:decimal(19,7)" json:"minContribution"` | MinContribution float64 `gorm:"type:decimal(19,7)" json:"minContribution"` | ||||
| Contributions []Contribution `json:"contributions"` | Contributions []Contribution `json:"contributions"` | ||||
| Title string `gorm:"type:varchar(50)" json:"title"` | Title string `gorm:"type:varchar(50)" json:"title"` | ||||
| Description string `gorm:"type:text" json:"description"` | Description string `gorm:"type:text" json:"description"` | ||||
| QueueID uint `json:"queueID"` | |||||
| Bonuses []Bonus `json:"bonuses"` | Bonuses []Bonus `json:"bonuses"` | ||||
| QueueOrder QueueOrder `json:"queueOrder"` | |||||
| } | } | ||||
| type QueueRewardFund struct { | |||||
| type QueueOrder struct { | |||||
| ModelBase | |||||
| QueueID uint `json:"queueID"` | |||||
| RewardFundID uint `json:"rewardFundID"` | |||||
| Order uint16 `gorm:"type:smallint" json:"order"` | |||||
| } | } | ||||
| type Contribution struct { | type Contribution struct { | ||||
| gorm.Model | |||||
| Wallet string `json:"wallet"` | |||||
| Amount float64 `gorm:"type:decimal(19,7)" json:"amount"` | |||||
| TransactionID string `json:"transactionID"` | |||||
| RewardFundID uint `json:"rewardFundID"` | |||||
| Tags []AppliedTag `json:"tags"` | |||||
| } | |||||
| type Tag struct { | |||||
| gorm.Model | |||||
| Description string `json:"description"` | |||||
| Active bool `json:"active"` | |||||
| Contribution AppliedTag `json:"contribution"` | |||||
| } | |||||
| type AppliedTag struct { | |||||
| gorm.Model | |||||
| TagID uint `json:"tagID"` | |||||
| ContributionID uint `json:"contributionID"` | |||||
| ModelBase | |||||
| Wallet string `json:"wallet"` | |||||
| Amount float64 `gorm:"type:decimal(19,7)" json:"amount"` | |||||
| TransactionID string `json:"transactionID"` | |||||
| RewardFundID uint `json:"rewardFundID"` | |||||
| } | } | ||||
| type User struct { | type User struct { | ||||
| gorm.Model | |||||
| ModelBase | |||||
| Username string `json:"username"` | Username string `json:"username"` | ||||
| Password string `json:"password"` | Password string `json:"password"` | ||||
| Privileges uint `json:"admin"` | Privileges uint `json:"admin"` | ||||
| @@ -84,7 +82,7 @@ func InitializeDatabase() { | |||||
| if err != nil { | if err != nil { | ||||
| panic("Could not open database") | panic("Could not open database") | ||||
| } | } | ||||
| err = Db.AutoMigrate(User{}, Queue{}, RewardFund{}, Contribution{}, Bonus{}) | |||||
| err = Db.AutoMigrate(User{}, Queue{}, RewardFund{}, QueueOrder{}, Contribution{}, Bonus{}) | |||||
| if err != nil { | if err != nil { | ||||
| panic("Could not migrate database") | panic("Could not migrate database") | ||||
| } | } | ||||
| @@ -9,7 +9,6 @@ import ( | |||||
| "github.com/stellar/go/protocols/horizon" | "github.com/stellar/go/protocols/horizon" | ||||
| "github.com/stellar/go/protocols/horizon/operations" | "github.com/stellar/go/protocols/horizon/operations" | ||||
| "golang.org/x/net/context" | "golang.org/x/net/context" | ||||
| "gorm.io/gorm" | |||||
| "net/http" | "net/http" | ||||
| "strconv" | "strconv" | ||||
| "strings" | "strings" | ||||
| @@ -74,9 +73,7 @@ func InitializeContributionStreams() { | |||||
| Db.Model(&RewardFund{}).Where("id = ?", fund.ID).Update("amount_available", newAmt) | Db.Model(&RewardFund{}).Where("id = ?", fund.ID).Update("amount_available", newAmt) | ||||
| contribution := Contribution{ | contribution := Contribution{ | ||||
| Model: gorm.Model{ | |||||
| CreatedAt: tx.LedgerCloseTime, | |||||
| }, | |||||
| ModelBase: ModelBase{CreatedAt: tx.LedgerCloseTime}, | |||||
| Wallet: payment.From, | Wallet: payment.From, | ||||
| Amount: amt, | Amount: amt, | ||||
| TransactionID: payment.GetTransactionHash(), | TransactionID: payment.GetTransactionHash(), | ||||
| @@ -11,12 +11,25 @@ import ( | |||||
| "strconv" | "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 { | type SuccessResponse struct { | ||||
| Success bool `json:"success"` | Success bool `json:"success"` | ||||
| } | } | ||||
| func CreateRewardFund(resp http.ResponseWriter, req *http.Request) { | func CreateRewardFund(resp http.ResponseWriter, req *http.Request) { | ||||
| var fund RewardFund | |||||
| var fund CreateRewardFundRequest | |||||
| dec := json.NewDecoder(req.Body) | dec := json.NewDecoder(req.Body) | ||||
| err := dec.Decode(&fund) | err := dec.Decode(&fund) | ||||
| if err != nil { | if err != nil { | ||||
| @@ -37,9 +50,14 @@ func CreateRewardFund(resp http.ResponseWriter, req *http.Request) { | |||||
| Title: fund.Title, | Title: fund.Title, | ||||
| Description: fund.Description, | Description: fund.Description, | ||||
| Contributions: nil, | Contributions: nil, | ||||
| QueueID: fund.QueueID, | |||||
| } | } | ||||
| 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{ | offerReq := horizonclient.OfferRequest{ | ||||
| Seller: rewardFund.SellingWallet, | Seller: rewardFund.SellingWallet, | ||||
| Selling: fmt.Sprintf("%s:%s", rewardFund.Asset, rewardFund.IssuerWallet), | Selling: fmt.Sprintf("%s:%s", rewardFund.Asset, rewardFund.IssuerWallet), | ||||
| @@ -104,6 +122,7 @@ func CreateRewardFund(resp http.ResponseWriter, req *http.Request) { | |||||
| if claims.Privileges <= Admin { | if claims.Privileges <= Admin { | ||||
| Db.Create(&rewardFund) | Db.Create(&rewardFund) | ||||
| Db.Create(&joinTable) | |||||
| for _, cancel := range cancellations { | for _, cancel := range cancellations { | ||||
| cancel() | cancel() | ||||
| @@ -0,0 +1,41 @@ | |||||
| package endpoints | |||||
| import ( | |||||
| "encoding/json" | |||||
| . "github.com/imosed/signet/data" | |||||
| "net/http" | |||||
| ) | |||||
| type QueueMember struct { | |||||
| Asset string `json:"asset"` | |||||
| Title string `json:"title"` | |||||
| } | |||||
| type GetQueueMembersRequest struct { | |||||
| ID uint `json:"id"` | |||||
| } | |||||
| type GetQueueMembersResponse struct { | |||||
| Members []QueueMember `json:"members"` | |||||
| } | |||||
| func GetQueueMembers(w http.ResponseWriter, r *http.Request) { | |||||
| var req GetQueueMembersRequest | |||||
| err := json.NewDecoder(r.Body).Decode(&req) | |||||
| if err != nil { | |||||
| panic("Could not decode body") | |||||
| } | |||||
| var members []QueueMember | |||||
| Db.Table("queue_orders qo").Select("reward_fund_id, asset, title").Where("queue_id = ?", req.ID). | |||||
| Joins("inner join reward_funds rf on qo.reward_fund_id = rf.id"). | |||||
| Scan(&members) | |||||
| var resp GetQueueMembersResponse | |||||
| resp.Members = members | |||||
| err = json.NewEncoder(w).Encode(resp) | |||||
| if err != nil { | |||||
| panic("Could not deliver response") | |||||
| } | |||||
| } | |||||
| @@ -33,6 +33,7 @@ func main() { | |||||
| router.HandleFunc("/GetContributions", endpoints.GetContributions) | router.HandleFunc("/GetContributions", endpoints.GetContributions) | ||||
| router.HandleFunc("/CreateQueue", endpoints.CreateQueue) | router.HandleFunc("/CreateQueue", endpoints.CreateQueue) | ||||
| router.HandleFunc("/GetQueues", endpoints.GetQueues) | router.HandleFunc("/GetQueues", endpoints.GetQueues) | ||||
| router.HandleFunc("/GetQueueMembers", endpoints.GetQueueMembers) | |||||
| router.HandleFunc("/CreateRewardFund", endpoints.CreateRewardFund) | router.HandleFunc("/CreateRewardFund", endpoints.CreateRewardFund) | ||||
| router.HandleFunc("/CloseRewardFund", endpoints.CloseRewardFund) | router.HandleFunc("/CloseRewardFund", endpoints.CloseRewardFund) | ||||
| //router.HandleFunc("/SubmitFund", endpoints.SubmitFund) | //router.HandleFunc("/SubmitFund", endpoints.SubmitFund) | ||||