From 9816c7090ecf85794491181ab5d95b2566075948 Mon Sep 17 00:00:00 2001 From: Jared Date: Thu, 5 Jan 2023 00:35:37 -0500 Subject: [PATCH] Get some infrastructure in place to queue funds --- data/context.go | 16 ++++++++- endpoints/contribute.go | 2 +- endpoints/createqueue.go | 45 ++++++++++++++++++++++++++ endpoints/createrewardfund.go | 1 + endpoints/getbalance.go | 61 +++++++++++++++++++++++++++++++++++ endpoints/getqueues.go | 22 +++++++++++++ main.go | 7 ++-- 7 files changed, 150 insertions(+), 4 deletions(-) create mode 100644 endpoints/createqueue.go create mode 100644 endpoints/getbalance.go create mode 100644 endpoints/getqueues.go diff --git a/data/context.go b/data/context.go index 3fbb98f..9ca8eec 100644 --- a/data/context.go +++ b/data/context.go @@ -8,11 +8,18 @@ import ( ) type Bonus struct { + gorm.Model 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"` +} + type RewardFund struct { gorm.Model Asset string `json:"asset"` @@ -26,9 +33,13 @@ type RewardFund struct { 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"` } +type QueueRewardFund struct { +} + type Contribution struct { gorm.Model Wallet string `json:"wallet"` @@ -70,7 +81,10 @@ func InitializeDatabase() { viper.GetInt("database.port"), viper.GetString("database.ssl")) Db, err = gorm.Open(postgres.Open(dcs), &gorm.Config{}) - err = Db.AutoMigrate(User{}, RewardFund{}, Contribution{}, Bonus{}) + if err != nil { + panic("Could not open database") + } + err = Db.AutoMigrate(User{}, Queue{}, RewardFund{}, Contribution{}, Bonus{}) if err != nil { panic("Could not migrate database") } diff --git a/endpoints/contribute.go b/endpoints/contribute.go index 8cbe1c7..18f20d3 100644 --- a/endpoints/contribute.go +++ b/endpoints/contribute.go @@ -61,7 +61,7 @@ func Contribute(resp http.ResponseWriter, req *http.Request) { BaseFee: txnbuild.MinBaseFee, Memo: txnbuild.Memo(txnbuild.MemoText(fund.Memo)), Preconditions: txnbuild.Preconditions{ - TimeBounds: txnbuild.NewInfiniteTimeout(), + TimeBounds: txnbuild.NewTimeout(15), }, }) if err != nil { diff --git a/endpoints/createqueue.go b/endpoints/createqueue.go new file mode 100644 index 0000000..2a5e92d --- /dev/null +++ b/endpoints/createqueue.go @@ -0,0 +1,45 @@ +package endpoints + +import ( + "encoding/json" + . "github.com/imosed/signet/data" + "net/http" +) + +type CreateQueueRequest struct { + Name string `json:"name"` +} + +type CreateQueueResponse struct { + ID uint `json:"id"` +} + +func CreateQueue(w http.ResponseWriter, r *http.Request) { + var req CreateQueueRequest + err := json.NewDecoder(r.Body).Decode(&req) + if err != nil { + panic("Could not decode body") + } + + var specificQueue Queue + Db.Table("queues").First(&specificQueue, "name = ?", req.Name) + + var resp CreateQueueResponse + + if specificQueue.ID != 0 { + resp.ID = specificQueue.ID + } else { + queue := Queue{ + Name: req.Name, + } + + Db.Create(&queue) + + resp.ID = queue.ID + } + + err = json.NewEncoder(w).Encode(resp) + if err != nil { + panic("Could not deliver response") + } +} diff --git a/endpoints/createrewardfund.go b/endpoints/createrewardfund.go index edd8fe4..4868a5a 100644 --- a/endpoints/createrewardfund.go +++ b/endpoints/createrewardfund.go @@ -37,6 +37,7 @@ func CreateRewardFund(resp http.ResponseWriter, req *http.Request) { Title: fund.Title, Description: fund.Description, Contributions: nil, + QueueID: fund.QueueID, } offerReq := horizonclient.OfferRequest{ diff --git a/endpoints/getbalance.go b/endpoints/getbalance.go new file mode 100644 index 0000000..c3d064d --- /dev/null +++ b/endpoints/getbalance.go @@ -0,0 +1,61 @@ +package endpoints + +import ( + "encoding/json" + "github.com/stellar/go/clients/horizonclient" + "github.com/stellar/go/keypair" + "github.com/stellar/go/protocols/horizon" + "net/http" + "strconv" +) + +type GetBalanceRequest struct { + SecretKey string `json:"secretKey"` +} + +type GetBalanceResponse struct { + Balance float64 `json:"balance"` +} + +func GetBalance(w http.ResponseWriter, r *http.Request) { + var req GetBalanceRequest + err := json.NewDecoder(r.Body).Decode(&req) + if err != nil { + panic("Could not decode body") + } + + var kp keypair.KP + kp, err = keypair.Parse(req.SecretKey) + if err != nil { + w.WriteHeader(404) + return + } + + acctReq := horizonclient.AccountRequest{ + AccountID: kp.Address(), + } + var acct horizon.Account + acct, err = client.AccountDetail(acctReq) + if err != nil { + panic("Could not get account data from public key") + } + + var blnce = -1.0 + for _, balance := range acct.Balances { + if balance.Asset.Type == "native" { + blnce, err = strconv.ParseFloat(balance.Balance, 64) + } + } + + if blnce == -1.0 { + panic("Could not get XLM balance") + } + + var resp GetBalanceResponse + resp.Balance = blnce + + err = json.NewEncoder(w).Encode(resp) + if err != nil { + panic("Could not deliver response") + } +} diff --git a/endpoints/getqueues.go b/endpoints/getqueues.go new file mode 100644 index 0000000..3e0a190 --- /dev/null +++ b/endpoints/getqueues.go @@ -0,0 +1,22 @@ +package endpoints + +import ( + "encoding/json" + . "github.com/imosed/signet/data" + "net/http" +) + +type GetQueuesResponse struct { + Queues []Queue `json:"queues"` +} + +func GetQueues(w http.ResponseWriter, r *http.Request) { + var resp GetQueuesResponse + + Db.Table("queues").Scan(&resp.Queues) + + err := json.NewEncoder(w).Encode(resp) + if err != nil { + panic("Could not deliver response") + } +} diff --git a/main.go b/main.go index 19e529e..be6ce40 100644 --- a/main.go +++ b/main.go @@ -17,7 +17,7 @@ var err error // TODO: beignet.io func main() { - viper.SetConfigFile("config.production.json") // TODO: change this for deployment + viper.SetConfigFile("config.production.json") err = viper.ReadInConfig() if err != nil { panic("Could not read in Viper config") @@ -31,9 +31,12 @@ func main() { router.HandleFunc("/GetRewardFunds", endpoints.GetRewardFunds) router.HandleFunc("/GetRewardFund", endpoints.GetRewardFund) router.HandleFunc("/GetContributions", endpoints.GetContributions) + router.HandleFunc("/CreateQueue", endpoints.CreateQueue) + router.HandleFunc("/GetQueues", endpoints.GetQueues) router.HandleFunc("/CreateRewardFund", endpoints.CreateRewardFund) router.HandleFunc("/CloseRewardFund", endpoints.CloseRewardFund) - router.HandleFunc("/SubmitFund", endpoints.SubmitFund) + //router.HandleFunc("/SubmitFund", endpoints.SubmitFund) + router.HandleFunc("/GetBalance", endpoints.GetBalance) router.HandleFunc("/Contribute", endpoints.Contribute) router.HandleFunc("/ContributorStream", endpoints.ContributorStream) router.HandleFunc("/Login", endpoints.Login)