<commit_before>angular.module('starter.services', [])

.factory('Account', function ($rootScope, UIHelper, Settings, Remote) {
	var account;
	var keysChanged = false;
	var connectionChanged = false;
	account = {
		address : 'loading',
		balance : 0,
		reserve : 20,
		transactions : []
	};

	var transactionFilter = function(msg){
		return (msg.engine_result_code == 0 && msg.type === 'transaction');
	};
	var transactionCallback = function(msg){
		account.transactions.unshift(msg.transaction);
		$rootScope.$broadcast('accountInfoLoaded');
	}
	Remote.addMessageHandler(transactionFilter, transactionCallback);
	
	var paymentFilter = function(msg){
		return (transactionFilter(msg) && msg.transaction.TransactionType === 'Payment')
	};
	var paymentCallback = function(msg){
		if (msg.transaction.Destination === account.address) {
			if (!msg.transaction.Amount.issuer) {
				console.log('payment received: ' + msg.transaction.Amount / 1000000 + ' STR');
				account.balance += msg.transaction.Amount / 1000000;
			} else {
				console.log('payment received: ' + msg.transaction.Amount.value + ' ' + msg.transaction.Amount.currency);
			}
		} 
		else if (msg.transaction.Account === account.address) {
			if (!msg.transaction.Amount.issuer) {
				console.log('payment sent: ' + msg.transaction.Amount / 1000000 + ' STR');
				account.balance -= msg.transaction.Amount / 1000000;
			} else {
				console.log('payment sent: ' + msg.transaction.Amount.value + ' ' + msg.transaction.Amount.currency);
			}
			UIHelper.blockScreen('Payment successful!', 1);
		}
	};	
	Remote.addMessageHandler(paymentFilter, paymentCallback);

	var successFilter = function(msg){
		return (msg.status === 'success' && msg.type === 'response' && msg.result);
	};
	var successCallback = function(msg){
		if (msg.result.account_data) {
			var newData = msg.result.account_data;
			account.balance = Math.round(newData.Balance / 1000000);
		}
		else if (msg.result.master_seed) {
			var newKeys = msg.result;
			console.log(newKeys.account_id + ': ' + newKeys.master_seed);
			Settings.setKeys(newKeys.account_id, newKeys.master_seed);
		} else if (msg.result.transactions) {
			var transactions = msg.result.transactions;
			for (index = 0; index < transactions.length; ++index) {
				account.transactions.push(transactions[index].tx);
			}
		}
		$rootScope.$broadcast('accountInfoLoaded');
	};
	Remote.addMessageHandler(successFilter, successCallback);
	
	var attachToKeys = function(){
		var keys = Settings.getKeys();
		account.address = keys.address;
		// initial balance
		var data = {
			command : 'account_info',
			account : keys.address
		};
		Remote.send(data);

		//initial transactions
		var data = {
			command : 'account_tx',
			account : keys.address,
			limit : 30
		};
		Remote.send(data);

		// subscribe for updates
		data = {
			command : 'subscribe',
			accounts : [keys.address]
		};<commit_msg>do not display failed transactions<commit_after>angular.module('starter.services', [])

.factory('Account', function ($rootScope, UIHelper, Settings, Remote) {
	var account;
	var keysChanged = false;
	var connectionChanged = false;
	account = {
		address : 'loading',
		balance : 0,
		reserve : 20,
		transactions : []
	};

	var transactionFilter = function(msg){
		return (msg.engine_result_code == 0 && msg.type === 'transaction');
	};
	var transactionCallback = function(msg){
		account.transactions.unshift(msg.transaction);
		$rootScope.$broadcast('accountInfoLoaded');
	}
	Remote.addMessageHandler(transactionFilter, transactionCallback);
	
	var paymentFilter = function(msg){
		return (transactionFilter(msg) && msg.transaction.TransactionType === 'Payment')
	};
	var paymentCallback = function(msg){
		if (msg.transaction.Destination === account.address) {
			if (!msg.transaction.Amount.issuer) {
				console.log('payment received: ' + msg.transaction.Amount / 1000000 + ' STR');
				account.balance += msg.transaction.Amount / 1000000;
			} else {
				console.log('payment received: ' + msg.transaction.Amount.value + ' ' + msg.transaction.Amount.currency);
			}
		} 
		else if (msg.transaction.Account === account.address) {
			if (!msg.transaction.Amount.issuer) {
				console.log('payment sent: ' + msg.transaction.Amount / 1000000 + ' STR');
				account.balance -= msg.transaction.Amount / 1000000;
			} else {
				console.log('payment sent: ' + msg.transaction.Amount.value + ' ' + msg.transaction.Amount.currency);
			}
			UIHelper.blockScreen('Payment successful!', 1);
		}
	};	
	Remote.addMessageHandler(paymentFilter, paymentCallback);

	var successFilter = function(msg){
		return (msg.status === 'success' && msg.type === 'response' && msg.result);
	};
	var successCallback = function(msg){
		if (msg.result.account_data) {
			var newData = msg.result.account_data;
			account.balance = Math.round(newData.Balance / 1000000);
		}
		else if (msg.result.master_seed) {
			var newKeys = msg.result;
			console.log(newKeys.account_id + ': ' + newKeys.master_seed);
			Settings.setKeys(newKeys.account_id, newKeys.master_seed);
		} else if (msg.result.transactions) {
			var transactions = msg.result.transactions;
			for (index = 0; index < transactions.length; ++index) {
				var currentTrx = transactions[index];
				if(currentTrx.meta && currentTrx.meta.TransactionResult === 'tesSUCCESS')
					account.transactions.push(currentTrx.tx);
			}
		}
		$rootScope.$broadcast('accountInfoLoaded');
	};
	Remote.addMessageHandler(successFilter, successCallback);
	
	var attachToKeys = function(){
		var keys = Settings.getKeys();
		account.address = keys.address;
		// initial balance
		var data = {
			command : 'account_info',
			account : keys.address
		};
		Remote.send(data);

		//initial transactions
		var data = {
			command : 'account_tx',
			account : keys.address,
			limit : 30
		};
		Remote.send(data);

		// subscribe for updates
		data = {
			command : 'subscribe',
			accounts : [keys.address]
		};
{'fraction_non_alphanumeric': 0.14563270822774116, 'fraction_numerical': 0.014529481331305964, 'mean_word_length': 4.034013605442177, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3476226', 'n_tokens_mistral': 2087, 'n_tokens_neox': 1779, 'n_words': 354}