From 318861b9efbca06e3dee7cccd043723f280545ed Mon Sep 17 00:00:00 2001 From: blinky3713 <31280145+blinky3713@users.noreply.github.com> Date: Wed, 9 May 2018 16:19:55 -0400 Subject: [PATCH] Foreign generics (#74) * builds with package upgrades * using network ID of type String * change bower file --- bower.json | 4 ++-- src/Chanterelle/Internal/Deploy.purs | 9 ++++---- src/Chanterelle/Internal/Types/Deploy.purs | 4 ++-- src/Chanterelle/Internal/Types/Project.purs | 23 ++++++++++----------- src/Chanterelle/Internal/Utils/Web3.purs | 7 ++----- 5 files changed, 21 insertions(+), 26 deletions(-) diff --git a/bower.json b/bower.json index 3109807..77a2c67 100644 --- a/bower.json +++ b/bower.json @@ -8,8 +8,8 @@ ], "dependencies": { "purescript-prelude": "^3.1.1", - "purescript-web3": "^0.22.1", - "purescript-web3-generator": "^0.21.2", + "purescript-web3": "^0.23.0", + "purescript-web3-generator": "v0.22.0", "purescript-console": "^3.0.0", "purescript-errors": "^3.0.0", "purescript-node-process": "^5.0.0", diff --git a/src/Chanterelle/Internal/Deploy.purs b/src/Chanterelle/Internal/Deploy.purs index ccaddda..72893ad 100644 --- a/src/Chanterelle/Internal/Deploy.purs +++ b/src/Chanterelle/Internal/Deploy.purs @@ -19,7 +19,6 @@ import Control.Monad.Reader.Class (class MonadAsk, ask) import Data.Argonaut (_Object, _String, jsonEmptyObject, (~>), (:=)) import Data.Argonaut.Parser (jsonParser) import Data.Either (Either(..), either) -import Data.Foreign.NullOrUndefined (unNullOrUndefined) import Data.Lens ((^?), (%~)) import Data.Lens.Index (ix) import Data.Maybe (isNothing, fromJust) @@ -47,7 +46,7 @@ writeDeployInfo MonadAff (fs :: FS | eff) m => FilePath -- filename of contract artifact - -> BigNumber + -> String -- network id -> DeployInfo -- deployed contract address @@ -60,7 +59,7 @@ writeDeployInfo filename nid {deployAddress, blockNumber, blockHash, transaction ~> "blockHash" := show blockHash ~> "transactionHash" := show transactionHash ~> jsonEmptyObject - artifactWithAddress = artifact # _Object <<< ix "networks" <<< _Object %~ M.insert (show nid) networkIdObj + artifactWithAddress = artifact # _Object <<< ix "networks" <<< _Object %~ M.insert nid networkIdObj liftAff $ writeTextFile UTF8 filename $ jsonStringifyWithSpaces 4 artifactWithAddress -- | Read the deployment address for a given network id from the solc artifact. @@ -103,12 +102,12 @@ getPublishedContractDeployInfo txHash name = do let message = "No Transaction Receipt found for deployment " <> show txHash in throwError $ OnDeploymentError {name, message} Right (TransactionReceipt txReceipt) -> - if txReceipt.status == Failed || isNothing (unNullOrUndefined txReceipt.contractAddress) + if txReceipt.status == Failed || isNothing (txReceipt.contractAddress) then let message = "Deployment failed to create contract, no address found or status 0x0 in receipt: " <> name in throwError $ OnDeploymentError {name, message} else do - let deployAddress = unsafePartial fromJust <<< unNullOrUndefined $ txReceipt.contractAddress + let deployAddress = unsafePartial fromJust $ txReceipt.contractAddress log Info $ "Contract " <> name <> " deployed to address " <> show deployAddress pure { deployAddress , blockNumber: txReceipt.blockNumber diff --git a/src/Chanterelle/Internal/Types/Deploy.purs b/src/Chanterelle/Internal/Types/Deploy.purs index 25da558..87eb790 100644 --- a/src/Chanterelle/Internal/Types/Deploy.purs +++ b/src/Chanterelle/Internal/Types/Deploy.purs @@ -15,7 +15,7 @@ import Data.Either (Either) import Data.Lens ((?~)) import Data.Maybe (Maybe(..)) import Data.Validation.Semigroup (V, invalid) -import Network.Ethereum.Web3 (Address, BigNumber, ETH, HexString, TransactionOptions, Web3, _data, _value, fromWei) +import Network.Ethereum.Web3 (Address, ETH, HexString, TransactionOptions, Web3, _data, _value, fromWei) import Network.Ethereum.Web3.Api (eth_sendTransaction) import Network.Ethereum.Web3.Types (NoPay) import Network.Ethereum.Web3.Types.Provider (Provider) @@ -92,7 +92,7 @@ throwDeploy = liftAff <<< liftEff' <<< throwException -- | primary deployment configuration newtype DeployConfig = - DeployConfig { networkId :: BigNumber + DeployConfig { networkId :: String , primaryAccount :: Address , provider :: Provider , timeout :: Milliseconds diff --git a/src/Chanterelle/Internal/Types/Project.purs b/src/Chanterelle/Internal/Types/Project.purs index 9189178..89b7277 100644 --- a/src/Chanterelle/Internal/Types/Project.purs +++ b/src/Chanterelle/Internal/Types/Project.purs @@ -4,7 +4,6 @@ import Prelude import Chanterelle.Internal.Utils.Json (decodeJsonAddress, decodeJsonHexString, encodeJsonAddress, encodeJsonHexString, gfWithDecoder) import Control.Alt ((<|>)) -import Control.Error.Util (note) import Data.Argonaut (class EncodeJson, class DecodeJson, encodeJson, decodeJson, (:=), (~>), (.?), (.??), jsonEmptyObject) import Data.Array (elem, filter, null) import Data.Either (Either(..)) @@ -14,8 +13,7 @@ import Data.StrMap as M import Data.String (Pattern(..), joinWith, split) import Data.Traversable (for) import Data.Tuple (Tuple(..)) -import Network.Ethereum.Core.BigNumber (decimal, embed, parseBigNumber, toString) -import Network.Ethereum.Web3 (Address, BigNumber, HexString) +import Network.Ethereum.Web3 (Address, HexString) import Node.Path (FilePath) -------------------------------------------------------------------------------- @@ -111,10 +109,10 @@ instance decodeJsonLibraries :: DecodeJson Libraries where --------------------------------------------------------------------- data ChainSpec = AllChains - | SpecificChains (Array BigNumber) + | SpecificChains (Array String) derive instance eqChainSpec :: Eq ChainSpec -networkIDFitsChainSpec :: ChainSpec -> BigNumber -> Boolean +networkIDFitsChainSpec :: ChainSpec -> String -> Boolean networkIDFitsChainSpec AllChains _ = true networkIDFitsChainSpec (SpecificChains chains) id = id `elem` chains @@ -122,13 +120,14 @@ instance decodeJsonChainSpec :: DecodeJson ChainSpec where decodeJson j = decodeAllChains <|> decodeSpecificChains <|> Left "Invalid chain specifier" where decodeStr = decodeJson j decodeAllChains = decodeStr >>= (\s -> if s == "*" then Right AllChains else Left "Not * for AllChains") - decodeSpecificChains = decodeStr >>= (\s -> SpecificChains <$> for (split (Pattern ",") s) (note "Network ID is not a decimal number" <<< parseBigNumber decimal)) + decodeSpecificChains = decodeStr >>= (\s -> SpecificChains <$> for (split (Pattern ",") s) pure) + instance encodeJsonChainSpec :: EncodeJson ChainSpec where encodeJson AllChains = encodeJson "*" - encodeJson (SpecificChains c) = encodeJson $ joinWith "," (toString decimal <$> c) + encodeJson (SpecificChains c) = encodeJson $ joinWith "," c -newtype Network = Network { name :: String +newtype Network = Network { name :: String , providerUrl :: String , allowedChains :: ChainSpec } @@ -189,10 +188,10 @@ resolveNetworkRefs refs definedNets = case refs of AllDefinedNetworks -> definedNets SpecificRefs specRefs -> Networks $ filter (\(Network { name }) -> name `elem` specRefs) mergedNetworks - where predefinedNets = [ Network { name: "mainnet", providerUrl: "https://mainnet.infura.io", allowedChains: SpecificChains [embed 1] } - , Network { name: "ropsten", providerUrl: "https://ropsten.infura.io", allowedChains: SpecificChains [embed 3] } - , Network { name: "rinkeby", providerUrl: "https://rinkeby.infura.io", allowedChains: SpecificChains [embed 4] } - , Network { name: "kovan" , providerUrl: "https://kovan.infura.io" , allowedChains: SpecificChains [embed 42] } + where predefinedNets = [ Network { name: "mainnet", providerUrl: "https://mainnet.infura.io", allowedChains: SpecificChains ["1"] } + , Network { name: "ropsten", providerUrl: "https://ropsten.infura.io", allowedChains: SpecificChains ["3"] } + , Network { name: "rinkeby", providerUrl: "https://rinkeby.infura.io", allowedChains: SpecificChains ["4"] } + , Network { name: "kovan" , providerUrl: "https://kovan.infura.io" , allowedChains: SpecificChains ["42"] } , Network { name: "localhost", providerUrl: "http://localhost:8545/", allowedChains: AllChains } ] (Networks definedNets') = definedNets diff --git a/src/Chanterelle/Internal/Utils/Web3.purs b/src/Chanterelle/Internal/Utils/Web3.purs index 607a8fd..7e44ac9 100644 --- a/src/Chanterelle/Internal/Utils/Web3.purs +++ b/src/Chanterelle/Internal/Utils/Web3.purs @@ -15,10 +15,8 @@ import Control.Monad.Except (ExceptT(..), except, runExceptT, withExceptT) import Control.Parallel (parOneOf) import Data.Array ((!!)) import Data.Either (Either(..)) -import Data.Int (decimal) import Data.Maybe (maybe) import Data.String (null) -import Network.Ethereum.Core.BigNumber (toString) import Network.Ethereum.Web3 (Address, ChainCursor(Latest), ETH, HexString, Web3, runWeb3, unHex) import Network.Ethereum.Web3.Api (eth_getAccounts, eth_getCode, eth_getTransactionReceipt, net_version) import Network.Ethereum.Web3.Types (TransactionReceipt, Web3Error(..)) @@ -52,7 +50,7 @@ resolveProvider rn@(Network realNet) = runExceptT do v <- net_version pure $ if networkIDFitsChainSpec realNet.allowedChains v then Right provider - else Left $ "Network " <> show realNet.name <> " resolves to a provider which is serving chain ID " <> toString decimal v <> ", which is not within that network's permitted chains." + else Left $ "Network " <> show realNet.name <> " resolves to a provider which is serving chain ID " <> v <> ", which is not within that network's permitted chains." except validatedProvider where showWeb3Error = case _ of @@ -60,7 +58,6 @@ resolveProvider rn@(Network realNet) = runExceptT do RemoteError e -> "Web3 Remote: " <> e ParserError e -> "Web3 Parser: " <> e NullError -> "Web3 NullError" - getCodeForContract :: forall eff m. @@ -123,4 +120,4 @@ web3WithTimeout maxTimeout action = do let timeout = liftAff do delay maxTimeout throwError $ error "TimeOut" - parOneOf [action, timeout] \ No newline at end of file + parOneOf [action, timeout]