|
| 1 | +module Kucoin |
| 2 | + |
| 3 | +export KucoinCommonQuery, |
| 4 | + KucoinPublicQuery, |
| 5 | + KucoinAccessQuery, |
| 6 | + KucoinPrivateQuery, |
| 7 | + KucoinAPIError, |
| 8 | + KucoinClient, |
| 9 | + KucoinData |
| 10 | + |
| 11 | +using Serde |
| 12 | +using Dates, NanoDates, TimeZones, Base64, Nettle |
| 13 | + |
| 14 | +using ..CryptoAPIs |
| 15 | +import ..CryptoAPIs: Maybe, AbstractAPIsError, AbstractAPIsData, AbstractAPIsQuery, AbstractAPIsClient |
| 16 | + |
| 17 | +abstract type KucoinData <: AbstractAPIsData end |
| 18 | +abstract type KucoinCommonQuery <: AbstractAPIsQuery end |
| 19 | +abstract type KucoinPublicQuery <: KucoinCommonQuery end |
| 20 | +abstract type KucoinAccessQuery <: KucoinCommonQuery end |
| 21 | +abstract type KucoinPrivateQuery <: KucoinCommonQuery end |
| 22 | + |
| 23 | +""" |
| 24 | + Data{D} <: AbstractAPIsData |
| 25 | +
|
| 26 | +## Required fields |
| 27 | +- `code::Int64`: Result code. |
| 28 | +- `data::D`: Result data. |
| 29 | +""" |
| 30 | +struct Data{D<:Union{A,Vector{A}} where {A<:AbstractAPIsData}} <: AbstractAPIsData |
| 31 | + code::Int64 |
| 32 | + data::D |
| 33 | +end |
| 34 | + |
| 35 | +""" |
| 36 | + Page{D} <: AbstractAPIsData |
| 37 | +
|
| 38 | +## Required fields |
| 39 | +- `pageSize::Int64`: Number of results per request. |
| 40 | +- `totalNum::Int64`: Total number of results. |
| 41 | +- `currentPage::Int64`: Current request page. |
| 42 | +- `totalPage::Int64`: Total request page. |
| 43 | +- `items::Vector{D}`: Result data. |
| 44 | +""" |
| 45 | +struct Page{D<:AbstractAPIsData} <:AbstractAPIsData |
| 46 | + pageSize::Int64 |
| 47 | + totalNum::Int64 |
| 48 | + currentPage::Int64 |
| 49 | + totalPage::Int64 |
| 50 | + items::Vector{D} |
| 51 | +end |
| 52 | + |
| 53 | +""" |
| 54 | + KucoinClient <: AbstractAPIsClient |
| 55 | +
|
| 56 | +Client info. |
| 57 | +
|
| 58 | +## Required fields |
| 59 | +- `base_url::String`: Base URL for the client. |
| 60 | +
|
| 61 | +## Optional fields |
| 62 | +- `public_key::String`: Public key for authentication. |
| 63 | +- `secret_key::String`: Secret key for authentication. |
| 64 | +- `interface::String`: Interface for the client. |
| 65 | +- `proxy::String`: Proxy information for the client. |
| 66 | +- `account_name::String`: Account name associated with the client. |
| 67 | +- `description::String`: Description of the client. |
| 68 | +""" |
| 69 | +Base.@kwdef struct KucoinClient <: AbstractAPIsClient |
| 70 | + base_url::String |
| 71 | + public_key::Maybe{String} = nothing |
| 72 | + secret_key::Maybe{String} = nothing |
| 73 | + passphrase::Maybe{String} = nothing |
| 74 | + interface::Maybe{String} = nothing |
| 75 | + proxy::Maybe{String} = nothing |
| 76 | + account_name::Maybe{String} = nothing |
| 77 | + description::Maybe{String} = nothing |
| 78 | +end |
| 79 | + |
| 80 | +""" |
| 81 | + KucoinAPIError{T} <: AbstractAPIsError |
| 82 | +
|
| 83 | +Exception thrown when an API method fails with code `T`. |
| 84 | +
|
| 85 | +## Required fields |
| 86 | +- `code::Int64`: Error code. |
| 87 | +- `msg::String`: Error message. |
| 88 | +""" |
| 89 | +struct KucoinAPIError{T} <: AbstractAPIsError |
| 90 | + code::Int64 |
| 91 | + msg::String |
| 92 | + |
| 93 | + function KucoinAPIError(code::Int64, x...) |
| 94 | + return new{code}(code, x...) |
| 95 | + end |
| 96 | +end |
| 97 | + |
| 98 | +CryptoAPIs.error_type(::KucoinClient) = KucoinAPIError |
| 99 | + |
| 100 | +function Base.show(io::IO, e::KucoinAPIError) |
| 101 | + return print(io, "code = ", "\"", e.code, "\"", ", ", "msg = ", "\"", e.msg, "\"") |
| 102 | +end |
| 103 | + |
| 104 | +function CryptoAPIs.request_sign!(::KucoinClient, query::Q, ::String)::Q where {Q<:KucoinPublicQuery} |
| 105 | + return query |
| 106 | +end |
| 107 | + |
| 108 | +function CryptoAPIs.request_sign!(client::KucoinClient, query::Q, endpoint::String)::Q where {Q <: KucoinPrivateQuery} |
| 109 | + query.timestamp = Dates.now(UTC) |
| 110 | + query.signature = nothing |
| 111 | + salt = join([string(round(Int64, 1000 * datetime2unix(query.timestamp))), "GET/$endpoint?", Serde.to_query(query)]) |
| 112 | + query.passphrase = Base64.base64encode(digest("sha256", client.secret_key, client.passphrase)) |
| 113 | + query.signature = Base64.base64encode(digest("sha256", client.secret_key, salt)) |
| 114 | + return query |
| 115 | +end |
| 116 | + |
| 117 | +function CryptoAPIs.request_body(::Q)::String where {Q<:KucoinCommonQuery} |
| 118 | + return "" |
| 119 | +end |
| 120 | + |
| 121 | +function CryptoAPIs.request_query(query::Q)::String where {Q<:KucoinCommonQuery} |
| 122 | + return Serde.to_query(query) |
| 123 | +end |
| 124 | + |
| 125 | +function CryptoAPIs.request_headers(client::KucoinClient, ::KucoinPublicQuery)::Vector{Pair{String,String}} |
| 126 | + return Pair{String,String}[ |
| 127 | + "Content-Type" => "application/json" |
| 128 | + ] |
| 129 | +end |
| 130 | + |
| 131 | +function CryptoAPIs.request_headers(client::KucoinClient, query::KucoinPrivateQuery)::Vector{Pair{String,String}} |
| 132 | + return Pair{String,String}[ |
| 133 | + "KC-API-SIGN" => query.signature, |
| 134 | + "KC-API-TIMESTAMP" => string(round(Int64, 1000 * datetime2unix(query.timestamp))), |
| 135 | + "KC-API-KEY" => client.public_key, |
| 136 | + "KC-API-PASSPHRASE" => query.passphrase, |
| 137 | + "Content-Type" => "application/json", |
| 138 | + "KC-API-KEY-VERSION" => "2", |
| 139 | + ] |
| 140 | +end |
| 141 | + |
| 142 | +include("Utils.jl") |
| 143 | +include("Errors.jl") |
| 144 | + |
| 145 | +include("Spot/Spot.jl") |
| 146 | +using .Spot |
| 147 | + |
| 148 | +end |
0 commit comments