// SPDX-License-Identifier: MIT pragma solidity ^0.8.20; /* * PaymentVault: USDT deposits + automatic (signed) withdrawals for the MLM site. * * HOW IT WORKS * - Deposit: the user approves USDT, then calls deposit(userId, amount). The contract pulls the USDT * and emits Deposit(from, userId, amount). The PHP site reads this event and credits the balance. * - Withdraw: the PHP site checks the user's balance, deducts it, and signs an approval with a SIGNER key * (this key holds NO funds). The user sends that approval to withdraw(); the contract checks the * signature and the limits, then pays out USDT to the user's wallet. The user pays the BNB gas. * * SAFETY * - Limits per withdrawal, per wallet per day, and for the whole contract per day (UTC day). * - Each approval (withdrawalId) works once, only for the wallet it was signed for, and expires at its deadline. * - The OWNER (use a separate, safe wallet, ideally a multisig) can pause everything, change the signer and * limits, and move funds out. The signer key alone can NOT move funds beyond the limits. * * COMPILE (Remix): compiler 0.8.24, EVM version "paris", optimizer ON (200 runs). * The imports below are pinned to OpenZeppelin 5.0.2; Remix downloads them automatically. */ import {IERC20} from "@openzeppelin/contracts@5.0.2/token/ERC20/IERC20.sol"; import {IERC20Metadata} from "@openzeppelin/contracts@5.0.2/token/ERC20/extensions/IERC20Metadata.sol"; import {SafeERC20} from "@openzeppelin/contracts@5.0.2/token/ERC20/utils/SafeERC20.sol"; import {Ownable} from "@openzeppelin/contracts@5.0.2/access/Ownable.sol"; import {Ownable2Step} from "@openzeppelin/contracts@5.0.2/access/Ownable2Step.sol"; import {Pausable} from "@openzeppelin/contracts@5.0.2/utils/Pausable.sol"; import {ReentrancyGuard} from "@openzeppelin/contracts@5.0.2/utils/ReentrancyGuard.sol"; import {ECDSA} from "@openzeppelin/contracts@5.0.2/utils/cryptography/ECDSA.sol"; import {MessageHashUtils} from "@openzeppelin/contracts@5.0.2/utils/cryptography/MessageHashUtils.sol"; contract PaymentVault is Ownable2Step, Pausable, ReentrancyGuard { using SafeERC20 for IERC20; bytes32 public constant WITHDRAW_TYPEHASH = keccak256("Withdraw(uint256 chainId,address vault,address to,bytes32 id,uint256 amount,uint256 deadline)"); IERC20 public immutable token; uint256 public immutable unit; // 10 ** token decimals (USDT on BSC has 18 decimals) address public signer; uint256 public minDeposit; uint256 public maxPerWithdrawal; uint256 public maxPerWalletPerDay; uint256 public maxTotalPerDay; mapping(bytes32 => bool) public used; // withdrawalId => already paid mapping(address => mapping(uint256 => uint256)) public walletWithdrawnOnDay; // wallet => day => amount mapping(uint256 => uint256) public totalWithdrawnOnDay; // day => amount event Deposit(address indexed from, uint256 indexed userId, uint256 amount); event Withdrawal(address indexed to, bytes32 indexed withdrawalId, uint256 amount); event SignerChanged(address indexed newSigner); event LimitsChanged(uint256 perWithdrawal, uint256 perWalletPerDay, uint256 totalPerDay); event MinDepositChanged(uint256 minDeposit); event OwnerWithdraw(address indexed to, uint256 amount); error InvalidUser(); error AmountTooSmall(); error Expired(); error AlreadyUsed(); error LimitExceeded(); error BadSignature(); error ZeroAddress(); error RenounceDisabled(); constructor(address _token, address _signer, address _owner) Ownable(_owner) { if (_token == address(0) || _signer == address(0)) revert ZeroAddress(); token = IERC20(_token); uint256 u = 10 ** uint256(IERC20Metadata(_token).decimals()); unit = u; signer = _signer; minDeposit = 1 * u; // 1 USDT maxPerWithdrawal = 100 * u; // 100 USDT per withdrawal maxPerWalletPerDay = 500 * u; // 500 USDT per wallet per day maxTotalPerDay = 2000 * u; // 2000 USDT for the whole contract per day emit SignerChanged(_signer); emit LimitsChanged(maxPerWithdrawal, maxPerWalletPerDay, maxTotalPerDay); } // ------------------------------------------------------------------ users function deposit(uint256 userId, uint256 amount) external whenNotPaused nonReentrant { if (userId == 0) revert InvalidUser(); if (amount < minDeposit) revert AmountTooSmall(); uint256 balBefore = token.balanceOf(address(this)); token.safeTransferFrom(msg.sender, address(this), amount); uint256 received = token.balanceOf(address(this)) - balBefore; emit Deposit(msg.sender, userId, received); } function withdraw(bytes32 id, uint256 amount, uint256 deadline, bytes calldata signature) external whenNotPaused nonReentrant { if (block.timestamp > deadline) revert Expired(); if (used[id]) revert AlreadyUsed(); if (amount == 0 || amount > maxPerWithdrawal) revert LimitExceeded(); bytes32 structHash = keccak256( abi.encode(WITHDRAW_TYPEHASH, block.chainid, address(this), msg.sender, id, amount, deadline) ); bytes32 digest = MessageHashUtils.toEthSignedMessageHash(structHash); if (ECDSA.recover(digest, signature) != signer) revert BadSignature(); uint256 day = block.timestamp / 1 days; uint256 walletTotal = walletWithdrawnOnDay[msg.sender][day] + amount; uint256 allTotal = totalWithdrawnOnDay[day] + amount; if (walletTotal > maxPerWalletPerDay || allTotal > maxTotalPerDay) revert LimitExceeded(); used[id] = true; walletWithdrawnOnDay[msg.sender][day] = walletTotal; totalWithdrawnOnDay[day] = allTotal; token.safeTransfer(msg.sender, amount); emit Withdrawal(msg.sender, id, amount); } // ------------------------------------------------------------------ owner function setSigner(address newSigner) external onlyOwner { if (newSigner == address(0)) revert ZeroAddress(); signer = newSigner; emit SignerChanged(newSigner); } function setLimits(uint256 perWithdrawal, uint256 perWalletPerDay, uint256 totalPerDay) external onlyOwner { maxPerWithdrawal = perWithdrawal; maxPerWalletPerDay = perWalletPerDay; maxTotalPerDay = totalPerDay; emit LimitsChanged(perWithdrawal, perWalletPerDay, totalPerDay); } function setMinDeposit(uint256 newMin) external onlyOwner { minDeposit = newMin; emit MinDepositChanged(newMin); } function pause() external onlyOwner { _pause(); } function unpause() external onlyOwner { _unpause(); } /// Move USDT out of the contract (e.g. to a cold wallet). Works even when paused. function ownerWithdraw(address to, uint256 amount) external onlyOwner { if (to == address(0)) revert ZeroAddress(); token.safeTransfer(to, amount); emit OwnerWithdraw(to, amount); } /// Recover any OTHER token that was sent to this contract by mistake. function rescueOtherToken(address otherToken, address to, uint256 amount) external onlyOwner { if (otherToken == address(token) || to == address(0)) revert ZeroAddress(); IERC20(otherToken).safeTransfer(to, amount); } /// Losing the owner would lock the settings forever, so renouncing is disabled. function renounceOwnership() public pure override { revert RenounceDisabled(); } }