@@ -8,11 +8,18 @@ import ( | |||||
) | ) | ||||
type Bonus struct { | type Bonus struct { | ||||
gorm.Model | |||||
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 { | |||||
gorm.Model | |||||
Name string `json:"name"` | |||||
Funds []RewardFund `json:"funds"` | |||||
} | |||||
type RewardFund struct { | type RewardFund struct { | ||||
gorm.Model | gorm.Model | ||||
Asset string `json:"asset"` | Asset string `json:"asset"` | ||||
@@ -26,9 +33,13 @@ type RewardFund struct { | |||||
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"` | ||||
} | } | ||||
type QueueRewardFund struct { | |||||
} | |||||
type Contribution struct { | type Contribution struct { | ||||
gorm.Model | gorm.Model | ||||
Wallet string `json:"wallet"` | Wallet string `json:"wallet"` | ||||
@@ -70,7 +81,10 @@ func InitializeDatabase() { | |||||
viper.GetInt("database.port"), | viper.GetInt("database.port"), | ||||
viper.GetString("database.ssl")) | viper.GetString("database.ssl")) | ||||
Db, err = gorm.Open(postgres.Open(dcs), &gorm.Config{}) | 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 { | if err != nil { | ||||
panic("Could not migrate database") | panic("Could not migrate database") | ||||
} | } | ||||
@@ -61,7 +61,7 @@ func Contribute(resp http.ResponseWriter, req *http.Request) { | |||||
BaseFee: txnbuild.MinBaseFee, | BaseFee: txnbuild.MinBaseFee, | ||||
Memo: txnbuild.Memo(txnbuild.MemoText(fund.Memo)), | Memo: txnbuild.Memo(txnbuild.MemoText(fund.Memo)), | ||||
Preconditions: txnbuild.Preconditions{ | Preconditions: txnbuild.Preconditions{ | ||||
TimeBounds: txnbuild.NewInfiniteTimeout(), | |||||
TimeBounds: txnbuild.NewTimeout(15), | |||||
}, | }, | ||||
}) | }) | ||||
if err != nil { | if err != nil { | ||||
@@ -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") | |||||
} | |||||
} |
@@ -37,6 +37,7 @@ 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, | |||||
} | } | ||||
offerReq := horizonclient.OfferRequest{ | offerReq := horizonclient.OfferRequest{ | ||||
@@ -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") | |||||
} | |||||
} |
@@ -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") | |||||
} | |||||
} |
@@ -17,7 +17,7 @@ var err error | |||||
// TODO: beignet.io | // TODO: beignet.io | ||||
func main() { | func main() { | ||||
viper.SetConfigFile("config.production.json") // TODO: change this for deployment | |||||
viper.SetConfigFile("config.production.json") | |||||
err = viper.ReadInConfig() | err = viper.ReadInConfig() | ||||
if err != nil { | if err != nil { | ||||
panic("Could not read in Viper config") | panic("Could not read in Viper config") | ||||
@@ -31,9 +31,12 @@ func main() { | |||||
router.HandleFunc("/GetRewardFunds", endpoints.GetRewardFunds) | router.HandleFunc("/GetRewardFunds", endpoints.GetRewardFunds) | ||||
router.HandleFunc("/GetRewardFund", endpoints.GetRewardFund) | router.HandleFunc("/GetRewardFund", endpoints.GetRewardFund) | ||||
router.HandleFunc("/GetContributions", endpoints.GetContributions) | router.HandleFunc("/GetContributions", endpoints.GetContributions) | ||||
router.HandleFunc("/CreateQueue", endpoints.CreateQueue) | |||||
router.HandleFunc("/GetQueues", endpoints.GetQueues) | |||||
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) | |||||
router.HandleFunc("/GetBalance", endpoints.GetBalance) | |||||
router.HandleFunc("/Contribute", endpoints.Contribute) | router.HandleFunc("/Contribute", endpoints.Contribute) | ||||
router.HandleFunc("/ContributorStream", endpoints.ContributorStream) | router.HandleFunc("/ContributorStream", endpoints.ContributorStream) | ||||
router.HandleFunc("/Login", endpoints.Login) | router.HandleFunc("/Login", endpoints.Login) | ||||