Skip to content

Commit

Permalink
v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aviad committed Sep 29, 2013
1 parent 4458cba commit 72bf975
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 38 deletions.
Empty file added CHANGES.org
Empty file.
6 changes: 0 additions & 6 deletions README.md

This file was deleted.

74 changes: 74 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
* clj-btc
Clojure bindings for the original (C++) Bitcoin Client json-rpc API.
For the full list of calls, see: [[https://en.bitcoin.it/wiki/Original_Bitcoin_client/API_calls_list.][API calls list]]

* Installation

** [[https://github.com/technomancy/leiningen][leiningen]]
add this to your =project.clj='s =:dependencies=:

#+BEGIN_EXAMPLE
[clj-btc "0.1.0"]
#+END_EXAMPLE

** [[http://maven.apache.org/][Maven]]
#+BEGIN_EXAMPLE
<dependency>
<groupId>clj-btc</groupId>
<artifactId>clj-btc</artifactId>
<version>0.1.0</version>
</dependency>
#+END_EXAMPLE


* Usage

To call the method (for example) =getinfo= on a bitcoind according
to the local default =bitcoin.conf= file:
#+BEGIN_EXAMPLE
(require '[clj-btc.core :as btc])
;;=> nil
user=> (btc/getinfo)
;;=> {"timeoffset" 0, "protocolversion" 70001, "blocks" 111908, "errors" "",
"testnet" true, "proxy" "", "connections" 4, "version" 80500,
"keypoololdest" 1380388750, "paytxfee" 0E-8M,
"difficulty" 4642.44443532M, "keypoolsize" 101, "balance" 0E-8M,
"walletversion" 60000}
#+END_EXAMPLE

By default, *any* call on an rpc-method will read the (default) local
configuration file. However, since this is somewhat less than
efficient, it is possible to specify a configuration manually. The
configuration must contain the keys
=:rpcpassword :rpcuser :testnet :rpcport= and =:rpchost=.
#+BEGIN_EXAMPLE
user=> (def config {:rpcpassword "PASSWORD", :rpcuser "USR", :testnet true,
:rpcport 18332, :rpchost "http://127.0.0.1"})
;;=> #'user/config
user=> (btc/getbalace :config config) ; no file is read to get configs
;;=> 2.718281M
#+END_EXAMPLE

It is also possible to use a different configuration file by
passing its path to =clj-btc.config/parse-config=:
#+BEGIN_EXAMPLE
user=> (require '[clj-btc.config :only (parse-config)])
;;=> nil
user=> (def config (parse-config "/path/to/my.conf"))
;;=> #'user/config
user=> (btc/getbalace :config config) ; no file is read to get configs
;;=> 777M
#+END_EXAMPLE


* Future Work [0/4]

- [ ] Add tests for all the rpc methods.
- [ ] Add types with [[https://github.com/clojure/core.typed][core.typed]].
- [ ] Improve documentation with more examples.
- [ ] Add clojurescript support via =cljx=.


* Changes

See CHANGES.org at the root of this repo.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject clj-btc "0.1.0-SNAPSHOT"
(defproject clj-btc "0.1.0"
:description "Clojure bindings for the original (C++) Bitcoin Client"
:url "https://github.com/aviad/clj-btc"
:license {:name "Eclipse Public License"
Expand Down
10 changes: 5 additions & 5 deletions src/clj_btc/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
[(nix-data-dir) ".bitcoin" "bitcoin.conf"])]
(str (apply jio/file path))))

;; Straight from http://stackoverflow.com/questions/7777882/loading-configuration-file-in-clojure-as-data-structure
(defn- parse-config [file-name]
;; Straight from http://stackoverflow.com/questions/7777882/loading-configuration-file-in-clojure-as-data-structure
(defn parse-config [file-name]
"read the file according to the given "
(let [config
(with-open [reader (jio/reader file-name)]
(let [props (java.util.Properties.)]
Expand All @@ -31,10 +32,9 @@
testnet (and (integer? (:testnet config))
(> (:testnet config) 0))]
;; add default values
(merge {:testnet testnet,
:rpcport (if testnet 18332 8332),
(merge {:rpcport (if testnet 18332 8332),
:rpchost "http://127.0.0.1"}
config)))
(assoc config :testnet testnet))))

(defn read-local-config
"Return a Map of properties from the given file, or from the default
Expand Down
54 changes: 28 additions & 26 deletions test/clj_btc/core_test.clj
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
(ns clj-btc.core-test
(:require [clojure.test :refer :all]
[clj-btc.core :refer :all]
[clj-btc.config :refer :all]))

;;; Since clj-btc is a wrapper around the C++ Bitcoin client, all the
;;; tests are integration tests - making sure the different functions
;;; are able to return the correct values, assuming that a
;;; "Bitcoin-Qt -server"/"bitcoind" is running and accessible according to the
;;; local configuration file.

(ns clj-btc.core-test
(:require [clojure.test :refer :all]
[clj-btc.core :refer :all]
[clj-btc.config :refer :all]))

(def cfg (atom {}))
(def addr-with-bitcoins
{:public "mqWiQRdS6MePCtALRg2smULFvqf8Ru1usj",
:private "cVGghpMADPnrX6YNnS7X8nSEnULR6epfJFHLyw9dvKi6n17tUkDg"})
;; (def addr-with-bitcoins
;; {:public "mqWiQRdS6MePCtALRg2smULFvqf8Ru1usj",
;; :private "cVGghpMADPnrX6YNnS7X8nSEnULR6epfJFHLyw9dvKi6n17tUkDg"})

(defn config-fixture
[f]
Expand Down Expand Up @@ -82,22 +82,24 @@
:bitcoinaddress "invalid-address")
"isvalid")))))

(deftest send-payment
(let [imported-account
(str "IMPORTED-" (subs (str (java.util.UUID/randomUUID)) 0 8))
;; (deftest send-payment
;; (let [imported-account
;; (str "IMPORTED-" (subs (str (java.util.UUID/randomUUID)) 0 8))

receiving-account (str "RECEIVING-"
(subs (str (java.util.UUID/randomUUID))
0 8))
receiving-address (getaccountaddress :config @cfg
:account receiving-account)]
(prn "importing private key and rescaning, this may take a few seconds.")
(when (nil?
(importprivkey :config @cfg
:bitcoinprivkey (:private addr-with-bitcoins)
:label imported-account :rescan true))
;; import was successful
(sendtoaddress :bitcoinaddress receiving-address :amount 1E-8
:comment "sending 1 (testcoin) satoshi.to test btc-clj.")
(is (= 1E-8 (getbalance :config @cfg :account receiving-account
:minconf 0))))))
;; receiving-account (str "RECEIVING-"
;; (subs (str (java.util.UUID/randomUUID))
;; 0 8))
;; receiving-address (getaccountaddress :config @cfg
;; :account receiving-account)]
;; (prn "importing private key and rescaning, this may take a few seconds.")
;; (when (nil?
;; (importprivkey :config @cfg
;; :bitcoinprivkey (:private addr-with-bitcoins)
;; :label imported-account :rescan true))
;; ;; import was successful
;; (sendtoaddress
;; :config @cfg :bitcoinaddress receiving-address :amount 1E-4
;; :comment "sending 1E-4 (testcoin) BTC .to test clj-btc.")
;; (is (= 1E-4M (getbalance :config @cfg :account receiving-account
;; :minconf 0))))
;; (prn "done.")))

0 comments on commit 72bf975

Please sign in to comment.