diff --git a/spago.dhall b/spago.dhall index eb92449..1da3b1d 100644 --- a/spago.dhall +++ b/spago.dhall @@ -8,7 +8,6 @@ , "control" , "coroutine-transducers" , "coroutines" - , "debug" , "effect" , "either" , "errors" @@ -28,7 +27,6 @@ , "partial" , "prelude" , "profunctor-lenses" - , "quickcheck" , "record" , "ring-modules" , "simple-json" diff --git a/src/Network/Ethereum/Web3/Solidity/Bytes.purs b/src/Network/Ethereum/Web3/Solidity/Bytes.purs index bb558a1..1afae38 100644 --- a/src/Network/Ethereum/Web3/Solidity/Bytes.purs +++ b/src/Network/Ethereum/Web3/Solidity/Bytes.purs @@ -14,7 +14,7 @@ import Data.ByteString (empty, ByteString, Encoding(Hex)) import Data.ByteString as BS import Data.Maybe (Maybe(..), fromJust) import Data.Reflectable (class Reflectable, reflectType) -import Network.Ethereum.Core.HexString (genBytes, toByteString) +import Network.Ethereum.Core.HexString as Hex import Network.Ethereum.Types (mkHexString) import Partial.Unsafe (unsafePartial) import Type.Proxy (Proxy(..)) @@ -32,8 +32,8 @@ instance showBytesN :: Show (BytesN n) where generator :: forall n m. Reflectable n Int => MonadGen m => Proxy n -> m (BytesN n) generator p = do - bs <- genBytes (reflectType p) - pure $ BytesN $ toByteString bs + bs <- Hex.generator (reflectType p) + pure $ BytesN $ Hex.toByteString bs -- | Access the underlying raw bytestring unBytesN :: forall n. BytesN n -> ByteString diff --git a/src/Network/Ethereum/Web3/Solidity/Int.purs b/src/Network/Ethereum/Web3/Solidity/Int.purs index 9b908b0..dae57e6 100644 --- a/src/Network/Ethereum/Web3/Solidity/Int.purs +++ b/src/Network/Ethereum/Web3/Solidity/Int.purs @@ -11,7 +11,7 @@ import Control.Monad.Gen (class MonadGen) import Data.Maybe (Maybe(..), fromJust) import Data.Reflectable (class Reflectable, reflectType) import Network.Ethereum.Core.BigNumber (BigNumber, embed, fromString, fromTwosComplement, pow) -import Network.Ethereum.Core.HexString (genBytes, unHex) +import Network.Ethereum.Core.HexString as Hex import Partial.Unsafe (unsafePartial) import Type.Proxy (Proxy(..)) @@ -28,11 +28,11 @@ derive newtype instance ordIntN :: Ord (IntN n) generator :: forall n m. Reflectable n Int => MonadGen m => Proxy n -> m (IntN n) generator p = do - bs <- genBytes (reflectType p `div` 8) + bs <- Hex.generator (reflectType p `div` 8) let a = if bs == mempty then zero - else unsafePartial $ fromJust $ fromString $ unHex $ bs + else unsafePartial $ fromJust $ fromString $ Hex.unHex $ bs pure $ IntN $ fromTwosComplement (reflectType (Proxy @n)) a -- | Access the raw underlying integer diff --git a/src/Network/Ethereum/Web3/Solidity/UInt.purs b/src/Network/Ethereum/Web3/Solidity/UInt.purs index d96699a..494812a 100644 --- a/src/Network/Ethereum/Web3/Solidity/UInt.purs +++ b/src/Network/Ethereum/Web3/Solidity/UInt.purs @@ -11,7 +11,7 @@ import Control.Monad.Gen (class MonadGen, chooseInt) import Data.Maybe (Maybe(..), fromJust) import Data.Reflectable (class Reflectable, reflectType) import Network.Ethereum.Core.BigNumber (BigNumber, embed, fromString, pow) -import Network.Ethereum.Core.HexString (genBytes, unHex) +import Network.Ethereum.Core.HexString as Hex import Partial.Unsafe (unsafePartial) import Test.QuickCheck (class Arbitrary) import Test.QuickCheck.Gen as Gen @@ -36,21 +36,21 @@ generator -> m (UIntN n) generator p = do nBytes <- (flip div 8) <$> chooseInt 1 (reflectType p) - bs <- genBytes nBytes + bs <- Hex.generator nBytes let a = if bs == mempty then zero - else unsafePartial $ fromJust $ fromString $ unHex bs + else unsafePartial $ fromJust $ fromString $ Hex.unHex bs pure $ UIntN $ if a < zero then -a else a instance Reflectable n Int => Arbitrary (UIntN n) where arbitrary = do nBytes <- (flip div 8) <$> Gen.chooseInt 1 (reflectType (Proxy @n)) - bs <- genBytes nBytes + bs <- Hex.generator nBytes let a = if bs == mempty then zero - else unsafePartial $ fromJust $ fromString $ unHex bs + else unsafePartial $ fromJust $ fromString $ Hex.unHex bs pure $ UIntN $ if a < zero then -a else a -- | Access the raw underlying unsigned integer diff --git a/test.dhall b/test.dhall index 4dd447d..c0a2cfc 100644 --- a/test.dhall +++ b/test.dhall @@ -14,5 +14,6 @@ in conf , "identity" , "enums" , "integers" + , "quickcheck" ] } diff --git a/test/web3/Web3Spec/Encoding/ContainersSpec.purs b/test/web3/Web3Spec/Encoding/ContainersSpec.purs index afd7d95..5134105 100644 --- a/test/web3/Web3Spec/Encoding/ContainersSpec.purs +++ b/test/web3/Web3Spec/Encoding/ContainersSpec.purs @@ -17,7 +17,8 @@ import Data.Reflectable (reifyType) import Data.String (CodePoint, fromCodePointArray) import Data.Tuple (Tuple(..)) import Effect.Class (liftEffect) -import Network.Ethereum.Core.HexString (genBytes, toByteString) +import Network.Ethereum.Core.HexString as Hex +import Network.Ethereum.Core.Signatures as Address import Network.Ethereum.Web3.Solidity (class GenericABIDecode, class GenericABIEncode, Tuple4(..), Tuple5(..), genericABIEncode, genericFromData) import Network.Ethereum.Web3.Solidity.AbiEncoding (class ABIEncode, class ABIDecode, toDataBuilder, fromData) import Network.Ethereum.Web3.Solidity.Bytes as BytesN @@ -25,7 +26,6 @@ import Network.Ethereum.Web3.Solidity.EncodingType (class EncodingType) import Network.Ethereum.Web3.Solidity.Int as IntN import Network.Ethereum.Web3.Solidity.UInt as UIntN import Network.Ethereum.Web3.Solidity.Vector as Vector -import Network.Ethereum.Web3.Types (Address, HexString) import Parsing (ParseError) import Partial.Unsafe (unsafePartial) import Test.QuickCheck (class Arbitrary, arbitrary, quickCheck, quickCheckGen, (===)) @@ -48,17 +48,18 @@ typePropertyTests = quickCheck \(x :: BMPString) -> (encodeDecode x) === Right x it "can encode/decode bytestring" $ liftEffect $ do - quickCheck \(_x :: HexString) -> - let - x = toByteString _x - in - (encodeDecode x) === Right x + quickCheckGen $ do + n <- chooseInt 1 100 + x <- Hex.toByteString <$> Hex.generator n + pure $ encodeDecode x === Right x it "can encode/decode bool" $ liftEffect $ do quickCheck \(x :: Boolean) -> encodeDecode x === Right x it "can encode/decode address" $ liftEffect $ do - quickCheck \(x :: Address) -> encodeDecode x === Right x + quickCheckGen $ do + x <- Address.generator + pure $ encodeDecode x === Right x it "can encode/decode intN" $ liftEffect $ do for_ intSizes $ \n -> quickCheckGen $ do @@ -105,7 +106,9 @@ arrayTypePropertyTests = do pure $ encodeDecode x === Right x it "Can encode/decode address[]" $ liftEffect do - quickCheck $ \(x :: Address) -> encodeDecode x === Right x + quickCheckGen $ do + x <- Address.generator + pure $ encodeDecode x === Right x it "Can encode/decode string[]" $ liftEffect do quickCheck $ \(x :: Array BMPString) -> @@ -147,7 +150,7 @@ vecTypePropertyTests = do quickCheckGen $ do k <- chooseInt 1 10 reifyType k \pk -> do - x <- Vector.generator pk (arbitrary :: Gen Address) + x <- Vector.generator pk Address.generator pure $ encodeDecode x === Right x it "Can encode/decode string[k]" $ liftEffect do @@ -243,7 +246,7 @@ tupleTests = do reifyType m \pm -> reifyType k \pk -> do int <- IntN.generator pn - addr <- arbitrary :: Gen Address + addr <- Address.generator bool <- arbitrary :: Gen Boolean uint <- UIntN.generator pm bytes <- BytesN.generator pk @@ -260,7 +263,7 @@ tupleTests = do reifyType k2 \pk2 -> reifyType n \pn -> do reifyType m \pm -> do - addrs <- arrayOf (Vector.generator pk1 (arbitrary @Address)) + addrs <- arrayOf (Vector.generator pk1 Address.generator) bool <- arbitrary @Boolean ints <- Vector.generator pk2 (IntN.generator pn) uint <- (UIntN.generator pm) @@ -278,8 +281,8 @@ tupleTests = do reifyType m \pm -> reifyType k \pk -> do ints <- arrayOf (IntN.generator pn) - bytes <- toByteString <$> (chooseInt 1 100 >>= genBytes) - addrs <- Vector.generator pm (arrayOf $ arbitrary @Address) + bytes <- Hex.toByteString <$> (chooseInt 1 100 >>= Hex.generator) + addrs <- Vector.generator pm (arrayOf Address.generator) strings <- arrayOf (Vector.generator pk (arbitrary @BMPString)) bool <- arbitrary :: Gen Boolean let x = Tuple5 ints bytes addrs strings bool @@ -295,7 +298,7 @@ tupleTests = do reifyType k2 \pk2 -> reifyType n \pn -> do reifyType m \pm -> do - addrs <- arrayOf (Vector.generator pk1 (arbitrary @Address)) + addrs <- arrayOf (Vector.generator pk1 Address.generator) bool <- arbitrary @Boolean ints <- Vector.generator pk2 (IntN.generator pn) uint <- (UIntN.generator pm) diff --git a/test/web3/Web3Spec/Encoding/DataSpec.purs b/test/web3/Web3Spec/Encoding/DataSpec.purs index 9373710..80cd4b9 100644 --- a/test/web3/Web3Spec/Encoding/DataSpec.purs +++ b/test/web3/Web3Spec/Encoding/DataSpec.purs @@ -5,12 +5,14 @@ import Prelude import Data.Functor.Tagged (Tagged, tagged) import Effect.Class (liftEffect) import Network.Ethereum.Core.Keccak256 (toSelector) +import Network.Ethereum.Core.Signatures as Address import Network.Ethereum.Web3.Contract (sendTx, mkDataField) import Network.Ethereum.Web3.Solidity (Tuple2, UIntN) import Network.Ethereum.Web3.Solidity.AbiEncoding (toDataBuilder) import Network.Ethereum.Web3.Solidity.Generic (genericFromRecordFields) +import Network.Ethereum.Web3.Solidity.UInt as UIntN import Network.Ethereum.Web3.Types (Address, HexString, NoPay, TransactionOptions, Web3) -import Test.QuickCheck (quickCheck, (===)) +import Test.QuickCheck (quickCheckGen, (===)) import Test.Spec (Spec, describe, it) import Type.Proxy (Proxy(..)) @@ -18,15 +20,15 @@ spec :: Spec Unit spec = describe "data maker" do it "can make the approval data" $ liftEffect do - quickCheck $ \(args :: { _spender :: Address, _value :: UIntN 256 }) -> + quickCheckGen do + args <- { _spender: _, _value: _ } <$> Address.generator <*> UIntN.generator (Proxy @256) let approvalD = mkDataField (Proxy :: Proxy ApproveFn) args sel = toSelector "approve(address,uint256)" fullDat = sel <> toDataBuilder args._spender <> toDataBuilder args._value - in - approvalD === fullDat + pure $ approvalD === fullDat type ApproveFn = Tagged "approve(address,uint256)" (Tuple2 (Tagged "_spender" Address) (Tagged "_value" (UIntN 256)))