Skip to content

Commit

Permalink
Fix transaction simulation flicker
Browse files Browse the repository at this point in the history
  • Loading branch information
ChewingGlass committed Sep 5, 2023
1 parent d8d5e48 commit f52afae
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 52 deletions.
6 changes: 3 additions & 3 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -861,17 +861,17 @@ 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
MapboxMaps: cb76511b98d3b95c74b0771ed105bc69f30ace6b
MapboxMobileEvents: de50b3a4de180dd129c326e09cd12c8adaaa46d6
MultiplatformBleAdapter: 5a6a897b006764392f9cef785e4360f54fb9477d
OneSignalXCFramework: 81ceac017a290f23793443323090cfbe888f74ea
RCT-Folly: 424b8c9a7a0b9ab2886ffe9c3b041ef628fd4fb1
RCT-Folly: 85766c3226c7ec638f05ad7cb3cf6a268d6c4241
RCTRequired: bd6045fbd511da5efe6db89eecb21e4e36bd7cbf
RCTTypeSafety: c06d9f906faa69dd1c88223204c3a24767725fd8
React: b9ea33557ef1372af247f95d110fbdea114ed3b2
Expand Down
11 changes: 7 additions & 4 deletions src/hooks/useHntSolConvert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -76,5 +78,6 @@ export function useHntSolConvert() {
hntEstimateLoading,
hntEstimateError,
hasEnoughSol,
hasEnoughHNTForSol,
}
}
89 changes: 44 additions & 45 deletions src/hooks/useSimulatedTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -117,59 +122,50 @@ export function useSimulatedTransaction(
[connection, transaction],
)

const {
result: simulationAccounts,
loading: loadingAccounts,
// error: getAccountsErr,
} = useAsync(async () => {
if (!connection || !transaction) return []

const addressLookupTableAccounts: Array<AddressLookupTableAccount> = []
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 (
!connection ||
!transaction ||
!anchorProvider ||
!wallet ||
!simulationAccounts ||
!tokenAccounts
!tokenAccounts ||
hntEstimateLoading
)
return undefined

setSimulationError(false)
setInsufficientFunds(false)

try {
const addressLookupTableAccounts: Array<AddressLookupTableAccount> = []
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, {
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand All @@ -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,
Expand Down

0 comments on commit f52afae

Please sign in to comment.