Skip to content

Commit

Permalink
Foreign generics (#74)
Browse files Browse the repository at this point in the history
* builds with package upgrades

* using network ID of type String

* change bower file
  • Loading branch information
martyall authored May 9, 2018
1 parent 91a2b38 commit 318861b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 26 deletions.
4 changes: 2 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 4 additions & 5 deletions src/Chanterelle/Internal/Deploy.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -47,7 +46,7 @@ writeDeployInfo
MonadAff (fs :: FS | eff) m
=> FilePath
-- filename of contract artifact
-> BigNumber
-> String
-- network id
-> DeployInfo
-- deployed contract address
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Chanterelle/Internal/Types/Deploy.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
23 changes: 11 additions & 12 deletions src/Chanterelle/Internal/Types/Project.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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(..))
Expand All @@ -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)

--------------------------------------------------------------------------------
Expand Down Expand Up @@ -111,24 +109,25 @@ 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

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
}
Expand Down Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions src/Chanterelle/Internal/Utils/Web3.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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(..))
Expand Down Expand Up @@ -52,15 +50,14 @@ 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
Rpc e -> "Web3 Rpc: " <> show e
RemoteError e -> "Web3 Remote: " <> e
ParserError e -> "Web3 Parser: " <> e
NullError -> "Web3 NullError"


getCodeForContract
:: forall eff m.
Expand Down Expand Up @@ -123,4 +120,4 @@ web3WithTimeout maxTimeout action = do
let timeout = liftAff do
delay maxTimeout
throwError $ error "TimeOut"
parOneOf [action, timeout]
parOneOf [action, timeout]

0 comments on commit 318861b

Please sign in to comment.