The backend for the project formerly known as signet, now known as beignet.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

contribute.go 2.6 KiB

il y a 2 ans
il y a 2 ans
il y a 2 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package endpoints
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. . "github.com/imosed/signet/data"
  6. "github.com/stellar/go/clients/horizonclient"
  7. "github.com/stellar/go/keypair"
  8. "github.com/stellar/go/network"
  9. "github.com/stellar/go/protocols/horizon"
  10. "github.com/stellar/go/txnbuild"
  11. "net/http"
  12. "strings"
  13. )
  14. type ContributeRequest struct {
  15. PrivateKey string `json:"privateKey"`
  16. Amount float64 `json:"amount"`
  17. RewardFund uint `json:"rewardFund"`
  18. }
  19. func Contribute(resp http.ResponseWriter, req *http.Request) {
  20. var cont ContributeRequest
  21. err := json.NewDecoder(req.Body).Decode(&cont)
  22. if err != nil {
  23. panic("Could not read in contribution")
  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. panic("Could not deliver unsuccessful contribution response")
  31. }
  32. return
  33. }
  34. source := keypair.MustParseFull(cont.PrivateKey)
  35. sourceReq := horizonclient.AccountRequest{AccountID: source.Address()}
  36. var sourceAcct horizon.Account
  37. sourceAcct, err = client.AccountDetail(sourceReq)
  38. if err != nil {
  39. panic("Horizon client: could not get account details")
  40. }
  41. tx, err := txnbuild.NewTransaction(
  42. txnbuild.TransactionParams{
  43. SourceAccount: &sourceAcct,
  44. IncrementSequenceNum: true,
  45. Operations: []txnbuild.Operation{
  46. &txnbuild.Payment{
  47. Destination: fund.FundWallet,
  48. Amount: fmt.Sprintf("%f", cont.Amount),
  49. Asset: txnbuild.NativeAsset{},
  50. SourceAccount: source.Address(),
  51. },
  52. },
  53. BaseFee: txnbuild.MinBaseFee,
  54. Memo: txnbuild.Memo(txnbuild.MemoText(fund.Memo)),
  55. Preconditions: txnbuild.Preconditions{
  56. TimeBounds: txnbuild.NewInfiniteTimeout(),
  57. },
  58. })
  59. if err != nil {
  60. panic("Could not create contribution transaction")
  61. }
  62. tx, err = tx.Sign(network.TestNetworkPassphrase, source)
  63. if err != nil {
  64. panic("Could not sign contribution transaction")
  65. }
  66. var response horizon.Transaction
  67. response, err = client.SubmitTransaction(tx)
  68. if err != nil {
  69. panic("Could not submit contribution transaction")
  70. }
  71. fmt.Println("Successful Transaction:")
  72. fmt.Println("Ledger:", response.Ledger)
  73. fmt.Println("Hash:", response.Hash)
  74. var result SuccessResponse
  75. result.Success = response.Successful && err == nil
  76. err = json.NewEncoder(resp).Encode(&result)
  77. if err != nil {
  78. panic("Could not create response for new contribution")
  79. }
  80. }