Post a comment with balance and QR code

This commit is contained in:
kagel 2016-09-07 03:20:17 +03:00
parent cfbfff4d30
commit d404e7d34f
6 changed files with 81 additions and 13 deletions

View File

@ -37,6 +37,7 @@
[org.webjars/webjars-locator-jboss-vfs "0.1.0"]
[luminus-immutant "0.2.2"]
[overtone/at-at "1.2.0"]
[clj.qrgen "0.4.0"]
[tentacles "0.5.1"]]
:min-lein-version "2.0.0"

View File

@ -102,11 +102,25 @@ UPDATE issues
SET transaction_hash = :transaction_hash
WHERE issue_id = :issue_id;
-- :name update-contract-address :! :n
-- :name update-contract-address :<! :1
-- :doc updates contract-address for a given issue
UPDATE issues
WITH t AS (
SELECT
i.issue_id AS issue_id,
i.issue_number AS issue_number,
i.title AS title,
i.transaction_hash AS transaction_hash,
i.contract_address AS contract_address,
r.login AS login,
r.repo AS repo
FROM issues i
INNER JOIN repositories r ON r.repo_id = i.repo_id
WHERE i.issue_id = :issue_id
)
UPDATE issues i
SET contract_address = :contract_address
WHERE issue_id = :issue_id;
FROM t
RETURNING t.issue_id, t.issue_number, t.title, t.transaction_hash, t.contract_address, t.login, t.repo;
-- :name list-pending-deployments :? :*
-- :doc retrieves pending transaction ids

View File

@ -5,7 +5,8 @@
[overtone.at-at :refer [every mk-pool]]
[commiteth.config :refer [env]]
[commiteth.db.issues :as issues]
[clojure.tools.logging :as log]))
[clojure.tools.logging :as log]
[commiteth.github.core :as github]))
(def eth-rpc-url "http://localhost:8545")
(defn eth-account [] (:eth-account env))
@ -24,6 +25,22 @@
(log/error "Method: " method ", error: " error))
(:result result)))
(defn hex->big-integer
[hex]
(new BigInteger (subs hex 2) 16))
(defn from-wei
[wei]
(/ wei 1000000000000000000))
(defn get-balance-wei
[account]
(hex->big-integer (eth-rpc "eth_getBalance" [account "latest"])))
(defn get-balance-eth
[account digits]
(->> (get-balance-wei account) from-wei double (format (str "%." digits "f"))))
(defn send-transaction
[from to value & [params]]
(eth-rpc "personal_signAndSendTransaction" [(merge params {:from from
@ -52,6 +69,11 @@
(when-let [receipt (get-transaction-receipt transaction-hash)]
(log/info "transaction receipt for issue #" issue-id ": " receipt)
(when-let [contract-address (:contractAddress receipt)]
(issues/update-contract-address issue-id contract-address)))))
(let [issue (issues/update-contract-address issue-id contract-address)
{user :login
repo :repo
issue-number :issue_number} issue
balance (get-balance-eth contract-address 4)]
(github/post-comment user repo issue-number contract-address balance))))))
(every (* 5 60 1000) update-issue-contract-address pool)

View File

@ -5,7 +5,8 @@
[tentacles.issues :as issues]
[ring.util.codec :as codec]
[clj-http.client :as http]
[commiteth.config :refer [env]])
[commiteth.config :refer [env]]
[clojure.tools.logging :as log])
(:import [java.util UUID]))
(defn server-address [] (:server-address env))
@ -92,14 +93,28 @@
(println "removing webhook")
(repos/delete-hook user repo hook-id (auth-params token)))
(defn post-comment
[user repo issue-id]
(issues/create-comment user repo issue-id
(str "a comment with an image link to the web service.") (self-auth-params)))
(defn- get-qr-url
[contract-address]
(str (server-address) "/qr.png?address=" contract-address))
(defn get-commit
[user repo commit-id]
(repos/specific-commit user repo commit-id (self-auth-params)))
(defn- md-url
([text url]
(str "[" text "](" url ")"))
([url]
(md-url url url)))
(defn- md-image
[alt src]
(str "!" (md-url alt src)))
(defn post-comment
[user repo issue-id contract-address balance]
(let [balance (str balance " ETH")
image-url (md-image "QR Code" (get-qr-url contract-address))
site-url (md-url (server-address) (server-address))
comment (format "Current balance: %s\n%s\n%s" balance image-url site-url)]
(log/info "Comment to" (str user "/" repo) ":" comment)
(issues/create-comment user repo issue-id comment (self-auth-params))))
(defn get-issue
[user repo issue-number]

View File

@ -5,6 +5,7 @@
[commiteth.routes.redirect :refer [redirect-routes]]
[commiteth.routes.services :refer [service-routes]]
[commiteth.routes.webhooks :refer [webhook-routes]]
[commiteth.routes.qrcodes :refer [qr-routes]]
[compojure.route :as route]
[ring.middleware.json :refer [wrap-json-params]]
[ring.middleware.keyword-params :refer [wrap-keyword-params]]
@ -26,6 +27,7 @@
(wrap-routes wrap-json-params)
(wrap-routes wrap-keyword-params))
#'service-routes
#'qr-routes
(route/not-found
(:body
(error-page {:status 404

View File

@ -0,0 +1,14 @@
(ns commiteth.routes.qrcodes
(:require [ring.util.http-response :refer :all]
[compojure.api.sweet :refer :all]
[clj.qrgen :as qr]))
(defn generate-qr-code
[address]
(qr/as-input-stream
(qr/from (str "ethereum:" address) :size [256 256])))
(defapi qr-routes
(context "/qr.png" []
(GET "/" {{address :address} :params}
(ok (generate-qr-code address)))))