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 2.5 KiB

2 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package endpoints
  2. import (
  3. "fmt"
  4. "github.com/gorilla/websocket"
  5. . "github.com/imosed/signet/data"
  6. "github.com/spf13/viper"
  7. "github.com/stellar/go/clients/horizonclient"
  8. "github.com/stellar/go/protocols/horizon"
  9. "github.com/stellar/go/protocols/horizon/operations"
  10. "golang.org/x/net/context"
  11. "gorm.io/gorm"
  12. "net/http"
  13. "strconv"
  14. "strings"
  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. panic("Could not establish websocket connection")
  36. }
  37. }
  38. func InitializeContributionStream() {
  39. var err error
  40. var wallets []struct {
  41. FundWallet string
  42. }
  43. Db.Table("reward_funds").Select("fund_wallet").Scan(&wallets)
  44. contributionUpdateHandler := func(op operations.Operation) {
  45. payment := op.(operations.Payment)
  46. var tx horizon.Transaction
  47. var amt float64
  48. var fund RewardFund
  49. tx, err = client.TransactionDetail(payment.GetTransactionHash())
  50. if err != nil {
  51. panic("Could not get transaction from hash")
  52. }
  53. amt, err = strconv.ParseFloat(payment.Amount, 64)
  54. if err != nil {
  55. panic("Could not convert payment to float")
  56. }
  57. if tx.Memo == "" {
  58. return
  59. }
  60. Db.Table("reward_funds").Select("id").Where("memo = ? and fund_wallet = ?", tx.Memo, payment.To).First(&fund)
  61. contribution := Contribution{
  62. Model: gorm.Model{
  63. CreatedAt: tx.LedgerCloseTime,
  64. },
  65. Wallet: payment.From,
  66. Amount: amt,
  67. TransactionID: payment.GetTransactionHash(),
  68. RewardFundID: fund.ID,
  69. }
  70. if wsConn != nil {
  71. err = wsConn.WriteJSON(contribution)
  72. if err != nil {
  73. panic("Unable to write json to contribution stream")
  74. }
  75. } else {
  76. fmt.Println("No websocket connections")
  77. }
  78. Db.Table("contributions").Create(&contribution)
  79. }
  80. for _, wallet := range wallets {
  81. opReq := horizonclient.OperationRequest{
  82. ForAccount: wallet.FundWallet,
  83. Cursor: "now",
  84. IncludeFailed: false,
  85. }
  86. opReq.SetOperationsEndpoint()
  87. ctx, _ := context.WithCancel(context.Background()) // TODO: what is cancelFunc?
  88. err = client.StreamOperations(ctx, opReq, contributionUpdateHandler)
  89. if err != nil {
  90. panic(err.Error())
  91. }
  92. }
  93. }