Commit 15cd7c82 authored by Cédric F.'s avatar Cédric F.

Show newest transactions on home page

parent 7aaf1da5
......@@ -43,6 +43,7 @@ type alias Model =
, lastWebSocketPong : Time
, lastBlockHash : String
, lastBlockHeight : Int
, lastTransactions : List BasicTransaction
, fetching : Bool
, webSocketConnected : Bool
}
......@@ -66,6 +67,7 @@ initialModel coin wsEndpoint query =
, lastWebSocketPong = -1
, lastBlockHash = ""
, lastBlockHeight = -1
, lastTransactions = []
, fetching = False
, webSocketConnected = False
}
......@@ -87,7 +89,15 @@ type Msg
type alias BestBlock =
{ hash : String, height : Int }
{ hash : String
, height : Int
}
type alias BasicTransaction =
{ hash : String
, amount : Float
}
type alias Keys =
......
......@@ -121,10 +121,25 @@ update action model =
connected =
Lib.WebSocket.isSuccess message
txAccepted =
if Lib.WebSocket.isMethod [ "txaccepted" ] message then
decodeTxAccepted message
else
Nothing
newLastTransactions =
case txAccepted of
Nothing ->
model.lastTransactions
Just tx ->
List.take 15 (tx :: model.lastTransactions)
updatedModel =
{ model
| webSocketConnected = connected
, lastWebSocketPong = model.now
, lastTransactions = newLastTransactions
}
cmd =
......@@ -440,3 +455,14 @@ decodeGetBestBlock =
Decode.map2 BestBlock
(Decode.at [ "result", "hash" ] Decode.string)
(Decode.at [ "result", "height" ] Decode.int)
decodeTxAccepted : String -> Maybe BasicTransaction
decodeTxAccepted json =
let
decodeParams =
Decode.map2 BasicTransaction
(Decode.index 0 Decode.string)
(Decode.index 1 Decode.float)
in
Decode.decodeString (Decode.field "params" decodeParams) json |> Result.toMaybe
......@@ -94,7 +94,7 @@ searchView model =
div [ class "row" ]
[ div
[ class "col text-center" ]
[ a [ href "/" ]
[ a [ href "javascript:query('')" ]
[ img
[ class
(if isFetching model then
......@@ -177,6 +177,45 @@ view model =
]
]
lastTransactions =
if List.isEmpty model.lastTransactions then
span [] []
else
div [ class "row text-center mt-3" ]
[ div [ class "col" ] <|
List.concat
[ [ hr [] []
, h3 []
[ span [ class "badge badge-pill badge-info" ]
[ text "Latest transactions" ]
]
]
, List.map formatTransaction model.lastTransactions
]
]
formatTransaction tx =
let
size =
toString (Basics.min 9 (logBase 10 tx.amount * 4)) ++ "rem"
fontSize =
toString (Basics.min 3 (logBase 10 tx.amount * 1.5)) ++ "rem"
amount =
toString (round tx.amount) ++ "<br>DCR"
in
div
[ class "badge badge-pill badge-info m-2"
, style
[ ( "line-height", fontSize )
, ( "font-size", fontSize )
, ( "height", size )
, ( "width", size )
]
]
[ queryLink tx.hash amount [] ]
content =
if model.error /= Nothing then
[ searchView model, errorView model ]
......@@ -185,7 +224,7 @@ view model =
Home ->
case model.query of
"" ->
[ searchView model, errorView model, ascii ]
[ searchView model, errorView model, ascii, lastTransactions ]
"particles" ->
[ div [ class "row" ]
......
......@@ -9,6 +9,8 @@ import Lib.WebSocket as WebSocket
import Components.Address as Address
import Components.Block as Block
import Components.Transaction as Transaction
import Trappisto.Model exposing (..)
import Trappisto.Update
import Trappisto.Helpers as Coin exposing (Coin)
......@@ -107,7 +109,7 @@ suite =
List.map (\fixture -> WebSocket.isSuccess fixture)
[ connectFixture, sessionFixture ]
|> Expect.equal [ True, True ]
, test "isMethod" <|
, test "isMethod with blockconnected" <|
\() ->
let
blockConnectedFixture =
......@@ -117,6 +119,37 @@ suite =
WebSocket.isMethod [ "blockconnected", "blockdisconnected" ] blockConnectedFixture
in
Expect.equal result True
, test "isMethod with txaccepted" <|
\() ->
let
txAcceptedFixture =
"{\"jsonrpc\":\"1.0\",\"method\":\"txaccepted\",\"params\":[\"e943704526165772229307a3e2406f5805160a9a0d33d702691044daef0ecbb2\",72.31055621],\"id\":null}"
result =
WebSocket.isMethod [ "txaccepted" ] txAcceptedFixture
in
Expect.equal result True
, test "decodeTxAccepted" <|
\() ->
let
txAcceptedFixture =
"{\"jsonrpc\":\"1.0\",\"method\":\"txaccepted\",\"params\":[\"e943704526165772229307a3e2406f5805160a9a0d33d702691044daef0ecbb2\",72.31055621],\"id\":null}"
result =
Trappisto.Update.decodeTxAccepted txAcceptedFixture
model =
case result of
Ok model ->
model
Err error ->
Debug.crash error
in
Expect.equal model <|
BasicTransaction
"e943704526165772229307a3e2406f5805160a9a0d33d702691044daef0ecbb2"
72.31055621
]
, describe "Fuzz test examples, using randomly generated input"
-- XXX: keping them as examples
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment