Add quoter example contract for decimals translation

This commit is contained in:
lash
2023-08-03 14:53:30 +01:00
parent af059d24a7
commit 5a1114ae55
9 changed files with 323 additions and 0 deletions

39
solidity/DecimalQuote.sol Normal file
View File

@@ -0,0 +1,39 @@
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");
dout = abi.decode(v, (uint8));
(r, v) = _inToken.call(abi.encodeWithSignature("decimals()"));
require(r, "ERR_TOKEN");
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;
}
}
}

View File

@@ -5,6 +5,10 @@ all:
$(SOLC) --abi SwapPool.sol --evm-version byzantium | awk 'NR>3' > SwapPool.json
$(SOLC) --metadata SwapPool.sol --evm-version byzantium | awk 'NR>3' > SwapPool.metadata.json
truncate -s -1 SwapPool.bin
$(SOLC) --bin DecimalQuote.sol --evm-version byzantium | awk 'NR>3' > DecimalQuote.bin
$(SOLC) --abi DecimalQuote.sol --evm-version byzantium | awk 'NR>3' > DecimalQuote.json
$(SOLC) --metadata DecimalQuote.sol --evm-version byzantium | awk 'NR>3' > DecimalQuote.metadata.json
truncate -s -1 DecimalQuote.bin
install: all
cp -v *.json ../python/erc20_pool/data/