Skip to content

Commit

Permalink
tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
martyall committed Sep 12, 2023
1 parent 59174eb commit aa58e2b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
25 changes: 8 additions & 17 deletions src/Network/Ethereum/Web3/Solidity/AbiEncoding.purs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import Data.ByteString (ByteString)
import Data.ByteString (toUTF8, fromUTF8, length) as BS
import Data.Either (Either)
import Data.Functor.Tagged (Tagged, tagged, untagged)
import Data.Maybe (Maybe(..), fromJust, maybe)
import Data.String (codePointFromChar, dropWhile, splitAt)
import Data.Maybe (fromJust, maybe)
import Data.String (splitAt)
import Data.Traversable (for, scanl)
import Data.Tuple (Tuple(..))
import Data.Unfoldable (replicateA)
import Network.Ethereum.Core.BigNumber (fromString, fromStringAs, fromTwosComplement256, hexadecimal, toString, toTwosComplement256, unsafeToInt)
import Network.Ethereum.Core.BigNumber (fromString, fromTwosComplement256, toString, toTwosComplement256, unsafeToInt)
import Network.Ethereum.Core.HexString (HexString, PadByte(..), fromByteString, mkHexString, numberOfBytes, padLeft, padRight, toByteString, unHex)
import Network.Ethereum.Types (Address, BigNumber, embed, mkAddress, unAddress)
import Network.Ethereum.Web3.Solidity.Bytes (BytesN, unBytesN, update, proxyBytesN)
Expand All @@ -38,7 +38,7 @@ import Network.Ethereum.Web3.Solidity.UInt (UIntN, unUIntN, uIntNFromBigNumber)
import Network.Ethereum.Web3.Solidity.Vector (Vector, unVector)
import Parsing (ParseError, Parser, ParseState(..), Position(..), ParserT, fail, getParserT, stateParserT, runParser)
import Parsing.Combinators (lookAhead)
import Partial.Unsafe (unsafeCrashWith, unsafePartial)
import Partial.Unsafe (unsafePartial)
import Type.Proxy (Proxy(..))

-- | Class representing values that have an encoding and decoding instance to/from a solidity type.
Expand Down Expand Up @@ -212,12 +212,6 @@ instance abiDecodeTagged :: ABIDecode a => ABIDecode (Tagged s a) where
-- | Special Builders and Parsers
--------------------------------------------------------------------------------

unsafeFromJust :: forall a. String -> Maybe a -> a
unsafeFromJust str a =
case a of
Just a' -> a'
Nothing -> unsafeCrashWith str

-- | base16 encode, then utf8 encode, then pad
bytesBuilder :: ByteString -> HexString
bytesBuilder = padRight Zero <<< fromByteString
Expand All @@ -227,18 +221,15 @@ int256HexBuilder :: BigNumber -> HexString
int256HexBuilder x =
let
a = toTwosComplement256 x
x' = unsafeFromJust ("int256HexBuilder " <> show a) $ mkHexString (toString a)
x' = unsafePartial $ fromJust $ mkHexString (toString a)
in
if x < zero then padLeft FF x'
else padLeft Zero x'

-- | Encode something that is essentially an unsigned integer.
uInt256HexBuilder :: BigNumber -> HexString
uInt256HexBuilder x =
let
a = mkHexString $ toString x
in
padLeft Zero $ unsafeFromJust ("uInt256HexBuilder " <> show x) a
uInt256HexBuilder x = unsafePartial $ fromJust $
padLeft Zero <$> mkHexString (toString x)

-- | Parse as a signed `BigNumber`
int256HexParser :: forall m. Monad m => ParserT HexString m BigNumber
Expand Down Expand Up @@ -275,7 +266,7 @@ parseByte = do
let
{ after, before } = splitAt 2 (unHex input)

mkHex s = maybe (fail $ "Unable to parse HexString: " <> s) pure $ mkHexString s
mkHex s = maybe (fail $ "Unable to parse bytes from hex: " <> s) pure $ mkHexString s

position' = Position $ position { column = position.column + 1 }

Expand Down
11 changes: 5 additions & 6 deletions test/web3/Web3Spec/Encoding/ContainersSpec.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,26 @@ import Data.ByteString as BS
import Data.Either (Either(..))
import Data.Generic.Rep (class Generic)
import Data.Maybe (fromJust)
import Debug (spy)
import Effect.Aff (Aff)
import Effect.Class (liftEffect)
import Network.Ethereum.Web3.Solidity (BytesN, IntN, Tuple1(..), Tuple2(..), Tuple4(..), Tuple9(..), UIntN, fromByteString, intNFromBigNumber, nilVector, toDataBuilder, uIntNFromBigNumber, (:<))
import Network.Ethereum.Web3.Solidity (BytesN, IntN, Tuple1(..), Tuple2(..), Tuple4(..), Tuple9(..), UIntN, fromByteString, intNFromBigNumber, nilVector, uIntNFromBigNumber, (:<))
import Network.Ethereum.Web3.Solidity.AbiEncoding (class ABIEncode, class ABIDecode, toDataBuilder, fromData)
import Network.Ethereum.Web3.Solidity.Generic (genericFromData, genericABIEncode, class GenericABIDecode, class GenericABIEncode)
import Network.Ethereum.Web3.Solidity.Sizes (s1, s16, s2, s224, s256, s4)
import Network.Ethereum.Web3.Solidity.Vector (Vector, toVector)
import Network.Ethereum.Web3.Types (Address, HexString, embed, mkAddress, mkHexString, unHex)
import Parsing (ParseError)
import Partial.Unsafe (unsafePartial)
import Test.QuickCheck (quickCheck, (<?>), (===))
import Test.QuickCheck (quickCheck, (<?>))
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (shouldEqual)

spec :: Spec Unit
spec =
describe "encoding-spec for containers" do
--staticArraysTests
--dynamicArraysTests
--tuplesTest
staticArraysTests
dynamicArraysTests
tuplesTest
typePropertyTests

roundTrip :: forall a. Show a => Eq a => ABIEncode a => ABIDecode a => a -> HexString -> Aff Unit
Expand Down

0 comments on commit aa58e2b

Please sign in to comment.