1.0.0 • Published 6 years ago

rue-tools v1.0.0

Weekly downloads
5
License
GPL-3.0
Repository
github
Last release
6 years ago

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"