Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exchanges: Okx Update #1420

Open
wants to merge 134 commits into
base: master
Choose a base branch
from
Open

exchanges: Okx Update #1420

wants to merge 134 commits into from

Conversation

samuael
Copy link
Collaborator

@samuael samuael commented Dec 11, 2023

Selam, ወንድሞች.

PR Description

This pull request incorporates modifications for the Okx exchange, encompassing changes to endpoint methods, rate limits, WebSocket subscriptions and handlers, wrapper functions, and conversion methods. Rigorous testing has been conducted on all facets of the functions to ensure their reliability. The code adheres to the GCT style standards, consistent with practices adopted by other exchanges. To enhance code quality, golangci-lint was utilized for linter testing, effectively rectifying a few errors. Furthermore, this implementation includes endpoint methods that the GCT wrapper did not initially support, ensuring compatibility for future use

The PR updated implementations for REST for trading and other endpoint methods, Web-socket streaming, and Trading through the web socket stream.

Fixes # (issue)

Type of change

Please delete options that are not relevant and add an x in [] as the item is complete.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How has this been tested

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration and also, consider improving test coverage whilst working on a certain feature or package.

  • go test ./... -race
  • golangci-lint run
  • Test X

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented on my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation and regenerated documentation via the documentation tool
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally and on Github Actions/AppVeyor with my changes
  • Any dependent changes have been merged and published in downstream modules

@gloriousCode gloriousCode requested a review from a team December 11, 2023 21:08
Copy link
Collaborator

@shazbert shazbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a comment. Then looks good from me.

Comment on lines 5866 to 5909
// GetLendingOffers get lending-supported currencies and estimated APY.
func (ok *Okx) GetLendingOffers(ctx context.Context, ccy currency.Code, term string) ([]PublicLendingOffer, error) {
params := url.Values{}
if !ccy.IsEmpty() {
params.Set("ccy", ccy.String())
}
if term != "" {
params.Set("term", term)
}
var resp []PublicLendingOffer
return resp, ok.SendHTTPRequest(ctx, exchange.RestSpot, lendingPublicOfferEPL, http.MethodGet, common.EncodeURLValues("finance/fixed-loan/lending-offers", params), nil, &resp, request.UnauthenticatedRequest)
}

// GetLendingAPYHistory retrieves a lending history.
func (ok *Okx) GetLendingAPYHistory(ctx context.Context, ccy currency.Code, term string) ([]LendingAPIHistoryItem, error) {
if ccy.IsEmpty() {
return nil, currency.ErrCurrencyCodeEmpty
}
if term == "" {
return nil, errLendingTermIsRequired
}
params := url.Values{}
params.Set("term", term)
params.Set("ccy", ccy.String())
var resp []LendingAPIHistoryItem
return resp, ok.SendHTTPRequest(ctx, exchange.RestSpot, lendingAPYHistoryEPL, http.MethodGet, common.EncodeURLValues("finance/fixed-loan/lending-apy-history", params), nil, &resp, request.UnauthenticatedRequest)
}

// GetLendingVolume retrieves a lending volume
func (ok *Okx) GetLendingVolume(ctx context.Context, ccy currency.Code, term string) ([]LendingVolume, error) {
if ccy.IsEmpty() {
return nil, currency.ErrCurrencyCodeEmpty
}
if term == "" {
return nil, errLendingTermIsRequired
}
params := url.Values{}
params.Set("term", term)
params.Set("ccy", ccy.String())
var resp []LendingVolume
return resp, ok.SendHTTPRequest(ctx, exchange.RestSpot, lendingVolumeEPL, http.MethodGet, common.EncodeURLValues("finance/fixed-loan/pending-lending-volume", params), nil, &resp, request.UnauthenticatedRequest)
}

// Trading Statistics endpoints.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why were these removed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code: 51739 message: This function is temporarily unavailable

We will add them when they are available.

I waited for these endpoints to get back to work and couldn't be updated to work

Copy link
Collaborator

@shazbert shazbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good! Thanks.

Copy link
Collaborator

@gloriousCode gloriousCode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a test that seems to periodically fail:
https://github.com/thrasher-corp/gocryptotrader/actions/runs/12216182485/job/34079014540?pr=1420#step:10:706

--- FAIL: TestGenerateSubscriptions (0.00s)
    okx_test.go:5793: 
        	Error Trace:	/home/runner/work/gocryptotrader/gocryptotrader/internal/testing/subscriptions/subscriptions.go:32
        	            				/home/runner/work/gocryptotrader/gocryptotrader/exchanges/okx/okx_test.go:5793
        	Error:      	Differences:
        	            	 + {"channel":"grid-positions"} grid-positions  
        	            	 - {"channel":"account"} myAccount  
        	Test:       	TestGenerateSubscriptions
        	Messages:   	Subscriptions should be equal

Other than that, I'm happy :)

@samuael
Copy link
Collaborator Author

samuael commented Dec 9, 2024

I hope this recent change has resolved the issue

Copy link
Collaborator

@gloriousCode gloriousCode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK!

🕊️

Copy link
Collaborator

@shazbert shazbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎺 tack 🎺

Copy link
Collaborator

@thrasher- thrasher- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on this massive PR @samuael, really like the types cleanup due to OKX dropping JSON keys similar to Bitfinex. Just some first round nits

@@ -29,6 +29,7 @@ const (
MarginFunding
Index
Binary
Spread
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be moved to below the futures section since it involves at least one futures contract

LiquidationOrders: liquidation,
NumberOfOrders: orders,
// GetItems returns a list of OrderbookItemDetail instances.
func GetItems(data [][4]types.Number) ([]OrderbookItemDetail, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can unexport this func

}

// MarginLendRatioItem represents margin lend ration information and creation timestamp
type MarginLendRatioItem struct {
Timestamp time.Time `json:"ts"`
MarginLendRatio float64 `json:"ratio"`
Timestamp types.Time `json:"ts"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can rid these JSON tags as they're not needed, but this is a sexy solution

type LongShortRatio struct {
Timestamp time.Time `json:"ts"`
MarginLendRatio float64 `json:"ratio"`
Timestamp types.Time `json:"ts"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, will stop reporting these as there's a few of them

errExceedLimit = errors.New("limit exceeded")
errOnlyThreeMonthsSupported = errors.New("only three months of trade data retrieval supported")
errOnlyOneResponseExpected = errors.New("one response item expected")
errNoInstrumentFound = errors.New("no instrument found")
)

/************************************ MarketData Endpoints *************************************************/

// OrderTypeFromString returns order.Type instance from string
func (ok *Okx) OrderTypeFromString(orderType string) (order.Type, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requires test coverage

}

// PlaceAlgoOrder order includes trigger order, oco order, conditional order,iceberg order, twap order and trailing order.
func (ok *Okx) PlaceAlgoOrder(ctx context.Context, arg *AlgoOrderParams) (*AlgoOrder, error) {
if *arg == (AlgoOrderParams{}) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test coverage

}
return ok.PlaceAlgoOrder(ctx, arg)
}

// PlaceTrailingStopOrder to place trailing stop order
func (ok *Okx) PlaceTrailingStopOrder(ctx context.Context, arg *AlgoOrderParams) (*AlgoOrder, error) {
if arg == nil {
return nil, errNilArgument
if *arg == (AlgoOrderParams{}) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test coverage for these offline checks

}
if arg.CallbackRatio == 0 && arg.CallbackSpreadVariance == "" {
return nil, errors.New("either \"callbackRatio\" or \"callbackSpread\" is allowed to be passed")
}
return ok.PlaceAlgoOrder(ctx, arg)
}

// PlaceIcebergOrder to place iceburg algo order
// PlaceIcebergOrder to place iceberg algo order
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for here and the 3 below

orderType = strings.ToLower(orderType)
if orderType == "" {
return nil, errors.New("order type is required")
return nil, order.ErrTypeIsInvalid
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs test coverage

if orderType == "" {
return nil, errors.New("order type is required")
return nil, order.ErrTypeIsInvalid
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs test coverage, will stop reporting them from here but please re-review the offline ones we can at least test

},
"spread": {
"assetEnabled": true,
"enabled": "BTC-USD-241113-70000-C,BTC-USD-241113-70000-P,BTC-USD-241113-72000-C,BTC-USD-241113-72000-P,BTC-USD-241113-74000-C",
Copy link
Collaborator

@thrasher- thrasher- Dec 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are option symbols, though when running it the expected symbols populate correctly

},
"spread": {
"assetEnabled": true,
"enabled": "BTC-USD-241113-70000-C,BTC-USD-241113-70000-P,BTC-USD-241113-72000-C,BTC-USD-241113-72000-P,BTC-USD-241113-74000-C",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for these ones

Copy link
Collaborator

@thrasher- thrasher- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making those changes! config_example and test config spread symbols looking good + substantial increase in test coverage. Found some additional ones

}
if arg.QuantityType != "" && arg.QuantityType != "base_ccy" && arg.QuantityType != "quote_ccy" {
switch arg.QuantityType {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test coverage

if args == nil {
return nil, errNilArgument
func (ok *Okx) CancelAlgoOrder(ctx context.Context, args []AlgoOrderCancelParams) (*AlgoOrder, error) {
if len(args) == 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test coverage

@@ -1005,7 +747,7 @@ func (ok *Okx) PlaceEasyConvert(ctx context.Context, arg PlaceEasyConvertParam)
return nil, fmt.Errorf("%w, missing 'toCcy'", errMissingRequiredParameter)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test coverage for these two params

}
if len(resp) == 1 {
return &resp[0], nil
return nil, errMissingLegs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test coverage for these two params

}

// CancelMultipleRfqs cancel multiple active Rfqs in a single batch. Maximum 100 Rfq orders can be canceled at a time.
func (ok *Okx) CancelMultipleRfqs(ctx context.Context, arg CancelRfqRequestsParam) ([]CancelRfqResponse, error) {
func (ok *Okx) CancelMultipleRfqs(ctx context.Context, arg *CancelRfqRequestsParam) ([]CancelRfqResponse, error) {
if arg == nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Acronym creation: MTC (Missing test coverage 😆 ) and the below len clientRfgIDs > 100 below


// ManualBorrowAndRepayInQuickMarginMode initiates a new manual borrow and repayment process in Quick Margin mode.
func (ok *Okx) ManualBorrowAndRepayInQuickMarginMode(ctx context.Context, arg *BorrowAndRepay) (*BorrowAndRepay, error) {
if *arg == (BorrowAndRepay{}) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MTC

}

// MasterAccountsManageTransfersBetweenSubaccounts master accounts manage the transfers between sub-accounts applies to master accounts only
func (ok *Okx) MasterAccountsManageTransfersBetweenSubaccounts(ctx context.Context, arg *SubAccountAssetTransferParams) ([]TransferIDInfo, error) {
if arg.Currency == "" {
return nil, errInvalidCurrencyValue
if *arg == (SubAccountAssetTransferParams{}) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MTC on these params

func (ok *Okx) PlaceGridAlgoOrder(ctx context.Context, arg *GridAlgoOrder) (*GridAlgoOrderIDResponse, error) {
if arg == nil {
return nil, errNilArgument
if *arg == (GridAlgoOrder{}) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate check but also MTC on the params

}

// GetPublicBlockTrades retrieves the recent block trading transactions of an instrument. Descending order by tradeId.
func (ok *Okx) GetPublicBlockTrades(ctx context.Context, instrumentID string) ([]BlockTrade, error) {
if instrumentID == "" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MTC

}

// GetInsuranceFundInformation returns insurance fund balance information.
func (ok *Okx) GetInsuranceFundInformation(ctx context.Context, arg *InsuranceFundInformationRequestParams) (*InsuranceFundInformation, error) {
if arg == nil {
return nil, errNilArgument
if *arg == (InsuranceFundInformationRequestParams{}) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MTC

authDialer.ReadBufferSize = 8192
authDialer.WriteBufferSize = 8192
err = ok.WsAuth(context.TODO(), &authDialer)
ok.Websocket.SetCanUseAuthenticatedEndpoints(true)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can rid this as this is always setting auth to true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
high priority in progress review me This pull request is ready for review
Projects
Status: In review
Development

Successfully merging this pull request may close these issues.

5 participants