diff --git a/contracts/libraries/HyperCoreLib.sol b/contracts/libraries/HyperCoreLib.sol index eecbf94b0..ec9b47f61 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 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 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 + */ + 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