Skip to content

Commit

Permalink
deal with the special case of Tuple0 and Tuple1
Browse files Browse the repository at this point in the history
  • Loading branch information
martyall committed Oct 8, 2023
1 parent c8c9b08 commit 00bc7fe
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 35 deletions.
36 changes: 21 additions & 15 deletions src/Data/AbiParser.purs
Expand Up @@ -431,22 +431,28 @@ instance Show AbiDecodeError where
withMissingLabels
:: Array NamedSolidityType
-> Array NamedSolidityType
withMissingLabels as = flip map (zip (1 .. length as) as) \(Tuple n f@(NamedSolidityType { name, type: _t })) ->
case name of
Nothing -> NamedSolidityType { name: Just ("_" <> show n), type: _t }
Just _ -> f
withMissingLabels as = case as of
[] -> []
[ x ] -> [ x ]
_ -> flip map (zip (1 .. length as) as) \(Tuple n f@(NamedSolidityType { name, type: _t })) ->
case name of
Nothing -> NamedSolidityType { name: Just ("_" <> show n), type: _t }
Just _ -> f

withMissingLabels'
:: Array IndexedSolidityValue
-> Array IndexedSolidityValue
withMissingLabels' as = flip map (zip (1 .. length as) as) \(Tuple n f@(IndexedSolidityValue { type: t, indexed })) ->
let
NamedSolidityType { name, type: _t } = t
in
case name of
Nothing ->
let
nt' = NamedSolidityType { name: Just ("_" <> show n), type: _t }
in
IndexedSolidityValue { type: nt', indexed }
Just _ -> f
withMissingLabels' as = case as of
[] -> []
[ x ] -> [ x ]
_ -> flip map (zip (1 .. length as) as) \(Tuple n f@(IndexedSolidityValue { type: t, indexed })) ->
let
NamedSolidityType { name, type: _t } = t
in
case name of
Nothing ->
let
nt' = NamedSolidityType { name: Just ("_" <> show n), type: _t }
in
IndexedSolidityValue { type: nt', indexed }
Just _ -> f
45 changes: 25 additions & 20 deletions src/Data/Generator.purs
Expand Up @@ -3,7 +3,7 @@ module Data.Generator where
import Prelude

import Data.AbiParser (Abi(..), AbiType(..), BasicSolidityType(..), IndexedSolidityValue(..), NamedSolidityType(..), SolidityConstructor(..), SolidityEvent(..), SolidityFunction(..), SolidityType(..), format)
import Data.Array (concat, filter, length, null, replicate, snoc, (:))
import Data.Array (concat, filter, length, replicate, snoc, (:))
import Data.Identity (Identity(..))
import Data.Maybe (Maybe(..), fromJust, fromMaybe, isJust, isNothing, maybe)
import Data.Newtype (un)
Expand Down Expand Up @@ -60,25 +60,30 @@ basicToPSType opts a = case a of
pure $ Gen.typeApp (Gen.typeCtor bytesN) [ Gen.typeInt n ]
SolidityBytesD -> unsafePartial $ maybeWrap opts true $
Gen.typeCtor <$> TidyM.importFrom "Network.Ethereum.Web3.Solidity" (TidyM.importType "ByteString")
tup@(SolidityTuple factors) -> unsafePartial $ maybeWrap opts (noRecordsAtOrBelow $ BasicType tup) do
if null factors then Gen.typeCtor <$> TidyM.importFrom "Network.Ethereum.Web3.Solidity" (TidyM.importType "Tuple0")
else case for_ factors \(NamedSolidityType { name }) -> name of
Nothing -> unsafeCrashWith "Names should have been proved by fallback method which uses coordinates"
_ | opts.onlyTuples -> do
let tupleType = "Tuple" <> show (length factors)
tuple <- Gen.typeCtor <$> TidyM.importFrom "Network.Ethereum.Web3.Solidity" (TidyM.importType tupleType)
ts <- for factors \(NamedSolidityType { name: _name, type: t }) -> do
let n = unsafePartial $ fromJust _name
_pst <- toPSType opts t
tagInput (Tuple n _pst)
pure $ Gen.typeApp tuple ts
_ -> do
fs <- for factors $ \(NamedSolidityType { name: _name, type: t }) ->
let
n = unsafePartial $ fromJust _name
in
Tuple n <$> toPSType opts t
pure $ Gen.typeRecord fs Nothing
tup@(SolidityTuple factors) -> unsafePartial $ maybeWrap opts (noRecordsAtOrBelow $ BasicType tup)
case factors of
[] -> Gen.typeCtor <$> TidyM.importFrom "Network.Ethereum.Web3.Solidity" (TidyM.importType "Tuple0")
[ NamedSolidityType { name: Nothing, type: t } ] -> do
_t <- toPSType opts t
tuple1 <- Gen.typeCtor <$> TidyM.importFrom "Network.Ethereum.Web3.Solidity" (TidyM.importType "Tuple1")
pure $ Gen.typeApp tuple1 [ _t ]
_ -> case for_ factors \(NamedSolidityType { name }) -> name of
Nothing -> unsafeCrashWith "Names should have been proved by fallback method which uses coordinates"
_ | opts.onlyTuples -> do
let tupleType = "Tuple" <> show (length factors)
tuple <- Gen.typeCtor <$> TidyM.importFrom "Network.Ethereum.Web3.Solidity" (TidyM.importType tupleType)
ts <- for factors \(NamedSolidityType { name: _name, type: t }) -> do
let n = unsafePartial $ fromJust _name
_pst <- toPSType opts t
tagInput (Tuple n _pst)
pure $ Gen.typeApp tuple ts
_ -> do
fs <- for factors $ \(NamedSolidityType { name: _name, type: t }) ->
let
n = unsafePartial $ fromJust _name
in
Tuple n <$> toPSType opts t
pure $ Gen.typeRecord fs Nothing

toPSType
:: forall m
Expand Down

0 comments on commit 00bc7fe

Please sign in to comment.