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 1 an
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package endpoints
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. . "github.com/imosed/signet/data"
  8. "github.com/stellar/go/clients/horizonclient"
  9. "github.com/stellar/go/keypair"
  10. "github.com/stellar/go/network"
  11. "github.com/stellar/go/protocols/horizon"
  12. "github.com/stellar/go/txnbuild"
  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.NewTimeout(15),
  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. }