mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
add deposit service and withdraw service
This commit is contained in:
parent
f22a6ee697
commit
3c90aa515d
60
pkg/service/deposit.go
Normal file
60
pkg/service/deposit.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
type DepositService struct {
|
||||
DB *sqlx.DB
|
||||
}
|
||||
|
||||
func (s *DepositService) QueryLast(ex types.ExchangeName, limit int) ([]types.Deposit, error) {
|
||||
sql := "SELECT * FROM `deposits` 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 *DepositService) Query(exchangeName types.ExchangeName) ([]types.Deposit, error) {
|
||||
args := map[string]interface{}{
|
||||
"exchange": exchangeName,
|
||||
}
|
||||
sql := "SELECT * FROM `deposits` 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 *DepositService) scanRows(rows *sqlx.Rows) (deposits []types.Deposit, err error) {
|
||||
for rows.Next() {
|
||||
var deposit types.Deposit
|
||||
if err := rows.StructScan(&deposit); err != nil {
|
||||
return deposits, err
|
||||
}
|
||||
|
||||
deposits = append(deposits, deposit)
|
||||
}
|
||||
|
||||
return deposits, rows.Err()
|
||||
}
|
||||
|
||||
func (s *DepositService) Insert(deposit types.Deposit) error {
|
||||
sql := `INSERT INTO deposits (exchange, asset, address, amount, txn_id, time)
|
||||
VALUES (:exchange, :asset, :address, :amount, :txn_id, :time)`
|
||||
_, err := s.DB.NamedExec(sql, deposit)
|
||||
return err
|
||||
}
|
39
pkg/service/deposit_test.go
Normal file
39
pkg/service/deposit_test.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/datatype"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
func TestDepositService(t *testing.T) {
|
||||
db, err := prepareDB(t)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
xdb := sqlx.NewDb(db.DB, "sqlite3")
|
||||
service := &DepositService{DB: xdb}
|
||||
|
||||
err = service.Insert(types.Deposit{
|
||||
Exchange: types.ExchangeMax,
|
||||
Time: datatype.Time(time.Now()),
|
||||
Amount: 0.001,
|
||||
Asset: "BTC",
|
||||
Address: "test",
|
||||
TransactionID: "02",
|
||||
Status: types.DepositSuccess,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
deposits, err := service.Query(types.ExchangeMax)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, deposits)
|
||||
}
|
60
pkg/service/withdraw.go
Normal file
60
pkg/service/withdraw.go
Normal file
|
@ -0,0 +1,60 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"github.com/jmoiron/sqlx"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
type WithdrawService struct {
|
||||
DB *sqlx.DB
|
||||
}
|
||||
|
||||
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
|
||||
}
|
41
pkg/service/withdraw_test.go
Normal file
41
pkg/service/withdraw_test.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/datatype"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
)
|
||||
|
||||
func TestWithdrawService(t *testing.T) {
|
||||
db, err := prepareDB(t)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
xdb := sqlx.NewDb(db.DB, "sqlite3")
|
||||
service := &WithdrawService{DB: xdb}
|
||||
|
||||
err = service.Insert(types.Withdraw{
|
||||
Exchange: types.ExchangeMax,
|
||||
Asset: "BTC",
|
||||
Amount: 0.0001,
|
||||
Address: "test",
|
||||
TransactionID: "01",
|
||||
TransactionFee: 0.0001,
|
||||
Network: "omni",
|
||||
ApplyTime: datatype.Time(time.Now()),
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
|
||||
withdraws, err := service.Query(types.ExchangeMax)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, withdraws)
|
||||
assert.Equal(t, types.ExchangeMax, withdraws[0].Exchange)
|
||||
}
|
Loading…
Reference in New Issue
Block a user