From 5baa947db86a4d18870ed929c31456596c64b7b9 Mon Sep 17 00:00:00 2001 From: Taylor Webb Date: Fri, 17 Oct 2025 13:32:44 -0600 Subject: [PATCH 1/2] Add function for checking if bridge amount is safe --- contracts/libraries/HyperCoreLib.sol | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/contracts/libraries/HyperCoreLib.sol b/contracts/libraries/HyperCoreLib.sol index eecbf94b0..7fc4bc86b 100644 --- a/contracts/libraries/HyperCoreLib.sol +++ b/contracts/libraries/HyperCoreLib.sol @@ -219,7 +219,7 @@ library HyperCoreLib { * @param index The asset index to get the spot price of * @return spotPx The spot price of the specified asset on HyperCore scaled by 1e8 */ - function spotPx(uint32 index) external view returns (uint64) { + function spotPx(uint32 index) internal view returns (uint64) { (bool success, bytes memory result) = SPOT_PX_PRECOMPILE_ADDRESS.staticcall(abi.encode(index)); if (!success) revert SpotPxPrecompileCallFailed(); return abi.decode(result, (uint64)); @@ -237,6 +237,26 @@ library HyperCoreLib { return _tokenInfo; } + /** + * @notice Checks if an amount is safe to bridge from HyperCore to HyperEVM + * @dev Verifies that the asset bridge has sufficient balance to cover the amount plus a buffer + * @param erc20CoreIndex The HyperCore index id of the token + * @param coreAmount The amount that the brdiging should result in on HyperCore + * @param coreBufferAmount The minimum buffer amount that should remain on HyperCore after bridging + * @return True if the bridge has enough balance to safely bridge the amount, false otherwise + */ + function isCoreAmountSafeToBridge( + uint64 erc20CoreIndex, + uint64 coreAmount, + uint64 coreBufferAmount + ) internal view returns (bool) { + address bridgeAddress = toAssetBridgeAddress(erc20CoreIndex); + uint64 currentBridgeBalance = spotBalance(bridgeAddress, erc20CoreIndex); + + // Return true if currentBridgeBalance >= coreAmount + coreBufferAmount + return currentBridgeBalance >= coreAmount + coreBufferAmount; + } + /** * @notice Converts a core index id to an asset bridge address * @param erc20CoreIndex The core token index id to convert From d4d615bb2a9d92cc2d9b85d96dfc92319f360dc3 Mon Sep 17 00:00:00 2001 From: Taylor Webb Date: Fri, 17 Oct 2025 13:37:30 -0600 Subject: [PATCH 2/2] fix function natspec --- contracts/libraries/HyperCoreLib.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contracts/libraries/HyperCoreLib.sol b/contracts/libraries/HyperCoreLib.sol index 7fc4bc86b..ec9b47f61 100644 --- a/contracts/libraries/HyperCoreLib.sol +++ b/contracts/libraries/HyperCoreLib.sol @@ -238,10 +238,10 @@ library HyperCoreLib { } /** - * @notice Checks if an amount is safe to bridge from HyperCore to HyperEVM + * @notice Checks if an amount is safe to bridge from HyperEVM to HyperCore * @dev Verifies that the asset bridge has sufficient balance to cover the amount plus a buffer * @param erc20CoreIndex The HyperCore index id of the token - * @param coreAmount The amount that the brdiging should result in on HyperCore + * @param coreAmount The amount that the bridging should result in on HyperCore * @param coreBufferAmount The minimum buffer amount that should remain on HyperCore after bridging * @return True if the bridge has enough balance to safely bridge the amount, false otherwise */