erc20-pool/solidity/DecimalQuote.sol

51 lines
1.1 KiB
Solidity
Raw Normal View History

pragma solidity ^0.8.0;
// Author: Louis Holbrook <dev@holbrook.no> 0826EDA1702D1E87C6E2875121D2E7BB88C2A746
// SPDX-License-Identifier: AGPL-3.0-or-later
// File-Version: 1
// Description: ACL-enabled ERC20 token swap for tokens with compatible properties.
contract DecimalQuote {
// Implements TokenQuote
function valueFor(address _outToken, address _inToken, uint256 _value) public returns(uint256) {
uint8 dout;
uint8 din;
uint256 d;
bool r;
bytes memory v;
(r, v) = _outToken.call(abi.encodeWithSignature("decimals()"));
require(r, "ERR_TOKEN_OUT");
dout = abi.decode(v, (uint8));
(r, v) = _inToken.call(abi.encodeWithSignature("decimals()"));
require(r, "ERR_TOKEN_IN");
din = abi.decode(v, (uint8));
if (din == dout) {
return _value;
}
if (din > dout) {
d = din - dout;
d = 10 ** d;
return _value / d;
} else {
d = dout - din;
d = 10 ** d;
return _value * d;
}
}
2023-08-03 16:51:28 +02:00
// Implements EIP165
function supportsInterface(bytes4 _sum) public pure returns (bool) {
if (_sum == 0x01ffc9a7) { // ERC165
return true;
}
if (_sum == 0xdbb21d40) { // TokenQuote
return true;
}
return false;
}
}