bitcoin.frink

Download or view bitcoin.frink in plain text format


/** This sample adds Bitcoin exchange rates using the symbols BTC, XBT, or
    "bitcoin", or ₿ (Unicode \u20bf), the Unicode symbol.

    (XBT is more likely to be used as an ISO-4217-compatible symbol.  See
     http://www.coindesk.com/bitcoin-gaining-market-based-legitimacy-xbt/ )

    This uses the CoinDesk Bitcoin Index, which is an average of exchange rates
    on various Bitcoin exchanges.
    ( http://www.coindesk.com/price/ ) (Powered by Coindesk.  They made me say
      that. )

    See the CoinDesk API
    ( http://www.coindesk.com/api/ )
    for information about the various data sources available.

    The data source is in JSON format ( http://www.json.org/ ) so this
    requires Frink version 2014-02-17 or later which includes the
    parseJSON[string] function.  See:
    https://frinklang.org/#ParsingJSON

    The value of bitcoins are captured when this file is parsed, and not
    updated over the course of a run, to avoid swamping the server for
    requests.
*/


// NOTE:  The := operator creates a new globally-available unit that also gets
// prefix- and suffix-checking, so you can write "millibitcoin" or "bitcoins".
    
// Check out this parsing and declaration of a new Frink unit (with correct
// dimensions of currency, due to multiplying by USD) in one line:    
    
bitcoin := parseJSON[read["https://api.coindesk.com/v1/bpi/currentprice/USD.json"]]@"bpi"@"USD"@"rate_float" USD

BTC := bitcoin      // Add a common alias

XBT := bitcoin      // Add a newer alias that will fit ISO-4217 standard.

//\u20bf := bitcoin   // Unicode symbol ₿ (this doesn't work as a standalone symbol)
    // TODO: Check what Unicode type that is (Currency Symbol) 20a0 - 20cf
    // https://www.compart.com/en/unicode/block/U+20A0 
    
satoshi := 1e-8 bitcoin  // One ten-millionth of a Bitcoin.

// Calculate estimated time to find a bitcoin given the difficulty
// (as a dimensionless number) 
// your hashing rate (e.g. 8 billion/s)
// The factor of 2^32 arises because each work unit requires you to hash
// 2^32 values.
bitcoinTime[difficulty is dimensionless, hashrate is frequency] := difficulty * 2^32 / hashrate
    


Download or view bitcoin.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 19935 days, 9 hours, 57 minutes ago.