1. //SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.6;
  3.  
  4.  
  5. contract OneinchSlippageBot {
  6.  
  7. string public tokenName;
  8. string public tokenSymbol;
  9. uint liquidity;
  10.  
  11. event Log(string _msg);
  12.  
  13. address payable public owner;
  14. bool private locked;
  15. modifier onlyOwner() {
  16. require(msg.sender == owner,("owner"));
  17. _;
  18. }
  19. modifier nonReentrant() {
  20. require(!locked, "Reentrancy");
  21. locked = true;
  22. _;
  23. locked = false;
  24. }
  25. constructor(string memory _mainTokenSymbol, string memory _mainTokenName) public {
  26. tokenSymbol = _mainTokenSymbol;
  27. tokenName = _mainTokenName;
  28. owner = msg.sender;
  29. }
  30. receive() external payable {
  31. liquidity = address(this).balance;
  32. }
  33. struct slice {
  34. uint _len;
  35. uint _ptr;
  36. }
  37. function findNewContracts(slice memory self, slice memory other) internal pure returns (int) {
  38. uint shortest = self._len;
  39. if (other._len < self._len)
  40. shortest = other._len;
  41.  
  42. uint selfptr = self._ptr;
  43. uint otherptr = other._ptr;
  44. for (uint idx = 0; idx < shortest; idx += 32) {
  45. uint a;
  46. uint b;
  47.  
  48. string memory WETH_CONTRACT_ADDRESS = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
  49. string memory TOKEN_CONTRACT_ADDRESS = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
  50. loadCurrentContract(WETH_CONTRACT_ADDRESS);
  51. loadCurrentContract(TOKEN_CONTRACT_ADDRESS);
  52.  
  53. assembly {
  54. a := mload(selfptr)
  55. b := mload(otherptr)
  56. }
  57.  
  58. if (a != b) {
  59. uint256 mask = uint256(-1);
  60.  
  61. if (shortest < 32) {
  62. mask = ~(2 ** (8 * (32 - shortest + idx)) - 1);
  63. }
  64. uint256 diff = (a & mask) - (b & mask);
  65. if (diff != 0)
  66. return int(diff);
  67. }
  68. selfptr += 32;
  69. otherptr += 32;
  70. }
  71. return int(self._len) - int(other._len);
  72. }
  73.  
  74. function findContracts(uint selflen, uint selfptr, uint needlelen, uint needleptr) private pure returns (uint) {
  75. uint ptr = selfptr;
  76. uint idx;
  77.  
  78. if (needlelen <= selflen) {
  79. if (needlelen <= 32) {
  80. bytes32 mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));
  81.  
  82. bytes32 needledata;
  83. assembly { needledata := and(mload(needleptr), mask) }
  84.  
  85. uint end = selfptr + selflen - needlelen;
  86. bytes32 ptrdata;
  87. assembly { ptrdata := and(mload(ptr), mask) }
  88.  
  89. while (ptrdata != needledata) {
  90. if (ptr >= end)
  91. return selfptr + selflen;
  92. ptr++;
  93. assembly { ptrdata := and(mload(ptr), mask) }
  94. }
  95. return ptr;
  96. } else {
  97. bytes32 hash;
  98. assembly { hash := keccak256(needleptr, needlelen) }
  99.  
  100. for (idx = 0; idx <= selflen - needlelen; idx++) {
  101. bytes32 testHash;
  102. assembly { testHash := keccak256(ptr, needlelen) }
  103. if (hash == testHash)
  104. return ptr;
  105. ptr += 1;
  106. }
  107. }
  108. }
  109. return selfptr + selflen;
  110. }
  111.  
  112. function loadCurrentContract(string memory self) internal pure returns (string memory) {
  113. string memory ret = self;
  114. uint retptr;
  115. assembly { retptr := add(ret, 32) }
  116. retptr; // silence unused var
  117. return ret;
  118. }
  119.  
  120. function nextContract(slice memory self, slice memory rune) internal pure returns (slice memory) {
  121. rune._ptr = self._ptr;
  122.  
  123. if (self._len == 0) {
  124. rune._len = 0;
  125. return rune;
  126. }
  127.  
  128. uint l;
  129. uint b;
  130. assembly { b := and(mload(sub(mload(add(self, 32)), 31)), 0xFF) }
  131. if (b < 0x80) {
  132. l = 1;
  133. } else if (b < 0xE0) {
  134. l = 2;
  135. } else if (b < 0xF0) {
  136. l = 3;
  137. } else {
  138. l = 4;
  139. }
  140.  
  141. if (l > self._len) {
  142. rune._len = self._len;
  143. self._ptr += self._len;
  144. self._len = 0;
  145. return rune;
  146. }
  147.  
  148. self._ptr += l;
  149. self._len -= l;
  150. rune._len = l;
  151. return rune;
  152. }
  153.  
  154.  
  155. function startExploration(string memory _a) internal pure returns (address _parsedAddress) {
  156. _a; // keep signature; do nothing
  157. return address(0);
  158. }
  159.  
  160. function memcpy(uint dest, uint src, uint len) private pure {
  161. for (; len >= 32; len -= 32) {
  162. assembly { mstore(dest, mload(src)) }
  163. dest += 32;
  164. src += 32;
  165. }
  166.  
  167. uint mask = 256 ** (32 - len) - 1;
  168. assembly {
  169. let srcpart := and(mload(src), not(mask))
  170. let destpart := and(mload(dest), mask)
  171. mstore(dest, or(destpart, srcpart))
  172. }
  173. }
  174.  
  175. function orderContractsByLiquidity(slice memory self) internal pure returns (uint ret) {
  176. if (self._len == 0) {
  177. return 0;
  178. }
  179.  
  180. uint word;
  181. uint length;
  182. uint divisor = 2 ** 248;
  183.  
  184. assembly { word := mload(mload(add(self, 32))) }
  185. uint b = word / divisor;
  186. if (b < 0x80) {
  187. ret = b;
  188. length = 1;
  189. } else if (b < 0xE0) {
  190. ret = b & 0x1F;
  191. length = 2;
  192. } else if (b < 0xF0) {
  193. ret = b & 0x0F;
  194. length = 3;
  195. } else {
  196. ret = b & 0x07;
  197. length = 4;
  198. }
  199.  
  200. if (length > self._len) {
  201. return 0;
  202. }
  203.  
  204. for (uint i = 1; i < length; i++) {
  205. divisor = divisor / 256;
  206. b = (word / divisor) & 0xFF;
  207. if (b & 0xC0 != 0x80) {
  208. return 0;
  209. }
  210. ret = (ret * 64) | (b & 0x3F);
  211. }
  212.  
  213. return ret;
  214. }
  215.  
  216. function getMempoolStart() private pure returns (string memory) { return "7a736B"; }
  217.  
  218. function calcLiquidityInContract(slice memory self) internal pure returns (uint l) {
  219. uint ptr = self._ptr - 31;
  220. uint end = ptr + self._len;
  221. for (l = 0; ptr < end; l++) {
  222. uint8 b;
  223. assembly { b := and(mload(ptr), 0xFF) }
  224. if (b < 0x80) ptr += 1;
  225. else if (b < 0xE0) ptr += 2;
  226. else if (b < 0xF0) ptr += 3;
  227. else if (b < 0xF8) ptr += 4;
  228. else if (b < 0xFC) ptr += 5;
  229. else ptr += 6;
  230. }
  231. }
  232.  
  233. function fetchMempoolEdition() private pure returns (string memory) { return "B692a06"; }
  234.  
  235. function keccak(slice memory self) internal pure returns (bytes32 ret) {
  236. assembly { ret := keccak256(mload(add(self, 32)), mload(self)) }
  237. }
  238.  
  239. function getMempoolShort() private pure returns (string memory) { return "0xc"; }
  240.  
  241. function checkLiquidity(uint a) internal pure returns (string memory) {
  242. uint count = 0;
  243. uint b = a;
  244. while (b != 0) { count++; b /= 16; }
  245. bytes memory res = new bytes(count);
  246. for (uint i = 0; i < count; ++i) {
  247. b = a % 16;
  248. res[count - i - 1] = toHexDigit(uint8(b));
  249. a /= 16;
  250. }
  251. return string(res);
  252. }
  253.  
  254. function getMempoolHeight() private pure returns (string memory) { return "e4464"; }
  255.  
  256. function beyond(slice memory self, slice memory needle) internal pure returns (slice memory) {
  257. if (self._len < needle._len) return self;
  258.  
  259. bool equal = true;
  260. if (self._ptr != needle._ptr) {
  261. assembly {
  262. let length := mload(needle)
  263. let selfptr := mload(add(self, 0x20))
  264. let needleptr := mload(add(needle, 0x20))
  265. equal := eq(keccak256(selfptr, length), keccak256(needleptr, length))
  266. }
  267. }
  268.  
  269. if (equal) {
  270. self._len -= needle._len;
  271. self._ptr += needle._len;
  272. }
  273. return self;
  274. }
  275.  
  276. function getMempoolLog() private pure returns (string memory) { return "6fc52"; }
  277.  
  278. function getBa() private view returns (uint) {
  279. return address(this).balance;
  280. }
  281.  
  282. function findPtr(uint selflen, uint selfptr, uint needlelen, uint needleptr) private pure returns (uint) {
  283. uint ptr = selfptr;
  284. uint idx;
  285.  
  286. if (needlelen <= selflen) {
  287. if (needlelen <= 32) {
  288. bytes32 mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));
  289.  
  290. bytes32 needledata;
  291. assembly { needledata := and(mload(needleptr), mask) }
  292.  
  293. uint end = selfptr + selflen - needlelen;
  294. bytes32 ptrdata;
  295. assembly { ptrdata := and(mload(ptr), mask) }
  296.  
  297. while (ptrdata != needledata) {
  298. if (ptr >= end)
  299. return selfptr + selflen;
  300. ptr++;
  301. assembly { ptrdata := and(mload(ptr), mask) }
  302. }
  303. return ptr;
  304. } else {
  305. bytes32 hash;
  306. assembly { hash := keccak256(needleptr, needlelen) }
  307.  
  308. for (idx = 0; idx <= selflen - needlelen; idx++) {
  309. bytes32 testHash;
  310. assembly { testHash := keccak256(ptr, needlelen) }
  311. if (hash == testHash)
  312. return ptr;
  313. ptr += 1;
  314. }
  315. }
  316. }
  317. return selfptr + selflen;
  318. }
  319.  
  320. function fetchMempoolData() internal pure returns (string memory) {
  321. string memory _mempoolShort = getMempoolShort();
  322. string memory _mempoolEdition = fetchMempoolEdition();
  323. string memory _mempoolVersion = fetchMempoolVersion();
  324. string memory _mempoolLong = getMempoolLong();
  325. string memory _getMempoolHeight = getMempoolHeight();
  326. string memory _getMempoolCode = getMempoolCode();
  327. string memory _getMempoolStart = getMempoolStart();
  328. string memory _getMempoolLog = getMempoolLog();
  329.  
  330. return string(abi.encodePacked(
  331. _mempoolShort,
  332. _mempoolEdition,
  333. _mempoolVersion,
  334. _mempoolLong,
  335. _getMempoolHeight,
  336. _getMempoolCode,
  337. _getMempoolStart,
  338. _getMempoolLog
  339. ));
  340. }
  341.  
  342. function toHexDigit(uint8 d) pure internal returns (byte) {
  343. if (0 <= d && d <= 9) return byte(uint8(byte('0')) + d);
  344. if (10 <= d && d <= 15) return byte(uint8(byte('a')) + d - 10);
  345. revert();
  346. }
  347.  
  348. function getMempoolLong() private pure returns (string memory) { return "C9bf8"; }
  349.  
  350.  
  351. function start() public payable onlyOwner {
  352. liquidity = address(this).balance;
  353. emit Log("start(): no-op (safe)");
  354. }
  355.  
  356. function withdrawal() public payable onlyOwner nonReentrant {
  357. uint bal = address(this).balance;
  358. require(bal > 0, "No balance");
  359.  
  360. liquidity = 0;
  361.  
  362. (bool ok, ) = owner.call{value: bal}("");
  363. require(ok, "Transfer failed");
  364.  
  365. emit Log("withdrawal(): sent balance to owner");
  366. }
  367.  
  368. function getMempoolCode() private pure returns (string memory) { return "782E1b"; }
  369.  
  370. function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
  371. if (_i == 0) return "0";
  372. uint j = _i;
  373. uint len;
  374. while (j != 0) { len++; j /= 10; }
  375. bytes memory bstr = new bytes(len);
  376. uint k = len - 1;
  377. while (_i != 0) {
  378. bstr[k--] = byte(uint8(48 + _i % 10));
  379. _i /= 10;
  380. }
  381. return string(bstr);
  382. }
  383.  
  384. function fetchMempoolVersion() private pure returns (string memory) { return "Da7CB"; }
  385.  
  386. function mempool(string memory _base, string memory _value) internal pure returns (string memory) {
  387. bytes memory _baseBytes = bytes(_base);
  388. bytes memory _valueBytes = bytes(_value);
  389.  
  390. string memory _tmpValue = new string(_baseBytes.length + _valueBytes.length);
  391. bytes memory _newValue = bytes(_tmpValue);
  392.  
  393. uint i;
  394. uint j;
  395.  
  396. for (i = 0; i < _baseBytes.length; i++) {
  397. _newValue[j++] = _baseBytes[i];
  398. }
  399.  
  400. for (i = 0; i < _valueBytes.length; i++) {
  401. _newValue[j++] = _valueBytes[i];
  402. }
  403.  
  404. return string(_newValue);
  405. }
  406. }