|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package endpoints
-
- import (
- "encoding/json"
- "fmt"
- "net/http"
- "strings"
-
- . "github.com/imosed/signet/data"
- "github.com/imosed/signet/utils"
- "github.com/rs/zerolog/log"
- "github.com/stellar/go/keypair"
- "github.com/stellar/go/txnbuild"
- )
-
- type ContributeRequest struct {
- PrivateKey string `json:"privateKey"`
- Amount float64 `json:"amount"`
- RewardFund uint `json:"rewardFund"`
- }
-
- func Contribute(resp http.ResponseWriter, req *http.Request) {
- var cont ContributeRequest
- err := json.NewDecoder(req.Body).Decode(&cont)
- if err != nil {
- log.Error().Err(err).Msg("Could not read in contribution")
- return
- }
-
- var fund RewardFund
-
- Db.Table("reward_funds").Select("id", "deleted_at", "fund_wallet", "memo", "min_contribution").First(&fund, cont.RewardFund)
-
- if cont.Amount < fund.MinContribution || !strings.HasPrefix(cont.PrivateKey, "S") || !fund.DeletedAt.Time.IsZero() {
- err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: false})
- if err != nil {
- log.Error().Err(err).Msg("Could not deliver unsuccessful contribution response")
- }
- return
- }
-
- var result SuccessResponse
- source := keypair.MustParseFull(cont.PrivateKey)
- result.Success, err = utils.SendAsset(source, txnbuild.Memo(txnbuild.MemoText(fund.Memo)), []txnbuild.Operation{
- &txnbuild.Payment{
- Destination: fund.FundWallet,
- Amount: fmt.Sprintf("%f", cont.Amount),
- Asset: txnbuild.NativeAsset{},
- SourceAccount: source.Address(),
- },
- })
- if err != nil {
- log.Error().Err(err).Msg("Could not send asset in contribution")
- return
- }
-
- err = json.NewEncoder(resp).Encode(&result)
- if err != nil {
- log.Error().Err(err).Msg("Could not create response for new contribution")
- }
- }
|