From b2ea422a0c8990a9472c332310c00bca87b318f8 Mon Sep 17 00:00:00 2001 From: ianwoodard <17186604+IanWoodard@users.noreply.github.com> Date: Mon, 30 Oct 2023 22:48:27 -0700 Subject: [PATCH] Add fuse 2 borrower nft fetching --- earn/src/data/BorrowerNft.ts | 70 +++++++++++++++++++++ earn/src/pages/MarketsPage.tsx | 8 +++ shared/src/data/constants/Addresses.ts | 4 ++ shared/src/data/constants/ChainSpecific.tsx | 9 +++ 4 files changed, 91 insertions(+) diff --git a/earn/src/data/BorrowerNft.ts b/earn/src/data/BorrowerNft.ts index ab6cb360..4186984a 100644 --- a/earn/src/data/BorrowerNft.ts +++ b/earn/src/data/BorrowerNft.ts @@ -1,10 +1,13 @@ import { ContractCallContext, Multicall } from 'ethereum-multicall'; import { BigNumber, ethers } from 'ethers'; +import { borrowerAbi } from 'shared/lib/abis/Borrower'; import { borrowerLensAbi } from 'shared/lib/abis/BorrowerLens'; import { borrowerNftAbi } from 'shared/lib/abis/BorrowerNft'; import { ALOE_II_BORROWER_LENS_ADDRESS, ALOE_II_BORROWER_NFT_ADDRESS, + ALOE_II_PERMIT2_MANAGER_ADDRESS, + ALOE_II_SIMPLE_MANAGER_ADDRESS, MULTICALL_ADDRESS, } from 'shared/lib/data/constants/ChainSpecific'; import { Address } from 'wagmi'; @@ -120,3 +123,70 @@ export async function fetchListOfBorrowerNfts( return { borrowers, tokenIds, indices }; } + +export async function fetchListOfFuse2BorrowNfts( + chainId: number, + provider: ethers.providers.BaseProvider, + userAddress: Address, + uniswapPool?: Address +): Promise< + Array<{ + borrowerAddress: Address; + tokenId: string; + index: number; + }> +> { + const originalBorrowerNfts = await fetchListOfBorrowerNfts(chainId, provider, userAddress, { + includeFreshBorrowers: false, // TODO: change later + onlyCheckMostRecentModify: true, // TODO: Hayden has concerns (as usual) + validManagerSet: new Set([ALOE_II_SIMPLE_MANAGER_ADDRESS[chainId], ALOE_II_PERMIT2_MANAGER_ADDRESS[chainId]]), + validUniswapPool: uniswapPool, + }); + + const slot0Contexts: ContractCallContext[] = originalBorrowerNfts.borrowers.map((borrower) => { + return { + abi: borrowerAbi as any, + calls: [ + { + methodName: 'slot0', + methodParameters: [], + reference: 'slot0', + }, + ], + contractAddress: borrower, + reference: borrower, + }; + }); + + // Execute multicall fetch + const multicall = new Multicall({ + ethersProvider: provider, + tryAggregate: true, + multicallCustomContractAddress: MULTICALL_ADDRESS[chainId], + }); + + const slot0Results = await multicall.call(slot0Contexts); + + const filterMap = originalBorrowerNfts.borrowers.map((borrower) => { + const result = slot0Results.results[borrower]; + const slot0Hex = result.callsReturnContext[0].returnValues[0].hex; + const extraDataHex: string = slot0Hex.slice(14, 30); + return extraDataHex.endsWith('83ee755b'); + }); + + const borrowerNfts: Array<{ + borrowerAddress: Address; + tokenId: string; + index: number; + }> = []; + for (let i = 0; i < originalBorrowerNfts.borrowers.length; i++) { + if (!filterMap[i]) continue; + borrowerNfts.push({ + borrowerAddress: originalBorrowerNfts.borrowers[i], + index: originalBorrowerNfts.indices[i], + tokenId: originalBorrowerNfts.tokenIds[i], + }); + } + + return borrowerNfts; +} diff --git a/earn/src/pages/MarketsPage.tsx b/earn/src/pages/MarketsPage.tsx index 6a8de693..e5d444b6 100644 --- a/earn/src/pages/MarketsPage.tsx +++ b/earn/src/pages/MarketsPage.tsx @@ -25,6 +25,7 @@ import { import { fetchMarginAccounts, MarginAccount } from '../data/MarginAccount'; import { PriceRelayLatestResponse } from '../data/PriceRelayResponse'; import { getProminentColor } from '../util/Colors'; +// import { fetchListOfFuse2BorrowNfts } from '../data/BorrowerNft'; export type TokenQuote = { token: Token; @@ -149,6 +150,13 @@ export default function MarketsPage() { })(); }, [userAddress, borrowerLensContract, provider, availablePools, setMarginAccounts]); + useEffect(() => { + (async () => { + if (userAddress === undefined) return; + //const fuse2BorrowerNfts = await fetchListOfFuse2BorrowNfts(activeChain.id, provider, userAddress); + })(); + }, [activeChain.id, provider, userAddress]); + const combinedBalances: TokenBalance[] = useMemo(() => { if (tokenQuotes.length === 0) { return []; diff --git a/shared/src/data/constants/Addresses.ts b/shared/src/data/constants/Addresses.ts index 2ed346a2..891154de 100644 --- a/shared/src/data/constants/Addresses.ts +++ b/shared/src/data/constants/Addresses.ts @@ -51,3 +51,7 @@ export const ALOE_II_BOOST_MANAGER_BASE = '0xAAE7aB9b8FBA833699842aC5f198CEFE384 export const ALOE_II_BORROWER_NFT_ADDRESS_OPTIMISM = '0x460d71e21e1768d943B1573B8B71399Ee7d84134'; export const ALOE_II_BORROWER_NFT_ADDRESS_ARBITRUM = '0x5e8D8Fb3Dd504bE6dCb535eaaacC0cdDFa086C9e'; export const ALOE_II_BORROWER_NFT_ADDRESS_BASE = '0xFB7d202BaFfeDc92Efc3E32aDB00B80F459FA9C5'; + +export const ALOE_II_PERMIT2_MANAGER_ADDRESS_OPTIMISM = '0xF9FBA8a3f471B78127b60eA343FD73ad20C15578'; +export const ALOE_II_PERMIT2_MANAGER_ADDRESS_ARBITRUM = '0xac340D6428052e396640726ed278774C738cfC4F'; +export const ALOE_II_PERMIT2_MANAGER_ADDRESS_BASE = '0xF9FBA8a3f471B78127b60eA343FD73ad20C15578'; // TODO: update diff --git a/shared/src/data/constants/ChainSpecific.tsx b/shared/src/data/constants/ChainSpecific.tsx index c0ded40a..1cec84d1 100644 --- a/shared/src/data/constants/ChainSpecific.tsx +++ b/shared/src/data/constants/ChainSpecific.tsx @@ -45,6 +45,9 @@ import { ALOE_II_BOOST_MANAGER_OPTIMISM, ALOE_II_BOOST_MANAGER_ARBITRUM, ALOE_II_BOOST_MANAGER_BASE, + ALOE_II_PERMIT2_MANAGER_ADDRESS_OPTIMISM, + ALOE_II_PERMIT2_MANAGER_ADDRESS_ARBITRUM, + ALOE_II_PERMIT2_MANAGER_ADDRESS_BASE, } from './Addresses'; export const BRIDGE_SUPPORTED_CHAINS = [mainnet, optimism, arbitrum]; @@ -152,3 +155,9 @@ export const ALOE_II_BOOST_MANAGER_ADDRESS: { [chainId: number]: Address } = { [arbitrum.id]: ALOE_II_BOOST_MANAGER_ARBITRUM, [base.id]: ALOE_II_BOOST_MANAGER_BASE, }; + +export const ALOE_II_PERMIT2_MANAGER_ADDRESS: { [chainId: number]: Address } = { + [optimism.id]: ALOE_II_PERMIT2_MANAGER_ADDRESS_OPTIMISM, + [arbitrum.id]: ALOE_II_PERMIT2_MANAGER_ADDRESS_ARBITRUM, + [base.id]: ALOE_II_PERMIT2_MANAGER_ADDRESS_BASE, +};