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 d3ca585
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
36 changes: 21 additions & 15 deletions src/Data/AbiParser.purs
Original file line number Diff line number Diff line change
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
43 changes: 24 additions & 19 deletions src/Data/Generator.purs
Original file line number Diff line number Diff line change
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 d3ca585

Please sign in to comment.