-
Notifications
You must be signed in to change notification settings - Fork 18
/
vSwap.sol
192 lines (173 loc) · 6.03 KB
/
vSwap.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.6.6;
library SafeMath {
function add(uint256 x, uint256 y) internal pure returns (uint256 z) {
require((z = x + y) >= x, "ds-math-add-overflow");
}
function sub(uint256 x, uint256 y) internal pure returns (uint256 z) {
require((z = x - y) <= x, "ds-math-sub-underflow");
}
function mul(uint256 x, uint256 y) internal pure returns (uint256 z) {
require(y == 0 || (z = x * y) / y == x, "ds-math-mul-overflow");
}
}
pragma solidity >=0.5.0;
interface IPancakePair {
event Transfer(address indexed from, address indexed to, uint256 value);
function getReserves()
external
view
returns (
uint112 reserve0,
uint112 reserve1,
uint32 blockTimestampLast
);
function swap(
uint256 amount0Out,
uint256 amount1Out,
address to,
bytes calldata data
) external;
}
library PancakeLibrary {
using SafeMath for uint256;
// returns sorted token addresses, used to handle return values from pairs sorted in this order
function sortTokens(address tokenA, address tokenB)
internal
pure
returns (address token0, address token1)
{
require(tokenA != tokenB, "Library Sort: IDENTICAL_ADDRESSES");
(token0, token1) = tokenA < tokenB
? (tokenA, tokenB)
: (tokenB, tokenA);
require(token0 != address(0), "Library Sort: ZERO_ADDRESS");
}
// fetches and sorts the reserves for a pair
function getReserves(
address pairAddress,
address tokenA,
address tokenB
) internal view returns (uint256 reserveA, uint256 reserveB) {
(address token0, ) = sortTokens(tokenA, tokenB);
(uint256 reserve0, uint256 reserve1, ) = IPancakePair(pairAddress)
.getReserves();
(reserveA, reserveB) = tokenA == token0
? (reserve0, reserve1)
: (reserve1, reserve0);
}
// given an input amount of an asset and pair reserves, returns the maximum output amount of the other asset
function getAmountOut(
uint256 amountIn,
uint256 reserveIn,
uint256 reserveOut,
uint256 fee
) internal pure returns (uint256 amountOut) {
require(amountIn > 0, "Library: INSUFFICIENT_INPUT_AMOUNT");
require(
reserveIn > 0 && reserveOut > 0,
"Library: INSUFFICIENT_LIQUIDITY"
);
uint256 tenThousand = 10000;
uint256 amountInWithFee = amountIn.mul(tenThousand.sub(fee));
uint256 numerator = amountInWithFee.mul(reserveOut);
uint256 denominator = reserveIn.mul(10000).add(amountInWithFee);
amountOut = numerator / denominator;
}
// performs chained getAmountOut calculations on any number of pairs
function getAmountsOut(
uint256 amountIn,
address[] memory path,
address[] memory pairPath,
uint256[] memory fee
) internal view returns (uint256[] memory amounts) {
require(path.length >= 2, "Library: INVALID_PATH");
amounts = new uint256[](path.length);
amounts[0] = amountIn;
for (uint256 i; i < path.length - 1; i++) {
(uint256 reserveIn, uint256 reserveOut) = getReserves(
pairPath[i],
path[i],
path[i + 1]
);
amounts[i + 1] = getAmountOut(
amounts[i],
reserveIn,
reserveOut,
fee[i]
);
}
}
}
contract vSwapContract {
address private owner;
modifier onlyOwner() {
require(msg.sender == owner, "vSwap: not authorised: ");
_;
}
modifier ensure(uint256 deadline) {
require(deadline >= block.timestamp, "vSwap: EXPIRED");
_;
}
constructor() {
owner = msg.sender;
}
function safeTransferFrom(
address token,
address from,
address to,
uint256 value
) internal {
// bytes4(keccak256(bytes('transferFrom(address,address,uint256)')));
(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 _swap(
uint256[] memory amounts,
address[] memory path,
address[] memory pairPath,
address _to
) internal virtual {
for (uint256 i; i < pairPath.length; i++) {
(address input, address output) = (path[i], path[i + 1]);
(address token0, ) = PancakeLibrary.sortTokens(input, output);
uint256 amountOut = amounts[i + 1];
(uint256 amount0Out, uint256 amount1Out) = input == token0
? (uint256(0), amountOut)
: (amountOut, uint256(0));
address to = i < pairPath.length - 1 ? pairPath[i + 1] : _to;
IPancakePair(pairPath[i]).swap(
amount0Out,
amount1Out,
to,
new bytes(0)
);
}
}
function vSwap(
uint256 amountIn,
uint256 amountOutMin,
address[] calldata path,
address[] calldata pairPath,
uint256[] calldata fee,
address to,
uint256 deadline
) external virtual ensure(deadline) onlyOwner returns (bool success) {
uint256[] memory amounts = PancakeLibrary.getAmountsOut(
amountIn,
path,
pairPath,
fee
);
success = true;
if (amounts[amounts.length - 1] < amountOutMin) return success;
safeTransferFrom(path[0], msg.sender, pairPath[0], amounts[0]);
_swap(amounts, path, pairPath, to);
return success;
}
}