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

ElmArk.Account

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

Models

type alias Balance = { success : Bool , balance : String , unconfirmedBalance : String }

The balance record returned by ark.

Sample JSON:

{
    "success": true,
    "balance": "21231062440",
    "unconfirmedBalance": "21231062440"
}
type alias PublicKey = { success : Bool , publicKey : String }

The public key object

Sample JSON:

    {
        "success": true,
        "publicKey": "03f01aca02d5f229081b0a538d42461d1860804111fe8008c671a8854222d9b2cc"
    }
type alias DelegateFee = { success : Bool , fee : Int }

The delegate fee object

Sample JSON:

{
    "success": true,
    "fee": 2500000000
}
type alias Delegates = { success : Bool , delegates : List Delegate }

A list of delegates.

Sample JSON:

{
    "success": true,
    "delegates": [
        {
            "username": "bioly",
            "address": "AbUdMhk96FbzxH7vDYAwdyqUELmLopZV5x",
            "publicKey": "02c0b645f19ab304d25aae3add139edd9f6ca9fd0d98e57a808100de0e93832181",
            "vote": "179820519458568",
            "producedblocks": 57624,
            "missedblocks": 254,
            "rate": 2,
            "approval": 1.37,
            "productivity": 99.56
        }
    ]
}
type alias Delegate = { username : String , address : String , publicKey : String , vote : String , producedblocks : Int , missedblocks : Int , rate : Int , approval : Float , productivity : Float }

A single delegate.

Sample JSON:

{
    "username": "bioly",
    "address": "AbUdMhk96FbzxH7vDYAwdyqUELmLopZV5x",
    "publicKey": "02c0b645f19ab304d25aae3add139edd9f6ca9fd0d98e57a808100de0e93832181",
    "vote": "179820519458568",
    "producedblocks": 57624,
    "missedblocks": 254,
    "rate": 2,
    "approval": 1.37,
    "productivity": 99.56
}
type alias Accounts = { success : Bool , account : Account }

Represents the result of running the getAccount

type alias Account = { address : String , unconfirmedBalance : String , balance : String , publicKey : String , unconfirmedSignature : Int , secondSignature : Int , secondPublicKey : String , multisignatures : List String , u_multisignatures : List String }

A single account

type alias TopAccounts = { success : Bool , accounts : List TopAccount }

Record representing the list of top accounts Sample JSON

{
    "success": true,
    "accounts": [
        {
            "address": "AUDud8tvyVZa67p3QY7XPRUTjRGnWQQ9Xv",
            "balance": "2125000000000000",
            "publicKey": "021d03bace0687a1a5e797f884b13fb46f817ec32de1374a7f223f24404401d220"
        },
        {
            "address": "AUexKjGtgsSpVzPLs6jNMM6vJ6znEVTQWK",
            "balance": "1520990429100614",
            "publicKey": "02ff171adaef486b7db9fc160b28433d20cf43163d56fd28fee72145f0d5219a4b"
        },
        {
            "address": "ALgvTAoz5Vi9easHqBK6aEMKatHb4beCXm",
            "balance": "875001500000000",
            "publicKey": "03aa4863c93d170d89675a6e381d08a451c1067fc0f6fed479571d9ecb178963b3"
        }
    ]
}
type alias TopAccount = { address : String , balance : String , publicKey : String }

Record representing a top account. Sample JSON:

{
    "address": "AUDud8tvyVZa67p3QY7XPRUTjRGnWQQ9Xv",
    "balance": "2125000000000000",
    "publicKey": "021d03bace0687a1a5e797f884b13fb46f817ec32de1374a7f223f24404401d220"
}

Requests

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

Get an account's balance

mainNet
    |> getBalance "AUexKjGtgsSpVzPLs6jNMM6vJ6znEVTQWK"
getPublicKey : String -> RequestHead -> ( RequestHead, RequestBody )

Get an account's public key

mainNet
    |> getPublicKey "AUexKjGtgsSpVzPLs6jNMM6vJ6znEVTQWK"
getDelegateFee : RequestHead -> ( RequestHead, RequestBody )

Get the current delegate fee

mainNet
    |> getDelegateFee
getDelegates : String -> RequestHead -> ( RequestHead, RequestBody )

Get the current delegate fee

mainNet
    |> getDelegates "AbUdMhk96FbzxH7vDYAwdyqUELmLopZV5x"
getAccount : String -> RequestHead -> ( RequestHead, RequestBody )

Get an account's info

mainNet
    |> getAccount "AUexKjGtgsSpVzPLs6jNMM6vJ6znEVTQWK"
getTopAccounts : RequestHead -> ( RequestHead, RequestBody )

Get a list of top accounts

mainNet
    |> getTopAccounts

JSON

encodeBalance : Balance -> Json.Encode.Value

encodeBalance turns a Balance record into a JSON string

decodeBalance : Json.Decode.Decoder Balance

decodeBalance parses JSON and turns it into a Balance record

encodePublicKey : PublicKey -> Json.Encode.Value

encodePublicKey turns JSON into a PublicKey record

decodePublicKey : Json.Decode.Decoder PublicKey

decodePublicKey parses JSON and turns it into a PublicKey record

encodeDelegateFee : DelegateFee -> Json.Encode.Value

encodeDelegateFee turns a DelegateFee record into a JSON string

decodeDelegateFee : Json.Decode.Decoder DelegateFee

decodeDelegateFee turns a JSON into a DelegateFee record

encodeDelegate : Delegate -> Json.Encode.Value

encodeDelegate turns a Delegate Record into a JSON

decodeDelegate : Json.Decode.Decoder Delegate

decodeDelegate turns a JSON string into a Delegate

encodeDelegates : Delegates -> Json.Encode.Value

Turns a list of delegates into JSON

decodeDelegates : Json.Decode.Decoder Delegates

Turns JSON into a list of delegates

encodeAccounts : Accounts -> Json.Encode.Value

encodeAccounts turns accounts record into JSON string

decodeAccounts : Json.Decode.Decoder Accounts

Parses getAccoutns JSON into an Accounts record

encodeAccount : Account -> Json.Encode.Value

encodeAccount turns account record into JSON string

decodeAccount : Json.Decode.Decoder Account

Turns account JSON into Account record

encodeTopAccounts : TopAccounts -> Json.Encode.Value

Turns TopAccounts record into JSON string

decodeTopAccounts : Json.Decode.Decoder TopAccounts

Parses JSON into TopAccounts record

encodeTopAccount : TopAccount -> Json.Encode.Value

Turns TopAccount record into JSON

decodeTopAccount : Json.Decode.Decoder TopAccount

Parses JSON string into TopAccount record

module ElmArk.Account exposing (..)

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


# Models

@docs Balance, PublicKey, DelegateFee, Delegates, Delegate, Accounts, Account, TopAccounts, TopAccount


# Requests

@docs getBalance, getPublicKey, getDelegateFee, getDelegates, getAccount, getTopAccounts


# JSON

@docs encodeBalance, decodeBalance, encodePublicKey, decodePublicKey, encodeDelegateFee, decodeDelegateFee, encodeDelegate, decodeDelegate, encodeDelegates, decodeDelegates, encodeAccounts, decodeAccounts, encodeAccount, decodeAccount, encodeTopAccounts, decodeTopAccounts, encodeTopAccount, decodeTopAccount

-}

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


{-| Get an account's balance

    mainNet
        |> getBalance "AUexKjGtgsSpVzPLs6jNMM6vJ6znEVTQWK"

-}
getBalance : String -> RequestHead -> ( RequestHead, RequestBody )
getBalance address heads =
    ( heads, GetRequest (heads.url ++ "api/accounts/getBalance") [ ( "address", address ) ] )


{-| The balance record returned by ark.

Sample JSON:

    {
        "success": true,
        "balance": "21231062440",
        "unconfirmedBalance": "21231062440"
    }

-}
type alias Balance =
    { success : Bool
    , balance : String
    , unconfirmedBalance : String
    }


{-| decodeBalance parses JSON and turns it into a Balance record
-}
decodeBalance : Json.Decode.Decoder Balance
decodeBalance =
    Json.Decode.Pipeline.decode Balance
        |> Json.Decode.Pipeline.required "success" (Json.Decode.bool)
        |> Json.Decode.Pipeline.required "balance" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "unconfirmedBalance" (Json.Decode.string)


{-| encodeBalance turns a Balance record into a JSON string
-}
encodeBalance : Balance -> Json.Encode.Value
encodeBalance record =
    Json.Encode.object
        [ ( "success", Json.Encode.bool <| record.success )
        , ( "balance", Json.Encode.string <| record.balance )
        , ( "unconfirmedBalance", Json.Encode.string <| record.unconfirmedBalance )
        ]


{-| Get an account's public key

    mainNet
        |> getPublicKey "AUexKjGtgsSpVzPLs6jNMM6vJ6znEVTQWK"

-}
getPublicKey : String -> RequestHead -> ( RequestHead, RequestBody )
getPublicKey address heads =
    ( heads, GetRequest (heads.url ++ "api/accounts/getPublicKey") [ ( "address", address ) ] )


{-| The public key object

Sample JSON:

        {
            "success": true,
            "publicKey": "03f01aca02d5f229081b0a538d42461d1860804111fe8008c671a8854222d9b2cc"
        }

-}
type alias PublicKey =
    { success : Bool
    , publicKey : String
    }


{-| decodePublicKey parses JSON and turns it into a PublicKey record
-}
decodePublicKey : Json.Decode.Decoder PublicKey
decodePublicKey =
    Json.Decode.Pipeline.decode PublicKey
        |> Json.Decode.Pipeline.required "success" (Json.Decode.bool)
        |> Json.Decode.Pipeline.required "publicKey" (Json.Decode.string)


{-| encodePublicKey turns JSON into a PublicKey record
-}
encodePublicKey : PublicKey -> Json.Encode.Value
encodePublicKey record =
    Json.Encode.object
        [ ( "success", Json.Encode.bool <| record.success )
        , ( "publicKey", Json.Encode.string <| record.publicKey )
        ]


{-| Get the current delegate fee

    mainNet
        |> getDelegateFee

-}
getDelegateFee : RequestHead -> ( RequestHead, RequestBody )
getDelegateFee heads =
    ( heads, GetRequest (heads.url ++ "api/accounts/delegates/fee") [] )


{-| The delegate fee object

Sample JSON:

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

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


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


{-| encodeDelegateFee turns a DelegateFee record into a JSON string
-}
encodeDelegateFee : DelegateFee -> Json.Encode.Value
encodeDelegateFee record =
    Json.Encode.object
        [ ( "success", Json.Encode.bool <| record.success )
        , ( "fee", Json.Encode.int <| record.fee )
        ]


{-| A single delegate.

Sample JSON:

    {
        "username": "bioly",
        "address": "AbUdMhk96FbzxH7vDYAwdyqUELmLopZV5x",
        "publicKey": "02c0b645f19ab304d25aae3add139edd9f6ca9fd0d98e57a808100de0e93832181",
        "vote": "179820519458568",
        "producedblocks": 57624,
        "missedblocks": 254,
        "rate": 2,
        "approval": 1.37,
        "productivity": 99.56
    }

-}
type alias Delegate =
    { username : String
    , address : String
    , publicKey : String
    , vote : String
    , producedblocks : Int
    , missedblocks : Int
    , rate : Int
    , approval : Float
    , productivity : Float
    }


{-| decodeDelegate turns a JSON string into a Delegate
-}
decodeDelegate : Json.Decode.Decoder Delegate
decodeDelegate =
    Json.Decode.Pipeline.decode Delegate
        |> Json.Decode.Pipeline.required "username" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "address" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "publicKey" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "vote" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "producedblocks" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "missedblocks" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "rate" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "approval" (Json.Decode.float)
        |> Json.Decode.Pipeline.required "productivity" (Json.Decode.float)


{-| encodeDelegate turns a Delegate Record into a JSON
-}
encodeDelegate : Delegate -> Json.Encode.Value
encodeDelegate record =
    Json.Encode.object
        [ ( "username", Json.Encode.string <| record.username )
        , ( "address", Json.Encode.string <| record.address )
        , ( "publicKey", Json.Encode.string <| record.publicKey )
        , ( "vote", Json.Encode.string <| record.vote )
        , ( "producedblocks", Json.Encode.int <| record.producedblocks )
        , ( "missedblocks", Json.Encode.int <| record.missedblocks )
        , ( "rate", Json.Encode.int <| record.rate )
        , ( "approval", Json.Encode.float <| record.approval )
        , ( "productivity", Json.Encode.float <| record.productivity )
        ]


{-| Get the current delegate fee

    mainNet
        |> getDelegates "AbUdMhk96FbzxH7vDYAwdyqUELmLopZV5x"

-}
getDelegates : String -> RequestHead -> ( RequestHead, RequestBody )
getDelegates address heads =
    ( heads, GetRequest (heads.url ++ "api/accounts/delegates") [ ( "address", address ) ] )


{-| A list of delegates.

Sample JSON:

    {
        "success": true,
        "delegates": [
            {
                "username": "bioly",
                "address": "AbUdMhk96FbzxH7vDYAwdyqUELmLopZV5x",
                "publicKey": "02c0b645f19ab304d25aae3add139edd9f6ca9fd0d98e57a808100de0e93832181",
                "vote": "179820519458568",
                "producedblocks": 57624,
                "missedblocks": 254,
                "rate": 2,
                "approval": 1.37,
                "productivity": 99.56
            }
        ]
    }

-}
type alias Delegates =
    { success : Bool
    , delegates : List Delegate
    }


{-| Turns JSON into a list of delegates
-}
decodeDelegates : Json.Decode.Decoder Delegates
decodeDelegates =
    Json.Decode.Pipeline.decode Delegates
        |> Json.Decode.Pipeline.required "success" (Json.Decode.bool)
        |> Json.Decode.Pipeline.required "delegates" (Json.Decode.list decodeDelegate)


{-| Turns a list of delegates into JSON
-}
encodeDelegates : Delegates -> Json.Encode.Value
encodeDelegates record =
    Json.Encode.object
        [ ( "success", Json.Encode.bool <| record.success )
        , ( "delegates", Json.Encode.list <| List.map encodeDelegate <| record.delegates )
        ]


{-| Get an account's info

    mainNet
        |> getAccount "AUexKjGtgsSpVzPLs6jNMM6vJ6znEVTQWK"

-}
getAccount : String -> RequestHead -> ( RequestHead, RequestBody )
getAccount address heads =
    ( heads, GetRequest (heads.url ++ "api/accounts") [ ( "address", address ) ] )


{-| Represents the result of running the getAccount
-}
type alias Accounts =
    { success : Bool
    , account : Account
    }


{-| A single account
-}
type alias Account =
    { address : String
    , unconfirmedBalance : String
    , balance : String
    , publicKey : String
    , unconfirmedSignature : Int
    , secondSignature : Int
    , secondPublicKey : String
    , multisignatures : List String
    , u_multisignatures : List String
    }


{-| Parses getAccoutns JSON into an Accounts record
-}
decodeAccounts : Json.Decode.Decoder Accounts
decodeAccounts =
    Json.Decode.Pipeline.decode Accounts
        |> Json.Decode.Pipeline.required "success" (Json.Decode.bool)
        |> Json.Decode.Pipeline.required "account" (decodeAccount)


{-| Turns account JSON into Account record
-}
decodeAccount : Json.Decode.Decoder Account
decodeAccount =
    Json.Decode.Pipeline.decode Account
        |> Json.Decode.Pipeline.required "address" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "unconfirmedBalance" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "balance" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "publicKey" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "unconfirmedSignature" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "secondSignature" (Json.Decode.int)
        |> Json.Decode.Pipeline.required "secondPublicKey" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "multisignatures" (Json.Decode.list Json.Decode.string)
        |> Json.Decode.Pipeline.required "u_multisignatures" (Json.Decode.list Json.Decode.string)


{-| encodeAccounts turns accounts record into JSON string
-}
encodeAccounts : Accounts -> Json.Encode.Value
encodeAccounts record =
    Json.Encode.object
        [ ( "success", Json.Encode.bool <| record.success )
        , ( "account", encodeAccount <| record.account )
        ]


{-| encodeAccount turns account record into JSON string
-}
encodeAccount : Account -> Json.Encode.Value
encodeAccount record =
    Json.Encode.object
        [ ( "address", Json.Encode.string <| record.address )
        , ( "unconfirmedBalance", Json.Encode.string <| record.unconfirmedBalance )
        , ( "balance", Json.Encode.string <| record.balance )
        , ( "publicKey", Json.Encode.string <| record.publicKey )
        , ( "unconfirmedSignature", Json.Encode.int <| record.unconfirmedSignature )
        , ( "secondSignature", Json.Encode.int <| record.secondSignature )
        , ( "secondPublicKey", Json.Encode.string <| record.secondPublicKey )
        , ( "multisignatures", Json.Encode.list <| List.map Json.Encode.string <| record.multisignatures )
        , ( "u_multisignatures", Json.Encode.list <| List.map Json.Encode.string <| record.u_multisignatures )
        ]

{-| Get a list of top accounts

    mainNet
        |> getTopAccounts

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

{-| Record representing the list of top accounts
Sample JSON
```
{
    "success": true,
    "accounts": [
        {
            "address": "AUDud8tvyVZa67p3QY7XPRUTjRGnWQQ9Xv",
            "balance": "2125000000000000",
            "publicKey": "021d03bace0687a1a5e797f884b13fb46f817ec32de1374a7f223f24404401d220"
        },
        {
            "address": "AUexKjGtgsSpVzPLs6jNMM6vJ6znEVTQWK",
            "balance": "1520990429100614",
            "publicKey": "02ff171adaef486b7db9fc160b28433d20cf43163d56fd28fee72145f0d5219a4b"
        },
        {
            "address": "ALgvTAoz5Vi9easHqBK6aEMKatHb4beCXm",
            "balance": "875001500000000",
            "publicKey": "03aa4863c93d170d89675a6e381d08a451c1067fc0f6fed479571d9ecb178963b3"
        }
    ]
}
```
-}
type alias TopAccounts =
    { success : Bool
    , accounts : List TopAccount
    }

{-| Parses JSON into TopAccounts record
-}
decodeTopAccounts : Json.Decode.Decoder TopAccounts
decodeTopAccounts =
    Json.Decode.Pipeline.decode TopAccounts
        |> Json.Decode.Pipeline.required "success" (Json.Decode.bool)
        |> Json.Decode.Pipeline.required "accounts" (Json.Decode.list decodeTopAccount)

{-| Turns TopAccounts record into JSON string
-}
encodeTopAccounts : TopAccounts -> Json.Encode.Value
encodeTopAccounts record =
    Json.Encode.object
        [ ("success",  Json.Encode.bool <| record.success)
        , ("accounts",  Json.Encode.list <| List.map encodeTopAccount <| record.accounts)
        ]
{-| Record representing a top account.
Sample JSON:
```
{
    "address": "AUDud8tvyVZa67p3QY7XPRUTjRGnWQQ9Xv",
    "balance": "2125000000000000",
    "publicKey": "021d03bace0687a1a5e797f884b13fb46f817ec32de1374a7f223f24404401d220"
}
```
-}
type alias TopAccount =
    { address : String
    , balance : String
    , publicKey : String
    }

{-| Parses JSON string into TopAccount record
-}
decodeTopAccount : Json.Decode.Decoder TopAccount
decodeTopAccount =
    Json.Decode.Pipeline.decode TopAccount
        |> Json.Decode.Pipeline.required "address" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "balance" (Json.Decode.string)
        |> Json.Decode.Pipeline.required "publicKey" (Json.Decode.string)

{-| Turns TopAccount record into JSON
-}
encodeTopAccount : TopAccount -> Json.Encode.Value
encodeTopAccount record =
    Json.Encode.object
        [ ("address",  Json.Encode.string <| record.address)
        , ("balance",  Json.Encode.string <| record.balance)
        , ("publicKey",  Json.Encode.string <| record.publicKey)
        ]