Skip to content
Permalink
Browse files

feat(frontier): add frontier to models

  • Loading branch information...
devinus committed Dec 26, 2018
1 parent 7fec6f2 commit 14fe52ac68a248343334de46ab9626f58c3dfbaf
@@ -11,10 +11,10 @@ export default class AccountAdapter extends Adapter {
return true;
}

async findRecord(store, type, id) {
async findRecord(store, type, id, snapshot) {
const info = await this.get('rpc').accountInfo(id);
info.account = id;
return info;
const { account } = this.serialize(snapshot, { includeId: true });
return Object.assign({}, info, { account });
}

async createRecord(store, type, snapshot) {
@@ -5,17 +5,17 @@ import { attr, hasMany, belongsTo } from '@ember-decorators/data';
const { Model } = DS;

export default class AccountModel extends Model {
@belongsTo({ async: true }) wallet;
@belongsTo('wallet', { async: true }) wallet;

@belongsTo('block', { async: true, inverse: null }) frontier;
@belongsTo('frontier', { async: true }) frontier;

@belongsTo('block', { async: true, inverse: null }) openBlock;

@belongsTo('block', { async: true, inverse: null }) representativeBlock;

@hasMany({ async: true, inverse: 'source' }) blocks;
@hasMany('block', { async: true, inverse: 'source' }) blocks;

@hasMany({ async: true, inverse: 'source' }) history;
@hasMany('history', { async: true, inverse: 'source' }) history;

@attr('big-number', { defaultValue: 0 }) balance;

@@ -25,5 +25,7 @@ export default class AccountModel extends Model {

@attr('number', { defaultValue: 0 }) blockCount;

@attr('number', { defaultValue: 0 }) weight;

@attr('timestamp', { defaultValue: () => new Date() }) modifiedTimestamp;
}
@@ -10,6 +10,12 @@ export default class BlockAdapter extends Adapter {
return true;
}

async findRecord(store, type, id, snapshot) {
const contents = await this.get('rpc').block(id);
const { block } = this.serialize(snapshot, { includeId: true });
return Object.assign({}, contents, { block });
}

createRecord(store, type, snapshot) {
const {
wallet,
@@ -5,11 +5,21 @@ import { attr, belongsTo } from '@ember-decorators/data';
const { Model } = DS;

export default class BlockModel extends Model {
@belongsTo({ async: true, inverse: null }) wallet;
@belongsTo('wallet', { async: true, inverse: 'blocks' }) wallet;

@belongsTo('account', { async: true, inverse: 'blocks' }) account;

@belongsTo('frontier', { async: true, inverse: 'blocks' }) frontier;

@belongsTo('block', { async: true, inverse: null }) previous;

@belongsTo('account', { async: true, inverse: null }) representative;

@belongsTo('account', { async: true, inverse: 'blocks' }) source;

@attr() destination;
@attr('string', { defaultValue: 'state' }) type;

@attr('string') destination;

@attr('big-number', { defaultValue: 0 }) amount;
}
@@ -0,0 +1,18 @@
import DS from 'ember-data';

import { service } from '@ember-decorators/service';

const { Adapter } = DS;

export default class FrontierAdapter extends Adapter {
@service rpc;

async query(store, type, { wallet }) {
const frontiers = await this.get('rpc').walletFrontiers(wallet);
return Object.entries(frontiers).map(([account, hash]) => ({
wallet,
account,
hash,
}));
}
}
@@ -0,0 +1,15 @@
import DS from 'ember-data';

import { belongsTo, hasMany } from '@ember-decorators/data';

const { Model } = DS;

export default class FrontierModel extends Model {
@belongsTo('wallet', { async: true, inverse: null }) wallet;

@belongsTo('account', { async: true, inverse: null }) account;

@belongsTo('block', { async: true, inverse: 'frontier' }) hash;

@hasMany('block', { async: true, inverse: 'frontier' }) blocks;
}
@@ -0,0 +1,7 @@
import DS from 'ember-data';

const { JSONSerializer } = DS;

export default class FrontierSerializer extends JSONSerializer {
primaryKey = 'hash';
}
@@ -5,11 +5,11 @@ import { attr, belongsTo } from '@ember-decorators/data';
const { Model } = DS;

export default class HistoryModel extends Model {
@belongsTo('account', { async: true }) source;
@belongsTo('account', { async: true, inverse: 'history' }) source;

@attr({ defaultValue: 'state' }) type;
@attr('string', { defaultValue: 'state' }) type;

@attr account;
@attr('string') account;

@attr('big-number', { defaultValue: 0 }) amount;
}
@@ -23,9 +23,13 @@ const sumAccounts = computedDecorator((desc) => {
export default class WalletModel extends Model {
@hasMany('account', { async: true }) accounts;

@attr seed;
@hasMany('frontier', { async: true }) frontiers;

@attr representative;
@hasMany('block', { async: true }) blocks;

@attr('string') seed;

@attr('string') representative;

@sumAccounts balance;

@@ -9,6 +9,7 @@ describe('Unit | Serializer | account', () => {
'model:wallet',
'model:block',
'model:history',
'model:frontier',
'serializer:account',
'transform:big-number',
'transform:timestamp',
@@ -0,0 +1,13 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { setupTest } from 'ember-mocha';

describe('Unit | Adapter | frontier', () => {
setupTest();

// Replace this with your real tests.
it('exists', function () {
const adapter = this.owner.lookup('adapter:frontier');
expect(adapter).to.be.ok;
});
});
@@ -8,6 +8,7 @@ describe('Unit | Serializer | block', () => {
needs: [
'model:wallet',
'model:account',
'model:frontier',
'serializer:block',
'transform:big-number',
],
@@ -0,0 +1,14 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { setupTest } from 'ember-mocha';

describe('Unit | Model | frontier', () => {
setupTest();

// Replace this with your real tests.
it('exists', function () {
const store = this.owner.lookup('service:store');
const model = store.createRecord('frontier', {});
expect(model).to.be.ok;
});
});
@@ -0,0 +1,24 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { setupTest } from 'ember-mocha';

describe('Unit | Serializer | frontier', () => {
setupTest();

// Replace this with your real tests.
it('exists', function () {
const store = this.owner.lookup('service:store');
const serializer = store.serializerFor('frontier');

expect(serializer).to.be.ok;
});

it('serializes records', function () {
const store = this.owner.lookup('service:store');
const record = store.createRecord('frontier', {});

const serializedRecord = record.serialize();

expect(serializedRecord).to.be.ok;
});
});
@@ -7,9 +7,9 @@ describe('Unit | Serializer | wallet', () => {
// Specify the other units that are required for this test.
needs: [
'model:account',
'model:frontier',
'serializer:wallet',
'transform:big-number',
'model:account',
],
});

0 comments on commit 14fe52a

Please sign in to comment.
You can’t perform that action at this time.