The backend for the project formerly known as signet, now known as beignet.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

62 linhas
1.7 KiB

  1. package endpoints
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. . "github.com/imosed/signet/data"
  8. "github.com/imosed/signet/utils"
  9. "github.com/rs/zerolog/log"
  10. "github.com/stellar/go/keypair"
  11. "github.com/stellar/go/txnbuild"
  12. )
  13. type ContributeRequest struct {
  14. PrivateKey string `json:"privateKey"`
  15. Amount float64 `json:"amount"`
  16. RewardFund uint `json:"rewardFund"`
  17. }
  18. func Contribute(resp http.ResponseWriter, req *http.Request) {
  19. var cont ContributeRequest
  20. err := json.NewDecoder(req.Body).Decode(&cont)
  21. if err != nil {
  22. log.Error().Err(err).Msg("Could not read in contribution")
  23. return
  24. }
  25. var fund RewardFund
  26. Db.Table("reward_funds").Select("id", "deleted_at", "fund_wallet", "memo", "min_contribution").First(&fund, cont.RewardFund)
  27. if cont.Amount < fund.MinContribution || !strings.HasPrefix(cont.PrivateKey, "S") || !fund.DeletedAt.Time.IsZero() {
  28. err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: false})
  29. if err != nil {
  30. log.Error().Err(err).Msg("Could not deliver unsuccessful contribution response")
  31. }
  32. return
  33. }
  34. var result SuccessResponse
  35. source := keypair.MustParseFull(cont.PrivateKey)
  36. result.Success, err = utils.SendAsset(source, txnbuild.Memo(txnbuild.MemoText(fund.Memo)), []txnbuild.Operation{
  37. &txnbuild.Payment{
  38. Destination: fund.FundWallet,
  39. Amount: fmt.Sprintf("%f", cont.Amount),
  40. Asset: txnbuild.NativeAsset{},
  41. SourceAccount: source.Address(),
  42. },
  43. })
  44. if err != nil {
  45. log.Error().Err(err).Msg("Could not send asset in contribution")
  46. return
  47. }
  48. err = json.NewEncoder(resp).Encode(&result)
  49. if err != nil {
  50. log.Error().Err(err).Msg("Could not create response for new contribution")
  51. }
  52. }