KucoinBase: Abstract Base Class for KuCoin API Clients
KucoinBase: Abstract Base Class for KuCoin API Clients
Details
Provides shared infrastructure for all KuCoin R6 classes, including API credential management, sync/async execution mode, timestamp source configuration, and a standardised method for calling implementation functions.
Sync vs Async
The async parameter controls execution mode for all API methods:
async = FALSE(default): methods return results directly (data.table, character, etc.).async = TRUE: methods return promises::promise objects that resolve to the same types.
When async, use coro::async() and await() or promises::then() to consume results.
The promises package must be installed for async mode (Suggests dependency).
Timestamp Source
The time_source parameter controls which clock is used for HMAC request
signing:
"local"(default): useslubridate::now()from the local machine."server": fetches the KuCoin server time viaGET /api/v1/timestampbefore each authenticated request. This is slower (one extra HTTP round trip) but ensures signing works even when the local clock is out of sync.
Design
This class is not meant to be instantiated directly. Subclasses (e.g.,
KucoinMarketData, KucoinTrading) inherit from it and define their
own public methods that delegate to private$.request() and private$.paginate().
Fields
All fields are private:
.keys: List; API credentials fromget_api_keys()..base_url: Character; API base URL fromget_base_url()..perform: Function; either httr2::req_perform or httr2::req_perform_promise..is_async: Logical; whether the instance is in async mode..time_source: Character;"local"or"server"..get_timestamp_ms: Function; returns epoch milliseconds for HMAC signing.
Active bindings
is_asyncLogical; read-only flag indicating whether this instance operates in async mode.
time_sourceCharacter; read-only flag indicating the timestamp source used for HMAC signing (
"local"or"server").
Methods
Method new()
Initialise a KucoinBase Object
Usage
KucoinBase$new(
keys = get_api_keys(),
base_url = get_base_url(),
async = FALSE,
time_source = c("local", "server")
)Arguments
keysList; API credentials from
get_api_keys(). Defaults toget_api_keys().base_urlCharacter; API base URL. Defaults to
get_base_url().asyncLogical; if
TRUE, methods return promises. DefaultFALSE.time_sourceCharacter; clock source for HMAC request signing.
"local"(default) useslubridate::now()."server"fetches the KuCoin server time before each authenticated request, which adds latency but avoids clock-drift issues.
Examples
if (FALSE) { # \dontrun{
# Not instantiated directly; use subclasses:
market <- KucoinMarketData$new() # sync
market_async <- KucoinMarketData$new(async = TRUE) # async
# Use server time for HMAC signing (avoids clock-drift issues):
trading <- KucoinTrading$new(time_source = "server")
} # }