-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixefloat.clj
152 lines (139 loc) · 5.57 KB
/
fixefloat.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
(ns website.fixedfloat
(:require
[buddy.core.mac :as mac]
[buddy.core.codecs :as codecs]
[website.db.cloud-core :as dat]
[clj-http.client :as client]
[clojure.data.json :as json]))
(defn helper-query-string
"Gets a map with keys
`:fromCurrency` - string
`:fromQty` - string
`:toCurrency` - string
and returns a query string required for fixedfloat API"
[{:keys [fromCurrency fromQty toCurrency]}]
(format "fromCurrency=%s&fromQty=%s&toCurrency=%s&type=float"
fromCurrency fromQty toCurrency))
(defn helper-emergency-string
"Given a map `m` with the needed data creates the query string needed for
set-emergency for fixedfloat api, refund address is hardcoded"
[{:keys [id token]}]
(format "id=%s&token=%s&choice=REFUND&address=0xd9ee72639C12B7e5BBe355E667fDb69Deb6Ca89D"
id token))
(defn sign-query-string
"Signs the `query-string` with the secret-key from fixedfloat"
[query-string]
(-> (mac/hash query-string
{:key secret
:alg :hmac+sha256})
(codecs/bytes->hex)))
(defn quote-order
"Takes a map with keys
`:fromCurrency` - string
`:fromQty` - string
`:toCurrency` - string
make a post request and returns the http response from the fixedfloat API"
[m]
(let [query-string (helper-query-string m)]
(client/post "https://fixedfloat.com/api/v1/getPrice"
{:headers {"X-API-KEY" api-key
"X-API-SIGN" (sign-query-string query-string)}
:content-type "application/x-www-form-urlencoded"
:body query-string})))
(defn create-order-params
"Takes a map with order data comming from the frontend and return the
data needed to place an order on fixedfloat"
[{:keys [from_currency inter_amount to_currency to_address from_amount network]}]
(if (= from_currency "MXN")
{:fromCurrency "BUSDBSC" :fromQty (-> inter_amount
clojure.edn/read-string
(* 0.995)
str)
:toCurrency (if (= "BTC" network)
"BTC"
(get currency-key to_currency)) :toAddress to_address}
(if (= to_currency "MXN")
{:fromCurrency (if (= "BTC" network)
"BTC"
(get currency-key from_currency)) :fromQty from_amount
:toCurrency "BUSDBSC" :toAddress "0xd9ee72639C12B7e5BBe355E667fDb69Deb6Ca89D"})))
(defn create-order
"Takes a map with keys
`:fromCurrency` - string
`:fromQty` - string
`:toCurrency` - string
`:toAddress` - string
places an order on fixedfloat. Check fixedfloat docs for further info"
[m]
(let [query-string (str (helper-query-string m)
"&toAddress=" (:toAddress m))]
(client/post "https://fixedfloat.com/api/v1/createOrder"
{:headers {"X-API-KEY" api-key
"X-API-SIGN" (sign-query-string query-string)}
:content-type "application/x-www-form-urlencoded"
:body query-string})))
(defn get-order
"Takes a map with keys
`:id` - string
`:token` - string
Returns the details for the specified order on fixedfloat"
[{:keys [id token]}]
(let [get-string (str "id=" id "&token=" token)]
(->
(client/get (str "https://fixedfloat.com/api/v1/getOrder?" get-string)
{:headers {"X-API-KEY" api-key
"X-API-SIGN" (sign-query-string get-string)}
:content-type "application/x-www-form-urlencoded"})
:body)))
(defn extract-relevant
"Takes a response from fixedfloat api after an order has been placed
and returns only the relevant data"
[resp-ff]
(let [{:keys [code msg data]} (-> resp-ff
:body
(json/read-str :key-fn keyword))
from (:from data)
to (:to data)]
{:code code
:msg msg
:data {:token (:token data)
:id (:id data)
:from_address_swap (:address from)
:to_amount_swap (:amount to)}}))
(defn set-emergency
"Once an order has succedfully been placed on fixedfloat,
place emergency settings for the order. Input must be the
responce body from `create-order` already with keywords"
[{:keys [data]}]
(let [emergency-string (helper-emergency-string data)]
(client/get (str "https://fixedfloat.com/api/v1/setEmergency?"
emergency-string)
{:headers {"X-API-KEY" api-key
"X-API-SIGN" (sign-query-string emergency-string)}
:content-type "application/x-www-form-urlencoded"})))
(defn relevant-data-swap
"Given a map with data from an order, places the order on
fixedfloat and returns the relevant data"
[{:keys [network] :as m}]
(if (.contains ff-nw network)
(-> m
create-order-params
create-order
extract-relevant)))
(defn valid-fixedfloat-order?
"Check if the order was placed correctly"
[{:keys [code msg]}]
(if (and (= code 0)
(= msg "OK"))
{:valid? true}
{:valid? false :msg msg}))
(defn final-tx-hash
"For a map with `:id` and `:token` keys, returns the
from tx-hash and to tx-hash once the order has been completed as well as the
final amounts"
[m]
(let [order (json/read-str (get-order m) :key-fn keyword)]
{:to_hash (-> order :data :to :tx :id)
:to_amount (-> order :data :to :tx :amount Float/parseFloat)
:from_hash (-> order :data :from :tx :id)
:from_amount (-> order :data :from :tx :amount Float/parseFloat)}))