TokenReg clojure support improvements

* allow loading of parity tokenreg contract data from any address,
  including our own test instance
* use tokenreg data from config.edn for testnet only when availaned,
  otherwise from our smart contract on rinkeby
* clean up + refactoring
* remove generated java wrapper for parity tokenreg contract
This commit is contained in:
Teemu Patja 2017-08-20 22:34:31 +03:00
parent 270dd3e277
commit 5f6ad85011
No known key found for this signature in database
GPG Key ID: F5B7035E6580FD4C
3 changed files with 46 additions and 355 deletions

View File

@ -13,8 +13,11 @@
(log/info "token-data started")
(let [token-data
(if (env :on-testnet true)
(env :testnet-token-data {})
(token-reg/load-parity-tokenreg-data))]
(if-let [token-data (env :testnet-token-data)]
token-data
(token-reg/load-parity-tokenreg-data token-reg/STATUS-RINKEBY-ADDR))
(token-reg/load-parity-tokenreg-data token-reg/PARITY-MAINNET-ADDR))]
(reset! token-data-atom token-data)))
:stop
(log/info "token-data stopped"))
@ -22,5 +25,5 @@
(defn as-map []
@token-data-atom)
(defn token-info [mnemonic]
(get @token-data-atom (keyword mnemonic)))
(defn token-info [tla]
(get @token-data-atom (keyword tla)))

View File

@ -1,56 +1,59 @@
(ns commiteth.eth.token-registry
(:require [commiteth.eth.core :as eth]
[commiteth.eth.web3j
:refer [create-web3j creds]]
[commiteth.config :refer [env]])
(:import [org.web3j
abi.datatypes.generated.Uint256
abi.datatypes.Address
abi.datatypes.Utf8String
protocol.Web3j
protocol.http.HttpService
crypto.Credentials
crypto.WalletUtils]
commiteth.contracts.TokenReg))
commiteth.eth.contracts.TokenReg))
(defonce PARITY-TOKENREG-ADDR "0x5f0281910af44bfb5fc7e86a404d0304b0e042f1")
(defonce GAS_PRICE (eth/gas-price))
(defonce GAS_LIMIT (BigInteger/valueOf 21000))
(defonce PARITY-MAINNET-ADDR "0x5f0281910af44bfb5fc7e86a404d0304b0e042f1")
(defonce STATUS-RINKEBY-ADDR "0x4826Ee32532EeA00Bb71C17Da491f1B2D2193C21")
(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))))
(defn load-tokenreg-contract []
(TokenReg/load PARITY-TOKENREG-ADDR
(defn- load-tokenreg-contract [addr]
(TokenReg/load addr
(create-web3j)
(creds)
GAS_PRICE
GAS_LIMIT))
(eth/gas-price)
(BigInteger/valueOf 21000)))
(defn load-parity-tokenreg-data
"Construct a mapping of ERC20 token mnemonic -> token data (name, address, digits, owner) from data
in Parity's mainnet token registry contract."
[]
(let [contract (load-tokenreg-contract)]
(assert (.isValid contract))
([]
(load-parity-tokenreg-data PARITY-MAINNET-ADDR))
([addr]
(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)
(into {}
(map (fn [[addr mnemonic digits name owner]]
[(-> mnemonic .toString (keyword))
{:name (.toString name)
:digits (.getValue digits)
:address (.toString addr)
:owner (.toString owner)}])
(map (fn [[addr tla digits name owner]]
[(-> tla str keyword)
{:tla (str tla)
:name (str name)
:base (.getValue digits)
:address (str addr)
:owner (str owner)}])
(for [i (range token-count)]
(-> (.token contract
(org.web3j.abi.datatypes.generated.Uint256. i))
.get)))))))
.get))))))))
(defn deploy-parity-tokenreg
"Deploy an instance of parity token-registry to current network"
[]
(let [web3j (create-web3j)]
(TokenReg/deploy web3j
(creds)
(eth/gas-price)
(BigInteger/valueOf 4000000) ;; gas limit
BigInteger/ZERO)))

File diff suppressed because one or more lines are too long