如何从Clojure调用json rpc?

如何从Clojure调用json rpc?,clojure,json-rpc,Clojure,Json Rpc,我在端口8545上运行了一个本地服务器,它监听JSON-RPC请求。我可以这样用curl来称呼它: curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0xf54c19d9ef3873bfd1f7a622d02d86249a328f06", "latest"],"id":1}' http://localhost:8545 Clojure的等效呼叫是什么?我需要向project.clj添加一些外部库

我在端口8545上运行了一个本地服务器,它监听JSON-RPC请求。我可以这样用curl来称呼它:

curl -X POST --data '{"jsonrpc":"2.0","method":"eth_getBalance","params":["0xf54c19d9ef3873bfd1f7a622d02d86249a328f06", "latest"],"id":1}' http://localhost:8545
Clojure的等效呼叫是什么?我需要向project.clj添加一些外部库吗?

我想你应该试试。 您还需要一些json库(或)

因此,将以下依赖项添加到您的
项目.clj

  • [http工具包“2.1.18”]
  • [org.clojure/data.json“0.2.6”]
试试这个

(ns your-ns
  (:require [org.httpkit.client :as http]
            [clojure.data.json :as json]))

(let [url "http://localhost:8545"
      body (json/write-str 
             {:jsonrpc "2.0"
              :method "eth_getBalance"
              :params ["0xf54c19d9ef3873bfd1f7a622d02d86249a328f06" "latest"]
              :id 1})
      options {:body body}
      result @(http/post url options)]
  (prn result))

我有一个类似的用例,所以我创建了一个用于进行JSON-RPC调用的小Clojure。有了这个,你可以

(ns example.core
  (:require [json-rpc.core :as rpc]))

(with-open [channel (rpc/open "http://localhost:8545")]
  (rpc/send! channel "eth_blockNumber" ["latest"]))
  ;; => {:result "0x14eca", :id "6fd9a7a8-c774-4b76-a61e-6802ae64e212"}
,样板将为您处理