From 9302096f591aecd11962551590992e67f22291bc Mon Sep 17 00:00:00 2001 From: wuhewuhe Date: Mon, 6 Nov 2023 00:44:54 +0100 Subject: [PATCH] asset --- asset.go | 443 +++++++++++++++++++++++ bybit_api_client.go | 6 + examples/asset/asset_exchange_records.go | 22 ++ examples/asset/coin_info.go | 22 ++ examples/user/create_sub_user.go | 1 + models/assetResponse.go | 223 ++++++++++++ user.go | 6 + 7 files changed, 723 insertions(+) create mode 100644 asset.go create mode 100644 examples/asset/asset_exchange_records.go create mode 100644 examples/asset/coin_info.go create mode 100644 examples/user/create_sub_user.go create mode 100644 user.go diff --git a/asset.go b/asset.go new file mode 100644 index 0000000..684f136 --- /dev/null +++ b/asset.go @@ -0,0 +1,443 @@ +package bybit_connector + +import ( + "context" + "encoding/json" + "net/http" +) + +type AssetClient struct { + c *Client + params map[string]interface{} +} + +func (s *AssetClient) GetAssetOrderRecord(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/v5/asset/exchange/order-record", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) GetAssetInfo(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/v5/asset/transfer/query-asset-info", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) GetDeliveryRecord(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/v5/asset/delivery-record", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) GetUsdcSettlement(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/v5/asset/settlement-record", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) GetAllCoinsBalance(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/v5/asset/transfer/query-account-coins-balance", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) GetSingleCoinsBalance(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/v5/asset/transfer/query-account-coin-balance", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) GetTransferableCoin(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/v5/asset/transfer/query-transfer-coin-list", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) CreateInternalTransfer(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodPost, + endpoint: "/v5/asset/transfer/inter-transfer", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) CreateUniversalTransfer(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodPost, + endpoint: "/v5/asset/transfer/universal-transfer", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) SetDepositAccount(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodPost, + endpoint: "/v5/asset/deposit/deposit-to-account", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) WithdrawAsset(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodPost, + endpoint: "/v5/asset/withdraw/create", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) CancelWithdrawAsset(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodPost, + endpoint: "/v5/asset/withdraw/cancel", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) GetInternalTransfer(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/v5/asset/transfer/query-inter-transfer-list", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) GetUniversalTransfer(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/v5/asset/transfer/query-universal-transfer-list", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) GetSubUids(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/v5/asset/transfer/query-sub-member-list", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) GetAllowedDepositCoin(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/v5/asset/deposit/query-allowed-list", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) GetDepositRecords(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/v5/asset/deposit/query-record", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) GetSubDepositRecords(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/v5/asset/deposit/query-sub-member-record", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) GetInternalDepositRecords(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/v5/asset/deposit/query-internal-record", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) GetMasterDepositAddress(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/v5/asset/deposit/query-address", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) GetSubDepositAddress(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/v5/asset/deposit/query-sub-member-address", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) GetCoinInfo(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/v5/asset/coin/query-info", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} + +func (s *AssetClient) GetWithdrawalAmount(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/v5/asset/withdraw/withdrawable-amount", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} +func (s *AssetClient) GetWithdrawalRecords(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) { + r := &request{ + method: http.MethodGet, + endpoint: "/v5/asset/withdraw/query-record", + secType: secTypeSigned, + } + data, err := s.c.callAPI(ctx, r, opts...) + if err != nil { + return nil, err + } + res = new(ServerResponse) + err = json.Unmarshal(data, res) + if err != nil { + return nil, err + } + return res, nil +} diff --git a/bybit_api_client.go b/bybit_api_client.go index b33bdc3..e19ecb9 100644 --- a/bybit_api_client.go +++ b/bybit_api_client.go @@ -268,3 +268,9 @@ func (c *Client) NewAccountServiceNoParams() *AccountClient { c: c, } } + +func (c *Client) NewAssetService(params map[string]interface{}) *AssetClient { + return &AssetClient{ + c: c, + } +} diff --git a/examples/asset/asset_exchange_records.go b/examples/asset/asset_exchange_records.go new file mode 100644 index 0000000..b11f339 --- /dev/null +++ b/examples/asset/asset_exchange_records.go @@ -0,0 +1,22 @@ +package main + +import ( + "context" + "fmt" + bybit "github.com/wuhewuhe/bybit.go.api" +) + +func main() { + AssetInfo() +} + +func AssetInfo() { + client := bybit.NewBybitHttpClient("8wYkmpLsMg10eNQyPm", "Ouxc34myDnXvei54XsBZgoQzfGxO4bkr2Zsj", bybit.WithBaseURL(bybit.TESTNET)) + params := map[string]interface{}{"accountType": "spot"} + assetResult, err := client.NewAssetService(params).GetAssetInfo(context.Background()) + if err != nil { + fmt.Println(err) + return + } + fmt.Println(bybit.PrettyPrint(assetResult)) +} diff --git a/examples/asset/coin_info.go b/examples/asset/coin_info.go new file mode 100644 index 0000000..2875c42 --- /dev/null +++ b/examples/asset/coin_info.go @@ -0,0 +1,22 @@ +package main + +import ( + "context" + "fmt" + bybit "github.com/wuhewuhe/bybit.go.api" +) + +func main() { + GetCoinInfo() +} + +func GetCoinInfo() { + client := bybit.NewBybitHttpClient("8wYkmpLsMg10eNQyPm", "Ouxc34myDnXvei54XsBZgoQzfGxO4bkr2Zsj", bybit.WithBaseURL(bybit.TESTNET)) + params := map[string]interface{}{"coin": "USDT"} + assetResult, err := client.NewAssetService(params).GetCoinInfo(context.Background()) + if err != nil { + fmt.Println(err) + return + } + fmt.Println(bybit.PrettyPrint(assetResult)) +} diff --git a/examples/user/create_sub_user.go b/examples/user/create_sub_user.go new file mode 100644 index 0000000..a00006b --- /dev/null +++ b/examples/user/create_sub_user.go @@ -0,0 +1 @@ +package user diff --git a/models/assetResponse.go b/models/assetResponse.go index a620b0a..5fdb213 100644 --- a/models/assetResponse.go +++ b/models/assetResponse.go @@ -32,3 +32,226 @@ type SettlementEntry struct { RealisedPnl string `json:"realisedPnl"` CreatedTime string `json:"createdTime"` } + +type AssetInfo struct { + Spot SpotInfo `json:"spot"` +} + +type SpotInfo struct { + Status string `json:"status"` + Assets []AssetItem `json:"assets"` +} + +type AssetItem struct { + Coin string `json:"coin"` + Frozen string `json:"frozen"` + Free string `json:"free"` + Withdraw string `json:"withdraw"` +} + +type AllCoinsBalance struct { + AccountType string `json:"accountType"` + MemberId string `json:"memberId"` + Balance []BalanceDetail `json:"balance"` +} + +type BalanceDetail struct { + Coin string `json:"coin"` + WalletBalance string `json:"walletBalance"` + TransferBalance string `json:"transferBalance"` + Bonus string `json:"bonus"` +} + +type SingleCoinBalance struct { + AccountType string `json:"accountType"` + BizType int `json:"bizType"` + AccountId string `json:"accountId"` + MemberId string `json:"memberId"` + Balance CoinBalance `json:"balance"` +} + +type CoinBalance struct { + Coin string `json:"coin"` + WalletBalance string `json:"walletBalance"` + TransferBalance string `json:"transferBalance"` + Bonus string `json:"bonus"` + TransferSafeAmount string `json:"transferSafeAmount"` + LtvTransferSafeAmount string `json:"ltvTransferSafeAmount"` +} + +type TransferResult struct { + TransferId string `json:"transferId"` +} + +type InternalTransferInfo struct { + List []TransferDetail `json:"list"` + NextPageCursor string `json:"nextPageCursor"` +} + +type TransferDetail struct { + TransferId string `json:"transferId"` + Coin string `json:"coin"` + Amount string `json:"amount"` + FromAccountType string `json:"fromAccountType"` + ToAccountType string `json:"toAccountType"` + Timestamp string `json:"timestamp"` + Status string `json:"status"` +} + +type SubUidsInfo struct { + SubMemberIds []string `json:"subMemberIds"` + TransferableSubMemberIds []string `json:"transferableSubMemberIds"` +} + +type UniversalTransferInfo struct { + List []UniversalTransferDetail `json:"list"` + NextPageCursor string `json:"nextPageCursor"` +} + +type UniversalTransferDetail struct { + TransferId string `json:"transferId"` + Coin string `json:"coin"` + Amount string `json:"amount"` + FromMemberId string `json:"fromMemberId"` + ToMemberId string `json:"toMemberId"` + FromAccountType string `json:"fromAccountType"` + ToAccountType string `json:"toAccountType"` + Timestamp string `json:"timestamp"` + Status string `json:"status"` +} + +type WithdrawAssetResult struct { + Id string `json:"id"` +} + +type CancelWithdrawAssetResult struct { + Status string `json:"status"` +} + +type SetDepositAccountResult struct { + Status string `json:"status"` +} + +type AllowDepositCoinInfo struct { + ConfigList []DepositCoinConfig `json:"configList"` + NextPageCursor string `json:"nextPageCursor"` +} + +type DepositCoinConfig struct { + Coin string `json:"coin"` + Chain string `json:"chain"` + CoinShowName string `json:"coinShowName"` + ChainType string `json:"chainType"` + BlockConfirmNumber int `json:"blockConfirmNumber"` + MinDepositAmount string `json:"minDepositAmount"` +} + +type DepositRecords struct { + Rows []DepositRecord `json:"rows"` + NextPageCursor string `json:"nextPageCursor"` +} + +type DepositRecord struct { + Coin string `json:"coin"` + Chain string `json:"chain"` + Amount string `json:"amount"` + TxID string `json:"txID"` + Status int `json:"status"` + ToAddress string `json:"toAddress"` + Tag string `json:"tag"` + DepositFee string `json:"depositFee"` + SuccessAt string `json:"successAt"` + Confirmations string `json:"confirmations"` + TxIndex string `json:"txIndex"` + BlockHash string `json:"blockHash"` + BatchReleaseLimit string `json:"batchReleaseLimit"` + DepositType int `json:"depositType"` +} + +type SubDepositResult struct { + Coin string `json:"coin"` + Chains []DepositChainInfo `json:"chains"` +} + +// MasterDepositResult represents the structure for master deposit results. +type MasterDepositResult struct { + Coin string `json:"coin"` + Chains []DepositChainInfo `json:"chains"` +} + +// DepositChainInfo represents the shared structure for deposit chain information. +type DepositChainInfo struct { + ChainType string `json:"chainType"` + AddressDeposit string `json:"addressDeposit"` + TagDeposit string `json:"tagDeposit"` + Chain string `json:"chain"` + BatchReleaseLimit string `json:"batchReleaseLimit"` +} + +// CoinInfoResult represents the structure for coin info results. +type CoinInfoResult struct { + Rows []CoinInfoRow `json:"rows"` +} + +// CoinInfoRow represents the structure for each row of coin information. +type CoinInfoRow struct { + Name string `json:"name"` + Coin string `json:"coin"` + RemainAmount string `json:"remainAmount"` + Chains []CoinChainInfo `json:"chains"` +} + +// CoinChainInfo represents the structure for each chain's information for a coin. +type CoinChainInfo struct { + Chain string `json:"chain"` + ChainType string `json:"chainType"` + Confirmation string `json:"confirmation"` + WithdrawFee string `json:"withdrawFee"` + DepositMin string `json:"depositMin"` + WithdrawMin string `json:"withdrawMin"` + MinAccuracy string `json:"minAccuracy"` + ChainDeposit string `json:"chainDeposit"` + ChainWithdraw string `json:"chainWithdraw"` + WithdrawPercentageFee string `json:"withdrawPercentageFee"` +} + +// WithdrawRecords represents the structure for withdrawal records. +type WithdrawRecords struct { + Rows []WithdrawRecord `json:"rows"` + NextPageCursor string `json:"nextPageCursor"` +} + +// WithdrawRecord represents the structure for each withdrawal record. +type WithdrawRecord struct { + WithdrawID string `json:"withdrawId"` + TxID string `json:"txID"` + WithdrawType string `json:"withdrawType"` + Coin string `json:"coin"` + Chain string `json:"chain"` + Amount string `json:"amount"` + WithdrawFee string `json:"withdrawFee"` + Status string `json:"status"` + ToAddress string `json:"toAddress"` + Tag string `json:"tag"` + CreateTime string `json:"createTime"` + UpdateTime string `json:"updateTime"` +} + +// WithdrawableAmount represents the structure for information about withdrawable amounts. +type WithdrawableAmount struct { + LimitAmountUsd string `json:"limitAmountUsd"` + WithdrawableAmounts []WithdrawableAsset `json:"withdrawableAmount"` +} + +// WithdrawableAsset represents the structure for withdrawable amounts for each wallet type. +type WithdrawableAsset struct { + SPOT *WalletInfo `json:"SPOT,omitempty"` // Omitted if empty + FUND *WalletInfo `json:"FUND,omitempty"` // Omitted if empty +} + +// WalletInfo represents the wallet information for withdrawable assets. +type WalletInfo struct { + Coin string `json:"coin"` + WithdrawableAmount string `json:"withdrawableAmount"` + AvailableBalance string `json:"availableBalance"` +} diff --git a/user.go b/user.go new file mode 100644 index 0000000..4fca008 --- /dev/null +++ b/user.go @@ -0,0 +1,6 @@ +package bybit_connector + +type UserServiceClient struct { + c *Client + params map[string]interface{} +}