import { Injectable } from '@angular/core' ; import BigNumber from 'bignumber.js' ; import { BLOCKCHAIN_NAME , DEPRECATED_BLOCKCHAIN_NAME } from '@shared/models/blockchain/blockchain-name' ; import { BridgeTrade } from '@features/bridge/models/bridge-trade' ; import { BridgeTokenPair } from '@features/bridge/models/bridge-token-pair' ; import { EMPTY , Observable } from 'rxjs' ; import { first , map , mergeMap , switchMap } from 'rxjs/operators' ; import { TableTrade } from '@shared/models/my-trades/table-trade' ; import { BridgeBlockchainApi , BridgeTableTradeApi } from '@core/services/backend/bridge-api/models/bridge-table-trade-api' ; import { TRANSACTION_STATUS } from '@shared/models/blockchain/transaction-status' ; import { TokensService } from 'src/app/core/services/tokens/tokens.service' ; import { HttpService } from '../../http/http.service' ; import { BOT_URL } from 'src/app/core/services/backend/constants/bot-url' ; import { BridgeBotRequest } from '@core/services/backend/bridge-api/models/bridge-bot-request' ; @ Injectable ( { providedIn : 'root' } ) export class BridgeApiService { private readonly tradeBlockchain : Record < BridgeBlockchainApi , BLOCKCHAIN_NAME | DEPRECATED_BLOCKCHAIN_NAME > = { ETH : BLOCKCHAIN_NAME . ETHEREUM , BSC : BLOCKCHAIN_NAME . BINANCE_SMART_CHAIN , POL : BLOCKCHAIN_NAME . POLYGON , XDAI : BLOCKCHAIN_NAME . XDAI , TRX : DEPRECATED_BLOCKCHAIN_NAME . TRON } ; constructor ( private httpService , private tokensService ) { } param walletAddress return public getUserTrades ( walletAddress ) { return this . httpService . get ( 'bridges/transactions' , { walletAddress : walletAddress . toLowerCase ( ) , t : Date . now ( ) } ) . pipe ( map ( ( tradesApi ) => tradesApi . map ( trade => this . parseTradeApiToTableTrade ( trade ) ) ) ) ; } param trade return private parseTradeApiToTableTrade ( trade ) { const fromBlockchain = this . tradeBlockchain [ trade . fromNetwork ] ; const toBlockchain = this . tradeBlockchain [ trade . toNetwork ] ; let status = trade . status . toLowerCase ( ) as TRANSACTION_STATUS ; if ( fromBlockchain === BLOCKCHAIN_NAME . POLYGON && status === TRANSACTION_STATUS . WAITING_FOR_DEPOSIT ) { status = TRANSACTION_STATUS . WAITING_FOR_RECEIVING ; } return { transactionId : trade . transaction_id , fromTransactionHash : trade . fromTransactionHash , toTransactionHash : trade . toTransactionHash , status , provider : trade . type , fromToken : { blockchain : fromBlockchain , symbol : trade . ethSymbol , amount : new BigNumber ( trade . actualFromAmount ) . toFixed ( ) , image : trade . image_link } , toToken : { blockchain : toBlockchain , symbol : trade . bscSymbol , amount : new BigNumber ( trade . actualToAmount ) . toFixed ( ) , image : trade . image_link } , date : new Date ( trade . updateTime ) } ; } param fromBlockchain param transactionHash param actualFromAmount param walletFromAddress public postRubicTransaction ( fromBlockchain , transactionHash , actualFromAmount , walletFromAddress ) { const body = { type : 'swap_rbc' , fromNetwork : fromBlockchain === BLOCKCHAIN_NAME . BINANCE_SMART_CHAIN ? 1 : 2 , transaction_id : transactionHash , actualFromAmount , walletFromAddress } ; return this . httpService . post < void > ( 'bridges/transactions' , body ) . toPromise ( ) ; } param bridgeTrade param status param transactionHash param userAddress public postPolygonTransaction ( bridgeTrade , status , transactionHash , userAddress ) { const body = { type : 'polygon' , fromNetwork : bridgeTrade . fromBlockchain === BLOCKCHAIN_NAME . POLYGON ? 'POL' : 'ETH' , toNetwork : bridgeTrade . toBlockchain === BLOCKCHAIN_NAME . POLYGON ? 'POL' : 'ETH' , actualFromAmount : bridgeTrade . amount , actualToAmount : bridgeTrade . amount , ethSymbol : bridgeTrade . token . tokenByBlockchain [ bridgeTrade . fromBlockchain ] . address , bscSymbol : bridgeTrade . token . tokenByBlockchain [ bridgeTrade . toBlockchain ] . address , updateTime : new Date ( ) , status : status . toLowerCase ( ) , transaction_id : transactionHash , walletFromAddress : userAddress , walletToAddress : userAddress , walletDepositAddress : '0xBbD7cBFA79faee899Eaf900F13C9065bF03B1A74' } ; return this . httpService . post < void > ( 'bridges/transactions' , body ) . toPromise ( ) ; } param burnTransactionHash param newTransactionHash param status public patchPolygonTransaction ( burnTransactionHash , newTransactionHash , status ) { return this . httpService . patch < void > ( 'bridges/transactions' , { type : 'polygon' , to_transaction_hash : newTransactionHash , status } , { transaction_id : burnTransactionHash } ) . toPromise ( ) ; } param transactionHash public postXDaiTransaction ( transactionHash ) { const body = { type : 'xdai' , fromNetwork : BLOCKCHAIN_NAME . ETHEREUM . toLowerCase ( ) , transaction_id : transactionHash } ; return this . httpService . post < void > ( 'bridges/transactions' , body ) . toPromise ( ) ; } param bridgeTrade param transactionHash param walletAddress public notifyBridgeBot ( bridgeTrade , transactionHash , walletAddress ) { return this . getTokenPrice ( bridgeTrade . token ) . pipe ( mergeMap ( price => { const body = { txHash : transactionHash , walletAddress , amount : bridgeTrade . amount . toNumber ( ) , fromBlockchain : bridgeTrade . fromBlockchain , toBlockchain : bridgeTrade . toBlockchain , symbol : bridgeTrade . token . symbol , price } ; return this . httpService . post ( BOT_URL . BRIDGES , body ) . pipe ( switchMap ( ( ) => EMPTY ) ) ; } ) ) . toPromise ( ) ; } param bridgeTokenPair return private getTokenPrice ( bridgeTokenPair ) { return this . tokensService . tokens$ . pipe ( first ( ) , map ( backendTokens => { const prices = Object . values ( BLOCKCHAIN_NAME ) . map ( blockchain => backendTokens . find ( token => bridgeTokenPair . tokenByBlockchain [ blockchain ] ?. address . toLowerCase ( ) === token . address . toLowerCase ( ) ) ?. price ) . filter ( it => it ) . sort ( ( a , b ) => b - a ) ; return prices [ 0 ] || 0 ; } ) ) ; } }