const MIN_GAS_PRICE = 10 ** 9 ; const MIN_GAS_LIMIT = 1 ; const DECIMALS = 10 ** 6 ; import { Component , Input , OnInit , OnDestroy , ChangeDetectorRef } from "@angular/core" ; import { BN , units } from "@zilliqa-js/util" ; import { Wallet } from "../wallet" ; import { Payment } from "./payment" ; import { ZilliqaService } from "../../zilliqa.service" ; import * as $ from "jquery" ; declare var grecaptcha ; @ Component ( { selector : "app-walletsend" , templateUrl : "./walletsend.component.html" , styleUrls : [ "./walletsend.component.css" ] } ) export class WalletsendComponent implements OnInit { state : number ; pendingTxId : string ; wallet : Wallet ; payment : Payment ; recaptchaFilled : boolean ; constructor ( public zilliqaService , private ref ) { this . state = 0 ; this . pendingTxId = null ; this . wallet = new Wallet ( ) ; this . payment = new Payment ( ) ; this . recaptchaFilled = false ; } ngOnInit ( ) { this . wallet = this . zilliqaService . getWallet ( ) ; this . payment = { amount : 1 , address : "" , gasPrice : MIN_GAS_PRICE , gasLimit : MIN_GAS_LIMIT } ; this . initRecaptcha ( ) ; } initRecaptcha ( ) { let that = this ; if ( $ ( ".g-recaptcha" ) . length > 0 ) { grecaptcha . render ( "recaptcha" , { sitekey : "6LfB808UAAAAABr8IkcXDwjj4_G6eRURtVgkj-i9" , callback : function ( data ) { that . recaptchaFilled = true ; that . ref . detectChanges ( ) ; } } ) ; return ; } window . setTimeout ( this . initRecaptcha . bind ( this ) , 1000 ) ; } ngOnDestroy ( ) { this . payment = new Payment ( ) ; this . pendingTxId = null ; this . recaptchaFilled = false ; this . setState ( 0 ) ; } setState ( newState ) { this . state = newState ; } convertHexAddress ( ) { if ( this . payment . address && this . payment . address . substr ( 0 , 2 ) == "0x" ) { return this . payment . address . substr ( 2 ) ; } else { return this . payment . address ; } } invalidAddress ( ) { let addr = this . convertHexAddress ( ) ; return addr . length > 0 && ! addr . match ( /^[0-9a-fA-F]{40}$/ ) ; } invalidAmount ( ) { return ( this . payment . amount == null || this . payment . amount <= 0 || ( this . payment . amount * 10 ** 12 ) % 1 !== 0 || units . toQa ( this . payment . amount , units . Units . Zil ) . gt ( new BN ( this . wallet . balance ) ) ) ; } invalidPayment ( ) { let addr = this . convertHexAddress ( ) ; return ( ! addr . match ( /^[0-9a-fA-F]{40}$/ ) || ! this . recaptchaFilled || this . invalidAmount ( ) ) ; } onSend ( ) { this . payment . address = this . convertHexAddress ( ) ; this . payment . amount = units . toQa ( this . payment . amount , units . Units . Zil ) . toString ( ) ; let that = this ; this . zilliqaService . sendPayment ( this . payment ) . then ( data => { that . pendingTxId = data . txId ; grecaptcha . reset ( ) ; that . setState ( 1 ) ; that . zilliqaService . refreshBalance ( ) . then ( data => { } ) ; } , err => { grecaptcha . reset ( ) ; that . setState ( 3 ) ; } ) ; } }