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

FEATURE: [okx] implement QueryDepositHistory method #1895

Merged
merged 4 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions pkg/exchange/okex/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,71 @@ func toGlobalMarginRepay(record okexapi.MarginHistoryEntry) types.MarginRepay {
IsolatedSymbol: "",
}
}

// DepositRecord.state represents the deposit state
/*
Status of deposit
0: Waiting for confirmation
1: Deposit credited
2: Deposit successful
8: Pending due to temporary deposit suspension on this crypto currency
11: Match the address blacklist
12: Account or deposit is frozen
13: Sub-account deposit interception
14: KYC limit
*/
func toDepositStatusMessage(state int64) string {
switch state {
case 0:
return "Waiting for confirmation"
case 1:
return "Deposit credited"
case 2:
return "Deposit successful"
case 8:
return "Pending due to temporary deposit suspension on this crypto currency"
case 11:
return "Match the address blacklist"
case 12:
return "Account or deposit is frozen"
case 13:
return "Sub-account deposit interception"
case 14:
return "KYC limit"
}

return ""
}

func toGlobalDepositStatus(state int64) types.DepositStatus {
switch state {
case 0:
return types.DepositPending
case 1:
return types.DepositCredited
case 2:
return types.DepositSuccess
case 8:
return types.DepositPending
default:
return types.DepositRejected
}
}

func toGlobalDeposit(record okexapi.DepositRecord) types.Deposit {
return types.Deposit{
Exchange: types.ExchangeOKEx,
Time: types.Time(record.Ts),
Amount: record.Amount,
Asset: record.Currency,
Address: record.To,
AddressTag: "",
TransactionID: record.DepId,
Status: toGlobalDepositStatus(int64(record.State)),
RawStatus: fmt.Sprintf("%s (%s)", record.State.String(), toDepositStatusMessage(int64(record.State))),
UnlockConfirm: 0,
Confirmation: record.ActualDepBlkConfirm.String(),
Network: strings.ToUpper(record.Chain),
}

}
36 changes: 36 additions & 0 deletions pkg/exchange/okex/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,28 @@ func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
return nil, fmt.Errorf("account balance is empty")
}

accountConfigs, err := e.client.NewGetAccountConfigRequest().Do(ctx)
if err != nil {
return nil, err
}

if len(accountConfigs) == 0 {
return nil, fmt.Errorf("account config is empty")
}

balances := toGlobalBalance(&accounts[0])
account := types.NewAccount()
account.UpdateBalances(balances)

// for margin account
account.MarginRatio = accounts[0].MarginRatio
account.MarginLevel = accounts[0].MarginRatio
account.TotalAccountValue = accounts[0].TotalEquityInUSD

if e.MarginSettings.IsMargin && !accountConfigs[0].EnableSpotBorrow {
log.Warnf("margin is set, but okx enableSpotBorrow field is false, please turn on auto-borrow from the okx UI")
}

return account, nil
}

Expand Down Expand Up @@ -682,6 +696,28 @@ func (e *Exchange) QueryInterestHistory(ctx context.Context, asset string, start
return nil, nil
}

func (e *Exchange) QueryDepositHistory(ctx context.Context, asset string, startTime, endTime *time.Time) ([]types.Deposit, error) {
req := e.client.NewGetAssetDepositHistoryRequest().Currency(asset)
if endTime != nil {
req.Before(*endTime)
}
if startTime != nil {
req.After(*startTime)
}

resp, err := req.Do(ctx)
if err != nil {
return nil, err
}

var records []types.Deposit
for _, r := range resp {
records = append(records, toGlobalDeposit(r))
}

return records, nil
}

/*
QueryTrades can query trades in last 3 months, there are no time interval limitations, as long as end_time >= start_time.
okx does not provide an API to query by trade ID, so we use the bill ID to do it. The trades result is ordered by timestamp.
Expand Down
46 changes: 46 additions & 0 deletions pkg/exchange/okex/okexapi/get_asset_deposit_history_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package okexapi

import (
"time"

"github.com/c9s/requestgen"

"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
)

//go:generate -command GetRequest requestgen -method GET -responseType .APIResponse -responseDataField Data
//go:generate -command PostRequest requestgen -method POST -responseType .APIResponse -responseDataField Data

type DepositRecord struct {
ActualDepBlkConfirm types.StrInt64 `json:"actualDepBlkConfirm"`
Amount fixedpoint.Value `json:"amt"`
AreaCodeFrom string `json:"areaCodeFrom"`
Currency string `json:"ccy"`
Chain string `json:"chain"`
DepId string `json:"depId"`
From string `json:"from"`
FromWdId string `json:"fromWdId"`
State types.StrInt64 `json:"state"`
To string `json:"to"`

Ts types.MillisecondTimestamp `json:"ts"`

TxId string `json:"txId"`
}

//go:generate GetRequest -url "/api/v5/asset/deposit-history" -type GetAssetDepositHistoryRequest -responseDataType []DepositRecord
type GetAssetDepositHistoryRequest struct {
client requestgen.AuthenticatedAPIClient

currency *string `param:"ccy"`
after *time.Time `param:"after,milliseconds"`
before *time.Time `param:"before,milliseconds"`
limit *uint64 `param:"limit" defaultValue:"100"`
}

func (c *RestClient) NewGetAssetDepositHistoryRequest() *GetAssetDepositHistoryRequest {
return &GetAssetDepositHistoryRequest{
client: c,
}
}
Loading
Loading