1- import { InterestRate } from '@aave/contract-helpers' ;
1+ import {
2+ InterestRate ,
3+ PoolBaseCurrencyHumanized ,
4+ ReserveDataHumanized ,
5+ UserReserveDataHumanized ,
6+ } from '@aave/contract-helpers' ;
27import { V3MigrationHelperSignedPermit } from '@aave/contract-helpers/dist/esm/v3-migration-contract/v3MigrationTypes' ;
3- import { valueToBigNumber } from '@aave/math-utils' ;
8+ import { formatReserves , formatUserSummary , valueToBigNumber } from '@aave/math-utils' ;
49import { SignatureLike } from '@ethersproject/bytes' ;
5- import { BigNumber , BigNumberish , constants } from 'ethers' ;
10+ import { BigNumberish , constants } from 'ethers' ;
11+
612import {
13+ selectCurrentChainIdV2MarketData ,
14+ selectCurrentChainIdV3MarketData ,
15+ selectFormatBaseCurrencyData ,
16+ selectFormatUserEmodeCategoryId ,
717 selectUserNonEmtpySummaryAndIncentive ,
818 selectUserSummaryAndIncentives ,
919} from './poolSelectors' ;
@@ -21,7 +31,6 @@ export const selectUserSupplyIncreasedReservesForMigrationPermits = (
2131 store : RootStore ,
2232 timestamp : number
2333) => {
24- console . log ( 'selectUserSupplyIncreasedReservesForMigrationPermits' )
2534 return selectedUserSupplyReservesForMigration ( store , timestamp ) . map ( ( userReserve ) => {
2635 const increasedAmount = addPercent ( userReserve . underlyingBalance ) ;
2736 return { ...userReserve , increasedAmount } ;
@@ -84,7 +93,122 @@ export const selectUserBorrowReservesForMigration = (store: RootStore, timestamp
8493 return selectedUserReserves ;
8594} ;
8695
87- export const selectCurrentMarketV2Reserves = ( store : RootStore , timestamp : number ) => {
88- const currentChainId = store . currentChainId ;
89- return store . data . get ( currentChainId ) ;
96+ export const selectFormatUserSummaryForMigration = (
97+ reserves : ReserveDataHumanized [ ] = [ ] ,
98+ userReserves : UserReserveDataHumanized [ ] = [ ] ,
99+ baseCurrencyData : PoolBaseCurrencyHumanized ,
100+ userEmodeCategoryId : number ,
101+ currentTimestamp : number
102+ ) => {
103+ const { marketReferenceCurrencyDecimals, marketReferenceCurrencyPriceInUsd } = baseCurrencyData ;
104+ const formattedReserves = formatReserves ( {
105+ reserves : reserves ,
106+ currentTimestamp,
107+ marketReferenceCurrencyDecimals : marketReferenceCurrencyDecimals ,
108+ marketReferencePriceInUsd : marketReferenceCurrencyPriceInUsd ,
109+ } ) ;
110+
111+ const formattedSummary = formatUserSummary ( {
112+ currentTimestamp,
113+ formattedReserves,
114+ marketReferenceCurrencyDecimals : marketReferenceCurrencyDecimals ,
115+ marketReferencePriceInUsd : marketReferenceCurrencyPriceInUsd ,
116+ userReserves,
117+ userEmodeCategoryId : 0 ,
118+ } ) ;
119+
120+ return formattedSummary ;
121+ } ;
122+
123+ export const selectV2UserSummaryAfterMigration = ( store : RootStore , currentTimestamp : number ) => {
124+ const poolReserve = selectCurrentChainIdV2MarketData ( store ) ;
125+
126+ const userReserves =
127+ poolReserve ?. userReserves ?. filter ( ( userReserve ) => {
128+ if (
129+ store . selectedMigrationSupplyAssets [ userReserve . underlyingAsset ] ||
130+ store . selectedMigrationBorrowAssets [ userReserve . underlyingAsset ]
131+ ) {
132+ return false ;
133+ }
134+ return true ;
135+ } ) || [ ] ;
136+
137+ const baseCurrencyData = selectFormatBaseCurrencyData ( poolReserve ) ;
138+ const userEmodeCategoryId = selectFormatUserEmodeCategoryId ( poolReserve ) ;
139+
140+ return selectFormatUserSummaryForMigration (
141+ poolReserve ?. reserves ,
142+ userReserves ,
143+ baseCurrencyData ,
144+ userEmodeCategoryId ,
145+ currentTimestamp
146+ ) ;
147+ } ;
148+
149+ const combine = ( a : string , b : string ) : string => {
150+ return valueToBigNumber ( a ) . plus ( valueToBigNumber ( b ) ) . toString ( ) ;
151+ } ;
152+
153+ export const selectV3UserSummaryAfterMigration = ( store : RootStore , currentTimestamp : number ) => {
154+ const poolReserveV3 = selectCurrentChainIdV3MarketData ( store ) ;
155+
156+ const supplies = selectedUserSupplyReservesForMigration ( store , currentTimestamp ) ;
157+ const borrows = selectUserBorrowReservesForMigration ( store , currentTimestamp ) ;
158+
159+ //TODO: refactor that to be more efficient
160+ const suppliesMap = supplies . concat ( borrows ) . reduce ( ( obj , item ) => {
161+ obj [ item . underlyingAsset ] = item ;
162+ return obj ;
163+ } , { } as Record < string , typeof supplies [ 0 ] > ) ;
164+
165+ const userReserves =
166+ poolReserveV3 ?. userReserves ?. map ( ( userReserve ) => {
167+ const suppliedAsset = suppliesMap [ userReserve . underlyingAsset ] ;
168+ if ( suppliedAsset ) {
169+ const combinedScaledATokenBalance = combine (
170+ userReserve . scaledATokenBalance ,
171+ suppliedAsset . scaledATokenBalance
172+ ) ;
173+ //TODO: merge only if emode categories are the same?
174+ return {
175+ ...userReserve ,
176+ scaledATokenBalance : combinedScaledATokenBalance ,
177+ scaledVariableDebt : combine (
178+ userReserve . scaledVariableDebt ,
179+ suppliedAsset . scaledVariableDebt
180+ ) ,
181+ principalStableDebt : combine (
182+ userReserve . principalStableDebt ,
183+ suppliedAsset . principalStableDebt
184+ ) ,
185+ } ;
186+ }
187+ return userReserve ;
188+ } ) || [ ] ;
189+
190+ const baseCurrencyData = selectFormatBaseCurrencyData ( poolReserveV3 ) ;
191+ const userEmodeCategoryId = selectFormatUserEmodeCategoryId ( poolReserveV3 ) ;
192+
193+ return selectFormatUserSummaryForMigration (
194+ poolReserveV3 ?. reserves ,
195+ userReserves ,
196+ baseCurrencyData ,
197+ userEmodeCategoryId ,
198+ currentTimestamp
199+ ) ;
200+ } ;
201+
202+ export const selectV3UserSummary = ( store : RootStore , timestamp : number ) => {
203+ const poolReserveV3 = selectCurrentChainIdV3MarketData ( store ) ;
204+ const baseCurrencyData = selectFormatBaseCurrencyData ( poolReserveV3 ) ;
205+ const userEmodeCategoryId = selectFormatUserEmodeCategoryId ( poolReserveV3 ) ;
206+
207+ return selectFormatUserSummaryForMigration (
208+ poolReserveV3 ?. reserves ,
209+ poolReserveV3 ?. userReserves ,
210+ baseCurrencyData ,
211+ userEmodeCategoryId ,
212+ timestamp
213+ ) ;
90214} ;
0 commit comments