package endpoints import ( "fmt" "github.com/gorilla/websocket" . "github.com/imosed/signet/data" "github.com/spf13/viper" "github.com/stellar/go/clients/horizonclient" "github.com/stellar/go/protocols/horizon" "github.com/stellar/go/protocols/horizon/operations" "golang.org/x/net/context" "net/http" "strconv" "strings" ) var upgrader = websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, CheckOrigin: func(r *http.Request) bool { origin := r.Header.Get("Origin") for _, domain := range viper.GetStringSlice("app.domains") { if !strings.HasPrefix(origin, domain) { continue } return true } return false }, } var wsConn *websocket.Conn func ContributorStream(resp http.ResponseWriter, req *http.Request) { var err error wsConn, err = upgrader.Upgrade(resp, req, nil) if err != nil { panic("Could not establish websocket connection") } } var cancellations []context.CancelFunc func InitializeContributionStreams() { var err error var wallets []struct { FundWallet string } Db.Table("reward_funds").Select("fund_wallet").Scan(&wallets) contributionUpdateHandler := func(op operations.Operation) { payment := op.(operations.Payment) var tx horizon.Transaction var amt float64 var fund RewardFund tx, err = client.TransactionDetail(payment.GetTransactionHash()) if err != nil { panic("Could not get transaction from hash") } amt, err = strconv.ParseFloat(payment.Amount, 64) if err != nil { panic("Could not convert payment to float") } if tx.Memo == "" { return } Db.Table("reward_funds").Where("memo = ? and fund_wallet = ?", tx.Memo, payment.To).First(&fund) newAmt := fund.AmountAvailable - amt Db.Model(&RewardFund{}).Where("id = ?", fund.ID).Update("amount_available", newAmt) contribution := Contribution{ ModelBase: ModelBase{CreatedAt: tx.LedgerCloseTime}, Wallet: payment.From, Amount: amt, TransactionID: payment.GetTransactionHash(), RewardFundID: fund.ID, } if wsConn != nil { err = wsConn.WriteJSON(contribution) if err != nil { panic("Unable to write json to contribution stream") } } else { fmt.Println("No websocket connections") } Db.Table("contributions").Create(&contribution) } for _, wallet := range wallets { opReq := horizonclient.OperationRequest{ ForAccount: wallet.FundWallet, Cursor: "now", IncludeFailed: false, } opReq.SetOperationsEndpoint() ctx, cancellation := context.WithCancel(context.Background()) cancellations = append(cancellations, cancellation) err = client.StreamOperations(ctx, opReq, contributionUpdateHandler) if err != nil { panic(err.Error()) } } }