The backend for the project formerly known as signet, now known as beignet.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

110 rader
2.7 KiB

  1. package endpoints
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "github.com/gorilla/websocket"
  8. . "github.com/imosed/signet/data"
  9. "github.com/spf13/viper"
  10. "github.com/stellar/go/clients/horizonclient"
  11. "github.com/stellar/go/protocols/horizon"
  12. "github.com/stellar/go/protocols/horizon/operations"
  13. "golang.org/x/net/context"
  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. }