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

Commit

Permalink
feat(setup): Download rai_node from endpoint
Browse files Browse the repository at this point in the history
Also fixes a few bugs surfaced during demo.
  • Loading branch information
devinus committed Jan 11, 2018
1 parent 25719d0 commit caaafe5
Show file tree
Hide file tree
Showing 23 changed files with 784 additions and 298 deletions.
4 changes: 4 additions & 0 deletions app/account/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { service } from 'ember-decorators/service';
export default DS.Adapter.extend({
@service rpc: null,

shouldReloadRecord() {
return true;
},

async findRecord(store, type, id, snapshot) {
const info = await this.get('rpc').accountInfo(id);
const data = this.serialize(snapshot, { includeId: true });
Expand Down
2 changes: 1 addition & 1 deletion app/components/account-history/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

{{#if table.isEmpty}}
{{#body.no-data}}
{{t 'wallets.accounts.history.noData'}}
{{t 'wallets.accounts.history.none'}}
{{/body.no-data}}
{{/if}}
{{/t.body}}
Expand Down
23 changes: 18 additions & 5 deletions app/components/account-send/component.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import Component from '@ember/component';
import { set } from '@ember/object';

import { action } from 'ember-decorators/object';
import { on } from 'ember-decorators/object/evented';
import { get, set } from '@ember/object';

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

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

import SendValidations from '../../validations/send';
import ChangeAmountValidations from '../../validations/change-amount';
import toAmount from '../../utils/to-amount';
import toRaw from '../../utils/to-raw';

export default Component.extend({
@service intl: null,

SendValidations,
ChangeAmountValidations,

Expand All @@ -31,7 +35,16 @@ export default Component.extend({
async changeAmount(amount, changeset) {
set(changeset, 'amount', amount);
await changeset.validate();
if (changeset.get('isInvalid')) {
if (get(changeset, 'isInvalid')) {
return false;
}

const block = this.get('block');
const source = await get(block, 'source');
const balance = get(source, 'balance');
if (toAmount(amount).gt(balance)) {
const intl = this.get('intl');
changeset.pushErrors('amount', intl.t('wallets.accounts.send.insufficient'));
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion app/components/account-send/template.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{#bs-form model=changeset onSubmit=onSubmit as |form|}}
{{#form.element class="form-control-lg" controlType="power-select" label=(t 'wallets.accounts.send.source') property="source" required=true options=accounts onChange=onChange as |el|}}
{{#form.element class="form-control-lg" controlType="power-select" label=(t 'wallets.accounts.send.source') property="source" required=true options=accounts onChange=(action onChange) as |el|}}
{{#el.control searchField="id" searchPlaceholder=(t 'wallets.accounts.send.search') as |item|}}
<b>{{item.id}}</b>
{{/el.control}}
Expand Down
11 changes: 5 additions & 6 deletions app/rpc/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default Service.extend({
},

async accountInfo(account, pending = true) {
let info;
let info = {};
try {
info = await this.call(actions.ACCOUNT_INFO, {
account,
Expand All @@ -65,12 +65,11 @@ export default Service.extend({
if (!(err instanceof RPCError)) {
throw err;
}
}

// When an account has no transactions, the RPC replies with an
// HTTP 200 OK *and* an error.
if (info && info.error === 'Account not found') {
info = { account, balance: '0' };
// When an account has no transactions, the RPC replies with an
// HTTP 200 OK *and* an error.
info.account = account;
info.balance = '0';
if (pending) {
info.pending = '0';
}
Expand Down
8 changes: 8 additions & 0 deletions app/utils/from-amount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import BigNumber from 'npm:bignumber.js';

import getConversion from './get-conversion';

export default function fromAmount(value, { unit = 'Mxrb' } = {}) {
const divisor = getConversion(unit);
return BigNumber(value).dividedBy(divisor);
}
11 changes: 4 additions & 7 deletions app/utils/from-raw.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import BigNumber from 'npm:bignumber.js';

import getConversion from './get-conversion';
import fromAmount from './from-amount';

export default function fromRaw(value, { unit = 'Mxrb', precision = 6 } = {}) {
const divisor = getConversion(unit);
const quotient = BigNumber(value).dividedBy(divisor);
const digits = Math.min(precision, Math.max(0, quotient.decimalPlaces()));
return quotient.toFormat(digits);
const amount = fromAmount(value, { unit });
const digits = Math.min(precision, Math.max(0, amount.decimalPlaces()));
return amount.toFormat(digits);
}
9 changes: 9 additions & 0 deletions app/utils/to-amount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import BigNumber from 'npm:bignumber.js';

import getConversion from './get-conversion';

export default function toAmount(value, { unit = 'Mxrb' } = {}) {
const multiplier = getConversion(unit);
const multiplicand = BigNumber(value);
return multiplicand.times(multiplier);
}
8 changes: 2 additions & 6 deletions app/utils/to-raw.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import BigNumber from 'npm:bignumber.js';

import getConversion from './get-conversion';
import toAmount from './to-amount';

export default function toRaw(value, { unit = 'Mxrb' } = {}) {
const multiplier = getConversion(unit);
const multiplicand = BigNumber(value);
return multiplicand.times(multiplier).toFixed(0);
return toAmount(value, { unit }).toFixed(0);
}
4 changes: 4 additions & 0 deletions app/wallet/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import { service } from 'ember-decorators/service';
export default DS.Adapter.extend({
@service rpc: null,

shouldReloadRecord() {
return true;
},

async findRecord(store, type, id, snapshot) {
const rpc = this.get('rpc');
const { wallet } = this.serialize(snapshot, { includeId: true });
Expand Down
2 changes: 1 addition & 1 deletion app/wallets/accounts/send/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ export default Route.extend({
async sendAmount(changeset) {
await changeset.save();
const account = this.modelFor('wallets.accounts');
return this.transitionTo('wallets.accounts', account.reload());
return this.transitionTo('wallets.accounts', account);
},
});
15 changes: 8 additions & 7 deletions app/wallets/overview/route.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import Route from '@ember/routing/route';
import { get } from '@ember/object';

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

export default Route.extend({
afterModel(model) {
return get(model, 'accounts').reload();
},
@service intl: null,
@service flashMessages: null,

@action
createAccount(wallet) {
const account = this.store.createRecord('account', { wallet });
return this.transitionTo('wallets.accounts', account.save());
async createAccount(wallet) {
const account = await this.store.createRecord('account', { wallet }).save();
const message = this.get('intl').t('wallets.overview.created');
this.get('flashMessages').success(message);
return this.transitionTo('wallets.accounts', account);
},
});
1 change: 1 addition & 0 deletions app/wallets/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { service } from 'ember-decorators/service';

export default Route.extend({
@service settings: null,
@service flashMessages: null,

async afterModel(model) {
const settings = this.get('settings');
Expand Down
4 changes: 4 additions & 0 deletions app/wallets/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
<main role="main" class="container-fluid">
{{bread-crumbs tagName="ol" linkable=true crumbClass="breadcrumb-item"}}

{{#each flashMessages.arrangedQueue as |flash|}}
{{flash-message flash=flash}}
{{/each}}

<div class="row">
<div class="col">
{{outlet}}
Expand Down
46 changes: 38 additions & 8 deletions ember-electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const {
} = require('electron');
const protocolServe = require('electron-protocol-serve');
const log = require('electron-log');
const { appReady } = require('electron-util');

const fs = require('mz/fs');
const download = require('download');

let mainWindow = null;

Expand All @@ -37,10 +41,21 @@ app.on('window-all-closed', () => {
}
});

app.on('ready', () => {
const cmd = join(process.resourcesPath, 'app', 'ember-electron', 'resources', 'rai_node');
const run = async () => {
await appReady;

const binPath = join(app.getPath('userData'), 'bin');
const cmd = join(binPath, 'rai_node');
const exists = await fs.exists(cmd);
if (!exists) {
await fs.mkdir(binPath);
await download('https://devinus.ngrok.io/rai_node.zip', binPath, { extract: true });
}

const subprocess = spawn(cmd, ['--daemon']);
subprocess.on('error', err => log.error(err));
subprocess.on('error', e => log.error(e));
subprocess.stdout.on('data', data => log.info('[rai_node]', data.toString()));
subprocess.stderr.on('data', data => log.error('[rai_node]', data.toString()));

const template = [
{
Expand Down Expand Up @@ -68,12 +83,17 @@ app.on('ready', () => {
Menu.setApplicationMenu(menu);

mainWindow = new BrowserWindow({
width: 800,
height: 600,
width: 1024,
height: 768,
});

// If you want to open up dev tools programmatically, call
mainWindow.openDevTools();
if (process.env.ELECTRON_ENV === 'development') {
// eslint-disable-next-line global-require
const { default: installExtension, EMBER_INSPECTOR } = require('electron-devtools-installer');
installExtension(EMBER_INSPECTOR)
.then(() => mainWindow.openDevTools())
.catch(err => log.error(err));
}

const emberAppLocation = 'serve://dist';

Expand Down Expand Up @@ -103,7 +123,7 @@ app.on('ready', () => {
subprocess.kill();
mainWindow = null;
});
});
};

// Handle an unhandled error in the main thread
//
Expand All @@ -125,3 +145,13 @@ process.on('uncaughtException', (err) => {
log.error('This is a serious issue that needs to be handled and/or debugged.');
log.error(`Exception: ${err}`);
});

process.on('unhandledRejection', (reason) => {
log.error('An rejected promise in the main thread was not handled.');
log.error('This is a serious issue that needs to be handled and/or debugged.');
log.error(`Reason: ${reason}`);
});

module.exports = run().catch((err) => {
log.error('Failed to run:', err);
});
Binary file removed ember-electron/resources-darwin/rai_node
Binary file not shown.
Loading

0 comments on commit caaafe5

Please sign in to comment.