-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Money Market user data on wallet page
- Loading branch information
Showing
6 changed files
with
132 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { UiPoolDataProvider } from "@aave/contract-helpers" | ||
import { formatReserves, formatUserSummary } from "@aave/math-utils" | ||
import { useQuery } from "@tanstack/react-query" | ||
import { isTestnetRpcUrl } from "api/provider" | ||
import { useRpcProvider } from "providers/rpcProvider" | ||
import { | ||
AaveV3HydrationMainnet, | ||
AaveV3HydrationTestnet, | ||
} from "sections/lending/ui-config/addresses" | ||
import { useEvmAccount } from "sections/web3-connect/Web3Connect.utils" | ||
import { QUERY_KEYS } from "utils/queryKeys" | ||
|
||
export const useUserBorrowSummary = () => { | ||
const { api, evm, isLoaded } = useRpcProvider() | ||
const { account } = useEvmAccount() | ||
|
||
const address = account?.address ?? "" | ||
|
||
return useQuery( | ||
QUERY_KEYS.borrowUserSummary(address), | ||
async () => { | ||
const isTestnet = isTestnetRpcUrl(evm.connection.url) | ||
|
||
const addresses = isTestnet | ||
? AaveV3HydrationTestnet | ||
: AaveV3HydrationMainnet | ||
|
||
const poolDataContract = new UiPoolDataProvider({ | ||
uiPoolDataProviderAddress: addresses.UI_POOL_DATA_PROVIDER, | ||
provider: evm, | ||
chainId: parseFloat(import.meta.env.VITE_EVM_CHAIN_ID), | ||
}) | ||
|
||
const [reserves, user, timestamp] = await Promise.all([ | ||
poolDataContract.getReservesHumanized({ | ||
lendingPoolAddressProvider: addresses.POOL_ADDRESSES_PROVIDER, | ||
}), | ||
poolDataContract.getUserReservesHumanized({ | ||
lendingPoolAddressProvider: addresses.POOL_ADDRESSES_PROVIDER, | ||
user: address, | ||
}), | ||
api.query.timestamp.now(), | ||
]) | ||
|
||
const { baseCurrencyData, reservesData } = reserves | ||
const { userEmodeCategoryId, userReserves } = user | ||
|
||
const currentTimestamp = timestamp.toNumber() / 1000 | ||
|
||
const formattedReserves = formatReserves({ | ||
currentTimestamp, | ||
reserves: reservesData, | ||
marketReferencePriceInUsd: | ||
baseCurrencyData.marketReferenceCurrencyPriceInUsd, | ||
marketReferenceCurrencyDecimals: | ||
baseCurrencyData.marketReferenceCurrencyDecimals, | ||
}) | ||
|
||
return formatUserSummary({ | ||
currentTimestamp, | ||
marketReferencePriceInUsd: | ||
baseCurrencyData.marketReferenceCurrencyPriceInUsd, | ||
marketReferenceCurrencyDecimals: | ||
baseCurrencyData.marketReferenceCurrencyDecimals, | ||
userReserves, | ||
formattedReserves, | ||
userEmodeCategoryId, | ||
}) | ||
}, | ||
{ | ||
enabled: isLoaded && !!address, | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { getNetwork, JsonRpcProvider, Network } from "@ethersproject/providers" | ||
import { ApiPromise, WsProvider } from "@polkadot/api" | ||
|
||
export class PolkadotEvmRpcProvider extends JsonRpcProvider { | ||
provider: WsProvider | ||
|
||
constructor(api: ApiPromise) { | ||
const provider = PolkadotEvmRpcProvider.getProviderInstance(api) | ||
const path = provider.endpoint | ||
super(path) | ||
this.provider = provider | ||
} | ||
|
||
async _uncachedDetectNetwork(): Promise<Network> { | ||
const chainId = await this.send("eth_chainId", []) | ||
return getNetwork(parseInt(chainId, 16)) | ||
} | ||
|
||
static getProviderInstance(api: ApiPromise) { | ||
// @ts-expect-error Property '_options' is protected | ||
const options = api?._options | ||
return options?.provider as WsProvider | ||
} | ||
|
||
send(method: string, params: Array<any> = []): Promise<any> { | ||
return this.provider.send(method, params) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters