import { Component } from "@angular/core" ; import { LoadingController , ModalController } from "@ionic/angular" ; import { Network , Peer } from "ark-ts" ; import { finalize } from "rxjs/operators" ; import { LoggerService } from "@/services/logger/logger.service" ; import { ToastProvider } from "@/services/toast/toast" ; import ArkClient from "@/utils/ark-client" ; import { HttpClient } from "@/utils/ark-http-client" ; @ Component ( { selector : "customNetworkCreate" , templateUrl : "custom-network-create.html" , styleUrls : [ "custom-network-create.pcss" ] , } ) export class CustomNetworkCreateModal { public network : Network = new Network ( ) ; public name : string ; public seedServer : string ; public constructor ( private modalCtrl , private toastProvider , private loadingCtrl , private httpClient , private loggerService ) { } public dismiss ( network ? ) { this . modalCtrl . dismiss ( network ) ; } public async configure ( ) { const loading = await this . loadingCtrl . create ( ) ; loading . present ( ) ; const seedServerUrl = this . getSeedServerUrl ( ) ; new ArkClient ( this . seedServer , this . httpClient , this . loggerService ) . getNodeConfiguration ( ) . pipe ( finalize ( ( ) => loading . dismiss ( ) ) ) . subscribe ( ( response ) => { this . network . name = this . name ; this . network . nethash = response . nethash ; this . network . token = response . token ; this . network . symbol = response . symbol ; this . network . explorer = response . explorer ; this . network . version = response . version ; this . network . type = null ; if ( ! response . ports || ! seedServerUrl . port ) { this . configureError ( ) ; return ; } this . network . apiPort = parseInt ( seedServerUrl . port ) ; this . network . activePeer = new Peer ( ) ; this . network . activePeer . ip = seedServerUrl . hostname ; this . network . activePeer . port = parseInt ( seedServerUrl . port ) ; if ( seedServerUrl . protocol === "https:" ) { this . network . activePeer . port = 443 ; } this . network . activePeer . protocol = seedServerUrl . protocol ; this . network . isV2 = true ; this . dismiss ( this . network ) ; } , ( ) => this . configureError ( ) ) ; } public getSeedServerUrl ( ) { try { return new URL ( this . seedServer ) ; } catch ( error ) { return null ; } } private configureError ( ) { this . network = new Network ( ) ; this . toastProvider . error ( "CUSTOM_NETWORK.CONFIGURE_ERROR" ) ; } }