import { Component , Injector , OnInit } from '@angular/core' ; import { ContributionMetric , WalletTokenRewardsService , } from './rewards.service' ; import * as moment from 'moment' ; import { Observable , of , Subscription , timer } from 'rxjs' ; import { UniswapModalService } from '../../../../blockchain/token-purchase/v2/uniswap/uniswap-modal.service' ; import { EarnModalService } from '../../../../blockchain/earn/earn-modal.service' ; import { map , shareReplay } from 'rxjs/operators' ; import { OnchainTransferModalService } from '../../components/onchain-transfer/onchain-transfer.service' ; import { WalletV2Service } from '../../wallet-v2.service' ; import { Session } from '../../../../../services/session' ; import { max } from 'bn.js' ; import { ConnectWalletModalService } from '../../../../blockchain/connect-wallet/connect-wallet-modal.service' ; @ Component ( { selector : 'm-wallet__tokenRewards' , templateUrl : './rewards.component.html' , styleUrls : [ './rewards.component.ng.scss' , '../../components/accordion/accordion.component.ng.scss' , ] , providers : [ WalletTokenRewardsService ] , } ) export class WalletTokenRewardsComponent implements OnInit { /**
     * The reference date
     * */ date = new Date ( ) ; /**
     * The max date we can select
     */ maxDate = new Date ( ) ; /**
     * The row to expand
     * */ expandedRow : string ; /**
     * Totals
     */ total = { daily : null , all_time : null , } ; /**
     * Has pending transactions.
     */ public readonly hasPending$ = this . rewards . hasPending$ . pipe ( shareReplay ( 1 ) ) ; /**
     * The data for the rows
     * TODO: add types
     */ data ; /**
     * Obserbale to determine if estimates should be shown
     */ showEstimates$ : Observable < boolean > = this . rewards . dateTs$ . pipe ( map ( dateTs => dateTs == moment ( ) . utc ( ) . startOf ( 'd' ) . unix ( ) ) ) ; nextPayoutDate$ : Observable < number > = timer ( 0 , 1000 ) . pipe ( map ( ( ) => { return ( moment ( ) . utc ( ) . endOf ( 'day' ) . unix ( ) - moment ( ) . utc ( ) . unix ( ) ) ; } ) ) ; /** Breakdown of relative dates engangement scores */ contributionScores$ : Observable < ContributionMetric [ ] > = this . rewards . contributionScores$ ; /** Breakdown of relative dates liquidity */ liquidityPositions$ : Observable < any > = this . rewards . liquidityPositions$ ; /**
     * Snapshot of isConnected observable
     */ isConnected : boolean ; /**
     * Subscriptions
     */ subscriptions : Subscription [ ] = [ ] ; constructor ( private rewards , private uniswapModalService , private earnModalService , protected injector , protected onchainTransferModal , private walletService , private session , protected connectWalletModalService ) { } ngOnInit ( ) { this . rewards . rewards$ . subscribe ( response => { this . total = response . total ; this . data = response ; } ) ; this . subscriptions . push ( this . connectWalletModalService . isConnected$ . subscribe ( isConnected => ( this . isConnected = isConnected ) ) ) ; } param date onDateChange ( date ) { date = this . adjustDateForUtc ( date ) ; this . date = new Date ( date ) ; this . total = { daily : null , all_time : null } ; this . data = null ; this . rewards . setDate ( date ) ; } /**
     *
     */ toggleRow ( rowId ) { if ( this . expandedRow === rowId ) { this . expandedRow = null ; return ; } this . expandedRow = rowId ; } /**
     * Returns the date in YYYY-MM-DD format
     */ private getDate ( ) { return ( this . date . getUTCFullYear ( ) + '-' + ( this . date . getUTCMonth ( ) + 1 ) + '-' + this . date . getUTCDate ( ) ) ; } round ( number ) { return Math . round ( number ) ; } /**
     * Returns the friendly date
     */ get friendlyDate ( ) : string { const d = new Date ( this . getDate ( ) ) ; const m = moment ( this . getDate ( ) , 'YYYY-MM-DD' ) ; return m . calendar ( null , { sameDay : '[Today]' , nextDay : '[Tomorrow]' , nextWeek : 'MMMM Do YYYY' , lastDay : '[Yesterday]' , lastWeek : 'MMMM Do' , sameElse : ( ) => { if ( moment ( ) . year ( ) === m . year ( ) ) { return 'MMMM Do' ; } else { return 'MMMM Do YYYY' ; } } , } ) ; } get uniswapAccountLink ( ) : string { const address = this . walletService . wallet . receiver . address ; if ( ! address ) { return ; } const uniswapUrl = 'https://v2.info.uniswap.org/account/' ; return uniswapUrl + address ; } param e async onTransferClick ( e ) { e . stopPropagation ( ) ; this . onchainTransferModal . setInjector ( this . injector ) . present ( ) . toPromise ( ) ; } param e onProvideLiquidityClick ( e ) { e . stopPropagation ( ) ; this . uniswapModalService . open ( 'add' ) ; } /**
     * True if opted out of the liquidity spot
     */ isOptedOutOfLiquiditySpot ( ) { return this . session . getLoggedInUser ( ) ?. liquidity_spot_opt_out ; } param multiplier calculateDaysFromMultiplier ( multiplier ) { const maxDays = 365 ; const maxMultiplier = 3 ; const minMultiplier = 1 ; const multiplierRange = maxMultiplier - minMultiplier ; const dailyIncrement = multiplierRange / maxDays ; return ( multiplier - minMultiplier ) / dailyIncrement ; } async joinRewards ( e ) { const onComplete = ( ) => ( this . isConnected = undefined ) ; await this . connectWalletModalService . joinRewards ( onComplete ) ; } param { Date } date returns { Date } private adjustDateForUtc ( date ) { return new Date ( date . getTime ( ) + Math . abs ( date . getTimezoneOffset ( ) * 1000 * 60 ) ) ; } }