Skip to content

Commit 5d314d7

Browse files
committed
feature: select current market v2 and v3 dynamic selectors added
1 parent 4d71cd6 commit 5d314d7

File tree

4 files changed

+55
-26
lines changed

4 files changed

+55
-26
lines changed

pages/v3-migration.page.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Trans } from '@lingui/macro';
22
import { Box, Divider } from '@mui/material';
3+
import { useEffect } from 'react';
34
import { ConnectWalletPaper } from 'src/components/ConnectWalletPaper';
45
import { ContentContainer } from 'src/components/ContentContainer';
56
import { MigrateV3Modal } from 'src/components/transactions/MigrateV3/MigrateV3Modal';
@@ -20,6 +21,7 @@ export default function V3Migration() {
2021
const { loading } = useAppDataContext();
2122
const { currentAccount, loading: web3Loading } = useWeb3Context();
2223
const { isPermissionsLoading } = usePermissions();
24+
const setCurrentMarketForMigration = useRootStore((state) => state.setCurrentMarketForMigration);
2325

2426
const { user, borrowPositions } = useUserReserves();
2527

@@ -30,7 +32,12 @@ export default function V3Migration() {
3032
selectedMigrationBorrowAssets: selectedBorrowAssets,
3133
} = useRootStore();
3234

33-
// start refreshing v3 market at the same time
35+
useEffect(() => {
36+
if (setCurrentMarketForMigration) {
37+
setCurrentMarketForMigration();
38+
}
39+
}, [setCurrentMarketForMigration]);
40+
3441
usePoolDataV3Subscription();
3542

3643
return (

src/store/poolSelectors.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
import { formatReservesAndIncentives, formatUserSummaryAndIncentives } from '@aave/math-utils';
22
import { EmodeCategory } from 'src/helpers/types';
33
import { fetchIconSymbolAndName, STABLE_ASSETS } from 'src/ui-config/reservePatches';
4+
import { CustomMarket, MarketDataType, marketsData } from 'src/utils/marketsAndNetworksConfig';
45

56
import { RootStore } from './root';
67

8+
export const selectCurrentChainIdV2MarketKey = (state: RootStore): CustomMarket => {
9+
// TODO: make it dynamic
10+
return 'fork_proto_polygon' as CustomMarket;
11+
};
12+
13+
export const selectCurrentChainIdV3MarketKey = (state: RootStore): CustomMarket => {
14+
// TODO: make it dynamic
15+
return 'fork_proto_polygon_v3' as CustomMarket;
16+
};
17+
18+
export const selectCurrentChainIdV2MarketData = (state: RootStore) => {
19+
const v2MarketKey = selectCurrentChainIdV2MarketKey(state);
20+
const marketData = marketsData[v2MarketKey];
21+
return state.data
22+
.get(state.currentChainId)
23+
?.get(marketData.addresses.LENDING_POOL_ADDRESS_PROVIDER);
24+
};
25+
726
export const selectCurrentUserLendingPoolData = (state: RootStore) => {
827
return state.data
928
.get(state.currentChainId)
1029
?.get(state.currentMarketData.addresses.LENDING_POOL_ADDRESS_PROVIDER);
1130
};
1231

13-
export const selectLendingPoolDataV2 = (state: RootStore) => {};
14-
1532
export const selectCurrentUserEmodeCategoryId = (state: RootStore): number => {
1633
return selectCurrentUserLendingPoolData(state)?.userEmodeCategoryId || 0;
1734
};

src/store/poolSlice.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ import { availableMarkets, marketsData, networkConfigs } from 'src/utils/markets
3939
import { optimizedPath } from 'src/utils/utils';
4040
import { StateCreator } from 'zustand';
4141

42-
import { selectFormattedReserves } from './poolSelectors';
42+
import {
43+
selectCurrentChainIdV3MarketKey,
44+
selectFormattedReserves,
45+
} from './poolSelectors';
4346
import { RootStore } from './root';
4447

4548
// TODO: add chain/provider/account mapping
@@ -199,7 +202,8 @@ export const createPoolSlice: StateCreator<
199202
},
200203
refreshPoolV3Data: async () => {
201204
// how to determine which v2 markets to pool? for now always fetch polygon fork
202-
const v3MarketData = marketsData['fork_proto_polygon_v3'];
205+
const marketKey = selectCurrentChainIdV3MarketKey(get());
206+
const v3MarketData = marketsData[marketKey];
203207
get().refreshPoolData(v3MarketData);
204208
},
205209
mint: async (args) => {

src/store/v3MigrationSlice.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ import dayjs from 'dayjs';
1414
import { BigNumber, BigNumberish, constants } from 'ethers';
1515
import { produce } from 'immer';
1616
import { Approval } from 'src/helpers/useTransactionHandler';
17+
import { CustomMarket } from 'src/ui-config/marketsConfig';
18+
import { marketsData } from 'src/utils/marketsAndNetworksConfig';
1719
import { StateCreator } from 'zustand';
20+
import {
21+
selectCurrentChainIdV2MarketData,
22+
selectCurrentChainIdV2MarketKey,
23+
selectCurrentChainIdV3MarketKey,
24+
} from './poolSelectors';
1825

1926
import { RootStore } from './root';
2027
import {
@@ -51,8 +58,7 @@ export type V3MigrationSlice = {
5158
migrateBorrow: (
5259
signedPermits?: V3MigrationHelperSignedPermit[]
5360
) => Promise<EthereumTransactionTypeExtended[]>;
54-
_testMigration: () => void;
55-
// migrateSelectedPositions: () => void;
61+
setCurrentMarketForMigration: () => void;
5662
};
5763

5864
export const createV3MigrationSlice: StateCreator<
@@ -142,13 +148,11 @@ export const createV3MigrationSlice: StateCreator<
142148
const timestamp = dayjs().unix();
143149
const approvalPermitsForMigrationAssets = await Promise.all(
144150
selectUserSupplyIncreasedReservesForMigrationPermits(get(), timestamp).map(
145-
async ({ reserve, increasedAmount, underlyingBalance }): Promise<Approval> => {
146-
console.log(underlyingBalance, 'underlyingBalance');
151+
async ({ reserve, increasedAmount }): Promise<Approval> => {
147152
const { getTokenData } = new ERC20Service(get().jsonRpcProvider());
148153
const { decimals } = await getTokenData(reserve.aTokenAddress);
149154
const convertedAmount = valueToWei(increasedAmount, decimals);
150155
return {
151-
// TODO: should we allow spending of exact ammount of the reserver?
152156
amount: convertedAmount,
153157
underlyingAsset: reserve.aTokenAddress,
154158
permitType: 'MIGRATOR',
@@ -160,7 +164,6 @@ export const createV3MigrationSlice: StateCreator<
160164
return approvalPermitsForMigrationAssets;
161165
},
162166
migrateWithoutPermits: () => {
163-
console.log('should be called before gas');
164167
const timestamp = dayjs().unix();
165168
set({ timestamp });
166169
const borrowedPositions = selectUserBorrowReservesForMigration(get(), timestamp);
@@ -206,28 +209,21 @@ export const createV3MigrationSlice: StateCreator<
206209
}
207210
const provider = get().jsonRpcProvider();
208211
const migratorAddress = get().getMigratorAddress();
209-
const currentMarketData = get().currentMarketData;
212+
210213
// TODO: make it dynamic when network switch will be there
214+
const v3MarketKey = selectCurrentChainIdV3MarketKey(get());
215+
const currentMarketV3Data = marketsData[v3MarketKey];
211216
const pool = new Pool(provider, {
212-
POOL: '0x794a61358D6845594F94dc1DB02A252b5b4814aD',
213-
REPAY_WITH_COLLATERAL_ADAPTER: currentMarketData.addresses.REPAY_WITH_COLLATERAL_ADAPTER,
214-
SWAP_COLLATERAL_ADAPTER: currentMarketData.addresses.SWAP_COLLATERAL_ADAPTER,
215-
WETH_GATEWAY: currentMarketData.addresses.WETH_GATEWAY,
216-
L2_ENCODER: currentMarketData.addresses.L2_ENCODER,
217+
POOL: currentMarketV3Data.addresses.LENDING_POOL,
218+
REPAY_WITH_COLLATERAL_ADAPTER: currentMarketV3Data.addresses.REPAY_WITH_COLLATERAL_ADAPTER,
219+
SWAP_COLLATERAL_ADAPTER: currentMarketV3Data.addresses.SWAP_COLLATERAL_ADAPTER,
220+
WETH_GATEWAY: currentMarketV3Data.addresses.WETH_GATEWAY,
221+
L2_ENCODER: currentMarketV3Data.addresses.L2_ENCODER,
217222
});
218223
const newMigratorInstance = new V3MigrationHelperService(provider, migratorAddress, pool);
219224
// TODO: don't forget to add maping here
220225
return newMigratorInstance;
221226
},
222-
_testMigration: async () => {
223-
const currentTimestamp = dayjs().unix();
224-
const selectedReservers = selectedUserSupplyReservesForMigration(get(), currentTimestamp);
225-
const mappedAddresses = await Promise.all(
226-
selectedReservers.map((reserve) =>
227-
get().getMigrationServiceInstance().testDeployment(reserve.underlyingAsset)
228-
)
229-
);
230-
},
231227
migrateBorrow: async (signedPermits: V3MigrationHelperSignedPermit[] = []) => {
232228
const currentTimestamp = dayjs().unix();
233229
const user = get().account;
@@ -249,5 +245,10 @@ export const createV3MigrationSlice: StateCreator<
249245
signedPermits,
250246
});
251247
},
248+
setCurrentMarketForMigration: () => {
249+
const newMarketData = selectCurrentChainIdV2MarketKey(get());
250+
// TOOD: fallback to mainnet if newMarketData do not support v2 and v3
251+
get().setCurrentMarket(newMarketData);
252+
},
252253
};
253254
};

0 commit comments

Comments
 (0)