Skip to content

Commit

Permalink
refactor: move InsufficientFunds to interface
Browse files Browse the repository at this point in the history
  • Loading branch information
guidanoli committed Nov 2, 2024
1 parent 1e72e32 commit a935a74
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
9 changes: 8 additions & 1 deletion contracts/dapp/Application.sol
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,14 @@ contract Application is
(address, uint256, bytes)
);

destination.safeCall(value, payload);
bool enoughFunds;
uint256 balance;

(enoughFunds, balance) = destination.safeCall(value, payload);

if (!enoughFunds) {
revert InsufficientFunds(value, balance);
}
}

/// @notice Executes a delegatecall voucher
Expand Down
5 changes: 5 additions & 0 deletions contracts/dapp/IApplication.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ interface IApplication is IOwnable {
/// @param output The output
error OutputNotReexecutable(bytes output);

/// @notice Could not execute an output, because the application contract doesn't have enough Ether.
/// @param value The amount of Wei necessary for the execution of the output
/// @param balance The current application contract balance
error InsufficientFunds(uint256 value, uint256 balance);

/// @notice Raised when the output hashes siblings array has an invalid size.
/// @dev Please consult `CanonicalMachine` for the maximum number of outputs.
error InvalidOutputHashesSiblingsArrayLength();
Expand Down
16 changes: 6 additions & 10 deletions contracts/library/LibAddress.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,23 @@ import {LibError} from "../library/LibError.sol";
library LibAddress {
using LibError for bytes;

/// @notice Caller does not have enough Ether to make call
/// @param caller The address of the contract making the call
/// @param value The value being transmitted through the call
/// @param balance The current contract balance
error InsufficientFunds(address caller, uint256 value, uint256 balance);

/// @notice Perform a low level call and raise error if failed
/// @param destination The address that will be called
/// @param value The amount of Wei to be transferred through the call
/// @param payload The payload, which—in the case of Solidity
/// contracts—encodes a function call
/// @dev Raises an `InsufficientFunds` error
/// if the caller has less than `value` Wei
/// @return Whether the caller had enough Ether to make the call,
/// and the balance before the call
function safeCall(
address destination,
uint256 value,
bytes memory payload
) internal {
) internal returns (bool, uint256) {
address caller = address(this);
uint256 balance = caller.balance;

if (value > balance) {
revert InsufficientFunds(caller, value, balance);
return (false, balance);
}

bool success;
Expand All @@ -41,6 +35,8 @@ library LibAddress {
if (!success) {
returndata.raise();
}

return (true, balance);
}

/// @notice Perform a delegate call and raise error if failed
Expand Down
3 changes: 1 addition & 2 deletions test/dapp/Application.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,7 @@ contract ApplicationTest is TestBase, OwnableTest {

vm.expectRevert(
abi.encodeWithSelector(
LibAddress.InsufficientFunds.selector,
address(_appContract),
IApplication.InsufficientFunds.selector,
_transferAmount,
0
)
Expand Down

0 comments on commit a935a74

Please sign in to comment.