Commit 1145a739 authored by Cédric F.'s avatar Cédric F.

Decode vin and vout for transactions

parent 46d2c864
......@@ -15,6 +15,10 @@ type alias Model =
{ hash : String
, size : Int
, confirmations : Int
, blockHash : Maybe String
, time : Maybe Time
, vIn : List VIn
, vOut : List VOut
, fetching : Bool
, error : Maybe String
}
......@@ -25,6 +29,10 @@ initialModel =
{ hash = ""
, size = -1
, confirmations = -1
, blockHash = Nothing
, time = Nothing
, vIn = []
, vOut = []
, fetching = False
, error = Nothing
}
......@@ -34,6 +42,10 @@ type alias JsonModel =
{ hash : String
, hex : String
, confirmations : Int
, blockHash : Maybe String
, time : Maybe Int
, vIn : List VIn
, vOut : List VOut
}
......@@ -42,11 +54,39 @@ modelFromJson jsonModel =
{ hash = jsonModel.hash
, size = (String.length jsonModel.hex) // 2
, confirmations = jsonModel.confirmations
, blockHash = jsonModel.blockHash
, time = Maybe.map timestampToTime jsonModel.time
, vIn = jsonModel.vIn
, vOut = jsonModel.vOut
, fetching = False
, error = Nothing
}
type alias VIn =
{ txId : String
, amountIn : Float
, blockHeight : Int
}
type alias VOut =
{ value : Float
, scriptPubKey : ScriptPubKey
}
type alias ScriptPubKey =
{ asm : String
, type_ : String
}
timestampToTime : Int -> Time
timestampToTime int =
Time.second * (toFloat int)
type Msg
= GetRawTransaction String
| GetRawTransactionResult (Result Http.Error JsonModel)
......@@ -54,29 +94,73 @@ type Msg
view : Model -> Html a
view model =
div
[ class "card bg-dark" ]
[ h5 [ class "card-header" ] [ text <| "Transaction " ++ model.hash ]
, div [ class "card-body" ]
[ p [ class "card-text" ]
[ dl [ class "row" ]
[ dt [ class "col-3 text-right" ] [ text "confirmations" ]
, dd [ class "col-9" ] [ text <| toString model.confirmations ]
, dt [ class "col-3 text-right" ] [ text "size" ]
, dd [ class "col-9" ] [ text <| toString model.size ++ " bytes" ]
let
formatTime time =
case time of
Nothing ->
span [] [ text "recent (still in mempool)" ]
Just time ->
span [] [ text <| TimeExtra.toISOString time ]
formatBlock block =
case block of
Nothing ->
span [] [ text "N/A (still in mempool)" ]
Just hash ->
a [ href hash ] [ text hash ]
in
div
[ class "card bg-dark" ]
[ h5 [ class "card-header" ] [ text <| "Transaction " ++ model.hash ]
, div [ class "card-body" ]
[ p [ class "card-text" ]
[ dl [ class "row" ]
[ 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" ] [ formatTime model.time ]
, dt [ class "col-3 text-right" ] [ text "block" ]
, dd [ class "col-9" ] [ formatBlock model.blockHash ]
, dt [ class "col-3 text-right" ] [ text "size" ]
, dd [ class "col-9" ] [ text <| toString model.size ++ " bytes" ]
]
]
]
]
, div [ class "card-footer" ]
[ small [ class "text-muted" ]
[ a
[ target "_blank"
, href <| "https://explorer.dcrdata.org/explorer/tx/" ++ model.hash
, hr [] []
, div [ class "row" ]
[ div [ class "col" ]
[ h4 [ class "text-center" ] [ text "inputs" ]
, ul [ class "list-group list-group-flush" ] <|
List.map
(\vIn ->
li [ class "list-group-item bg-primary" ]
[ text <| toString vIn.amountIn ]
)
model.vIn
]
, div [ class "col" ]
[ h4 [ class "text-center" ] [ text "outputs" ]
, ul [ class "list-group list-group-flush" ] <|
List.map
(\vOut ->
li [ class "list-group-item bg-success" ]
[ text <| toString vOut.value ]
)
model.vOut
]
]
, div [ class "card-footer" ]
[ small [ class "text-muted" ]
[ a
[ target "_blank"
, href <| "https://explorer.dcrdata.org/explorer/tx/" ++ model.hash
]
[ text "More details at dcrdata.org" ]
]
[ text "More details at dcrdata.org" ]
]
]
]
update : Msg -> Model -> ( Model, Cmd Msg )
......@@ -117,9 +201,30 @@ decodeGetRawTransaction =
Pipeline.decode JsonModel
|> Pipeline.requiredAt [ "result", "txid" ] Decode.string
|> Pipeline.requiredAt [ "result", "hex" ] Decode.string
|> Pipeline.requiredAt [ "result", "confirmations" ] Decode.int
|> Pipeline.optionalAt [ "result", "confirmations" ] Decode.int 0
|> Pipeline.optionalAt [ "result", "blockhash" ] (Decode.maybe Decode.string) Nothing
|> Pipeline.optionalAt [ "result", "time" ] (Decode.maybe Decode.int) Nothing
|> Pipeline.requiredAt [ "result", "vin" ] (Decode.list decodeVIn)
|> Pipeline.requiredAt [ "result", "vout" ] (Decode.list decodeVOut)
decodeVIn : Decode.Decoder VIn
decodeVIn =
Pipeline.decode VIn
|> Pipeline.requiredAt [ "txid" ] Decode.string
|> Pipeline.requiredAt [ "amountin" ] Decode.float
|> Pipeline.requiredAt [ "blockheight" ] Decode.int
decodeVOut : Decode.Decoder VOut
decodeVOut =
Pipeline.decode VOut
|> Pipeline.requiredAt [ "value" ] Decode.float
|> Pipeline.requiredAt [ "scriptPubKey" ] decodeScriptPubKey
-- |> Pipeline.optionalAt [ "result", "blockhash" ] Decode.string
-- |> Pipeline.optionalAt [ "result", "time" ] Decode.int
decodeScriptPubKey : Decode.Decoder ScriptPubKey
decodeScriptPubKey =
Pipeline.decode ScriptPubKey
|> Pipeline.requiredAt [ "asm" ] Decode.string
|> Pipeline.requiredAt [ "type" ] Decode.string
......@@ -29,7 +29,7 @@ decodeError json =
result
Err err ->
"Cannot decode error: " ++ err
"Cannot decode error: " ++ err ++ " | " ++ json
parseError : Http.Error -> Maybe String
......
......@@ -27,3 +27,4 @@ toISOString time =
++ (String.padLeft 2 '0' (toString minute))
++ ":"
++ (String.padLeft 2 '0' (toString second))
++ " UTC"
This diff is collapsed.
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