creation_sourcecode: // SPDX-License-Identifier: Unlicensed
  /**
   *Submitted for verification at Etherscan.io on 2024-07-04
  */
  
  // File: @openzeppelin/contracts/proxy/Clones.sol
  
  
  // OpenZeppelin Contracts (last updated v5.0.0) (proxy/Clones.sol)
  
  pragma solidity ^0.8.20;
  
  /**
   * @dev https://eips.ethereum.org/EIPS/eip-1167[EIP 1167] is a standard for
   * deploying minimal proxy contracts, also known as "clones".
   *
   * > To simply and cheaply clone contract functionality in an immutable way, this standard specifies
   * > a minimal bytecode implementation that delegates all calls to a known, fixed address.
   *
   * The library includes functions to deploy a proxy using either `create` (traditional deployment) or `create2`
   * (salted deterministic deployment). It also includes functions to predict the addresses of clones deployed using the
   * deterministic method.
   */
  library Clones {
      /**
       * @dev A clone instance deployment failed.
       */
      error ERC1167FailedCreateClone();
  
      /**
       * @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
       *
       * This function uses the create opcode, which should never revert.
       */
      function clone(address implementation) internal returns (address instance) {
          /// @solidity memory-safe-assembly
          assembly {
              // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes
              // of the `implementation` address with the bytecode before the address.
              mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
              // Packs the remaining 17 bytes of `implementation` with the bytecode after the address.
              mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3))
              instance := create(0, 0x09, 0x37)
          }
          if (instance == address(0)) {
              revert ERC1167FailedCreateClone();
          }
      }
  
      /**
       * @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
       *
       * This function uses the create2 opcode and a `salt` to deterministically deploy
       * the clone. Using the same `implementation` and `salt` multiple time will revert, since
       * the clones cannot be deployed twice at the same address.
       */
      function cloneDeterministic(address implementation, bytes32 salt) internal returns (address instance) {
          /// @solidity memory-safe-assembly
          assembly {
              // Cleans the upper 96 bits of the `implementation` word, then packs the first 3 bytes
              // of the `implementation` address with the bytecode before the address.
              mstore(0x00, or(shr(0xe8, shl(0x60, implementation)), 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000))
              // Packs the remaining 17 bytes of `implementation` with the bytecode after the address.
              mstore(0x20, or(shl(0x78, implementation), 0x5af43d82803e903d91602b57fd5bf3))
              instance := create2(0, 0x09, 0x37, salt)
          }
          if (instance == address(0)) {
              revert ERC1167FailedCreateClone();
          }
      }
  
      /**
       * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
       */
      function predictDeterministicAddress(
          address implementation,
          bytes32 salt,
          address deployer
      ) internal pure returns (address predicted) {
          /// @solidity memory-safe-assembly
          assembly {
              let ptr := mload(0x40)
              mstore(add(ptr, 0x38), deployer)
              mstore(add(ptr, 0x24), 0x5af43d82803e903d91602b57fd5bf3ff)
              mstore(add(ptr, 0x14), implementation)
              mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73)
              mstore(add(ptr, 0x58), salt)
              mstore(add(ptr, 0x78), keccak256(add(ptr, 0x0c), 0x37))
              predicted := keccak256(add(ptr, 0x43), 0x55)
          }
      }
  
      /**
       * @dev Computes the address of a clone deployed using {Clones-cloneDeterministic}.
       */
      function predictDeterministicAddress(
          address implementation,
          bytes32 salt
      ) internal view returns (address predicted) {
          return predictDeterministicAddress(implementation, salt, address(this));
      }
  }
  
  // File: @openzeppelin/contracts/token/ERC20/IERC20.sol
  
  
  // OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/IERC20.sol)
  
  pragma solidity ^0.8.20;
  
  /**
   * @dev Interface of the ERC20 standard as defined in the EIP.
   */
  interface IERC20 {
      /**
       * @dev Emitted when `value` tokens are moved from one account (`from`) to
       * another (`to`).
       *
       * Note that `value` may be zero.
       */
      event Transfer(address indexed from, address indexed to, uint256 value);
  
      /**
       * @dev Emitted when the allowance of a `spender` for an `owner` is set by
       * a call to {approve}. `value` is the new allowance.
       */
      event Approval(address indexed owner, address indexed spender, uint256 value);
  
      /**
       * @dev Returns the value of tokens in existence.
       */
      function totalSupply() external view returns (uint256);
  
      /**
       * @dev Returns the value of tokens owned by `account`.
       */
      function balanceOf(address account) external view returns (uint256);
  
      /**
       * @dev Moves a `value` amount of tokens from the caller's account to `to`.
       *
       * Returns a boolean value indicating whether the operation succeeded.
       *
       * Emits a {Transfer} event.
       */
      function transfer(address to, uint256 value) external returns (bool);
  
      /**
       * @dev Returns the remaining number of tokens that `spender` will be
       * allowed to spend on behalf of `owner` through {transferFrom}. This is
       * zero by default.
       *
       * This value changes when {approve} or {transferFrom} are called.
       */
      function allowance(address owner, address spender) external view returns (uint256);
  
      /**
       * @dev Sets a `value` amount of tokens as the allowance of `spender` over the
       * caller's tokens.
       *
       * Returns a boolean value indicating whether the operation succeeded.
       *
       * IMPORTANT: Beware that changing an allowance with this method brings the risk
       * that someone may use both the old and the new allowance by unfortunate
       * transaction ordering. One possible solution to mitigate this race
       * condition is to first reduce the spender's allowance to 0 and set the
       * desired value afterwards:
       * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
       *
       * Emits an {Approval} event.
       */
      function approve(address spender, uint256 value) external returns (bool);
  
      /**
       * @dev Moves a `value` amount of tokens from `from` to `to` using the
       * allowance mechanism. `value` is then deducted from the caller's
       * allowance.
       *
       * Returns a boolean value indicating whether the operation succeeded.
       *
       * Emits a {Transfer} event.
       */
      function transferFrom(address from, address to, uint256 value) external returns (bool);
  }
  
  // File: @openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol
  
  
  // OpenZeppelin Contracts (last updated v5.0.0) (proxy/utils/Initializable.sol)
  
  pragma solidity ^0.8.20;
  
  /**
   * @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
   * behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an
   * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
   * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
   *
   * The initialization functions use a version number. Once a version number is used, it is consumed and cannot be
   * reused. This mechanism prevents re-execution of each "step" but allows the creation of new initialization steps in
   * case an upgrade adds a module that needs to be initialized.
   *
   * For example:
   *
   * [.hljs-theme-light.nopadding]
   * ```solidity
   * contract MyToken is ERC20Upgradeable {
   *     function initialize() initializer public {
   *         __ERC20_init("MyToken", "MTK");
   *     }
   * }
   *
   * contract MyTokenV2 is MyToken, ERC20PermitUpgradeable {
   *     function initializeV2() reinitializer(2) public {
   *         __ERC20Permit_init("MyToken");
   *     }
   * }
   * ```
   *
   * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
   * possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
   *
   * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
   * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
   *
   * [CAUTION]
   * ====
   * Avoid leaving a contract uninitialized.
   *
   * An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation
   * contract, which may impact the proxy. To prevent the implementation contract from being used, you should invoke
   * the {_disableInitializers} function in the constructor to automatically lock it when it is deployed:
   *
   * [.hljs-theme-light.nopadding]
   * ```
   * /// @custom:oz-upgrades-unsafe-allow constructor
   * constructor() {
   *     _disableInitializers();
   * }
   * ```
   * ====
   */
  abstract contract Initializable {
      /**
       * @dev Storage of the initializable contract.
       *
       * It's implemented on a custom ERC-7201 namespace to reduce the risk of storage collisions
       * when using with upgradeable contracts.
       *
       * @custom:storage-location erc7201:openzeppelin.storage.Initializable
       */
      struct InitializableStorage {
          /**
           * @dev Indicates that the contract has been initialized.
           */
          uint64 _initialized;
          /**
           * @dev Indicates that the contract is in the process of being initialized.
           */
          bool _initializing;
      }
  
      // keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.Initializable")) - 1)) & ~bytes32(uint256(0xff))
      bytes32 private constant INITIALIZABLE_STORAGE = 0xf0c57e16840df040f15088dc2f81fe391c3923bec73e23a9662efc9c229c6a00;
  
      /**
       * @dev The contract is already initialized.
       */
      error InvalidInitialization();
  
      /**
       * @dev The contract is not initializing.
       */
      error NotInitializing();
  
      /**
       * @dev Triggered when the contract has been initialized or reinitialized.
       */
      event Initialized(uint64 version);
  
      /**
       * @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope,
       * `onlyInitializing` functions can be used to initialize parent contracts.
       *
       * Similar to `reinitializer(1)`, except that in the context of a constructor an `initializer` may be invoked any
       * number of times. This behavior in the constructor can be useful during testing and is not expected to be used in
       * production.
       *
       * Emits an {Initialized} event.
       */
      modifier initializer() {
          // solhint-disable-next-line var-name-mixedcase
          InitializableStorage storage $ = _getInitializableStorage();
  
          // Cache values to avoid duplicated sloads
          bool isTopLevelCall = !$._initializing;
          uint64 initialized = $._initialized;
  
          // Allowed calls:
          // - initialSetup: the contract is not in the initializing state and no previous version was
          //                 initialized
          // - construction: the contract is initialized at version 1 (no reininitialization) and the
          //                 current contract is just being deployed
          bool initialSetup = initialized == 0 && isTopLevelCall;
          bool construction = initialized == 1 && address(this).code.length == 0;
  
          if (!initialSetup && !construction) {
              revert InvalidInitialization();
          }
          $._initialized = 1;
          if (isTopLevelCall) {
              $._initializing = true;
          }
          _;
          if (isTopLevelCall) {
              $._initializing = false;
              emit Initialized(1);
          }
      }
  
      /**
       * @dev A modifier that defines a protected reinitializer function that can be invoked at most once, and only if the
       * contract hasn't been initialized to a greater version before. In its scope, `onlyInitializing` functions can be
       * used to initialize parent contracts.
       *
       * A reinitializer may be used after the original initialization step. This is essential to configure modules that
       * are added through upgrades and that require initialization.
       *
       * When `version` is 1, this modifier is similar to `initializer`, except that functions marked with `reinitializer`
       * cannot be nested. If one is invoked in the context of another, execution will revert.
       *
       * Note that versions can jump in increments greater than 1; this implies that if multiple reinitializers coexist in
       * a contract, executing them in the right order is up to the developer or operator.
       *
       * WARNING: Setting the version to 2**64 - 1 will prevent any future reinitialization.
       *
       * Emits an {Initialized} event.
       */
      modifier reinitializer(uint64 version) {
          // solhint-disable-next-line var-name-mixedcase
          InitializableStorage storage $ = _getInitializableStorage();
  
          if ($._initializing || $._initialized >= version) {
              revert InvalidInitialization();
          }
          $._initialized = version;
          $._initializing = true;
          _;
          $._initializing = false;
          emit Initialized(version);
      }
  
      /**
       * @dev Modifier to protect an initialization function so that it can only be invoked by functions with the
       * {initializer} and {reinitializer} modifiers, directly or indirectly.
       */
      modifier onlyInitializing() {
          _checkInitializing();
          _;
      }
  
      /**
       * @dev Reverts if the contract is not in an initializing state. See {onlyInitializing}.
       */
      function _checkInitializing() internal view virtual {
          if (!_isInitializing()) {
              revert NotInitializing();
          }
      }
  
      /**
       * @dev Locks the contract, preventing any future reinitialization. This cannot be part of an initializer call.
       * Calling this in the constructor of a contract will prevent that contract from being initialized or reinitialized
       * to any version. It is recommended to use this to lock implementation contracts that are designed to be called
       * through proxies.
       *
       * Emits an {Initialized} event the first time it is successfully executed.
       */
      function _disableInitializers() internal virtual {
          // solhint-disable-next-line var-name-mixedcase
          InitializableStorage storage $ = _getInitializableStorage();
  
          if ($._initializing) {
              revert InvalidInitialization();
          }
          if ($._initialized != type(uint64).max) {
              $._initialized = type(uint64).max;
              emit Initialized(type(uint64).max);
          }
      }
  
      /**
       * @dev Returns the highest version that has been initialized. See {reinitializer}.
       */
      function _getInitializedVersion() internal view returns (uint64) {
          return _getInitializableStorage()._initialized;
      }
  
      /**
       * @dev Returns `true` if the contract is currently initializing. See {onlyInitializing}.
       */
      function _isInitializing() internal view returns (bool) {
          return _getInitializableStorage()._initializing;
      }
  
      /**
       * @dev Returns a pointer to the storage namespace.
       */
      // solhint-disable-next-line var-name-mixedcase
      function _getInitializableStorage() private pure returns (InitializableStorage storage $) {
          assembly {
              $.slot := INITIALIZABLE_STORAGE
          }
      }
  }
  
  // File: newFactorySTYLE.sol
  
  
  pragma solidity ^0.8.9;
  
  
  
  
  
  contract Swap is Initializable {
      struct Vesting {
          address beneficiary;
          uint256 cliffDuration;
          uint totalAmount;
          uint start;
          uint duration;
          uint lastClaimed;
          uint totalClaimed;
          address contractAddress;
      }
  
      error NotOwner();
      error SwapFailed();
  
      event TokensSwapped(
          address indexed user,
          address indexed oldToken,
          address indexed newToken,
          uint amount
      );
  
      IERC20 public oldToken;
      IERC20 public newToken;
  
      address public owner;
  
      mapping(address => Vesting) public vestings;
      uint256 public cliffDuration;
      uint256 public duration;
  
      modifier onlyOwner() {
          if (msg.sender != owner) {
              revert NotOwner();
          }
          _;
      }
  
      function initialize(
          address _oldToken,
          address _newToken,
          address _owner,
          uint _cliffDuration,
          uint _duration
      ) external initializer {
          oldToken = IERC20(_oldToken);
          newToken = IERC20(_newToken);
          owner = _owner;
          cliffDuration = _cliffDuration;
          duration = _duration;
      }
  
      function isClaimable() external view returns (bool) {
          return vestings[msg.sender].start != 0;
      }
  
      function setCliffDuration(uint _days) external onlyOwner {
          cliffDuration = _days;
      }
  
      function setDuration(uint _days) external onlyOwner {
          duration = _days;
      }
  
      function setOldToken(address _token) external onlyOwner {
          oldToken = IERC20(_token);
      }
  
      function setNewToken(address _token) external onlyOwner {
          newToken = IERC20(_token);
      }
  
      // add token address as a parameter
      function withdrawToken(
          uint amount,
          address tokenAddress
      ) external onlyOwner {
          require(
              amount <= IERC20(tokenAddress).balanceOf(address(this)),
              "Insufficient Balance"
          );
          bool success = IERC20(tokenAddress).transfer(msg.sender, amount);
          require(success, "Transfer Failed");
      }
  
      function calculateAmount() public view returns (uint amount) {
          Vesting memory vesting = vestings[msg.sender];
  
          require(
              (vesting.start + vesting.duration * 1 days) >= block.timestamp,
              "Vesting Period ended"
          );
  
          require(
              (vesting.start + vesting.cliffDuration * 1 days) <= block.timestamp,
              "Vesting Period didn't start yet"
          );
          amount = 0;
          if (vesting.lastClaimed != 0) {
              amount =
                  ((block.timestamp - vesting.lastClaimed) *
                      vesting.totalAmount) /
                  (vesting.duration * 1 days - vesting.cliffDuration * 1 days);
          } else {
              amount =
                  ((block.timestamp - vesting.start) * vesting.totalAmount) /
                  (vesting.duration * 1 days - vesting.cliffDuration * 1 days);
          }
      }
  
      //add claim amount function
      function swap() external returns (bool _success) {
          uint amount = oldToken.balanceOf(msg.sender);
  
          //prevents swapping zero(0) amount
          require(amount > 0, "Zero Amount");
  
          Vesting memory _vesting = Vesting(
              msg.sender,
              cliffDuration,
              amount,
              block.timestamp,
              duration,
              0,
              0,
              address(newToken)
          );
          vestings[msg.sender] = _vesting;
  
          _success = true;
  
          if (_success) {
              emit TokensSwapped(
                  msg.sender,
                  address(oldToken),
                  address(newToken),
                  amount
              );
          } else {
              revert SwapFailed();
          }
      }
  
      function claim() external {
          Vesting memory vesting = vestings[msg.sender];
          require(
              vesting.beneficiary == msg.sender,
              "Only beneficiary can claim"
          );
          uint amount = calculateAmount();
  
          require(amount > 0, "Amount must be greater than zero");
          require(
              amount < vesting.totalAmount - vesting.totalClaimed,
              "Insufficient amount to be claimed"
          );
          //transfer from approved balance given by msg.sender
          bool success = oldToken.transferFrom(msg.sender, address(this), amount);
          require(success, "Swapping failed");
          vesting.lastClaimed = block.timestamp;
          vesting.totalClaimed += amount;
          vestings[msg.sender] = vesting;
          bool success2 = newToken.transferFrom(owner, msg.sender, amount);
          require(success2, "Swapping failed");
      }
  }
  
  contract SwapFactory {
      address public immutable swapImplementation;
      address private immutable _owner;
  
      mapping(address => address[]) private _admins;
  
      function getSwaps() external view returns (address[] memory) {
          return _admins[msg.sender];
      }
  
      constructor() {
          swapImplementation = address(new Swap());
      }
  
      function createSwap(
          address _oldToken,
          address _newToken,
          uint _cliffDuration,
          uint _duration
      ) external returns (address) {
          // uint balanceA = IERC20(tokenA).balanceOf(_beneficiary);
          // require(balanceA > 0, "Insufficient balance Token A from beneficiary");
          uint balanceB = IERC20(_newToken).balanceOf(msg.sender);
          require(balanceB >= 0, "Insufficient balance Token B from admin");
          if (_duration == 0) {
              revert();
          }
          bytes32 salt = keccak256(
              abi.encodePacked(msg.sender, _admins[msg.sender].length)
          );
  
          address clone = Clones.cloneDeterministic(swapImplementation, salt);
          _admins[msg.sender].push(clone);
          Swap(clone).initialize(
              _oldToken,
              _newToken,
              msg.sender,
              _cliffDuration,
              _duration
          );
          return clone;
      }
  }