diff --git a/src/contracts/Teleporter/AvalancheICTTRouterFixedFees.sol b/src/contracts/Teleporter/AvalancheICTTRouterFixedFees.sol index 99946a7..2251bca 100644 --- a/src/contracts/Teleporter/AvalancheICTTRouterFixedFees.sol +++ b/src/contracts/Teleporter/AvalancheICTTRouterFixedFees.sol @@ -128,7 +128,7 @@ contract AvalancheICTTRouterFixedFees is uint256 primaryFeeAmount = (adjustedAmount * primaryRelayerFeeBips) / BASIS_POINTS_DIVIDER; uint256 secondaryFeeAmount = destinationBridge.isMultihop - ? (amount * secondaryRelayerFeeBips) / BASIS_POINTS_DIVIDER + ? (adjustedAmount * secondaryRelayerFeeBips) / BASIS_POINTS_DIVIDER : 0; if ( @@ -191,7 +191,7 @@ contract AvalancheICTTRouterFixedFees is uint256 primaryFeeAmount = (adjustedAmount * primaryRelayerFeeBips) / BASIS_POINTS_DIVIDER; uint256 secondaryFeeAmount = destinationBridge.isMultihop - ? (amount * secondaryRelayerFeeBips) / BASIS_POINTS_DIVIDER + ? (adjustedAmount * secondaryRelayerFeeBips) / BASIS_POINTS_DIVIDER : 0; if ( @@ -264,8 +264,14 @@ contract AvalancheICTTRouterFixedFees is ); } - SafeERC20.safeIncreaseAllowance(IERC20(feeToken), bridgeSource, primaryFeeAmount); - WrappedNativeToken(payable(feeToken)).deposit{value: primaryFeeAmount}(); + WrappedNativeToken wrappedFeeToken = WrappedNativeToken(payable(feeToken)); + uint256 wrappedNativeBalance = wrappedFeeToken.balanceOf(address(this)); + wrappedFeeToken.deposit{value: primaryFeeAmount}(); + wrappedNativeBalance = wrappedFeeToken.balanceOf(address(this)) - wrappedNativeBalance; + + if (wrappedNativeBalance > 0) { + SafeERC20.safeIncreaseAllowance(IERC20(feeToken), bridgeSource, wrappedNativeBalance); + } uint256 bridgeAmount = msg.value - primaryFeeAmount; @@ -319,8 +325,14 @@ contract AvalancheICTTRouterFixedFees is ); } - SafeERC20.safeIncreaseAllowance(IERC20(feeToken), bridgeSource, primaryFeeAmount); - WrappedNativeToken(payable(feeToken)).deposit{value: primaryFeeAmount}(); + WrappedNativeToken wrappedFeeToken = WrappedNativeToken(payable(feeToken)); + uint256 wrappedNativeBalance = wrappedFeeToken.balanceOf(address(this)); + wrappedFeeToken.deposit{value: primaryFeeAmount}(); + wrappedNativeBalance = wrappedFeeToken.balanceOf(address(this)) - wrappedNativeBalance; + + if (wrappedNativeBalance > 0) { + SafeERC20.safeIncreaseAllowance(IERC20(feeToken), bridgeSource, wrappedNativeBalance); + } uint256 bridgeAmount = msg.value - primaryFeeAmount; @@ -349,6 +361,14 @@ contract AvalancheICTTRouterFixedFees is return (primaryRelayerFeeBips, secondaryRelayerFeeBips); } + /// @inheritdoc IAvalancheICTTRouterFixedFees + function getMinBridgeFeesForTokenOnDestinationChain( + bytes32 chainID, + address token + ) external view returns (MinBridgeFees memory) { + return destinationChainTokenToMinBridgeFees[chainID][token]; + } + /// @notice Always revert as you need to input minimal bridge fees for a token on a destination chain function registerDestinationTokenBridge( address, /* tokenAddress */ diff --git a/src/interfaces/Teleporter/IAvalancheICTTRouterFixedFees.sol b/src/interfaces/Teleporter/IAvalancheICTTRouterFixedFees.sol index befbaa8..e6787ff 100644 --- a/src/interfaces/Teleporter/IAvalancheICTTRouterFixedFees.sol +++ b/src/interfaces/Teleporter/IAvalancheICTTRouterFixedFees.sol @@ -76,6 +76,17 @@ interface IAvalancheICTTRouterFixedFees is IAvalancheICTTRouter { */ function getRelayerFeesBips() external view returns (uint256, uint256); + /** + * @notice Get the minimum required fees to bridge a specific token to a given destination chain + * @param chainID The ID of the destination chain + * @param token The address of the token to be bridged + * @return A struct containing the minimum primary and secondary relayer fees for the token on the destination chain + */ + function getMinBridgeFeesForTokenOnDestinationChain( + bytes32 chainID, + address token + ) external view returns (MinBridgeFees memory); + /** * @notice Bridge ERC20 token to a destination chain. The relayer fees are set by the contract. * @param tokenAddress Address of the ERC20 token contract