ERC20 related smart contract call clj functions

* functions for querying ERC20 token balance from bounty contract and
  from token contract
* fix several bugs
* web3j related shared code to own module
This commit is contained in:
Teemu Patja 2017-08-20 21:32:59 +03:00
parent 97c0314ec2
commit b00d30e370
No known key found for this signature in database
GPG Key ID: F5B7035E6580FD4C
2 changed files with 94 additions and 16 deletions

View File

@ -1,7 +1,13 @@
(ns commiteth.eth.multisig-wallet (ns commiteth.eth.multisig-wallet
(:require [commiteth.eth.core :as eth] (:require [commiteth.eth.core :as eth]
[commiteth.config :refer [env]] [commiteth.config :refer [env]]
[clojure.tools.logging :as log])) [commiteth.eth.web3j
:refer [create-web3j creds]]
[clojure.tools.logging :as log]
[commiteth.eth.token-data :as token-data])
(:import [org.web3j
abi.datatypes.Address]
[commiteth.eth.contracts MultiSigTokenWallet]))
(defonce method-ids (defonce method-ids
(into {} (into {}
@ -12,7 +18,8 @@
: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,bytes)"
:balance-of "balanceOf(address)"})))
(defonce topics (defonce topics
{:factory-create (eth/event-sig->topic-id "Create(address,address)") {:factory-create (eth/event-sig->topic-id "Create(address,address)")
@ -76,7 +83,7 @@
(str "0x" (subs factory-data 26))))) (str "0x" (subs factory-data 26)))))
(defn send-all (defn send-all ;; TODO: not tested
[contract to] [contract to]
(log/debug "multisig.send-all(contract, to)" contract to) (log/debug "multisig.send-all(contract, to)" contract to)
(let [params (eth/format-call-params (let [params (eth/format-call-params
@ -92,19 +99,67 @@
params))) params)))
(defn get-token-address [token]
(let [token-details (token-data/token-info token)]
(assert token-details)
(:address token-details)))
(defn watch-token (defn watch-token
[contract token] [bounty-addr token]
(log/debug "multisig.watch-token(contract, token)" contract token) (log/debug "multisig.watch-token(contract, token)" bounty-addr token)
(eth/execute (eth/eth-account) (let [token-address (get-token-address token)]
contract (assert token-address)
(:watch method-ids) (eth/execute (eth/eth-account)
token bounty-addr
0)) (: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
(create-web3j)
(creds)
(eth/gas-price)
(BigInteger/valueOf 500000)))
(defn convert-token-value
"Convert given value to decimal using given token's base"
[value token]
(let [token-details (token-data/token-info token)
token-base (:base token-details)]
(-> value
(/ (Math/pow 10 token-base)))))
(defn token-balance-in-bounty
"Query ERC20 token balances from bounty contract"
[bounty-addr token]
(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))))
(defn token-balance (defn token-balance
[contract token] "Query balance of given ERC20 token for given address from ERC20 contract"
(eth/call contract (:token-balances method-ids) token)) [bounty-addr token]
(let [token-address (get-token-address token)]
(defn tokens-list (-> (eth/call token-address
[contract] (:balance-of method-ids)
(eth/call contract (:get-token-list method-ids))) bounty-addr)
eth/hex->big-integer
(convert-token-value token))))

View File

@ -0,0 +1,23 @@
(ns commiteth.eth.web3j
(:require [commiteth.eth.core :as eth]
[commiteth.config :refer [env]])
(:import [org.web3j
protocol.Web3j
protocol.http.HttpService
crypto.Credentials
crypto.WalletUtils]))
(defn wallet-file-path []
(env :eth-wallet-file))
(defn wallet-password []
(env :eth-password))
(defn creds []
(WalletUtils/loadCredentials
(wallet-password)
(wallet-file-path)))
(defn create-web3j []
(Web3j/build (HttpService. (eth/eth-rpc-url))))