Skip to content

Commit

Permalink
Simply data (#55)
Browse files Browse the repository at this point in the history
* tests pass with new web3

* use uncurried flag tests pass

* use new web3 token units
  • Loading branch information
martyall committed Jul 25, 2018
1 parent 27bcde6 commit 2d81005
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 32 deletions.
2 changes: 1 addition & 1 deletion bower.json
Expand Up @@ -28,7 +28,7 @@
"purescript-node-fs-aff": "^5.0.0",
"purescript-console": "^3.0.0",
"purescript-string-parsers": "^3.0.1",
"purescript-web3": "^0.24.0",
"purescript-web3": "^0.25.0",
"purescript-mkdirp": "^0.3.0",
"purescript-fixed-points": "^4.0.0",
"purescript-record-extra": "^0.3.0"
Expand Down
10 changes: 8 additions & 2 deletions src/Data/AbiParser.purs
Expand Up @@ -8,10 +8,10 @@ import Data.Argonaut as A
import Data.Argonaut.Core (fromObject)
import Data.Argonaut.Decode ((.?))
import Data.Argonaut.Decode.Class (class DecodeJson, decodeJson)
import Data.Array (fromFoldable)
import Data.Array (fromFoldable, null)
import Data.Bifunctor (lmap)
import Data.Either (Either(..))
import Data.Foldable (foldMap)
import Data.Foldable (all, foldMap)
import Data.FunctorWithIndex (mapWithIndex)
import Data.Generic (class Generic, gEq, gShow)
import Data.Int (fromString)
Expand Down Expand Up @@ -174,6 +174,7 @@ data SolidityFunction =
, constant :: Boolean
, payable :: Boolean
, isConstructor :: Boolean
, isUnCurried :: Boolean
}

derive instance genericSolidityFunction :: Generic SolidityFunction
Expand All @@ -195,6 +196,8 @@ instance decodeJsonSolidityFunction :: DecodeJson SolidityFunction where
, constant : c
, payable: p
, isConstructor: false
, isUnCurried: all (\(FunctionInput fi) -> fi.name /= "") is
&& not (null is)
}

--------------------------------------------------------------------------------
Expand All @@ -203,6 +206,7 @@ instance decodeJsonSolidityFunction :: DecodeJson SolidityFunction where

data SolidityConstructor =
SolidityConstructor { inputs :: Array FunctionInput
, isUnCurried :: Boolean
}

derive instance genericSolidityConstructor :: Generic SolidityConstructor
Expand All @@ -215,6 +219,8 @@ instance decodeJsonSolidityConstructor :: DecodeJson SolidityConstructor where
obj <- decodeJson json
is <- obj .? "inputs"
pure $ SolidityConstructor { inputs : is
, isUnCurried: all (\(FunctionInput fi) -> fi.name /= "") is
&& not (null is)
}

--------------------------------------------------------------------------------
Expand Down
63 changes: 34 additions & 29 deletions src/Data/Generator.purs
Expand Up @@ -5,15 +5,15 @@ import Prelude
import Control.Monad.Writer (Writer, tell)
import Data.AbiParser (Abi(..), AbiType(..), FunctionInput(..), IndexedSolidityValue(..), SolidityEvent(..), SolidityFunction(..), SolidityConstructor(..), SolidityType(..), format)
import Data.Array (filter, length, mapWithIndex, null, replicate, uncons, unsnoc, zip, zipWith, (:))
import Data.Foldable (all, fold)
import Data.Foldable (fold)
import Data.Identity (Identity(..))
import Data.List (uncons) as List
import Data.List.Types (NonEmptyList(..)) as List
import Data.Maybe (Maybe(..))
import Data.Newtype (un)
import Data.NonEmpty ((:|))
import Data.String (drop, fromCharArray, joinWith, singleton, take, toCharArray, toLower, toUpper)
import Data.Traversable (for, traverse)
import Data.Traversable (for)
import Data.Tuple (Tuple(..), uncurry)
import Network.Ethereum.Core.HexString (fromByteString)
import Network.Ethereum.Core.Keccak256 (keccak256)
Expand Down Expand Up @@ -146,8 +146,10 @@ data FunTypeDecl =

funToTypeDecl :: SolidityFunction -> CodeOptions -> Imported FunTypeDecl
funToTypeDecl fun@(SolidityFunction f) opts = do
factorTypes <- for f.inputs $ \(FunctionInput fi) ->
toPSType $ fi.type
factorTypes <-
if f.isUnCurried
then for f.inputs tagInput
else for f.inputs $ \(FunctionInput fi) -> toPSType fi.type
pure $
FunTypeDecl
{ typeName: if isValidType f.name then capitalize $ f.name <> "Fn" else "FnT" <> f.name <> "Fn"
Expand Down Expand Up @@ -211,8 +213,8 @@ funToHelperFunction isWhereClause fun@(SolidityFunction f) opts = do
pure ["TransactionOptions NoPay", "ChainCursor"]
else if f.payable
then do
import' "Network.Ethereum.Web3.Types" [IType "Wei"]
pure ["TransactionOptions Wei"]
import' "Network.Ethereum.Web3.Types.TokenUnit" [IType "MinorUnit"]
pure ["TransactionOptions MinorUnit"]
else do
import' "Network.Ethereum.Web3.Types" [IType "NoPay"]
pure ["TransactionOptions NoPay"]
Expand All @@ -232,7 +234,10 @@ funToHelperFunction isWhereClause fun@(SolidityFunction f) opts = do
helperTransport <- toTransportPrefix f.isConstructor f.constant $ length f.outputs
helperPayload <- toPayload isWhereClause decl.typeName conVars
returnType <- toReturnType f.constant f.outputs
ins <- for f.inputs $ \(FunctionInput fi) -> toPSType fi.type
ins <-
if f.isUnCurried
then for f.inputs tagInput
else for f.inputs $ \(FunctionInput fi) -> toPSType fi.type
pure
{ signature: sigPrefix <> ins <> [returnType]
, unpackExpr:
Expand Down Expand Up @@ -263,8 +268,8 @@ funToHelperFunction' fun@(SolidityFunction f) opts = do
pure ["TransactionOptions NoPay", "ChainCursor"]
else if f.payable
then do
import' "Network.Ethereum.Web3.Types" [IType "Wei"]
pure ["TransactionOptions Wei"]
import' "Network.Ethereum.Web3.Types.TokenUnit" [IType "MinorUnit"]
pure ["TransactionOptions MinorUnit"]
else do
import' "Network.Ethereum.Web3.Types" [IType "NoPay"]
pure ["TransactionOptions NoPay"]
Expand Down Expand Up @@ -292,26 +297,33 @@ funToHelperFunction' fun@(SolidityFunction f) opts = do
, whereClause: whereC
}
where
tagInput (FunctionInput fi) = do
ty <- toPSType fi.type
import' "Data.Functor.Tagged" [IType "Tagged"]
import' "Data.Symbol" [IType "SProxy"]
pure $ "Tagged (SProxy " <> "\"" <> fi.name <> "\") " <> ty
recordInput fis = do
rowElems <- for fis $ \(FunctionInput fi) -> do
ty <- toPSType fi.type
pure $ fi.name <> " :: " <> ty
pure $ "{ " <> joinWith ", " rowElems <> " }"
whereHelper d pre is ret = do
helper <- funToHelperFunction true fun opts
tys <- traverse tagInput is
tys <-
if f.isUnCurried
then for f.inputs tagInput
else for f.inputs $ \(FunctionInput fi) -> toPSType fi.type
pure $ CurriedHelperFunction helper
{ constraints = []
, quantifiedVars = []
, unpackExpr = helper.unpackExpr {name = helper.unpackExpr.name <> "'"}
, signature = pre <> tys <> [ret]
}

tagInput
:: FunctionInput
-> Imported String
tagInput (FunctionInput fi) = do
ty <- toPSType fi.type
import' "Data.Functor.Tagged" [IType "Tagged"]
import' "Data.Symbol" [IType "SProxy"]
pure $ "(Tagged (SProxy " <> "\"" <> fi.name <> "\") " <> ty <> ")"

toTransportPrefix :: Boolean -> Boolean -> Int -> Imported String
toTransportPrefix isConstructor isCall outputCount = do
fun <- if isConstructor
Expand Down Expand Up @@ -340,13 +352,7 @@ toPayload isWhereClause typeName args = do
import' "Data.Functor.Tagged" [IVal "tagged"]
let tupleType = "Tuple" <> show (length args)
import' "Network.Ethereum.Web3.Solidity" [ITypeCtr tupleType]
args' <- if isWhereClause
then do
import' "Data.Functor.Tagged" [IVal "untagged"]
pure $ map (\s -> "(untagged " <> s <> " )") args
else pure args

pure $ "((tagged $ " <> tupleType <> " " <> joinWith " " args' <> ") :: " <> typeName <> ")"
pure $ "((tagged $ " <> tupleType <> " " <> joinWith " " args <> ") :: " <> typeName <> ")"

toReturnType :: Boolean -> Array SolidityType -> Imported String
toReturnType constant outputs' = do
Expand Down Expand Up @@ -573,13 +579,11 @@ data CodeBlock =

funToFunctionCodeBlock :: SolidityFunction -> CodeOptions -> Imported CodeBlock
funToFunctionCodeBlock fun@(SolidityFunction f) opts = do
typeDecl <- funToTypeDecl fun opts
helperFunction <- if isUnCurried f
then funToHelperFunction' fun opts
else funToHelperFunction false fun opts <#> CurriedHelperFunction
pure $ FunctionCodeBlock typeDecl helperFunction
where
isUnCurried f' = all (\(FunctionInput fi) -> fi.name /= "") f'.inputs && not (null f'.inputs)
typeDecl <- funToTypeDecl fun opts
helperFunction <- if f.isUnCurried
then funToHelperFunction' fun opts
else funToHelperFunction false fun opts <#> CurriedHelperFunction
pure $ FunctionCodeBlock typeDecl helperFunction

newLine1 :: Array String -> String
newLine1 = joinWith "\n"
Expand Down Expand Up @@ -629,6 +633,7 @@ instance codeAbi :: Code (Abi Identity) where
, constant : false
, payable : false
, isConstructor : true
, isUnCurried: c.isUnCurried
}
functionCodeBlock <- funToFunctionCodeBlock f opts
genCode functionCodeBlock opts
Expand Down

0 comments on commit 2d81005

Please sign in to comment.