2021-03-14 03:04:18 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2021-03-13 12:49:51 +00:00
|
|
|
"context"
|
2021-12-31 06:20:36 +00:00
|
|
|
"fmt"
|
2021-03-13 12:49:51 +00:00
|
|
|
"time"
|
|
|
|
|
2022-06-01 18:31:46 +00:00
|
|
|
sq "github.com/Masterminds/squirrel"
|
2021-03-14 03:04:18 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2021-12-31 06:20:36 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-03-14 03:04:18 +00:00
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type WithdrawService struct {
|
|
|
|
DB *sqlx.DB
|
|
|
|
}
|
|
|
|
|
2022-06-02 03:32:21 +00:00
|
|
|
// Sync syncs the withdrawal records into db
|
2021-03-13 12:49:51 +00:00
|
|
|
func (s *WithdrawService) Sync(ctx context.Context, ex types.Exchange) error {
|
|
|
|
txnIDs := map[string]struct{}{}
|
|
|
|
|
2021-03-11 08:44:43 +00:00
|
|
|
// query descending
|
2021-03-15 18:14:24 +00:00
|
|
|
records, err := s.QueryLast(ex.Name(), 10)
|
2021-03-13 12:49:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, record := range records {
|
|
|
|
txnIDs[record.TransactionID] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
transferApi, ok := ex.(types.ExchangeTransferService)
|
|
|
|
if !ok {
|
|
|
|
return ErrNotImplemented
|
|
|
|
}
|
|
|
|
|
2021-03-11 08:44:43 +00:00
|
|
|
since := time.Time{}
|
|
|
|
if len(records) > 0 {
|
|
|
|
since = records[len(records)-1].ApplyTime.Time()
|
|
|
|
}
|
|
|
|
|
|
|
|
// asset "" means all assets
|
|
|
|
withdraws, err := transferApi.QueryWithdrawHistory(ctx, "", since, time.Now())
|
2021-03-13 12:49:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, withdraw := range withdraws {
|
2021-03-11 08:44:43 +00:00
|
|
|
if _, exists := txnIDs[withdraw.TransactionID]; exists {
|
2021-03-13 12:49:51 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-03-11 08:44:43 +00:00
|
|
|
|
2021-12-31 06:20:36 +00:00
|
|
|
if withdraw.Status == "rejected" {
|
|
|
|
log.Warnf("skip record, withdraw transaction rejected: %+v", withdraw)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(withdraw.TransactionID) == 0 {
|
|
|
|
return fmt.Errorf("empty withdraw transacion ID: %+v", withdraw)
|
|
|
|
}
|
|
|
|
|
2021-03-11 08:44:43 +00:00
|
|
|
if err := s.Insert(withdraw); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-03-13 12:49:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-01 18:31:46 +00:00
|
|
|
func SelectLastWithdraws(ex types.ExchangeName, limit uint64) sq.SelectBuilder {
|
|
|
|
return sq.Select("*").
|
|
|
|
From("withdraws").
|
|
|
|
Where(sq.And{
|
|
|
|
sq.Eq{"exchange": ex},
|
|
|
|
}).
|
|
|
|
OrderBy("time DESC").
|
|
|
|
Limit(limit)
|
|
|
|
}
|
|
|
|
|
2021-03-14 03:04:18 +00:00
|
|
|
func (s *WithdrawService) QueryLast(ex types.ExchangeName, limit int) ([]types.Withdraw, error) {
|
|
|
|
sql := "SELECT * FROM `withdraws` WHERE `exchange` = :exchange ORDER BY `time` DESC LIMIT :limit"
|
|
|
|
rows, err := s.DB.NamedQuery(sql, map[string]interface{}{
|
|
|
|
"exchange": ex,
|
|
|
|
"limit": limit,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
return s.scanRows(rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WithdrawService) Query(exchangeName types.ExchangeName) ([]types.Withdraw, error) {
|
|
|
|
args := map[string]interface{}{
|
|
|
|
"exchange": exchangeName,
|
|
|
|
}
|
|
|
|
sql := "SELECT * FROM `withdraws` WHERE `exchange` = :exchange ORDER BY `time` ASC"
|
|
|
|
rows, err := s.DB.NamedQuery(sql, args)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
return s.scanRows(rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WithdrawService) scanRows(rows *sqlx.Rows) (withdraws []types.Withdraw, err error) {
|
|
|
|
for rows.Next() {
|
|
|
|
var withdraw types.Withdraw
|
|
|
|
if err := rows.StructScan(&withdraw); err != nil {
|
|
|
|
return withdraws, err
|
|
|
|
}
|
|
|
|
|
|
|
|
withdraws = append(withdraws, withdraw)
|
|
|
|
}
|
|
|
|
|
|
|
|
return withdraws, rows.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WithdrawService) Insert(withdrawal types.Withdraw) error {
|
|
|
|
sql := `INSERT INTO withdraws (exchange, asset, network, address, amount, txn_id, txn_fee, time)
|
|
|
|
VALUES (:exchange, :asset, :network, :address, :amount, :txn_id, :txn_fee, :time)`
|
|
|
|
_, err := s.DB.NamedExec(sql, withdrawal)
|
|
|
|
return err
|
|
|
|
}
|