Skip to content

Commit 6cad0e4

Browse files
authored
Upgrade foreign-generics (#101)
* update bower file * tests pass * remove simple-json dep * fix tests * add live tests * add cliquebait to docker file * make cliquebait make more accounts * update bower file
1 parent f9dab66 commit 6cad0e4

File tree

10 files changed

+467
-132
lines changed

10 files changed

+467
-132
lines changed

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ language: node_js
22
dist: trusty
33
sudo: required
44
node_js: stable
5+
services:
6+
- docker
57
install:
68
- npm install -g bower
79
- npm install
810
script:
911
- bower install --production
1012
- npm run -s build
1113
- bower install
14+
- docker run -d -p 8545:8545 -e ACCOUNTS_TO_CREATE=10 foamspace/cliquebait:latest
15+
- sleep 10
1216
- npm -s test
1317
after_success:
1418
- >-

bower.json

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,22 @@
1717
],
1818
"dependencies": {
1919
"purescript-errors": "^3.0.0",
20-
"purescript-profunctor-lenses": "^3.7.0",
20+
"purescript-profunctor-lenses": "^3.8.0",
2121
"purescript-foreign": "^4.0.1",
22-
"purescript-foreign-generic": "^5.0.0",
22+
"purescript-foreign-generic": "^6.0.0",
2323
"purescript-proxy": "^2.1.0",
2424
"purescript-bytestrings": "^6.0.0",
25-
"purescript-eth-core": "^0.0.1",
25+
"purescript-eth-core": "^1.0.0",
2626
"purescript-partial": "^1.2.1",
2727
"purescript-parsing": "^4.3.1",
2828
"purescript-transformers": "^3.6.0",
2929
"purescript-identity": "^3.1.0",
30-
"purescript-aff": "^4.0.0",
30+
"purescript-aff": "^4.1.0",
3131
"purescript-tagged": "^2.0.0",
32-
"purescript-free": "^4.2.0",
32+
"purescript-free": "^4.3.0",
3333
"purescript-coroutines": "^4.0.0",
34-
"purescript-typelevel-prelude": "^2.6.0",
35-
"purescript-type-equality": "^2.1.0",
36-
"purescript-modules": "^3.0.0",
37-
"purescript-mmorph": "^3.0.0",
38-
"purescript-simple-json": "^2.0.1"
34+
"purescript-typelevel-prelude": "^2.7.0",
35+
"purescript-modules": "^3.0.0"
3936
},
4037
"devDependencies": {
4138
"purescript-debug": "v3.0.0",

src/Network/Ethereum/Web3/Api.purs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
module Network.Ethereum.Web3.Api where
22

3+
import Data.Maybe (Maybe, fromMaybe)
34
import Network.Ethereum.Web3.JsonRPC (remote)
45
import Network.Ethereum.Types (Address, HexString, BigNumber)
56
import Network.Ethereum.Web3.Types (Block, BlockNumber, ChainCursor, Change, FalseOrObject, Filter, FilterId, NoPay, SyncStatus, Transaction, TransactionOptions, TransactionReceipt, Web3, Wei)
67
import Type.Data.Boolean (kind Boolean)
78

89
-- | Returns current node version string.
9-
web3_clientVersion :: forall e . Web3 e String
10+
web3_clientVersion :: forall e . Partial => Web3 e String
1011
web3_clientVersion = remote "web3_clientVersion"
1112

1213
-- | Returns Keccak-256 (*not* the standardized SHA3-256) of the given data.
13-
web3_sha3 :: forall e. HexString -> Web3 e HexString
14+
web3_sha3 :: forall e. Partial => HexString -> Web3 e HexString
1415
web3_sha3 hexInput = remote "web3_sha3" hexInput
1516

1617
-- | Get the network id that the node is listening to.
17-
net_version :: forall e . Web3 e BigNumber
18+
net_version :: forall e . Web3 e String
1819
net_version = remote "net_version"
1920

2021
-- | Returns `true`` if client is actively listening for network connections
@@ -73,8 +74,6 @@ eth_getBlockTransactionCountByHash blockHash = remote "eth_getBlockTransactionCo
7374
eth_getBlockTransactionCountByNumber :: forall e. ChainCursor -> Web3 e BigNumber
7475
eth_getBlockTransactionCountByNumber cm = remote "eth_getBlockTransactionCountByNumber" cm
7576

76-
-- TODO - is it appropriate for these to be Ints?
77-
7877
-- | Returns the number of uncles in a block from a block matching the given block hash
7978
eth_getUncleCountByBlockHash :: forall e. HexString -> Web3 e BigNumber
8079
eth_getUncleCountByBlockHash blockNumber = remote "eth_getUncleCountByBlockHash" blockNumber
@@ -87,19 +86,13 @@ eth_getUncleCountByBlockNumber cm = remote "eth_getUncleCountByBlockNumber" cm
8786
eth_getCode :: forall e. Address -> ChainCursor -> Web3 e HexString
8887
eth_getCode addr cm = remote "eth_getCode" addr cm
8988

90-
-- | The sign method calculates an Ethereum specific signature with: `sign(keccak256("\x19Ethereum Signed Message:\n" + len(message) + message)))`.
91-
-- | By adding a prefix to the message makes the calculated signature recognisable as an Ethereum specific signature. This prevents misuse where a malicious DApp can sign arbitrary data (e.g. transaction) and use the signature to impersonate the victim.
92-
-- | **Note** the address to sign with must be unlocked.
93-
eth_sign :: forall e. Warn "eth_sign is deprecated in favor of personal_sign" => Address -> HexString -> Web3 e HexString
94-
eth_sign addr msg = remote "eth_sign" addr msg
95-
9689
-- | Creates new message call transaction or a contract creation for signed transactions
9790
eth_sendRawTransaction :: forall e. HexString -> Web3 e HexString
9891
eth_sendRawTransaction rawTx = remote "eth_sendRawTransaction" rawTx
9992

10093
-- | Makes a call or transaction, which won't be added to the blockchain and returns the used gas, which can be used for estimating the used gas.
101-
eth_estimateGas :: forall e. TransactionOptions Wei -> ChainCursor -> Web3 e BigNumber
102-
eth_estimateGas txOpts cm = remote "eth_estimateGas" txOpts cm
94+
eth_estimateGas :: forall e. TransactionOptions Wei -> Web3 e BigNumber
95+
eth_estimateGas txOpts = remote "eth_estimateGas" txOpts
10396

10497
-- | Returns information about a transaction by block hash and transaction index position.
10598
eth_getTransactionByBlockHashAndIndex :: forall e. HexString -> BigNumber -> Web3 e Transaction
@@ -122,14 +115,9 @@ eth_getUncleByBlockNumberAndIndex :: forall e. ChainCursor -> BigNumber -> Web3
122115
eth_getUncleByBlockNumberAndIndex cm uncleIndex = remote "eth_getUncleByBlockNumberAndIndex" cm uncleIndex
123116

124117
-- | Returns a list of available compilers in the client.
125-
eth_getCompilers :: forall e. Web3 e (Array String)
118+
eth_getCompilers :: forall e. Partial => Web3 e (Array String)
126119
eth_getCompilers = remote "eth_getCompilers"
127120

128-
-- TODO: As the ABI is returned decoding this isn't trivial - not going to implement without a need
129-
-- -- | Returns compiled solidity code.
130-
-- eth_compileSolidity :: forall e. String -> Web3 e HexString
131-
-- eth_compileSolidity code = remote "eth_compileSolidity"
132-
133121
-- | Returns information about a block by number.
134122
eth_getBlockByNumber :: forall e . ChainCursor -> Web3 e Block
135123
eth_getBlockByNumber cm = remote "eth_getBlockByNumber" cm false
@@ -181,9 +169,9 @@ eth_uninstallFilter :: forall e . FilterId -> Web3 e Boolean
181169
eth_uninstallFilter fid = remote "eth_uninstallFilter" fid
182170

183171
-- | Sign a message with the given address, returning the signature.
184-
personal_sign :: forall e . HexString -> Address -> Web3 e HexString
185-
personal_sign _data signer = remote "personal_sign" _data signer
172+
personal_sign :: forall e . HexString -> Address -> Maybe String -> Web3 e HexString
173+
personal_sign _data signer password = remote "personal_sign" _data signer (fromMaybe "" password)
186174

187-
-- | Recover the address that signed the message.
175+
-- | Recover the address that signed the message from (1) the message and (2) the signature
188176
personal_ecRecover :: forall e . HexString -> HexString -> Web3 e Address
189177
personal_ecRecover _data sig = remote "personal_ecRecover" _data sig

src/Network/Ethereum/Web3/Contract.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ _sendTransaction :: forall a u rep e selector .
121121
-> Web3 e HexString
122122
_sendTransaction txOptions dat = do
123123
let sel = toSelector <<< reflectSymbol $ (SProxy :: SProxy selector)
124-
eth_sendTransaction <<< txdata $ sel <> (genericABIEncode <<< untagged $ dat)
124+
eth_sendTransaction $ txdata $ sel <> (genericABIEncode <<< untagged $ dat)
125125
where
126126
txdata d = txOptions # _data .~ Just d
127127
# _value %~ map convert

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

Lines changed: 52 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ import Prelude
4646

4747
import Control.Alt (class Alt)
4848
import Control.Alternative (class Alternative, class Plus, (<|>))
49-
import Control.Monad.Aff (Aff, Fiber, ParAff, forkAff, liftEff', throwError)
49+
import Control.Monad.Aff (Aff, Fiber, ParAff, forkAff, liftEff')
5050
import Control.Monad.Aff.Class (class MonadAff, liftAff)
5151
import Control.Monad.Eff (kind Effect)
5252
import Control.Monad.Eff.Class (class MonadEff)
5353
import Control.Monad.Eff.Exception (Error, throwException)
5454
import Control.Monad.Error.Class (class MonadThrow, catchError)
55-
import Control.Monad.Except (ExceptT, except, runExceptT)
55+
import Control.Monad.Except (ExceptT, runExceptT)
5656
import Control.Monad.Reader (class MonadAsk, class MonadReader, ReaderT, ask, runReaderT)
5757
import Control.Monad.Rec.Class (class MonadRec)
5858
import Control.Parallel.Class (class Parallel, parallel, sequential)
@@ -61,7 +61,6 @@ import Data.Foreign (F, Foreign, ForeignError(..), fail, isNull, readBoolean, re
6161
import Data.Foreign.Class (class Decode, class Encode, decode, encode)
6262
import Data.Foreign.Generic (defaultOptions, genericDecode, genericEncode)
6363
import Data.Foreign.Index (readProp)
64-
import Data.Foreign.NullOrUndefined (NullOrUndefined(..), unNullOrUndefined)
6564
import Data.Functor.Compose (Compose)
6665
import Data.Generic.Rep (class Generic)
6766
import Data.Generic.Rep.Eq (genericEq)
@@ -70,12 +69,9 @@ import Data.Lens.Lens (Lens', Lens, lens)
7069
import Data.Maybe (Maybe(..))
7170
import Data.Newtype (class Newtype, unwrap)
7271
import Data.Ordering (invert)
73-
import Data.Record as Record
74-
import Data.Symbol (SProxy(..))
7572
import Network.Ethereum.Types (Address, BigNumber, HexString)
7673
import Network.Ethereum.Web3.Types.EtherUnit (class EtherUnit, NoPay, Value, Wei, convert)
7774
import Network.Ethereum.Web3.Types.Provider (Provider)
78-
import Simple.JSON (read)
7975

8076
--------------------------------------------------------------------------------
8177
-- * Block
@@ -128,11 +124,11 @@ newtype Block
128124
, extraData :: HexString
129125
, gasLimit :: BigNumber
130126
, gasUsed :: BigNumber
131-
, hash :: HexString
132-
, logsBloom :: HexString
127+
, hash :: Maybe HexString
128+
, logsBloom :: Maybe HexString
133129
, miner :: HexString
134-
, nonce :: HexString
135-
, number :: BigNumber
130+
, nonce :: Maybe HexString
131+
, number :: Maybe BigNumber
136132
, parentHash :: HexString
137133
, receiptsRoot :: HexString
138134
, sha3Uncles :: HexString
@@ -153,18 +149,7 @@ instance showBlock :: Show Block where
153149
show = genericShow
154150

155151
instance decodeBlock :: Decode Block where
156-
decode x = catchError (genericDecode decodeOpts x)
157-
-- if this attempt fails for any reason pass back the original error
158-
\origError -> catchError tryKovanAuthorHack (\_ -> throwError origError)
159-
where
160-
decodeOpts = defaultOptions { unwrapSingleConstructors = true }
161-
tryKovanAuthorHack = do
162-
rec <- except $ read x
163-
let blockRec = Record.delete (SProxy :: SProxy "author") rec
164-
# Record.insert (SProxy :: SProxy "nonce") rec.author
165-
pure $ Block blockRec
166-
167-
152+
decode x = genericDecode (defaultOptions { unwrapSingleConstructors = true }) x
168153

169154
--------------------------------------------------------------------------------
170155
-- * Transaction
@@ -173,11 +158,11 @@ instance decodeBlock :: Decode Block where
173158
newtype Transaction =
174159
Transaction { hash :: HexString
175160
, nonce :: BigNumber
176-
, blockHash :: HexString
177-
, blockNumber :: BlockNumber
178-
, transactionIndex :: BigNumber
161+
, blockHash :: Maybe HexString
162+
, blockNumber :: Maybe BlockNumber
163+
, transactionIndex :: Maybe BigNumber
179164
, from :: Address
180-
, to :: NullOrUndefined Address
165+
, to :: Maybe Address
181166
, value :: Value Wei
182167
, gas :: BigNumber
183168
, gasPrice :: BigNumber
@@ -221,7 +206,7 @@ newtype TransactionReceipt =
221206
, blockNumber :: BlockNumber
222207
, cumulativeGasUsed :: BigNumber
223208
, gasUsed :: BigNumber
224-
, contractAddress :: NullOrUndefined Address
209+
, contractAddress :: Maybe Address
225210
, logs :: Array Change
226211
, status :: TransactionStatus
227212
}
@@ -241,13 +226,13 @@ instance decodeTxReceipt :: Decode TransactionReceipt where
241226
--------------------------------------------------------------------------------
242227

243228
newtype TransactionOptions u =
244-
TransactionOptions { from :: NullOrUndefined Address
245-
, to :: NullOrUndefined Address
246-
, value :: NullOrUndefined (Value u)
247-
, gas :: NullOrUndefined BigNumber
248-
, gasPrice :: NullOrUndefined BigNumber
249-
, data :: NullOrUndefined HexString
250-
, nonce :: NullOrUndefined BigNumber
229+
TransactionOptions { from :: Maybe Address
230+
, to :: Maybe Address
231+
, value :: Maybe (Value u)
232+
, gas :: Maybe BigNumber
233+
, gasPrice :: Maybe BigNumber
234+
, data :: Maybe HexString
235+
, nonce :: Maybe BigNumber
251236
}
252237

253238
derive instance genericTransactionOptions :: Generic (TransactionOptions u) _
@@ -262,42 +247,42 @@ instance encodeTransactionOptions :: Encode (TransactionOptions u) where
262247

263248
defaultTransactionOptions :: TransactionOptions NoPay
264249
defaultTransactionOptions =
265-
TransactionOptions { from : NullOrUndefined Nothing
266-
, to : NullOrUndefined Nothing
267-
, value : NullOrUndefined Nothing
268-
, gas : NullOrUndefined Nothing
269-
, gasPrice : NullOrUndefined Nothing
270-
, data : NullOrUndefined Nothing
271-
, nonce : NullOrUndefined Nothing
250+
TransactionOptions { from: Nothing
251+
, to: Nothing
252+
, value: Nothing
253+
, gas: Nothing
254+
, gasPrice: Nothing
255+
, data: Nothing
256+
, nonce: Nothing
272257
}
273258
-- * Lens Boilerplate
274259
_from :: forall u. Lens' (TransactionOptions u) (Maybe Address)
275-
_from = lens (\(TransactionOptions txOpt) -> unNullOrUndefined $ txOpt.from)
276-
(\(TransactionOptions txOpts) addr -> TransactionOptions $ txOpts {from = NullOrUndefined addr})
260+
_from = lens (\(TransactionOptions txOpt) -> txOpt.from)
261+
(\(TransactionOptions txOpts) addr -> TransactionOptions $ txOpts {from = addr})
277262

278263
_to :: forall u. Lens' (TransactionOptions u) (Maybe Address)
279-
_to = lens (\(TransactionOptions txOpt) -> unNullOrUndefined $ txOpt.to)
280-
(\(TransactionOptions txOpts) addr -> TransactionOptions $ txOpts {to = NullOrUndefined addr})
264+
_to = lens (\(TransactionOptions txOpt) -> txOpt.to)
265+
(\(TransactionOptions txOpts) addr -> TransactionOptions $ txOpts {to = addr})
281266

282267
_data :: forall u. Lens' (TransactionOptions u) (Maybe HexString)
283-
_data = lens (\(TransactionOptions txOpt) -> unNullOrUndefined $ txOpt.data)
284-
(\(TransactionOptions txOpts) dat -> TransactionOptions $ txOpts {data = NullOrUndefined dat})
268+
_data = lens (\(TransactionOptions txOpt) -> txOpt.data)
269+
(\(TransactionOptions txOpts) dat -> TransactionOptions $ txOpts {data = dat})
285270

286271
_value :: forall u. EtherUnit (Value u) => Lens (TransactionOptions u) (TransactionOptions Wei) (Maybe (Value u)) (Maybe (Value Wei))
287-
_value = lens (\(TransactionOptions txOpt) -> unNullOrUndefined $ txOpt.value)
288-
(\(TransactionOptions txOpts) val -> TransactionOptions $ txOpts {value = NullOrUndefined $ map convert val})
272+
_value = lens (\(TransactionOptions txOpt) -> txOpt.value)
273+
(\(TransactionOptions txOpts) val -> TransactionOptions $ txOpts {value = map convert val})
289274

290275
_gas :: forall u. Lens' (TransactionOptions u) (Maybe BigNumber)
291-
_gas = lens (\(TransactionOptions txOpt) -> unNullOrUndefined $ txOpt.gas)
292-
(\(TransactionOptions txOpts) g -> TransactionOptions $ txOpts {gas = NullOrUndefined g})
276+
_gas = lens (\(TransactionOptions txOpt) -> txOpt.gas)
277+
(\(TransactionOptions txOpts) g -> TransactionOptions $ txOpts {gas = g})
293278

294279
_gasPrice :: forall u. Lens' (TransactionOptions u) (Maybe BigNumber)
295-
_gasPrice = lens (\(TransactionOptions txOpt) -> unNullOrUndefined $ txOpt.gasPrice)
296-
(\(TransactionOptions txOpts) gp -> TransactionOptions $ txOpts {gasPrice = NullOrUndefined gp})
280+
_gasPrice = lens (\(TransactionOptions txOpt) -> txOpt.gasPrice)
281+
(\(TransactionOptions txOpts) gp -> TransactionOptions $ txOpts {gasPrice = gp})
297282

298283
_nonce :: forall u. Lens' (TransactionOptions u) (Maybe BigNumber)
299-
_nonce = lens (\(TransactionOptions txOpt) -> unNullOrUndefined $ txOpt.nonce)
300-
(\(TransactionOptions txOpts) n -> TransactionOptions $ txOpts {nonce = NullOrUndefined n})
284+
_nonce = lens (\(TransactionOptions txOpt) -> txOpt.nonce)
285+
(\(TransactionOptions txOpts) n -> TransactionOptions $ txOpts {nonce = n})
301286

302287
--------------------------------------------------------------------------------
303288
-- * Node Synchronisation
@@ -395,8 +380,8 @@ forkWeb3' web3Action = do
395380

396381
-- | Low-level event filter data structure
397382
newtype Filter a = Filter
398-
{ address :: NullOrUndefined Address
399-
, topics :: NullOrUndefined (Array (NullOrUndefined HexString))
383+
{ address :: Maybe Address
384+
, topics :: Maybe (Array (Maybe HexString))
400385
, fromBlock :: ChainCursor
401386
, toBlock :: ChainCursor
402387
}
@@ -414,19 +399,19 @@ instance encodeFilter :: Encode (Filter a) where
414399
encode x = genericEncode (defaultOptions { unwrapSingleConstructors = true }) x
415400

416401
defaultFilter :: forall a. Filter a
417-
defaultFilter = Filter { address: NullOrUndefined Nothing
418-
, topics: NullOrUndefined Nothing
402+
defaultFilter = Filter { address: Nothing
403+
, topics: Nothing
419404
, fromBlock: Latest
420405
, toBlock: Latest
421406
}
422407

423408
_address :: forall a. Lens' (Filter a) (Maybe Address)
424-
_address = lens (\(Filter f) -> unNullOrUndefined f.address)
425-
(\(Filter f) addr -> Filter $ f {address = NullOrUndefined addr})
409+
_address = lens (\(Filter f) -> f.address)
410+
(\(Filter f) addr -> Filter $ f {address = addr})
426411

427412
_topics :: forall a. Lens' (Filter a) (Maybe (Array (Maybe HexString)))
428-
_topics = lens (\(Filter f) -> map unNullOrUndefined <$> unNullOrUndefined f.topics)
429-
(\(Filter f) ts -> Filter $ f {topics = NullOrUndefined (map NullOrUndefined <$> ts)})
413+
_topics = lens (\(Filter f) -> f.topics)
414+
(\(Filter f) ts -> Filter $ f {topics = ts})
430415

431416
_fromBlock :: forall a. Lens' (Filter a) ChainCursor
432417
_fromBlock = lens (\(Filter f) -> f.fromBlock)
@@ -437,7 +422,7 @@ _toBlock = lens (\(Filter f) -> f.toBlock)
437422
(\(Filter f) b -> Filter $ f {toBlock = b})
438423

439424
-- | Used by the ethereum client to identify the filter you are querying
440-
newtype FilterId = FilterId HexString
425+
newtype FilterId = FilterId BigNumber
441426

442427
derive instance genericFilterId :: Generic FilterId _
443428

@@ -480,9 +465,10 @@ instance eqEventAction :: Eq EventAction where
480465
-- | Changes pulled by low-level call 'eth_getFilterChanges', 'eth_getLogs',
481466
-- | and 'eth_getFilterLogs'
482467
newtype Change = Change
483-
{ logIndex :: HexString
484-
, transactionIndex :: HexString
468+
{ logIndex :: BigNumber
469+
, transactionIndex :: BigNumber
485470
, transactionHash :: HexString
471+
, removed :: Boolean
486472
, blockHash :: HexString
487473
, blockNumber :: BlockNumber
488474
, address :: Address

0 commit comments

Comments
 (0)