The backend for the project formerly known as signet, now known as beignet.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

100 行
2.8 KiB

  1. package endpoints
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "github.com/imosed/signet/client"
  8. . "github.com/imosed/signet/data"
  9. "github.com/rs/zerolog/log"
  10. "github.com/stellar/go/clients/horizonclient"
  11. "github.com/stellar/go/keypair"
  12. "github.com/stellar/go/network"
  13. "github.com/stellar/go/protocols/horizon"
  14. "github.com/stellar/go/txnbuild"
  15. )
  16. type ContributeRequest struct {
  17. PrivateKey string `json:"privateKey"`
  18. Amount float64 `json:"amount"`
  19. RewardFund uint `json:"rewardFund"`
  20. }
  21. func Contribute(resp http.ResponseWriter, req *http.Request) {
  22. var cont ContributeRequest
  23. err := json.NewDecoder(req.Body).Decode(&cont)
  24. if err != nil {
  25. log.Error().Err(err).Msg("Could not read in contribution")
  26. return
  27. }
  28. var fund RewardFund
  29. Db.Table("reward_funds").Select("id", "deleted_at", "fund_wallet", "memo", "min_contribution").First(&fund, cont.RewardFund)
  30. if cont.Amount < fund.MinContribution || !strings.HasPrefix(cont.PrivateKey, "S") || !fund.DeletedAt.Time.IsZero() {
  31. err = json.NewEncoder(resp).Encode(&SuccessResponse{Success: false})
  32. if err != nil {
  33. log.Error().Err(err).Msg("Could not deliver unsuccessful contribution response")
  34. }
  35. return
  36. }
  37. source := keypair.MustParseFull(cont.PrivateKey)
  38. sourceReq := horizonclient.AccountRequest{AccountID: source.Address()}
  39. var sourceAcct horizon.Account
  40. sourceAcct, err = client.SignetClient.AccountDetail(sourceReq)
  41. if err != nil {
  42. log.Error().Err(err).Msg("Could not get account details from Horizon SignetClient")
  43. return
  44. }
  45. tx, err := txnbuild.NewTransaction(
  46. txnbuild.TransactionParams{
  47. SourceAccount: &sourceAcct,
  48. IncrementSequenceNum: true,
  49. Operations: []txnbuild.Operation{
  50. &txnbuild.Payment{
  51. Destination: fund.FundWallet,
  52. Amount: fmt.Sprintf("%f", cont.Amount),
  53. Asset: txnbuild.NativeAsset{},
  54. SourceAccount: source.Address(),
  55. },
  56. },
  57. BaseFee: txnbuild.MinBaseFee,
  58. Memo: txnbuild.Memo(txnbuild.MemoText(fund.Memo)),
  59. Preconditions: txnbuild.Preconditions{
  60. TimeBounds: txnbuild.NewTimeout(15),
  61. },
  62. })
  63. if err != nil {
  64. log.Error().Err(err).Msg("Could not create contribution transaction")
  65. return
  66. }
  67. tx, err = tx.Sign(network.TestNetworkPassphrase, source)
  68. if err != nil {
  69. log.Error().Err(err).Msg("Could not sign contribution transaction")
  70. return
  71. }
  72. var response horizon.Transaction
  73. response, err = client.SignetClient.SubmitTransaction(tx)
  74. if err != nil {
  75. log.Error().Err(err).Msg("Could not submit contribution transaction")
  76. return
  77. }
  78. log.Info().Msg(fmt.Sprintf("Successful Transaction: { Ledger: %d, Hash: %s }", response.Ledger, response.Hash))
  79. var result SuccessResponse
  80. result.Success = response.Successful && err == nil
  81. err = json.NewEncoder(resp).Encode(&result)
  82. if err != nil {
  83. log.Error().Err(err).Msg("Could not create response for new contribution")
  84. }
  85. }