import { Component, OnInit, ViewEncapsulation, AfterViewInit } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { Helpers } from '../../../../../helpers'; import { ScriptLoaderService } from '../../../../../_services/script-loader.service'; import { GlobalsComponent } from '../../globals/globals.component'; import Web3 from 'web3'; import { DetailsService } from '../../details.service'; declare var $: any; @Component({ selector: '.m-grid__item.m-grid__item--fluid.m-wrapper', templateUrl: './rewards-ledger.component.html', encapsulation: ViewEncapsulation.None }) export class RewardsLedgerComponent implements OnInit, AfterViewInit { public web3; public memberAddress; public masterAddresses = []; public companyNameArray = []; public finalLedger = []; public rewardLedger = []; public dAppNameArray = []; public masterDappArray = {}; public dAppMasterMap = {}; public dAppRecords = {}; public dAppArray = []; public cSize; public cCount = 0; public noRecord = false; public userObj; public metamaskLogin = true; constructor( private _script: ScriptLoaderService, private detailService: DetailsService, private globals: GlobalsComponent, private router: Router ) {} ngOnInit() { this.detailService.setTitle('Rewards Ledger'); $('#filters').hide(); let user = localStorage.getItem('currentUser'); this.userObj = JSON.parse(user); this.web3 = new Web3(window['web3'].currentProvider); this.web3.eth.defaultAccount = this.web3.eth.coinbase; if (this.web3.eth.accounts[0] == undefined) { this.metamaskLogin = false; this.memberAddress = '0x0000000000000000000000000000000000000000'; } else { this.memberAddress = this.web3.eth.accounts[0]; } import('../../globals/globals.component').then(module => { module.generator(); }); var timeoutVar = setInterval( function() { if (GlobalsComponent.gbmaster) { this.getInitialAddress(); clearInterval(timeoutVar); } }.bind(this), 500 ); } getInitialAddress() { var length; GlobalsComponent.gbmaster.getAlldAppRegistered( function(error, result1) { length = result1.length; var i = 0; var companyName; this.cSize = length; if (length == 0) this.demo(); for (i = 0; i < length; i++) { var index = i; this.dAppNameArray.push(this.hex_to_ascii(result1[index])); GlobalsComponent.gbmaster.getdAppDetails( this.dAppNameArray[index], function(error, result) { this.companyMasterAddress = result[1]; if ( result[5] > 0 && !this.hex_to_ascii(result[0]).startsWith('TEST') ) { this.masterAddresses.push(this.companyMasterAddress); this.dAppArray.push(this.hex_to_ascii(result[0])); var cname = this.hex_to_ascii(result[0]); this.companyNameArray.push({ name: cname }); this.masterDappArray[this.companyMasterAddress] = cname; this.dAppMasterMap[cname] = this.companyMasterAddress; if (this.masterAddresses.length == this.cSize) this.initialCalls(cname); } else { this.cSize--; if (this.masterAddresses.length == this.cSize) this.initialCalls(cname); } }.bind(this) ); } }.bind(this) ); } initialCalls(companyName) { for (let i = 0; i < this.masterAddresses.length; i++) { this.masterCall(companyName, i); } } masterCall(companyName, i) { var cName = this.masterDappArray[this.masterAddresses[i]]; GlobalsComponent.gbmaster.getRewardLedgerData( cName, this.memberAddress, function(error, result) { this.rewardLedger = this.rewardLedger.concat(result); this.cCount++; if (this.cCount == this.cSize) this.demo(); }.bind(this) ); } hex_to_ascii(str1) { var hex = str1.toString(); var str = ''; for (var n = 0; n < hex.length; n += 2) { str += String.fromCharCode(parseInt(hex.substr(n, 2), 16)); } str1 = ''; for (var i = 0; i < str.length; i++) { if (str.charCodeAt(i) != 0) { str1 += str[i]; } } return str1; } ngAfterViewInit() {} predicateBy(prop) { return function(a, b) { if (a[prop] > b[prop]) { return 1; } else if (a[prop] < b[prop]) { return -1; } return 0; }; } public balance = 0; onSelectProposal(obj) { GlobalsComponent.gbmaster.isMemberAuthorized( obj.cname, this.memberAddress, function(errorMR, resultMR) { var authorized = true; if (!resultMR.authorized) { authorized = false; } var masterAddress = this.dAppMasterMap[obj.cname]; let user = localStorage.getItem('currentUser'); var userObj = JSON.parse(user); var data = { masterAddress: masterAddress, userName: userObj.userName, companyName: obj.cname, GBMasterAddress: userObj.GBMasterAddress, allAbis: userObj.allAbis, email: userObj.email, token: 'token', authorized: authorized }; localStorage.setItem('currentUser', JSON.stringify(data)); import('../../globals/globals.component').then(module => { module.setDappInstance(obj.cname); }); this.router.navigate([ '/components/proposals/' + obj.cname + '/' + obj.proposalId ]); }.bind(this) ); } demo() { this.rewardLedger.sort(this.predicateBy('timestamp')); var i = -1; var balance = 0; while (i < this.rewardLedger.length) { if (i == -1) i++; if (this.rewardLedger.length > 0) { // if (i == 0) { // balance = this.rewardLedger[i].reward + this.rewardLedger[i].penalty; // } // else { balance = balance + this.rewardLedger[i].dAppReward; // } this.finalLedger.push({ proposalId: this.rewardLedger[i].proposalId, // blockNumber: this.rewardLedger[i].blockNumber, // type: this.rewardLedger[i].type, // penalty: 0, id: this.finalLedger.length + 1, // totalReward: balance, date: this.rewardLedger[i].date, proposalName: this.rewardLedger[i].title, desc: this.rewardLedger[i].description, // reward: this.rewardLedger[i].dAppReward, cname: this.rewardLedger[i].dAppName }); i++; if (this.finalLedger.length == this.rewardLedger.length) { $('#filters').show(); $('#loader').hide(); var datatable = $('.m-datatable-rewards').mDatatable({ // datasource definition data: { type: 'local', source: this.finalLedger, pageSize: 10, saveState: { cookie: true, webstorage: true } }, // layout definition layout: { theme: 'default', // datatable theme class: '', // custom wrapper class scroll: true, // enable/disable datatable scroll both horizontal and vertical when needed. // height: 550, // datatable's body's fixed height footer: false // display/hide footer }, // column sorting sortable: false, pagination: true, search: { input: $('#generalSearch') }, // columns definition columns: [ { field: 'id', title: 'S No.', sortable: true, textAlign: 'center', width: 40 }, { field: 'cname', title: 'Company Name', width: 150, sortable: false }, { field: 'date', title: 'Date', sortable: false }, { field: 'proposalName', title: 'Proposal', width: 150, sortable: false }, { field: 'desc', title: 'Description', width: 150, sortable: false }, // { // field:"finalVerdict", // title:"Final Verdict", // sortable:false // }, // { // field: 'reward', // title: 'Reward', // textAlign: 'center', // width: 60, // sortable: false // }, // { // field: 'penalty', // title: 'Penalty', // textAlign: 'center', // width: 60, // sortable: false // }, // { // field: 'totalReward', // title: 'Total Reward', // textAlign: 'center', // width: 100, // sortable: false // }, { field: 'proposalId', title: '', width: 25, sortable: false, template: function(row) { return ( '
' ); } } ] }); $('.m-datatable-rewards').on( 'click', '.rbtn', function(args, err) { this.onSelectProposal( this.finalLedger[args.currentTarget.value - 1] ); }.bind(this) ); var query = datatable.getDataSourceQuery(); var type = { r: { title: 'Reward' }, p: { title: 'Penalty' } }; $('#m_form_Dapp') .on('change', function() { datatable.search($(this).val(), 'cname'); }) .val(typeof query.cname !== 'undefined' ? query.cname : ''); $('#m_form_type') .on('change', function() { datatable.search($(this).val(), 'type'); }) .val(typeof query.type !== 'undefined' ? query.type : ''); $('#m_form_Dapp, #m_form_type').selectpicker(); } } else { $('#loader').hide(); this.noRecord = true; } } } }