This repository has been archived by the owner on Oct 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(wallet): poll for account balance changes within a wallet
- Loading branch information
Showing
12 changed files
with
123 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
import DS from 'ember-data'; | ||
|
||
import { underscore } from '@ember/string'; | ||
|
||
export default DS.JSONSerializer.extend({ | ||
primaryKey: 'account', | ||
|
||
keyForAttribute(attr) { | ||
return underscore(attr); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { A } from '@ember/array'; | ||
import { isPresent } from '@ember/utils'; | ||
|
||
import BigNumber from 'npm:bignumber.js'; | ||
|
||
export default function sumAmounts(amounts) { | ||
return A(amounts) | ||
.filter(isPresent) | ||
.map(x => new BigNumber(String(x))) | ||
.reduce((x, y) => y.plus(x), 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,55 @@ | ||
import Controller from '@ember/controller'; | ||
import { get } from '@ember/object'; | ||
|
||
import { service } from 'ember-decorators/service'; | ||
import { on } from 'ember-decorators/object/evented'; | ||
|
||
const WALLET_POLL_INTERVAL = 5000; // 5s | ||
|
||
export default Controller.extend({ | ||
@service pollboy: null, | ||
@service flashMessages: null, | ||
@service rpc: null, | ||
|
||
poller: null, | ||
|
||
@on('init') | ||
setupPoller() { | ||
const pollboy = this.get('pollboy'); | ||
this.poller = pollboy.add(this, this.onPoll, WALLET_POLL_INTERVAL); | ||
this.poller.pause(); | ||
}, | ||
|
||
willDestroy(...args) { | ||
this._super(...args); | ||
|
||
const poller = this.get('poller'); | ||
if (poller) { | ||
this.get('pollboy').remove(poller); | ||
} | ||
}, | ||
|
||
async onPoll() { | ||
if (!this.isDestroying) { | ||
const model = this.get('model'); | ||
if (model) { | ||
const isNew = get(model, 'isNew'); | ||
if (!isNew) { | ||
const wallet = get(model, 'id'); | ||
this.updateBalances(wallet); | ||
} | ||
} | ||
} | ||
}, | ||
|
||
async updateBalances(wallet) { | ||
const balances = await this.get('rpc').walletBalances(wallet); | ||
const data = Object.entries(balances).map(([id, attributes]) => ({ | ||
id, | ||
attributes, | ||
type: 'account', | ||
})); | ||
|
||
this.store.push({ data }); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
import sumAmounts from '@nanocurrency/nano-desktop/utils/sum-amounts'; | ||
|
||
describe('Unit | Utility | sum-amounts', () => { | ||
// Replace this with your real tests. | ||
it('works', () => { | ||
const result = sumAmounts([1, 2]); | ||
expect(result).to.be.ok; | ||
expect(result.toFixed(0)).to.equal('3'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters