DecimalQuote.sol (1167B)
1 pragma solidity ^0.8.0; 2 3 // Author: Louis Holbrook <dev@holbrook.no> 0826EDA1702D1E87C6E2875121D2E7BB88C2A746 4 // SPDX-License-Identifier: AGPL-3.0-or-later 5 // File-Version: 1 6 // Description: ACL-enabled ERC20 token swap for tokens with compatible properties. 7 8 contract DecimalQuote { 9 // Implements TokenQuote 10 function valueFor(address _outToken, address _inToken, uint256 _value) public returns(uint256) { 11 uint8 dout; 12 uint8 din; 13 uint256 d; 14 bool r; 15 bytes memory v; 16 17 (r, v) = _outToken.call(abi.encodeWithSignature("decimals()")); 18 require(r, "ERR_TOKEN_OUT"); 19 dout = abi.decode(v, (uint8)); 20 21 (r, v) = _inToken.call(abi.encodeWithSignature("decimals()")); 22 require(r, "ERR_TOKEN_IN"); 23 din = abi.decode(v, (uint8)); 24 25 if (din == dout) { 26 return _value; 27 } 28 29 if (din > dout) { 30 d = din - dout; 31 d = 10 ** d; 32 return _value / d; 33 } else { 34 d = dout - din; 35 d = 10 ** d; 36 return _value * d; 37 } 38 } 39 40 // Implements EIP165 41 function supportsInterface(bytes4 _sum) public pure returns (bool) { 42 if (_sum == 0x01ffc9a7) { // ERC165 43 return true; 44 } 45 if (_sum == 0xdbb21d40) { // TokenQuote 46 return true; 47 } 48 return false; 49 } 50 }