The backend for the project formerly known as signet, now known as beignet.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.5 KiB

  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", "fund_wallet", "memo", "min_contribution").First(&fund, cont.RewardFund)
  27. if cont.Amount < fund.MinContribution || !strings.HasPrefix(cont.PrivateKey, "S") {
  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. }