This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.

ElmArk.Block

Handles building requests for the block endpoints and encoding/decoding their json.

Models

type alias BlockResponse = { success : Bool , block : Block }

Record used to represent the response from getting a block Sample JSON:

{
    "success": true,
    "block": {
        "id": "16338263029223576159",
        "version": 0,
        "timestamp": 23958872,
        "height": 2953518,
        "previousBlock": "7539730724110889322",
        "numberOfTransactions": 1,
        "totalAmount": 100000000,
        "totalFee": 10000000,
        "reward": 200000000,
        "payloadLength": 32,
        "payloadHash": "e380f252e9d92c9085d6cfc6d14b7adbd43fc38f1036bd85f68565e7a912c8c6",
        "generatorPublicKey": "02a0ed5604868461a87639f58bd3a55f661774c3cbb705a735f58c50087f942c3d",
        "generatorId": "AHazk56nvQ3isZrYkarJV1RPcDUrQocmfR",
        "blockSignature": "3045022100fdd83eb7be8238837841375e7730109600223e1735a29ec1db9bf5763a3d2c9d0220402351074b1a24b5bf640617b07c8e8cfaa72896f2438bb44235537af06730ed",
        "confirmations": 43,
        "totalForged": "210000000"
    }
}
type alias Block = { id : String , version : Int , timestamp : Int , height : Int , previousBlock : String , numberOfTransactions : Int , totalAmount : Int , totalFee : Int , reward : Int , payloadLength : Int , payloadHash : String , generatorPublicKey : String , generatorId : String , blockSignature : String , confirmations : Int , totalForged : String }

Record for an Ark blockchain block Sample JSON:

{
  "id": "16338263029223576159",
  "version": 0,
  "timestamp": 23958872,
  "height": 2953518,
  "previousBlock": "7539730724110889322",
  "numberOfTransactions": 1,
  "totalAmount": 100000000,
  "totalFee": 10000000,
  "reward": 200000000,
  "payloadLength": 32,
  "payloadHash": "e380f252e9d92c9085d6cfc6d14b7adbd43fc38f1036bd85f68565e7a912c8c6",
  "generatorPublicKey": "02a0ed5604868461a87639f58bd3a55f661774c3cbb705a735f58c50087f942c3d",
  "generatorId": "AHazk56nvQ3isZrYkarJV1RPcDUrQocmfR",
  "blockSignature": "3045022100fdd83eb7be8238837841375e7730109600223e1735a29ec1db9bf5763a3d2c9d0220402351074b1a24b5bf640617b07c8e8cfaa72896f2438bb44235537af06730ed",
  "confirmations": 43,
  "totalForged": "210000000"
}
type alias BlockSearch = { success : Bool , blocks : List Block , count : Int }

Result of searching blocks

type alias Epoch = { success : Bool , epoch : String }

Result of getting the epoch Sample JSON

{
    "success": true,
    "epoch": "2017-03-21T13:00:00.000Z"
}
type alias BlockHeight = { success : Bool , height : Int , id : String }

The current block height

type alias Nethash = { success : Bool , nethash : String }

Represents a netash Sample JSON:

{
    "success": true,
    "nethash": "6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988"
}
type alias Fee = { success : Bool , fee : Int }

Represents the current fees for transactions Sample JSON:

{
    "success": true,
    "fee": 10000000
}
type alias FeesResponse = { success : Bool , fees : Fees }

Object representing the current fees response Sample JSON:

{
    "success": true,
    "fees": {
        "send": 10000000,
        "vote": 100000000,
        "secondsignature": 500000000,
        "delegate": 2500000000,
        "multisignature": 500000000
    }
}
type alias Fees = { send : Int , vote : Int , secondsignature : Int , delegate : Int , multisignature : Int }

Object representing the current fees Sample JSON:

{
  "send": 10000000,
  "vote": 100000000,
  "secondsignature": 500000000,
  "delegate": 2500000000,
  "multisignature": 500000000
}
type alias Milestone = { success : Bool , milestone : Int }

Milestone record Sample JSON:

{
    "success": true,
    "milestone": 0
}
type alias Reward = { success : Bool , reward : Int }

Current block reward Sample JSON:

{
    "success": true,
    "reward": 200000000
}
type alias Supply = { success : Bool , supply : Int }

Record for the current supply Sample JSON:

{
    "success": true,
    "supply": 13090826000000000
}
type alias Status = { success : Bool , epoch : String , height : Int , fee : Int , milestone : Int , nethash : String , reward : Int , supply : Int }

Status of the blockchain Sample JSON:

{
    "success": true,
    "epoch": "2017-03-21T13:00:00.000Z",
    "height": 2954161,
    "fee": 10000000,
    "milestone": 0,
    "nethash": "6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988",
    "reward": 200000000,
    "supply": 13090832200000000
}

Requests

getBlock : String -> RequestHead -> ( RequestHead, RequestBody )

Get info about a block

mainNet
    |> getBlock "16338263029223576159"
searchBlocks : List ( String, String ) -> RequestHead -> ( RequestHead, RequestBody )

Search all blocksblocks.

Available search params: limit, orderBy, offset, generatorPublicKey, totalAmount, totalFee, reward, previousBlock, height

mainNet
    |> searchBlocks [("orderBy", "timestamp"), ("limit", "2")]
getEpoch : RequestHead -> ( RequestHead, RequestBody )

Gets the epoch

mainNet
    |> getEpoch
getBlockHeight : RequestHead -> ( RequestHead, RequestBody )

Gets the current block height

mainNet
    |> getBlockHeight
getNethash : RequestHead -> ( RequestHead, RequestBody )

Gets the current netHash

mainNet
    |> getNethash
getFee : RequestHead -> ( RequestHead, RequestBody )

Gets the current fees for a transaction

mainNet
    |> getFee
getFees : RequestHead -> ( RequestHead, RequestBody )

Get the current fees

mainNet
    |> getFees
getMilestone : RequestHead -> ( RequestHead, RequestBody )

Gets the current milestone

mainNet
    |> getMilestone
getReward : RequestHead -> ( RequestHead, RequestBody )

Gets the current block reward

mainNet
    |> getReward
getSupply : RequestHead -> ( RequestHead, RequestBody )

Gets the current block supply

mainNet
    |> getSupply
getStatus : RequestHead -> ( RequestHead, RequestBody )

Gets the current block status

mainNet
    |> getStatus

JSON

decodeBlockResponse : Json.Decode.Decoder BlockResponse

Parses JSON from getting block information

encodeBlockResponse : BlockResponse -> Json.Encode.Value

Turns BlockResponse record into JSON string

decodeBlock : Json.Decode.Decoder Block

Parses JSON for a single block

encodeBlock : Block -> Json.Encode.Value

Turns Block record into JSON string

decodeBlockSearch : Json.Decode.Decoder BlockSearch

Parses JSON into BlockSearch record

encodeBlockSearch : BlockSearch -> Json.Encode.Value

Turns BlockSearch record into JSON string

decodeEpoch : Json.Decode.Decoder Epoch

Parses JSON into Epoch record

encodeEpoch : Epoch -> Json.Encode.Value

Turns Epoch record into JSON string

decodeBlockHeight : Json.Decode.Decoder BlockHeight

Parses JSON into BlockHeight record

encodeBlockHeight : BlockHeight -> Json.Encode.Value

Turns BlockHeight record into JSON

decodeNethash : Json.Decode.Decoder Nethash

Parses JSON into Nethash struct

encodeNethash : Nethash -> Json.Encode.Value

Turns Nethash into JSON string

decodeFee : Json.Decode.Decoder Fee

Parses JSON into a Fee record

encodeFee : Fee -> Json.Encode.Value

Turns Fee record into JSON

decodeFeesResponse : Json.Decode.Decoder FeesResponse

Turns JSON into FeesResponse

encodeFeesResponse : FeesResponse -> Json.Encode.Value

Turns FeesResponse into JSON

decodeFees : Json.Decode.Decoder Fees

Turns JSON into Fees

encodeFees : Fees -> Json.Encode.Value

Turns Fees into JSON

decodeMilestone : Json.Decode.Decoder Milestone

Parses JSON into Milestone record

encodeMilestone : Milestone -> Json.Encode.Value

Turns Milestone record into JSON

decodeReward : Json.Decode.Decoder Reward

Parses JSON into Reward

encodeReward : Reward -> Json.Encode.Value

Turns Reward record into JSON

decodeSupply : Json.Decode.Decoder Supply

Parse JSON into Supply

encodeSupply : Supply -> Json.Encode.Value

Turn Supply into JSON string

decodeStatus : Json.Decode.Decoder Status

Turns JSON into Status record

encodeStatus : Status -> Json.Encode.Value

Turns Status record into JSON string

module ElmArk.Block exposing (..)

{-| Handles building requests for the block endpoints
and encoding/decoding their json.


# Models

@docs BlockResponse, Block, BlockSearch, Epoch, BlockHeight, Nethash, Fee, FeesResponse, Fees, Milestone, Reward, Supply, Status


# Requests

@docs getBlock, searchBlocks, getEpoch, getBlockHeight, getNethash, getFee, getFees, getMilestone, getReward, getSupply, getStatus


# JSON

@docs decodeBlockResponse, encodeBlockResponse, decodeBlock, encodeBlock, decodeBlockSearch, encodeBlockSearch, decodeEpoch, encodeEpoch, decodeBlockHeight, encodeBlockHeight, decodeNethash, encodeNethash, decodeFee, encodeFee, decodeFeesResponse, encodeFeesResponse, decodeFees, encodeFees, decodeMilestone, encodeMilestone, decodeReward, encodeReward, decodeSupply, encodeSupply, decodeStatus, encodeStatus

-}

import ElmArk.Api.Model exposing (RequestBody(..), RequestHead)
import Json.Encode
import Json.Decode
import Json.Decode.Pipeline


{-| Get info about a block

    mainNet
        |> getBlock "16338263029223576159"

-}
getBlock : String -> RequestHead -> ( RequestHead, RequestBody )
getBlock id heads =
    ( heads, GetRequest (heads.url ++ "api/blocks/get") [ ( "id", id ) ] )


{-| Record used to represent the response from getting a block
Sample JSON:

    {
        "success": true,
        "block": {
            "id": "16338263029223576159",
            "version": 0,
            "timestamp": 23958872,
            "height": 2953518,
            "previousBlock": "7539730724110889322",
            "numberOfTransactions": 1,
            "totalAmount": 100000000,
            "totalFee": 10000000,
            "reward": 200000000,
            "payloadLength": 32,
            "payloadHash": "e380f252e9d92c9085d6cfc6d14b7adbd43fc38f1036bd85f68565e7a912c8c6",
            "generatorPublicKey": "02a0ed5604868461a87639f58bd3a55f661774c3cbb705a735f58c50087f942c3d",
            "generatorId": "AHazk56nvQ3isZrYkarJV1RPcDUrQocmfR",
            "blockSignature": "3045022100fdd83eb7be8238837841375e7730109600223e1735a29ec1db9bf5763a3d2c9d0220402351074b1a24b5bf640617b07c8e8cfaa72896f2438bb44235537af06730ed",
            "confirmations": 43,
            "totalForged": "210000000"
        }
    }

-}
type alias BlockResponse =
    { success : Bool
    , block : Block
    }


{-| Record for an Ark blockchain block
Sample JSON:

    {
      "id": "16338263029223576159",
      "version": 0,
      "timestamp": 23958872,
      "height": 2953518,
      "previousBlock": "7539730724110889322",
      "numberOfTransactions": 1,
      "totalAmount": 100000000,
      "totalFee": 10000000,
      "reward": 200000000,
      "payloadLength": 32,
      "payloadHash": "e380f252e9d92c9085d6cfc6d14b7adbd43fc38f1036bd85f68565e7a912c8c6",
      "generatorPublicKey": "02a0ed5604868461a87639f58bd3a55f661774c3cbb705a735f58c50087f942c3d",
      "generatorId": "AHazk56nvQ3isZrYkarJV1RPcDUrQocmfR",
      "blockSignature": "3045022100fdd83eb7be8238837841375e7730109600223e1735a29ec1db9bf5763a3d2c9d0220402351074b1a24b5bf640617b07c8e8cfaa72896f2438bb44235537af06730ed",
      "confirmations": 43,
      "totalForged": "210000000"
    }

-}
type alias Block =
    { id : String
    , version : Int
    , timestamp : Int
    , height : Int
    , previousBlock : String
    , numberOfTransactions : Int
    , totalAmount : Int
    , totalFee : Int
    , reward : Int
    , payloadLength : Int
    , payloadHash : String
    , generatorPublicKey : String
    , generatorId : String
    , blockSignature : String
    , confirmations : Int
    , totalForged : String
    }


{-| Parses JSON from getting block information
-}
decodeBlockResponse : Json.Decode.Decoder BlockResponse
decodeBlockResponse =
    Json.Decode.Pipeline.decode BlockResponse
        |> Json.Decode.Pipeline.required "success" (Json.Decode.bool)
        |> Json.Decode.Pipeline.required "block" (decodeBlock)


{-| Parses JSON for a single block
-}
decodeBlock : Json.Decode.Decoder Block
decodeBlock =
    Json.Decode.Pipeline.decode Block
        |> Json.Decode.Pipeline.required "id" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "version" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "timestamp" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "height" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "previousBlock" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "numberOfTransactions" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "totalAmount" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "totalFee" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "reward" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "payloadLength" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "payloadHash" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "generatorPublicKey" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "generatorId" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "blockSignature" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "confirmations" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "totalForged" (Json.Decode.string)


{-| Turns BlockResponse record into JSON string
-}
encodeBlockResponse : BlockResponse -> Json.Encode.Value
encodeBlockResponse record =
    Json.Encode.object
        [ ( "success", Json.Encode.bool <| record.success )
        , ( "block", encodeBlock <| record.block )
        ]


{-| Turns Block record into JSON string
-}
encodeBlock : Block -> Json.Encode.Value
encodeBlock record =
    Json.Encode.object
        [ ( "id", Json.Encode.string <| record.id )
        , ( "version", Json.Encode.int <| record.version )
        , ( "timestamp", Json.Encode.int <| record.timestamp )
        , ( "height", Json.Encode.int <| record.height )
        , ( "previousBlock", Json.Encode.string <| record.previousBlock )
        , ( "numberOfTransactions", Json.Encode.int <| record.numberOfTransactions )
        , ( "totalAmount", Json.Encode.int <| record.totalAmount )
        , ( "totalFee", Json.Encode.int <| record.totalFee )
        , ( "reward", Json.Encode.int <| record.reward )
        , ( "payloadLength", Json.Encode.int <| record.payloadLength )
        , ( "payloadHash", Json.Encode.string <| record.payloadHash )
        , ( "generatorPublicKey", Json.Encode.string <| record.generatorPublicKey )
        , ( "generatorId", Json.Encode.string <| record.generatorId )
        , ( "blockSignature", Json.Encode.string <| record.blockSignature )
        , ( "confirmations", Json.Encode.int <| record.confirmations )
        , ( "totalForged", Json.Encode.string <| record.totalForged )
        ]


{-| Search all blocksblocks.

Available search params:
limit, orderBy, offset, generatorPublicKey, totalAmount,
totalFee, reward, previousBlock, height

    mainNet
        |> searchBlocks [("orderBy", "timestamp"), ("limit", "2")]

-}
searchBlocks : List ( String, String ) -> RequestHead -> ( RequestHead, RequestBody )
searchBlocks params heads =
    ( heads, GetRequest (heads.url ++ "api/blocks/get") params )


{-| Result of searching blocks
-}
type alias BlockSearch =
    { success : Bool
    , blocks : List Block
    , count : Int
    }


{-| Parses JSON into BlockSearch record
-}
decodeBlockSearch : Json.Decode.Decoder BlockSearch
decodeBlockSearch =
    Json.Decode.Pipeline.decode BlockSearch
        |> Json.Decode.Pipeline.required "success" (Json.Decode.bool)
        |> Json.Decode.Pipeline.required "blocks" (Json.Decode.list decodeBlock)
        |> Json.Decode.Pipeline.required "count" (Json.Decode.int)


{-| Turns BlockSearch record into JSON string
-}
encodeBlockSearch : BlockSearch -> Json.Encode.Value
encodeBlockSearch record =
    Json.Encode.object
        [ ( "success", Json.Encode.bool <| record.success )
        , ( "blocks", Json.Encode.list <| List.map encodeBlock <| record.blocks )
        , ( "count", Json.Encode.int <| record.count )
        ]


{-| Gets the epoch

    mainNet
        |> getEpoch

-}
getEpoch : RequestHead -> ( RequestHead, RequestBody )
getEpoch heads =
    ( heads, GetRequest (heads.url ++ "api/blocks/get") [] )


{-| Result of getting the epoch
Sample JSON

    {
        "success": true,
        "epoch": "2017-03-21T13:00:00.000Z"
    }

-}
type alias Epoch =
    { success : Bool
    , epoch : String
    }


{-| Parses JSON into Epoch record
-}
decodeEpoch : Json.Decode.Decoder Epoch
decodeEpoch =
    Json.Decode.Pipeline.decode Epoch
        |> Json.Decode.Pipeline.required "success" (Json.Decode.bool)
        |> Json.Decode.Pipeline.required "epoch" (Json.Decode.string)


{-| Turns Epoch record into JSON string
-}
encodeEpoch : Epoch -> Json.Encode.Value
encodeEpoch record =
    Json.Encode.object
        [ ( "success", Json.Encode.bool <| record.success )
        , ( "epoch", Json.Encode.string <| record.epoch )
        ]


{-| Gets the current block height

    mainNet
        |> getBlockHeight

-}
getBlockHeight : RequestHead -> ( RequestHead, RequestBody )
getBlockHeight heads =
    ( heads, GetRequest (heads.url ++ "api/blocks/getHeight") [] )


{-| The current block height
-}
type alias BlockHeight =
    { success : Bool
    , height : Int
    , id : String
    }


{-| Parses JSON into BlockHeight record
-}
decodeBlockHeight : Json.Decode.Decoder BlockHeight
decodeBlockHeight =
    Json.Decode.Pipeline.decode BlockHeight
        |> Json.Decode.Pipeline.required "success" (Json.Decode.bool)
        |> Json.Decode.Pipeline.required "height" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "id" (Json.Decode.string)


{-| Turns BlockHeight record into JSON
-}
encodeBlockHeight : BlockHeight -> Json.Encode.Value
encodeBlockHeight record =
    Json.Encode.object
        [ ( "success", Json.Encode.bool <| record.success )
        , ( "height", Json.Encode.int <| record.height )
        , ( "id", Json.Encode.string <| record.id )
        ]


{-| Gets the current netHash

    mainNet
        |> getNethash

-}
getNethash : RequestHead -> ( RequestHead, RequestBody )
getNethash heads =
    ( heads, GetRequest (heads.url ++ "api/blocks/getNethash") [] )


{-| Represents a netash
Sample JSON:

    {
        "success": true,
        "nethash": "6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988"
    }

-}
type alias Nethash =
    { success : Bool
    , nethash : String
    }


{-| Parses JSON into Nethash struct
-}
decodeNethash : Json.Decode.Decoder Nethash
decodeNethash =
    Json.Decode.Pipeline.decode Nethash
        |> Json.Decode.Pipeline.required "success" (Json.Decode.bool)
        |> Json.Decode.Pipeline.required "nethash" (Json.Decode.string)


{-| Turns Nethash into JSON string
-}
encodeNethash : Nethash -> Json.Encode.Value
encodeNethash record =
    Json.Encode.object
        [ ( "success", Json.Encode.bool <| record.success )
        , ( "nethash", Json.Encode.string <| record.nethash )
        ]


{-| Gets the current fees for a transaction

    mainNet
        |> getFee

-}
getFee : RequestHead -> ( RequestHead, RequestBody )
getFee heads =
    ( heads, GetRequest (heads.url ++ "api/blocks/getFee") [] )


{-| Represents the current fees for transactions
Sample JSON:

    {
        "success": true,
        "fee": 10000000
    }

-}
type alias Fee =
    { success : Bool
    , fee : Int
    }


{-| Parses JSON into a Fee record
-}
decodeFee : Json.Decode.Decoder Fee
decodeFee =
    Json.Decode.Pipeline.decode Fee
        |> Json.Decode.Pipeline.required "success" (Json.Decode.bool)
        |> Json.Decode.Pipeline.required "fee" (Json.Decode.int)


{-| Turns Fee record into JSON
-}
encodeFee : Fee -> Json.Encode.Value
encodeFee record =
    Json.Encode.object
        [ ( "success", Json.Encode.bool <| record.success )
        , ( "fee", Json.Encode.int <| record.fee )
        ]


{-| Get the current fees

    mainNet
        |> getFees

-}
getFees : RequestHead -> ( RequestHead, RequestBody )
getFees heads =
    ( heads, GetRequest (heads.url ++ "api/blocks/getFees") [] )


{-| Object representing the current fees response
Sample JSON:

    {
        "success": true,
        "fees": {
            "send": 10000000,
            "vote": 100000000,
            "secondsignature": 500000000,
            "delegate": 2500000000,
            "multisignature": 500000000
        }
    }

-}
type alias FeesResponse =
    { success : Bool
    , fees : Fees
    }


{-| Object representing the current fees
Sample JSON:

    {
      "send": 10000000,
      "vote": 100000000,
      "secondsignature": 500000000,
      "delegate": 2500000000,
      "multisignature": 500000000
    }

-}
type alias Fees =
    { send : Int
    , vote : Int
    , secondsignature : Int
    , delegate : Int
    , multisignature : Int
    }


{-| Turns JSON into FeesResponse
-}
decodeFeesResponse : Json.Decode.Decoder FeesResponse
decodeFeesResponse =
    Json.Decode.Pipeline.decode FeesResponse
        |> Json.Decode.Pipeline.required "success" (Json.Decode.bool)
        |> Json.Decode.Pipeline.required "fees" (decodeFees)


{-| Turns JSON into Fees
-}
decodeFees : Json.Decode.Decoder Fees
decodeFees =
    Json.Decode.Pipeline.decode Fees
        |> Json.Decode.Pipeline.required "send" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "vote" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "secondsignature" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "delegate" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "multisignature" (Json.Decode.int)


{-| Turns FeesResponse into JSON
-}
encodeFeesResponse : FeesResponse -> Json.Encode.Value
encodeFeesResponse record =
    Json.Encode.object
        [ ( "success", Json.Encode.bool <| record.success )
        , ( "fees", encodeFees <| record.fees )
        ]


{-| Turns Fees into JSON
-}
encodeFees : Fees -> Json.Encode.Value
encodeFees record =
    Json.Encode.object
        [ ( "send", Json.Encode.int <| record.send )
        , ( "vote", Json.Encode.int <| record.vote )
        , ( "secondsignature", Json.Encode.int <| record.secondsignature )
        , ( "delegate", Json.Encode.int <| record.delegate )
        , ( "multisignature", Json.Encode.int <| record.multisignature )
        ]


{-| Gets the current milestone

    mainNet
        |> getMilestone

-}
getMilestone : RequestHead -> ( RequestHead, RequestBody )
getMilestone heads =
    ( heads, GetRequest (heads.url ++ "api/blocks/getMilestone") [] )


{-| Milestone record
Sample JSON:

    {
        "success": true,
        "milestone": 0
    }

-}
type alias Milestone =
    { success : Bool
    , milestone : Int
    }


{-| Parses JSON into Milestone record
-}
decodeMilestone : Json.Decode.Decoder Milestone
decodeMilestone =
    Json.Decode.Pipeline.decode Milestone
        |> Json.Decode.Pipeline.required "success" (Json.Decode.bool)
        |> Json.Decode.Pipeline.required "milestone" (Json.Decode.int)


{-| Turns Milestone record into JSON
-}
encodeMilestone : Milestone -> Json.Encode.Value
encodeMilestone record =
    Json.Encode.object
        [ ( "success", Json.Encode.bool <| record.success )
        , ( "milestone", Json.Encode.int <| record.milestone )
        ]


{-| Gets the current block reward

    mainNet
        |> getReward

-}
getReward : RequestHead -> ( RequestHead, RequestBody )
getReward heads =
    ( heads, GetRequest (heads.url ++ "api/blocks/getReward") [] )


{-| Current block reward
Sample JSON:

    {
        "success": true,
        "reward": 200000000
    }

-}
type alias Reward =
    { success : Bool
    , reward : Int
    }


{-| Parses JSON into Reward
-}
decodeReward : Json.Decode.Decoder Reward
decodeReward =
    Json.Decode.Pipeline.decode Reward
        |> Json.Decode.Pipeline.required "success" (Json.Decode.bool)
        |> Json.Decode.Pipeline.required "reward" (Json.Decode.int)


{-| Turns Reward record into JSON
-}
encodeReward : Reward -> Json.Encode.Value
encodeReward record =
    Json.Encode.object
        [ ( "success", Json.Encode.bool <| record.success )
        , ( "reward", Json.Encode.int <| record.reward )
        ]


{-| Gets the current block supply

    mainNet
        |> getSupply

-}
getSupply : RequestHead -> ( RequestHead, RequestBody )
getSupply heads =
    ( heads, GetRequest (heads.url ++ "api/blocks/getSupply") [] )


{-| Record for the current supply
Sample JSON:

    {
        "success": true,
        "supply": 13090826000000000
    }

-}
type alias Supply =
    { success : Bool
    , supply : Int
    }


{-| Parse JSON into Supply
-}
decodeSupply : Json.Decode.Decoder Supply
decodeSupply =
    Json.Decode.Pipeline.decode Supply
        |> Json.Decode.Pipeline.required "success" (Json.Decode.bool)
        |> Json.Decode.Pipeline.required "supply" (Json.Decode.int)


{-| Turn Supply into JSON string
-}
encodeSupply : Supply -> Json.Encode.Value
encodeSupply record =
    Json.Encode.object
        [ ( "success", Json.Encode.bool <| record.success )
        , ( "supply", Json.Encode.int <| record.supply )
        ]


{-| Gets the current block status

    mainNet
        |> getStatus

-}
getStatus : RequestHead -> ( RequestHead, RequestBody )
getStatus heads =
    ( heads, GetRequest (heads.url ++ "api/blocks/getStatus") [] )


{-| Status of the blockchain
Sample JSON:

    {
        "success": true,
        "epoch": "2017-03-21T13:00:00.000Z",
        "height": 2954161,
        "fee": 10000000,
        "milestone": 0,
        "nethash": "6e84d08bd299ed97c212c886c98a57e36545c8f5d645ca7eeae63a8bd62d8988",
        "reward": 200000000,
        "supply": 13090832200000000
    }

-}
type alias Status =
    { success : Bool
    , epoch : String
    , height : Int
    , fee : Int
    , milestone : Int
    , nethash : String
    , reward : Int
    , supply : Int
    }


{-| Turns JSON into Status record
-}
decodeStatus : Json.Decode.Decoder Status
decodeStatus =
    Json.Decode.Pipeline.decode Status
        |> Json.Decode.Pipeline.required "success" (Json.Decode.bool)
        |> Json.Decode.Pipeline.required "epoch" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "height" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "fee" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "milestone" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "nethash" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "reward" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "supply" (Json.Decode.int)


{-| Turns Status record into JSON string
-}
encodeStatus : Status -> Json.Encode.Value
encodeStatus record =
    Json.Encode.object
        [ ( "success", Json.Encode.bool <| record.success )
        , ( "epoch", Json.Encode.string <| record.epoch )
        , ( "height", Json.Encode.int <| record.height )
        , ( "fee", Json.Encode.int <| record.fee )
        , ( "milestone", Json.Encode.int <| record.milestone )
        , ( "nethash", Json.Encode.string <| record.nethash )
        , ( "reward", Json.Encode.int <| record.reward )
        , ( "supply", Json.Encode.int <| record.supply )
        ]