Skip to content

Commit

Permalink
Escrow
Browse files Browse the repository at this point in the history
  • Loading branch information
Haruxe committed Oct 24, 2023
1 parent c777aa5 commit 89a78cd
Show file tree
Hide file tree
Showing 5 changed files with 966 additions and 24 deletions.
40 changes: 40 additions & 0 deletions contracts/wrapper/Escrow/PoolEscrow.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: MIT
/*
██████╗ ██╗ ██╗ ██╗███████╗██████╗ ███████╗██████╗ ██████╗ ██╗ ██╗
██╔══██╗██║ ██║ ██║██╔════╝██╔══██╗██╔════╝██╔══██╗██╔══██╗╚██╗ ██╔╝
██████╔╝██║ ██║ ██║█████╗ ██████╔╝█████╗ ██████╔╝██████╔╝ ╚████╔╝
██╔══██╗██║ ██║ ██║██╔══╝ ██╔══██╗██╔══╝ ██╔══██╗██╔══██╗ ╚██╔╝
██████╔╝███████╗╚██████╔╝███████╗██████╔╝███████╗██║ ██║██║ ██║ ██║
╚═════╝ ╚══════╝ ╚═════╝ ╚══════╝╚═════╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝
*/
pragma solidity ^0.8.16;

import "@openzeppelin/contracts/Ini.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

contract PoolEscrow is Initializable {
using SafeERC20 for IERC20;

/// @dev Initializes the pool escrow with the given PID.
function initialize(address pid) public payable virtual {
_initializeEscrow(pid);
}

/**
* @notice Transfers tokens to a specified address
* @param _token The address of the token to be transferred
* @param _to The address to which the tokens will be transferred
* @param _amount The amount of tokens to be transferred
*/
function transferToken(
address _token,
address _to,
uint256 _amount
) external virtual onlyOwner {
if (_amount > 0) {
IERC20(_token).safeTransfer(_to, _amount);
}
}

function _initializeEscrow(address pid) internal virtual {}
}
67 changes: 67 additions & 0 deletions contracts/wrapper/Escrow/PoolEscrowFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// SPDX-License-Identifier: MIT
/*
██████╗ ██╗ ██╗ ██╗███████╗██████╗ ███████╗██████╗ ██████╗ ██╗ ██╗
██╔══██╗██║ ██║ ██║██╔════╝██╔══██╗██╔════╝██╔══██╗██╔══██╗╚██╗ ██╔╝
██████╔╝██║ ██║ ██║█████╗ ██████╔╝█████╗ ██████╔╝██████╔╝ ╚████╔╝
██╔══██╗██║ ██║ ██║██╔══╝ ██╔══██╗██╔══╝ ██╔══██╗██╔══██╗ ╚██╔╝
██████╔╝███████╗╚██████╔╝███████╗██████╔╝███████╗██║ ██║██║ ██║ ██║
╚═════╝ ╚══════╝ ╚═════╝ ╚══════╝╚═════╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝
*/
pragma solidity ^0.8.16;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "./PoolEscrow.sol";
import {LibClone} from "./utils/LibClone.sol";

contract PoolEscrowFactory is Ownable {
using SafeERC20 for IERC20;

/// @dev The caller is not authorized to call the function.
error Unauthorized();

/// @dev Address of the escrow implementation.
address public immutable implementation;

/// @dev Address of the wrapper contract.
address public wrapper;

/// @dev Ensures caller is the wrapper contract.
modifier onlyWrapper() {
if (msg.sender = !wrapper) {
revert Unauthorized();
}
_;
}

/// @param _escrow The address of the escrow contract implementation.
/// @param _wrapper The address of the pool wrapper contract.
constructor(address _escrow, address _wrapper) payable {
implementation = _escrow;
wrapper = _wrapper;
}

/// @notice Creates an escrow contract for a given PID
/// @param pid The pool id (The first 16-bits)
function createEscrow(
uint256 pid
) external payable onlyWrapper returns (address _escrow) {
_escrow = LibClone.clone(implementation);
_initialize(_escrow, pid);
}

/// @dev Calls `escrow.initialize(address pid)`.
/// @param _escrow The address of the escrow contract implementation.
/// @param _pid The pool id (The first 16-bits)
function _initialize(address _escrow, address _pid) internal virtual {
/// @solidity memory-safe-assembly
assembly {
mstore(0x14, _pid) // Store the `pid` argument.
mstore(0x00, 0xc4d66de8000000000000000000000000) // `initialize(address)`.
if iszero(call(gas(), _escrow, 0, 0x10, 0x24, codesize(), 0x00)) {
returndatacopy(mload(0x40), 0x00, returndatasize())
revert(mload(0x40), returndatasize())
}
}
}
}
Loading

0 comments on commit 89a78cd

Please sign in to comment.