Skip to content
This repository has been archived by the owner on Oct 21, 2020. It is now read-only.

Commit

Permalink
feat: Auto convert between units
Browse files Browse the repository at this point in the history
  • Loading branch information
devinus committed Jan 11, 2018
1 parent 1fd85d6 commit fa806fe
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
20 changes: 19 additions & 1 deletion app/components/account-send/component.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
import Component from '@ember/component';
import { get } from '@ember/object';
import { get, set } from '@ember/object';

import { action } from 'ember-decorators/object';
import { on } from 'ember-decorators/object/evented';

import Changeset from 'ember-changeset';
import lookupValidator from 'ember-changeset-validations';

import BigNumber from 'npm:bignumber.js';

import SendValidations from '../../validations/send';
import { prefixes } from '../../utils/format-amount';

export default Component.extend({
accounts: null,
block: null,

changeset: null,
amount: null,

@on('init')
createChangeset() {
const block = get(this, 'block');
this.changeset = new Changeset(block, lookupValidator(SendValidations), SendValidations);
},

@action
updateAmount(value, changeset) {
if (!value) {
return false;
}

const multiplier = prefixes.Mxrb;
const multiplicand = BigNumber(value).times(multiplier);
const raw = multiplicand.toFixed(0);
set(changeset, 'amount', raw);
return false;
},
});
2 changes: 1 addition & 1 deletion app/components/account-send/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

{{form.element class="form-control-lg" controlType="text" label="Destination Account" property="destination" placeholder="e.g. xrb_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo"}}

{{form.element class="form-control-lg" static=true controlType="number" label="Amount (XRB)" property="amount"}}
{{form.element class="form-control-lg" static=true controlType="number" label="Amount (XRB)" value=amount onChange=(action 'updateAmount')}}

<div role="group" class="d-flex justify-content-center">
{{#bs-button size="lg" type="primary" icon="fa fa-paper-plane" buttonType="submit"}}
Expand Down
4 changes: 2 additions & 2 deletions app/utils/format-amount.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import BigNumber from 'npm:bignumber.js';

const base10 = BigNumber(10);

const PREFIXES = {
export const prefixes = {
Gxrb: base10.pow(33),
Mxrb: base10.pow(30),
kxrb: base10.pow(27),
Expand All @@ -12,7 +12,7 @@ const PREFIXES = {
};

export default function formatAmount(value = 0, { prefix = 'Mxrb', precision = 6 } = {}) {
const divisor = PREFIXES[prefix] || PREFIXES.Mxrb;
const divisor = prefixes[prefix] || prefixes.Mxrb;
const quotient = BigNumber(value).dividedBy(divisor);
const digits = Math.max(precision, Math.min(1, quotient.decimalPlaces()));
return quotient.toFormat(digits);
Expand Down

0 comments on commit fa806fe

Please sign in to comment.