1. /**
    2. *Submitted for verification at Etherscan.io on 2020-12-18
    3. */
    4. /**
    5. * https://tornado.cash
    6. *
    7. * d888888P dP a88888b. dP
    8. * 88 88 d8' `88 88
    9. * 88 .d8888b. 88d888b. 88d888b. .d8888b. .d888b88 .d8888b. 88 .d8888b. .d8888b. 88d888b.
    10. * 88 88' `88 88' `88 88' `88 88' `88 88' `88 88' `88 88 88' `88 Y8ooooo. 88' `88
    11. * 88 88. .88 88 88 88 88. .88 88. .88 88. .88 dP Y8. .88 88. .88 88 88 88
    12. * dP `88888P' dP dP dP `88888P8 `88888P8 `88888P' 88 Y88888P' `88888P8 `88888P' dP dP
    13. * ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
    14. */
    15. // File: @openzeppelin/contracts/proxy/Proxy.sol
    16. // SPDX-License-Identifier: MIT
    17. pragma solidity ^0.6.0;
    18. /**
    19. * @dev This abstract contract provides a fallback function that delegates all calls to another contract using the EVM
    20. * instruction `delegatecall`. We refer to the second contract as the _implementation_ behind the proxy, and it has to
    21. * be specified by overriding the virtual {_implementation} function.
    22. *
    23. * Additionally, delegation to the implementation can be triggered manually through the {_fallback} function, or to a
    24. * different contract through the {_delegate} function.
    25. *
    26. * The success and return data of the delegated call will be returned back to the caller of the proxy.
    27. */
    28. abstract contract Proxy {
    29. /**
    30. * @dev Delegates the current call to `implementation`.
    31. *
    32. * This function does not return to its internall call site, it will return directly to the external caller.
    33. */
    34. function _delegate(address implementation) internal {
    35. // solhint-disable-next-line no-inline-assembly
    36. assembly {
    37. // Copy msg.data. We take full control of memory in this inline assembly
    38. // block because it will not return to Solidity code. We overwrite the
    39. // Solidity scratch pad at memory position 0.
    40. calldatacopy(0, 0, calldatasize())
    41. // Call the implementation.
    42. // out and outsize are 0 because we don't know the size yet.
    43. let result := delegatecall(gas(), implementation, 0, calldatasize(), 0, 0)
    44. // Copy the returned data.
    45. returndatacopy(0, 0, returndatasize())
    46. switch result
    47. // delegatecall returns 0 on error.
    48. case 0 { revert(0, returndatasize()) }
    49. default { return(0, returndatasize()) }
    50. }
    51. }
    52. /**
    53. * @dev This is a virtual function that should be overriden so it returns the address to which the fallback function
    54. * and {_fallback} should delegate.
    55. */
    56. function _implementation() internal virtual view returns (address);
    57. /**
    58. * @dev Delegates the current call to the address returned by `_implementation()`.
    59. *
    60. * This function does not return to its internall call site, it will return directly to the external caller.
    61. */
    62. function _fallback() internal {
    63. _beforeFallback();
    64. _delegate(_implementation());
    65. }
    66. /**
    67. * @dev Fallback function that delegates calls to the address returned by `_implementation()`. Will run if no other
    68. * function in the contract matches the call data.
    69. */
    70. fallback () payable external {
    71. _fallback();
    72. }
    73. /**
    74. * @dev Fallback function that delegates calls to the address returned by `_implementation()`. Will run if call data
    75. * is empty.
    76. */
    77. receive () payable external {
    78. _fallback();
    79. }
    80. /**
    81. * @dev Hook that is called before falling back to the implementation. Can happen as part of a manual `_fallback`
    82. * call, or as part of the Solidity `fallback` or `receive` functions.
    83. *
    84. * If overriden should call `super._beforeFallback()`.
    85. */
    86. function _beforeFallback() internal virtual {
    87. }
    88. }
    89. // File: @openzeppelin/contracts/utils/Address.sol
    90. pragma solidity ^0.6.2;
    91. /**
    92. * @dev Collection of functions related to the address type
    93. */
    94. library Address {
    95. /**
    96. * @dev Returns true if `account` is a contract.
    97. *
    98. * [IMPORTANT]
    99. * ====
    100. * It is unsafe to assume that an address for which this function returns
    101. * false is an externally-owned account (EOA) and not a contract.
    102. *
    103. * Among others, `isContract` will return false for the following
    104. * types of addresses:
    105. *
    106. * - an externally-owned account
    107. * - a contract in construction
    108. * - an address where a contract will be created
    109. * - an address where a contract lived, but was destroyed
    110. * ====
    111. */
    112. function isContract(address account) internal view returns (bool) {
    113. // This method relies in extcodesize, which returns 0 for contracts in
    114. // construction, since the code is only stored at the end of the
    115. // constructor execution.
    116. uint256 size;
    117. // solhint-disable-next-line no-inline-assembly
    118. assembly { size := extcodesize(account) }
    119. return size > 0;
    120. }
    121. /**
    122. * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
    123. * `recipient`, forwarding all available gas and reverting on errors.
    124. *
    125. * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
    126. * of certain opcodes, possibly making contracts go over the 2300 gas limit
    127. * imposed by `transfer`, making them unable to receive funds via
    128. * `transfer`. {sendValue} removes this limitation.
    129. *
    130. * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
    131. *
    132. * IMPORTANT: because control is transferred to `recipient`, care must be
    133. * taken to not create reentrancy vulnerabilities. Consider using
    134. * {ReentrancyGuard} or the
    135. * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
    136. */
    137. function sendValue(address payable recipient, uint256 amount) internal {
    138. require(address(this).balance >= amount, "Address: insufficient balance");
    139. // solhint-disable-next-line avoid-low-level-calls, avoid-call-value
    140. (bool success, ) = recipient.call{ value: amount }("");
    141. require(success, "Address: unable to send value, recipient may have reverted");
    142. }
    143. /**
    144. * @dev Performs a Solidity function call using a low level `call`. A
    145. * plain`call` is an unsafe replacement for a function call: use this
    146. * function instead.
    147. *
    148. * If `target` reverts with a revert reason, it is bubbled up by this
    149. * function (like regular Solidity function calls).
    150. *
    151. * Returns the raw returned data. To convert to the expected return value,
    152. * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
    153. *
    154. * Requirements:
    155. *
    156. * - `target` must be a contract.
    157. * - calling `target` with `data` must not revert.
    158. *
    159. * _Available since v3.1._
    160. */
    161. function functionCall(address target, bytes memory data) internal returns (bytes memory) {
    162. return functionCall(target, data, "Address: low-level call failed");
    163. }
    164. /**
    165. * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
    166. * `errorMessage` as a fallback revert reason when `target` reverts.
    167. *
    168. * _Available since v3.1._
    169. */
    170. function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
    171. return _functionCallWithValue(target, data, 0, errorMessage);
    172. }
    173. /**
    174. * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
    175. * but also transferring `value` wei to `target`.
    176. *
    177. * Requirements:
    178. *
    179. * - the calling contract must have an ETH balance of at least `value`.
    180. * - the called Solidity function must be `payable`.
    181. *
    182. * _Available since v3.1._
    183. */
    184. function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
    185. return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
    186. }
    187. /**
    188. * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
    189. * with `errorMessage` as a fallback revert reason when `target` reverts.
    190. *
    191. * _Available since v3.1._
    192. */
    193. function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
    194. require(address(this).balance >= value, "Address: insufficient balance for call");
    195. return _functionCallWithValue(target, data, value, errorMessage);
    196. }
    197. function _functionCallWithValue(address target, bytes memory data, uint256 weiValue, string memory errorMessage) private returns (bytes memory) {
    198. require(isContract(target), "Address: call to non-contract");
    199. // solhint-disable-next-line avoid-low-level-calls
    200. (bool success, bytes memory returndata) = target.call{ value: weiValue }(data);
    201. if (success) {
    202. return returndata;
    203. } else {
    204. // Look for revert reason and bubble it up if present
    205. if (returndata.length > 0) {
    206. // The easiest way to bubble the revert reason is using memory via assembly
    207. // solhint-disable-next-line no-inline-assembly
    208. assembly {
    209. let returndata_size := mload(returndata)
    210. revert(add(32, returndata), returndata_size)
    211. }
    212. } else {
    213. revert(errorMessage);
    214. }
    215. }
    216. }
    217. }
    218. // File: @openzeppelin/contracts/proxy/UpgradeableProxy.sol
    219. pragma solidity ^0.6.0;
    220. /**
    221. * @dev This contract implements an upgradeable proxy. It is upgradeable because calls are delegated to an
    222. * implementation address that can be changed. This address is stored in storage in the location specified by
    223. * https://eips.ethereum.org/EIPS/eip-1967[EIP1967], so that it doesn't conflict with the storage layout of the
    224. * implementation behind the proxy.
    225. *
    226. * Upgradeability is only provided internally through {_upgradeTo}. For an externally upgradeable proxy see
    227. * {TransparentUpgradeableProxy}.
    228. */
    229. contract UpgradeableProxy is Proxy {
    230. /**
    231. * @dev Initializes the upgradeable proxy with an initial implementation specified by `_logic`.
    232. *
    233. * If `_data` is nonempty, it's used as data in a delegate call to `_logic`. This will typically be an encoded
    234. * function call, and allows initializating the storage of the proxy like a Solidity constructor.
    235. */
    236. constructor(address _logic, bytes memory _data) public payable {
    237. assert(_IMPLEMENTATION_SLOT == bytes32(uint256(keccak256("eip1967.proxy.implementation")) - 1));
    238. _setImplementation(_logic);
    239. if(_data.length > 0) {
    240. // solhint-disable-next-line avoid-low-level-calls
    241. (bool success,) = _logic.delegatecall(_data);
    242. require(success);
    243. }
    244. }
    245. /**
    246. * @dev Emitted when the implementation is upgraded.
    247. */
    248. event Upgraded(address indexed implementation);
    249. /**
    250. * @dev Storage slot with the address of the current implementation.
    251. * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is
    252. * validated in the constructor.
    253. */
    254. bytes32 private constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
    255. /**
    256. * @dev Returns the current implementation address.
    257. */
    258. function _implementation() internal override view returns (address impl) {
    259. bytes32 slot = _IMPLEMENTATION_SLOT;
    260. // solhint-disable-next-line no-inline-assembly
    261. assembly {
    262. impl := sload(slot)
    263. }
    264. }
    265. /**
    266. * @dev Upgrades the proxy to a new implementation.
    267. *
    268. * Emits an {Upgraded} event.
    269. */
    270. function _upgradeTo(address newImplementation) internal {
    271. _setImplementation(newImplementation);
    272. emit Upgraded(newImplementation);
    273. }
    274. /**
    275. * @dev Stores a new address in the EIP1967 implementation slot.
    276. */
    277. function _setImplementation(address newImplementation) private {
    278. require(Address.isContract(newImplementation), "UpgradeableProxy: new implementation is not a contract");
    279. bytes32 slot = _IMPLEMENTATION_SLOT;
    280. // solhint-disable-next-line no-inline-assembly
    281. assembly {
    282. sstore(slot, newImplementation)
    283. }
    284. }
    285. }
    286. // File: @openzeppelin/contracts/proxy/TransparentUpgradeableProxy.sol
    287. pragma solidity ^0.6.0;
    288. /**
    289. * @dev This contract implements a proxy that is upgradeable by an admin.
    290. *
    291. * To avoid https://medium.com/nomic-labs-blog/malicious-backdoors-in-ethereum-proxies-62629adf3357[proxy selector
    292. * clashing], which can potentially be used in an attack, this contract uses the
    293. * https://blog.openzeppelin.com/the-transparent-proxy-pattern/[transparent proxy pattern]. This pattern implies two
    294. * things that go hand in hand:
    295. *
    296. * 1. If any account other than the admin calls the proxy, the call will be forwarded to the implementation, even if
    297. * that call matches one of the admin functions exposed by the proxy itself.
    298. * 2. If the admin calls the proxy, it can access the admin functions, but its calls will never be forwarded to the
    299. * implementation. If the admin tries to call a function on the implementation it will fail with an error that says
    300. * "admin cannot fallback to proxy target".
    301. *
    302. * These properties mean that the admin account can only be used for admin actions like upgrading the proxy or changing
    303. * the admin, so it's best if it's a dedicated account that is not used for anything else. This will avoid headaches due
    304. * to sudden errors when trying to call a function from the proxy implementation.
    305. *
    306. * Our recommendation is for the dedicated account to be an instance of the {ProxyAdmin} contract. If set up this way,
    307. * you should think of the `ProxyAdmin` instance as the real administrative inerface of your proxy.
    308. */
    309. contract TransparentUpgradeableProxy is UpgradeableProxy {
    310. /**
    311. * @dev Initializes an upgradeable proxy managed by `_admin`, backed by the implementation at `_logic`, and
    312. * optionally initialized with `_data` as explained in {UpgradeableProxy-constructor}.
    313. */
    314. constructor(address _logic, address _admin, bytes memory _data) public payable UpgradeableProxy(_logic, _data) {
    315. assert(_ADMIN_SLOT == bytes32(uint256(keccak256("eip1967.proxy.admin")) - 1));
    316. _setAdmin(_admin);
    317. }
    318. /**
    319. * @dev Emitted when the admin account has changed.
    320. */
    321. event AdminChanged(address previousAdmin, address newAdmin);
    322. /**
    323. * @dev Storage slot with the admin of the contract.
    324. * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
    325. * validated in the constructor.
    326. */
    327. bytes32 private constant _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;
    328. /**
    329. * @dev Modifier used internally that will delegate the call to the implementation unless the sender is the admin.
    330. */
    331. modifier ifAdmin() {
    332. if (msg.sender == _admin()) {
    333. _;
    334. } else {
    335. _fallback();
    336. }
    337. }
    338. /**
    339. * @dev Returns the current admin.
    340. *
    341. * NOTE: Only the admin can call this function. See {ProxyAdmin-getProxyAdmin}.
    342. *
    343. * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
    344. * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
    345. * `0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103`
    346. */
    347. function admin() external ifAdmin returns (address) {
    348. return _admin();
    349. }
    350. /**
    351. * @dev Returns the current implementation.
    352. *
    353. * NOTE: Only the admin can call this function. See {ProxyAdmin-getProxyImplementation}.
    354. *
    355. * TIP: To get this value clients can read directly from the storage slot shown below (specified by EIP1967) using the
    356. * https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
    357. * `0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc`
    358. */
    359. function implementation() external ifAdmin returns (address) {
    360. return _implementation();
    361. }
    362. /**
    363. * @dev Changes the admin of the proxy.
    364. *
    365. * Emits an {AdminChanged} event.
    366. *
    367. * NOTE: Only the admin can call this function. See {ProxyAdmin-changeProxyAdmin}.
    368. */
    369. function changeAdmin(address newAdmin) external ifAdmin {
    370. require(newAdmin != address(0), "TransparentUpgradeableProxy: new admin is the zero address");
    371. emit AdminChanged(_admin(), newAdmin);
    372. _setAdmin(newAdmin);
    373. }
    374. /**
    375. * @dev Upgrade the implementation of the proxy.
    376. *
    377. * NOTE: Only the admin can call this function. See {ProxyAdmin-upgrade}.
    378. */
    379. function upgradeTo(address newImplementation) external ifAdmin {
    380. _upgradeTo(newImplementation);
    381. }
    382. /**
    383. * @dev Upgrade the implementation of the proxy, and then call a function from the new implementation as specified
    384. * by `data`, which should be an encoded function call. This is useful to initialize new storage variables in the
    385. * proxied contract.
    386. *
    387. * NOTE: Only the admin can call this function. See {ProxyAdmin-upgradeAndCall}.
    388. */
    389. function upgradeToAndCall(address newImplementation, bytes calldata data) external payable ifAdmin {
    390. _upgradeTo(newImplementation);
    391. // solhint-disable-next-line avoid-low-level-calls
    392. (bool success,) = newImplementation.delegatecall(data);
    393. require(success);
    394. }
    395. /**
    396. * @dev Returns the current admin.
    397. */
    398. function _admin() internal view returns (address adm) {
    399. bytes32 slot = _ADMIN_SLOT;
    400. // solhint-disable-next-line no-inline-assembly
    401. assembly {
    402. adm := sload(slot)
    403. }
    404. }
    405. /**
    406. * @dev Stores a new address in the EIP1967 admin slot.
    407. */
    408. function _setAdmin(address newAdmin) private {
    409. bytes32 slot = _ADMIN_SLOT;
    410. // solhint-disable-next-line no-inline-assembly
    411. assembly {
    412. sstore(slot, newAdmin)
    413. }
    414. }
    415. /**
    416. * @dev Makes sure the admin cannot access the fallback function. See {Proxy-_beforeFallback}.
    417. */
    418. function _beforeFallback() internal override virtual {
    419. require(msg.sender != _admin(), "TransparentUpgradeableProxy: admin cannot fallback to proxy target");
    420. super._beforeFallback();
    421. }
    422. }
    423. // File: torn-token/contracts/ENS.sol
    424. pragma solidity ^0.6.0;
    425. interface ENS {
    426. function resolver(bytes32 node) external view returns (Resolver);
    427. }
    428. interface Resolver {
    429. function addr(bytes32 node) external view returns (address);
    430. }
    431. contract EnsResolve {
    432. function resolve(bytes32 node) public view virtual returns (address) {
    433. ENS Registry = ENS(
    434. getChainId() == 1 ? 0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e : 0x8595bFb0D940DfEDC98943FA8a907091203f25EE
    435. );
    436. return Registry.resolver(node).addr(node);
    437. }
    438. function bulkResolve(bytes32[] memory domains) public view returns (address[] memory result) {
    439. result = new address[](domains.length);
    440. for (uint256 i = 0; i < domains.length; i++) {
    441. result[i] = resolve(domains[i]);
    442. }
    443. }
    444. function getChainId() internal pure returns (uint256) {
    445. uint256 chainId;
    446. assembly {
    447. chainId := chainid()
    448. }
    449. return chainId;
    450. }
    451. }
    452. // File: contracts/LoopbackProxy.sol
    453. pragma solidity ^0.6.0;
    454. /**
    455. * @dev TransparentUpgradeableProxy that sets its admin to the implementation itself.
    456. * It is also allowed to call implementation methods.
    457. */
    458. contract LoopbackProxy is TransparentUpgradeableProxy, EnsResolve {
    459. /**
    460. * @dev Initializes an upgradeable proxy backed by the implementation at `_logic`.
    461. */
    462. constructor(bytes32 _logic, bytes memory _data)
    463. public
    464. payable
    465. TransparentUpgradeableProxy(resolve(_logic), address(this), _data)
    466. {}
    467. /**
    468. * @dev Override to allow admin (itself) access the fallback function.
    469. */
    470. function _beforeFallback() internal override {}
    471. }