import { Injectable } from '@angular/core' ; import { ApiService } from '@shared/services/api.service' ; import { BehaviorSubject , Observable , of } from 'rxjs' ; import { HttpClient , HttpParams } from '@angular/common/http' ; import { GlobalService } from '@shared/services/global.service' ; import { ErrorService } from '@shared/services/error-service' ; import { catchError } from 'rxjs/operators' ; import { TokenBalanceRequest } from '../tokens/models/token-balance-request' ; import { LocalExecutionResult } from '@shared/models/local-execution-result' ; import { SmartContractsServiceBase } from './smart-contracts-service.base' ; import { WalletInfo } from '@shared/models/wallet-info' ; import { CurrentAccountService } from '@shared/services/current-account.service' ; import { WalletService } from '@shared/services/wallet.service' ; export class SmartContractsContractItem { constructor ( public blockId , public type , public hash , public destinationAddress , public amount ) { } } export interface ContractTransactionItem { blockHeight : number ; type : number ; hash : string ; to : string ; amount : number ; transactionFee : number ; gasFee : number ; } @ Injectable ( ) export class SmartContractsService extends ApiService implements SmartContractsServiceBase { private historySubject = new BehaviorSubject < ContractTransactionItem [ ] > ( [ ] ) ; private currentWallet : WalletInfo ; private lastBalance : number = null ; private currentAddress : string ; constructor ( httpClient , globalService , walletService , currentAccountService , errorService ) { super ( httpClient , globalService , errorService ) ; globalService . currentWallet . subscribe ( ( wallet ) => { this . currentWallet = wallet ; } ) ; currentAccountService . currentAddress . subscribe ( address => { this . currentAddress = address ; } ) ; walletService . wallet ( ) . subscribe ( wallet => { if ( wallet && this . currentAddress && this . lastBalance !== ( wallet . amountConfirmed + wallet . amountUnconfirmed ) ) { this . lastBalance = ( wallet . amountConfirmed + wallet . amountUnconfirmed ) ; this . GetHistoryFromApi ( this . currentWallet . walletName , this . currentAddress ) ; } } ) ; } public GetReceipt ( hash , silent = false ) { const params = new HttpParams ( ) . set ( 'txHash' , hash ) ; return this . get < string > ( 'smartcontracts/receipt' , params ) . pipe ( catchError ( err => this . handleHttpError ( err , silent ) ) ) ; } public GetReceiptSilent ( hash ) { const params = new HttpParams ( ) . set ( 'txHash' , hash ) ; return this . get < string > ( 'smartcontracts/receipt' , params ) ; } public GetAddress ( walletName ) { const params = new HttpParams ( ) . set ( 'walletName' , walletName ) ; return this . get ( 'smartcontractwallet/account-address' , params ) . pipe ( catchError ( err => this . handleHttpError ( err ) ) ) ; } public GetAddresses ( walletName ) { const params = new HttpParams ( ) . set ( 'walletName' , walletName ) ; return this . get ( 'smartcontractwallet/account-addresses' , params ) . pipe ( catchError ( err => this . handleHttpError ( err ) ) ) ; } public GetBalance ( walletName ) { const params = new HttpParams ( ) . set ( 'walletName' , walletName ) ; return this . get ( 'smartcontractwallet/account-balance' , params ) . pipe ( catchError ( err => this . handleHttpError ( err ) ) ) ; } public GetHistory ( walletName , address ) { return this . historySubject . asObservable ( ) ; } public GetHistoryFromApi ( walletName , address ) { const params = new HttpParams ( ) . set ( 'walletName' , walletName ) . set ( 'address' , address ) ; this . get < ContractTransactionItem [ ] > ( 'smartcontractwallet/history' , params ) . pipe ( catchError ( err => this . handleHttpError ( err ) ) ) . toPromise ( ) . then ( response => { this . historySubject . next ( response ) ; } ) ; } public PostCreate ( transaction ) { return this . post ( 'smartcontractwallet/create' , transaction ) . pipe ( catchError ( err => this . handleHttpError ( err ) ) ) ; } public PostCall ( transaction ) { return this . post ( 'smartcontractwallet/call' , transaction ) . pipe ( catchError ( err => this . handleHttpError ( err ) ) ) ; } public LocalCall ( localCall ) { return this . post < LocalExecutionResult > ( 'smartcontracts/local-call' , localCall ) . pipe ( catchError ( err => this . handleHttpError ( err ) ) ) ; } GetContracts ( walletName ) { return of ( [ new SmartContractsContractItem ( '7809' , 'Transfer' , 'bbdbcae72f1085710' , 'SdrP9wvxZmaG7t3UAjxxyB6RNT9FV1Z2Sn' , 10898025 ) , new SmartContractsContractItem ( '7810' , 'Transfer' , 'bbdbcae72f1085710' , 'SdrP9wvxZmaG7t3UAjxxyB6RNT9FV1Z2Sn' , 11898025 ) , new SmartContractsContractItem ( '7811' , 'Transfer' , 'bbdbcae72f1085710' , 'SdrP9wvxZmaG7t3UAjxxyB6RNT9FV1Z2Sn' , 12898025 ) , new SmartContractsContractItem ( '7812' , 'Transfer' , 'bbdbcae72f1085710' , 'SdrP9wvxZmaG7t3UAjxxyB6RNT9FV1Z2Sn' , 13898025 ) , new SmartContractsContractItem ( '7813' , 'Transfer' , 'bbdbcae72f1085710' , 'SdrP9wvxZmaG7t3UAjxxyB6RNT9FV1Z2Sn' , 14898025 ) , new SmartContractsContractItem ( '7814' , 'Transfer' , 'bbdbcae72f1085710' , 'SdrP9wvxZmaG7t3UAjxxyB6RNT9FV1Z2Sn' , 15898025 ) , ] ) ; } GetSenderAddresses ( walletName ) { return of ( [ 'SarP7wvxZmaG7t1UAjaxyB6RNT9FV1Z2Sn' , 'SbrP8wvxZmaG7t2UAjbxyB7RNT9FV1Z2Sn' , 'ScrP9wvxZmaG7t3UAjcxyB8RNT9FV1Z2Sn' ] ) ; } GetParameterTypes ( walletName ) { return of ( [ 'Type 1' , 'Type 2' , 'Type 3' ] ) ; } }