From f8d00196d640f678b0cd45c2981403c9c61c5b33 Mon Sep 17 00:00:00 2001 From: Teemu Patja Date: Tue, 22 Aug 2017 16:45:10 +0300 Subject: [PATCH] DB storage for token balances * store token bounties to DB when changed * added some TODOs --- resources/sql/queries.sql | 31 ++++++++++++++++++++----------- src/clj/commiteth/db/issues.clj | 15 ++++++++------- src/clj/commiteth/scheduler.clj | 22 ++++++++++++++++------ 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/resources/sql/queries.sql b/resources/sql/queries.sql index 246bfaa..5fe7c74 100644 --- a/resources/sql/queries.sql +++ b/resources/sql/queries.sql @@ -252,6 +252,7 @@ SELECT i.issue_number AS issue_number, i.issue_id AS issue_id, i.balance_eth AS balance, + i.tokens AS tokens, u.login AS winner_login, u.address AS payout_address FROM issues i, pull_requests p, users u, repositories r @@ -288,6 +289,7 @@ SELECT i.issue_number AS issue_number, i.issue_id AS issue_id, i.balance_eth AS balance, + i.tokens AS tokens, u.address AS payout_address, u.login AS payee_login, i.payout_hash AS payout_hash @@ -329,6 +331,14 @@ updated = timezone('utc'::text, now()) WHERE issue_id = :issue_id; +-- :name update-token-balances :! :n +-- :doc updates issue with given token balances +UPDATE issues +SET tokens = :token_balances::jsonb, +updated = timezone('utc'::text, now()) +WHERE contract_address = :contract_address; + + -- :name open-bounties :? :* -- :doc all bounty issues for given owner SELECT @@ -338,6 +348,7 @@ SELECT i.title AS issue_title, i.repo_id AS repo_id, i.balance_eth AS balance, + i.tokens AS tokens, i.confirm_hash AS confirm_hash, i.payout_hash AS payout_hash, i.payout_receipt AS payout_receipt, @@ -349,7 +360,7 @@ FROM issues i, repositories r WHERE r.repo_id = i.repo_id AND i.confirm_hash is null -ORDER BY balance desc, updated desc; +ORDER BY balance desc, updated desc; -- TODO: order by USD value first @@ -361,7 +372,8 @@ SELECT i.issue_number AS issue_number, i.title AS issue_title, i.repo_id AS repo_id, - i.balance_eth AS balance, + i.balance_eth AS balance_eth, + i.tokens AS tokens, i.confirm_hash AS confirm_hash, i.payout_hash AS payout_hash, i.payout_receipt AS payout_receipt, @@ -384,6 +396,7 @@ SELECT i.title AS issue_title, i.repo_id AS repo_id, i.balance_eth AS balance, + i.tokens AS tokens, i.confirm_hash AS confirm_hash, i.payout_hash AS payout_hash, i.payout_receipt AS payout_receipt, @@ -417,7 +430,8 @@ SELECT i.comment_id AS comment_id, i.issue_number AS issue_number, i.issue_id AS issue_id, - i.balance_eth AS balance + i.balance_eth AS balance, + i.tokens AS tokens FROM issues i, repositories r WHERE r.repo_id = i.repo_id AND contract_address IS NOT NULL @@ -430,6 +444,7 @@ SELECT i.issue_id AS issue_id, i.issue_number AS issue_number, i.balance_eth AS balance, + i.tokens AS tokens, r.owner AS owner, r.repo AS repo FROM issues i, repositories r @@ -438,13 +453,7 @@ AND r.repo_id = i.repo_id AND r.owner = :owner AND r.repo = :repo; --- :name get-balance :? :1 --- :doc gets current balance of a wallet attached to a given issue -SELECT balance_eth AS balance -FROM issues -WHERE contract_address = :contract_address; - --- :name update-balance :! :n +-- :name update-eth-balance :! :n -- :doc updates balance of a wallet attached to a given issue UPDATE issues SET balance_eth = :balance, @@ -484,7 +493,7 @@ pr.commit_sha = i.commit_sha AND u.id = pr.user_id AND i.payout_receipt IS NOT NULL GROUP BY u.id -ORDER BY total_eth DESC; +ORDER BY total_eth DESC; -- TODO: this should work with value_usd -- :name bounties-activity :? :* diff --git a/src/clj/commiteth/db/issues.clj b/src/clj/commiteth/db/issues.clj index 8eac323..5e49ea3 100644 --- a/src/clj/commiteth/db/issues.clj +++ b/src/clj/commiteth/db/issues.clj @@ -64,13 +64,14 @@ (jdbc/with-db-connection [con-db *db*] (db/list-failed-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 +(defn update-eth-balance [contract-address balance] (jdbc/with-db-connection [con-db *db*] - (db/update-balance con-db {:contract_address contract-address + (db/update-eth-balance con-db {:contract_address contract-address :balance balance}))) + +(defn update-token-balances + [contract-address balances] + (jdbc/with-db-connection [con-db *db*] + (db/update-token-balances con-db {:contract_address contract-address + :token_balances balances}))) diff --git a/src/clj/commiteth/scheduler.clj b/src/clj/commiteth/scheduler.clj index a123605..fb19247 100644 --- a/src/clj/commiteth/scheduler.clj +++ b/src/clj/commiteth/scheduler.clj @@ -157,16 +157,23 @@ repo :repo comment-id :comment_id issue-id :issue_id - old-balance :balance + db-balance-eth :balance + db-tokens :tokens issue-number :issue_number} (db-bounties/open-bounty-contracts)] (when comment-id (let [current-balance-eth-str (eth/get-balance-eth contract-address 6) - current-balance-eth (read-string current-balance-eth-str)] + current-balance-eth (read-string current-balance-eth-str) + current-token-balances (multisig/token-balances contract-address)] (log/debug "update-balances" current-balance-eth current-balance-eth-str owner repo issue-number) - (when-not (float= old-balance current-balance-eth) + (when (or + (not (float= db-balance-eth current-balance-eth)) + (not= db-tokens current-token-balances)) (log/debug "balances differ") - (issues/update-balance contract-address current-balance-eth) + (-> contract-address + (issues/update-eth-balance current-balance-eth) + (issues/update-token-balances current-token-balances)) + ;; TODO: comment and comment image will show tokens and USD value (bounties/update-bounty-comment-image issue-id owner repo @@ -204,8 +211,11 @@ + key total-usd -> current total USD value for all funds" [bounty-addr] (let [token-balances (multisig/token-balances bounty-addr) - eth-balance (read-string (eth/get-balance-eth bounty-addr 4))] - (merge token-balances {:ETH eth-balance}))) + eth-balance (read-string (eth/get-balance-eth bounty-addr 4)) + all-funds + (merge token-balances + {:ETH eth-balance})] + (merge all-funds {:total-usd (fiat-util/bounty-usd-value all-funds)})))