Skip to content

Commit

Permalink
gRPC/Engine/Exchanges: Implement direct getwithdrawalshistory method (t…
Browse files Browse the repository at this point in the history
…hrasher-corp#600)

* GetClosedOrder implemented for Kraken and Binance, fixed Binance MARKET order creaton, added rate, fee and cost fileds on SubmitOrder responce

* return Trades on Binance SubmitOrder, new validation methods on Binance and kraken GetClosedOrderInfo

* removed the Binance extra method GetClosedOrder

* func description corrected

* removed price, fee and cost from SimulateOrder response, as we get all necessary info in response to calculate them on client side

* GetClosedOrder implementation moved to GetOrderInfo

* changed GetOrderInfo params

* removed Canceled order.Type used for Kraken

* update QueryOrder in gctscript

* add missed params to QueryOrder validator (gctscript)

* fixed testing issues

* GetClosedOrder implemented for Kraken and Binance, fixed Binance MARKET order creaton, added rate, fee and cost fileds on SubmitOrder responce

* return Trades on Binance SubmitOrder, new validation methods on Binance and kraken GetClosedOrderInfo

* removed the Binance extra method GetClosedOrder

* func description corrected

* removed price, fee and cost from SimulateOrder response, as we get all necessary info in response to calculate them on client side

* GetClosedOrder implementation moved to GetOrderInfo

* changed GetOrderInfo params

* removed Canceled order.Type used for Kraken

* update QueryOrder in gctscript

* add missed params to QueryOrder validator (gctscript)

* fixed testing issues

* pull previous changes

* linter issues fix

* updated query_order exmple in gctscript, fixed params check

* removed orderPair unnecessary conversion

* added wsCancelAllOrders, fixed bugs

* fixed Kraken wsAddOrder method

* cleanup

* CancelBatchOrders implementation

* changed CancelBatchOrders signature

* fixed tests and wrappers

* btcmarkets_test fix

* cleanup

* cleanup

* changed CancelBatchOrders signature

* fmt

* Update configtest.json

* Update configtest.json

* rollback configtest

* refactored Kraken wsHandleData to allow tests

* removed unnecessary error test in TestWsAddOrderJSON

* dependencies updates

* fixed issue with PortfolioSleepDelay set on startup

* add GetWithdrawalsHistory method to exchanges interface

* param name changes

* add extra params for Binance WithdrawStatus method

* add Binance TestWithdrawHistory

* linter errors fix

Co-authored-by: Vazha Bezhanishvili <[email protected]>
  • Loading branch information
vazha and Vazha Bezhanishvili authored Dec 4, 2020
1 parent 8cc1e58 commit e3ad2d5
Show file tree
Hide file tree
Showing 48 changed files with 1,594 additions and 1,182 deletions.
5 changes: 5 additions & 0 deletions cmd/exchange_template/wrapper_file.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ func ({{.Variable}} *{{.CapitalName}}) GetFundingHistory() ([]exchange.FundHisto
return nil, common.ErrNotYetImplemented
}

// GetWithdrawalsHistory returns previous withdrawals data
func ({{.Variable}} *{{.CapitalName}}) GetWithdrawalsHistory(c currency.Code) (resp []exchange.WithdrawalHistory, err error) {
return nil, common.ErrNotYetImplemented
}

// GetRecentTrades returns the most recent trades for a currency and asset
func ({{.Variable}} *{{.CapitalName}}) GetRecentTrades(p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
return nil, common.ErrNotYetImplemented
Expand Down
11 changes: 10 additions & 1 deletion cmd/gctcli/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2690,6 +2690,10 @@ var withdrawalRequestCommand = cli.Command{
Name: "limit",
Usage: "<limit>",
},
cli.StringFlag{
Name: "currency",
Usage: "<currency>",
},
},
Action: withdrawlRequestByExchangeID,
},
Expand Down Expand Up @@ -2767,7 +2771,7 @@ func withdrawlRequestByExchangeID(c *cli.Context) error {
return nil
}

var exchange string
var exchange, currency string
if c.IsSet("exchange") {
exchange = c.String("exchange")
} else {
Expand Down Expand Up @@ -2800,6 +2804,10 @@ func withdrawlRequestByExchangeID(c *cli.Context) error {
}
limit = limitStr
}

if c.IsSet("currency") {
currency = c.String("currency")
}
}

conn, err := setupClient()
Expand All @@ -2815,6 +2823,7 @@ func withdrawlRequestByExchangeID(c *cli.Context) error {
Exchange: exchange,
Id: ID,
Limit: int32(limit),
Currency: currency,
},
)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions engine/fake_exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ func (h *FakePassingExchange) GetOrderInfo(_ string, _ currency.Pair, _ asset.It
ID: "fakeOrder",
}, nil
}
func (h *FakePassingExchange) GetWithdrawalsHistory(_ currency.Code) ([]exchange.WithdrawalHistory, error) {
return nil, nil
}
func (h *FakePassingExchange) GetDepositAddress(_ currency.Code, _ string) (string, error) {
return "", nil
}
Expand Down
14 changes: 14 additions & 0 deletions engine/rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,20 @@ func (s *RPCServer) WithdrawalEventByID(_ context.Context, r *gctrpc.WithdrawalE
// WithdrawalEventsByExchange returns previous withdrawal request details by exchange
func (s *RPCServer) WithdrawalEventsByExchange(_ context.Context, r *gctrpc.WithdrawalEventsByExchangeRequest) (*gctrpc.WithdrawalEventsByExchangeResponse, error) {
if !s.Config.Database.Enabled {
if r.Id == "" {
exch := s.GetExchangeByName(r.Exchange)
if exch == nil {
return nil, errExchangeNotLoaded
}

c := currency.NewCode(strings.ToUpper(r.Currency))
ret, err := exch.GetWithdrawalsHistory(c)
if err != nil {
return nil, err
}

return parseWithdrawalsHistory(ret, exch.GetName(), int(r.Limit)), nil
}
return nil, database.ErrDatabaseSupportDisabled
}
if r.Id == "" {
Expand Down
38 changes: 38 additions & 0 deletions engine/withdraw.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/golang/protobuf/ptypes"
withdrawDataStore "github.com/thrasher-corp/gocryptotrader/database/repository/withdraw"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/gctrpc"
"github.com/thrasher-corp/gocryptotrader/log"
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
Expand Down Expand Up @@ -159,6 +160,43 @@ func parseMultipleEvents(ret []*withdraw.Response) *gctrpc.WithdrawalEventsByExc
return v
}

func parseWithdrawalsHistory(ret []exchange.WithdrawalHistory, exchName string, limit int) *gctrpc.WithdrawalEventsByExchangeResponse {
v := &gctrpc.WithdrawalEventsByExchangeResponse{}
for x := range ret {
if limit > 0 && x >= limit {
return v
}

tempEvent := &gctrpc.WithdrawalEventResponse{
Id: ret[x].TransferID,
Exchange: &gctrpc.WithdrawlExchangeEvent{
Name: exchName,
Status: ret[x].Status,
},
Request: &gctrpc.WithdrawalRequestEvent{
Currency: ret[x].Currency,
Description: ret[x].Description,
Amount: ret[x].Amount,
},
}

updatedAtPtype, err := ptypes.TimestampProto(ret[x].Timestamp)
if err != nil {
log.Errorf(log.Global, "failed to convert time: %v", err)
}

tempEvent.UpdatedAt = updatedAtPtype
tempEvent.Request.Crypto = &gctrpc.CryptoWithdrawalEvent{
Address: ret[x].CryptoToAddress,
Fee: ret[x].Fee,
TxId: ret[x].CryptoTxID,
}

v.Event = append(v.Event, tempEvent)
}
return v
}

func parseSingleEvents(ret *withdraw.Response) *gctrpc.WithdrawalEventsByExchangeResponse {
tempEvent := &gctrpc.WithdrawalEventResponse{
Id: ret.ID.String(),
Expand Down
5 changes: 5 additions & 0 deletions exchanges/alphapoint/alphapoint_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ func (a *Alphapoint) GetFundingHistory() ([]exchange.FundHistory, error) {
return nil, common.ErrNotYetImplemented
}

// GetWithdrawalsHistory returns previous withdrawals data
func (a *Alphapoint) GetWithdrawalsHistory(c currency.Code) (resp []exchange.WithdrawalHistory, err error) {
return nil, common.ErrNotYetImplemented
}

// GetRecentTrades returns the most recent trades for a currency and asset
func (a *Alphapoint) GetRecentTrades(_ currency.Pair, _ asset.Item) ([]trade.Data, error) {
return nil, common.ErrNotYetImplemented
Expand Down
42 changes: 42 additions & 0 deletions exchanges/binance/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,48 @@ func (b *Binance) WithdrawCrypto(asset, address, addressTag, name, amount string
return resp.ID, nil
}

// WithdrawStatus gets the status of recent withdrawals
// status `param` used as string to prevent default value 0 (for int) interpreting as EmailSent status
func (b *Binance) WithdrawStatus(c currency.Code, status string, startTime, endTime int64) ([]WithdrawStatusResponse, error) {
var response struct {
Success bool `json:"success"`
WithdrawList []WithdrawStatusResponse `json:"withdrawList"`
}

path := b.API.Endpoints.URL + withdrawalHistory
params := url.Values{}
params.Set("asset", c.String())

if status != "" {
i, err := strconv.Atoi(status)
if err != nil {
return response.WithdrawList, fmt.Errorf("wrong param (status): %s. Error: %v", status, err)
}

switch i {
case EmailSent, Cancelled, AwaitingApproval, Rejected, Processing, Failure, Completed:
default:
return response.WithdrawList, fmt.Errorf("wrong param (status): %s", status)
}

params.Set("status", status)
}

if startTime > 0 {
params.Set("startTime", strconv.FormatInt(startTime, 10))
}

if endTime > 0 {
params.Set("endTime", strconv.FormatInt(endTime, 10))
}

if err := b.SendAuthHTTPRequest(http.MethodGet, path, params, request.Unset, &response); err != nil {
return response.WithdrawList, err
}

return response.WithdrawList, nil
}

// GetDepositAddressForCurrency retrieves the wallet address for a given currency
func (b *Binance) GetDepositAddressForCurrency(currency string) (string, error) {
path := b.API.Endpoints.URL + depositAddress
Expand Down
15 changes: 15 additions & 0 deletions exchanges/binance/binance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,21 @@ func TestWithdraw(t *testing.T) {
}
}

func TestWithdrawHistory(t *testing.T) {
t.Parallel()
if areTestAPIKeysSet() && !canManipulateRealOrders && !mockTests {
t.Skip("API keys set, canManipulateRealOrders false, skipping test")
}

_, err := b.GetWithdrawalsHistory(currency.XBT)
switch {
case areTestAPIKeysSet() && err != nil:
t.Error("GetWithdrawalsHistory() error", err)
case !areTestAPIKeysSet() && err == nil && !mockTests:
t.Error("GetWithdrawalsHistory() expecting an error when no keys are set")
}
}

func TestWithdrawFiat(t *testing.T) {
t.Parallel()

Expand Down
24 changes: 24 additions & 0 deletions exchanges/binance/binance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ import (
"github.com/thrasher-corp/gocryptotrader/currency"
)

// withdrawals status codes description
const (
EmailSent = iota
Cancelled
AwaitingApproval
Rejected
Processing
Failure
Completed
)

// Response holds basic binance api response data
type Response struct {
Code int `json:"code"`
Expand Down Expand Up @@ -607,6 +618,19 @@ type WithdrawResponse struct {
ID string `json:"id"`
}

// WithdrawStatusResponse defines a withdrawal status response
type WithdrawStatusResponse struct {
Amount float64 `json:"amount"`
TransactionFee float64 `json:"transactionFee"`
Address string `json:"address"`
TxID string `json:"txId"`
ID string `json:"id"`
Asset string `json:"asset"`
ApplyTime int64 `json:"applyTime"`
Status int64 `json:"status"`
Network string `json:"network"`
}

// UserAccountStream contains a key to maintain an authorised
// websocket connection
type UserAccountStream struct {
Expand Down
23 changes: 23 additions & 0 deletions exchanges/binance/binance_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,29 @@ func (b *Binance) GetFundingHistory() ([]exchange.FundHistory, error) {
return nil, common.ErrFunctionNotSupported
}

// GetWithdrawalsHistory returns previous withdrawals data
func (b *Binance) GetWithdrawalsHistory(c currency.Code) (resp []exchange.WithdrawalHistory, err error) {
w, err := b.WithdrawStatus(c, "", 0, 0)
if err != nil {
return nil, err
}

for i := range w {
resp = append(resp, exchange.WithdrawalHistory{
Status: strconv.FormatInt(w[i].Status, 10),
TransferID: w[i].ID,
Currency: w[i].Asset,
Amount: w[i].Amount,
Fee: w[i].TransactionFee,
CryptoToAddress: w[i].Address,
CryptoTxID: w[i].TxID,
Timestamp: time.Unix(w[i].ApplyTime/1000, 0),
})
}

return resp, nil
}

// GetRecentTrades returns the most recent trades for a currency and asset
func (b *Binance) GetRecentTrades(p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
var err error
Expand Down
5 changes: 5 additions & 0 deletions exchanges/bitfinex/bitfinex_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,11 @@ func (b *Bitfinex) GetFundingHistory() ([]exchange.FundHistory, error) {
return nil, common.ErrFunctionNotSupported
}

// GetWithdrawalsHistory returns previous withdrawals data
func (b *Bitfinex) GetWithdrawalsHistory(c currency.Code) (resp []exchange.WithdrawalHistory, err error) {
return nil, common.ErrNotYetImplemented
}

// GetRecentTrades returns the most recent trades for a currency and asset
func (b *Bitfinex) GetRecentTrades(p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
return b.GetHistoricTrades(p, assetType, time.Now().Add(-time.Hour), time.Now())
Expand Down
5 changes: 5 additions & 0 deletions exchanges/bitflyer/bitflyer_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ func (b *Bitflyer) GetFundingHistory() ([]exchange.FundHistory, error) {
return nil, common.ErrFunctionNotSupported
}

// GetWithdrawalsHistory returns previous withdrawals data
func (b *Bitflyer) GetWithdrawalsHistory(c currency.Code) (resp []exchange.WithdrawalHistory, err error) {
return nil, common.ErrNotYetImplemented
}

// GetRecentTrades returns recent historic trades
func (b *Bitflyer) GetRecentTrades(p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
var err error
Expand Down
5 changes: 5 additions & 0 deletions exchanges/bithumb/bithumb_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,11 @@ func (b *Bithumb) GetFundingHistory() ([]exchange.FundHistory, error) {
return nil, common.ErrFunctionNotSupported
}

// GetWithdrawalsHistory returns previous withdrawals data
func (b *Bithumb) GetWithdrawalsHistory(c currency.Code) (resp []exchange.WithdrawalHistory, err error) {
return nil, common.ErrNotYetImplemented
}

// GetRecentTrades returns the most recent trades for a currency and asset
func (b *Bithumb) GetRecentTrades(p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
var err error
Expand Down
5 changes: 5 additions & 0 deletions exchanges/bitmex/bitmex_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,11 @@ func (b *Bitmex) GetFundingHistory() ([]exchange.FundHistory, error) {
return nil, common.ErrNotYetImplemented
}

// GetWithdrawalsHistory returns previous withdrawals data
func (b *Bitmex) GetWithdrawalsHistory(c currency.Code) (resp []exchange.WithdrawalHistory, err error) {
return nil, common.ErrNotYetImplemented
}

// GetRecentTrades returns the most recent trades for a currency and asset
func (b *Bitmex) GetRecentTrades(p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
return b.GetHistoricTrades(p, assetType, time.Now().Add(-time.Hour), time.Now())
Expand Down
5 changes: 5 additions & 0 deletions exchanges/bitstamp/bitstamp_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,11 @@ func (b *Bitstamp) GetFundingHistory() ([]exchange.FundHistory, error) {
return nil, common.ErrFunctionNotSupported
}

// GetWithdrawalsHistory returns previous withdrawals data
func (b *Bitstamp) GetWithdrawalsHistory(c currency.Code) (resp []exchange.WithdrawalHistory, err error) {
return nil, common.ErrNotYetImplemented
}

// GetRecentTrades returns the most recent trades for a currency and asset
func (b *Bitstamp) GetRecentTrades(p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
var err error
Expand Down
5 changes: 5 additions & 0 deletions exchanges/bittrex/bittrex_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,11 @@ func (b *Bittrex) GetFundingHistory() ([]exchange.FundHistory, error) {
return nil, common.ErrFunctionNotSupported
}

// GetWithdrawalsHistory returns previous withdrawals data
func (b *Bittrex) GetWithdrawalsHistory(c currency.Code) (resp []exchange.WithdrawalHistory, err error) {
return nil, common.ErrNotYetImplemented
}

// GetRecentTrades returns the most recent trades for a currency and asset
func (b *Bittrex) GetRecentTrades(p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
var err error
Expand Down
5 changes: 5 additions & 0 deletions exchanges/btcmarkets/btcmarkets_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ func (b *BTCMarkets) GetFundingHistory() ([]exchange.FundHistory, error) {
return nil, common.ErrFunctionNotSupported
}

// GetWithdrawalsHistory returns previous withdrawals data
func (b *BTCMarkets) GetWithdrawalsHistory(c currency.Code) (resp []exchange.WithdrawalHistory, err error) {
return nil, common.ErrNotYetImplemented
}

// GetRecentTrades returns the most recent trades for a currency and asset
func (b *BTCMarkets) GetRecentTrades(p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
var err error
Expand Down
5 changes: 5 additions & 0 deletions exchanges/btse/btse_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,11 @@ func (b *BTSE) withinLimits(pair currency.Pair, amount float64) bool {
amount > val.MaxOrderSize
}

// GetWithdrawalsHistory returns previous withdrawals data
func (b *BTSE) GetWithdrawalsHistory(c currency.Code) (resp []exchange.WithdrawalHistory, err error) {
return nil, common.ErrNotYetImplemented
}

// GetRecentTrades returns the most recent trades for a currency and asset
func (b *BTSE) GetRecentTrades(p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
var err error
Expand Down
5 changes: 5 additions & 0 deletions exchanges/coinbasepro/coinbasepro_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,11 @@ func (c *CoinbasePro) GetFundingHistory() ([]exchange.FundHistory, error) {
return nil, common.ErrFunctionNotSupported
}

// GetWithdrawalsHistory returns previous withdrawals data
func (c *CoinbasePro) GetWithdrawalsHistory(cur currency.Code) (resp []exchange.WithdrawalHistory, err error) {
return nil, common.ErrNotYetImplemented
}

// GetRecentTrades returns the most recent trades for a currency and asset
func (c *CoinbasePro) GetRecentTrades(p currency.Pair, assetType asset.Item) ([]trade.Data, error) {
var err error
Expand Down
Loading

0 comments on commit e3ad2d5

Please sign in to comment.