GithubHelp home page GithubHelp logo

meteor-package-tools's Introduction

Ethereum tools

A set of helper functions for ethereum dapps.

See here for a demo of the template helpers.

Installation

You can either add it as a Meteor package using:

$ Meteor add ethereum:tools

or add link to the ethtools.js in your HTML.

Usage

This package provides formating and converting functionality.

When using the EthTools.ticker it will call the cryptocompare.com public API every 30s to retrive price information for ether. When used as a Meteor package, the following units are possible for some methods:

- `btc`
- `usd`
- `eur`
- `cad`
- `gbp`
- `jpy`
- And all ether units ('ether', 'finney', 'wei', etc)

Note As non-meteor package you can only use the ether units.


EthTools.ticker

EthTools.ticker.start();
EthTools.ticker.findOne(unit)

Note This is only available when used as a Meteor package.

To start polling for ticker prices run EthTools.ticker.start()

It gives you the latest price for ether based on the kraken.com public API. EthTools.ticker is a reactive collection, so when used in a reactive function it will re-run this function when the price is updated.

The ticker will be updated every 30 seconds.

Methods

Its a normal Meteor collection

  • start(options) - starts the polling for the ticker, the options object can be an object with {extraParams: 'mydata'} to be added to the ticker polling call
  • findOne(unit) - returns an object with the price of the unit
  • find().fetch() - returns all available price ticker units

Returns

  • Object
{
    _id: 'btc',
    price: '0.02000'
}

Example

var usd = EthTools.ticker.findOne("usd");

if (usd) console.log(usd.price); // "2.0000"

EthTools.setLocale

EthTools.setLocale(locale)

Set the locale to display numbers differently in other countries. This functions lets EthTools.formatBalance() and EthTools.formatNumber() reactivly re-run, to show the new format.

Parameters

  • locale (String) - the locale to set

Returns

String - the set locale e.g. en

Example

EthTools.setLocale("de");
EthTools.formatNumber(2000, "0,0.00");
// 2 000,00

EthTools.setUnit

EthTools.setUnit(unit)

Note This is only available when used as a Meteor package.

Reactivly sets a unit used as default unit, when no unit is passed to other EthTools methods. And also persists it in localstorage so its the same when you reload you app.

Default is unit ether.

Parameters

  • unit (String) - the unit to set, see Usage for more

Returns

Boolean - TRUE if the unit is an allowed unit and could be set

Example

EthTools.setUnit("btc");

Tracker.autorun(function() {
  var amount = EthTools.formatBalance("23000000000000000000", "0,0.0[00] unit");
  // amount = "0.287 btc"
});

EthTools.getUnit

EthTools.getUnit()

Note This is only available when used as a Meteor package.

Reactivly gets the current set default unit, used byt other EthTools methods when no unit was passed. And also persists it in localstorage so its the same when you reload you app.

Default is unit ether.

Parameters

none

Returns

String - the current default unit.

Example

EthTools.setUnit("btc");

Tracker.autorun(function() {
  var unit = EthTools.getUnit();
  // unit === 'btc'
});

EthTools.formatNumber

EthTools.formatNumber(number, format)

Formats any number using numeral.js, e.g. "0,0.00[0000]".

Parameters

  • number (String|Number) - the number to format
  • format (String) - the format see numeral.js for examples, e.g. "0,0.00[0000]"

Returns

String - the formated number.

Example

var finney = EthTools.formatNumber(2000, "0,0.00");
// finney = '2,000.00'

Format number template helper

Usage

{{dapp_formatNumber "1000000133" "0,0.00[0000]"}}

EthTools.formatBalance

EthTools.formatBalance(wei, format, unit)

Formats a number of wei into any other ethereum unit and other currencies (see Usage).

Default is unit ether.

The format property follows the numeral.js formatting, e.g. "0,0.00[0000]". Additionally you can add "unit" or "UNIT" (for uppercase) to display the unit after or before the number the number.

Additionally this function uses the reactive EthTools.getUnit() variable, when no unit was given. You can then reactivly change the unit using EthTools.setUnit('finney')

Parameters

  • wei (String|Number) - the amount of wei to convert and format
  • format (String) - the format see numeral.js for examples, e.g. "0,0.00[0000]".
  • unit (String) - (optional) the unit to convert the given wei amount to, if not given it will use EthTools.getUnit()

Returns

String - the formated balance.

Example

var amount = EthTools.formatBalance(
  112345676543212345,
  "0,0.0[00] unit",
  "finney"
);
// amount = "112.346 finney"

Format balances template helper

format balances

Usage

{{dapp_formatBalance "1000000133" "0,0.00[0000]" "ether"}}

If you leave the last value it will use EthTools.getUnit(), as reactive localstorage variable.

{{dapp_formatBalance "1000000133" "0,0.00"}}

Use then EthTools.setUnit(finney') to change the unit and displayed balances.


EthTools.toWei

EthTools.toWei(number, unit)

Formats an amount of any supported unit (see Usage) into wei.

Default is unit ether.

Additionally this function uses the reactive EthTools.getUnit() variable, when no unit was given. You can then reactivly change the unit using EthTools.setUnit('finney')

Parameters

  • number (String|Number) - the number of a unit, see Usage for more
  • unit (String) - the unit of the given number

Returns

String - the number in wei.

Example

var wei = EthTools.toWei(23, "btc");
// wei = "80000000000000000000"

meteor-package-tools's People

Contributors

derron avatar dev-dan avatar evertonfraga avatar frozeman avatar hackmod avatar logvik avatar ryanio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

meteor-package-tools's Issues

License

Hi there,

I suppose this project is licensed under the GPL 3.0 but it is not stated anywhere.
Could you add a license file to confirm that?

Cryptocompare API connection refused

I don't believe this api is active or at least not public anymore.

GET https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=BTC,USD,EUR net::ERR_CONNECTION_REFUSED

A USD alternative would be etherscan's:
https://api.etherscan.io/api?module=stats&action=ethprice

Ethereum:tools gets added because I am using ethereum:elements on the clientside.

Inaccurate info in README

Krakken API no longer used but still referenced in README.
Doc's detail usage of unit references that are not utilized in the code. Am not clear if these were deprecated or are net yet implemented.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.