Skip to content

Commit

Permalink
Add fallbacks tests and improve coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestognw committed Dec 28, 2024
1 parent 84b0b4e commit 2b76630
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 2 deletions.
29 changes: 29 additions & 0 deletions contracts/mocks/account/modules/ERC7579FallbackHandlerMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import {ERC2771Context} from "@openzeppelin/contracts/metatx/ERC2771Context.sol";
import {ERC7579ModuleMock} from "./ERC7579ModuleMock.sol";
import {MODULE_TYPE_FALLBACK} from "@openzeppelin/contracts/interfaces/draft-IERC7579.sol";

abstract contract ERC7579FallbackHandlerMock is ERC2771Context, ERC7579ModuleMock(MODULE_TYPE_FALLBACK) {
event ERC7579FallbackHandlerMockCalled(address sender, uint256 value, bytes data);

error ERC7579FallbackHandlerMockRevert();

function callRevert() public pure {
revert ERC7579FallbackHandlerMockRevert();
}

function _fallback() internal {
emit ERC7579FallbackHandlerMockCalled(_msgSender(), msg.value, _msgData());
}

fallback() external payable {
_fallback();
}

receive() external payable {
_fallback();
}
}
56 changes: 54 additions & 2 deletions test/account/Account.behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,10 @@ function shouldBehaveLikeAccountERC7579() {
});
});

it('unsupported module types are not installed', async function () {
await expect(this.mock.isModuleInstalled(999, this.mock, '0x')).to.eventually.equal(false);
});

describe('module installation', function () {
beforeEach(async function () {
this.mockFromEntrypoint = this.mock.connect(await impersonate(entrypoint.target));
Expand All @@ -369,7 +373,7 @@ function shouldBehaveLikeAccountERC7579() {
.withArgs(MODULE_TYPE_VALIDATOR, moduleMock);
});

for (const moduleTypeId of [MODULE_TYPE_VALIDATOR, MODULE_TYPE_EXECUTOR]) {
for (const moduleTypeId of [MODULE_TYPE_VALIDATOR, MODULE_TYPE_EXECUTOR, MODULE_TYPE_FALLBACK]) {
it(`does not allow to install a module of ${moduleTypeId} id twice`, async function () {
const moduleMock = await ethers.deployContract('$ERC7579ModuleMock', [moduleTypeId]);
const initData = moduleTypeId === MODULE_TYPE_FALLBACK ? data : '0x';
Expand Down Expand Up @@ -420,6 +424,16 @@ function shouldBehaveLikeAccountERC7579() {
});
}

it('should revert uninstalling a module of type MODULE_TYPE_FALLBACK if a different module was installed for the provided selector', async function () {
const moduleMock = await ethers.deployContract('$ERC7579ModuleMock', [MODULE_TYPE_FALLBACK]);
const anotherModuleMock = await ethers.deployContract('$ERC7579ModuleMock', [MODULE_TYPE_FALLBACK]);

await this.mockFromEntrypoint.$_installModule(MODULE_TYPE_FALLBACK, moduleMock, data);
await expect(this.mockFromEntrypoint.uninstallModule(MODULE_TYPE_FALLBACK, anotherModuleMock, data))
.to.be.revertedWithCustomError(this.mock, 'ERC7579UninstalledModule')
.withArgs(MODULE_TYPE_FALLBACK, anotherModuleMock);
});

for (const moduleTypeId of [MODULE_TYPE_VALIDATOR, MODULE_TYPE_EXECUTOR, MODULE_TYPE_FALLBACK]) {
it(`should uninstall a module of type ${moduleTypeId}`, async function () {
const moduleMock = await ethers.deployContract('$ERC7579ModuleMock', [moduleTypeId]);
Expand All @@ -435,7 +449,7 @@ function shouldBehaveLikeAccountERC7579() {
}
});

describe('execute', function () {
describe('execution', function () {
beforeEach(async function () {
const moduleMock = await ethers.deployContract('$ERC7579ModuleMock', [MODULE_TYPE_EXECUTOR]);
await this.mock.$_installModule(MODULE_TYPE_EXECUTOR, moduleMock, '0x');
Expand Down Expand Up @@ -634,6 +648,44 @@ function shouldBehaveLikeAccountERC7579() {
});
}
});

describe('fallback', function () {
beforeEach(async function () {
this.fallbackHandler = await ethers.deployContract('$ERC7579FallbackHandlerMock', [
this.mock.target, // Trusted forwarder
]);
});

it('reverts if there is no fallback module installed', async function () {
await expect(
this.other.sendTransaction({
data: fnSig,
to: this.mock,
}),
)
.to.be.revertedWithCustomError(this.mock, 'ERC7579MissingFallbackHandler')
.withArgs(fnSig);
});

describe('with a fallback module installed', function () {
it('forwards the call to the fallback handler', async function () {
await this.mock.$_installModule(MODULE_TYPE_FALLBACK, this.fallbackHandler, data);
await expect(this.other.sendTransaction({ data: fnSig, to: this.mock, value: 32 }))
.to.emit(this.fallbackHandler, 'ERC7579FallbackHandlerMockCalled')
.withArgs(this.other, 32, fnSig);
});

it('bubble up reverts from the fallback handler', async function () {
const revertData = this.fallbackHandler.interface.encodeFunctionData('callRevert');
const initData = coder.encode(['bytes4', 'bytes'], [revertData, '0x']);
await this.mock.$_installModule(MODULE_TYPE_FALLBACK, this.fallbackHandler, initData);
await expect(this.other.sendTransaction({ data: revertData, to: this.mock })).to.be.revertedWithCustomError(
this.fallbackHandler,
'ERC7579FallbackHandlerMockRevert',
);
});
});
});
});
}

Expand Down

0 comments on commit 2b76630

Please sign in to comment.