import { Component, OnInit, ViewEncapsulation, AfterViewInit } from '@angular/core'; import { DetailsService } from '../details.service'; import Web3 from 'web3'; import { ToastrService } from 'ngx-toastr'; import { ScriptLoaderService } from '../../../../_services/script-loader.service'; import { GlobalsComponent } from '../globals/globals.component'; declare var $: any; @Component({ selector: '.m-grid__item.m-grid__item--fluid.m-wrapper', templateUrl: './index.component.html', styleUrls: ['./index.component.css'], encapsulation: ViewEncapsulation.None }) export class IndexComponent implements OnInit, AfterViewInit { public web3; public poolBalance; public proposalDetails = []; public GBTBalance; public memberAddress; public userAddress; public memberRoleAddress; public memberRoleAbi; public masterAbi; public rewards; public userObj; public noProposals = false; public reputation; public tokenBalance; public userBalance; public tokenPrice; public amount; public totalProposals = 1; public metamaskLogin = true; public companyMasterAddress; public dAppTokenBalance; public iframeURl = ''; public draftProposals; public pendingProposals; public acceptedProposals; public rejectedProposals; public show = true; constructor( private _script: ScriptLoaderService, private toastr: ToastrService, private detailService: DetailsService, private globals: GlobalsComponent ) { if (window.location.href.indexOf('iframe') >= 0) { this.iframeURl = '/iframe'; this.show = false; } } ngOnInit() { console.log('Init'); this.web3 = new Web3(window['web3'].currentProvider); if (this.web3.eth.accounts[0] == undefined) { this.metamaskLogin = false; this.memberAddress = '0x0000000000000000000000000000000000000000'; } else this.memberAddress = this.web3.eth.accounts[0]; let user = localStorage.getItem('currentUser'); this.userObj = JSON.parse(user); this.indexInitiate(); } indexInitiate() { var urlPath = window.location.href; var index = urlPath.lastIndexOf('/'); var urlCompanyName = urlPath.substring(index + 1, urlPath.length); console.log(urlCompanyName); if (this.userObj.companyName != urlCompanyName) { var data = { masterAddress: '', userName: this.userObj.userName, companyName: urlCompanyName, GBMasterAddress: this.userObj.GBMasterAddress, allAbis: this.userObj.allAbis, email: this.userObj.email, token: 'token', authorized: true }; localStorage.setItem('currentUser', JSON.stringify(data)); import('../globals/globals.component').then(module => { module.generator(); }); let user1 = localStorage.getItem('currentUser'); this.userObj = JSON.parse(user1); console.log(this.userObj); var timeoutVar1 = setInterval( function() { console.log('insideTimeout'); let user = localStorage.getItem('currentUser'); this.userObj = JSON.parse(user); if (this.userObj.companyName in GlobalsComponent.governance) { GlobalsComponent.gbmaster.isMemberAuthorized( this.userObj.companyName, this.memberAddress, function(errorMR1, resultMR1) { var authorized = false; if (resultMR1.authorized) { authorized = true; } this.userObj.authorized = authorized; var user = localStorage.getItem('currentUser'); this.userObj = JSON.parse(user); var data = { masterAddress: this.companyMasterAddress, userName: this.userObj.userName, companyName: urlCompanyName, GBMasterAddress: this.userObj.GBMasterAddress, allAbis: this.userObj.allAbis, email: this.userObj.email, token: 'token', authorized: authorized }; localStorage.setItem('currentUser', JSON.stringify(data)); var user = localStorage.getItem('currentUser'); this.userObj = JSON.parse(user); this.web3.eth.defaultAccount = this.web3.eth.coinbase; this.memberAddress = this.memberAddress; this.userAddress = this.truncate(this.memberAddress, 14, '...'); this.detailService.setTitle( this.userObj.companyName + ' Dashboard' ); console.log('Entering --- assets/app/js/dashboard.js'); this._script.load( '.m-grid__item.m-grid__item--fluid.m-wrapper', 'assets/app/js/dashboard.js' ); this.getInitialAddress(); }.bind(this) ); clearInterval(timeoutVar1); } }.bind(this), 500 ); } else { console.log(this.web3); this.web3.eth.defaultAccount = this.web3.eth.coinbase; this.memberAddress = this.memberAddress; this.userAddress = this.truncate(this.memberAddress, 14, '...'); this.detailService.setTitle(this.userObj.companyName + ' Dashboard'); import('../globals/globals.component').then(module => { module.generator(); }); var timeoutVar1 = setInterval( function() { console.log('insideTimeout'); if (this.userObj.companyName in GlobalsComponent.proposalData) { this.getInitialAddress(); clearInterval(timeoutVar1); } }.bind(this), 500 ); } } getInitialAddress() { this.getProposalLength(); this.initialCalls(); this.detailService.setdAppName(this.userObj.companyName); GlobalsComponent.gbmaster.getdAppContractAddresses( this.userObj.companyName, function(error, result) { this.poolAddress = result['Pool']; this.detailService.setPoolAddress(result['Pool']); }.bind(this) ); } ngAfterViewInit() { this._script.load( '.m-grid__item.m-grid__item--fluid.m-wrapper', 'assets/app/js/dashboard.js' ); this.iframeResize(); } public proposalLength = 1; public poolAddress; getProposalLength() { GlobalsComponent.proposalData[this.userObj.companyName].getProposalLength( function(errPropoLen, resPropLen) { if (!errPropoLen) { this.proposalLength = resPropLen - 1; this.totalProposals = resPropLen; this.getProposals(); } else { console.log('Error in getProposalLength'); } }.bind(this) ); } public balance; public gbTokenPrice = 0; public decimals = {}; public authorizedCompany; public tokenName; public pooldAppTokenBalance; public gbtRewards = 0; initialCalls() { this.getProposalStats(); GlobalsComponent.gbmaster.getdAppTokenDecimals( 'GOVBLOCKS', function(errGBTdecimals, resGBTdecimals) { this.decimals['GOVBLOCKS'] = resGBTdecimals; }.bind(this) ); GlobalsComponent.gbmaster.getdAppTokenDecimals( this.userObj.companyName, function(errTokendecimals, resTokendecimals) { this.decimals[this.userObj.companyName] = resTokendecimals; this.getPendingRewards(); GlobalsComponent.governance[ this.userObj.companyName ].getTotalRewardsByDapp( function(error, result) { this.rewards = result / Math.pow(10, this.decimals[this.userObj.companyName]); this.rewards = this.rewards.toFixed(2); }.bind(this) ); }.bind(this) ); let user = localStorage.getItem('currentUser'); this.userObj = JSON.parse(user); GlobalsComponent.gbmaster.getPoolBalanceinETH( this.userObj.companyName, function(errorPoolBal, resPoolBal) { this.poolBalance = (resPoolBal.toNumber() / 1e18).toFixed(2); }.bind(this) ); GlobalsComponent.gbmaster.getBalance_user( this.memberAddress, this.userObj.companyName, function(errBal, resBal) { if (resBal.e < 10) { this.balance = Math.round(resBal * 100) / 100; } else { this.balance = Math.round((resBal / 1e18) * 100) / 100; } }.bind(this) ); GlobalsComponent.gbmaster.getGBTokenPrice( function(errTokenPrice, resTokenPrice) { this.gbTokenPrice = resTokenPrice / 1e18; }.bind(this) ); GlobalsComponent.gbmaster.getdAppTokenAddress( this.userObj.companyName, function(err, res) { GlobalsComponent.gbmaster.getdAppTokenDetails( res, function(errName, resName) { this.tokenName = resName[2]; if (this.tokenName == null) this.tokenName = ''; this.tokenPrice = resName[1] / 1e18; this.tokenBalance = this.balance + ' ' + this.tokenName; this.dAppTokenBalance = this.balance + ' ' + this.tokenName; }.bind(this) ); }.bind(this) ); GlobalsComponent.gbmaster.getPooldAppTokenBalance( this.userObj.companyName, function(error, result) { this.pooldAppTokenBalance = (result / 1e18).toFixed(2); }.bind(this) ); GlobalsComponent.gbmaster.getGBTBalance_user( this.memberAddress, function(errorBalance, resBalance) { if (resBalance > 1e10) { this.userBalance = (resBalance / 1e18).toFixed(2); } else { this.userBalance = resBalance; } }.bind(this) ); GlobalsComponent.gbmaster.getBalance_pool( this.userObj.companyName, function(errorBalance, resBalance) { if (resBalance > 1e10) { this.GBTBalance = resBalance / 1e18; } else { this.GBTBalance = resBalance; } }.bind(this) ); GlobalsComponent.memberData[this.userObj.companyName].getMemberDetails( this.memberAddress, function(errRep, resRep) { this.reputation = resRep[0]; }.bind(this) ); } public pendingRewards; public pendingGBTRewards; public totalPendingRewards; getPendingRewards() { GlobalsComponent.governance[this.userObj.companyName].getPendingReward( this.memberAddress, function(error, result) { this.pendingGBTRewards = Math.round( (result[0] / Math.pow(10, this.decimals['GOVBLOCKS'])) * 100 ); this.pendingRewards = result[1] / Math.pow(10, this.decimals[this.userObj.companyName]); this.pendingRewards = Math.round(this.pendingRewards * 100) / 100; this.totalPendingRewards = this.pendingGBTRewards + this.pendingRewards; }.bind(this) ); } public stakeAmount; public categoryData; public closingTime; public incentive; public loadingCreate = false; resetValues() { this.tokenPrice = null; this.amount = null; this.stakeAmount = null; } Reset() { $('div').removeClass('has-success'); $('div').removeClass('has-danger'); $('.form-control-feedback').remove(); } public message; public actualTokenPrice; transferETHtoPool() { this.detailService.setPoolAddress(this.poolAddress); // this.detailService.setTokenPrice(this.gbTokenPrice); // this.detailService.setPoolBalance(this.poolBalance); $('#buyETHpool').modal('show'); } buyGBT() { this.detailService.setTokenPrice(this.gbTokenPrice); this.detailService.setUserTokenBalance(this.userBalance); $('#buyGBTPool').modal('show'); } transferGBTtoPool() { this.detailService.setPoolAddress(this.poolAddress); this.detailService.setUserTokenBalance(this.userBalance); this.detailService.setPoolTokenBalance(this.GBTBalance); $('#transferGBTtoPool').modal('show'); } transferdAppTokens() { this.detailService.setPoolAddress(this.poolAddress); this.detailService.setUserTokenBalance(this.balance); this.detailService.setPoolTokenBalance(this.pooldAppTokenBalance); this.detailService.setTokenName(this.tokenName); $('#transferdAppToken').modal('show'); } timeout1(hash) { setTimeout(() => { var boundFunction = this.web3.eth.getTransactionReceipt( hash, function(error, result) { if (result != null) { setTimeout(() => { this.toastr.success('GBT Token purchased successfully!'); this.loadingCreate = false; }, 4000); } else this.timeout1(hash); }.bind(this) ); }, 10 * 1000); } truncate(fullStr, strLen, separator) { if (fullStr.length <= strLen) return fullStr; separator = separator || '...'; var sepLen = separator.length, charsToShow = strLen - sepLen, frontChars = Math.ceil(charsToShow / 2), backChars = Math.floor(charsToShow / 2); return ( fullStr.substr(0, frontChars) + separator + fullStr.substr(fullStr.length - backChars) ); } getProposals() { var j = 3; for (let i = this.totalProposals - 1; i <= j ? i > 0 : j >= 1; i--, j--) { this.getProposalDetails(i); } } getProposalDetails(proposalId) { GlobalsComponent.proposalData[ this.userObj.companyName ].getProposalEventData( proposalId, function(errorDesc, resultDesc) { var hashDetails = resultDesc.proposalSD; var title = resultDesc.proposalTitle; var description = resultDesc.proposalSD; var d = new Date(resultDesc.dateAdd * 1000); var options = { year: 'numeric', month: 'short', day: 'numeric' }; var daysDiff = d.toLocaleDateString('en', options); var statusid = resultDesc.statusId; var status; var style; if (statusid < 1) { status = 'draft'; style = 'metal'; } else if (statusid == 1) { status = 'awaiting solutions'; style = 'orange'; } else if (statusid == 2) { status = 'voting started'; style = 'warning'; } else if (statusid == 3 || statusid == 5 || statusid == 7) { status = 'accepted'; style = 'success'; } else if (statusid == 4) { status = 'rejected'; style = 'danger'; } else if (statusid == 6) { status = 'denied'; style = 'danger'; } var propDetails = { id: proposalId, title: title, description: description, date: daysDiff, status: status, style: style }; this.proposalDetails.push(propDetails); if ( this.proposalDetails.length >= 3 || this.proposalDetails.length == this.totalProposals - 1 ) { this.proposalDetails.sort(this.predicateBy('id')); $('#loader').hide(); } }.bind(this) ); } public loader1 = true; getProposalStats() { GlobalsComponent.proposalData[ this.userObj.companyName ].getStatusOfProposals( function(errorProposalStats, resProposalStatus) { this.totalProposals = resProposalStatus[0].toNumber(); this.proposalLength = this.totalProposals - 1; if (this.totalProposals < 1) { this.draftProposals = 0; this.awaitingSolution = 0; this.pendingProposals = 0; this.acceptedProposals = 0; this.rejectedProposals = 0; this.noProposals = true; $('#loader1').hide(); $('#loader').hide(); this.loader1 = false; } else { this.draftProposals = ( ((resProposalStatus[1].toNumber() - 1) / this.proposalLength) * 100 ).toFixed(2); this.awaitingSolution = ( (resProposalStatus[2].toNumber() / this.proposalLength) * 100 ).toFixed(2); this.pendingProposals = ( (resProposalStatus[3].toNumber() / this.proposalLength) * 100 ).toFixed(2); this.acceptedProposals = ( (resProposalStatus[4].toNumber() / this.proposalLength) * 100 ).toFixed(2); this.rejectedProposals = ( (resProposalStatus[5].toNumber() / this.proposalLength) * 100 ).toFixed(2); $('#loader1').hide(); this.loader1 = false; } }.bind(this) ); } //method for sorting the object predicateBy(prop) { return function(a, b) { if (a[prop] < b[prop]) { return 1; } else if (a[prop] > b[prop]) { return -1; } return 0; }; } claimRewards() { this.loadingClaim = true; setTimeout(() => { GlobalsComponent.governance[this.userObj.companyName].claimPendingRewards( this.memberAddress, function(error, result) { if (!error) { this.toastr.info( 'Submitted to ethereum(' + GlobalsComponent.network + '). Awaiting confirmation!' ); this.timeoutClaimReward(result); } else { this.loadingClaim = false; } }.bind(this) ); }, 200); } public loadingClaim; timeoutClaimReward(hash) { setTimeout(() => { var boundFunction = this.web3.eth.getTransactionReceipt( hash, function(error, result) { if (result != null) { setTimeout(() => { this.toastr.success('Reward claim successfull!'); this.loadingClaim = false; this.ngOnInit(); }, 4000); } else this.timeoutClaimReward(hash); }.bind(this) ); }, 1000); } iframeResize() { import('../globals/globals.component').then(module => { module.iframe_resize(); }); } } export class IndexIframeComponent extends IndexComponent {}