import { Component, OnInit, ViewEncapsulation, AfterViewInit } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import Web3 from 'web3'; import { ScriptLoaderService } from '../../../../../_services/script-loader.service'; import { DetailsService } from '../../details.service'; import { ToastrService } from 'ngx-toastr'; import { GlobalsComponent } from '../../globals/globals.component'; declare var $: any; @Component({ selector: '.m-grid__item.m-grid__item--fluid.m-wrapper', templateUrl: './locking.component.html', encapsulation: ViewEncapsulation.None }) export class LockingComponent implements OnInit, AfterViewInit { public userObj; public authMember; public web3; public memberAddress; public metamaskLogin = true; constructor( private toastr: ToastrService, private _script: ScriptLoaderService, private details: DetailsService, private router: Router, private route: ActivatedRoute, private globals: GlobalsComponent ) {} ngOnInit() { let user = localStorage.getItem('currentUser'); this.userObj = JSON.parse(user); this.authMember = this.userObj.authorized; 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]; } this.indexInitiate(); } indexInitiate() { var urlPath = window.location.href; var index = urlPath.lastIndexOf('/'); var urlCompanyName = urlPath.substring(index + 1, urlPath.length); 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)); let user1 = localStorage.getItem('currentUser'); this.userObj = JSON.parse(user1); console.log(this.userObj); import('../../globals/globals.component').then(module => { module.generator(); }); this.details.setTitle('Token Management'); GlobalsComponent.gbmaster.getdAppDetails( this.userObj.companyName, function(error, result) { this.versionNum = result[5].toNumber(); this.companyMasterAddress = result[1]; this.userObj.masterAddress = this.companyMasterAddress; var version; GlobalsComponent.gbmaster.isMemberAuthorized( this.userObj.companyName, this.memberAddress, function(errorMR1, resultMR1) { var authorized = false; if (resultMR1.authorized) { authorized = true; } this.userObj.authorized = authorized; this.authMember = this.userObj.authorized; var mrAbi = this.userObj.allAbis[2]; var timeoutVar1 = setInterval( function() { console.log('insideTimeout'); let user = localStorage.getItem('currentUser'); this.userObj = JSON.parse(user); if (this.userObj.companyName in GlobalsComponent.governance) { 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)); this.initialCalls(); clearInterval(timeoutVar1); } }.bind(this), 500 ); }.bind(this) ); }.bind(this) ); } else { this.details.setTitle('Token Management'); import('../../globals/globals.component').then(module => { module.generator(); }); var timeoutVar1 = setInterval( function() { console.log('insideTimeout'); if (this.userObj.companyName in GlobalsComponent.governance) { this.initialCalls(); clearInterval(timeoutVar1); } }.bind(this), 500 ); } } initialCalls() { GlobalsComponent.governance[ this.userObj.companyName ].getTotalGBTBalance_user( this.memberAddress, function(errGBTBalance, resGBTBalance) { this.gbtBalance = Math.round((resGBTBalance / 1e18) * 100) / 100; }.bind(this) ); this.getTokenDetails(this.userObj.companyName); this.getTokenDetails(this.gb); } public tokenName = {}; public tokenBalance = {}; public transferrableBalance = {}; public tokensLocked = {}; public lockedUpto = {}; public validity = {}; public unlockableTokens = {}; public gb = 'GOVBLOCKS'; public validityTimeStamp = {}; getTokenDetails(companyName) { GlobalsComponent.gbmaster.getdAppTokenName( companyName, function(errTokenName, resTokenName) { this.tokenName[companyName] = resTokenName; }.bind(this) ); GlobalsComponent.governance[this.userObj.companyName].getTotalBalance_user( this.memberAddress, companyName, function(errTokenBalance, resTokenBalance) { this.tokenBalance[companyName] = Math.round((resTokenBalance / 1e18) * 100) / 100; GlobalsComponent.governance[this.userObj.companyName].getTokensLocked( companyName, this.memberAddress, 'GOV', function(errTokensLocked, resultTokensLocked) { this.tokensLocked[companyName] = Math.round((resultTokensLocked / 1e18) * 100) / 100; this.transferrableBalance[companyName] = this.tokenBalance[companyName] - this.tokensLocked[companyName]; }.bind(this) ); }.bind(this) ); GlobalsComponent.governance[ this.userObj.companyName ].getTokensLockedValidity( companyName, this.memberAddress, 'GOV', function(errValidity, resValidity) { if (resValidity[2] == true) { resValidity[1] = 0; } this.validityTimeStamp[companyName] = resValidity[1]; var date = new Date(resValidity[1] * 1000); var options = { year: 'numeric', month: 'short', day: 'numeric' }; if (resValidity[1] == 0) { this.validity[companyName] = 'N/A'; var date = new Date(); this.now = this.getFormattedDate(date); this.lockedUpto[companyName] = this.now; } else { this.validity[companyName] = date.toLocaleDateString('en', options); var date = new Date(resValidity[1] * 1000); this.lockedUpto[companyName] = this.getFormattedDate(date); } }.bind(this) ); GlobalsComponent.governance[this.userObj.companyName].getUnlockableTokens( companyName, this.memberAddress, 'GOV', function(errUnlockableTokens, resUnlockableTokens) { this.unlockableTokens[companyName] = Math.round((resUnlockableTokens / 1e18) * 100) / 100; }.bind(this) ); } public dAppName; public type; public now; openModal(type, dAppName) { this.type = type; this.dAppName = dAppName; this.recepientsAddress = null; this.amount = null; this.time = null; var date; if (this.type == 'Lock') { date = new Date(); this.now = this.getFormattedDate(date); } else if (this.type == 'ExtendValidity') { date = new Date(); this.now = this.getFormattedDate(date); } $('#m_modal_5').modal('show'); } submit() { this.loading = true; if (this.type == 'Transfer') { if (this.transferrableBalance[this.dAppName] >= this.amount) { this.transferTokens(); } else { this.toastr.warning('Insufficient amount for transfer.'); this.loading = false; } } else if (this.type == 'Lock') { if (this.transferrableBalance[this.dAppName] >= this.amount) { this.lockTokens(); } else { this.toastr.warning('Insufficient amount for locking.'); this.loading = false; } } else if (this.type == 'ExtendValidity') { this.extendValidity(); } else if (this.type == 'Unlock') { this.unlockTokens(); } } public amount = null; public time = ''; public recepientsAddress = ''; public loading = false; transferTokens() { GlobalsComponent.governance[this.userObj.companyName].transferTokens( this.dAppName, this.recepientsAddress, this.amount, function(error, result) { if (error) { this.loading = false; } else { this.timeout(result); this.toastr.info( 'Request submitted to ethereum(' + GlobalsComponent.network + '). Awaiting confirmation!' ); } }.bind(this) ); } lockTokens() { var d = new Date(this.time); var currentDate = new Date(); var timeStamp = d.getTime(); var currentTimestamp = currentDate.getTime(); var validity = Math.round(timeStamp / 1000); validity = validity - Math.round(currentTimestamp / 1000); GlobalsComponent.governance[this.userObj.companyName].lockTokens( this.dAppName, 'GOV', this.amount, validity, function(error, result) { if (error) { this.loading = false; } else { this.timeout(result); this.toastr.info( 'Request submitted to ethereum(' + GlobalsComponent.network + '). Awaiting confirmation!' ); } }.bind(this) ); } extendValidity() { var d = new Date(this.time); var timeStamp = d.getTime(); var validity = Math.round(timeStamp / 1000); validity = validity - this.validityTimeStamp[this.dAppName]; GlobalsComponent.governance[this.userObj.companyName].extendLockValidity( this.dAppName, 'GOV', validity, function(error, result) { if (error) { this.loading = false; } else { this.timeout(result); this.toastr.info( 'Request submitted to ethereum(' + GlobalsComponent.network + '). Awaiting confirmation!' ); } }.bind(this) ); } unlockTokens() { GlobalsComponent.governance[this.userObj.companyName].unlockTokens( this.dAppName, this.memberAddress, function(error, result) { if (error) { this.loading = false; } else { this.timeout(result); this.toastr.info( 'Request submitted to ethereum(' + GlobalsComponent.network + '). Awaiting confirmation!' ); } }.bind(this) ); } getFormattedDate(minDate) { var time = minDate; var mm = time.getMonth() + 1; var month; if (mm < 10) month = '0' + mm; else month = mm; var dd = time.getDate() + 1; var date; if (dd < 10) date = '0' + dd; else date = dd; var year = time.getFullYear(); if (date == 32) { date = date - 1; } return year + '-' + month + '-' + date; } timeout(hash) { setTimeout(() => { var boundFunction = this.web3.eth.getTransactionReceipt( hash, function(error, result) { if (result != null) { this.initialCalls(); this.toastr.success(this.type + ' Successful'); this.loading = false; $('#m_modal_5').modal('hide'); } else this.timeout(hash); }.bind(this) ); }, 1000); } ngAfterViewInit() {} }