Skip to content

Commit

Permalink
forge fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
valar999 committed Jul 13, 2024
1 parent f3e5026 commit cca874a
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 58 deletions.
66 changes: 33 additions & 33 deletions contracts/src/Wingman.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {ERC7579ExecutorBase} from "modulekit/Modules.sol";
import {ModeLib} from "erc7579/lib/ModeLib.sol";
import {ExecutionLib} from "erc7579/lib/ExecutionLib.sol";
import {Execution, IERC7579Account} from "erc7579/interfaces/IERC7579Account.sol";

import { ERC7579ExecutorBase } from "modulekit/Modules.sol";
import { ModeLib } from "erc7579/lib/ModeLib.sol";
import { ExecutionLib } from "erc7579/lib/ExecutionLib.sol";
import { Execution, IERC7579Account } from "erc7579/interfaces/IERC7579Account.sol";

contract Wingman is ERC7579ExecutorBase {
mapping(address => string[]) internal backupNames;
Expand All @@ -26,42 +25,51 @@ contract Wingman is ERC7579ExecutorBase {
event BackupUpdated(address indexed account, string indexed name, Backup backup);
event BackupExecuted(address indexed account, string indexed name);

function onInstall(bytes calldata data) external override {}
function onInstall(bytes calldata data) external override { }

function onUninstall(bytes calldata data) external override {
_removeBackups(msg.sender);
}

function isInitialized(address smartAccount) external view returns (bool) {}

function isInitialized(address smartAccount) external view returns (bool) { }

function updateBackup(string memory name, uint48 unlockAt, Beneficiary[] memory beneficiaries) public {
function updateBackup(
string memory name,
uint48 unlockAt,
Beneficiary[] memory beneficiaries
)
public
{
require(unlockAt > block.timestamp, "Unlock time must be in the future");

Backup storage backup = backups[msg.sender][name];

if (backup.createdAt == 0) { // new backup
if (backup.createdAt == 0) {
// new backup
backup.createdAt = uint48(block.timestamp);
backupNames[msg.sender].push(name);
}

backup.unlockAt = unlockAt;
delete backup.beneficiaries;

uint i;
uint totalPercentage;
uint benCount = beneficiaries.length;
uint256 i;
uint256 totalPercentage;
uint256 benCount = beneficiaries.length;

// beneficiaries with absolute amount
for (; i < benCount; i++) {
if (beneficiaries[i].amount == 0) break; // goto percentage amounts
if (beneficiaries[i].amount == 0) break; // goto percentage amounts
require(beneficiaries[i].percentage == 0, "Both amount and percentage are not 0");

backup.beneficiaries.push(beneficiaries[i]);
}
// beneficiaries with percent amount
for (; i < benCount; i++) {
require(beneficiaries[i].percentage > 0 && beneficiaries[i].percentage <= 100, "Invalid percentage");
require(
beneficiaries[i].percentage > 0 && beneficiaries[i].percentage <= 100,
"Invalid percentage"
);
require(beneficiaries[i].amount == 0, "Both amount and percentage are not 0");

totalPercentage += beneficiaries[i].percentage;
Expand All @@ -79,30 +87,25 @@ contract Wingman is ERC7579ExecutorBase {
_removeBackup(msg.sender, name);
}


function executeBackup(address owner, string memory name) public {
Backup storage backup = backups[owner][name];
require(backup.createdAt != 0, "Backup does not exist");
require(block.timestamp >= backup.unlockAt, "Backup is not ready to be executed");

uint balance = owner.balance;
uint count = backup.beneficiaries.length;
uint i;
uint256 balance = owner.balance;
uint256 count = backup.beneficiaries.length;
uint256 i;

Execution[] memory executions = new Execution[](count);


// absolute amounts
for (; i < count; i++) {
Beneficiary memory beneficiary = backup.beneficiaries[i];
if (beneficiary.amount == 0 ) break; // goto percentage amounts
if (beneficiary.amount == 0) break; // goto percentage amounts

balance -= beneficiary.amount;
executions[i] = Execution({
target: beneficiary.account,
value: beneficiary.amount,
callData: ""
});
executions[i] =
Execution({ target: beneficiary.account, value: beneficiary.amount, callData: "" });
}

// percent amounts
Expand All @@ -116,7 +119,6 @@ contract Wingman is ERC7579ExecutorBase {
});
}


IERC7579Account(owner).executeFromExecutor(
ModeLib.encodeSimpleBatch(), ExecutionLib.encodeBatch(executions)
);
Expand All @@ -126,7 +128,6 @@ contract Wingman is ERC7579ExecutorBase {
emit BackupExecuted(owner, name);
}


function getBackups(address owner) public view returns (string[] memory) {
return backupNames[owner];
}
Expand All @@ -137,12 +138,11 @@ contract Wingman is ERC7579ExecutorBase {

// INTERNAL


function _removeBackup(address account, string memory name) internal {
string[] storage userBackups = backupNames[account];
uint count = userBackups.length;
uint256 count = userBackups.length;

for (uint i = 0; i < count; i++) {
for (uint256 i = 0; i < count; i++) {
if (_compareStr(userBackups[i], name)) {
userBackups[i] = userBackups[userBackups.length - 1];
userBackups.pop();
Expand All @@ -157,9 +157,9 @@ contract Wingman is ERC7579ExecutorBase {

function _removeBackups(address account) internal {
string[] storage userBackups = backupNames[account];
uint count = userBackups.length;
uint256 count = userBackups.length;

for (uint i = 0; i < count; i++) {
for (uint256 i = 0; i < count; i++) {
string memory name = userBackups[count - i];
userBackups.pop();

Expand Down
45 changes: 20 additions & 25 deletions contracts/test/Wingman.t.sol
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import {Test} from "forge-std/Test.sol";
import { Test } from "forge-std/Test.sol";
import {
RhinestoneModuleKit,
ModuleKitHelpers,
ModuleKitUserOp,
AccountInstance,
UserOpData
RhinestoneModuleKit,
ModuleKitHelpers,
ModuleKitUserOp,
AccountInstance,
UserOpData
} from "modulekit/ModuleKit.sol";
import {MODULE_TYPE_EXECUTOR} from "modulekit/external/ERC7579.sol";
import {Wingman} from "src/Wingman.sol";
import { MODULE_TYPE_EXECUTOR } from "modulekit/external/ERC7579.sol";
import { Wingman } from "src/Wingman.sol";
import "forge-std/console2.sol";

contract WingmanTest is RhinestoneModuleKit, Test {
Expand Down Expand Up @@ -46,27 +46,27 @@ contract WingmanTest is RhinestoneModuleKit, Test {
beneficiaries[2] = Wingman.Beneficiary(address(0x3), 25, 0);
beneficiaries[3] = Wingman.Beneficiary(address(0x4), 75, 0);

uint256 balanceBefore1 = address(0x1).balance;
uint256 balanceBefore2 = address(0x2).balance;
uint256 balanceBefore3 = address(0x3).balance;
uint256 balanceBefore4 = address(0x4).balance;

uint balanceBefore1 = address(0x1).balance;
uint balanceBefore2 = address(0x2).balance;
uint balanceBefore3 = address(0x3).balance;
uint balanceBefore4 = address(0x4).balance;


vm.warp(10000000);
vm.warp(10_000_000);

instance.getExecOps({
target: address(wingman),
value: 0,
callData: abi.encodeWithSelector(Wingman.updateBackup.selector, "name1", 20000000, beneficiaries),
callData: abi.encodeWithSelector(
Wingman.updateBackup.selector, "name1", 20_000_000, beneficiaries
),
txValidator: address(instance.defaultValidator)
}).execUserOps();

vm.deal(address(instance.account), 350);
Wingman.Backup memory backup = wingman.getBackup(address(instance.account), "name1");

assertEq(backup.createdAt, 10000000);
assertEq(backup.unlockAt, 20000000);
assertEq(backup.createdAt, 10_000_000);
assertEq(backup.unlockAt, 20_000_000);
assertEq(backup.beneficiaries.length, 4);
assertEq(backup.beneficiaries[0].account, address(0x1));
assertEq(backup.beneficiaries[0].amount, 50);
Expand All @@ -75,24 +75,19 @@ contract WingmanTest is RhinestoneModuleKit, Test {
assertEq(backup.beneficiaries[1].amount, 100);
assertEq(backup.beneficiaries[1].percentage, 0);


vm.expectRevert();
wingman.executeBackup(address(instance.account), "name1");
//

vm.warp(20000000);
//
vm.warp(20_000_000);
wingman.executeBackup(address(instance.account), "name1");


assertEq(address(0x1).balance, balanceBefore1 + 50);
assertEq(address(0x2).balance, balanceBefore2 + 100);
assertEq(address(0x3).balance, balanceBefore3 + 50);
assertEq(address(0x4).balance, balanceBefore4 + 150);


// deleted
backup = wingman.getBackup(address(instance.account), "name1");
assertEq(backup.createdAt, 0);

}
}

0 comments on commit cca874a

Please sign in to comment.