diff --git a/resources/migrations/20160913192604-add-balance-column.down.sql b/resources/migrations/20160913192604-add-balance-column.down.sql new file mode 100644 index 0000000..767642a --- /dev/null +++ b/resources/migrations/20160913192604-add-balance-column.down.sql @@ -0,0 +1,3 @@ +-- noinspection SqlResolve +ALTER TABLE public.issues + DROP COLUMN balance; diff --git a/resources/migrations/20160913192604-add-balance-column.up.sql b/resources/migrations/20160913192604-add-balance-column.up.sql new file mode 100644 index 0000000..35bf83b --- /dev/null +++ b/resources/migrations/20160913192604-add-balance-column.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE public.issues + ADD balance VARCHAR(32) DEFAULT '0x0'; diff --git a/resources/sql/queries.sql b/resources/sql/queries.sql index f285dea..50894ef 100644 --- a/resources/sql/queries.sql +++ b/resources/sql/queries.sql @@ -248,3 +248,15 @@ FROM issues i INNER JOIN repositories r ON r.repo_id = i.repo_id WHERE i.issue_number = :issue_number AND r.login = :login AND r.repo = :repo; + +-- :name get-balance :? :1 +-- :doc gets current balance of a wallet attached to a given issue +SELECT balance +FROM issues +WHERE contract_address = :contract_address; + +-- :name update-banlance :! :n +-- :doc updates balance of a wallet attached to a given issue +UPDATE issues +SET balance = :balance +WHERE contract_address = :contract_address; diff --git a/src/clj/commiteth/db/issues.clj b/src/clj/commiteth/db/issues.clj index 88b5833..3281fb2 100644 --- a/src/clj/commiteth/db/issues.clj +++ b/src/clj/commiteth/db/issues.clj @@ -44,3 +44,14 @@ [] (jdbc/with-db-connection [con-db *db*] (db/list-pending-deployments con-db))) + +(defn get-balance + [contract-address] + (jdbc/with-db-connection [con-db *db*] + (db/get-balance con-db {:contract_address contract-address}))) + +(defn update-balance + [contract-address balance] + (jdbc/with-db-connection [con-db *db*] + (db/update-balance con-db {:contract_address contract-address + :balance balance}))) diff --git a/src/clj/commiteth/eth/core.clj b/src/clj/commiteth/eth/core.clj index d555ac2..a7c90e4 100644 --- a/src/clj/commiteth/eth/core.clj +++ b/src/clj/commiteth/eth/core.clj @@ -32,6 +32,10 @@ [wei] (/ wei 1000000000000000000)) +(defn hex->eth + [hex digits] + (->> hex hex->big-integer from-wei double (format (str "%." digits "f")))) + (defn get-balance-hex [account] (eth-rpc "eth_getBalance" [account "latest"])) @@ -42,7 +46,7 @@ (defn get-balance-eth [account digits] - (->> (get-balance-wei account) from-wei double (format (str "%." digits "f")))) + (hex->eth (get-balance-hex account) digits)) (defn send-transaction [from to value & [params]] diff --git a/src/clj/commiteth/scheduler.clj b/src/clj/commiteth/scheduler.clj index d76ca76..d09aa06 100644 --- a/src/clj/commiteth/scheduler.clj +++ b/src/clj/commiteth/scheduler.clj @@ -51,8 +51,12 @@ comment-id :comment_id issue-number :issue_number} (bounties/list-wallets)] (when comment-id - (let [balance (eth/get-balance-eth contract-address 8)] - (github/update-comment login repo comment-id issue-number balance))))) + (let [old-balance (issues/get-balance contract-address) + current-balance-hex (eth/get-balance-hex contract-address) + current-balance-eth (eth/hex->eth current-balance-hex 8)] + (when-not (= old-balance current-balance-hex) + (issues/update-balance contract-address current-balance-hex) + (github/update-comment login repo comment-id issue-number current-balance-eth)))))) (mount/defstate scheduler :start (do