From a2a569f980b690868edd0f7cdc4a6076f051846c Mon Sep 17 00:00:00 2001 From: Teemu Patja Date: Mon, 21 Aug 2017 00:30:49 +0300 Subject: [PATCH] More token support changes * prepare scheduler for updating bounty balances * fix retrieving of created multisig address * retrieve token info by address * fix method-id for watch(address) --- src/clj/commiteth/eth/multisig_wallet.clj | 20 +++++------------ src/clj/commiteth/eth/token_data.clj | 6 ++++++ src/clj/commiteth/eth/token_registry.clj | 5 +++-- src/clj/commiteth/scheduler.clj | 26 ++++++++++++++++++----- 4 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/clj/commiteth/eth/multisig_wallet.clj b/src/clj/commiteth/eth/multisig_wallet.clj index 7150f85..d7aacb7 100644 --- a/src/clj/commiteth/eth/multisig_wallet.clj +++ b/src/clj/commiteth/eth/multisig_wallet.clj @@ -9,7 +9,7 @@ abi.datatypes.Address] [commiteth.eth.contracts MultiSigTokenWallet])) -(defonce method-ids +(def method-ids (into {} (map (fn [[k signature]] [k (eth/sig->method-id signature)]) @@ -18,10 +18,10 @@ :token-balances "tokenBalances(address)" :get-token-list "getTokenList()" :create "create(address[],uint256)" - :watch "watch(address,bytes)" + :watch "watch(address)" :balance-of "balanceOf(address)"}))) -(defonce topics +(def topics {:factory-create (eth/event-sig->topic-id "Create(address,address)") :submission (eth/event-sig->topic-id "Submission(uint256)")}) @@ -114,16 +114,6 @@ (:watch method-ids) token-address))) -(defn token-balances - "Query ERC20 token balances from bounty contract" - [bounty-contract token] - (let [token-address (get-token-address token)] - (println "token-address:" token-address) - (eth/call bounty-contract - (:token-balances method-ids) - token-address - 0))) - (defn load-bounty-contract [addr] (MultiSigTokenWallet/load addr @@ -142,7 +132,7 @@ (defn token-balance-in-bounty - "Query ERC20 token balances from bounty contract" + "Query (internal) ERC20 token balance from bounty contract for given token TLA" [bounty-addr token] (let [bounty-contract (load-bounty-contract bounty-addr) token-address (get-token-address token) @@ -155,7 +145,7 @@ (convert-token-value token)))) (defn token-balance - "Query balance of given ERC20 token for given address from ERC20 contract" + "Query balance of given ERC20 token TLA for given address from ERC20 contract" [bounty-addr token] (let [token-address (get-token-address token)] (-> (eth/call token-address diff --git a/src/clj/commiteth/eth/token_data.clj b/src/clj/commiteth/eth/token_data.clj index 34389d8..412f2fb 100644 --- a/src/clj/commiteth/eth/token_data.clj +++ b/src/clj/commiteth/eth/token_data.clj @@ -27,3 +27,9 @@ (defn token-info [tla] (get @token-data-atom (keyword tla))) + +(defn token-info-by-addr [addr] + (let [tokens-data (as-map)] + (first (filter (fn [[tla data]] + (= (:address data) addr)) + tokens-data)))) diff --git a/src/clj/commiteth/eth/token_registry.clj b/src/clj/commiteth/eth/token_registry.clj index f5c0c14..2543f9d 100644 --- a/src/clj/commiteth/eth/token_registry.clj +++ b/src/clj/commiteth/eth/token_registry.clj @@ -2,7 +2,8 @@ (:require [commiteth.eth.core :as eth] [commiteth.eth.web3j :refer [create-web3j creds]] - [commiteth.config :refer [env]]) + [commiteth.config :refer [env]] + [clojure.tools.logging :as log]) (:import [org.web3j abi.datatypes.generated.Uint256 abi.datatypes.Address @@ -34,7 +35,7 @@ (let [contract (load-tokenreg-contract addr)] ;(assert (.isValid contract)) ;; web3j's isValid can't be trusted... (let [token-count (-> contract .tokenCount .get .getValue)] - (println "token-count" token-count) + (log/debug "token-count" token-count) (into {} (map (fn [[addr tla digits name owner]] [(-> tla str keyword) diff --git a/src/clj/commiteth/scheduler.clj b/src/clj/commiteth/scheduler.clj index 9a6ceea..5a08a71 100644 --- a/src/clj/commiteth/scheduler.clj +++ b/src/clj/commiteth/scheduler.clj @@ -1,6 +1,7 @@ (ns commiteth.scheduler (:require [commiteth.eth.core :as eth] - [commiteth.eth.multisig-wallet :as wallet] + [commiteth.eth.multisig-wallet :as multisig] + [commiteth.eth.token-data :as token-data] [commiteth.github.core :as github] [commiteth.db.issues :as issues] [commiteth.db.bounties :as db-bounties] @@ -21,7 +22,7 @@ (log/debug "pending deployment:" transaction-hash) (when-let [receipt (eth/get-transaction-receipt transaction-hash)] (log/info "transaction receipt for issue #" issue-id ": " receipt) - (when-let [contract-address (:contractAddress receipt)] + (when-let [contract-address (multisig/find-created-multisig-address receipt)] (let [issue (issues/update-contract-address issue-id contract-address) {owner :owner repo :repo @@ -46,7 +47,7 @@ (defn deploy-contract [owner-address issue-id] - (let [transaction-hash (wallet/deploy-multisig owner-address)] + (let [transaction-hash (multisig/deploy-multisig owner-address)] (if (nil? transaction-hash) (log/error "Failed to deploy contract to" owner-address) (log/info "Contract deployed, transaction-hash:" @@ -88,7 +89,7 @@ :let [value (eth/get-balance-hex contract-address)]] (if (empty? payout-address) (log/error "Cannot sign pending bounty - winner has no payout address") - (let [execute-hash (wallet/send-all contract-address payout-address)] + (let [execute-hash (multisig/send-all contract-address payout-address)] (db-bounties/update-execute-hash issue-id execute-hash) (github/update-merged-issue-comment owner repo @@ -105,7 +106,7 @@ (log/debug "pending payout:" execute-hash) (when-let [receipt (eth/get-transaction-receipt execute-hash)] (log/info "execution receipt for issue #" issue-id ": " receipt) - (when-let [confirm-hash (wallet/find-confirmation-hash receipt)] + (when-let [confirm-hash (multisig/find-confirmation-hash receipt)] (db-bounties/update-confirm-hash issue-id confirm-hash))))) (defn update-payout-receipt @@ -181,6 +182,21 @@ current-balance-eth-str)))))) +(defn update-bounty-token-balances + "Helper function for updating internal ERC20 token balances to token multisig contract. Will be called periodically for all open bounty contracts." + [bounty-addr] + (for [[tla token-data] (token-data/as-map)] + (let [balance (multisig/token-balance bounty-addr tla)] + (when (> balance 0) + (do + (println "bounty at" bounty-addr "has" balance "of tla" tla) + (let [internal-balance (multisig/token-balance-in-bounty bounty-addr tla)] + (when (not= balance internal-balance) + (println "balances not in sync, calling watch") + (multisig/watch-token bounty-addr tla)))))))) + + + (defn run-periodic-tasks [time] (do (log/debug "run-periodic-tasks" time)