Browse Source

Get some infrastructure in place to queue funds

master
Jared 1 year ago
parent
commit
9816c7090e
7 changed files with 150 additions and 4 deletions
  1. +15
    -1
      data/context.go
  2. +1
    -1
      endpoints/contribute.go
  3. +45
    -0
      endpoints/createqueue.go
  4. +1
    -0
      endpoints/createrewardfund.go
  5. +61
    -0
      endpoints/getbalance.go
  6. +22
    -0
      endpoints/getqueues.go
  7. +5
    -2
      main.go

+ 15
- 1
data/context.go View File

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


+ 1
- 1
endpoints/contribute.go View File

@@ -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 {


+ 45
- 0
endpoints/createqueue.go View File

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

+ 1
- 0
endpoints/createrewardfund.go View File

@@ -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{


+ 61
- 0
endpoints/getbalance.go View File

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

+ 22
- 0
endpoints/getqueues.go View File

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

+ 5
- 2
main.go View File

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


Loading…
Cancel
Save