Skip to content
Closed
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
31 changes: 30 additions & 1 deletion src/AccessManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,25 @@ pragma solidity ^0.8.30;

import "@openzeppelin/contracts/access/AccessControl.sol";
import "./Roles.sol";

contract AccessManager is AccessControl {
using Roles for bytes32;

constructor() {
_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);

// Set role admin relationships
_setRoleAdmin(Roles.ADMIN_ROLE, DEFAULT_ADMIN_ROLE);
_setRoleAdmin(Roles.MULTISIG_ADMIN_ROLE, DEFAULT_ADMIN_ROLE);
_setRoleAdmin(Roles.ALLOWED_EIP712_SWAP_ROLE, DEFAULT_ADMIN_ROLE);
}

function addAdmin(address _admin) external onlyRole(DEFAULT_ADMIN_ROLE) {
_grantRole(Roles.ADMIN_ROLE, _admin);
}

function removeAdmin(address _admin) external onlyRole(DEFAULT_ADMIN_ROLE) {
_revokeRole(Roles.ADMIN_ROLE, _admin);
}

function addMultisigAdmin(address _multisigAdmin) external onlyRole(DEFAULT_ADMIN_ROLE) {
Expand All @@ -18,7 +31,23 @@ contract AccessManager is AccessControl {
_revokeRole(Roles.MULTISIG_ADMIN_ROLE, _multisigAdmin);
}

function addEIP712Swapper(address _swapper) external onlyRole(DEFAULT_ADMIN_ROLE) {
_grantRole(Roles.ALLOWED_EIP712_SWAP_ROLE, _swapper);
}

function removeEIP712Swapper(address _swapper) external onlyRole(DEFAULT_ADMIN_ROLE) {
_revokeRole(Roles.ALLOWED_EIP712_SWAP_ROLE, _swapper);
}

function isAdmin(address _address) external view returns (bool) {
return hasRole(Roles.ADMIN_ROLE, _address);
}

function isMultisigAdmin(address _address) external view returns (bool) {
return hasRole(Roles.MULTISIG_ADMIN_ROLE, _address);
}
}

function isEIP712Swapper(address _address) external view returns (bool) {
return hasRole(Roles.ALLOWED_EIP712_SWAP_ROLE, _address);
}
}
77 changes: 49 additions & 28 deletions src/EIP712Swap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,62 @@ import "./LiquidityPool.sol";
import "./ISwap.sol";

contract EIP712Swap is EIP712 {
using ECDSA for bytes32;
using ECDSA for bytes32;

mapping(address => uint256) private _nonces;
mapping(address => uint256) private _nonces;

bytes32 private constant SWAP_TYPEHASH =
keccak256("SwapRequest(address pool,address sender,address tokenIn,address tokenOut,uint256 amountIn,uint256 minAmountOut,uint256 nonce,uint256 deadline)");
bytes32 private constant SWAP_TYPEHASH = keccak256(
"SwapRequest(address pool,address sender,address tokenIn,address tokenOut,uint256 amountIn,uint256 minAmountOut,uint256 nonce,uint256 deadline)"
);

error InvalidSignature();
error ExpiredSwapRequest();
error InvalidNonce();
error InvalidSignature();
error ExpiredSwapRequest();
error InvalidNonce();

constructor() EIP712("EIP712Swap", "1") {}
constructor() EIP712("EIP712Swap", "1") {}

function getDomainSeparator() public view returns (bytes32) {
return _domainSeparatorV4();
}
function getDomainSeparator() public view returns (bytes32) {
return _domainSeparatorV4();
}

function getNonce(address _sender) public view returns (uint256) {
return _nonces[_sender];
}
function getNonce(address _sender) public view returns (uint256) {
return _nonces[_sender];
}

function verify(ISwap.SwapRequest memory _swapRequest, bytes memory _signature) public view returns (bool) {
bytes32 digest = _hashTypedDataV4(keccak256(abi.encode(SWAP_TYPEHASH, _swapRequest.pool, _swapRequest.sender, _swapRequest.tokenIn, _swapRequest.tokenOut, _swapRequest.amountIn, _swapRequest.minAmountOut, _swapRequest.nonce, _swapRequest.deadline)));
address signer = digest.recover(_signature);
return signer == _swapRequest.sender;
}
function verify(ISwap.SwapRequest memory _swapRequest, bytes memory _signature) public view returns (bool) {
bytes32 digest = _hashTypedDataV4(
keccak256(
abi.encode(
SWAP_TYPEHASH,
_swapRequest.pool,
_swapRequest.sender,
_swapRequest.tokenIn,
_swapRequest.tokenOut,
_swapRequest.amountIn,
_swapRequest.minAmountOut,
_swapRequest.nonce,
_swapRequest.deadline
)
)
);
address signer = digest.recover(_signature);
return signer == _swapRequest.sender;
}

function executeSwap(ISwap.SwapRequest memory _swapRequest, bytes memory _signature) public returns (bool) {
if (!verify(_swapRequest, _signature)) revert InvalidSignature();
if (_swapRequest.deadline < block.timestamp) revert ExpiredSwapRequest();
if (_swapRequest.nonce != _nonces[_swapRequest.sender]) revert InvalidNonce();
function executeSwap(ISwap.SwapRequest memory _swapRequest, bytes memory _signature) public returns (bool) {
if (!verify(_swapRequest, _signature)) revert InvalidSignature();
if (_swapRequest.deadline < block.timestamp) revert ExpiredSwapRequest();
if (_swapRequest.nonce != _nonces[_swapRequest.sender]) revert InvalidNonce();

_nonces[_swapRequest.sender]++;
LiquidityPool(_swapRequest.pool).swap(_swapRequest.sender, _swapRequest.tokenIn, _swapRequest.tokenOut, _swapRequest.amountIn, _swapRequest.minAmountOut);
_nonces[_swapRequest.sender]++;
LiquidityPool(_swapRequest.pool).swap(
_swapRequest.sender,
_swapRequest.tokenIn,
_swapRequest.tokenOut,
_swapRequest.amountIn,
_swapRequest.minAmountOut
);

return true;
}
}
return true;
}
}
17 changes: 11 additions & 6 deletions src/FeeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ pragma solidity ^0.8.30;
import "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

import "./ISwap.sol";

contract FeeManager is Initializable, AccessControlUpgradeable, UUPSUpgradeable {
uint256 public constant FEE_DENOMINATOR = 10000;

uint256 public fee;
uint256 public fee; // Fee in basis points (e.g., 250 = 2.5%)

function initialize(uint256 _fee) external initializer {
_disableInitializers();
__AccessControl_init();
__UUPSUpgradeable_init();
_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
fee = _fee;
}
Expand All @@ -23,7 +22,13 @@ contract FeeManager is Initializable, AccessControlUpgradeable, UUPSUpgradeable
fee = _fee;
}

/// @notice Calculate absolute fee amount for a swap
/// @param swapParams The swap parameters
/// @return Absolute fee amount in output token units
function getFee(ISwap.SwapParams memory swapParams) external view returns (uint256) {
return (swapParams.amount0 * fee) / FEE_DENOMINATOR;
// Calculate fee based on input amount and convert to output token equivalent
uint256 amountOut =
(swapParams.amount0 * swapParams.reserveToken1) / (swapParams.reserveToken0 + swapParams.amount0);
return (amountOut * fee) / FEE_DENOMINATOR;
}
}
}
66 changes: 33 additions & 33 deletions src/ISwap.sol
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
pragma solidity ^0.8.30;

interface ISwap {
/// @notice Parameters for the swap
/// @param token0 The address of the first token
/// @param token1 The address of the second token
/// @param amount0 The amount of the first token to swap
/// @param reserveToken0 The reserve of the first token
/// @param reserveToken1 The reserve of the second token
struct SwapParams {
address token0;
address token1;
uint256 amount0;
uint256 reserveToken0;
uint256 reserveToken1;
}
/// @notice Parameters for the swap
/// @param token0 The address of the first token
/// @param token1 The address of the second token
/// @param amount0 The amount of the first token to swap
/// @param reserveToken0 The reserve of the first token
/// @param reserveToken1 The reserve of the second token
struct SwapParams {
address token0;
address token1;
uint256 amount0;
uint256 reserveToken0;
uint256 reserveToken1;
}

/// @notice Parameters for the swap request
/// @param pool The address of the pool
/// @param sender The address of the sender
/// @param tokenIn The address of the token to swap in
/// @param tokenOut The address of the token to swap out
/// @param amountIn The amount of the token to swap in
/// @param minAmountOut The minimum amount of the token to swap out
/// @param nonce The nonce of the swap
/// @param deadline The deadline of the swap
struct SwapRequest {
address pool;
address sender;
address tokenIn;
address tokenOut;
uint256 amountIn;
uint256 minAmountOut;
uint256 nonce;
uint256 deadline;
}
}
/// @notice Parameters for the swap request
/// @param pool The address of the pool
/// @param sender The address of the sender
/// @param tokenIn The address of the token to swap in
/// @param tokenOut The address of the token to swap out
/// @param amountIn The amount of the token to swap in
/// @param minAmountOut The minimum amount of the token to swap out
/// @param nonce The nonce of the swap
/// @param deadline The deadline of the swap
struct SwapRequest {
address pool;
address sender;
address tokenIn;
address tokenOut;
uint256 amountIn;
uint256 minAmountOut;
uint256 nonce;
uint256 deadline;
}
}
Loading
Loading