diff --git a/liquidity/lib/useAccountCollateral/useAccountCollateral.ts b/liquidity/lib/useAccountCollateral/useAccountCollateral.ts index 394ed3e9f..8efcb3a01 100644 --- a/liquidity/lib/useAccountCollateral/useAccountCollateral.ts +++ b/liquidity/lib/useAccountCollateral/useAccountCollateral.ts @@ -5,7 +5,7 @@ import { useDefaultProvider, useNetwork } from '@snx-v3/useBlockchain'; import { Wei, wei } from '@synthetixio/wei'; import { useCollateralTypes } from '@snx-v3/useCollateralTypes'; import { erc7412Call } from '@snx-v3/withERC7412'; -import { useCollateralPriceUpdates } from '../useCollateralPriceUpdates'; +import { useAllCollateralPriceUpdates } from '../useCollateralPriceUpdates'; export type AccountCollateralType = { tokenAddress: string; @@ -68,7 +68,7 @@ export function useAccountCollateral({ const collateralTypes = useCollateralTypes(includeDelegationOff); const tokenAddresses = collateralTypes.data?.map((c) => c.tokenAddress) ?? []; const provider = useDefaultProvider(); - const { data: priceUpdateTx } = useCollateralPriceUpdates(); + const { data: priceUpdateTx } = useAllCollateralPriceUpdates(); return useQuery({ queryKey: [ @@ -106,7 +106,7 @@ export function useAccountSpecificCollateral(accountId?: string, collateralAddre const { data: CoreProxy } = useCoreProxy(); const { network } = useNetwork(); const provider = useDefaultProvider(); - const { data: priceUpdateTx } = useCollateralPriceUpdates(); + const { data: priceUpdateTx } = useAllCollateralPriceUpdates(); return useQuery({ queryKey: [ diff --git a/liquidity/lib/useClearDebt/package.json b/liquidity/lib/useClearDebt/package.json index 67b59cb6e..acd8211ef 100644 --- a/liquidity/lib/useClearDebt/package.json +++ b/liquidity/lib/useClearDebt/package.json @@ -4,7 +4,6 @@ "main": "index.ts", "version": "0.0.1", "dependencies": { - "@snx-v3/fetchPythPrices": "workspace:*", "@snx-v3/isBaseAndromeda": "workspace:*", "@snx-v3/tsHelpers": "workspace:*", "@snx-v3/txnReducer": "workspace:*", diff --git a/liquidity/lib/useClearDebt/useClearDebt.tsx b/liquidity/lib/useClearDebt/useClearDebt.tsx index fa7f1aaed..d37764282 100644 --- a/liquidity/lib/useClearDebt/useClearDebt.tsx +++ b/liquidity/lib/useClearDebt/useClearDebt.tsx @@ -11,9 +11,9 @@ import { useGasSpeed } from '@snx-v3/useGasSpeed'; import { notNil } from '@snx-v3/tsHelpers'; import { withERC7412 } from '@snx-v3/withERC7412'; import { useAllCollateralPriceIds } from '@snx-v3/useAllCollateralPriceIds'; -import { fetchPriceUpdates, priceUpdatesToPopulatedTx } from '@snx-v3/fetchPythPrices'; import { useSpotMarketProxy } from '../useSpotMarketProxy'; import { USDC_BASE_MARKET, getRepayerContract } from '@snx-v3/isBaseAndromeda'; +import { useCollateralPriceUpdates } from '../useCollateralPriceUpdates'; export const DEBT_REPAYER_ABI = [ { @@ -49,6 +49,7 @@ export const useClearDebt = ({ const { data: CoreProxy } = useCoreProxy(); const { data: SpotMarketProxy } = useSpotMarketProxy(); const { data: collateralPriceIds } = useAllCollateralPriceIds(); + const { data: priceUpdateTx } = useCollateralPriceUpdates(); const signer = useSigner(); const { network } = useNetwork(); @@ -97,24 +98,13 @@ export const useClearDebt = ({ const callsPromise = Promise.all([depositDebtToRepay, burn].filter(notNil)); - const walletAddress = await signer.getAddress(); + const [calls, gasPrices] = await Promise.all([callsPromise, getGasPrice({ provider })]); - const collateralPriceCallsPromise = fetchPriceUpdates( - collateralPriceIds, - network.isTestnet - ).then((signedData) => - priceUpdatesToPopulatedTx(walletAddress, collateralPriceIds, signedData) - ); - - const [calls, gasPrices, collateralPriceCalls] = await Promise.all([ - callsPromise, - getGasPrice({ provider }), - collateralPriceCallsPromise, - ]); - - const allCalls = collateralPriceCalls.concat(calls); + if (priceUpdateTx) { + calls.unshift(priceUpdateTx as any); + } - const erc7412Tx = await withERC7412(network, allCalls, 'useRepay', CoreProxy.interface); + const erc7412Tx = await withERC7412(network, calls, 'useRepay', CoreProxy.interface); const gasOptionsForTransaction = formatGasPriceForTransaction({ gasLimit: erc7412Tx.gasLimit, diff --git a/liquidity/lib/useCollateralPriceUpdates/package.json b/liquidity/lib/useCollateralPriceUpdates/package.json index 622d4e442..ede5fb276 100644 --- a/liquidity/lib/useCollateralPriceUpdates/package.json +++ b/liquidity/lib/useCollateralPriceUpdates/package.json @@ -9,6 +9,7 @@ "@snx-v3/isBaseAndromeda": "workspace:*", "@snx-v3/useBlockchain": "workspace:*", "@snx-v3/withERC7412": "workspace:*", + "@synthetixio/v3-contracts": "workspace:*", "@tanstack/react-query": "^5.8.3", "ethers": "^5.7.2" } diff --git a/liquidity/lib/useCollateralPriceUpdates/useCollateralPriceUpdates.ts b/liquidity/lib/useCollateralPriceUpdates/useCollateralPriceUpdates.ts index 035ad88f2..e25abf0ce 100644 --- a/liquidity/lib/useCollateralPriceUpdates/useCollateralPriceUpdates.ts +++ b/liquidity/lib/useCollateralPriceUpdates/useCollateralPriceUpdates.ts @@ -1,11 +1,12 @@ import { useQuery } from '@tanstack/react-query'; import { ethers } from 'ethers'; -import { useNetwork } from '@snx-v3/useBlockchain'; +import { Network, useDefaultProvider, useNetwork, useWallet } from '@snx-v3/useBlockchain'; import { EvmPriceServiceConnection } from '@pythnetwork/pyth-evm-js'; import { offchainMainnetEndpoint } from '@snx-v3/constants'; import { ERC7412_ABI } from '@snx-v3/withERC7412'; import { getsPythWrapper, isBaseAndromeda } from '@snx-v3/isBaseAndromeda'; +import { importMulticall3 } from '@synthetixio/v3-contracts'; const priceIds = [ '0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d', @@ -34,34 +35,117 @@ const priceIds = [ '0xec7a775f46379b5e943c3526b1c8d54cd49749176b0b98e02dde68d1bd335c17', '0x09f7c1d7dfbb7df2b8fe3d3d87ee94a2259d212da4f30c1f0540d066dfa44723', '0x8963217838ab4cf5cadc172203c1f0b763fbaa45f346d8ee50ba994bbcac3026', + '0x9a4df90b25497f66b1afb012467e316e801ca3d839456db028892fe8c70c8016', + '0x5fcf71143bb70d41af4fa9aa1287e2efd3c5911cee59f909f915c9f61baacb1e', ]; const priceService = new EvmPriceServiceConnection(offchainMainnetEndpoint); -export const useCollateralPriceUpdates = () => { +const getPriceUpdates = async ( + priceIds: string[], + stalenessTolerance: number, + network: Network | null +) => { + const signedOffchainData = await priceService.getPriceFeedsUpdateData(priceIds); + const updateType = 1; + const data = ethers.utils.defaultAbiCoder.encode( + ['uint8', 'uint64', 'bytes32[]', 'bytes[]'], + [updateType, stalenessTolerance, priceIds, signedOffchainData] + ); + const erc7412Interface = new ethers.utils.Interface(ERC7412_ABI); + + return { + //pyth wrapper + to: getsPythWrapper(network?.id), + data: erc7412Interface.encodeFunctionData('fulfillOracleQuery', [data]), + value: priceIds.length * 10, + }; +}; +export const useAllCollateralPriceUpdates = () => { const { network } = useNetwork(); return useQuery({ - queryKey: [`${network?.id}-${network?.preset}`, 'price-updates', priceIds.join(',')], + queryKey: [`${network?.id}-${network?.preset}`, 'all-price-updates', priceIds.join(',')], enabled: isBaseAndromeda(network?.id, network?.preset), queryFn: async () => { - const updateType = 1, - stalenessTolerance = 60; + const stalenessTolerance = 60; - const signedOffchainData = await priceService.getPriceFeedsUpdateData(priceIds); - - const data = ethers.utils.defaultAbiCoder.encode( - ['uint8', 'uint64', 'bytes32[]', 'bytes[]'], - [updateType, stalenessTolerance, priceIds, signedOffchainData] - ); - const erc7412Interface = new ethers.utils.Interface(ERC7412_ABI); + const tx = await getPriceUpdates(priceIds, stalenessTolerance, network); return { - //pyth wrapper - to: getsPythWrapper(network?.id), - data: erc7412Interface.encodeFunctionData('fulfillOracleQuery', [data]), - value: priceIds.length * 10, + ...tx, + value: tx.value * 10, }; }, }); }; + +export const useCollateralPriceUpdates = () => { + const { network } = useNetwork(); + const provider = useDefaultProvider(); + const { activeWallet } = useWallet(); + + return useQuery({ + queryKey: [`${network?.id}-${network?.preset}`, 'price-updates'], + enabled: isBaseAndromeda(network?.id, network?.preset), + queryFn: async () => { + const stalenessTolerance = 3300; + if (!network) { + return; + } + + try { + const { address: multicallAddress, abi: multiCallAbi } = await importMulticall3( + network.id, + network.preset + ); + + const multicallInterface = new ethers.utils.Interface(multiCallAbi); + const pythInterface = new ethers.utils.Interface([ + 'function getLatestPrice(bytes32 priceId, uint256 stalenessTolerance) external view returns (int256)', + ]); + + const txs = [ + ...priceIds.map((priceId) => ({ + target: getsPythWrapper(network.id), + callData: pythInterface.encodeFunctionData('getLatestPrice', [ + priceId, + stalenessTolerance, + ]), + value: 0, + requireSuccess: false, + })), + ]; + + const getPricesTx = multicallInterface.encodeFunctionData('aggregate3Value', [txs]); + + const result = await provider?.call({ + data: getPricesTx, + to: multicallAddress, + }); + + const decodedMultiCall: { success: boolean }[] = multicallInterface.decodeFunctionResult( + 'aggregate3Value', + result || '' + )[0]; + + const outdatedPriceIds: string[] = []; + + decodedMultiCall.forEach(({ success }, i) => { + if (!success) { + outdatedPriceIds.push(priceIds[i]); + } + }); + + if (outdatedPriceIds.length) { + return { + from: activeWallet?.address, + ...(await getPriceUpdates(outdatedPriceIds, stalenessTolerance, network)), + }; + } + } catch (error) { + return null; + } + }, + }); +}; diff --git a/liquidity/lib/useCollateralPrices/useCollateralPrices.ts b/liquidity/lib/useCollateralPrices/useCollateralPrices.ts index ef4469e90..4ca63e3bb 100644 --- a/liquidity/lib/useCollateralPrices/useCollateralPrices.ts +++ b/liquidity/lib/useCollateralPrices/useCollateralPrices.ts @@ -6,7 +6,7 @@ import Wei, { wei } from '@synthetixio/wei'; import { useDefaultProvider, useNetwork } from '@snx-v3/useBlockchain'; import { erc7412Call } from '@snx-v3/withERC7412'; import { useCollateralTypes } from '@snx-v3/useCollateralTypes'; -import { useCollateralPriceUpdates } from '../useCollateralPriceUpdates'; +import { useAllCollateralPriceUpdates } from '../useCollateralPriceUpdates'; const PriceSchema = ZodBigNumber.transform((x) => wei(x)); @@ -47,7 +47,7 @@ export const useCollateralPrices = () => { const provider = useDefaultProvider(); const collateralAddresses = collateralData?.map((x) => x.tokenAddress); - const { data: priceUpdateTx } = useCollateralPriceUpdates(); + const { data: priceUpdateTx } = useAllCollateralPriceUpdates(); return useQuery({ enabled: Boolean(CoreProxy && collateralAddresses && collateralAddresses?.length > 0), diff --git a/liquidity/lib/useDepositBaseAndromeda/package.json b/liquidity/lib/useDepositBaseAndromeda/package.json index b748cbe2d..9a21a25b1 100644 --- a/liquidity/lib/useDepositBaseAndromeda/package.json +++ b/liquidity/lib/useDepositBaseAndromeda/package.json @@ -4,12 +4,10 @@ "main": "index.ts", "version": "0.0.1", "dependencies": { - "@snx-v3/fetchPythPrices": "workspace:*", "@snx-v3/format": "workspace:*", "@snx-v3/isBaseAndromeda": "workspace:*", "@snx-v3/tsHelpers": "workspace:*", "@snx-v3/txnReducer": "workspace:*", - "@snx-v3/useAllCollateralPriceIds": "workspace:*", "@snx-v3/useApprove": "workspace:*", "@snx-v3/useBlockchain": "workspace:*", "@snx-v3/useCoreProxy": "workspace:*", diff --git a/liquidity/lib/useDepositBaseAndromeda/useDepositBaseAndromeda.tsx b/liquidity/lib/useDepositBaseAndromeda/useDepositBaseAndromeda.tsx index 7d9565d3a..9c4abf38c 100644 --- a/liquidity/lib/useDepositBaseAndromeda/useDepositBaseAndromeda.tsx +++ b/liquidity/lib/useDepositBaseAndromeda/useDepositBaseAndromeda.tsx @@ -11,12 +11,11 @@ import { getGasPrice } from '@snx-v3/useGasPrice'; import { useGasSpeed } from '@snx-v3/useGasSpeed'; import { withERC7412 } from '@snx-v3/withERC7412'; import { notNil } from '@snx-v3/tsHelpers'; -import { useAllCollateralPriceIds } from '@snx-v3/useAllCollateralPriceIds'; -import { fetchPriceUpdates, priceUpdatesToPopulatedTx } from '@snx-v3/fetchPythPrices'; import { useSpotMarketProxy } from '../useSpotMarketProxy'; import { parseUnits } from '@snx-v3/format'; import { USDC_BASE_MARKET, getsUSDCAddress } from '@snx-v3/isBaseAndromeda'; import { approveAbi } from '@snx-v3/useApprove'; +import { useCollateralPriceUpdates } from '../useCollateralPriceUpdates'; export const useDepositBaseAndromeda = ({ accountId, @@ -38,7 +37,7 @@ export const useDepositBaseAndromeda = ({ const [txnState, dispatch] = useReducer(reducer, initialState); const { data: CoreProxy } = useCoreProxy(); const { data: SpotMarketProxy } = useSpotMarketProxy(); - const { data: collateralPriceUpdates } = useAllCollateralPriceIds(); + const { data: priceUpdateTx } = useCollateralPriceUpdates(); const { gasSpeed } = useGasSpeed(); @@ -57,8 +56,7 @@ export const useDepositBaseAndromeda = ({ SpotMarketProxy && poolId && collateralTypeAddress && - availableCollateral && - collateralPriceUpdates + availableCollateral ) ) { return; @@ -67,7 +65,6 @@ export const useDepositBaseAndromeda = ({ try { dispatch({ type: 'prompting' }); - const walletAddress = await signer.getAddress(); const id = accountId ?? newAccountId; // create account only when no account exists @@ -111,24 +108,15 @@ export const useDepositBaseAndromeda = ({ [wrap, sUSDCApproval, createAccount, deposit, delegate].filter(notNil) ); - const collateralPriceCallsPromise = fetchPriceUpdates( - collateralPriceUpdates, - network?.isTestnet - ).then((signedData) => - priceUpdatesToPopulatedTx(walletAddress, collateralPriceUpdates, signedData) - ); - - const [calls, gasPrices, collateralPriceCalls] = await Promise.all([ - callsPromise, - getGasPrice({ provider }), - collateralPriceCallsPromise, - ]); + const [calls, gasPrices] = await Promise.all([callsPromise, getGasPrice({ provider })]); - const allCalls = collateralPriceCalls.concat(calls); + if (priceUpdateTx) { + calls.unshift(priceUpdateTx as any); + } const erc7412Tx = await withERC7412( network, - allCalls, + calls, 'useDepositBaseAndromeda', CoreProxy.interface ); diff --git a/liquidity/lib/useLiquidityPosition/useLiquidityPosition.ts b/liquidity/lib/useLiquidityPosition/useLiquidityPosition.ts index ab89702ac..a7555b5a0 100644 --- a/liquidity/lib/useLiquidityPosition/useLiquidityPosition.ts +++ b/liquidity/lib/useLiquidityPosition/useLiquidityPosition.ts @@ -11,7 +11,7 @@ import { loadAccountCollateral, AccountCollateralType } from '@snx-v3/useAccount import { useAllCollateralPriceIds } from '@snx-v3/useAllCollateralPriceIds'; import { fetchPriceUpdates, priceUpdatesToPopulatedTx } from '@snx-v3/fetchPythPrices'; import { useUSDProxy } from '@snx-v3/useUSDProxy'; -import { useCollateralPriceUpdates } from '../useCollateralPriceUpdates'; +import { useAllCollateralPriceUpdates } from '../useCollateralPriceUpdates'; const PositionCollateralSchema = z.object({ value: ZodBigNumber.transform((x) => wei(x)).optional(), // This is currently only removed on base-goreli @@ -80,7 +80,7 @@ export const useLiquidityPosition = ({ const { data: CoreProxy } = useCoreProxy(); const { data: UsdProxy } = useUSDProxy(); const { network } = useNetwork(); - const { data: priceUpdateTx } = useCollateralPriceUpdates(); + const { data: priceUpdateTx } = useAllCollateralPriceUpdates(); const provider = useProviderForChain(network!); return useQuery({ diff --git a/liquidity/lib/useLiquidityPositions/useLiquidityPositions.ts b/liquidity/lib/useLiquidityPositions/useLiquidityPositions.ts index 279769cef..5645bf2e6 100644 --- a/liquidity/lib/useLiquidityPositions/useLiquidityPositions.ts +++ b/liquidity/lib/useLiquidityPositions/useLiquidityPositions.ts @@ -11,7 +11,7 @@ import { erc7412Call } from '@snx-v3/withERC7412'; import { keyBy } from '@snx-v3/tsHelpers'; import { useAllCollateralPriceIds } from '@snx-v3/useAllCollateralPriceIds'; import { fetchPriceUpdates, priceUpdatesToPopulatedTx } from '@snx-v3/fetchPythPrices'; -import { useCollateralPriceUpdates } from '../useCollateralPriceUpdates'; +import { useAllCollateralPriceUpdates } from '../useCollateralPriceUpdates'; export type LiquidityPositionType = { id: `${string}-${string}`; @@ -43,7 +43,7 @@ export const useLiquidityPositions = ({ accountId }: { accountId?: string }) => const { data: pools } = usePools(); const { data: collateralTypes } = useCollateralTypes(); const { data: collateralPriceUpdates } = useAllCollateralPriceIds(); - const { data: priceUpdateTx } = useCollateralPriceUpdates(); + const { data: priceUpdateTx } = useAllCollateralPriceUpdates(); const { network } = useNetwork(); const provider = useProviderForChain(network!); diff --git a/liquidity/lib/usePoolConfiguration/usePoolConfiguration.ts b/liquidity/lib/usePoolConfiguration/usePoolConfiguration.ts index 092378400..47b1bb53e 100644 --- a/liquidity/lib/usePoolConfiguration/usePoolConfiguration.ts +++ b/liquidity/lib/usePoolConfiguration/usePoolConfiguration.ts @@ -7,7 +7,7 @@ import { ethers } from 'ethers'; import { erc7412Call } from '@snx-v3/withERC7412'; import { fetchPriceUpdates, priceUpdatesToPopulatedTx } from '@snx-v3/fetchPythPrices'; import { useAllCollateralPriceIds } from '@snx-v3/useAllCollateralPriceIds'; -import { useCollateralPriceUpdates } from '../useCollateralPriceUpdates'; +import { useAllCollateralPriceUpdates } from '../useCollateralPriceUpdates'; export const MarketConfigurationSchema = z.object({ id: SmallIntSchema, @@ -29,7 +29,7 @@ export const usePoolConfiguration = (poolId?: string) => { const { data: CoreProxy } = useCoreProxy(); const { data: collateralPriceUpdates } = useAllCollateralPriceIds(); const provider = useDefaultProvider(); - const { data: priceUpdateTx } = useCollateralPriceUpdates(); + const { data: priceUpdateTx } = useAllCollateralPriceUpdates(); return useQuery({ enabled: Boolean(CoreProxy && poolId && collateralPriceUpdates), diff --git a/liquidity/lib/useRepayBaseAndromeda/package.json b/liquidity/lib/useRepayBaseAndromeda/package.json index 628b1e6f2..6d436d8de 100644 --- a/liquidity/lib/useRepayBaseAndromeda/package.json +++ b/liquidity/lib/useRepayBaseAndromeda/package.json @@ -4,12 +4,10 @@ "main": "index.ts", "version": "0.0.1", "dependencies": { - "@snx-v3/fetchPythPrices": "workspace:*", "@snx-v3/format": "workspace:*", "@snx-v3/isBaseAndromeda": "workspace:*", "@snx-v3/tsHelpers": "workspace:*", "@snx-v3/txnReducer": "workspace:*", - "@snx-v3/useAllCollateralPriceIds": "workspace:*", "@snx-v3/useApprove": "workspace:*", "@snx-v3/useBlockchain": "workspace:*", "@snx-v3/useCoreProxy": "workspace:*", diff --git a/liquidity/lib/useRepayBaseAndromeda/useRepayBaseAndromeda.tsx b/liquidity/lib/useRepayBaseAndromeda/useRepayBaseAndromeda.tsx index 5c22d57ec..2249a40b7 100644 --- a/liquidity/lib/useRepayBaseAndromeda/useRepayBaseAndromeda.tsx +++ b/liquidity/lib/useRepayBaseAndromeda/useRepayBaseAndromeda.tsx @@ -11,12 +11,11 @@ import { useGasSpeed } from '@snx-v3/useGasSpeed'; import { useUSDProxy } from '@snx-v3/useUSDProxy'; import { notNil } from '@snx-v3/tsHelpers'; import { withERC7412 } from '@snx-v3/withERC7412'; -import { useAllCollateralPriceIds } from '@snx-v3/useAllCollateralPriceIds'; -import { fetchPriceUpdates, priceUpdatesToPopulatedTx } from '@snx-v3/fetchPythPrices'; import { useSpotMarketProxy } from '../useSpotMarketProxy'; import { USDC_BASE_MARKET, getsUSDCAddress } from '@snx-v3/isBaseAndromeda'; import { parseUnits } from '@snx-v3/format'; import { approveAbi } from '@snx-v3/useApprove'; +import { useCollateralPriceUpdates } from '../useCollateralPriceUpdates'; export const useRepayBaseAndromeda = ({ accountId, @@ -35,7 +34,7 @@ export const useRepayBaseAndromeda = ({ const { data: CoreProxy } = useCoreProxy(); const { data: UsdProxy } = useUSDProxy(); const { data: SpotMarketProxy } = useSpotMarketProxy(); - const { data: collateralPriceIds } = useAllCollateralPriceIds(); + const { data: priceUpdateTx } = useCollateralPriceUpdates(); const signer = useSigner(); const { network } = useNetwork(); @@ -47,15 +46,7 @@ export const useRepayBaseAndromeda = ({ if (!signer || !network || !provider) throw new Error('No signer or network'); if ( - !( - CoreProxy && - poolId && - accountId && - collateralTypeAddress && - UsdProxy && - SpotMarketProxy && - collateralPriceIds - ) + !(CoreProxy && poolId && accountId && collateralTypeAddress && UsdProxy && SpotMarketProxy) ) { return; } @@ -123,24 +114,12 @@ export const useRepayBaseAndromeda = ({ [wrap, sUSDC_Approval, sell, sUSD_Approval, deposit, burn].filter(notNil) ); - const walletAddress = await signer.getAddress(); + const [calls, gasPrices] = await Promise.all([callsPromise, getGasPrice({ provider })]); + if (priceUpdateTx) { + calls.push(priceUpdateTx as any); + } - const collateralPriceCallsPromise = fetchPriceUpdates( - collateralPriceIds, - network.isTestnet - ).then((signedData) => - priceUpdatesToPopulatedTx(walletAddress, collateralPriceIds, signedData) - ); - - const [calls, gasPrices, collateralPriceCalls] = await Promise.all([ - callsPromise, - getGasPrice({ provider }), - collateralPriceCallsPromise, - ]); - - const allCalls = collateralPriceCalls.concat(calls); - - const erc7412Tx = await withERC7412(network, allCalls, 'useRepay', CoreProxy.interface); + const erc7412Tx = await withERC7412(network, calls, 'useRepay', CoreProxy.interface); const gasOptionsForTransaction = formatGasPriceForTransaction({ gasLimit: erc7412Tx.gasLimit, diff --git a/liquidity/lib/useUndelegateBaseAndromeda/package.json b/liquidity/lib/useUndelegateBaseAndromeda/package.json index b37d559af..cb4f0cd06 100644 --- a/liquidity/lib/useUndelegateBaseAndromeda/package.json +++ b/liquidity/lib/useUndelegateBaseAndromeda/package.json @@ -4,7 +4,6 @@ "main": "index.ts", "version": "0.0.1", "dependencies": { - "@snx-v3/fetchPythPrices": "workspace:*", "@snx-v3/format": "workspace:*", "@snx-v3/isBaseAndromeda": "workspace:*", "@snx-v3/tsHelpers": "workspace:*", diff --git a/liquidity/lib/useUndelegateBaseAndromeda/useUndelegateBaseAndromeda.tsx b/liquidity/lib/useUndelegateBaseAndromeda/useUndelegateBaseAndromeda.tsx index 184a354ea..1da3308ba 100644 --- a/liquidity/lib/useUndelegateBaseAndromeda/useUndelegateBaseAndromeda.tsx +++ b/liquidity/lib/useUndelegateBaseAndromeda/useUndelegateBaseAndromeda.tsx @@ -10,7 +10,6 @@ import { getGasPrice } from '@snx-v3/useGasPrice'; import { useGasSpeed } from '@snx-v3/useGasSpeed'; import { withERC7412 } from '@snx-v3/withERC7412'; import { useAllCollateralPriceIds } from '@snx-v3/useAllCollateralPriceIds'; -import { fetchPriceUpdates, priceUpdatesToPopulatedTx } from '@snx-v3/fetchPythPrices'; import { LiquidityPosition } from '@snx-v3/useLiquidityPosition'; import { useApprove } from '@snx-v3/useApprove'; import { USDC_BASE_MARKET, getRepayerContract, getUSDCAddress } from '@snx-v3/isBaseAndromeda'; @@ -18,6 +17,7 @@ import { parseUnits } from '@snx-v3/format'; import { DEBT_REPAYER_ABI } from '../useClearDebt'; import { useSpotMarketProxy } from '../useSpotMarketProxy'; import { notNil } from '@snx-v3/tsHelpers'; +import { useCollateralPriceUpdates } from '../useCollateralPriceUpdates'; export const useUndelegateBaseAndromeda = ({ accountId, @@ -37,6 +37,7 @@ export const useUndelegateBaseAndromeda = ({ const [txnState, dispatch] = useReducer(reducer, initialState); const { data: CoreProxy } = useCoreProxy(); const { data: SpotMarketProxy } = useSpotMarketProxy(); + const { data: priceUpdateTx } = useCollateralPriceUpdates(); const signer = useSigner(); const { gasSpeed } = useGasSpeed(); @@ -102,29 +103,15 @@ export const useUndelegateBaseAndromeda = ({ wei(1).toBN() ); - const walletAddress = await signer.getAddress(); - const callsPromise = Promise.all([...transactions, populatedTxnPromised].filter(notNil)); - const collateralPriceCallsPromise = fetchPriceUpdates( - collateralPriceUpdates, - network.isTestnet - ).then((signedData) => - priceUpdatesToPopulatedTx(walletAddress, collateralPriceUpdates, signedData) - ); - const [calls, gasPrices, collateralPriceCalls] = await Promise.all([ - callsPromise, - getGasPrice({ provider }), - collateralPriceCallsPromise, - ]); - const allCalls = collateralPriceCalls.concat(...calls); - - const erc7412Tx = await withERC7412( - network, - allCalls, - 'useUndelegate', - CoreProxy.interface - ); + const [calls, gasPrices] = await Promise.all([callsPromise, getGasPrice({ provider })]); + + if (priceUpdateTx) { + calls.unshift(priceUpdateTx as any); + } + + const erc7412Tx = await withERC7412(network, calls, 'useUndelegate', CoreProxy.interface); const gasOptionsForTransaction = formatGasPriceForTransaction({ gasLimit: erc7412Tx.gasLimit, diff --git a/liquidity/lib/useVaultsData/useVaultsData.ts b/liquidity/lib/useVaultsData/useVaultsData.ts index 3850031da..510a6c49e 100644 --- a/liquidity/lib/useVaultsData/useVaultsData.ts +++ b/liquidity/lib/useVaultsData/useVaultsData.ts @@ -8,7 +8,7 @@ import { useDefaultProvider, useNetwork } from '@snx-v3/useBlockchain'; import { erc7412Call } from '@snx-v3/withERC7412'; import { useAllCollateralPriceIds } from '@snx-v3/useAllCollateralPriceIds'; import { fetchPriceUpdates, priceUpdatesToPopulatedTx } from '@snx-v3/fetchPythPrices'; -import { useCollateralPriceUpdates } from '../useCollateralPriceUpdates'; +import { useAllCollateralPriceUpdates } from '../useCollateralPriceUpdates'; const VaultCollateralSchema = z .object({ value: ZodBigNumber, amount: ZodBigNumber }) @@ -21,7 +21,7 @@ export const useVaultsData = (poolId?: number) => { const { data: CoreProxyContract } = useCoreProxy(); const { data: collateralPriceUpdates } = useAllCollateralPriceIds(); const provider = useDefaultProvider(); - const { data: priceUpdateTx } = useCollateralPriceUpdates(); + const { data: priceUpdateTx } = useAllCollateralPriceUpdates(); return useQuery({ queryKey: [ diff --git a/liquidity/lib/useWithdrawBaseAndromeda/package.json b/liquidity/lib/useWithdrawBaseAndromeda/package.json index f2aab6789..950221b3b 100644 --- a/liquidity/lib/useWithdrawBaseAndromeda/package.json +++ b/liquidity/lib/useWithdrawBaseAndromeda/package.json @@ -4,12 +4,10 @@ "main": "index.ts", "version": "0.0.1", "dependencies": { - "@snx-v3/fetchPythPrices": "workspace:*", "@snx-v3/isBaseAndromeda": "workspace:*", "@snx-v3/tsHelpers": "workspace:*", "@snx-v3/txnReducer": "workspace:*", "@snx-v3/useAccountCollateral": "workspace:*", - "@snx-v3/useAllCollateralPriceIds": "workspace:*", "@snx-v3/useBlockchain": "workspace:*", "@snx-v3/useCoreProxy": "workspace:*", "@snx-v3/useGasOptions": "workspace:*", diff --git a/liquidity/lib/useWithdrawBaseAndromeda/useWithdrawBaseAndromeda.tsx b/liquidity/lib/useWithdrawBaseAndromeda/useWithdrawBaseAndromeda.tsx index 12f56f23a..38d698e56 100644 --- a/liquidity/lib/useWithdrawBaseAndromeda/useWithdrawBaseAndromeda.tsx +++ b/liquidity/lib/useWithdrawBaseAndromeda/useWithdrawBaseAndromeda.tsx @@ -8,13 +8,12 @@ import { formatGasPriceForTransaction } from '@snx-v3/useGasOptions'; import { getGasPrice } from '@snx-v3/useGasPrice'; import { useGasSpeed } from '@snx-v3/useGasSpeed'; import { AccountCollateralWithSymbol } from '@snx-v3/useAccountCollateral'; -import { useAllCollateralPriceIds } from '@snx-v3/useAllCollateralPriceIds'; -import { fetchPriceUpdates, priceUpdatesToPopulatedTx } from '@snx-v3/fetchPythPrices'; import { withERC7412 } from '@snx-v3/withERC7412'; import { useSpotMarketProxy } from '../useSpotMarketProxy'; import { USDC_BASE_MARKET } from '@snx-v3/isBaseAndromeda'; import { notNil } from '@snx-v3/tsHelpers'; import { useUSDProxy } from '@snx-v3/useUSDProxy'; +import { useCollateralPriceUpdates } from '../useCollateralPriceUpdates'; export const useWithdrawBaseAndromeda = ({ accountId, @@ -29,7 +28,7 @@ export const useWithdrawBaseAndromeda = ({ const { data: CoreProxy } = useCoreProxy(); const { data: SpotProxy } = useSpotMarketProxy(); const { data: UsdProxy } = useUSDProxy(); - const { data: collateralPriceIds } = useAllCollateralPriceIds(); + const { data: priceUpdateTx } = useCollateralPriceUpdates(); const { network } = useNetwork(); const { gasSpeed } = useGasSpeed(); @@ -45,9 +44,7 @@ export const useWithdrawBaseAndromeda = ({ mutationFn: async () => { if (!signer || !network || !provider) throw new Error('No signer or network'); - if (!(CoreProxy && SpotProxy && amount.gt(0) && collateralPriceIds && accountId)) return; - - const walletAddress = await signer.getAddress(); + if (!(CoreProxy && SpotProxy && amount.gt(0) && accountId)) return; try { dispatch({ type: 'prompting' }); @@ -95,12 +92,6 @@ export const useWithdrawBaseAndromeda = ({ ).toFixed() ); - const collateralPriceCallsPromise = fetchPriceUpdates( - collateralPriceIds, - network.isTestnet - ).then((signedData) => - priceUpdatesToPopulatedTx(walletAddress, collateralPriceIds, signedData) - ); const [ gasPrices, withdraw_sUSDC_Txn, @@ -108,7 +99,6 @@ export const useWithdrawBaseAndromeda = ({ sUSDCApproval_Txn, buy_SUSD_Txn, unwrapTxn, - collateralPriceCalls, ] = await Promise.all([ gasPricesPromised, withdraw_sUSDC, @@ -116,17 +106,19 @@ export const useWithdrawBaseAndromeda = ({ sUSDCApproval, buy_SUSD, unwrapTxnPromised, - collateralPriceCallsPromise, ]); - const allCalls = collateralPriceCalls.concat( - [ - withdraw_sUSDC_Txn, - withdraw_SUSD_Txn, - sUSDCApproval_Txn, - buy_SUSD_Txn, - unwrapTxn, - ].filter(notNil) - ); + + const allCalls = [ + withdraw_sUSDC_Txn, + withdraw_SUSD_Txn, + sUSDCApproval_Txn, + buy_SUSD_Txn, + unwrapTxn, + ].filter(notNil); + + if (priceUpdateTx) { + allCalls.unshift(priceUpdateTx as any); + } const erc7412Tx = await withERC7412(network, allCalls, 'useWithdraw', CoreProxy.interface); diff --git a/liquidity/ui/src/pages/Home/Home.tsx b/liquidity/ui/src/pages/Home/Home.tsx index 9d7e9ba6f..fe983a225 100644 --- a/liquidity/ui/src/pages/Home/Home.tsx +++ b/liquidity/ui/src/pages/Home/Home.tsx @@ -7,6 +7,7 @@ import { useParams } from '@snx-v3/useParams'; import { useLiquidityPositions } from '@snx-v3/useLiquidityPositions'; import { PoolsList, Stats, AvailableCollateral } from '@snx-v3/Pools'; import { useApr } from '@snx-v3/useApr'; +import { useCollateralPriceUpdates } from '../../../../lib/useCollateralPriceUpdates'; export function Home() { const { accountId } = useParams(); @@ -17,6 +18,7 @@ export function Home() { const { data: liquidityPositionsById, isLoading: liquidityPositionLoading } = useLiquidityPositions({ accountId }); + useCollateralPriceUpdates(); const isLoading = collateralTypesLoading || isPoolsLoading || liquidityPositionLoading || isAprLoading; diff --git a/yarn.lock b/yarn.lock index 1cf3e4081..cdd2b1e77 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6774,7 +6774,6 @@ __metadata: version: 0.0.0-use.local resolution: "@snx-v3/useClearDebt@workspace:liquidity/lib/useClearDebt" dependencies: - "@snx-v3/fetchPythPrices": "workspace:*" "@snx-v3/isBaseAndromeda": "workspace:*" "@snx-v3/tsHelpers": "workspace:*" "@snx-v3/txnReducer": "workspace:*" @@ -6801,6 +6800,7 @@ __metadata: "@snx-v3/isBaseAndromeda": "workspace:*" "@snx-v3/useBlockchain": "workspace:*" "@snx-v3/withERC7412": "workspace:*" + "@synthetixio/v3-contracts": "workspace:*" "@tanstack/react-query": "npm:^5.8.3" ethers: "npm:^5.7.2" languageName: unknown @@ -6886,12 +6886,10 @@ __metadata: version: 0.0.0-use.local resolution: "@snx-v3/useDepositBaseAndromeda@workspace:liquidity/lib/useDepositBaseAndromeda" dependencies: - "@snx-v3/fetchPythPrices": "workspace:*" "@snx-v3/format": "workspace:*" "@snx-v3/isBaseAndromeda": "workspace:*" "@snx-v3/tsHelpers": "workspace:*" "@snx-v3/txnReducer": "workspace:*" - "@snx-v3/useAllCollateralPriceIds": "workspace:*" "@snx-v3/useApprove": "workspace:*" "@snx-v3/useBlockchain": "workspace:*" "@snx-v3/useCoreProxy": "workspace:*" @@ -7104,12 +7102,10 @@ __metadata: version: 0.0.0-use.local resolution: "@snx-v3/useRepayBaseAndromeda@workspace:liquidity/lib/useRepayBaseAndromeda" dependencies: - "@snx-v3/fetchPythPrices": "workspace:*" "@snx-v3/format": "workspace:*" "@snx-v3/isBaseAndromeda": "workspace:*" "@snx-v3/tsHelpers": "workspace:*" "@snx-v3/txnReducer": "workspace:*" - "@snx-v3/useAllCollateralPriceIds": "workspace:*" "@snx-v3/useApprove": "workspace:*" "@snx-v3/useBlockchain": "workspace:*" "@snx-v3/useCoreProxy": "workspace:*" @@ -7231,7 +7227,6 @@ __metadata: version: 0.0.0-use.local resolution: "@snx-v3/useUndelegateBaseAndromeda@workspace:liquidity/lib/useUndelegateBaseAndromeda" dependencies: - "@snx-v3/fetchPythPrices": "workspace:*" "@snx-v3/format": "workspace:*" "@snx-v3/isBaseAndromeda": "workspace:*" "@snx-v3/tsHelpers": "workspace:*" @@ -7293,12 +7288,10 @@ __metadata: version: 0.0.0-use.local resolution: "@snx-v3/useWithdrawBaseAndromeda@workspace:liquidity/lib/useWithdrawBaseAndromeda" dependencies: - "@snx-v3/fetchPythPrices": "workspace:*" "@snx-v3/isBaseAndromeda": "workspace:*" "@snx-v3/tsHelpers": "workspace:*" "@snx-v3/txnReducer": "workspace:*" "@snx-v3/useAccountCollateral": "workspace:*" - "@snx-v3/useAllCollateralPriceIds": "workspace:*" "@snx-v3/useBlockchain": "workspace:*" "@snx-v3/useCoreProxy": "workspace:*" "@snx-v3/useGasOptions": "workspace:*"