55 lines
1.7 KiB
Solidity
55 lines
1.7 KiB
Solidity
// SPDX-License-Identifier: MIT
|
|
pragma solidity ^0.8.0;
|
|
|
|
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
|
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
|
|
|
|
contract PaymentContract {
|
|
using SafeERC20 for IERC20;
|
|
|
|
address public owner;
|
|
mapping(address => uint256) public balances;
|
|
|
|
event Deposit(address indexed user, uint256 amount);
|
|
event Withdraw(address indexed user, uint256 amount);
|
|
event Payment(address indexed from, address indexed to, uint256 amount);
|
|
|
|
modifier onlyOwner() {
|
|
require(msg.sender == owner, "Not the contract owner");
|
|
_;
|
|
}
|
|
|
|
constructor() {
|
|
owner = msg.sender;
|
|
}
|
|
|
|
function deposit(address token, uint256 amount) external {
|
|
IERC20(token).safeTransferFrom(msg.sender, address(this), amount);
|
|
balances[msg.sender] += amount;
|
|
emit Deposit(msg.sender, amount);
|
|
}
|
|
|
|
function withdraw(address token, uint256 amount) external {
|
|
require(balances[msg.sender] >= amount, "Insufficient balance");
|
|
balances[msg.sender] -= amount;
|
|
IERC20(token).safeTransfer(msg.sender, amount);
|
|
emit Withdraw(msg.sender, amount);
|
|
}
|
|
|
|
function pay(address token, address to, uint256 amount) external {
|
|
require(balances[msg.sender] >= amount, "Insufficient balance");
|
|
balances[msg.sender] -= amount;
|
|
balances[to] += amount;
|
|
IERC20(token).safeTransfer(to, amount);
|
|
emit Payment(msg.sender, to, amount);
|
|
}
|
|
|
|
function getBalance(address user) external view returns (uint256) {
|
|
return balances[user];
|
|
}
|
|
|
|
function emergencyWithdraw(address token, uint256 amount) external onlyOwner {
|
|
IERC20(token).safeTransfer(owner, amount);
|
|
}
|
|
}
|