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.

contributionstream.go 2.6 KiB

il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
il y a 2 ans
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. "net/http"
  12. "strconv"
  13. "strings"
  14. )
  15. var upgrader = websocket.Upgrader{
  16. ReadBufferSize: 1024,
  17. WriteBufferSize: 1024,
  18. CheckOrigin: func(r *http.Request) bool {
  19. origin := r.Header.Get("Origin")
  20. for _, domain := range viper.GetStringSlice("app.domains") {
  21. if !strings.HasPrefix(origin, domain) {
  22. continue
  23. }
  24. return true
  25. }
  26. return false
  27. },
  28. }
  29. var wsConn *websocket.Conn
  30. func ContributorStream(resp http.ResponseWriter, req *http.Request) {
  31. var err error
  32. wsConn, err = upgrader.Upgrade(resp, req, nil)
  33. if err != nil {
  34. panic("Could not establish websocket connection")
  35. }
  36. }
  37. var cancellations []context.CancelFunc
  38. func InitializeContributionStreams() {
  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").Where("memo = ? and fund_wallet = ?", tx.Memo, payment.To).First(&fund)
  61. newAmt := fund.AmountAvailable - amt
  62. Db.Model(&RewardFund{}).Where("id = ?", fund.ID).Update("amount_available", newAmt)
  63. contribution := Contribution{
  64. ModelBase: ModelBase{CreatedAt: tx.LedgerCloseTime},
  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, cancellation := context.WithCancel(context.Background())
  88. cancellations = append(cancellations, cancellation)
  89. err = client.StreamOperations(ctx, opReq, contributionUpdateHandler)
  90. if err != nil {
  91. panic(err.Error())
  92. }
  93. }
  94. }