|
| 1 | +module Bithumb |
| 2 | + |
| 3 | +export BithumbCommonQuery, |
| 4 | + BithumbPublicQuery, |
| 5 | + BithumbAccessQuery, |
| 6 | + BithumbPrivateQuery, |
| 7 | + BithumbAPIError, |
| 8 | + BithumbClient, |
| 9 | + BithumbData |
| 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 BithumbData <: AbstractAPIsData end |
| 18 | +abstract type BithumbCommonQuery <: AbstractAPIsQuery end |
| 19 | +abstract type BithumbPublicQuery <: BithumbCommonQuery end |
| 20 | +abstract type BithumbAccessQuery <: BithumbCommonQuery end |
| 21 | +abstract type BithumbPrivateQuery <: BithumbCommonQuery end |
| 22 | + |
| 23 | +""" |
| 24 | + Data{D<:Union{A,Vector{A},Dict{String,A}} where {A<:AbstractAPIsData}} <: AbstractAPIsData |
| 25 | +
|
| 26 | +## Required fields |
| 27 | +- `status::String`: Request status. |
| 28 | +- `data::D`: Request result data. |
| 29 | +
|
| 30 | +## Optional fields |
| 31 | +- `date::NanoDate`: Current time. |
| 32 | +""" |
| 33 | +struct Data{D<:Union{A,Vector{A},Dict{String,A}} where {A<:AbstractAPIsData}} <: AbstractAPIsData |
| 34 | + status::String |
| 35 | + date::Maybe{NanoDate} |
| 36 | + data::D |
| 37 | +end |
| 38 | + |
| 39 | +""" |
| 40 | + BithumbClient <: AbstractAPIsClient |
| 41 | +
|
| 42 | +Client info. |
| 43 | +
|
| 44 | +## Required fields |
| 45 | +- `base_url::String`: Base URL for the client. |
| 46 | +
|
| 47 | +## Optional fields |
| 48 | +- `public_key::String`: Public key for authentication. |
| 49 | +- `secret_key::String`: Secret key for authentication. |
| 50 | +- `interface::String`: Interface for the client. |
| 51 | +- `proxy::String`: Proxy information for the client. |
| 52 | +- `account_name::String`: Account name associated with the client. |
| 53 | +- `description::String`: Description of the client. |
| 54 | +""" |
| 55 | +Base.@kwdef struct BithumbClient <: AbstractAPIsClient |
| 56 | + base_url::String |
| 57 | + public_key::Maybe{String} = nothing |
| 58 | + secret_key::Maybe{String} = nothing |
| 59 | + interface::Maybe{String} = nothing |
| 60 | + proxy::Maybe{String} = nothing |
| 61 | + account_name::Maybe{String} = nothing |
| 62 | + description::Maybe{String} = nothing |
| 63 | +end |
| 64 | + |
| 65 | +""" |
| 66 | + BithumbAPIError{T} <: AbstractAPIsError |
| 67 | +
|
| 68 | +Exception thrown when an API method fails with code `T`. |
| 69 | +
|
| 70 | +## Required fields |
| 71 | +- `status::Int64`: Error status code. |
| 72 | +
|
| 73 | +## Optional fields |
| 74 | +- `message::String`: Error message. |
| 75 | +""" |
| 76 | +struct BithumbAPIError{T} <: AbstractAPIsError |
| 77 | + status::Int64 |
| 78 | + message::Maybe{String} |
| 79 | + |
| 80 | + function BithumbAPIError(status::Int64, x...) |
| 81 | + return new{status}(status, x...) |
| 82 | + end |
| 83 | +end |
| 84 | + |
| 85 | +function Base.show(io::IO, e::BithumbAPIError) |
| 86 | + return print(io, "status = ", "\"", e.status, "\"", ", ", "message = ", "\"", e.message, "\"") |
| 87 | +end |
| 88 | + |
| 89 | +CryptoAPIs.error_type(::BithumbClient) = BithumbAPIError |
| 90 | + |
| 91 | +function CryptoAPIs.request_sign!(::BithumbClient, query::Q, ::String)::Q where {Q<:BithumbPublicQuery} |
| 92 | + return query |
| 93 | +end |
| 94 | + |
| 95 | +function CryptoAPIs.request_sign!(client::BithumbClient, query::Q, endpoint::String)::Q where {Q<:BithumbPrivateQuery} |
| 96 | + query.nonce = Dates.now(UTC) |
| 97 | + query.endpoint = Serde.SerQuery.escape_query("/" * endpoint) |
| 98 | + query.signature = nothing |
| 99 | + body = Serde.to_query(query) |
| 100 | + salt = string("/", endpoint, Char(0), body, Char(0), round(Int64, 1000 * datetime2unix(query.nonce))) |
| 101 | + query.signature = Base64.base64encode(hexdigest("sha512", client.secret_key, salt)) |
| 102 | + return query |
| 103 | +end |
| 104 | + |
| 105 | +function CryptoAPIs.request_sign!(::BithumbClient, query::Q, ::String)::Q where {Q<:BithumbAccessQuery} |
| 106 | + return query |
| 107 | +end |
| 108 | + |
| 109 | +function CryptoAPIs.request_body(::Q)::String where {Q<:BithumbCommonQuery} |
| 110 | + return "" |
| 111 | +end |
| 112 | + |
| 113 | +function CryptoAPIs.request_query(query::Q)::String where {Q<:BithumbCommonQuery} |
| 114 | + return Serde.to_query(query) |
| 115 | +end |
| 116 | + |
| 117 | +function CryptoAPIs.request_headers(client::BithumbClient, ::BithumbPublicQuery)::Vector{Pair{String,String}} |
| 118 | + return Pair{String,String}[ |
| 119 | + "Content-Type" => "application/json" |
| 120 | + ] |
| 121 | +end |
| 122 | + |
| 123 | +function CryptoAPIs.request_headers(client::BithumbClient, query::BithumbPrivateQuery)::Vector{Pair{String,String}} |
| 124 | + return Pair{String,String}[ |
| 125 | + "Api-Key" => client.public_key, |
| 126 | + "Api-Sign" => query.signature, |
| 127 | + "Api-Nonce" => string(round(Int64, 1000 * datetime2unix(query.nonce))), |
| 128 | + ] |
| 129 | +end |
| 130 | + |
| 131 | +include("Utils.jl") |
| 132 | +include("Errors.jl") |
| 133 | + |
| 134 | +include("Spot/Spot.jl") |
| 135 | +using .Spot |
| 136 | + |
| 137 | +end |
0 commit comments