Skip to content

Commit

Permalink
Remove dependency on StringLike class (#154)
Browse files Browse the repository at this point in the history
* attempt to replace take, 2 tests failing

* tests apss

* purty

* wip: tests failing

* upadate eth-core
  • Loading branch information
martyall authored Apr 29, 2022
1 parent 222a2aa commit 9a99958
Show file tree
Hide file tree
Showing 15 changed files with 194 additions and 204 deletions.
2 changes: 1 addition & 1 deletion packages.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ let additions =
, "simple-json"
]
, repo = "https://github.com/f-o-a-m/purescript-eth-core.git"
, version = "v7.0.0"
, version = "v8.0.0"
}
, tagged =
{ dependencies = [ "identity", "profunctor" ]
Expand Down
4 changes: 0 additions & 4 deletions src/Network/Ethereum/Web3/Contract.purs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ import Network.Ethereum.Web3.Contract.Events (MultiFilterStreamState(..), event'
import Network.Ethereum.Web3.Solidity (class DecodeEvent, class GenericABIDecode, class GenericABIEncode, class RecordFieldsIso, genericABIEncode, genericFromData, genericFromRecordFields)
import Network.Ethereum.Web3.Types (class TokenUnit, CallError(..), ChainCursor, ETHER, Filter, NoPay, TransactionOptions, Value, Web3, _data, _value, convert, throwWeb3)


--------------------------------------------------------------------------------
-- * Events
--------------------------------------------------------------------------------
class EventFilter :: forall k. k -> Constraint
class EventFilter e where
-- | Event filter structure used by low-level subscription methods
Expand Down
46 changes: 31 additions & 15 deletions src/Network/Ethereum/Web3/Solidity/AbiEncoding.purs
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
module Network.Ethereum.Web3.Solidity.AbiEncoding where

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

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

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

instance abiEncodeBytesD :: ABIEncode ByteString where
Expand All @@ -66,7 +67,7 @@ instance abiEncodeBytesD :: ABIEncode ByteString where
instance abiDecodeBytesD :: ABIDecode ByteString where
fromDataParser = do
len <- unsafeToInt <$> fromDataParser
bytesDecode <<< unHex <$> take (len * 2)
bytesDecode <<< unHex <$> parseBytes len

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

zeroBytes = getPadLength (len * 2)
raw <- take $ len * 2
_ <- take $ zeroBytes
zeroBytes = 32 - len
raw <- parseBytes len
_ <- parseBytes zeroBytes
pure <<< update proxyBytesN <<< bytesDecode <<< unHex $ raw

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

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

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

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

-- | Read any number of HexDigits
take :: forall m. Monad m => Int -> ParserT HexString m HexString
take n = unsafePartial fromJust <<< mkHexString <<< fromCharArray <$> replicateA n anyChar
parseBytes :: forall m. Monad m => Int -> ParserT HexString m HexString
parseBytes n = A.fold <$> replicateA n parseByte

parseByte :: forall m. Monad m => ParserT HexString m HexString
parseByte = do
ParseState input (Position position) _ <- get
if numberOfBytes input < 1 then
fail "Unexpected EOF"
else do
let
{ after, before } = splitAt 2 (unHex input)

unsafeMkHex s = unsafePartial $ fromJust $ mkHexString s

position' = Position $ position { column = position.column + 1 }
put $ ParseState (unsafeMkHex after) position' true
pure $ unsafeMkHex before
3 changes: 0 additions & 3 deletions src/Network/Ethereum/Web3/Solidity/EncodingType.purs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import Network.Ethereum.Web3.Solidity.Vector (Vector)
import Network.Ethereum.Types (Address, BigNumber)
import Type.Proxy (Proxy(..))

--------------------------------------------------------------------------------
-- | Encoding Types
--------------------------------------------------------------------------------
class EncodingType :: forall k. k -> Constraint
class EncodingType a where
typeName :: Proxy a -> String
Expand Down
13 changes: 5 additions & 8 deletions src/Network/Ethereum/Web3/Solidity/Generic.purs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import Data.Generic.Rep (class Generic, Argument(..), Constructor(..), NoArgumen
import Data.Maybe (Maybe(..))
import Record as Record
import Data.Symbol (class IsSymbol)
import Network.Ethereum.Web3.Solidity.AbiEncoding (class ABIDecode, class ABIEncode, fromDataParser, take, toDataBuilder)
import Network.Ethereum.Web3.Solidity.AbiEncoding (class ABIDecode, class ABIEncode, fromDataParser, parseBytes, toDataBuilder)
import Network.Ethereum.Web3.Solidity.EncodingType (class EncodingType, isDynamic)
import Network.Ethereum.Core.HexString (HexString, hexLength)
import Network.Ethereum.Core.HexString (HexString, numberOfBytes)
import Network.Ethereum.Core.BigNumber (unsafeToInt)
import Text.Parsing.Parser (ParseError, ParseState(..), Parser, runParser)
import Text.Parsing.Parser.Combinators (lookAhead)
Expand Down Expand Up @@ -100,13 +100,13 @@ combineEncodedValues encodings =
in
case e.offset of
Nothing -> addTailOffsets init (head : acc) tail
Just _ -> addTailOffsets init (head : acc) (adjust (hexLength e.encoding `div` 2) tail)
Just _ -> addTailOffsets init (head : acc) (adjust (numberOfBytes e.encoding) tail)

headsOffset :: Int
headsOffset =
foldl
( \acc (EncodedValue e) -> case e.offset of
Nothing -> acc + (hexLength e.encoding `div` 2)
Nothing -> acc + numberOfBytes e.encoding
Just _ -> acc + 32
)
0
Expand Down Expand Up @@ -215,12 +215,9 @@ dParser = do
lookAhead
$ do
(ParseState _ (Position p) _) <- get
_ <- take (dataOffset * 2 - (p.column - 1))
_ <- parseBytes (dataOffset - (p.column - 1))
fromDataParser

--------------------------------------------------------------------------------
-- * Generator Helpers
--------------------------------------------------------------------------------
class ArgsToRowListProxy :: forall k. k -> RowList Type -> Constraint
class ArgsToRowListProxy args l | args -> l, l -> args where
argsToRowListProxy :: Proxy args -> RLProxy l
Expand Down
1 change: 0 additions & 1 deletion src/Network/Ethereum/Web3/Types/TokenUnit.purs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ instance unitTokenUnitSpec :: TokenUnitSpec a => TokenUnit (Value a) where
unValue :: forall a. Value a -> BigNumber
unValue (Value a) = a

-- | Useful for converting to and from the base denomination
class TokenUnit :: Type -> Constraint
class TokenUnit a where
fromMinorUnit :: BigNumber -> a
Expand Down
6 changes: 1 addition & 5 deletions src/Network/Ethereum/Web3/Types/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ instance decodeTransactionStatus :: Decode TransactionStatus where
case str of
"0x1" -> pure Succeeded
"0x0" -> pure Failed
_ -> fail $ TypeMismatch "TransactionStatus" str
_ -> fail $ TypeMismatch "TransactionStatus" str

newtype TransactionReceipt
= TransactionReceipt
Expand Down Expand Up @@ -458,10 +458,6 @@ forkWeb3' web3Action = do
p <- ask
liftAff $ forkWeb3 p web3Action

--------------------------------------------------------------------------------
-- * Filters
--------------------------------------------------------------------------------
-- | Low-level event filter data structure
newtype Filter :: forall k. k -> Type
newtype Filter a
= Filter
Expand Down
3 changes: 2 additions & 1 deletion test/web3/Web3Spec/Encoding/ContainersSpec.purs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ staticArraysTests =
given = unsafePartial fromJust (toVector s4 $ [ elem1, elem2, elem3, elem4 ]) :: Vector S4 (BytesN S1)

expected =
unsafePartial (fromJust <<< mkHexString) $ "cf00000000000000000000000000000000000000000000000000000000000000"
unsafePartial (fromJust <<< mkHexString)
$ "cf00000000000000000000000000000000000000000000000000000000000000"
<> "6800000000000000000000000000000000000000000000000000000000000000"
<> "4d00000000000000000000000000000000000000000000000000000000000000"
<> "fb00000000000000000000000000000000000000000000000000000000000000"
Expand Down
Loading

0 comments on commit 9a99958

Please sign in to comment.