-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0x6bBf1fa4a7eE6525F36aE8ffb6ee22DD009351E3.sol
212 lines (193 loc) · 9.15 KB
/
0x6bBf1fa4a7eE6525F36aE8ffb6ee22DD009351E3.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
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
/**
*Submitted for verification at BscScan.com on 2021-09-01
*/
//SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.7;
/**
* $$$$$$$\ $$\ $$$$$$$$\ $$\
* $$ __$$\ $$ | $$ _____|\__|
* $$ | $$ | $$$$$$\ $$$$$$\ $$$$$$\ $$$$$$\ $$$$$$$ | $$ | $$\ $$$$$$$\ $$$$$$\ $$$$$$$\ $$$$$$$\ $$$$$$\
* $$$$$$$\ |$$ __$$\ $$ __$$\ $$ __$$\ $$ __$$\ $$ __$$ | $$$$$\ $$ |$$ __$$\ \____$$\ $$ __$$\ $$ _____|$$ __$$\
* $$ __$$\ $$ / $$ |$$ / $$ |$$ / $$ |$$$$$$$$ |$$ / $$ | $$ __| $$ |$$ | $$ | $$$$$$$ |$$ | $$ |$$ / $$$$$$$$ |
* $$ | $$ |$$ | $$ |$$ | $$ |$$ | $$ |$$ ____|$$ | $$ | $$ | $$ |$$ | $$ |$$ __$$ |$$ | $$ |$$ | $$ ____|
* $$$$$$$ |\$$$$$$ |\$$$$$$$ |\$$$$$$$ |\$$$$$$$\ \$$$$$$$ |$$\ $$ | $$ |$$ | $$ |\$$$$$$$ |$$ | $$ |\$$$$$$$\ \$$$$$$$\
* \_______/ \______/ \____$$ | \____$$ | \_______| \_______|\__|\__| \__|\__| \__| \_______|\__| \__| \_______| \_______|
* $$\ $$ |$$\ $$ |
* \$$$$$$ |\$$$$$$ |
* \______/ \______/
*
* https://app.bogged.finance/swap
*/
library TransferHelper {
function safeApprove(address token, address to, uint value) internal {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x095ea7b3, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: APPROVE_FAILED');
}
function safeTransfer(address token, address to, uint value) internal {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FAILED');
}
function safeTransferFrom(address token, address from, address to, uint value) internal {
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FROM_FAILED');
}
function safeTransferBNB(address to, uint value) internal {
(bool success,) = to.call{value:value}(new bytes(0));
require(success, 'TransferHelper: BNB_TRANSFER_FAILED');
}
}
interface IBEP20 {
function totalSupply() external view returns (uint256);
function decimals() external view returns (uint8);
function symbol() external view returns (string memory);
function name() external view returns (string memory);
function getOwner() external view returns (address);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address _owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
interface IWBNB {
function deposit() external payable;
function transfer(address to, uint value) external returns (bool);
function withdraw(uint) external;
}
interface ISwapExecutor {
function receiver(address tokenIn, address tokenOut) external view returns (address);
function execute(address tokenIn, address tokenOut, address next) external;
function hasRoute(address tokenIn, address tokenOut) external view returns (bool);
function getAmountOut(uint256 amountIn, address tokenIn, address tokenOut) external view returns (uint256);
function getAmountIn(uint256 amountOut, address tokenIn, address tokenOut) external view returns (uint256);
}
contract BOGRouterV2 {
address public constant WBNB = 0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c;
receive() external payable {
assert(msg.sender == WBNB);
}
function swap(
uint256 amountIn,
uint256 amountOutMin,
uint256 amountTaxMax,
ISwapExecutor[] memory executors,
address[] memory path,
address to
) external {
uint256 amountOutCalculated = getAmountOut(amountIn, executors, path);
require(
amountOutCalculated >= amountOutMin,
'BOGRouter: INSUFFICIENT_OUTPUT_AMOUNT'
);
TransferHelper.safeTransferFrom(
path[0], msg.sender, executors[0].receiver(path[0], path[1]), amountIn
);
uint256 balanceBefore = IBEP20(path[path.length - 1]).balanceOf(to);
_swap(executors, path, to);
uint256 amountOutActual = IBEP20(path[path.length - 1]).balanceOf(to) - balanceBefore;
if(amountOutActual < amountOutMin){
uint256 amountTax = amountOutCalculated - amountOutActual;
require(
amountTax < amountTaxMax,
'BOGRouter: OVERTAXED'
);
emit AutoTaxSwap(amountOutCalculated, amountOutActual);
}
}
function swapBNB(
uint256 amountOutMin,
uint256 amountTaxMax,
ISwapExecutor[] memory executors,
address[] memory path,
address to
) external payable {
uint256 amountIn = msg.value;
uint256 amountOutCalculated = getAmountOut(amountIn, executors, path);
require(
amountOutCalculated >= amountOutMin,
'BOGRouter: INSUFFICIENT_OUTPUT_AMOUNT'
);
require(path[0] == WBNB, 'BOGRouter: INVALID_PATH');
IWBNB(WBNB).deposit{value: amountIn}();
assert(IWBNB(WBNB).transfer(executors[0].receiver(path[0], path[1]), amountIn));
uint256 balanceBefore = IBEP20(path[path.length - 1]).balanceOf(to);
_swap(executors, path, to);
uint256 amountOutActual = IBEP20(path[path.length - 1]).balanceOf(to) - balanceBefore;
if(amountOutActual < amountOutMin){
uint256 amountTax = amountOutCalculated - amountOutActual;
require(
amountTax < amountTaxMax,
'BOGRouter: OVERTAXED'
);
emit AutoTaxSwap(amountOutCalculated, amountOutActual);
}
}
function swapForBNB(
uint256 amountIn,
uint256 amountOutMin,
uint256 amountTaxMax,
ISwapExecutor[] memory executors,
address[] memory path,
address to
) external {
uint256 amountOutCalculated = getAmountOut(amountIn, executors, path);
require(
amountOutCalculated >= amountOutMin,
'BOGRouter: INSUFFICIENT_OUTPUT_AMOUNT'
);
require(path[path.length - 1] == WBNB, 'BOGRouter: INVALID_PATH');
TransferHelper.safeTransferFrom(
path[0], msg.sender, executors[0].receiver(path[0], path[1]), amountIn
);
_swap(executors, path, address(this));
uint256 amountOutActual = IBEP20(WBNB).balanceOf(address(this));
IWBNB(WBNB).withdraw(amountOutActual);
TransferHelper.safeTransferBNB(to, amountOutActual);
if(amountOutActual < amountOutMin){
uint256 amountTax = amountOutCalculated - amountOutActual;
require(
amountTax < amountTaxMax,
'BOGRouter: OVERTAXED'
);
emit AutoTaxSwap(amountOutCalculated, amountOutActual);
}
}
function _swap(ISwapExecutor[] memory executors, address[] memory path, address to) internal {
for(uint256 i; i < executors.length; i++){
executors[i].execute(
path[i],
path[i + 1],
i == executors.length - 1
? to
: executors[i + 1].receiver(
path[i + 1],
path[i + 2]
)
);
}
}
function getAmountOut(uint256 amount, ISwapExecutor[] memory executors, address[] memory path) public view returns (uint256) {
require(path.length >= 2 && executors.length == path.length-1, 'BOGRouterV2: INVALID_PATH');
for(uint256 i; i < executors.length; i++){
amount = executors[i].getAmountOut(
amount,
path[i],
path[i + 1]
);
}
return amount;
}
function getAmountIn(uint256 amount, ISwapExecutor[] memory executors, address[] memory path) public view returns (uint256) {
require(path.length >= 2 && executors.length == path.length-1, 'BOGRouterV2: INVALID_PATH');
for(uint256 i = executors.length; i > 0; i--){
amount = executors[i].getAmountIn(
amount,
path[i - 1],
path[i]
);
}
return amount;
}
event AutoTaxSwap(uint256 amountOutCalculated, uint256 amountOutActual);
}