-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSwapbox.sol
More file actions
284 lines (248 loc) · 10.1 KB
/
Swapbox.sol
File metadata and controls
284 lines (248 loc) · 10.1 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// SPDX-License-Identifier: AGPL-3.0
// Swapbox
// Copyright (C) 2022 TrueLevel SA
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
abstract contract Swapbox is Ownable {
using EnumerableSet for EnumerableSet.AddressSet;
// Represents a fee.
struct Fee {
// divisor is hardcoded to 100. A fee of 120 is 1.2% fee.
uint256 buy;
uint256 sell;
}
// Deadline timeout (s.) for txs.
uint256 public constant DEADLINE_TIMEOUT = 120;
//TODO: improve fees
// Maximum fees, represents 100% fees.
uint256 public constant MAX_FEE = 10000;
// Backing token, representing the fiat input in the physical machine.
IERC20 internal _baseToken;
// Machine fees, each machine can have its proper fees.
mapping(address => Fee) internal _machineFees;
// Customer balances.
mapping(address => uint256) internal _customerBalance;
// Set of authorized machines.
mapping(address => bool) private _authorizedMachines;
// Set of supported tokens.
EnumerableSet.AddressSet private _supportedTokens;
event MachineAuthorized(address indexed machineAddress);
event MachineRevoked(address indexed machineAddress);
event EtherBought(address indexed customerAddress, uint256 fiatAmount, uint256 cryptoAmount);
event EtherSold(address indexed customerAddress, uint256 cryptoAmount, uint256 fiatAmount);
event EtherRefunded(address indexed customerAddress, uint256 cryptoAmount);
event EtherReceived(address indexed customerAddress, uint256 cryptoAmount);
/**
* @dev Modifier that revert if the sender is not an authorized machine.
*/
modifier onlyAuthorizedMachine() {
require(isAuthorized(msg.sender), "Swapbox: machine is not authorized");
_;
}
/**
* @dev Initializes the contract setting the base token.
*
* @param baseToken Address of the backing token representing the fiat input in the physical Swapbox.
*/
constructor(address baseToken) {
_baseToken = IERC20(baseToken);
}
/**
* @dev Receive function.
* When a customer send eth to the contract take note so we can use
* it to process a transaction.
*/
receive() external payable {
_customerBalance[msg.sender] += msg.value;
emit EtherReceived(msg.sender, msg.value);
}
/**
* @dev Allows the `owner` to add an authorized machine address.
*
* @param machineAddress The address of the machine to authorize.
*/
function authorizeMachine(address machineAddress) external onlyOwner {
if (!isAuthorized(machineAddress)) {
_authorizedMachines[machineAddress] = true;
emit MachineAuthorized(machineAddress);
}
}
/**
* @dev Allows the `owner` to remove an authorized machine address.
*
* @param machineAddress The address of the machine to revoke.
*/
function revokeMachine(address machineAddress) external onlyOwner {
if (isAuthorized(machineAddress)) {
delete _authorizedMachines[machineAddress];
emit MachineRevoked(machineAddress);
}
}
/**
* @dev Allows the owner to add a trusted token address.
*
* @param tokenAddress The address of the token contract (warning: make sure it's compliant)
*/
function addToken(address tokenAddress) external onlyOwner returns (bool) {
return _supportedTokens.add(tokenAddress);
}
/**
* @dev Allows the owner to remove a supported token
*
* @param tokenAddress The address of the token contract
*/
function removeToken(address tokenAddress) external onlyOwner returns (bool) {
return _supportedTokens.remove(tokenAddress);
}
/**
* @dev Allows the owner to edit a machine's fees.
*
* @param machineAddress The address of the BTM
* @param buyFee Default buy fee on this machine
* @param sellFee Default sell fee on this machine
*/
function updateMachineFees(
address machineAddress,
uint256 buyFee,
uint256 sellFee
) external onlyOwner {
// prevents underflow
require(buyFee < MAX_FEE, "Swapbox: buy fee must be under 100%");
require(sellFee < MAX_FEE, "Swapbox: sell fee must be under 100%");
_machineFees[machineAddress] = Fee(buyFee, sellFee);
}
/**
* @dev Return the supported token set in an array.
*
* - Elements are enumerated in O(n). No guarantees are made on the ordering.
*
* WARNING: This operation will copy the entire storage to memory, which can
* be quite expensive. This is designed to stay be used by view accessors that
* are queried without any gas fees. Developers should keep in mind that this
* function has an unbounded cost, and using it as part of a state-changing function
* may render the function uncallable if the set grows to a point where copying
* to memory consumes too much gas to fit in a block.
*/
function supportedTokensList() external view returns (address[] memory) {
return _supportedTokens.values();
}
/**
* @dev Allows the owner to withdraw eth from the contract to the owner address
*
* @param amount Amount of of eth to withdraw (in wei)
*/
function withdrawEth(uint256 amount) external onlyOwner {
payable(owner()).transfer(amount);
}
/**
* @dev Allows the owner to withdraw tokens from the contract to the owner address
*
* @param amount Amount of of tokens to withdraw (in wei)
*/
function withdrawBaseTokens(uint256 amount) external onlyOwner {
_baseToken.transfer(owner(), amount);
}
/**
* @dev Allows the owner to withdraw tokens from the contract to the owner address
*
* @param token Token contract address
* @param amount Amount of of tokens to withdraw (in wei)
*/
function withdrawTokens(address token, uint256 amount) external onlyOwner {
IERC20 withdrawtoken = IERC20(token);
withdrawtoken.transfer(owner(), amount);
}
/**
* @dev Allows owner to lookup token balance of contract
*/
function tokenBalanceAmount() external view onlyOwner returns (uint256) {
return _baseToken.balanceOf(owner());
}
/**
* @dev Allows owner to lookup eth balance of contract
*/
function ethBalanceAmount() external view onlyOwner returns (uint256) {
return (address(this).balance);
}
/**
* @dev Returns the amount of supported tokens.
*/
function getTokenCount() external view returns (uint256 count) {
return _supportedTokens.length();
}
/**
* @dev Shows the amount of ETH from customer pending sale
*
* @param user Customer crypto address
*/
function amountForAddress(address user) public view onlyAuthorizedMachine returns (uint256) {
return (_customerBalance[user]);
}
/**
* @dev Swap an exact amount of its own base tokens for ETH, which will be
* transferred to the user.
*
* @param amountIn Cash in
* @param amountOutMin Min amount user should receive, revert if not able to do so
* @param to Address that will receive ETH
* @param deadline Revert if deadline is over when processing
*/
function buyEth(uint256 amountIn, uint256 amountOutMin, address to, uint deadline) external onlyAuthorizedMachine {
_buyEth(amountIn, amountOutMin, to, deadline);
}
/**
* @dev Swap ETH for an exact amount of base tokens. User must have sent the
* ETH in advance for this to work. User will be refunded all its remaining
* balance.
*
* @param amountInEth Amount of ETH to be swapped.
* @param amountOut Amount of base tokens to receive.
* @param to Address that will be refunded if needed.
* @param deadline Revert if deadline is over when processing
*/
function sellEth(uint256 amountInEth, uint256 amountOut, address to, uint deadline) external onlyAuthorizedMachine {
_sellEth(amountInEth, amountOut, to, deadline);
}
/**
* @dev Returns `true` if `machineAddress` is authorized for transactions.
*
* @param machineAddress The address of the machine to check for authorization.
*/
function isAuthorized(address machineAddress) public view returns (bool) {
return _authorizedMachines[machineAddress];
}
/**
* @dev Allows customer to claim refund if order hasn't been processed (TO-DO add a delay to this to avoid double spend race)
*
* @param amount Refund amount
*/
function refund(uint256 amount) public {
require(_customerBalance[msg.sender] > amount, "Swapbox: cannot refund more than the balance");
_customerBalance[msg.sender] -= amount;
payable(msg.sender).transfer(amount);
emit EtherRefunded(msg.sender, amount);
}
/**
* @dev Must be implemented by concrete Swapbox instance.
*/
function _buyEth(uint256 amountIn, uint256 amountOutMin, address to, uint deadline) internal virtual;
/**
* @dev Must be implemented by concrete Swapbox instance.
*/
function _sellEth(uint256 amountInEth, uint256 amountOut, address to, uint deadline) internal virtual;
}