Skip to content

Commit 9a99958

Browse files
authored
Remove dependency on StringLike class (#154)
* attempt to replace take, 2 tests failing * tests apss * purty * wip: tests failing * upadate eth-core
1 parent 222a2aa commit 9a99958

File tree

15 files changed

+194
-204
lines changed

15 files changed

+194
-204
lines changed

packages.dhall

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ let additions =
2727
, "simple-json"
2828
]
2929
, repo = "https://github.com/f-o-a-m/purescript-eth-core.git"
30-
, version = "v7.0.0"
30+
, version = "v8.0.0"
3131
}
3232
, tagged =
3333
{ dependencies = [ "identity", "profunctor" ]

src/Network/Ethereum/Web3/Contract.purs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ import Network.Ethereum.Web3.Contract.Events (MultiFilterStreamState(..), event'
2727
import Network.Ethereum.Web3.Solidity (class DecodeEvent, class GenericABIDecode, class GenericABIEncode, class RecordFieldsIso, genericABIEncode, genericFromData, genericFromRecordFields)
2828
import Network.Ethereum.Web3.Types (class TokenUnit, CallError(..), ChainCursor, ETHER, Filter, NoPay, TransactionOptions, Value, Web3, _data, _value, convert, throwWeb3)
2929

30-
31-
--------------------------------------------------------------------------------
32-
-- * Events
33-
--------------------------------------------------------------------------------
3430
class EventFilter :: forall k. k -> Constraint
3531
class EventFilter e where
3632
-- | Event filter structure used by low-level subscription methods

src/Network/Ethereum/Web3/Solidity/AbiEncoding.purs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
module Network.Ethereum.Web3.Solidity.AbiEncoding where
22

33
import Prelude
4-
import Data.Array (length) as A
4+
import Data.Array (length, fold) as A
55
import Data.ByteString (ByteString)
66
import Data.ByteString (toUTF8, fromUTF8, toString, fromString, length, Encoding(Hex)) as BS
77
import Data.Either (Either)
88
import Data.Foldable (foldMap)
99
import Data.Functor.Tagged (Tagged, tagged, untagged)
1010
import Data.Maybe (maybe, fromJust)
11-
import Data.String.CodeUnits (fromCharArray)
11+
import Data.String (splitAt)
1212
import Data.Unfoldable (replicateA)
1313
import Network.Ethereum.Core.BigNumber (toTwosComplement, unsafeToInt)
14-
import Network.Ethereum.Core.HexString (HexString, Signed(..), getPadLength, mkHexString, padLeft, padLeftSigned, padRight, toBigNumberFromSignedHexString, toBigNumber, toSignedHexString, unHex)
14+
import Network.Ethereum.Core.HexString (HexString, Signed(..), mkHexString, padLeft, padLeftSigned, padRight, toBigNumberFromSignedHexString, toBigNumber, toSignedHexString, unHex, numberOfBytes)
1515
import Network.Ethereum.Types (Address, BigNumber, embed, mkAddress, unAddress)
1616
import Network.Ethereum.Web3.Solidity.Bytes (BytesN, unBytesN, update, proxyBytesN)
1717
import Network.Ethereum.Web3.Solidity.Int (IntN, unIntN, intNFromBigNumber)
1818
import Network.Ethereum.Web3.Solidity.Size (class ByteSize, class IntSize, class KnownSize, DLProxy(..), sizeVal)
1919
import Network.Ethereum.Web3.Solidity.UInt (UIntN, unUIntN, uIntNFromBigNumber)
2020
import Network.Ethereum.Web3.Solidity.Vector (Vector)
2121
import Partial.Unsafe (unsafePartial)
22-
import Text.Parsing.Parser (ParseError, Parser, ParserT, fail, runParser)
23-
import Text.Parsing.Parser.String (anyChar)
22+
import Text.Parsing.Parser (ParseError, Parser, ParseState(..), ParserT, fail, runParser)
23+
import Control.Monad.State (get, put)
24+
import Text.Parsing.Parser.Pos (Position(..))
2425

2526
-- | Class representing values that have an encoding and decoding instance to/from a solidity type.
2627
class ABIEncode a where
@@ -56,8 +57,8 @@ instance abiEncodeAddress :: ABIEncode Address where
5657

5758
instance abiDecodeAddress :: ABIDecode Address where
5859
fromDataParser = do
59-
_ <- take 24
60-
maddr <- mkAddress <$> take 40
60+
_ <- parseBytes 12
61+
maddr <- mkAddress <$> parseBytes 20
6162
maybe (fail "Address is 20 bytes, receieved more") pure maddr
6263

6364
instance abiEncodeBytesD :: ABIEncode ByteString where
@@ -66,7 +67,7 @@ instance abiEncodeBytesD :: ABIEncode ByteString where
6667
instance abiDecodeBytesD :: ABIDecode ByteString where
6768
fromDataParser = do
6869
len <- unsafeToInt <$> fromDataParser
69-
bytesDecode <<< unHex <$> take (len * 2)
70+
bytesDecode <<< unHex <$> parseBytes len
7071

7172
instance abiEncodeString :: ABIEncode String where
7273
toDataBuilder = toDataBuilder <<< BS.toUTF8
@@ -82,9 +83,9 @@ instance abiDecodeBytesN :: ByteSize n => ABIDecode (BytesN n) where
8283
let
8384
len = sizeVal (DLProxy :: DLProxy n)
8485

85-
zeroBytes = getPadLength (len * 2)
86-
raw <- take $ len * 2
87-
_ <- take $ zeroBytes
86+
zeroBytes = 32 - len
87+
raw <- parseBytes len
88+
_ <- parseBytes zeroBytes
8889
pure <<< update proxyBytesN <<< bytesDecode <<< unHex $ raw
8990

9091
instance abiEncodeVector :: (ABIEncode a, KnownSize n) => ABIEncode (Vector n a) where
@@ -168,11 +169,11 @@ uInt256HexBuilder x =
168169

169170
-- | Parse as a signed `BigNumber`
170171
int256HexParser :: forall m. Monad m => ParserT HexString m BigNumber
171-
int256HexParser = toBigNumberFromSignedHexString <$> take 64
172+
int256HexParser = toBigNumberFromSignedHexString <$> parseBytes 32
172173

173174
-- | Parse an unsigned `BigNumber`
174175
uInt256HexParser :: forall m. Monad m => ParserT HexString m BigNumber
175-
uInt256HexParser = toBigNumber <$> take 64
176+
uInt256HexParser = toBigNumber <$> parseBytes 32
176177

177178
-- | Decode a `Boolean` as a BigNumber
178179
fromBool :: Boolean -> BigNumber
@@ -183,5 +184,20 @@ toBool :: BigNumber -> Boolean
183184
toBool bn = not $ bn == zero
184185

185186
-- | Read any number of HexDigits
186-
take :: forall m. Monad m => Int -> ParserT HexString m HexString
187-
take n = unsafePartial fromJust <<< mkHexString <<< fromCharArray <$> replicateA n anyChar
187+
parseBytes :: forall m. Monad m => Int -> ParserT HexString m HexString
188+
parseBytes n = A.fold <$> replicateA n parseByte
189+
190+
parseByte :: forall m. Monad m => ParserT HexString m HexString
191+
parseByte = do
192+
ParseState input (Position position) _ <- get
193+
if numberOfBytes input < 1 then
194+
fail "Unexpected EOF"
195+
else do
196+
let
197+
{ after, before } = splitAt 2 (unHex input)
198+
199+
unsafeMkHex s = unsafePartial $ fromJust $ mkHexString s
200+
201+
position' = Position $ position { column = position.column + 1 }
202+
put $ ParseState (unsafeMkHex after) position' true
203+
pure $ unsafeMkHex before

src/Network/Ethereum/Web3/Solidity/EncodingType.purs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import Network.Ethereum.Web3.Solidity.Vector (Vector)
1313
import Network.Ethereum.Types (Address, BigNumber)
1414
import Type.Proxy (Proxy(..))
1515

16-
--------------------------------------------------------------------------------
17-
-- | Encoding Types
18-
--------------------------------------------------------------------------------
1916
class EncodingType :: forall k. k -> Constraint
2017
class EncodingType a where
2118
typeName :: Proxy a -> String

src/Network/Ethereum/Web3/Solidity/Generic.purs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import Data.Generic.Rep (class Generic, Argument(..), Constructor(..), NoArgumen
2727
import Data.Maybe (Maybe(..))
2828
import Record as Record
2929
import Data.Symbol (class IsSymbol)
30-
import Network.Ethereum.Web3.Solidity.AbiEncoding (class ABIDecode, class ABIEncode, fromDataParser, take, toDataBuilder)
30+
import Network.Ethereum.Web3.Solidity.AbiEncoding (class ABIDecode, class ABIEncode, fromDataParser, parseBytes, toDataBuilder)
3131
import Network.Ethereum.Web3.Solidity.EncodingType (class EncodingType, isDynamic)
32-
import Network.Ethereum.Core.HexString (HexString, hexLength)
32+
import Network.Ethereum.Core.HexString (HexString, numberOfBytes)
3333
import Network.Ethereum.Core.BigNumber (unsafeToInt)
3434
import Text.Parsing.Parser (ParseError, ParseState(..), Parser, runParser)
3535
import Text.Parsing.Parser.Combinators (lookAhead)
@@ -100,13 +100,13 @@ combineEncodedValues encodings =
100100
in
101101
case e.offset of
102102
Nothing -> addTailOffsets init (head : acc) tail
103-
Just _ -> addTailOffsets init (head : acc) (adjust (hexLength e.encoding `div` 2) tail)
103+
Just _ -> addTailOffsets init (head : acc) (adjust (numberOfBytes e.encoding) tail)
104104

105105
headsOffset :: Int
106106
headsOffset =
107107
foldl
108108
( \acc (EncodedValue e) -> case e.offset of
109-
Nothing -> acc + (hexLength e.encoding `div` 2)
109+
Nothing -> acc + numberOfBytes e.encoding
110110
Just _ -> acc + 32
111111
)
112112
0
@@ -215,12 +215,9 @@ dParser = do
215215
lookAhead
216216
$ do
217217
(ParseState _ (Position p) _) <- get
218-
_ <- take (dataOffset * 2 - (p.column - 1))
218+
_ <- parseBytes (dataOffset - (p.column - 1))
219219
fromDataParser
220220

221-
--------------------------------------------------------------------------------
222-
-- * Generator Helpers
223-
--------------------------------------------------------------------------------
224221
class ArgsToRowListProxy :: forall k. k -> RowList Type -> Constraint
225222
class ArgsToRowListProxy args l | args -> l, l -> args where
226223
argsToRowListProxy :: Proxy args -> RLProxy l

src/Network/Ethereum/Web3/Types/TokenUnit.purs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ instance unitTokenUnitSpec :: TokenUnitSpec a => TokenUnit (Value a) where
7272
unValue :: forall a. Value a -> BigNumber
7373
unValue (Value a) = a
7474

75-
-- | Useful for converting to and from the base denomination
7675
class TokenUnit :: Type -> Constraint
7776
class TokenUnit a where
7877
fromMinorUnit :: BigNumber -> a

src/Network/Ethereum/Web3/Types/Types.purs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ instance decodeTransactionStatus :: Decode TransactionStatus where
219219
case str of
220220
"0x1" -> pure Succeeded
221221
"0x0" -> pure Failed
222-
_ -> fail $ TypeMismatch "TransactionStatus" str
222+
_ -> fail $ TypeMismatch "TransactionStatus" str
223223

224224
newtype TransactionReceipt
225225
= TransactionReceipt
@@ -458,10 +458,6 @@ forkWeb3' web3Action = do
458458
p <- ask
459459
liftAff $ forkWeb3 p web3Action
460460

461-
--------------------------------------------------------------------------------
462-
-- * Filters
463-
--------------------------------------------------------------------------------
464-
-- | Low-level event filter data structure
465461
newtype Filter :: forall k. k -> Type
466462
newtype Filter a
467463
= Filter

test/web3/Web3Spec/Encoding/ContainersSpec.purs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ staticArraysTests =
8585
given = unsafePartial fromJust (toVector s4 $ [ elem1, elem2, elem3, elem4 ]) :: Vector S4 (BytesN S1)
8686

8787
expected =
88-
unsafePartial (fromJust <<< mkHexString) $ "cf00000000000000000000000000000000000000000000000000000000000000"
88+
unsafePartial (fromJust <<< mkHexString)
89+
$ "cf00000000000000000000000000000000000000000000000000000000000000"
8990
<> "6800000000000000000000000000000000000000000000000000000000000000"
9091
<> "4d00000000000000000000000000000000000000000000000000000000000000"
9192
<> "fb00000000000000000000000000000000000000000000000000000000000000"

0 commit comments

Comments
 (0)