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 = ...@@ -43,6 +43,7 @@ type alias Model =
, lastWebSocketPong : Time , lastWebSocketPong : Time
, lastBlockHash : String , lastBlockHash : String
, lastBlockHeight : Int , lastBlockHeight : Int
, lastTransactions : List BasicTransaction
, fetching : Bool , fetching : Bool
, webSocketConnected : Bool , webSocketConnected : Bool
} }
...@@ -66,6 +67,7 @@ initialModel coin wsEndpoint query = ...@@ -66,6 +67,7 @@ initialModel coin wsEndpoint query =
, lastWebSocketPong = -1 , lastWebSocketPong = -1
, lastBlockHash = "" , lastBlockHash = ""
, lastBlockHeight = -1 , lastBlockHeight = -1
, lastTransactions = []
, fetching = False , fetching = False
, webSocketConnected = False , webSocketConnected = False
} }
...@@ -87,7 +89,15 @@ type Msg ...@@ -87,7 +89,15 @@ type Msg
type alias BestBlock = type alias BestBlock =
{ hash : String, height : Int } { hash : String
, height : Int
}
type alias BasicTransaction =
{ hash : String
, amount : Float
}
type alias Keys = type alias Keys =
......
...@@ -121,10 +121,25 @@ update action model = ...@@ -121,10 +121,25 @@ update action model =
connected = connected =
Lib.WebSocket.isSuccess message 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 = updatedModel =
{ model { model
| webSocketConnected = connected | webSocketConnected = connected
, lastWebSocketPong = model.now , lastWebSocketPong = model.now
, lastTransactions = newLastTransactions
} }
cmd = cmd =
...@@ -440,3 +455,14 @@ decodeGetBestBlock = ...@@ -440,3 +455,14 @@ decodeGetBestBlock =
Decode.map2 BestBlock Decode.map2 BestBlock
(Decode.at [ "result", "hash" ] Decode.string) (Decode.at [ "result", "hash" ] Decode.string)
(Decode.at [ "result", "height" ] Decode.int) (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 = ...@@ -94,7 +94,7 @@ searchView model =
div [ class "row" ] div [ class "row" ]
[ div [ div
[ class "col text-center" ] [ class "col text-center" ]
[ a [ href "/" ] [ a [ href "javascript:query('')" ]
[ img [ img
[ class [ class
(if isFetching model then (if isFetching model then
...@@ -177,6 +177,45 @@ view model = ...@@ -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 = content =
if model.error /= Nothing then if model.error /= Nothing then
[ searchView model, errorView model ] [ searchView model, errorView model ]
...@@ -185,7 +224,7 @@ view model = ...@@ -185,7 +224,7 @@ view model =
Home -> Home ->
case model.query of case model.query of
"" -> "" ->
[ searchView model, errorView model, ascii ] [ searchView model, errorView model, ascii, lastTransactions ]
"particles" -> "particles" ->
[ div [ class "row" ] [ div [ class "row" ]
......
...@@ -9,6 +9,8 @@ import Lib.WebSocket as WebSocket ...@@ -9,6 +9,8 @@ import Lib.WebSocket as WebSocket
import Components.Address as Address import Components.Address as Address
import Components.Block as Block import Components.Block as Block
import Components.Transaction as Transaction import Components.Transaction as Transaction
import Trappisto.Model exposing (..)
import Trappisto.Update
import Trappisto.Helpers as Coin exposing (Coin) import Trappisto.Helpers as Coin exposing (Coin)
...@@ -107,7 +109,7 @@ suite = ...@@ -107,7 +109,7 @@ suite =
List.map (\fixture -> WebSocket.isSuccess fixture) List.map (\fixture -> WebSocket.isSuccess fixture)
[ connectFixture, sessionFixture ] [ connectFixture, sessionFixture ]
|> Expect.equal [ True, True ] |> Expect.equal [ True, True ]
, test "isMethod" <| , test "isMethod with blockconnected" <|
\() -> \() ->
let let
blockConnectedFixture = blockConnectedFixture =
...@@ -117,6 +119,37 @@ suite = ...@@ -117,6 +119,37 @@ suite =
WebSocket.isMethod [ "blockconnected", "blockdisconnected" ] blockConnectedFixture WebSocket.isMethod [ "blockconnected", "blockdisconnected" ] blockConnectedFixture
in in
Expect.equal result True 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" , describe "Fuzz test examples, using randomly generated input"
-- XXX: keping them as examples -- 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