Files
gocryptotrader/engine/withdraw_manager_test.go
Samuael A. 3f534a15f1 cmd/exchange_template, exchanges: Update templates and propogate to exchanges (#1777)
* Added TimeInForce type and updated related files

* Linter issue fix and minor coinbasepro type update

* Bitrex consts update

* added unit test and minor changes in bittrex

* Unit tests update

* Fix minor linter issues

* Update TestStringToTimeInForce unit test

* Exchange test template change

* A different approach

* fix conflict with gateio timeInForce

* minor exchange template update

* Minor fix to test_files template

* Update order tests

* Complete updating the order unit tests

* Updating exchange wrapper and test template files

* update kucoin and deribit wrapper to match the time in force change

* minor comment update

* fix time-in-force related test errors

* linter issue fix

* ADD_NEW_EXCHANGE documentation update

* time in force constants, functions and unit tests update

* shift tif policies to TimeInForce

* Update time-in-force, related functions, and unit tests

* fix linter issue and time-in-force processing

* added a good till crossing tif value

* order type fix and fix related tim-in-force entries

* update time-in-force unmarshaling and unit test

* consistency guideline added

* fix time-in-force error in gateio

* linter issue fix

* update based on review comments

* add unit test and fix missing issues

* minor fix and added benchmark unit test

* change GTT to GTC for limit

* fix linter issue

* added time-in-force value to place order param

* fix minor issues based on review comment and move tif code to separate files

* update on exchanges linked to time-in-force

* resolve missing review comments

* minor linter issues fix

* added time-in-force handler and update timeInForce parametered endpoint

* minor fixes based on review

* nits fix

* update based on review

* linter fix

* rm getTimeInForce func and minor change to time-in-force

* minor change

* update based on review comments

* wrappers and time-in-force calling approach

* minor change

* update gateio string to timeInForce conversion and unit test

* update exchange template

* update wrapper template file

* policy comments, and template files update

* rename all exchange types name to Exchange

* update on template files and template generation

* templates and generation code and other updates

* linter issue fix

* added subscriptions and websocket templates

* update ADD_NEW_EXCHANGE.md with recent binance functions and implementations

* rename template files and update unit tests

* minor template and unit test fix

* rename templates and fix on unit tests

* update on template files and documentation

* removed unnecessary tag fix and update templates

* fix Add_NEW_EXCHANGE.md doc file

* formatting, comments, and error checks update on template files

* rename exchange receivers to e and ex for consistency

* rename unit test exchange receiver and minor updates

* linter issues fix

* fix deribit issue and minor style update

* fix test issues caused by receiver change

* raname local variables exchange declaration variables

* update templates comments

* update templates and related comments

* renamed ex to e

* update template comments

* toggle WS to false to improve coverage

* template comments update

* added test coverage to Ws enabled and minor changes

---------

Co-authored-by: Samuel Reid <43227667+cranktakular@users.noreply.github.com>
2025-07-17 10:46:36 +10:00

185 lines
4.8 KiB
Go

package engine
import (
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/okx"
"github.com/thrasher-corp/gocryptotrader/portfolio"
"github.com/thrasher-corp/gocryptotrader/portfolio/banking"
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
)
const (
withdrawManagerTestExchangeName = "okx"
)
func withdrawManagerTestHelper(t *testing.T) (*ExchangeManager, *portfolioManager) {
t.Helper()
em := NewExchangeManager()
b := new(okx.Exchange)
cfg, err := exchange.GetDefaultConfig(t.Context(), b)
if err != nil {
t.Fatal(err)
}
err = b.Setup(cfg)
if err != nil {
t.Fatal(err)
}
err = em.Add(b)
require.NoError(t, err)
pm, err := setupPortfolioManager(em, 0, &portfolio.Base{Addresses: []portfolio.Address{}})
if err != nil {
t.Fatal(err)
}
return em, pm
}
func TestSubmitWithdrawal(t *testing.T) {
t.Parallel()
em, pm := withdrawManagerTestHelper(t)
m, err := SetupWithdrawManager(em, pm, false)
if err != nil {
t.Fatal(err)
}
bank := banking.Account{
Enabled: true,
ID: "test-bank-01",
BankName: "Test Bank",
BankAddress: "42 Bank Street",
BankPostalCode: "13337",
BankPostalCity: "Satoshiville",
BankCountry: "Japan",
AccountName: "Satoshi Nakamoto",
AccountNumber: "0234",
BSBNumber: "123456",
SWIFTCode: "91272837",
IBAN: "98218738671897",
SupportedCurrencies: "AUD,USD",
SupportedExchanges: "Binance",
}
banking.AppendAccounts(bank)
req := &withdraw.Request{
Exchange: withdrawManagerTestExchangeName,
Currency: currency.AUD,
Description: withdrawManagerTestExchangeName,
Amount: 1.0,
Type: withdraw.Fiat,
Fiat: withdraw.FiatRequest{
Bank: bank,
},
}
_, err = m.SubmitWithdrawal(t.Context(), req)
assert.ErrorIs(t, err, common.ErrFunctionNotSupported)
req.Type = withdraw.Crypto
req.Currency = currency.BTC
req.Crypto.Address = "1337"
_, err = m.SubmitWithdrawal(t.Context(), req)
assert.ErrorIs(t, err, withdraw.ErrStrAddressNotWhiteListed)
var wg sync.WaitGroup
err = pm.Start(&wg)
if err != nil {
t.Error(err)
}
err = pm.AddAddress("1337", "", req.Currency, 1337)
if err != nil {
t.Error(err)
}
adds := pm.GetAddresses()
adds[0].WhiteListed = true
assert.NoError(t, err)
_, err = m.SubmitWithdrawal(t.Context(), req)
assert.ErrorIs(t, err, withdraw.ErrStrExchangeNotSupportedByAddress)
adds[0].SupportedExchanges = withdrawManagerTestExchangeName
_, err = m.SubmitWithdrawal(t.Context(), req)
assert.ErrorIs(t, err, exchange.ErrAuthenticationSupportNotEnabled)
_, err = m.SubmitWithdrawal(t.Context(), nil)
assert.ErrorIs(t, err, withdraw.ErrRequestCannotBeNil)
m.isDryRun = true
_, err = m.SubmitWithdrawal(t.Context(), req)
assert.NoError(t, err)
}
func TestWithdrawEventByID(t *testing.T) {
t.Parallel()
em, pm := withdrawManagerTestHelper(t)
m, err := SetupWithdrawManager(em, pm, false)
if err != nil {
t.Fatal(err)
}
tempResp := &withdraw.Response{
ID: withdraw.DryRunID,
}
_, err = m.WithdrawalEventByID(withdraw.DryRunID.String())
assert.ErrorIs(t, err, ErrWithdrawRequestNotFound)
withdraw.Cache.Add(withdraw.DryRunID.String(), tempResp)
v, err := m.WithdrawalEventByID(withdraw.DryRunID.String())
assert.NoError(t, err)
if v == nil {
t.Error("expected WithdrawalEventByID() to return data from cache")
}
}
func TestWithdrawalEventByExchange(t *testing.T) {
t.Parallel()
em, pm := withdrawManagerTestHelper(t)
m, err := SetupWithdrawManager(em, pm, false)
if err != nil {
t.Fatal(err)
}
_, err = (*WithdrawManager)(nil).WithdrawalEventByExchange("xxx", 0)
assert.ErrorIs(t, err, ErrNilSubsystem)
_, err = m.WithdrawalEventByExchange("xxx", 0)
assert.ErrorIs(t, err, ErrExchangeNotFound)
}
func TestWithdrawEventByDate(t *testing.T) {
t.Parallel()
em, pm := withdrawManagerTestHelper(t)
m, err := SetupWithdrawManager(em, pm, false)
if err != nil {
t.Fatal(err)
}
_, err = (*WithdrawManager)(nil).WithdrawEventByDate("xxx", time.Now(), time.Now(), 1)
assert.ErrorIs(t, err, ErrNilSubsystem)
_, err = m.WithdrawEventByDate("xxx", time.Now(), time.Now(), 1)
assert.ErrorIs(t, err, ErrExchangeNotFound)
}
func TestWithdrawalEventByExchangeID(t *testing.T) {
t.Parallel()
em, _ := withdrawManagerTestHelper(t)
m, err := SetupWithdrawManager(em, nil, false)
if err != nil {
t.Fatal(err)
}
_, err = (*WithdrawManager)(nil).WithdrawalEventByExchangeID("xxx", "xxx")
assert.ErrorIs(t, err, ErrNilSubsystem)
_, err = m.WithdrawalEventByExchangeID("xxx", "xxx")
assert.ErrorIs(t, err, ErrExchangeNotFound)
}