Skip to content
This repository has been archived by the owner on Apr 26, 2019. It is now read-only.

Commit

Permalink
Merge branch 'develop' into feature/readd-bounty-#185
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitaliy Vlasov authored Mar 20, 2018
2 parents d6c311b + 2eb2de2 commit 8bb9f7c
Show file tree
Hide file tree
Showing 19 changed files with 281 additions and 249 deletions.
3 changes: 2 additions & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
:exclusions [joda-time]]
[re-frame "0.10.2"]
[cljs-ajax "0.7.2"]
[secretary "1.2.3"]
[funcool/bide "1.6.0"]
[reagent-utils "0.2.1"]
[reagent "0.7.0"]
[org.clojure/clojurescript "1.9.946"]
[org.clojure/clojure "1.8.0"]
[selmer "1.11.1"]
[com.taoensso/tufte "1.3.0"]
[markdown-clj "1.0.1"]
[ring-middleware-format "0.7.2"]
[ring/ring-core "1.6.2"]
Expand Down
27 changes: 16 additions & 11 deletions resources/sql/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,16 @@ WHERE i.repo_id = :repo_id
AND i.confirm_hash is null
AND i.is_open = true;

-- :name update-repo-generic :! :n
/* :require [clojure.string :as string]
[hugsql.parameters :refer [identifier-param-quote]] */
-- :name update-repo-name :! :n
UPDATE repositories
SET
/*~
(string/join ","
(for [[field _] (:updates params)]
(str (identifier-param-quote (name field) options)
" = :v:updates." (name field))))
~*/
where repo_id = :repo_id;
SET repo = :repo_name
WHERE repo_id = :repo_id
AND repo != :repo_name

-- :name update-repo-state :! :n
UPDATE repositories
SET state = :repo_state
WHERE repo_id = :repo_id


-- Issues --------------------------------------------------------------------------
Expand Down Expand Up @@ -430,6 +427,14 @@ SELECT exists(SELECT 1
FROM issues
WHERE issue_id = :issue_id);

-- :name get-issue :? :1
-- :doc get issue from DB by repo-id and issue-number
SELECT issue_id, issue_number, is_open, winner_login, commit_sha
FROM issues
WHERE repo_id = :repo_id
AND issue_number = :issue_number;



-- :name open-bounties :? :*
-- :doc all open bounty issues
Expand Down
1 change: 1 addition & 0 deletions resources/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
src="https://www.facebook.com/tr?id=293089407869419&ev=PageView&noscript=1"
/></noscript>
<link href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.1.8/semantic.min.css" rel="stylesheet" type="text/css" />
<link href="https://unpkg.com/[email protected]/css/tachyons.min.css" rel="stylesheet" type="text/css" />
<link href="/css/style.css?v={{commiteth-version}}" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="/js/app.js?v={{commiteth-version}}"></script>
<script>
Expand Down
7 changes: 7 additions & 0 deletions src/clj/commiteth/db/issues.clj
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,14 @@
:exists
boolean)))


(defn contract-from-pool
[owner-address]
(jdbc/with-db-connection [con-db *db*]
(db/contract-from-pool con-db {:address owner-address})))

(defn get-issue
[repo-id issue-number]
(jdbc/with-db-connection [con-db *db*]
(db/get-issue con-db {:repo_id repo-id
:issue_number issue-number})))
14 changes: 9 additions & 5 deletions src/clj/commiteth/db/repositories.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@
(jdbc/with-db-connection [con-db *db*]
(db/get-enabled-repositories con-db {:user_id user-id}))))

(defn update-repo
[repo-id updates]
(defn update-repo-name
[repo-id repo-name]
(jdbc/with-db-connection [con-db *db*]
(db/update-repo-generic con-db {:repo_id repo-id
:updates updates})))

(db/update-repo-name con-db {:repo_id repo-id
:repo_name repo-name})))

(defn update-repo-state
[repo-id repo-state]
(jdbc/with-db-connection [con-db *db*]
(db/update-repo-name con-db {:repo_id repo-id
:repo_state repo-state})))
(defn get-repo
"Get a repo from DB given it's full name (owner/repo-name)"
[full-name]
Expand Down
45 changes: 33 additions & 12 deletions src/clj/commiteth/eth/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
[clojure.java.io :as io]
[commiteth.config :refer [env]]
[clojure.string :refer [join]]
[taoensso.tufte :as tufte :refer (defnp p profiled profile)]
[clojure.tools.logging :as log]
[clojure.string :as str]
[mount.core :as mount]
[pandect.core :as pandect]
[commiteth.util.util :refer [json-api-request]])
(:import [org.web3j
Expand All @@ -26,24 +28,30 @@
(defn auto-gas-price? [] (env :auto-gas-price false))
(defn offline-signing? [] (env :offline-signing true))

(def web3j-obj (atom nil))
(def creds-obj (atom nil))

(defn wallet-file-path []
(env :eth-wallet-file))

(defn wallet-password []
(env :eth-password))

(defn creds []
(let [password (wallet-password)
file-path (wallet-file-path)]
(if (and password file-path)
(WalletUtils/loadCredentials
password
file-path)
(throw (ex-info "Make sure you provided proper credentials in appropriate resources/config.edn"
{:password password :file-path file-path})))))

(defn create-web3j []
(Web3j/build (HttpService. (eth-rpc-url))))
(or @web3j-obj
(swap! web3j-obj (constantly (Web3j/build (HttpService. (eth-rpc-url)))))))

(defn creds []
(or @creds-obj
(let [password (wallet-password)
file-path (wallet-file-path)]
(if (and password file-path)
(swap! creds-obj
(constantly (WalletUtils/loadCredentials
password
file-path)))
(throw (ex-info "Make sure you provided proper credentials in appropriate resources/config.edn"
{:password password :file-path file-path}))))))

(defn get-signed-tx [gas-price gas-limit to data]
"Create a sign a raw transaction.
Expand Down Expand Up @@ -192,7 +200,8 @@

(defn get-balance-eth
[account digits]
(hex->eth (get-balance-hex account) digits))
(p :get-balance-eth
(hex->eth (get-balance-hex account) digits)))

(defn- format-param
[param]
Expand Down Expand Up @@ -296,3 +305,15 @@
(filter true?)
(empty?)))
true))))

(mount/defstate
eth-core
:start
(do
(swap! web3j-obj (constantly nil))
(swap! creds-obj (constantly nil))
(log/info "eth/core started"))
:stop
(log/info "eth/core stopped"))


22 changes: 13 additions & 9 deletions src/clj/commiteth/eth/multisig_wallet.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
:refer [create-web3j creds]]
[commiteth.config :refer [env]]
[clojure.tools.logging :as log]
[taoensso.tufte :as tufte :refer (defnp p profiled profile)]
[commiteth.eth.token-data :as token-data])
(:import [org.web3j
abi.datatypes.Address
Expand Down Expand Up @@ -147,20 +148,22 @@
"Query (internal) ERC20 token balance from bounty contract for given
token TLA."
[bounty-addr token]
(let [bounty-contract (load-bounty-contract bounty-addr)
(p :token-balance-in-bounty
(let [bounty-contract (load-bounty-contract bounty-addr)
token-address (get-token-address token)
token-addr-web3j (Address. token-address)]
(-> bounty-contract
(.tokenBalances token-addr-web3j)
.get
.getValue
(convert-token-value token))))
(convert-token-value token)))))

(defn token-balance
"Query balance of given ERC20 token TLA for given address from ERC20
contract."
[bounty-addr token]
(let [token-address (get-token-address token)]
(p :token-balance
(let [token-address (get-token-address token)]
(log/debug "token-balance" bounty-addr token token-address)
(try
(-> (eth/call token-address
Expand All @@ -170,25 +173,26 @@
(convert-token-value token))
(catch Throwable t
(log/debug "Failed to query token balance " t)
0))))
0)))))


(defn token-balances
"Get a given bounty contract's token balances. Assumes contract's
internal balances have been updated."
[bounty-addr]
(let [bounty-contract (load-bounty-contract bounty-addr)
token-addresses (-> bounty-contract
(p :token-balances
(let [bounty-contract (p :load-bounty-contract (load-bounty-contract bounty-addr))
token-addresses (p :getTokenList (-> bounty-contract
(.getTokenList)
.get)]
.get))]
(if token-addresses
(let [addrs (map str
(.getValue token-addresses))]
(p :getValue (.getValue token-addresses)))]
(into {}
(map (fn [addr] (if-let [info (token-data/token-info-by-addr addr)]
(let [tla (first info)]
[tla (token-balance bounty-addr tla)]))) addrs)))
{})))
{}))))

(defn uint256 [x]
(org.web3j.abi.datatypes.generated.Uint256. x))
Expand Down
68 changes: 1 addition & 67 deletions src/clj/commiteth/routes/services.clj
Original file line number Diff line number Diff line change
Expand Up @@ -46,67 +46,6 @@
(log/debug "token" token "member?" member?)
member?))

(defn enable-repo [repo-id repo full-repo token]
(log/debug "enable-repo" repo-id repo)
(when (github/webhook-exists? full-repo token)
(github/remove-our-webhooks full-repo token))

(let [hook-secret (random/base64 32)]
(repositories/update-repo repo-id {:state 1
:hook_secret hook-secret})
(let [created-hook (github/add-webhook full-repo token hook-secret)]
(log/debug "Created webhook:" created-hook)
(repositories/update-repo repo-id {:hook_id (:id created-hook)})))
(github/create-label full-repo token)
(repositories/update-repo repo-id {:state 2})
(when (add-bounties-for-existing-issues?)
(bounties/add-bounties-for-existing-issues full-repo)))

(defn disable-repo [repo-id full-repo hook-id token]
(log/debug "disable-repo" repo-id full-repo)
(github/remove-webhook full-repo hook-id token)
(repositories/update-repo repo-id {:hook_secret ""
:state 0
:hook_id nil}))

;; NOTE(oskarth): This and above two functions about to be deprecated with Github App
(defn handle-toggle-repo [user params can-create?]
(log/info "XXX handle-toggle-repo" (pr-str user) (pr-str params))
(let [{user-id :id} user
{repo-id :id
full-repo :full_name
owner-avatar-url :owner-avatar-url
token :token
repo :name} params
[owner _] (str/split full-repo #"/")
db-user (users/get-user (:id user))]

(cond (not can-create?)
{:status 400
:body "Please join our Riot - chat.status.im/#/register and request
access in our #openbounty room to have your account whitelisted"}

(empty? (:address db-user))
{:status 400
:body "Please add your ethereum address to your profile first"}

:else
(try
(let [_ (println "CREATING")
db-item (repositories/create (merge params {:user_id user-id
:owner owner}))
is-enabled (= 2 (:state db-item))]
(if is-enabled
(disable-repo repo-id full-repo (:hook_id db-item) token)
(enable-repo repo-id repo full-repo token))
(ok (merge
{:enabled (not is-enabled)}
(select-keys params [:id :full_name]))))
(catch Exception e
(log/error "exception when enabling repo" e)
(repositories/update-repo repo-id {:state -1})
(internal-server-error))))))

(defn in? [coll elem]
(some #(= elem %) coll))

Expand Down Expand Up @@ -286,9 +225,4 @@
:auth-rules authenticated?
:current-user user
(log/debug "/user/bounties")
(ok (user-bounties user)))
(POST "/repository/toggle" {:keys [params]}
;; NOTE: Don't allow anyone to create repos; manual add
:auth-rules authenticated?
:current-user user
(handle-toggle-repo user params (user-whitelisted? (:login user)))))))
(ok (user-bounties user))))))
Loading

0 comments on commit 8bb9f7c

Please sign in to comment.