This repository was archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathETHSwapAgentImpl.sol
118 lines (94 loc) · 4.2 KB
/
ETHSwapAgentImpl.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
pragma solidity 0.6.4;
import "./interfaces/IERC20Query.sol";
import "openzeppelin-solidity/contracts/proxy/Initializable.sol";
import "openzeppelin-solidity/contracts/GSN/Context.sol";
import "openzeppelin-solidity/contracts/token/ERC20/SafeERC20.sol";
contract ETHSwapAgentImpl is Context, Initializable {
using SafeERC20 for IERC20;
mapping(address => bool) public registeredERC20;
mapping(bytes32 => bool) public filledBSCTx;
address payable public owner;
uint256 public swapFee;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
event SwapPairRegister(address indexed sponsor,address indexed erc20Addr, string name, string symbol, uint8 decimals);
event SwapStarted(address indexed erc20Addr, address indexed fromAddr, uint256 amount, uint256 feeAmount);
event SwapFilled(address indexed erc20Addr, bytes32 indexed bscTxHash, address indexed toAddress, uint256 amount);
constructor() public {
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
require(owner == _msgSender(), "Ownable: caller is not the owner");
_;
}
function initialize(uint256 fee, address payable ownerAddr) public initializer {
swapFee = fee;
owner = ownerAddr;
}
modifier notContract() {
require(!isContract(msg.sender), "contract is not allowed to swap");
require(msg.sender == tx.origin, "no proxy contract is allowed");
_;
}
function isContract(address addr) internal view returns (bool) {
uint size;
assembly { size := extcodesize(addr) }
return size > 0;
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() public onlyOwner {
emit OwnershipTransferred(owner, address(0));
owner = address(0);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address payable newOwner) public onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
/**
* @dev Returns set minimum swap fee from ERC20 to BEP20
*/
function setSwapFee(uint256 fee) onlyOwner external {
swapFee = fee;
}
function registerSwapPairToBSC(address erc20Addr) external returns (bool) {
require(!registeredERC20[erc20Addr], "already registered");
string memory name = IERC20Query(erc20Addr).name();
string memory symbol = IERC20Query(erc20Addr).symbol();
uint8 decimals = IERC20Query(erc20Addr).decimals();
require(bytes(name).length>0, "empty name");
require(bytes(symbol).length>0, "empty symbol");
registeredERC20[erc20Addr] = true;
emit SwapPairRegister(msg.sender, erc20Addr, name, symbol, decimals);
return true;
}
function fillBSC2ETHSwap(bytes32 bscTxHash, address erc20Addr, address toAddress, uint256 amount) onlyOwner external returns (bool) {
require(!filledBSCTx[bscTxHash], "bsc tx filled already");
require(registeredERC20[erc20Addr], "not registered token");
filledBSCTx[bscTxHash] = true;
IERC20(erc20Addr).safeTransfer(toAddress, amount);
emit SwapFilled(erc20Addr, bscTxHash, toAddress, amount);
return true;
}
function swapETH2BSC(address erc20Addr, uint256 amount) payable external notContract returns (bool) {
require(registeredERC20[erc20Addr], "not registered token");
require(msg.value == swapFee, "swap fee not equal");
IERC20(erc20Addr).safeTransferFrom(msg.sender, address(this), amount);
if (msg.value != 0) {
owner.transfer(msg.value);
}
emit SwapStarted(erc20Addr, msg.sender, amount, msg.value);
return true;
}
}