Commit fed352ee authored by Cédric F.'s avatar Cédric F.

Pass coin to models to format views accordingly

parent 3b1b864c
......@@ -24,6 +24,12 @@
<script src="assets/js/elm.min.js" async defer></script>
<script>
// CONFIGURE ME:
const coin = 'BCH';
// TODO: use fuzzy search to guess coin based on hostname?
</script>
<style>
/* inconsolata-regular - latin */
@font-face {
......@@ -58,10 +64,6 @@
background: orange;
box-shadow: 0 0 10px 5px orange;
}
.amount::after {
content: " DCR";
}
</style>
</head>
<body class="bg-black text-white">
......@@ -72,6 +74,13 @@
};
</script>
<script>
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = '.amount::after { content: " ' + coin + '" };';
document.getElementsByTagName('head')[0].appendChild(style);
</script>
<div class="container-fluid">
<div class="row" style="position:absolute;width:100%;height:100%">
<div class="col">
......@@ -95,7 +104,7 @@
window.addEventListener('load', () => {
const elmAppDiv = document.querySelector('#elm-app');
const elmApp = Elm.Trappisto.embed(elmAppDiv, { coin: 'BTC' });
const elmApp = Elm.Trappisto.embed(elmAppDiv, { coin: coin });
elmApp.ports.elmToJs.subscribe((command) => {
console.log('Received command: ' + command);
......
......@@ -16,26 +16,29 @@ type alias Model =
, tickets : List String
, fetching : Bool
, error : Maybe String
, coin : Coin
}
initialModel : Model
initialModel =
initialModel : Coin -> Model
initialModel coin =
{ address = ""
, transactions = []
, tickets = []
, fetching = False
, error = Nothing
, coin = coin
}
modelFromJson : JsonModel -> Model
modelFromJson jsonModel =
modelFromJson : JsonModel -> Coin -> Model
modelFromJson jsonModel coin =
{ address = jsonModel.address
, transactions = List.map Transaction.modelFromJson jsonModel.transactions
, transactions = List.map (\tx -> Transaction.modelFromJson tx coin) jsonModel.transactions
, tickets = []
, fetching = False
, error = Nothing
, coin = coin
}
......@@ -144,7 +147,7 @@ update msg model =
SearchRawTransactionsResult result ->
case result of
Ok jsonModel ->
( modelFromJson jsonModel, Cmd.none )
( modelFromJson jsonModel model.coin, Cmd.none )
Err error ->
( { model
......
......@@ -26,11 +26,12 @@ type alias Model =
, nextBlockHash : Maybe String
, fetching : Bool
, error : Maybe String
, coin : Coin
}
initialModel : Model
initialModel =
initialModel : Coin -> Model
initialModel coin =
{ hash = ""
, height = -1
, time = -1
......@@ -43,6 +44,25 @@ initialModel =
, nextBlockHash = Nothing
, fetching = False
, error = Nothing
, coin = coin
}
modelFromJson : JsonModel -> Coin -> Model
modelFromJson jsonModel coin =
{ height = jsonModel.height
, hash = jsonModel.hash
, time = Time.second * (toFloat jsonModel.time)
, confirmations = jsonModel.confirmations
, size = jsonModel.size
, ticketPrice = jsonModel.ticketPrice
, transactions = jsonModel.transactions
, tickets = jsonModel.tickets
, previousBlockHash = zeroesToNothing jsonModel.previousBlockHash
, nextBlockHash = jsonModel.nextBlockHash
, fetching = False
, error = Nothing
, coin = coin
}
......@@ -70,45 +90,52 @@ type Msg
view : Model -> Html Msg
view model =
let
transactions =
[ dt [ class "col-3 text-right" ] [ text "stake transactions" ]
, dd [ class "col-9" ]
((span [ class "mr-2" ] [ text (toString <| List.length model.tickets) ])
:: (List.map
(\tx ->
sibbling maybeHash html =
[ case maybeHash of
Nothing ->
span [] []
Just hash ->
a
[ href tx, class "badge badge-secondary ml-1" ]
[ text <| shortHash tx ]
)
model.tickets
)
)
, dt [ class "col-3 text-right" ] [ text "normal transactions" ]
, dd [ class "col-9" ]
((span [ class "mr-2" ] [ text (toString <| List.length model.transactions) ])
[ class "btn btn-secondary"
, href "javascript:void(0)"
, onClick (GetBlock hash)
]
[ text html ]
]
transactions list color =
case list of
[] ->
Nothing
list ->
div []
((span [ class "mr-2" ] [ text (toString <| List.length list) ])
:: (List.map
(\tx ->
a
[ href tx, class "badge badge-light ml-1" ]
[ href tx, class <| "ml-1 badge badge-" ++ color ]
[ text <| shortHash tx ]
)
model.transactions
list
)
)
|> Just
allTransactions =
case model.coin of
DCR ->
[ ( "stake transactions", transactions model.tickets "secondary" )
, ( "normal transactions", transactions model.transactions "light" )
]
_ ->
[ ( "transactions", transactions model.transactions "light" ) ]
in
div [ class "row align-items-center" ]
[ div [ class "col-2 text-right" ]
[ a
[ class "btn btn-secondary"
, href "javascript:void(0)"
-- FIXME: prevblock
, onClick (GetBlock "000000000000006fa9bf10d4a39d0cbc6ef8dc2f2db3e7841fd33b7ad973811a")
]
[ text "<" ]
]
, div [ class "col-8" ]
[ div [ class "col-1 text-right" ] (sibbling model.previousBlockHash "<")
, div [ class "col-10" ]
[ div
[ class "card bg-dark" ]
[ h5 [ class "card-header" ]
......@@ -118,10 +145,11 @@ view model =
, div [ class "card-body" ]
[ p [ class "card-text" ]
[ dlBuilder <|
[ ( "height"
List.concat
[ [ ( "height"
, Just <|
span []
[ text <| toString model.height ]
[ text <| formatNumber model.height ]
)
, ( "time"
, Just <|
......@@ -136,14 +164,17 @@ view model =
, ( "size"
, Just <|
span []
[ text <| toString model.size ++ " bytes" ]
[ text <| formatNumber model.size ++ " bytes" ]
)
, ( "ticket price", Maybe.map formatAmount model.ticketPrice )
]
, allTransactions
]
]
]
]
]
, div [ class "col-1 text-left" ] (sibbling model.nextBlockHash ">")
]
......@@ -167,26 +198,7 @@ update msg model =
GetBlockResult result ->
case result of
Ok jsonModel ->
let
previousBlockHash =
zeroesToNothing jsonModel.previousBlockHash
in
( { model
| height = jsonModel.height
, hash = jsonModel.hash
, time = Time.second * (toFloat jsonModel.time)
, confirmations = jsonModel.confirmations
, size = jsonModel.size
, ticketPrice = jsonModel.ticketPrice
, transactions = jsonModel.transactions
, tickets = jsonModel.tickets
, previousBlockHash = previousBlockHash
, nextBlockHash = jsonModel.nextBlockHash
, fetching = False
, error = Nothing
}
, Cmd.none
)
( modelFromJson jsonModel model.coin, Cmd.none )
Err error ->
( { model
......
......@@ -25,11 +25,12 @@ type alias Model =
, vOut : List VOut
, fetching : Bool
, error : Maybe String
, coin : Coin
}
initialModel : Model
initialModel =
initialModel : Coin -> Model
initialModel coin =
{ hash = ""
, type_ = "?"
, size = -1
......@@ -41,11 +42,12 @@ initialModel =
, vOut = []
, fetching = False
, error = Nothing
, coin = coin
}
modelFromJson : JsonModel -> Model
modelFromJson jsonModel =
modelFromJson : JsonModel -> Coin -> Model
modelFromJson jsonModel coin =
{ hash = jsonModel.hash
, type_ = computeType jsonModel
, size = computeSize jsonModel
......@@ -57,6 +59,7 @@ modelFromJson jsonModel =
, vOut = List.sortBy .value jsonModel.vOut |> List.reverse
, fetching = False
, error = Nothing
, coin = coin
}
......@@ -110,7 +113,7 @@ view model =
span [] [ text "N/A (unconfirmed)" ]
( Just hash, Nothing ) ->
a [ href hash ] [ text hash ]
a [ href hash ] [ text <| shortHash hash ]
( Nothing, Just height ) ->
a [ href <| toString height ] [ text <| toString height ]
......@@ -118,6 +121,24 @@ view model =
( Just hash, Just height ) ->
a [ href hash ] [ text <| toString height ]
formatFees model =
case model.coin of
DCR ->
[ ( "total sent", Just <| formatAmount (totalSent model) )
, ( "fee"
, Just <|
span []
[ formatAmount (fee model)
, span [] [ text " (" ]
, formatAmount (feePerKb model)
, span [] [ text "/kB)" ]
]
)
]
_ ->
[]
formatAddresses scriptPubKey =
div [] <|
(List.map
......@@ -140,7 +161,14 @@ view model =
"stakebase"
)
]
, span [ class "float-right" ] [ text <| toString vIn.amountIn ]
, span [ class "float-right" ]
[ text <|
(if vIn.amountIn > 0 then
toString vIn.amountIn
else
""
)
]
, div []
[ (case vIn.txId of
Just hash ->
......@@ -168,7 +196,7 @@ view model =
)
in
div [ class "row" ]
[ div [ class "col-8 offset-2" ]
[ div [ class "col-12 col-xl-10 offset-xl-1" ]
[ div
[ class "card bg-dark" ]
[ h5 [ class "card-header" ]
......@@ -177,32 +205,25 @@ view model =
]
, div [ class "card-body" ]
[ p [ class "card-text" ]
[ dl [ class "row" ]
[ dt [ class "col-3 text-right" ] [ text "type" ]
, dd [ class "col-9" ]
[ formatType model.type_ ]
, dt [ class "col-3 text-right" ] [ text "confirmations" ]
, dd [ class "col-9" ]
[ text <| toString model.confirmations ]
, dt [ class "col-3 text-right" ] [ text "time" ]
, dd [ class "col-9" ]
[ text <| formatTime model ]
, dt [ class "col-3 text-right" ] [ text "block" ]
, dd [ class "col-9" ]
[ formatBlock model.blockHash model.blockHeight ]
, dt [ class "col-3 text-right" ] [ text "size" ]
, dd [ class "col-9" ]
[ text <| toString model.size ++ " bytes" ]
, dt [ class "col-3 text-right" ] [ text "total sent" ]
, dd [ class "col-9" ]
[ formatAmount (totalSent model) ]
, dt [ class "col-3 text-right" ] [ text "fee" ]
, dd [ class "col-9" ]
[ formatAmount (fee model)
, span [] [ text " (" ]
, formatAmount (feePerKb model)
, span [] [ text "/kB)" ]
[ dlBuilder <|
List.concat
[ [ ( "type"
, Just <| formatType model.type_
)
, ( "confirmations"
, Just <| span [] [ text <| toString model.confirmations ]
)
, ( "time"
, Just <| span [] [ text <| formatTime model ]
)
, ( "block"
, Just <| formatBlock model.blockHash model.blockHeight
)
, ( "size"
, Just <| span [] [ text <| (formatNumber model.size) ++ " bytes" ]
)
]
, formatFees model
]
]
, hr [] []
......@@ -243,7 +264,7 @@ update msg model =
GetRawTransactionResult result ->
case result of
Ok jsonModel ->
( modelFromJson jsonModel, Cmd.none )
( modelFromJson jsonModel model.coin, Cmd.none )
Err error ->
( { model
......@@ -296,7 +317,7 @@ decodeVIn =
)
Nothing
|> Pipeline.optionalAt [ "coinbase" ] (Decode.maybe Decode.string) Nothing
|> Pipeline.optionalAt [ "amountin" ] Decode.float 0
|> Pipeline.optionalAt [ "amountin" ] Decode.float -1
|> Pipeline.optionalAt [ "blockheight" ] (Decode.maybe Decode.int) Nothing
......
......@@ -6,7 +6,7 @@ import Trappisto.Update exposing (init, update, subscriptions)
import Trappisto.View exposing (view)
main : Program Trappisto.Model.Config Trappisto.Model.Model Trappisto.Model.Msg
main : Program Trappisto.Model.Flags Trappisto.Model.Model Trappisto.Model.Msg
main =
Navigation.programWithFlags Trappisto.Model.NewUrl
{ init = init
......
......@@ -4,6 +4,12 @@ import Html exposing (..)
import Html.Attributes exposing (..)
type Coin
= BCH
| BTC
| DCR
pluralize : Int -> String -> String
pluralize count singular =
let
......@@ -27,6 +33,21 @@ formatAmount float =
[ text <| toString rounded ]
formatNumber : Int -> String
formatNumber int =
let
split digits =
if String.length digits > 3 then
digits
|> String.dropRight 3
|> split
|> (::) (String.right 3 digits)
else
[ digits ]
in
toString int |> split |> List.reverse |> String.join ","
shortHash : String -> String
shortHash hash =
String.concat [ String.left 2 hash, "...", String.right 2 hash ]
......
......@@ -8,12 +8,17 @@ import Components.Status as StatusComponent
import Components.Address as AddressComponent
import Components.Block as BlockComponent
import Components.Transaction as TransactionComponent
import Trappisto.Helpers exposing (Coin)
type alias Config =
type alias Flags =
{ coin : String }
type alias Config =
{ coin : Coin }
type Template
= Status
| Address
......@@ -38,16 +43,16 @@ type alias Model =
}
initialModel : Model
initialModel =
{ config = Config "BTC"
initialModel : Coin -> String -> Model
initialModel coin query =
{ config = Config coin
, keys = Keys False False False False False False
, window = Window.Size 0 0
, statusModel = StatusComponent.initialModel
, addressModel = AddressComponent.initialModel
, blockModel = BlockComponent.initialModel
, transactionModel = TransactionComponent.initialModel
, query = ""
, addressModel = AddressComponent.initialModel coin
, blockModel = BlockComponent.initialModel coin
, transactionModel = TransactionComponent.initialModel coin
, query = query
, template = Status
, error = Nothing
, vimMode = False
......
......@@ -10,6 +10,7 @@ import Components.Status as StatusComponent
import Components.Address as AddressComponent
import Components.Block as BlockComponent
import Components.Transaction as TransactionComponent
import Trappisto.Helpers as Coin exposing (Coin)
port elmToJs : String -> Cmd msg
......@@ -18,14 +19,31 @@ port elmToJs : String -> Cmd msg
port jsToElm : (String -> msg) -> Sub msg
init : Config -> Navigation.Location -> ( Model, Cmd Msg )
init config location =
init : Flags -> Navigation.Location -> ( Model, Cmd Msg )
init flags location =
let
query =
extractQuery location
coin =
case flags.coin of
"BCH" ->
Coin.BCH
"BTC" ->
Coin.BTC
"DCR" ->
Coin.DCR
_ ->
Debug.crash <|
"Invalid coin: "
++ flags.coin
++ " (valid coins are BCH, BTC and DCR)"
model =
{ initialModel | config = config, query = query }
initialModel coin query
( updatedModel, msg ) =
if String.isEmpty query then
......@@ -82,7 +100,7 @@ update action model =
"298e5cc3d985bfe7f81dc135f360abe089edd4396b86d2de66b0cef42b21d980"
possibleAddress query =
String.length query /= 64 && String.left 1 query == "D"
String.length query /= 64
-- XXX: Remove a few zeros in the future... 00000000
possibleBlockHash query =
......@@ -169,20 +187,18 @@ update action model =
( updatedModel, cmd ) =
BlockComponent.update blockMsg model.blockModel
query =
case blockMsg of
BlockComponent.GetBlock foo ->
updatedModel.hash
BlockComponent.GetBlockHash foo ->
toString updatedModel.height
_ ->
model.query
-- query =
-- case blockMsg of
-- BlockComponent.GetBlock foo ->
-- updatedModel.hash
-- BlockComponent.GetBlockHash foo ->
-- toString updatedModel.height
-- _ ->
-- model.query
in
( { model
| template = Block
, query = query
, query = updatedModel.hash
, blockModel = updatedModel
}
, Cmd.map BlockMsg cmd
......
......@@ -125,7 +125,7 @@ view model =
[ pre [ id "logo", class glow ]
[ text <|
if model.query /= "" then
"Trappisto - Decred Interactive Block Explorer"
"Trappisto - " ++ (toString model.config.coin) ++ " Interactive Block Explorer"
else
" ______ _______ _______ _______ _______ ______ \n( __ \\ ( ____ \\( ____ \\( ____ )( ____ \\( __ \\ \n| ( \\ )| ( \\/| ( \\/| ( )|| ( \\/| ( \\ )\n| | ) || (__ | | | (____)|| (__ | | ) |\n| | | || __) | | | __)| __) | | | |\n| | ) || ( | | | (\\ ( | ( | | ) |\n| (__/ )| (____/\\| (____/\\| ) \\ \\__| (____/\\| (__/ )\n(______/ (_______/(_______/|/ \\__/(_______/(______/ \n"
]
......
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