Skip to content

Commit

Permalink
fix: update spot tx when it status is new
Browse files Browse the repository at this point in the history
  • Loading branch information
NguyenHuy1812 committed May 16, 2024
1 parent 08a3988 commit 6c85df3
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/repo/binance_spot_transaction/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,7 @@ func (pg *pg) GetUserAverageCost(profileId string) ([]model.BinanceAssetAverageC

return avgCost, nil
}

func (pg *pg) Update(tx *model.BinanceSpotTransaction) error {
return pg.db.Save(&tx).Error
}
1 change: 1 addition & 0 deletions pkg/repo/binance_spot_transaction/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ type Store interface {
Create(tx *model.BinanceSpotTransaction) error
List(q ListQuery) ([]model.BinanceSpotTransaction, error)
GetUserAverageCost(profileId string) ([]model.BinanceAssetAverageCost, error)
Update(tx *model.BinanceSpotTransaction) error
}
22 changes: 22 additions & 0 deletions pkg/scheduler/update-binance-spot-history.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/defipod/mochi/pkg/entities"
"github.com/defipod/mochi/pkg/logger"
"github.com/defipod/mochi/pkg/model"
binancespottransaction "github.com/defipod/mochi/pkg/repo/binance_spot_transaction"
"github.com/defipod/mochi/pkg/request"
"github.com/defipod/mochi/pkg/service"
"github.com/defipod/mochi/pkg/service/binance"
Expand Down Expand Up @@ -65,6 +66,27 @@ func (s *updateBinanceSpotHistory) schedulerUpdate() error {
s.log.Fields(logger.Fields{"profileId": acc.ProfileId}).Error(err, "[updateBinanceSpotHistory] - BinanceTracking.FirstOrCreate() fail to first or create binance tracking ")
continue
}
// update status of NEW order in case it filled or cancel
newTxs, _ := s.entity.GetRepo().BinanceSpotTransaction.List(binancespottransaction.ListQuery{
ProfileId: acc.ProfileId,
Status: "NEW",
})
for _, newTx := range newTxs {
tx, err := s.svc.Binance.GetSpotTransactionByOrderId(acc.ApiKey, acc.ApiSecret, newTx.Pair, newTx.OrderId)
if err != nil {
continue
}
if tx.Status != newTx.Status {
newTx.Status = tx.Status
newTx.ExecutedQty = tx.ExecutedQty
newTx.UpdateTime = tx.UpdateTime
newTx.UpdatedAt = time.UnixMilli(tx.UpdateTime)
}
err = s.entity.GetRepo().BinanceSpotTransaction.Update(&newTx)
if err != nil {
continue
}
}

symbols := []string{}
assetBal, _, _, _ := s.entity.GetBinanceAssets(request.GetBinanceAssetsRequest{
Expand Down
40 changes: 40 additions & 0 deletions pkg/service/binance/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,3 +425,43 @@ func Kline(symbol, interval string, startTime, endTime int64) (tickers [][]strin

return tickers, nil
}

func GetSpotTransactionByOrderId(apiKey, apiSecret, symbol string, orderId int64) (tx *response.BinanceSpotTransactionResponse, err error) {
q := map[string]string{
"timestamp": strconv.Itoa(int(time.Now().UnixMilli())),
"orderId": fmt.Sprintf("%d", orderId),
"symbol": symbol,
"recvWindow": "59000",
}
queryString := butils.QueryString(q, apiSecret)

// http request
req, err := http.NewRequest("GET", url+"/api/v3/order?"+queryString, nil)
if err != nil {
return
}
resp, err := do(req, apiKey, 0)
if err != nil {
return
}
defer resp.Body.Close()
resBody, err := io.ReadAll(resp.Body)
if err != nil {
return
}

if resp.Header.Get("X-Mbx-Used-Weight-1m") != "" {
usedWeight1M, err := strconv.Atoi(resp.Header.Get("X-Mbx-Used-Weight-1m"))
if err != nil || usedWeight1M > 5000 {
fmt.Printf("err: %+v, %d", err, usedWeight1M)
time.Sleep(1 * time.Minute)
}
}

// decode response json
err = json.Unmarshal(resBody, &tx)
if err != nil {
return
}
return
}
8 changes: 8 additions & 0 deletions pkg/service/binance/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,11 @@ const (
func (b *Binance) Kline(symbol string, interval Interval, startTime int64, endTime int64) ([][]string, error) {
return badapter.Kline(symbol, string(interval), startTime, endTime)
}

func (b *Binance) GetSpotTransactionByOrderId(apiKey, apiSecret, symbol string, orderId int64) (*response.BinanceSpotTransactionResponse, error) {
b.logger.Debug("start binance.GetSpotTransactionByOrderId()")
defer b.logger.Debug("end binance.GetSpotTransactionByOrderId()")

// get spot transaction
return badapter.GetSpotTransactionByOrderId(apiKey, apiSecret, symbol, orderId)
}
1 change: 1 addition & 0 deletions pkg/service/binance/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ type Service interface {
GetFutureAccountInfo(apiKey, apiSecret string) ([]response.BinanceFuturePositionInfo, error)
GetPrice(symbol string) (*response.BinanceApiTickerPriceResponse, error)
GetSpotTransactions(apiKey, apiSecret, symbol, startTime, endTime string) ([]response.BinanceSpotTransactionResponse, error)
GetSpotTransactionByOrderId(apiKey, apiSecret, symbol string, orderId int64) (*response.BinanceSpotTransactionResponse, error)
Kline(symbol string, interval Interval, startTime int64, endTime int64) ([][]string, error)
}

0 comments on commit 6c85df3

Please sign in to comment.