/
core.clj
94 lines (84 loc) · 2.85 KB
/
core.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
(ns clj-kwallet.core
(:require [clj-http.client :as http]
[clojure.data.json :as json])
(:gen-class))
(def ^:dynamic wallet-connection
{:base "http://localhost:3000"
:network "testnet"})
(defn- make-request
[type url data]
(try (let [response (http/request {:method type
:url url
:content-type :json
:accept :json
:body (json/write-str data)})]
(json/read-str (:body response) :key-fn #'keyword))
(catch Exception e (println e))))
(defn- make-url
[account-hash action asset & [position network]]
(apply str
(interpose "/"
[(:base wallet-connection)
account-hash
action
(or network
"mainnet")
asset
(or position 0)])))
(defn get-address
[account-hash password asset & [position network]]
(make-request :POST
(make-url account-hash
"address"
asset
position
(or network "mainnet"))
{:password password}))
(defn get-balance
[account-hash password asset & [position network]]
(let [x (make-request :POST
(make-url account-hash
"balance"
asset
position
(or network "mainnet"))
{:password password})
]
x))
(defn send-amount
[account-hash password asset position amount to-address & [ network ]]
(make-request :POST
(make-url account-hash
"send"
asset
position
(or network "mainnet"))
{:to to-address
:amount (if (string? amount) amount
(format "%.8f" (try (* amount 1.0) (catch Exception e 0.0))))
:password password}))
(defn create-account
[]
(let [url (str (:base wallet-connection)
"/generate")]
(make-request :GET url {})))
(defn generate-wallet
[{:keys [account password network]
:or {network "mainnet"}}]
(fn [ & [wrapper wallet-fn
asset position
args]]
(let [network network
position (or position 0)
wrapper (or wrapper identity)
wallet-fn (or wallet-fn (fn [& ags]
(if (= (count ags) 0)
(first ags) ags)))]
(wrapper
(apply wallet-fn
(concat
[account password
asset
position]
args
[network]))))))