The backend for the project formerly known as signet, now known as beignet.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

109 Zeilen
2.6 KiB

  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. }