/ src / utils / utils.ts
utils.ts
  1  import { ChainId, ProtocolAction } from '@aave/contract-helpers';
  2  import { BigNumberValue, USD_DECIMALS, valueToBigNumber } from '@aave/math-utils';
  3  
  4  import { CustomMarket } from './marketsAndNetworksConfig';
  5  
  6  export function hexToAscii(_hex: string): string {
  7    const hex = _hex.toString();
  8    let str = '';
  9    for (let n = 0; n < hex.length; n += 2) {
 10      str += String.fromCharCode(parseInt(hex.substr(n, 2), 16));
 11    }
 12    return str;
 13  }
 14  
 15  export interface CancelablePromise<T = unknown> {
 16    promise: Promise<T>;
 17    cancel: () => void;
 18  }
 19  
 20  export const makeCancelable = <T>(promise: Promise<T>) => {
 21    let hasCanceled_ = false;
 22  
 23    const wrappedPromise = new Promise<T>((resolve, reject) => {
 24      promise.then(
 25        (val) => (hasCanceled_ ? reject({ isCanceled: true }) : resolve(val)),
 26        (error) => (hasCanceled_ ? reject({ isCanceled: true }) : reject(error))
 27      );
 28    });
 29  
 30    return {
 31      promise: wrappedPromise,
 32      cancel() {
 33        hasCanceled_ = true;
 34      },
 35    };
 36  };
 37  
 38  export const optimizedPath = (currentChainId: ChainId) => {
 39    return (
 40      currentChainId === ChainId.arbitrum_one || currentChainId === ChainId.optimism
 41      // ||
 42      // currentChainId === ChainId.optimism_kovan
 43    );
 44  };
 45  
 46  // Overrides for minimum base token remaining after performing an action
 47  export const minBaseTokenRemainingByNetwork: Record<number, string> = {
 48    [ChainId.optimism]: '0.0001',
 49    [ChainId.arbitrum_one]: '0.0001',
 50  };
 51  
 52  export const amountToUsd = (
 53    amount: BigNumberValue,
 54    formattedPriceInMarketReferenceCurrency: string,
 55    marketReferencePriceInUsd: string
 56  ) => {
 57    return valueToBigNumber(amount)
 58      .multipliedBy(formattedPriceInMarketReferenceCurrency)
 59      .multipliedBy(marketReferencePriceInUsd)
 60      .shiftedBy(-USD_DECIMALS);
 61  };
 62  
 63  export const roundToTokenDecimals = (inputValue: string, tokenDecimals: number) => {
 64    const [whole, decimals] = inputValue.split('.');
 65  
 66    // If there are no decimal places or the number of decimal places is within the limit
 67    if (!decimals || decimals.length <= tokenDecimals) {
 68      return inputValue;
 69    }
 70  
 71    // Truncate the decimals to the specified number of token decimals
 72    const adjustedDecimals = decimals.slice(0, tokenDecimals);
 73  
 74    // Combine the whole and adjusted decimal parts
 75    return whole + '.' + adjustedDecimals;
 76  };
 77  
 78  export type ExternalIncentivesTooltipsConfig = {
 79    superFestRewards: boolean;
 80    spkAirdrop: boolean;
 81    kernelPoints: boolean;
 82  };
 83  
 84  export const showExternalIncentivesTooltip = (
 85    symbol: string,
 86    currentMarket: string,
 87    protocolAction?: ProtocolAction
 88  ) => {
 89    const superFestRewardsEnabled = false;
 90    const spkRewardsEnabled = true;
 91    const kernelPointsEnabled = true;
 92  
 93    const tooltipsConfig: ExternalIncentivesTooltipsConfig = {
 94      superFestRewards: false,
 95      spkAirdrop: false,
 96      kernelPoints: false,
 97    };
 98  
 99    if (
100      superFestRewardsEnabled &&
101      currentMarket === CustomMarket.proto_base_v3 &&
102      protocolAction === ProtocolAction.supply &&
103      (symbol == 'ETH' || symbol == 'WETH' || symbol == 'wstETH')
104    ) {
105      tooltipsConfig.superFestRewards = true;
106    }
107  
108    if (
109      spkRewardsEnabled &&
110      currentMarket === CustomMarket.proto_mainnet_v3 &&
111      protocolAction === ProtocolAction.supply &&
112      symbol == 'USDS'
113    ) {
114      tooltipsConfig.spkAirdrop = true;
115    }
116  
117    if (
118      kernelPointsEnabled &&
119      (currentMarket === CustomMarket.proto_mainnet_v3 ||
120        currentMarket === CustomMarket.proto_lido_v3 ||
121        currentMarket === CustomMarket.proto_base_v3 ||
122        currentMarket === CustomMarket.proto_arbitrum_v3) &&
123      protocolAction === ProtocolAction.supply &&
124      (symbol == 'rsETH' || symbol == 'wrsETH')
125    ) {
126      tooltipsConfig.kernelPoints = true;
127    }
128  
129    return tooltipsConfig;
130  };