Skip to content

Commit 21f22b6

Browse files
authored
feat: add safe bridge check (#1140)
* Add function for checking if bridge amount is safe * fix function natspec
1 parent 7ae992f commit 21f22b6

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

contracts/libraries/HyperCoreLib.sol

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ library HyperCoreLib {
219219
* @param index The asset index to get the spot price of
220220
* @return spotPx The spot price of the specified asset on HyperCore scaled by 1e8
221221
*/
222-
function spotPx(uint32 index) external view returns (uint64) {
222+
function spotPx(uint32 index) internal view returns (uint64) {
223223
(bool success, bytes memory result) = SPOT_PX_PRECOMPILE_ADDRESS.staticcall(abi.encode(index));
224224
if (!success) revert SpotPxPrecompileCallFailed();
225225
return abi.decode(result, (uint64));
@@ -237,6 +237,26 @@ library HyperCoreLib {
237237
return _tokenInfo;
238238
}
239239

240+
/**
241+
* @notice Checks if an amount is safe to bridge from HyperEVM to HyperCore
242+
* @dev Verifies that the asset bridge has sufficient balance to cover the amount plus a buffer
243+
* @param erc20CoreIndex The HyperCore index id of the token
244+
* @param coreAmount The amount that the bridging should result in on HyperCore
245+
* @param coreBufferAmount The minimum buffer amount that should remain on HyperCore after bridging
246+
* @return True if the bridge has enough balance to safely bridge the amount, false otherwise
247+
*/
248+
function isCoreAmountSafeToBridge(
249+
uint64 erc20CoreIndex,
250+
uint64 coreAmount,
251+
uint64 coreBufferAmount
252+
) internal view returns (bool) {
253+
address bridgeAddress = toAssetBridgeAddress(erc20CoreIndex);
254+
uint64 currentBridgeBalance = spotBalance(bridgeAddress, erc20CoreIndex);
255+
256+
// Return true if currentBridgeBalance >= coreAmount + coreBufferAmount
257+
return currentBridgeBalance >= coreAmount + coreBufferAmount;
258+
}
259+
240260
/**
241261
* @notice Converts a core index id to an asset bridge address
242262
* @param erc20CoreIndex The core token index id to convert

0 commit comments

Comments
 (0)