From 8606e5f208d036cb921ae807899781a02f46b11e Mon Sep 17 00:00:00 2001 From: kl456123 Date: Thu, 23 May 2024 12:01:36 +0800 Subject: [PATCH 1/2] fix approve --- packages/hebao_v3/contracts/base/SmartWallet.sol | 10 ---------- .../contracts/base/libwallet/AutomationLib.sol | 16 +++++++--------- packages/hebao_v3/deployments/deployments.json | 2 +- .../hebao_v3/test/automation/automation.test.ts | 5 +++++ .../hebao_v3/test/automation/automation_utils.ts | 12 ++++++------ 5 files changed, 19 insertions(+), 26 deletions(-) diff --git a/packages/hebao_v3/contracts/base/SmartWallet.sol b/packages/hebao_v3/contracts/base/SmartWallet.sol index f10a56d65..342ce671b 100644 --- a/packages/hebao_v3/contracts/base/SmartWallet.sol +++ b/packages/hebao_v3/contracts/base/SmartWallet.sol @@ -498,16 +498,6 @@ abstract contract SmartWallet is return AutomationLib.approveExecutor(wallet, executor, validUntil); } - function unApproveExecutor( - address executor - ) external onlyFromEntryPointOrWalletOrOwnerWhenUnlocked { - _require( - AutomationLib.isExecutorOrOwner(wallet, executor), - Errors.NOT_EXECUTOR - ); - return AutomationLib.unApproveExecutor(wallet, executor); - } - function castFromEntryPoint( address[] calldata targets, bytes[] calldata datas diff --git a/packages/hebao_v3/contracts/base/libwallet/AutomationLib.sol b/packages/hebao_v3/contracts/base/libwallet/AutomationLib.sol index 7e8fd6993..8638fc017 100644 --- a/packages/hebao_v3/contracts/base/libwallet/AutomationLib.sol +++ b/packages/hebao_v3/contracts/base/libwallet/AutomationLib.sol @@ -86,19 +86,17 @@ library AutomationLib { address executor, uint256 validUntil ) internal { + uint256 curValidUntil = wallet.executorsPermission[executor]; require( - wallet.executorsPermission[executor] < validUntil, + curValidUntil == 0 || validUntil == 0, "approve failed" ); wallet.executorsPermission[executor] = validUntil; - emit AutomationApproveExecutor(address(this), executor, validUntil); - } - function unApproveExecutor( - Wallet storage wallet, - address executor - ) internal { - wallet.executorsPermission[executor] = 0; - emit AutomationUnapproveExecutor(address(this), executor); + if (validUntil == 0) { + emit AutomationUnapproveExecutor(address(this), executor); + } else { + emit AutomationApproveExecutor(address(this), executor, validUntil); + } } } diff --git a/packages/hebao_v3/deployments/deployments.json b/packages/hebao_v3/deployments/deployments.json index fa5478148..be6a4df0a 100644 --- a/packages/hebao_v3/deployments/deployments.json +++ b/packages/hebao_v3/deployments/deployments.json @@ -58,4 +58,4 @@ "USDT": "0xB2DC2da9684DfEF77CFa5c6bb07e733023715292", "LRC": "0xc837BbEa8C7b0caC0e8928f797ceB04A34c9c06e" } -} \ No newline at end of file +} diff --git a/packages/hebao_v3/test/automation/automation.test.ts b/packages/hebao_v3/test/automation/automation.test.ts index fe7b1b4e2..6114175ea 100644 --- a/packages/hebao_v3/test/automation/automation.test.ts +++ b/packages/hebao_v3/test/automation/automation.test.ts @@ -81,6 +81,11 @@ describe('automation test', () => { ).to.revertedWith('LRC#104') const executor = ethers.Wallet.createRandom() await approveExecutor(loadedFixture, executor.address) + await approveExecutor( + loadedFixture, + executor.address, + ethers.constants.MaxUint256 + ) // check it is a valid executor const { smartWallet } = loadedFixture expect(await smartWallet.isExecutorOrOwner(executor.address)).to diff --git a/packages/hebao_v3/test/automation/automation_utils.ts b/packages/hebao_v3/test/automation/automation_utils.ts index 00691e80a..95fcd3ea8 100644 --- a/packages/hebao_v3/test/automation/automation_utils.ts +++ b/packages/hebao_v3/test/automation/automation_utils.ts @@ -335,7 +335,10 @@ export const userOpCast = async ( export const approveExecutor = async ( loadedFixture: Fixture, - executorAddr: string + executorAddr: string, + validUntil: BigNumberish = Math.floor( + Date.now() / 1000 + 24 * 60 * 60 + ).toString() ): Promise => { const { smartWallet, @@ -345,9 +348,6 @@ export const approveExecutor = async ( sendUserOp } = loadedFixture const nonce = await smartWallet.getNonce() - const validUntil = Math.floor( - Date.now() / 1000 + 24 * 60 * 60 - ).toString() const data = smartWallet.interface.encodeFunctionData( 'approveExecutor', [executorAddr, validUntil] @@ -387,8 +387,8 @@ export const unApproveExecutor = async ( } = loadedFixture const nonce = await smartWallet.getNonce() const data = smartWallet.interface.encodeFunctionData( - 'unApproveExecutor', - [executor] + 'approveExecutor', + [executor, 0] ) const signedUserOp = await getSignedUserOp( data, From c63d9a6e22015e3a12e4c201cb227304c419e29c Mon Sep 17 00:00:00 2001 From: kl456123 Date: Thu, 23 May 2024 14:47:04 +0800 Subject: [PATCH 2/2] remove unapprove event --- .../contracts/base/libwallet/AutomationLib.sol | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/packages/hebao_v3/contracts/base/libwallet/AutomationLib.sol b/packages/hebao_v3/contracts/base/libwallet/AutomationLib.sol index 8638fc017..c9b4ae7e8 100644 --- a/packages/hebao_v3/contracts/base/libwallet/AutomationLib.sol +++ b/packages/hebao_v3/contracts/base/libwallet/AutomationLib.sol @@ -16,8 +16,6 @@ library AutomationLib { uint validUntils ); - event AutomationUnapproveExecutor(address wallet, address executor); - function _spell( address _target, bytes memory _data @@ -87,16 +85,9 @@ library AutomationLib { uint256 validUntil ) internal { uint256 curValidUntil = wallet.executorsPermission[executor]; - require( - curValidUntil == 0 || validUntil == 0, - "approve failed" - ); + require(curValidUntil == 0 || validUntil == 0, "approve failed"); wallet.executorsPermission[executor] = validUntil; - if (validUntil == 0) { - emit AutomationUnapproveExecutor(address(this), executor); - } else { - emit AutomationApproveExecutor(address(this), executor, validUntil); - } + emit AutomationApproveExecutor(address(this), executor, validUntil); } }