2021-08-11 17:04:24 +02:00
|
|
|
//SPDX-License-Identifier: MIT
|
|
|
|
pragma solidity ^0.7.0;
|
|
|
|
|
|
|
|
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
|
|
|
import "@openzeppelin/contracts/access/Ownable.sol";
|
|
|
|
import "@openzeppelin/contracts/math/SafeMath.sol";
|
|
|
|
|
2021-08-12 19:17:47 +02:00
|
|
|
interface IENS {
|
|
|
|
function owner(bytes32 node) external view returns (address);
|
|
|
|
}
|
|
|
|
|
2021-08-11 17:04:24 +02:00
|
|
|
contract RelayerRegistry is Ownable {
|
|
|
|
using SafeMath for uint256;
|
|
|
|
|
2021-08-12 19:17:47 +02:00
|
|
|
IERC20 public constant TORN = IERC20(0x77777FeDdddFfC19Ff86DB637967013e6C6A116C);
|
|
|
|
IENS public constant ENS = IENS(0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e);
|
|
|
|
address public immutable withdrawalProxy;
|
|
|
|
|
2021-08-11 17:04:24 +02:00
|
|
|
mapping(address => Relayer) public relayers;
|
|
|
|
uint256 public txFee;
|
2021-08-12 19:17:47 +02:00
|
|
|
uint256 public minStake;
|
|
|
|
uint256 public totalTornBurned;
|
|
|
|
|
|
|
|
event Register(bytes32 ensHash, address relayer);
|
|
|
|
event Stake(address indexed relayer, uint256 stake);
|
|
|
|
event Kick(address indexed relayer, bool confiscation);
|
|
|
|
event NewMinStake(uint256 stake);
|
|
|
|
event NewTxFee(uint256 fee);
|
|
|
|
event Transaction(address indexed _relayer, uint _fee);
|
|
|
|
|
|
|
|
|
|
|
|
modifier onlyWithdrawalProxy {
|
|
|
|
require(msg.sender == withdrawalProxy, "only withdrawal proxy");
|
|
|
|
_;
|
|
|
|
}
|
2021-08-11 17:04:24 +02:00
|
|
|
|
|
|
|
struct Relayer {
|
|
|
|
uint256 balance;
|
2021-08-12 19:17:47 +02:00
|
|
|
bytes32 ensHash;
|
|
|
|
mapping(address => bool) addresses;
|
2021-08-11 17:04:24 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 19:17:47 +02:00
|
|
|
constructor (address _withdrawalProxy, uint256 _txFee, uint256 _minStake) {
|
|
|
|
withdrawalProxy = _withdrawalProxy;
|
|
|
|
txFee = _txFee;
|
|
|
|
minStake = _minStake;
|
2021-08-11 17:04:24 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 19:17:47 +02:00
|
|
|
function register(uint _stake, bytes32 _ensHash) external {
|
|
|
|
require(msg.sender == ENS.owner(_ensHash), "only owner of the ENS name");
|
|
|
|
Relayer storage relayer = relayers[msg.sender];
|
|
|
|
require(relayer.ensHash == bytes32(0) && _stake >= minStake);
|
|
|
|
|
|
|
|
_updateStake(_stake, relayer);
|
|
|
|
|
|
|
|
relayer.addresses[msg.sender] = true;
|
|
|
|
relayer.ensHash = _ensHash;
|
|
|
|
emit Register(_ensHash, msg.sender);
|
2021-08-11 17:04:24 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 19:17:47 +02:00
|
|
|
function stake(uint _stake) external {
|
2021-08-11 17:04:24 +02:00
|
|
|
Relayer storage relayer = relayers[msg.sender];
|
2021-08-12 19:17:47 +02:00
|
|
|
require(relayer.ensHash != bytes32(0), "Only registered relayers can top up balance");
|
|
|
|
_updateStake(_stake, relayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function transaction(address _sender, address _feeReceiver) onlyWithdrawalProxy external {
|
|
|
|
Relayer storage relayer = relayers[_feeReceiver];
|
|
|
|
if(relayer.ensHash != bytes32(0)) {
|
|
|
|
require(relayer.addresses[_sender], "only registered relayer can send");
|
|
|
|
uint256 fee = relayer.balance > txFee ? txFee : relayer.balance;
|
|
|
|
relayer.balance -= fee;
|
|
|
|
totalTornBurned += fee;
|
|
|
|
emit Transaction(_feeReceiver, fee);
|
|
|
|
}
|
2021-08-11 17:04:24 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 19:17:47 +02:00
|
|
|
function setTxFee(uint256 _txFee) external onlyOwner {
|
2021-08-11 17:04:24 +02:00
|
|
|
txFee = _txFee;
|
2021-08-12 19:17:47 +02:00
|
|
|
emit NewTxFee(_txFee);
|
2021-08-11 17:04:24 +02:00
|
|
|
}
|
|
|
|
|
2021-08-12 19:17:47 +02:00
|
|
|
function setMinStake(uint256 _stake) external onlyOwner {
|
|
|
|
minStake = _stake;
|
|
|
|
emit NewMinStake(_stake);
|
|
|
|
}
|
|
|
|
|
|
|
|
function kick(address _relayer, bool confiscateStake) external onlyOwner {
|
2021-08-11 17:04:24 +02:00
|
|
|
Relayer storage relayer = relayers[_relayer];
|
2021-08-12 19:17:47 +02:00
|
|
|
TORN.transfer(confiscateStake ? msg.sender : _relayer, relayer.balance);
|
2021-08-11 17:04:24 +02:00
|
|
|
relayer.balance = 0;
|
2021-08-12 19:17:47 +02:00
|
|
|
emit Kick(_relayer, confiscateStake);
|
|
|
|
}
|
|
|
|
|
|
|
|
function _updateStake(uint _stake, Relayer storage relayer) private {
|
|
|
|
TORN.transferFrom(msg.sender, address(this), _stake);
|
|
|
|
relayer.balance = relayer.balance.add(_stake);
|
|
|
|
emit Stake(msg.sender, _stake);
|
2021-08-11 17:04:24 +02:00
|
|
|
}
|
|
|
|
}
|