diff --git a/ios/Podfile.lock b/ios/Podfile.lock index a85de240a..b761b4cf7 100644 --- a/ios/Podfile.lock +++ b/ios/Podfile.lock @@ -861,9 +861,9 @@ SPEC CHECKSUMS: FBLazyVector: f1897022b53abf1469d6ad692ee2c69f57d967f3 FBReactNativeSpec: 627fd07f1b9d498c9fa572e76d7f1a6b1ee9a444 fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9 - glog: 04b94705f318337d7ead9e6d17c019bd9b1f6b1b + glog: 791fe035093b84822da7f0870421a25839ca7870 helium-react-native-sdk: 32c0a7e3abc733a7f3d291013b2db31475fc6980 - hermes-engine: 0784cadad14b011580615c496f77e0ae112eed75 + hermes-engine: 7a53ccac09146018a08239c5425625fdb79a6162 libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913 MapboxCommon: fdf7fd31c90b7b607cd9c63e37797f023c01d860 MapboxCoreMaps: 24270c7c6b8cb71819fc2f3c549db9620ee4d019 @@ -871,7 +871,7 @@ SPEC CHECKSUMS: MapboxMobileEvents: de50b3a4de180dd129c326e09cd12c8adaaa46d6 MultiplatformBleAdapter: 5a6a897b006764392f9cef785e4360f54fb9477d OneSignalXCFramework: 81ceac017a290f23793443323090cfbe888f74ea - RCT-Folly: 424b8c9a7a0b9ab2886ffe9c3b041ef628fd4fb1 + RCT-Folly: 85766c3226c7ec638f05ad7cb3cf6a268d6c4241 RCTRequired: bd6045fbd511da5efe6db89eecb21e4e36bd7cbf RCTTypeSafety: c06d9f906faa69dd1c88223204c3a24767725fd8 React: b9ea33557ef1372af247f95d110fbdea114ed3b2 diff --git a/src/hooks/useHntSolConvert.ts b/src/hooks/useHntSolConvert.ts index 4494e5565..af912950d 100644 --- a/src/hooks/useHntSolConvert.ts +++ b/src/hooks/useHntSolConvert.ts @@ -40,13 +40,15 @@ export function useHntSolConvert() { } }, [baseUrl]) - const hasEnoughSol = useMemo(() => { - if (!hntBalance || !hntEstimate) return true + const hasEnoughHNTForSol = useMemo(() => { + if (!hntBalance || !hntEstimate) return false - if (hntBalance.lt(hntEstimate)) return true + return hntBalance.gt(hntEstimate) + }, [hntBalance, hntEstimate]) + const hasEnoughSol = useMemo(() => { return (solBalance || new BN(0)).gt(new BN(0.02 * LAMPORTS_PER_SOL)) - }, [hntBalance, solBalance, hntEstimate]) + }, [solBalance]) const { result: hntSolConvertTransaction, @@ -76,5 +78,6 @@ export function useHntSolConvert() { hntEstimateLoading, hntEstimateError, hasEnoughSol, + hasEnoughHNTForSol, } } diff --git a/src/hooks/useSimulatedTransaction.ts b/src/hooks/useSimulatedTransaction.ts index 7f953d0b2..b8289969d 100644 --- a/src/hooks/useSimulatedTransaction.ts +++ b/src/hooks/useSimulatedTransaction.ts @@ -44,8 +44,13 @@ export function useSimulatedTransaction( const { tokenAccounts } = useBalance() const { connection, anchorProvider } = useSolana() const { t: tr } = useTranslation() - const { hntSolConvertTransaction, hntEstimate, hasEnoughSol } = - useHntSolConvert() + const { + hntSolConvertTransaction, + hntEstimate, + hasEnoughSol, + hasEnoughHNTForSol, + hntEstimateLoading, + } = useHntSolConvert() const [simulationError, setSimulationError] = useState(false) const [insufficientFunds, setInsufficientFunds] = useState(false) @@ -117,43 +122,6 @@ export function useSimulatedTransaction( [connection, transaction], ) - const { - result: simulationAccounts, - loading: loadingAccounts, - // error: getAccountsErr, - } = useAsync(async () => { - if (!connection || !transaction) return [] - - const addressLookupTableAccounts: Array = [] - const { addressTableLookups } = transaction.message - if (addressTableLookups.length > 0) { - // eslint-disable-next-line no-restricted-syntax - for (const addressTableLookup of addressTableLookups) { - // eslint-disable-next-line no-await-in-loop - const result = await connection?.getAddressLookupTable( - addressTableLookup.accountKey, - ) - if (result?.value) { - addressLookupTableAccounts.push(result?.value) - } - } - } - const accountKeys = transaction.message.getAccountKeys({ - addressLookupTableAccounts, - }) - - return [ - ...new Set( - accountKeys.staticAccountKeys.concat( - accountKeys.accountKeysFromLookups - ? // Only writable accounts will contribute to balance changes - accountKeys.accountKeysFromLookups.writable - : [], - ), - ), - ] - }, [transaction, connection]) - const { loading: loadingBal, result: estimatedBalanceChanges } = useAsync(async () => { if ( @@ -161,8 +129,8 @@ export function useSimulatedTransaction( !transaction || !anchorProvider || !wallet || - !simulationAccounts || - !tokenAccounts + !tokenAccounts || + hntEstimateLoading ) return undefined @@ -170,6 +138,34 @@ export function useSimulatedTransaction( setInsufficientFunds(false) try { + const addressLookupTableAccounts: Array = [] + const { addressTableLookups } = transaction.message + if (addressTableLookups.length > 0) { + // eslint-disable-next-line no-restricted-syntax + for (const addressTableLookup of addressTableLookups) { + // eslint-disable-next-line no-await-in-loop + const result = await connection?.getAddressLookupTable( + addressTableLookup.accountKey, + ) + if (result?.value) { + addressLookupTableAccounts.push(result?.value) + } + } + } + const accountKeys = transaction.message.getAccountKeys({ + addressLookupTableAccounts, + }) + + const simulationAccounts = [ + ...new Set( + accountKeys.staticAccountKeys.concat( + accountKeys.accountKeysFromLookups + ? // Only writable accounts will contribute to balance changes + accountKeys.accountKeysFromLookups.writable + : [], + ), + ), + ] const { blockhash } = await connection?.getLatestBlockhash() transaction.message.recentBlockhash = blockhash const result = await connection?.simulateTransaction(transaction, { @@ -184,7 +180,7 @@ export function useSimulatedTransaction( console.warn('failed to simulate', result?.value.err) console.warn(result?.value.logs?.join('\n')) if (JSON.stringify(result?.value.err).includes('{"Custom":1}')) { - if (!hasEnoughSol) { + if (!hasEnoughSol && hasEnoughHNTForSol) { await showHNTConversionAlert() } setInsufficientFunds(true) @@ -306,7 +302,9 @@ export function useSimulatedTransaction( nativeChange: Math.abs(toNumber(nativeChange, decimals)), decimals, mint: tokenMint, - symbol: tokenMetadata?.symbol, + symbol: tokenMint.equals(NATIVE_MINT) + ? 'SOL' + : tokenMetadata?.symbol, type, } as BalanceChange } @@ -328,17 +326,18 @@ export function useSimulatedTransaction( return undefined } }, [ - simulationAccounts, connection, transaction, tokenAccounts, hasEnoughSol, + hasEnoughHNTForSol, anchorProvider, wallet, + hntEstimateLoading, ]) return { - loading: loadingBal || loadingAccounts || loadingFee, + loading: loadingBal || loadingFee, simulationError, insufficientFunds, balanceChanges: estimatedBalanceChanges,