mirror of
https://github.com/grassrootseconomics/erc20-pool.git
synced 2026-03-19 05:00:28 +01:00
Add quoter example contract for decimals translation
This commit is contained in:
39
solidity/DecimalQuote.sol
Normal file
39
solidity/DecimalQuote.sol
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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/
|
||||
|
||||
Reference in New Issue
Block a user