The backend for the project formerly known as signet, now known as beignet.
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

contributionstream.go 3.0 KiB

2 lat temu
2 lat temu
2 lat temu
2 lat temu
2 lat temu
2 lat temu
2 lat temu
2 lat temu
2 lat temu
2 lat temu
2 lat temu
2 lat temu
2 lat temu
2 lat temu
2 lat temu
2 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package endpoints
  2. import (
  3. "net/http"
  4. "strconv"
  5. "strings"
  6. "github.com/gorilla/websocket"
  7. "github.com/imosed/signet/client"
  8. . "github.com/imosed/signet/data"
  9. "github.com/rs/zerolog/log"
  10. "github.com/spf13/viper"
  11. "github.com/stellar/go/clients/horizonclient"
  12. "github.com/stellar/go/protocols/horizon"
  13. "github.com/stellar/go/protocols/horizon/operations"
  14. "golang.org/x/net/context"
  15. )
  16. var upgrader = websocket.Upgrader{
  17. ReadBufferSize: 1024,
  18. WriteBufferSize: 1024,
  19. CheckOrigin: func(r *http.Request) bool {
  20. origin := r.Header.Get("Origin")
  21. for _, domain := range viper.GetStringSlice("app.domains") {
  22. if !strings.HasPrefix(origin, domain) {
  23. continue
  24. }
  25. return true
  26. }
  27. return false
  28. },
  29. }
  30. var wsConn *websocket.Conn
  31. func ContributorStream(resp http.ResponseWriter, req *http.Request) {
  32. var err error
  33. wsConn, err = upgrader.Upgrade(resp, req, nil)
  34. if err != nil {
  35. log.Error().Err(err).Msg("Could not establish websocket connection")
  36. return
  37. }
  38. }
  39. var cancellations []context.CancelFunc
  40. func InitializeContributionStreams() {
  41. var err error
  42. var wallets []struct {
  43. FundWallet string
  44. }
  45. Db.Table("reward_funds").Select("fund_wallet").Scan(&wallets)
  46. contributionUpdateHandler := func(op operations.Operation) {
  47. if op.GetBase().GetTypeI() == 6 || op.GetBase().GetTypeI() == 12 {
  48. return
  49. }
  50. payment := op.(operations.Payment)
  51. var tx horizon.Transaction
  52. var amt float64
  53. var fund RewardFund
  54. tx, err = client.SignetClient.TransactionDetail(payment.GetTransactionHash())
  55. if err != nil {
  56. log.Error().Err(err).Msg("Could not get transaction from hash")
  57. return
  58. }
  59. amt, err = strconv.ParseFloat(payment.Amount, 64)
  60. if err != nil {
  61. log.Error().Err(err).Msg("Could not convert payment to float")
  62. return
  63. }
  64. if tx.Memo == "" {
  65. return
  66. }
  67. Db.Table("reward_funds").Where("memo = ? and fund_wallet = ?", tx.Memo, payment.To).First(&fund)
  68. newAmt := fund.AmountAvailable - amt
  69. Db.Model(&RewardFund{}).Where("id = ?", fund.ID).Update("amount_available", newAmt)
  70. contribution := Contribution{
  71. ModelBase: ModelBase{CreatedAt: tx.LedgerCloseTime},
  72. Wallet: payment.From,
  73. Amount: amt,
  74. TransactionID: payment.GetTransactionHash(),
  75. RewardFundID: fund.ID,
  76. }
  77. if wsConn != nil {
  78. err = wsConn.WriteJSON(contribution)
  79. if err != nil {
  80. log.Error().Err(err).Msg("Unable to write json to contribution stream")
  81. }
  82. } else {
  83. log.Info().Msg("No websocket connections")
  84. }
  85. Db.Table("contributions").Create(&contribution)
  86. }
  87. for _, wallet := range wallets {
  88. opReq := horizonclient.OperationRequest{
  89. ForAccount: wallet.FundWallet,
  90. Cursor: "now",
  91. IncludeFailed: false,
  92. }
  93. opReq.SetOperationsEndpoint()
  94. ctx, cancellation := context.WithCancel(context.Background())
  95. cancellations = append(cancellations, cancellation)
  96. err = client.SignetClient.StreamOperations(ctx, opReq, contributionUpdateHandler)
  97. if err != nil {
  98. log.Error().Err(err).Msg("Failed to stream contributions from Horizon SignetClient")
  99. }
  100. }
  101. }