import { Injectable } from '@angular/core' ; import { LocalExecutionResult } from '@shared/models/local-execution-result' ; import { Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { LocalCallRequest } from '../models/LocalCallRequest' ; import { Result , ResultStatus } from '../models/result' ; import { SavedToken , Token } from '../models/token' ; import { TokenBalanceRequest } from '../models/token-balance-request' ; import { StorageService } from './storage.service' ; import { GlobalService } from '@shared/services/global.service' ; import { SmartContractsService } from "../../smart-contracts/smart-contracts.service" ; @ Injectable ( ) export class TokensService { private savedTokens = 'savedTokens' ; private defaultTokens = [ ] ; constructor ( private smartContractService , private storage , private globalService ) { this . savedTokens = `${ globalService . getNetwork ( ) }:savedTokens` ; const oldTokens = this . storage . getItem < SavedToken [ ] > ( 'savedTokens' ) ; if ( oldTokens ) { this . UpdateTokens ( oldTokens ) ; this . storage . removeItem ( 'savedTokens' ) ; } } GetSavedTokens ( ) { const savedTokens = this . storage . getItem < SavedToken [ ] > ( this . savedTokens ) ; return savedTokens ? this . defaultTokens . concat ( savedTokens ) : this . defaultTokens ; } GetAvailableTokens ( ) { return [ new Token ( 'CG1' , 'CXa9fNVXPfYL9rdqiR22NoAc9kZUfBAUCu' , 'Cirrus Giveaway' ) ] . concat ( this . defaultTokens ) ; } UpdateTokens ( tokens ) { this . storage . setItem ( this . savedTokens , tokens ) ; return Result . ok ( tokens ) ; } AddToken ( token ) { if ( ! token ) { return new Result ( ResultStatus . Error , 'Invalid token' ) ; } const tokens = this . GetSavedTokens ( ) ; const index = tokens . map ( t => t . address ) . indexOf ( token . address ) ; if ( index >= 0 ) { return new Result ( ResultStatus . Error , 'Specified token is already saved' ) ; } tokens . push ( token ) ; this . storage . setItem ( this . savedTokens , tokens ) ; return Result . ok ( token ) ; } RemoveToken ( token ) { if ( ! token ) { return new Result ( ResultStatus . Error , 'Invalid token' ) ; } const tokens = this . GetSavedTokens ( ) ; const index = tokens . map ( t => t . address ) . indexOf ( token . address ) ; if ( index < 0 ) { return new Result ( ResultStatus . Error , 'Specified token was not found' ) ; } tokens . splice ( index , 1 ) ; this . storage . setItem ( this . savedTokens , tokens ) ; return Result . ok ( token ) ; } GetTokenBalance ( request ) { return this . LocalCall ( request ) . pipe ( map ( localExecutionresult => localExecutionresult . return ? localExecutionresult . return : 0 ) ) ; } LocalCall ( request ) { return this . smartContractService . LocalCall ( request ) . pipe ( map ( response => { const anyResponse = response as any ; const result = new LocalExecutionResult ( ) ; result . gasConsumed = anyResponse . hasOwnProperty ( 'GasConsumed' ) ? anyResponse . GasConsumed : anyResponse . gasConsumed ; result . return = anyResponse . hasOwnProperty ( 'Return' ) ? anyResponse . Return : anyResponse . return ; result . revert = anyResponse . hasOwnProperty ( 'Revert' ) ? anyResponse . Revert : anyResponse . revert ; result . logs = anyResponse . hasOwnProperty ( 'Logs' ) ? anyResponse . Revert : anyResponse . logs ; result . internalTransfers = anyResponse . hasOwnProperty ( 'InternalTransfers' ) ? anyResponse . Revert : anyResponse . internalTransfers ; return result ; } ) ) ; } }