/ src / modules / history / types.ts
types.ts
  1  export type TransactionHistoryItem<T = unknown> = {
  2    id: string;
  3    txHash: string;
  4    action: string;
  5    pool: {
  6      id: string;
  7    };
  8    timestamp: number;
  9  } & T;
 10  
 11  export type ReserveSubset = {
 12    symbol: string;
 13    decimals: number;
 14    underlyingAsset: string;
 15    name: string;
 16  };
 17  
 18  export type ActionFields = {
 19    Supply: {
 20      reserve: ReserveSubset;
 21      amount: string;
 22      assetPriceUSD: string;
 23    };
 24    Deposit: {
 25      reserve: ReserveSubset;
 26      amount: string;
 27      assetPriceUSD: string;
 28    };
 29    Borrow: {
 30      reserve: ReserveSubset;
 31      amount: string;
 32      assetPriceUSD: string;
 33    };
 34    Repay: {
 35      reserve: ReserveSubset;
 36      amount: string;
 37      assetPriceUSD: string;
 38    };
 39    RedeemUnderlying: {
 40      reserve: ReserveSubset;
 41      amount: string;
 42      assetPriceUSD: string;
 43    };
 44    LiquidationCall: {
 45      collateralReserve: ReserveSubset;
 46      collateralAmount: string;
 47      principalReserve: ReserveSubset;
 48      principalAmount: string;
 49      borrowAssetPriceUSD: string;
 50      collateralAssetPriceUSD: string;
 51    };
 52    SwapBorrowRate: {
 53      reserve: ReserveSubset;
 54      borrowRateModeFrom: string;
 55      borrowRateModeTo: string;
 56      stableBorrowRate: string;
 57      variableBorrowRate: string;
 58      assetPriceUSD: string;
 59    };
 60    Swap: {
 61      reserve: ReserveSubset;
 62      borrowRateModeFrom: number;
 63      borrowRateModeTo: number;
 64      stableBorrowRate: string;
 65      variableBorrowRate: string;
 66      assetPriceUSD: string;
 67    };
 68    UsageAsCollateral: {
 69      reserve: ReserveSubset;
 70      fromState: boolean;
 71      toState: boolean;
 72      assetPriceUSD: string;
 73    };
 74  };
 75  
 76  export type TransactionHistoryItemUnion =
 77    | TransactionHistoryItem<ActionFields['Supply']>
 78    | TransactionHistoryItem<ActionFields['Deposit']>
 79    | TransactionHistoryItem<ActionFields['Borrow']>
 80    | TransactionHistoryItem<ActionFields['Repay']>
 81    | TransactionHistoryItem<ActionFields['RedeemUnderlying']>
 82    | TransactionHistoryItem<ActionFields['LiquidationCall']>
 83    | TransactionHistoryItem<ActionFields['SwapBorrowRate']>
 84    | TransactionHistoryItem<ActionFields['Swap']>
 85    | TransactionHistoryItem<ActionFields['UsageAsCollateral']>;
 86  
 87  // Type guards
 88  export const hasCollateralReserve = (
 89    txn: TransactionHistoryItemUnion
 90  ): txn is TransactionHistoryItem<ActionFields['LiquidationCall']> => {
 91    return (
 92      (txn as TransactionHistoryItem<ActionFields['LiquidationCall']>).collateralReserve !== undefined
 93    );
 94  };
 95  
 96  export const hasPrincipalReserve = (
 97    txn: TransactionHistoryItemUnion
 98  ): txn is TransactionHistoryItem<ActionFields['LiquidationCall']> => {
 99    return (
100      (txn as TransactionHistoryItem<ActionFields['LiquidationCall']>).principalReserve !== undefined
101    );
102  };
103  
104  export const hasReserve = (
105    txn: TransactionHistoryItemUnion
106  ): txn is
107    | TransactionHistoryItem<ActionFields['Supply']>
108    | TransactionHistoryItem<ActionFields['Deposit']>
109    | TransactionHistoryItem<ActionFields['Borrow']>
110    | TransactionHistoryItem<ActionFields['Repay']>
111    | TransactionHistoryItem<ActionFields['RedeemUnderlying']>
112    | TransactionHistoryItem<ActionFields['SwapBorrowRate']>
113    | TransactionHistoryItem<ActionFields['Swap']>
114    | TransactionHistoryItem<ActionFields['UsageAsCollateral']> => {
115    return (txn as TransactionHistoryItem<ActionFields['Supply']>).reserve !== undefined;
116  };
117  
118  export const hasAmountAndReserve = (
119    txn: TransactionHistoryItemUnion
120  ): txn is
121    | TransactionHistoryItem<ActionFields['Supply']>
122    | TransactionHistoryItem<ActionFields['Deposit']>
123    | TransactionHistoryItem<ActionFields['Borrow']>
124    | TransactionHistoryItem<ActionFields['Repay']>
125    | TransactionHistoryItem<ActionFields['RedeemUnderlying']> => {
126    return (
127      (txn as TransactionHistoryItem<ActionFields['Supply']>).amount !== undefined &&
128      (txn as TransactionHistoryItem<ActionFields['Supply']>).reserve !== undefined
129    );
130  };
131  
132  export const hasSwapBorrowRate = (
133    txn: TransactionHistoryItemUnion
134  ): txn is
135    | TransactionHistoryItem<ActionFields['SwapBorrowRate']>
136    | TransactionHistoryItem<ActionFields['Swap']> => {
137    return (
138      (txn as TransactionHistoryItem<ActionFields['SwapBorrowRate']>).variableBorrowRate !==
139        undefined &&
140      (txn as TransactionHistoryItem<ActionFields['SwapBorrowRate']>).stableBorrowRate !== undefined
141    );
142  };
143  
144  export enum FilterOptions {
145    SUPPLY,
146    BORROW,
147    WITHDRAW,
148    REPAY,
149    RATECHANGE,
150    COLLATERALCHANGE,
151    LIQUIDATION,
152  }
153  
154  export interface HistoryFilters {
155    searchQuery: string;
156    filterQuery: FilterOptions[];
157  }
158  
159  export const actionFilterMap = (action: string): number => {
160    switch (action) {
161      case 'Deposit': // v2
162      case 'Supply': // v3
163        return 0;
164      case 'Borrow':
165        return 1;
166      case 'RedeemUnderlying':
167        return 2;
168      case 'Repay':
169        return 3;
170      case 'Swap': // v2
171      case 'SwapBorrowRate': // v3
172        return 4;
173      case 'UsageAsCollateral':
174        return 5;
175      case 'LiquidationCall':
176        return 6;
177      default:
178        return 7;
179    }
180  };