Skip to content

Commit

Permalink
v0.14 (#65)
Browse files Browse the repository at this point in the history
* Support for purescript v0.14
  • Loading branch information
kejace committed Jul 14, 2021
1 parent 3a6b690 commit 1864132
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,5 +1,5 @@
language: node_js
dist: trusty
dist: focal
sudo: required
node_js: stable
install:
Expand Down
4 changes: 1 addition & 3 deletions README.md
Expand Up @@ -3,7 +3,7 @@

[![Latest release](http://img.shields.io/github/release/f-o-a-m/purescript-web3-generator.svg?branch=master)](https://github.com/f-o-a-m/purescript-web3-generator/releases)
[![purescript-web3-generator on Pursuit](https://pursuit.purescript.org/packages/purescript-web3-generator/badge)](https://pursuit.purescript.org/packages/purescript-web3-generator)
[![Build status](https://travis-ci.org/f-o-a-m/purescript-web3-generator.svg?branch=master)](https://travis-ci.org/f-o-a-m/purescript-web3-generator?branch=master)
[![Build status](https://travis-ci.com/f-o-a-m/purescript-web3-generator.svg?branch=master)](https://travis-ci.com/f-o-a-m/purescript-web3-generator?branch=master)

Generats purescript modules from Solidity ABIs

Expand Down Expand Up @@ -35,15 +35,13 @@ module Generator where
import Data.GeneratorMain (generatorMain)
main = generatorMain
```

2. From there, add a build step

```sh

pulp run -m Generator --src-path generator -- --abis <abis> --dest src --module Contracts

```

(note that we specify both a different source directory than `src` and a different module `Generator` that `purs` is looking for `main` in)
Expand Down
7 changes: 3 additions & 4 deletions package.json
Expand Up @@ -13,9 +13,8 @@
"yargs": "^8.0.2"
},
"devDependencies": {
"pulp": "^15.0.0",
"purescript": "^0.13.0",
"purescript-psa": "^0.7.3",
"spago": "^0.16.0"
"purescript": "^0.14.2",
"purescript-psa": "^0.8.2",
"spago": "^0.20.3"
}
}
11 changes: 5 additions & 6 deletions packages.dhall
@@ -1,5 +1,5 @@
let upstream =
https://github.com/purescript/package-sets/releases/download/psc-0.13.8-20201007/packages.dhall sha256:35633f6f591b94d216392c9e0500207bb1fec42dd355f4fecdfd186956567b6b
https://github.com/purescript/package-sets/releases/download/psc-0.14.2-20210629/packages.dhall sha256:534c490bb73cae75adb5a39871142fd8db5c2d74c90509797a80b8bb0d5c3f7b

let overrides = {=}

Expand All @@ -24,15 +24,14 @@ let additions =
, "parsing"
, "partial"
, "profunctor-lenses"
, "proxy"
, "psci-support"
, "tagged"
, "transformers"
, "typelevel-prelude"
, "variant"
]
, repo = "https://github.com/f-o-a-m/purescript-web3"
, version = "v3.0.0"
, version = "v4.0.0"
}
, eth-core =
{ dependencies =
Expand All @@ -52,7 +51,7 @@ let additions =
, repo =
"https://github.com/f-o-a-m/purescript-eth-core.git"
, version =
"v6.0.0"
"v7.0.0"
}
, coroutine-transducers =
{ dependencies =
Expand Down Expand Up @@ -90,9 +89,9 @@ let additions =
, "profunctor"
]
, repo =
"https://github.com/LiamGoodacre/purescript-tagged"
"https://github.com/kejace/purescript-tagged"
, version =
"v3.0.0"
"v0.14"
}
}

Expand Down
27 changes: 25 additions & 2 deletions spago.dhall
Expand Up @@ -4,20 +4,43 @@ You can edit this file as you like.
-}
{ name = "web3-generator"
, dependencies =
[ "ansi"
[ "aff"
, "ansi"
, "argonaut"
, "argonaut-codecs"
, "argonaut-core"
, "argonaut-traversals"
, "arrays"
, "bifunctors"
, "console"
, "control"
, "effect"
, "either"
, "errors"
, "eth-core"
, "exceptions"
, "fixed-points"
, "foldable-traversable"
, "identity"
, "integers"
, "lists"
, "maybe"
, "mkdirp"
, "newtype"
, "node-buffer"
, "node-fs"
, "node-fs-aff"
, "node-path"
, "nonempty"
, "ordered-collections"
, "partial"
, "prelude"
, "profunctor-lenses"
, "psci-support"
, "record-extra"
, "string-parsers"
, "strings"
, "transformers"
, "tuples"
, "web3"
, "yargs"
]
Expand Down
6 changes: 3 additions & 3 deletions src/Data/AbiParser.purs
Expand Up @@ -16,8 +16,8 @@ import Data.Either (Either(..))
import Data.Foldable (all, foldMap)
import Data.FunctorWithIndex (mapWithIndex)
import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Eq (genericEq)
import Data.Generic.Rep.Show (genericShow)
import Data.Eq.Generic (genericEq)
import Data.Show.Generic (genericShow)
import Data.Int (fromString)
import Data.List.Types (List(..), NonEmptyList(..))
import Data.Maybe (Maybe(..))
Expand Down Expand Up @@ -305,7 +305,7 @@ instance showSolidityFallback :: Show SolidityFallback where
show = genericShow

instance decodeJsonSolidityFallback :: DecodeJson SolidityFallback where
decodeJson json = do
decodeJson _ = do
pure $ SolidityFallback

--------------------------------------------------------------------------------
Expand Down
5 changes: 2 additions & 3 deletions src/Data/CodeGen.purs
Expand Up @@ -27,7 +27,7 @@ import Data.Identity (Identity(..))
import Data.Lens ((^?))
import Data.Array as Array
import Data.Lens.Index (ix)
import Data.Map (Map, fromFoldableWith, insert, lookup, member, toUnfoldable)
import Data.Map (Map, fromFoldableWith, insert, lookup, member, toUnfoldable, empty)
import Data.Maybe (Maybe(..), isNothing)
import Data.Newtype (un)
import Data.Map as Map
Expand Down Expand Up @@ -68,7 +68,7 @@ runImports = mergeImports >>> map runImport >>> newLine1 >>> ("import Prelude \n
runImport (Tuple mName mImports) = "import " <> mName <> " (" <> joinWith ", " (runModuleImports mImports) <> ")"
runModuleImports :: ModuleImports -> Array String
runModuleImports =
runAcc <<< foldl f { types: mempty, imports: mempty }
runAcc <<< foldl f { types: empty, imports: mempty }
where
runAcc :: ModuleImportsAcc -> Array String
runAcc acc = sort $ nub $ append acc.imports $ (toUnfoldable acc.types) >>= resolveCtrImports
Expand Down Expand Up @@ -267,7 +267,6 @@ getAllJsonFiles root = evalStateT getAllJsonFiles' root
where
getAllJsonFiles' :: StateT FilePath m (Array FilePath)
getAllJsonFiles' = do
cd <- get
hereFiles <- getJsonFilesInDirectory
hereDirectories <- getAllDirectories
if null hereDirectories
Expand Down
40 changes: 20 additions & 20 deletions src/Data/Generator.purs
Expand Up @@ -145,8 +145,8 @@ data FunTypeDecl =
, typeName :: String
}

funToTypeDecl :: SolidityFunction -> CodeOptions -> Imported FunTypeDecl
funToTypeDecl fun@(SolidityFunction f) opts = do
funToTypeDecl :: SolidityFunction -> Imported FunTypeDecl
funToTypeDecl fun@(SolidityFunction f) = do
factorTypes <-
if f.isUnCurried
then for f.inputs tagInput
Expand All @@ -164,14 +164,14 @@ instance codeDataDecl :: Code FunTypeDecl where
nArgs = length decl.factorTypes
tupleType = "Tuple" <> show nArgs
import' "Data.Functor.Tagged" [IType "Tagged"]
import' "Data.Symbol" [IType "SProxy"]
import' "Type.Proxy" [IType "Proxy"]
import' "Network.Ethereum.Web3.Solidity" [ITypeCtr tupleType]
pure $
fold
["type "
, decl.typeName
, " = "
, "Tagged (SProxy \"" <> decl.signature <> "\") (" <> tupleType <> " " <> joinWith " " decl.factorTypes <> ")"
, "Tagged (Proxy \"" <> decl.signature <> "\") (" <> tupleType <> " " <> joinWith " " decl.factorTypes <> ")"
]

--------------------------------------------------------------------------------
Expand Down Expand Up @@ -199,7 +199,7 @@ data HelperFunction

funToHelperFunction :: Boolean -> SolidityFunction -> CodeOptions -> Imported CurriedHelperFunctionR
funToHelperFunction isWhereClause fun@(SolidityFunction f) opts = do
(FunTypeDecl decl) <- funToTypeDecl fun opts
(FunTypeDecl decl) <- funToTypeDecl fun
import' "Network.Ethereum.Web3.Types" [IType "TransactionOptions"]
sigPrefix <-
if f.isConstructor
Expand Down Expand Up @@ -233,7 +233,7 @@ funToHelperFunction isWhereClause fun@(SolidityFunction f) opts = do
inputs' = map (\(FunctionInput fi) -> fi.type) f.inputs
conVars = mapWithIndex (\i _ -> var <> show (offset + i)) inputs'
helperTransport <- toTransportPrefix f.isConstructor f.constant $ length f.outputs
helperPayload <- toPayload isWhereClause decl.typeName conVars
helperPayload <- toPayload decl.typeName conVars
returnType <- toReturnType f.constant f.outputs
ins <-
if f.isUnCurried
Expand All @@ -254,7 +254,7 @@ funToHelperFunction isWhereClause fun@(SolidityFunction f) opts = do

funToHelperFunction' :: SolidityFunction -> CodeOptions -> Imported HelperFunction
funToHelperFunction' fun@(SolidityFunction f) opts = do
(FunTypeDecl decl) <- funToTypeDecl fun opts
(FunTypeDecl decl) <- funToTypeDecl fun
import' "Network.Ethereum.Web3.Types" [IType "TransactionOptions"]
sigPrefix <-
if f.isConstructor
Expand Down Expand Up @@ -284,7 +284,7 @@ funToHelperFunction' fun@(SolidityFunction f) opts = do
else ["x0"]
returnType <- toReturnType f.constant f.outputs
recIn <- recordInput f.inputs
whereC <- whereHelper decl sigPrefix f.inputs returnType >>= \h -> genCode h opts {indentationLevel = opts.indentationLevel + 4}
whereC <- whereHelper sigPrefix returnType >>= \h -> genCode h opts {indentationLevel = opts.indentationLevel + 4}
pure $
UnCurriedHelperFunction
{ signature: sigPrefix <> [recIn, returnType]
Expand All @@ -303,7 +303,7 @@ funToHelperFunction' fun@(SolidityFunction f) opts = do
ty <- toPSType fi.type
pure $ fi.name <> " :: " <> ty
pure $ "{ " <> joinWith ", " rowElems <> " }"
whereHelper d pre is ret = do
whereHelper pre ret = do
helper <- funToHelperFunction true fun opts
tys <-
if f.isUnCurried
Expand All @@ -322,8 +322,8 @@ tagInput
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 <> ")"
import' "Type.Proxy" [IType "Proxy"]
pure $ "(Tagged (Proxy " <> "\"" <> fi.name <> "\") " <> ty <> ")"

toTransportPrefix :: Boolean -> Boolean -> Int -> Imported String
toTransportPrefix isConstructor isCall outputCount = do
Expand All @@ -348,8 +348,8 @@ toTransportPrefix isConstructor isCall outputCount = do
pure ""
pure $ modifier <> fun

toPayload :: Boolean -> String -> Array String -> Imported String
toPayload isWhereClause typeName args = do
toPayload :: String -> Array String -> Imported String
toPayload typeName args = do
import' "Data.Functor.Tagged" [IVal "tagged"]
let tupleType = "Tuple" <> show (length args)
import' "Network.Ethereum.Web3.Solidity" [ITypeCtr tupleType]
Expand Down Expand Up @@ -453,11 +453,11 @@ instance codeEventGenericInstance :: Code EventGenericInstance where
in pure $ newLine2 $ i.genericDeriving : instances

eventToEventGenericInstance :: SolidityEvent -> Imported EventGenericInstance
eventToEventGenericInstance ev@(SolidityEvent e) = do
eventToEventGenericInstance ev@(SolidityEvent _) = do
(EventDataDecl decl) <- eventToDataDecl ev
let capConst = capitalize decl.constructor
import' "Data.Generic.Rep.Eq" [IVal "genericEq"]
import' "Data.Generic.Rep.Show" [IVal "genericShow"]
import' "Data.Eq.Generic" [IVal "genericEq"]
import' "Data.Show.Generic" [IVal "genericShow"]
import' "Data.Generic.Rep" [IClass "Generic"]
pure $
EventGenericInstance
Expand Down Expand Up @@ -500,8 +500,8 @@ eventToDecodeEventInstance event@(SolidityEvent ev) = do
where
taggedFactor (Tuple label value) = do
import' "Data.Functor.Tagged" [IType "Tagged"]
import' "Data.Symbol" [IType "SProxy"]
pure $ "(Tagged (SProxy \"" <> label <> "\") " <> value <> ")"
import' "Type.Proxy" [IType "Proxy"]
pure $ "(Tagged (Proxy \"" <> label <> "\") " <> value <> ")"


data EventFilterInstance =
Expand Down Expand Up @@ -561,7 +561,7 @@ eventToEventFilterInstance ev@(SolidityEvent e) = do


eventToEventCodeBlock :: SolidityEvent -> Imported CodeBlock
eventToEventCodeBlock ev@(SolidityEvent e) = do
eventToEventCodeBlock ev@(SolidityEvent _) = do
eventDec <- eventToDataDecl ev
eventFilterInstance <- eventToEventFilterInstance ev
decodeEventInstance <- eventToDecodeEventInstance ev
Expand All @@ -580,7 +580,7 @@ data CodeBlock =

funToFunctionCodeBlock :: SolidityFunction -> CodeOptions -> Imported CodeBlock
funToFunctionCodeBlock fun@(SolidityFunction f) opts = do
typeDecl <- funToTypeDecl fun opts
typeDecl <- funToTypeDecl fun
helperFunction <- if f.isUnCurried
then funToHelperFunction' fun opts
else funToHelperFunction false fun opts <#> CurriedHelperFunction
Expand Down
35 changes: 31 additions & 4 deletions test.dhall
@@ -1,6 +1,33 @@
let conf = ./spago.dhall

in conf // {
sources = conf.sources # [ "test/**/*.purs" ],
dependencies = conf.dependencies # [ "spec" ]
}
in conf
{ sources = conf.sources # [ "test/**/*.purs" ]
, dependencies =
conf.dependencies
# [ "spec"
, "arrays"
, "aff"
, "control"
, "argonaut-codecs"
, "argonaut-core"
, "argonaut-traversals"
, "bifunctors"
, "either"
, "exceptions"
, "foldable-traversable"
, "identity"
, "integers"
, "lists"
, "maybe"
, "newtype"
, "node-buffer"
, "node-fs"
, "node-path"
, "nonempty"
, "partial"
, "profunctor-lenses"
, "strings"
, "transformers"
, "tuples"
]
}
6 changes: 3 additions & 3 deletions test/Main.purs
Expand Up @@ -30,13 +30,13 @@ simpleStorageParserSpec =
parseSolidityType' "bytes32[2147483647][12][]" `shouldEqual`
Right (SolidityArray $ SolidityVector (pure top <> pure 12) (SolidityBytesN 32))
parseSolidityType' "bytes32[1asd][]" `shouldEqual`
Left "Failed to parse SolidityType \"bytes32[1asd][]\" with error: Could not match character ']'"
Left "Failed to parse SolidityType \"bytes32[1asd][]\" with error: { error: \"Could not match character ']'\", pos: 9 }"
parseSolidityType' "bytes32[21474836471][12][]" `shouldEqual`
Left "Failed to parse SolidityType \"bytes32[21474836471][12][]\" with error: Couldn't parse as Int : 21474836471"
Left "Failed to parse SolidityType \"bytes32[21474836471][12][]\" with error: { error: \"Couldn't parse as Int : 21474836471\", pos: 19 }"
parseSolidityType' "bytes999[]" `shouldEqual`
Right (SolidityArray $ SolidityBytesN 999)
parseSolidityType' "bytes32[] " `shouldEqual`
Left "Failed to parse SolidityType \"bytes32[] \" with error: Expected EOF"
Left "Failed to parse SolidityType \"bytes32[] \" with error: { error: \"Expected EOF\", pos: 9 }"

it "can parse the simple storage abi" do
ejson <- jsonParser <$> readTextFile UTF8 "./abi-data/truffle/build/contracts/SimpleStorage.json"
Expand Down

0 comments on commit 1864132

Please sign in to comment.