/ src / services / MigrationService.ts
MigrationService.ts
 1  import { Pool, V3MigrationHelperService } from '@aave/contract-helpers';
 2  import { Provider } from '@ethersproject/providers';
 3  import {
 4    MIGRATION_ASSETS_EXCEPTIONS,
 5    MigrationException,
 6    MigrationSupplyException,
 7  } from 'src/store/v3MigrationSlice';
 8  import { MarketDataType } from 'src/ui-config/marketsConfig';
 9  import { getNetworkConfig } from 'src/utils/marketsAndNetworksConfig';
10  import invariant from 'tiny-invariant';
11  
12  export class MigrationService {
13    constructor(private readonly getProvider: (chainId: number) => Provider) {}
14  
15    private getMigrationService(fromMarketData: MarketDataType, toMarketData: MarketDataType) {
16      invariant(
17        fromMarketData.addresses.V3_MIGRATOR,
18        'V3_MIGRATOR address is not defined in fromMarketData'
19      );
20      invariant(fromMarketData.chainId === toMarketData.chainId, 'ChainId mismatch');
21      const provider = this.getProvider(toMarketData.chainId);
22      const toPool = new Pool(provider, {
23        POOL: toMarketData.addresses.LENDING_POOL,
24        REPAY_WITH_COLLATERAL_ADAPTER: toMarketData.addresses.REPAY_WITH_COLLATERAL_ADAPTER,
25        SWAP_COLLATERAL_ADAPTER: toMarketData.addresses.SWAP_COLLATERAL_ADAPTER,
26        WETH_GATEWAY: toMarketData.addresses.WETH_GATEWAY,
27        L2_ENCODER: toMarketData.addresses.L2_ENCODER,
28      });
29      return new V3MigrationHelperService(provider, fromMarketData.addresses.V3_MIGRATOR, toPool);
30    }
31  
32    async getMigrationExceptionSupplyBalances(
33      migrationSupplyException: MigrationSupplyException[],
34      fromMarketData: MarketDataType,
35      toMarketData: MarketDataType
36    ) {
37      const networkConfig = getNetworkConfig(fromMarketData.chainId);
38      const chainId = networkConfig.underlyingChainId || fromMarketData.chainId;
39      const exceptions = MIGRATION_ASSETS_EXCEPTIONS[chainId] || [];
40      const filteredSuppliesForExceptions = migrationSupplyException.filter(
41        (supply) =>
42          exceptions.indexOf(supply.underlyingAsset) >= 0 && supply.scaledATokenBalance !== '0'
43      );
44      const migrationExceptions: Record<string, MigrationException> = {};
45      if (filteredSuppliesForExceptions.length !== 0) {
46        const migrationService = this.getMigrationService(fromMarketData, toMarketData);
47        const mappedSupplies = filteredSuppliesForExceptions.map(
48          ({ scaledATokenBalance, underlyingAsset }) => {
49            return migrationService.getMigrationSupply({
50              amount: scaledATokenBalance,
51              asset: underlyingAsset,
52            });
53          }
54        );
55        const result = await Promise.all(mappedSupplies);
56        result.forEach(([asset, amount], index) => {
57          const v2UnderlyingAsset = filteredSuppliesForExceptions[index].underlyingAsset;
58          migrationExceptions[v2UnderlyingAsset] = {
59            v2UnderlyingAsset,
60            v3UnderlyingAsset: asset.toLowerCase(),
61            amount: amount.toString(),
62          };
63        });
64      }
65      return migrationExceptions;
66    }
67  }