diff --git a/resources/migrations/20160905235051-contracts.down.sql b/resources/migrations/20160905235051-contracts.down.sql index 1bfa275..1fd9a7b 100644 --- a/resources/migrations/20160905235051-contracts.down.sql +++ b/resources/migrations/20160905235051-contracts.down.sql @@ -3,5 +3,7 @@ ALTER TABLE public.issues DROP COLUMN transaction_hash; ALTER TABLE public.issues DROP COLUMN contract_address; +ALTER TABLE public.issues + DROP COLUMN confirm_hash; ALTER TABLE public.issues ADD address VARCHAR(256); diff --git a/resources/migrations/20160905235051-contracts.up.sql b/resources/migrations/20160905235051-contracts.up.sql index 8a47b89..6be7006 100644 --- a/resources/migrations/20160905235051-contracts.up.sql +++ b/resources/migrations/20160905235051-contracts.up.sql @@ -2,6 +2,8 @@ ALTER TABLE public.issues ADD transaction_hash VARCHAR(128) NULL; ALTER TABLE public.issues ADD contract_address VARCHAR(42) NULL; +ALTER TABLE public.issues + ADD confirm_hash VARCHAR(128) NULL; -- noinspection SqlResolve ALTER TABLE public.issues DROP COLUMN address; diff --git a/resources/sql/queries.sql b/resources/sql/queries.sql index 7a57865..8da7336 100644 --- a/resources/sql/queries.sql +++ b/resources/sql/queries.sql @@ -155,6 +155,26 @@ INSERT INTO pull_requests (repo_id, pr_id, pr_number, issue_number, commit_id, u -- Bounties ------------------------------------------------------------------------ +-- :name pending-bounties-list :? :* +-- :doc lists all recently closed issues awaiting to be signed +SELECT + i.contract_address AS contract_address, + i.issue_id AS issue_id, + u.address AS payout_address +FROM issues i + INNER JOIN pull_requests p + ON (p.commit_id = i.commit_id OR coalesce(p.issue_number, -1) = i.issue_number) + AND p.repo_id = i.repo_id + INNER JOIN users u + ON u.id = p.user_id +WHERE i.confirm_hash IS NULL; + +-- :name update-confirm-hash :! :n +-- :doc updates issue with transaction hash +UPDATE issues +SET confirm_hash = :confirm_hash +WHERE issue_id = :issue_id; + -- :name bounties-list :? :* -- :doc lists fixed issues SELECT diff --git a/src/clj/commiteth/db/bounties.clj b/src/clj/commiteth/db/bounties.clj index 7b9a046..0166366 100644 --- a/src/clj/commiteth/db/bounties.clj +++ b/src/clj/commiteth/db/bounties.clj @@ -13,3 +13,13 @@ [owner-id] (jdbc/with-db-connection [con-db *db*] (db/issues-list con-db {:owner_id owner-id}))) + +(defn pending-bounties-list + [] + (jdbc/with-db-connection [con-db *db*] + (db/pending-bounties-list con-db))) + +(defn update-confirm-hash + [issue-id confirm-hash] + (jdbc/with-db-connection [con-db *db*] + (db/update-confirm-hash con-db {:issue_id issue-id :confirm_hash confirm-hash}))) diff --git a/src/clj/commiteth/eth/core.clj b/src/clj/commiteth/eth/core.clj index cf45bb3..d555ac2 100644 --- a/src/clj/commiteth/eth/core.clj +++ b/src/clj/commiteth/eth/core.clj @@ -32,9 +32,13 @@ [wei] (/ wei 1000000000000000000)) +(defn get-balance-hex + [account] + (eth-rpc "eth_getBalance" [account "latest"])) + (defn get-balance-wei [account] - (hex->big-integer (eth-rpc "eth_getBalance" [account "latest"]))) + (hex->big-integer (get-balance-hex account))) (defn get-balance-eth [account digits] diff --git a/src/clj/commiteth/eth/multisig_wallet.clj b/src/clj/commiteth/eth/multisig_wallet.clj index 2381ce4..a659c76 100644 --- a/src/clj/commiteth/eth/multisig_wallet.clj +++ b/src/clj/commiteth/eth/multisig_wallet.clj @@ -12,3 +12,11 @@ (defn add-owner [contract owner] (eth/execute (eth/eth-account) contract "0x7065cb48" owner)) + +(defn execute + [contract to value] + (eth/execute (eth/eth-account) contract "0xb61d27f6" to value)) + +(defn confirm + [contract hash] + (eth/execute (eth/eth-account) contract "0x797af627" hash)) diff --git a/src/clj/commiteth/scheduler.clj b/src/clj/commiteth/scheduler.clj index 5614ae1..89dbed4 100644 --- a/src/clj/commiteth/scheduler.clj +++ b/src/clj/commiteth/scheduler.clj @@ -4,13 +4,17 @@ [commiteth.github.core :as github] [commiteth.db.issues :as issues] [commiteth.db.users :as users] + [commiteth.db.bounties :as bounties] [overtone.at-at :refer [every mk-pool]] [clojure.tools.logging :as log] [mount.core :as mount])) (def pool (mk-pool)) -(defn update-issue-contract-address [] +(defn update-issue-contract-address + "For each pending deployment: + gets transasction receipt, updates db state, posts github comment and adds owners to the wallet" + [] (for [{issue-id :issue_id transaction-hash :transaction_hash} (issues/list-pending-deployments)] (when-let [receipt (eth/get-transaction-receipt transaction-hash)] @@ -27,4 +31,18 @@ (wallet/add-owner contract-address (eth/eth-account)) (wallet/add-owner contract-address repo-owner-address)))))) -(mount/defstate scheduler :start (every (* 5 60 1000) update-issue-contract-address pool)) +(defn self-sign-bounty + "Walks through all issues eligible for bounty payout and signs corresponding transaction" + [] + (for [{contract-address :contract_address + issue-id :issue_id + payout-address :payout_address} (bounties/pending-bounties-list)] + (let [value (eth/get-balance-hex contract-address)] + (->> + (wallet/execute contract-address payout-address value) + (bounties/update-confirm-hash issue-id))))) + +(mount/defstate scheduler :start + (do + (every (* 5 60 1000) update-issue-contract-address pool) + (every (* 60 1000) self-sign-bounty pool)))