Permalink
Browse files

feat(assets): asset transactions are displayed in transaction lists

  • Loading branch information...
1 parent 9c98c0f commit d1ee4862cc511d7898478c77a2e7635007e4a065 @beregovoy68 beregovoy68 committed Nov 17, 2016
Showing with 109 additions and 27 deletions.
  1. +30 −2 src/js/application.context.factory.js
  2. +70 −23 src/js/shared/transaction.filter.js
  3. +9 −2 src/js/wallet/wallet.controller.js
@@ -1,10 +1,38 @@
(function () {
'use strict';
- function ApplicationContextFactory() {
+ function ApplicationContextFactory(constants) {
var assets = {};
+ function updateTotalTokens(assetId, reissuedAmount) {
+ var asset = assets[assetId];
+ if (angular.isDefined(asset)) {
+ var reissued = Money.fromCoins(reissuedAmount, asset.currency);
+ asset.totalTokens = asset.totalTokens.plus(reissued);
+ console.log(asset.totalTokens.formatAmount());
+ }
+ }
+
+ assets.grab = function (transactions) {
+ var confirmed = _.reject(transactions, function (tx) {
+ return tx.unconfirmed;
+ });
+ var issueTransactions = _.where(confirmed, {type: constants.ASSET_ISSUE_TRANSACTION_TYPE});
+ _.map(issueTransactions, assets.put);
+
+ var reissueTransactions = _.where(confirmed, {type: constants.ASSET_REISSUE_TRANSACTION_TYPE});
+ var grouped = _.groupBy(reissueTransactions, 'assetId');
+ var accumulated = _.mapObject(grouped, function (values) {
+ return _.reduce(values, function (memo, tx) {
+ return memo + tx.quantity;
+ }, 0);
+ });
+ _.mapObject(accumulated, function (value, assetId) {
+ updateTotalTokens(assetId, value);
+ });
+ };
+
assets.put = function (issueTransaction) {
var currency = new Currency({
id: issueTransaction.assetId,
@@ -37,7 +65,7 @@
};
}
- ApplicationContextFactory.$inject = [];
+ ApplicationContextFactory.$inject = ['constants.transactions'];
angular
.module('app.ui')
@@ -1,16 +1,29 @@
(function () {
'use strict';
- function TransactionFilter(applicationContext, formattingService, addressService) {
- var TRANSACTION_TYPES = {
- 2: 'Payment',
- 3: 'Asset Issue',
- 4: 'Transfer',
- 5: 'Asset Reissue'
+ function TransactionFilter(constants, applicationContext, formattingService, addressService) {
+ var TRANSACTION_SPEC = {};
+ TRANSACTION_SPEC[constants.PAYMENT_TRANSACTION_TYPE] = {
+ type: 'Payment',
+ processor: processPaymentTransaction
+ };
+ TRANSACTION_SPEC[constants.ASSET_ISSUE_TRANSACTION_TYPE] = {
+ type: 'Asset Issue',
+ processor: processAssetIssueTransaction
+ };
+ TRANSACTION_SPEC[constants.ASSET_TRANSFER_TRANSACTION_TYPE] = {
+ type: 'Transfer',
+ processor: processAssetTransferTransaction
+ };
+ TRANSACTION_SPEC[constants.ASSET_REISSUE_TRANSACTION_TYPE] = {
+ type: 'Asset Reissue',
+ processor: processAssetReissueTransaction
};
function buildTransactionType (number) {
- return TRANSACTION_TYPES[number] || '';
+ var spec = TRANSACTION_SPEC[number];
+
+ return spec ? spec.type : '';
}
function transformAddress(rawAddress) {
@@ -22,34 +35,68 @@
return result;
}
+ function processTransaction(transaction) {
+ var spec = TRANSACTION_SPEC[transaction.type];
+ if (angular.isUndefined(spec) || angular.isUndefined(spec.processor))
+ return;
+
+ spec.processor(transaction);
+ }
+
+ function processPaymentTransaction(transaction) {
+ transaction.formatted.amount = Money.fromCoins(transaction.amount, Currency.WAV).formatAmount();
+ }
+
+ function processAssetIssueTransaction(transaction) {
+ var asset = new Currency({
+ id: transaction.id,
+ displayName: transaction.name,
+ precision: transaction.decimals
+ });
+ transaction.formatted.amount = Money.fromCoins(transaction.quantity, asset).formatAmount();
+ }
+
+ function processAssetTransferTransaction(transaction) {
+
+ }
+
+ function processAssetReissueTransaction(transaction) {
+ var asset = applicationContext.cache.assets[transaction.assetId];
+ if (angular.isUndefined(asset))
+ return;
+
+ transaction.formatted.amount = Money.fromCoins(transaction.quantity, asset.currency).formatAmount();
+ }
+
+ function formatFee(transaction) {
+ var currency = Currency.WAV;
+ var assetId = transaction.assetId;
+ if (angular.isDefined(assetId)) {
+ var asset = applicationContext.cache.assets[assetId];
+ if (angular.isDefined(asset))
+ currency = asset.currency;
+ }
+
+ return Money.fromCoins(transaction.fee, currency).formatAmount(true);
+ }
+
function formatTransaction(transaction) {
// in the future currency should be a part of transaction itself
- var currency = Currency.WAV;
var currentAddress = applicationContext.account.address;
var type = transaction.sender === currentAddress ? 'Outgoing' : 'Incoming';
- var formattedAmount = 'N/A';
- if (angular.isDefined(transaction.amount))
- formattedAmount = Money.fromCoins(transaction.amount, currency).formatAmount();
- if (angular.isDefined(transaction.quantity)) {
- var asset = new Currency({
- id: transaction.id,
- displayName: transaction.name,
- precision: transaction.decimals
- });
- formattedAmount = Money.fromCoins(transaction.quantity, asset).formatAmount();
- }
- var fee = Money.fromCoins(transaction.fee, currency);
transaction.formatted = {
type: type + ' ' + buildTransactionType(transaction.type),
datetime: formattingService.formatTimestamp(transaction.timestamp),
isOutgoing: transaction.sender === currentAddress,
sender: transformAddress(transaction.sender),
recipient: transformAddress(transaction.recipient),
- amount: formattedAmount,
- fee: fee.formatAmount(true)
+ amount: 'N/A',
+ fee: formatFee(transaction)
};
+ processTransaction(transaction);
+
transaction.formatted.isSenderCopiable = addressService.validateAddress(transaction.formatted.sender);
transaction.formatted.isRecipientCopiable = addressService.validateAddress(transaction.formatted.recipient);
@@ -61,7 +108,7 @@
};
}
- TransactionFilter.$inject = ['applicationContext', 'formattingService', 'addressService'];
+ TransactionFilter.$inject = ['constants.transactions', 'applicationContext', 'formattingService', 'addressService'];
angular
.module('app.shared')
@@ -213,9 +213,13 @@
});
}
+ function fillUpTransactionCache (transactions) {
+ applicationContext.cache.assets.grab(transactions);
+ }
+
function loadDataFromBackend() {
refreshWallets();
- refreshTransactions();
+ refreshTransactions(fillUpTransactionCache);
refreshPromise = $interval(function() {
refreshWallets();
@@ -234,9 +238,12 @@
});
}
- function refreshTransactions() {
+ function refreshTransactions(transactionHandler) {
transactionLoadingService.loadTransactions(applicationContext.account.address)
.then(function (transactions) {
+ if (angular.isDefined(transactionHandler))
+ transactionHandler(transactions);
+
wallet.transactions = transactions;
});
}

0 comments on commit d1ee486

Please sign in to comment.