Browse Source

Fix up queue stuff, not ready yet

master
Jared 1 year ago
parent
commit
8fb6e59b91
5 changed files with 91 additions and 35 deletions
  1. +27
    -29
      data/context.go
  2. +1
    -4
      endpoints/contributionstream.go
  3. +21
    -2
      endpoints/createrewardfund.go
  4. +41
    -0
      endpoints/getqueuemembers.go
  5. +1
    -0
      main.go

+ 27
- 29
data/context.go View File

@@ -5,65 +5,63 @@ import (
"github.com/spf13/viper"
"gorm.io/driver/postgres"
"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 {
gorm.Model
ModelBase
Goal float64 `json:"goal"`
Percent float64 `json:"percent"`
RewardFundID uint `json:"rewardFundID"`
}

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 {
gorm.Model
ModelBase
Asset string `json:"asset"`
FundWallet string `json:"fundWallet"`
SellingWallet string `json:"sellingWallet"`
IssuerWallet string `json:"issuerWallet"`
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"`
MinContribution float64 `gorm:"type:decimal(19,7)" json:"minContribution"`
Contributions []Contribution `json:"contributions"`
Title string `gorm:"type:varchar(50)" json:"title"`
Description string `gorm:"type:text" json:"description"`
QueueID uint `json:"queueID"`
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 {
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 {
gorm.Model
ModelBase
Username string `json:"username"`
Password string `json:"password"`
Privileges uint `json:"admin"`
@@ -84,7 +82,7 @@ func InitializeDatabase() {
if err != nil {
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 {
panic("Could not migrate database")
}


+ 1
- 4
endpoints/contributionstream.go View File

@@ -9,7 +9,6 @@ import (
"github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/protocols/horizon/operations"
"golang.org/x/net/context"
"gorm.io/gorm"
"net/http"
"strconv"
"strings"
@@ -74,9 +73,7 @@ func InitializeContributionStreams() {
Db.Model(&RewardFund{}).Where("id = ?", fund.ID).Update("amount_available", newAmt)

contribution := Contribution{
Model: gorm.Model{
CreatedAt: tx.LedgerCloseTime,
},
ModelBase: ModelBase{CreatedAt: tx.LedgerCloseTime},
Wallet: payment.From,
Amount: amt,
TransactionID: payment.GetTransactionHash(),


+ 21
- 2
endpoints/createrewardfund.go View File

@@ -11,12 +11,25 @@ import (
"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 RewardFund
var fund CreateRewardFundRequest
dec := json.NewDecoder(req.Body)
err := dec.Decode(&fund)
if err != nil {
@@ -37,9 +50,14 @@ func CreateRewardFund(resp http.ResponseWriter, req *http.Request) {
Title: fund.Title,
Description: fund.Description,
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{
Seller: rewardFund.SellingWallet,
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 {
Db.Create(&rewardFund)
Db.Create(&joinTable)

for _, cancel := range cancellations {
cancel()


+ 41
- 0
endpoints/getqueuemembers.go View File

@@ -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")
}
}

+ 1
- 0
main.go View File

@@ -33,6 +33,7 @@ func main() {
router.HandleFunc("/GetContributions", endpoints.GetContributions)
router.HandleFunc("/CreateQueue", endpoints.CreateQueue)
router.HandleFunc("/GetQueues", endpoints.GetQueues)
router.HandleFunc("/GetQueueMembers", endpoints.GetQueueMembers)
router.HandleFunc("/CreateRewardFund", endpoints.CreateRewardFund)
router.HandleFunc("/CloseRewardFund", endpoints.CloseRewardFund)
//router.HandleFunc("/SubmitFund", endpoints.SubmitFund)


Loading…
Cancel
Save