Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] batch transactions #82

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/HookCoveredCallImplV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import "@openzeppelin/contracts/utils/Create2.sol";

import "./lib/Entitlements.sol";
import "./lib/BeaconSalts.sol";
import "./lib/Exec.sol";

import "./interfaces/IHookERC721VaultFactory.sol";
import "./interfaces/IHookVault.sol";
Expand All @@ -52,6 +53,8 @@ import "./interfaces/IWETH.sol";

import "./mixin/PermissionConstants.sol";
import "./mixin/HookInstrumentERC721.sol";
import "forge-std/Test.sol";


/// @title HookCoveredCallImplV1 an implementation of covered calls on Hook
/// @author Jake [email protected]
Expand All @@ -64,7 +67,9 @@ contract HookCoveredCallImplV1 is
HookInstrumentERC721,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there functions within here, like on the OZ contracts, that pay attention to msg.Sender... do we need to update them?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems likely that we want to allow only a fixed set of external functions call with msg.sender, but perhaps there are some like safeTransferFrom where that is not the case?

ReentrancyGuard,
Initializable,
PermissionConstants
PermissionConstants,
BatchUtil,
Test
{
using Counters for Counters.Counter;

Expand Down Expand Up @@ -139,6 +144,8 @@ contract HookCoveredCallImplV1 is
/// financial situation for the holder of the options.
bool public marketPaused;

address public execAddr;

/// @dev Emitted when the market is paused or unpaused
/// @param paused true if paused false otherwise
event MarketPauseUpdated(bool paused);
Expand Down Expand Up @@ -192,6 +199,9 @@ contract HookCoveredCallImplV1 is
}

/// ---- Option Writer Functions ---- //
function setExec(address _execAddr) public {
execAddr = _execAddr;
}

/// @dev See {IHookCoveredCall-mintWithVault}.
function mintWithVault(
Expand Down Expand Up @@ -292,16 +302,23 @@ contract HookCoveredCallImplV1 is
uint128 strikePrice,
uint32 expirationTime
) external nonReentrant whenNotPaused returns (uint256) {
address msgSender = msg.sender;
emit log_named_address("msg.sender", msgSender);
if (msg.sender == execAddr) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be a modifier, that way we can always just use the msgSender variable. Or an alternative would be a library or internal function somehow, and we chang all other usages to msgSender (which falls back to msg.Sender if not the trusted proxy address)

msgSender = unpackTrailingParamMsgSender();
emit log_named_address("msgSender", msgSender);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should cut these logs?

}

address tokenOwner = IERC721(tokenAddress).ownerOf(tokenId);
require(
allowedUnderlyingAddress == tokenAddress,
"mWE7-token not on allowlist"
);

require(
msg.sender == tokenOwner ||
IERC721(tokenAddress).isApprovedForAll(tokenOwner, msg.sender) ||
IERC721(tokenAddress).getApproved(tokenId) == msg.sender,
msgSender == tokenOwner ||
IERC721(tokenAddress).isApprovedForAll(tokenOwner, msgSender) ||
IERC721(tokenAddress).getApproved(tokenId) == msgSender,
"mWE7-caller not owner or operator"
);

Expand Down
28 changes: 28 additions & 0 deletions src/lib/Exec.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.10;

contract Exec {
struct Operation {
address dest;
bytes data;
}

function batch(Operation[] calldata operations) external returns (bytes memory) {
for (uint256 i = 0; i < operations.length; ++i) {
Operation calldata operation = operations[i];
address destAddr = operation.dest;
bytes memory dataWithSender = abi.encodePacked(operation.data, msg.sender);
(bool success, bytes memory result) = destAddr.call(dataWithSender);
require(success, "Delegate call failed");
return result;
}
}
}

contract BatchUtil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be a library? perhaps should also be in a different file?

function unpackTrailingParamMsgSender() internal pure returns (address msgSender) {
assembly {
msgSender := shr(96, calldataload(sub(calldatasize(), 20)))
}
}
}
20 changes: 13 additions & 7 deletions src/test/HookCoveredCallIntegrationTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ contract HookCoveredCallIntegrationTest is HookProtocolTest {
vm.startPrank(address(writer));
uint32 expiration = uint32(block.timestamp) + 3 days;

emit log_named_address("writer", address(writer));
Exec.Operation[] memory operations = new Exec.Operation[](1);
operations[0] = Exec.Operation(address(calls), abi.encodeWithSignature(
"mintWithErc721(address,uint256,uint128,uint32)",
address(token),
underlyingTokenId,
1000,
expiration
)
);

vm.expectEmit(true, true, true, false);
emit CallCreated(
address(writer),
Expand All @@ -47,13 +58,8 @@ contract HookCoveredCallIntegrationTest is HookProtocolTest {
1000,
expiration
);
uint256 optionId = calls.mintWithErc721(
address(token),
underlyingTokenId,
1000,
expiration
);

bytes memory result = exec.batch(operations);
uint256 optionId = uint256(bytes32(result));
assertTrue(
calls.ownerOf(optionId) == address(writer),
"owner should own the option"
Expand Down
6 changes: 6 additions & 0 deletions src/test/utils/base.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import "../../HookProtocol.sol";

import "../../lib/Entitlements.sol";
import "../../lib/Signatures.sol";
import "../../lib/Exec.sol";
import "../../mixin/EIP712.sol";
import "../../mixin/PermissionConstants.sol";

Expand All @@ -44,6 +45,7 @@ contract HookProtocolTest is Test, EIP712, PermissionConstants {
uint256 internal optionTokenId;
address internal preApprovedOperator;
HookERC721VaultFactory vaultFactory;
Exec exec;

event CallCreated(
address writer,
Expand Down Expand Up @@ -82,6 +84,7 @@ contract HookProtocolTest is Test, EIP712, PermissionConstants {
}

function setUpFullProtocol() public {
exec = new Exec();
weth = new WETH();
protocol = new HookProtocol(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it make sense to have exec live on HookProtocol and then just make the HookProtocol address that all of these contracts already store the exec address?

admin,
Expand Down Expand Up @@ -141,6 +144,9 @@ contract HookProtocolTest is Test, EIP712, PermissionConstants {
// make a call insturment for our token
calls = IHookCoveredCall(callFactory.makeCallInstrument(address(token)));
callInternal = HookCoveredCallImplV1(address(calls));

// set exec for call instrument
callInternal.setExec(address(exec));
}

function setUpMintOption() public {
Expand Down