Skip to content

Commit

Permalink
chore: merge branch 'main' into prerelease/2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
guidanoli committed Dec 13, 2024
2 parents 98611a8 + 6dbb678 commit 6055295
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
tomlq -ti '.package.license = "Apache-2.0"' Cargo.toml
tomlq -ti '.package.description = "Rust bindings for Cartesi Rollups smart contracts"' Cargo.toml
tomlq -ti 'del(.dependencies.alloy.git)' Cargo.toml
tomlq -ti '.dependencies.alloy.version = "0.3.1"' Cargo.toml
tomlq -ti '.dependencies.alloy.version = "0.8.0"' Cargo.toml
- name: Publish Rust package to crates.io
working-directory: out/bindings
Expand Down
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 6055295

Please sign in to comment.