|
| 1 | +package scheduler |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/defipod/mochi/pkg/config" |
| 10 | + "github.com/defipod/mochi/pkg/entities" |
| 11 | + "github.com/defipod/mochi/pkg/logger" |
| 12 | + "github.com/defipod/mochi/pkg/model" |
| 13 | + "github.com/defipod/mochi/pkg/request" |
| 14 | + "github.com/defipod/mochi/pkg/service" |
| 15 | +) |
| 16 | + |
| 17 | +type updateBinanceSpotHistory struct { |
| 18 | + entity *entities.Entity |
| 19 | + log logger.Logger |
| 20 | + svc *service.Service |
| 21 | + config config.Config |
| 22 | +} |
| 23 | + |
| 24 | +// NewCheckInvalidateEmoji returns a new job that checks for invalid emojis |
| 25 | +func NewUpdateBinanceSpotHistory(e *entities.Entity, l logger.Logger, s *service.Service, cfg config.Config) Scheduler { |
| 26 | + return &updateBinanceSpotHistory{ |
| 27 | + entity: e, |
| 28 | + log: l, |
| 29 | + svc: s, |
| 30 | + config: cfg, |
| 31 | + } |
| 32 | +} |
| 33 | +func binanceStartTime() time.Time { |
| 34 | + return time.Now().Add(-90 * 24 * time.Hour).UTC() |
| 35 | +} |
| 36 | +func (s *updateBinanceSpotHistory) Run() error { |
| 37 | + for { |
| 38 | + s.schedulerUpdate() |
| 39 | + // Sleep for an hour interval before checking the database again |
| 40 | + time.Sleep(5 * time.Second) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func (s *updateBinanceSpotHistory) schedulerUpdate() error { |
| 45 | + |
| 46 | + res, err := s.svc.MochiProfile.GetAllBinanceAccount() |
| 47 | + if err != nil { |
| 48 | + s.log.Error(err, "[updateBinanceSpotHistory] - MochiProfile.GetAllBinanceAccount() fail to get all binance associated account") |
| 49 | + return err |
| 50 | + } |
| 51 | + // get binance exchangeInfo |
| 52 | + data, _, _ := s.svc.Binance.GetExchangeInfo("") |
| 53 | + pairs := []string{} |
| 54 | + for _, d := range data.Symbols { |
| 55 | + pairs = append(pairs, d.Symbol) |
| 56 | + } |
| 57 | + |
| 58 | + for _, acc := range res.Data { |
| 59 | + binanceTracking, err := s.entity.GetRepo().BinanceTracking.FirstOrCreate(&model.BinanceTracking{ProfileId: acc.ProfileId, SpotLastTime: binanceStartTime()}) |
| 60 | + if err != nil { |
| 61 | + s.log.Fields(logger.Fields{"profileId": acc.ProfileId}).Error(err, "[updateBinanceSpotHistory] - BinanceTracking.FirstOrCreate() fail to first or create binance tracking ") |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + symbols := []string{} |
| 66 | + assetBal, _, _, _ := s.entity.GetBinanceAssets(request.GetBinanceAssetsRequest{ |
| 67 | + Id: acc.ProfileId, |
| 68 | + Platform: "binance", |
| 69 | + }) |
| 70 | + for _, asset := range assetBal.Asset { |
| 71 | + symbols = append(symbols, asset.Token.Symbol) |
| 72 | + } |
| 73 | + symbolPairs := make(map[string][]string) |
| 74 | + |
| 75 | + // Populate the map |
| 76 | + for _, pair := range pairs { |
| 77 | + for _, symbol := range symbols { |
| 78 | + if strings.HasPrefix(pair, symbol) { |
| 79 | + symbolPairs[symbol] = append(symbolPairs[symbol], pair) |
| 80 | + break |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // Print pairs sorted by the order of symbols |
| 86 | + for _, symbol := range symbols { |
| 87 | + pairs := symbolPairs[symbol] |
| 88 | + for _, p := range pairs { |
| 89 | + startTime := strconv.Itoa(int(binanceTracking.SpotLastTime.UnixMilli())) |
| 90 | + endTime := strconv.Itoa(int(binanceTracking.SpotLastTime.Add(24 * time.Hour).UnixMilli())) |
| 91 | + txs, err := s.svc.Binance.GetSpotTransactions(acc.ApiKey, acc.ApiSecret, p, startTime, endTime) |
| 92 | + if err != nil { |
| 93 | + s.log.Fields(logger.Fields{"profileId": acc.ProfileId}).Error(err, "[updateBinanceSpotHistory] - svc.Binance.GetSpotTransactions() fail to get spot txs") |
| 94 | + break |
| 95 | + } |
| 96 | + for _, tx := range txs { |
| 97 | + err = s.entity.GetRepo().BinanceSpotTransaction.Create(&model.BinanceSpotTransaction{ |
| 98 | + ProfileId: acc.ProfileId, |
| 99 | + Symbol: tx.Symbol, |
| 100 | + OrderId: tx.OrderId, |
| 101 | + OrderListId: tx.OrderListId, |
| 102 | + ClientOrderId: tx.ClientOrderId, |
| 103 | + Price: tx.Price, |
| 104 | + OrigQty: tx.OrigQty, |
| 105 | + ExecutedQty: tx.ExecutedQty, |
| 106 | + CumulativeQuoteQty: tx.CummulativeQuoteQty, |
| 107 | + Status: tx.Status, |
| 108 | + TimeInForce: tx.TimeInForce, |
| 109 | + Type: tx.Type, |
| 110 | + Side: tx.Side, |
| 111 | + StopPrice: tx.StopPrice, |
| 112 | + IcebergQty: tx.IcebergQty, |
| 113 | + IsWorking: tx.IsWorking, |
| 114 | + OrigQuoteOrderQty: tx.OrigQuoteOrderQty, |
| 115 | + SelfTradePreventionMode: tx.SelfTradePreventionMode, |
| 116 | + }) |
| 117 | + if err != nil { |
| 118 | + fmt.Printf("err: %v", err) |
| 119 | + break |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + binanceTracking.SpotLastTime = binanceTracking.SpotLastTime.Add(24 * time.Hour) |
| 125 | + err = s.entity.GetRepo().BinanceTracking.Update(binanceTracking) |
| 126 | + if err != nil { |
| 127 | + s.log.Fields(logger.Fields{"profileId": acc.ProfileId}).Error(err, "[updateBinanceSpotHistory] -BinanceTracking.Update() fail to update binance tracking") |
| 128 | + break |
| 129 | + } |
| 130 | + } |
| 131 | + return nil |
| 132 | +} |
0 commit comments