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)
This commit is contained in:
Teemu Patja 2017-08-21 00:30:49 +03:00
parent 5f6ad85011
commit a2a569f980
No known key found for this signature in database
GPG Key ID: F5B7035E6580FD4C
4 changed files with 35 additions and 22 deletions

View File

@ -9,7 +9,7 @@
abi.datatypes.Address] abi.datatypes.Address]
[commiteth.eth.contracts MultiSigTokenWallet])) [commiteth.eth.contracts MultiSigTokenWallet]))
(defonce method-ids (def method-ids
(into {} (into {}
(map (fn [[k signature]] (map (fn [[k signature]]
[k (eth/sig->method-id signature)]) [k (eth/sig->method-id signature)])
@ -18,10 +18,10 @@
:token-balances "tokenBalances(address)" :token-balances "tokenBalances(address)"
:get-token-list "getTokenList()" :get-token-list "getTokenList()"
:create "create(address[],uint256)" :create "create(address[],uint256)"
:watch "watch(address,bytes)" :watch "watch(address)"
:balance-of "balanceOf(address)"}))) :balance-of "balanceOf(address)"})))
(defonce topics (def topics
{:factory-create (eth/event-sig->topic-id "Create(address,address)") {:factory-create (eth/event-sig->topic-id "Create(address,address)")
:submission (eth/event-sig->topic-id "Submission(uint256)")}) :submission (eth/event-sig->topic-id "Submission(uint256)")})
@ -114,16 +114,6 @@
(:watch method-ids) (:watch method-ids)
token-address))) 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] (defn load-bounty-contract [addr]
(MultiSigTokenWallet/load addr (MultiSigTokenWallet/load addr
@ -142,7 +132,7 @@
(defn token-balance-in-bounty (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] [bounty-addr token]
(let [bounty-contract (load-bounty-contract bounty-addr) (let [bounty-contract (load-bounty-contract bounty-addr)
token-address (get-token-address token) token-address (get-token-address token)
@ -155,7 +145,7 @@
(convert-token-value token)))) (convert-token-value token))))
(defn token-balance (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] [bounty-addr token]
(let [token-address (get-token-address token)] (let [token-address (get-token-address token)]
(-> (eth/call token-address (-> (eth/call token-address

View File

@ -27,3 +27,9 @@
(defn token-info [tla] (defn token-info [tla]
(get @token-data-atom (keyword 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))))

View File

@ -2,7 +2,8 @@
(:require [commiteth.eth.core :as eth] (:require [commiteth.eth.core :as eth]
[commiteth.eth.web3j [commiteth.eth.web3j
:refer [create-web3j creds]] :refer [create-web3j creds]]
[commiteth.config :refer [env]]) [commiteth.config :refer [env]]
[clojure.tools.logging :as log])
(:import [org.web3j (:import [org.web3j
abi.datatypes.generated.Uint256 abi.datatypes.generated.Uint256
abi.datatypes.Address abi.datatypes.Address
@ -34,7 +35,7 @@
(let [contract (load-tokenreg-contract addr)] (let [contract (load-tokenreg-contract addr)]
;(assert (.isValid contract)) ;; web3j's isValid can't be trusted... ;(assert (.isValid contract)) ;; web3j's isValid can't be trusted...
(let [token-count (-> contract .tokenCount .get .getValue)] (let [token-count (-> contract .tokenCount .get .getValue)]
(println "token-count" token-count) (log/debug "token-count" token-count)
(into {} (into {}
(map (fn [[addr tla digits name owner]] (map (fn [[addr tla digits name owner]]
[(-> tla str keyword) [(-> tla str keyword)

View File

@ -1,6 +1,7 @@
(ns commiteth.scheduler (ns commiteth.scheduler
(:require [commiteth.eth.core :as eth] (: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.github.core :as github]
[commiteth.db.issues :as issues] [commiteth.db.issues :as issues]
[commiteth.db.bounties :as db-bounties] [commiteth.db.bounties :as db-bounties]
@ -21,7 +22,7 @@
(log/debug "pending deployment:" transaction-hash) (log/debug "pending deployment:" transaction-hash)
(when-let [receipt (eth/get-transaction-receipt transaction-hash)] (when-let [receipt (eth/get-transaction-receipt transaction-hash)]
(log/info "transaction receipt for issue #" issue-id ": " receipt) (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) (let [issue (issues/update-contract-address issue-id contract-address)
{owner :owner {owner :owner
repo :repo repo :repo
@ -46,7 +47,7 @@
(defn deploy-contract [owner-address issue-id] (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) (if (nil? transaction-hash)
(log/error "Failed to deploy contract to" owner-address) (log/error "Failed to deploy contract to" owner-address)
(log/info "Contract deployed, transaction-hash:" (log/info "Contract deployed, transaction-hash:"
@ -88,7 +89,7 @@
:let [value (eth/get-balance-hex contract-address)]] :let [value (eth/get-balance-hex contract-address)]]
(if (empty? payout-address) (if (empty? payout-address)
(log/error "Cannot sign pending bounty - winner has no 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) (db-bounties/update-execute-hash issue-id execute-hash)
(github/update-merged-issue-comment owner (github/update-merged-issue-comment owner
repo repo
@ -105,7 +106,7 @@
(log/debug "pending payout:" execute-hash) (log/debug "pending payout:" execute-hash)
(when-let [receipt (eth/get-transaction-receipt execute-hash)] (when-let [receipt (eth/get-transaction-receipt execute-hash)]
(log/info "execution receipt for issue #" issue-id ": " receipt) (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))))) (db-bounties/update-confirm-hash issue-id confirm-hash)))))
(defn update-payout-receipt (defn update-payout-receipt
@ -181,6 +182,21 @@
current-balance-eth-str)))))) 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] (defn run-periodic-tasks [time]
(do (do
(log/debug "run-periodic-tasks" time) (log/debug "run-periodic-tasks" time)