From dc891ce9bb897659ced9d9fcc312b873fc05a06c Mon Sep 17 00:00:00 2001 From: lash Date: Fri, 10 Feb 2023 10:05:10 +0000 Subject: [PATCH] Rehabilitate deployer cli script --- .../erc20_demurrage_token/runnable/deploy.py | 145 ++-- python/erc20_demurrage_token/token.py | 19 +- .../unittest/__init__.py | 2 +- python/erc20_demurrage_token/unittest/base.py | 167 +--- .../erc20_demurrage_token/unittest/newbase.py | 11 +- python/requirements.txt | 1 + python/tests/test_period.py | 2 +- python/tests/test_redistribution_single.py | 4 +- python/tests/test_redistribution_unit.py | 2 +- python/tests/test_single.py | 2 +- solidity/aux/ABDKMath64x64.sol | 752 ++++++++++++++++++ 11 files changed, 886 insertions(+), 221 deletions(-) create mode 100644 solidity/aux/ABDKMath64x64.sol diff --git a/python/erc20_demurrage_token/runnable/deploy.py b/python/erc20_demurrage_token/runnable/deploy.py index 71b8060..487b26f 100644 --- a/python/erc20_demurrage_token/runnable/deploy.py +++ b/python/erc20_demurrage_token/runnable/deploy.py @@ -29,6 +29,21 @@ from chainlib.eth.connection import EthHTTPConnection from chainlib.eth.tx import receipt from chainlib.eth.constant import ZERO_ADDRESS import chainlib.eth.cli +from chainlib.eth.cli.arg import ( + Arg, + ArgFlag, + process_args, + ) +from chainlib.eth.cli.config import ( + Config, + process_config, + ) +from chainlib.eth.cli.log import process_log +from chainlib.eth.settings import process_settings +from chainlib.eth.address import to_checksum_address +from chainlib.settings import ChainSettings + +from dexif import to_fixed # local imports import erc20_demurrage_token @@ -37,7 +52,6 @@ from erc20_demurrage_token import ( DemurrageTokenSettings, ) -logging.basicConfig(level=logging.WARNING) logg = logging.getLogger() script_dir = os.path.dirname(__file__) @@ -45,76 +59,103 @@ data_dir = os.path.join(script_dir, '..', 'data') config_dir = os.path.join(data_dir, 'config') -arg_flags = chainlib.eth.cli.argflag_std_write + +def process_config_local(config, arg, args, flags): + config.add(args.token_name, 'TOKEN_NAME') + config.add(args.token_symbol, 'TOKEN_SYMBOL') + config.add(args.token_decimals, 'TOKEN_DECIMALS') + sink_address = to_checksum_address(args.sink_address) + config.add(sink_address, 'TOKEN_SINK_ADDRESS') + config.add(args.redistribution_period, 'TOKEN_REDISTRIBUTION_PERIOD') + + v = args.demurrage_level / 1000000 + if v >= 1.0: + raise ValueError('demurrage level must be less than 100%') + demurrage_level = to_fixed(v) + config.add(demurrage_level, 'TOKEN_DEMURRAGE_LEVEL') + return config + + +arg_flags = ArgFlag() +arg = Arg(arg_flags) +flags = arg_flags.STD_WRITE | arg_flags.WALLET + argparser = chainlib.eth.cli.ArgumentParser(arg_flags) +argparser = process_args(argparser, arg, flags) argparser.add_argument('--name', dest='token_name', type=str, help='Token name') argparser.add_argument('--symbol', dest='token_symbol', required=True, type=str, help='Token symbol') argparser.add_argument('--decimals', dest='token_decimals', type=int, help='Token decimals') argparser.add_argument('--sink-address', dest='sink_address', type=str, help='demurrage level,ppm per minute') -argparser.add_argument('--supply-limit', dest='supply_limit', type=int, help='token supply limit (0 = no limit)') argparser.add_argument('--redistribution-period', type=int, help='redistribution period, minutes (0 = deactivate)') # default 10080 = week -argparser.add_argument('--multi', action='store_true', help='automatic redistribution') -argparser.add_argument('--demurrage-level', dest='demurrage_level', type=int, help='demurrage level, ppm per minute') +argparser.add_argument('--demurrage-level', dest='demurrage_level', type=int, help='demurrage level, ppm per period') args = argparser.parse_args() -arg_flags = chainlib.eth.cli.argflag_std_write +logg = process_log(args, logg) -extra_args = { - 'redistribution_period': 'TOKEN_REDISTRIBUTION_PERIOD', - 'demurrage_level': 'TOKEN_DEMURRAGE_LEVEL', - 'supply_limit': 'TOKEN_SUPPLY_LIMIT', - 'token_name': 'TOKEN_NAME', - 'token_symbol': 'TOKEN_SYMBOL', - 'token_decimals': 'TOKEN_DECIMALS', - 'sink_address': 'TOKEN_SINK_ADDRESS', - 'multi': None, - } -config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=DemurrageToken.gas(), base_config_dir=config_dir) - -if not bool(config.get('TOKEN_NAME')): - logg.info('token name not set, using symbol {} as name'.format(config.get('TOKEN_SYMBOL'))) - config.add(config.get('TOKEN_SYMBOL'), 'TOKEN_NAME', True) - -if config.get('TOKEN_SUPPLY_LIMIT') == None: - config.add(0, 'TOKEN_SUPPLY_LIMIT', True) - -if config.get('TOKEN_REDISTRIBUTION_PERIOD') == None: - config.add(10800, 'TOKEN_REDISTRIBUTION_PERIOD', True) +config = Config() +config = process_config(config, arg, args, flags) +config = process_config_local(config, arg, args, flags) logg.debug('config loaded:\n{}'.format(config)) -wallet = chainlib.eth.cli.Wallet() -wallet.from_config(config) +settings = ChainSettings() +settings = process_settings(settings, config) +logg.debug('settings loaded:\n{}'.format(settings)) -rpc = chainlib.eth.cli.Rpc(wallet=wallet) -conn = rpc.connect_by_config(config) -chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC')) +#extra_args = { +# 'redistribution_period': 'TOKEN_REDISTRIBUTION_PERIOD', +# 'demurrage_level': 'TOKEN_DEMURRAGE_LEVEL', +# 'supply_limit': 'TOKEN_SUPPLY_LIMIT', +# 'token_name': 'TOKEN_NAME', +# 'token_symbol': 'TOKEN_SYMBOL', +# 'token_decimals': 'TOKEN_DECIMALS', +# 'sink_address': 'TOKEN_SINK_ADDRESS', +# 'multi': None, +# } +#config = chainlib.eth.cli.Config.from_args(args, arg_flags, extra_args=extra_args, default_fee_limit=DemurrageToken.gas(), base_config_dir=config_dir) +# +#if not bool(config.get('TOKEN_NAME')): +# logg.info('token name not set, using symbol {} as name'.format(config.get('TOKEN_SYMBOL'))) +# config.add(config.get('TOKEN_SYMBOL'), 'TOKEN_NAME', True) +# +#if config.get('TOKEN_SUPPLY_LIMIT') == None: +# config.add(0, 'TOKEN_SUPPLY_LIMIT', True) +# +#if config.get('TOKEN_REDISTRIBUTION_PERIOD') == None: +# config.add(10800, 'TOKEN_REDISTRIBUTION_PERIOD', True) +#logg.debug('config loaded:\n{}'.format(config)) +# +#wallet = chainlib.eth.cli.Wallet() +#wallet.from_config(config) +# +#rpc = chainlib.eth.cli.Rpc(wallet=wallet) +#conn = rpc.connect_by_config(config) +# +#chain_spec = ChainSpec.from_chain_str(config.get('CHAIN_SPEC')) def main(): - signer = rpc.get_signer() - signer_address = rpc.get_sender_address() - - gas_oracle = rpc.get_gas_oracle() - nonce_oracle = rpc.get_nonce_oracle() - - c = DemurrageToken(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle) - settings = DemurrageTokenSettings() - settings.name = config.get('TOKEN_NAME') - settings.symbol = config.get('TOKEN_SYMBOL') - settings.decimals = int(config.get('TOKEN_DECIMALS')) - settings.demurrage_level = int(config.get('TOKEN_DEMURRAGE_LEVEL')) - settings.period_minutes = int(config.get('TOKEN_REDISTRIBUTION_PERIOD')) - settings.sink_address = config.get('TOKEN_SINK_ADDRESS') + conn = settings.get('CONN') + c = DemurrageToken( + settings.get('CHAIN_SPEC'), + signer=settings.get('SIGNER'), + gas_oracle=settings.get('FEE_ORACLE'), + nonce_oracle=settings.get('NONCE_ORACLE'), + ) + token_settings = DemurrageTokenSettings() + token_settings.name = config.get('TOKEN_NAME') + token_settings.symbol = config.get('TOKEN_SYMBOL') + token_settings.decimals = int(config.get('TOKEN_DECIMALS')) + token_settings.demurrage_level = int(config.get('TOKEN_DEMURRAGE_LEVEL')) + token_settings.period_minutes = int(config.get('TOKEN_REDISTRIBUTION_PERIOD')) + token_settings.sink_address = config.get('TOKEN_SINK_ADDRESS') (tx_hash_hex, o) = c.constructor( - signer_address, - settings, - redistribute=config.true('_MULTI'), - cap=int(config.get('TOKEN_SUPPLY_LIMIT')), + settings.get('SENDER_ADDRESS'), + token_settings, ) - if config.get('_RPC_SEND'): + if settings.get('RPC_SEND'): conn.do(o) - if config.get('_WAIT'): + if config.true('_WAIT'): r = conn.wait(tx_hash_hex) if r['status'] == 0: sys.stderr.write('EVM revert while deploying contract. Wish I had more to tell you') diff --git a/python/erc20_demurrage_token/token.py b/python/erc20_demurrage_token/token.py index 8168173..2053256 100644 --- a/python/erc20_demurrage_token/token.py +++ b/python/erc20_demurrage_token/token.py @@ -21,10 +21,10 @@ from hexathon import ( add_0x, strip_0x, ) +from dexif import from_fixed # local imports from erc20_demurrage_token.data import data_dir -from erc20_demurrage_token.fixed import from_fixed logg = logging.getLogger(__name__) @@ -82,10 +82,10 @@ class DemurrageToken(ERC20): 'SingleCap', ] - def constructor(self, sender_address, settings, redistribute=True, cap=0, tx_format=TxFormat.JSONRPC): + def constructor(self, sender_address, settings, cap=0, tx_format=TxFormat.JSONRPC): if int(cap) < 0: raise ValueError('cap must be 0 or positive integer') - code = DemurrageToken.bytecode(multi=redistribute, cap=cap>0) + code = DemurrageToken.bytecode() enc = ABIContractEncoder() enc.string(settings.name) enc.string(settings.symbol) @@ -603,18 +603,7 @@ class DemurrageToken(ERC20): @classmethod def parse_redistributions(self, v): return strip_0x(v) - #return DemurrageRedistribution(v) -# d = ABIContractDecoder() -# v = strip_0x(v) -# d.typ(ABIContractType.BYTES32) -# d.typ(ABIContractType.BYTES32) -# d.typ(ABIContractType.BYTES32) -# d.val(v[:64]) -# d.val(v[64:128]) -# d.val(v[128:192]) -# r = d.decode() -# return ''.join(r) - + @classmethod def parse_account_period(self, v): diff --git a/python/erc20_demurrage_token/unittest/__init__.py b/python/erc20_demurrage_token/unittest/__init__.py index 61f6a46..9b5ed21 100644 --- a/python/erc20_demurrage_token/unittest/__init__.py +++ b/python/erc20_demurrage_token/unittest/__init__.py @@ -1 +1 @@ -from .newbase import * +from .base import * diff --git a/python/erc20_demurrage_token/unittest/base.py b/python/erc20_demurrage_token/unittest/base.py index c7eda7c..d9979c2 100644 --- a/python/erc20_demurrage_token/unittest/base.py +++ b/python/erc20_demurrage_token/unittest/base.py @@ -1,6 +1,7 @@ # standard imports import logging import os +import math # external imports from chainlib.eth.unittest.ethtester import EthTesterCase @@ -19,6 +20,7 @@ from erc20_demurrage_token import ( DemurrageTokenSettings, DemurrageToken, ) +from dexif import * logg = logging.getLogger() @@ -26,12 +28,12 @@ logg = logging.getLogger() TAX_LEVEL = int(10000 * 2) # 2% # calc "1-(0.98)^(1/518400)" <- 518400 = 30 days of blocks # 0.00000003897127107225 -#PERIOD = int(60/BLOCKTIME) * 60 * 24 * 30 # month -PERIOD = 10 +PERIOD = 43200 class TestTokenDeploy: + """tax level is ppm, 1000000 = 100%""" def __init__(self, rpc, token_symbol='FOO', token_name='Foo Token', sink_address=ZERO_ADDRESS, supply=10**12, tax_level=TAX_LEVEL, period=PERIOD): self.tax_level = tax_level self.period_seconds = period * 60 @@ -40,7 +42,8 @@ class TestTokenDeploy: self.settings.name = token_name self.settings.symbol = token_symbol self.settings.decimals = 6 - self.settings.demurrage_level = tax_level * (10 ** 32) + tax_level_input = to_fixed((1 - (tax_level / 1000000)) ** (1 / period)) + self.settings.demurrage_level = tax_level_input self.settings.period_minutes = period self.settings.sink_address = sink_address self.sink_address = self.settings.sink_address @@ -58,24 +61,13 @@ class TestTokenDeploy: self.start_time = int(r['timestamp']) self.default_supply = supply - self.default_supply_cap = int(self.default_supply * 10) + self.default_supply_cap = 0 - def deploy(self, rpc, deployer_address, interface, mode, supply_cap=10**12): + def deploy(self, rpc, deployer_address, interface, supply_cap=0): tx_hash = None o = None - logg.debug('mode {} {}'.format(mode, self.settings)) - self.mode = mode - if mode == 'MultiNocap': - (tx_hash, o) = interface.constructor(deployer_address, self.settings, redistribute=True, cap=0) - elif mode == 'SingleNocap': - (tx_hash, o) = interface.constructor(deployer_address, self.settings, redistribute=False, cap=0) - elif mode == 'MultiCap': - (tx_hash, o) = interface.constructor(deployer_address, self.settings, redistribute=True, cap=supply_cap) - elif mode == 'SingleCap': - (tx_hash, o) = interface.constructor(deployer_address, self.settings, redistribute=False, cap=supply_cap) - else: - raise ValueError('Invalid mode "{}", valid are {}'.format(mode, DemurrageToken.valid_modes)) + (tx_hash, o) = interface.constructor(deployer_address, self.settings, redistribute=False, cap=0) r = rpc.do(o) o = receipt(tx_hash) @@ -95,13 +87,6 @@ class TestDemurrage(EthTesterCase): def setUp(self): super(TestDemurrage, self).setUp() -# token_deploy = TestTokenDeploy() -# self.settings = token_deploy.settings -# self.sink_address = token_deploy.sink_address -# self.start_block = token_deploy.start_block -# self.start_time = token_deploy.start_time -# self.default_supply = self.default_supply -# self.default_supply_cap = self.default_supply_cap period = PERIOD try: period = getattr(self, 'period') @@ -115,8 +100,8 @@ class TestDemurrage(EthTesterCase): self.start_time = None - def deploy(self, interface, mode): - self.address = self.deployer.deploy(self.rpc, self.accounts[0], interface, mode, supply_cap=self.default_supply_cap) + def deploy(self, interface): + self.address = self.deployer.deploy(self.rpc, self.accounts[0], interface, supply_cap=self.default_supply_cap) self.start_block = self.deployer.start_block self.start_time = self.deployer.start_time self.tax_level = self.deployer.tax_level @@ -126,6 +111,17 @@ class TestDemurrage(EthTesterCase): logg.debug('contract address {} start block {} start time {}'.format(self.address, self.start_block, self.start_time)) + def assert_within(self, v, target, tolerance_ppm): + lower_target = target - (target * (tolerance_ppm / 1000000)) + higher_target = target + (target * (tolerance_ppm / 1000000)) + #self.assertGreaterEqual(v, lower_target) + #self.assertLessEqual(v, higher_target) + if v >= lower_target and v <= higher_target: + logg.debug('asserted within {} <= {} <= {}'.format(lower_target, v, higher_target)) + return + raise AssertionError('{} not within lower {} and higher {}'.format(v, lower_target, higher_target)) + + def assert_within_lower(self, v, target, tolerance_ppm): lower_target = target - (target * (tolerance_ppm / 1000000)) self.assertGreaterEqual(v, lower_target) @@ -133,6 +129,12 @@ class TestDemurrage(EthTesterCase): logg.debug('asserted within lower {} <= {} <= {}'.format(lower_target, v, target)) + def assert_equal_decimals(self, v, target, precision): + target = int(target * (10 ** precision)) + target = target / (10 ** precision) + self.assertEqual(v, target) + + def tearDown(self): pass @@ -145,115 +147,4 @@ class TestDemurrageDefault(TestDemurrage): nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) - self.mode = os.environ.get('ERC20_DEMURRAGE_TOKEN_TEST_MODE') - if self.mode == None: - self.mode = 'MultiNocap' - logg.debug('executing test setup default mode {}'.format(self.mode)) - - self.deploy(c, self.mode) - - logg.info('deployed with mode {}'.format(self.mode)) - - -class TestDemurrageSingle(TestDemurrage): - - def setUp(self): - super(TestDemurrageSingle, self).setUp() - - nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) - c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) - - self.mode = os.environ.get('ERC20_DEMURRAGE_TOKEN_TEST_MODE') - single_valid_modes = [ - 'SingleNocap', - 'SingleCap', - ] - if self.mode != None: - if self.mode not in single_valid_modes: - raise ValueError('Invalid mode "{}" for "single" contract tests, valid are {}'.format(self.mode, single_valid_modes)) - else: - self.mode = 'SingleNocap' - logg.debug('executing test setup demurragesingle mode {}'.format(self.mode)) - - self.deploy(c, self.mode) - - logg.info('deployed with mode {}'.format(self.mode)) - - -class TestDemurrageCap(TestDemurrage): - - def setUp(self): - super(TestDemurrageCap, self).setUp() - - nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) - c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) - - self.mode = os.environ.get('ERC20_DEMURRAGE_TOKEN_TEST_MODE') - cap_valid_modes = [ - 'MultiCap', - 'SingleCap', - ] - if self.mode != None: - if self.mode not in cap_valid_modes: - raise ValueError('Invalid mode "{}" for "cap" contract tests, valid are {}'.format(self.mode, cap_valid_modes)) - else: - self.mode = 'MultiCap' - logg.debug('executing test setup demurragecap mode {}'.format(self.mode)) - - self.deploy(c, self.mode) - - logg.info('deployed with mode {}'.format(self.mode)) - - - -class TestDemurrageUnit(TestDemurrage): - - def setUp(self): - self.period = 1 - self.period_seconds = self.period * 60 - self.tax_level = TAX_LEVEL - - super(TestDemurrageUnit, self).setUp() - - nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) - self.settings = DemurrageTokenSettings() - self.settings.name = 'Foo Token' - self.settings.symbol = 'FOO' - self.settings.decimals = 6 - self.settings.demurrage_level = self.tax_level * (10 ** 32) - self.settings.period_minutes = int(self.period_seconds/60) - self.settings.sink_address = self.accounts[9] - self.sink_address = self.settings.sink_address - - o = block_latest() - self.start_block = self.rpc.do(o) - - o = block_by_number(self.start_block, include_tx=False) - r = self.rpc.do(o) - - try: - self.start_time = int(r['timestamp'], 16) - except TypeError: - self.start_time = int(r['timestamp']) - - self.default_supply = 1000000000000 - self.default_supply_cap = int(self.default_supply * 10) - - nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) - c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) - - self.mode = os.environ.get('ERC20_DEMURRAGE_TOKEN_TEST_MODE') - unit_valid_modes = [ - 'SingleNocap', - 'SingleCap', - ] - if self.mode != None: - if self.mode not in unit_valid_modes: - raise ValueError('Invalid mode "{}" for "unit" contract tests, valid are {}'.format(self.mode, unit_valid_modes)) - else: - self.mode = 'SingleNocap' - logg.debug('executing test setup unit mode {}'.format(self.mode)) - - self.deploy(c, self.mode) - - logg.info('deployed with mode {}'.format(self.mode)) + self.deploy(c) diff --git a/python/erc20_demurrage_token/unittest/newbase.py b/python/erc20_demurrage_token/unittest/newbase.py index a06fae1..ae177bc 100644 --- a/python/erc20_demurrage_token/unittest/newbase.py +++ b/python/erc20_demurrage_token/unittest/newbase.py @@ -20,19 +20,12 @@ from erc20_demurrage_token import ( DemurrageTokenSettings, DemurrageToken, ) -from erc20_demurrage_token.fixed import ( - to_fixed, - from_fixed, - ) +from dexif import * logg = logging.getLogger() -#BLOCKTIME = 5 # seconds TAX_LEVEL = int(10000 * 2) # 2% -# calc "1-(0.98)^(1/518400)" <- 518400 = 30 days of blocks -# 0.00000003897127107225 -PERIOD = 43200 - +PERIOD = 43200 # 30 days in minutes class TestTokenDeploy: diff --git a/python/requirements.txt b/python/requirements.txt index 8cc5de0..9dbeab8 100644 --- a/python/requirements.txt +++ b/python/requirements.txt @@ -1,3 +1,4 @@ chainlib-eth~=0.4.11 eth-erc20~=0.5.0 funga-eth~=0.6.0 +dexif~=0.0.1 diff --git a/python/tests/test_period.py b/python/tests/test_period.py index 1b0b16e..9fc6da5 100644 --- a/python/tests/test_period.py +++ b/python/tests/test_period.py @@ -18,11 +18,11 @@ from chainlib.eth.contract import ( ) from hexathon import same as hex_same from hexathon import strip_0x +from dexif import from_fixed # local imports from erc20_demurrage_token import DemurrageToken from erc20_demurrage_token import DemurrageRedistribution -from erc20_demurrage_token.fixed import from_fixed # test imports from erc20_demurrage_token.unittest import TestDemurrageDefault diff --git a/python/tests/test_redistribution_single.py b/python/tests/test_redistribution_single.py index a3420b1..c45bcc2 100644 --- a/python/tests/test_redistribution_single.py +++ b/python/tests/test_redistribution_single.py @@ -18,10 +18,10 @@ from hexathon import ( add_0x, same as hex_same, ) +from dexif import from_fixed # local imports from erc20_demurrage_token import DemurrageToken -from erc20_demurrage_token.fixed import from_fixed # test imports from erc20_demurrage_token.unittest import TestDemurrageDefault @@ -63,7 +63,6 @@ class TestRedistribution(TestDemurrageDefault): o = c.to_redistribution_demurrage_modifier(self.address, redistribution, sender_address=self.accounts[0]) r = self.rpc.do(o) - #demurrage = c.parse_to_redistribution_item(r) demurrage = from_fixed(r) o = c.redistributions(self.address, i-1, sender_address=self.accounts[0]) @@ -71,7 +70,6 @@ class TestRedistribution(TestDemurrageDefault): o = c.to_redistribution_demurrage_modifier(self.address, redistribution, sender_address=self.accounts[0]) r = self.rpc.do(o) - #demurrage_previous = c.parse_to_redistribution_item(r) demurrage_previous = from_fixed(r) o = c.balance_of(self.address, self.sink_address, sender_address=self.accounts[0]) diff --git a/python/tests/test_redistribution_unit.py b/python/tests/test_redistribution_unit.py index a2a7ebe..386fb23 100644 --- a/python/tests/test_redistribution_unit.py +++ b/python/tests/test_redistribution_unit.py @@ -16,10 +16,10 @@ from hexathon import ( strip_0x, add_0x, ) +from dexif import to_fixed # local imports from erc20_demurrage_token import DemurrageToken -from erc20_demurrage_token.fixed import to_fixed from erc20_demurrage_token import DemurrageRedistribution # test imports diff --git a/python/tests/test_single.py b/python/tests/test_single.py index 508ca07..230c644 100644 --- a/python/tests/test_single.py +++ b/python/tests/test_single.py @@ -13,13 +13,13 @@ from hexathon import ( strip_0x, add_0x, ) +from dexif import to_fixed # local imports from erc20_demurrage_token import DemurrageToken # test imports from erc20_demurrage_token.unittest import TestDemurrageDefault -from erc20_demurrage_token.fixed import to_fixed logging.basicConfig(level=logging.DEBUG) logg = logging.getLogger() diff --git a/solidity/aux/ABDKMath64x64.sol b/solidity/aux/ABDKMath64x64.sol new file mode 100644 index 0000000..8b53809 --- /dev/null +++ b/solidity/aux/ABDKMath64x64.sol @@ -0,0 +1,752 @@ +// SPDX-License-Identifier: BSD-4-Clause +/* + * ABDK Math 64.64 Smart Contract Library. Copyright © 2019 by ABDK Consulting. + * Author: Mikhail Vladimirov + */ +pragma solidity ^0.8.0; + +/** + * Smart contract library of mathematical functions operating with signed + * 64.64-bit fixed point numbers. Signed 64.64-bit fixed point number is + * basically a simple fraction whose numerator is signed 128-bit integer and + * denominator is 2^64. As long as denominator is always the same, there is no + * need to store it, thus in Solidity signed 64.64-bit fixed point numbers are + * represented by int128 type holding only the numerator. + */ +library ABDKMath64x64 { + /* + * Minimum value signed 64.64-bit fixed point number may have. + */ + int128 private constant MIN_64x64 = -0x80000000000000000000000000000000; + + /* + * Maximum value signed 64.64-bit fixed point number may have. + */ + int128 private constant MAX_64x64 = 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF; + + /** + * Convert signed 256-bit integer number into signed 64.64-bit fixed point + * number. Revert on overflow. + * + * @param x signed 256-bit integer number + * @return signed 64.64-bit fixed point number + */ + function fromInt (int256 x) internal pure returns (int128) { + unchecked { + require (x >= -0x8000000000000000 && x <= 0x7FFFFFFFFFFFFFFF); + return int128 (x << 64); + } + } + + /** + * Convert signed 64.64 fixed point number into signed 64-bit integer number + * rounding down. + * + * @param x signed 64.64-bit fixed point number + * @return signed 64-bit integer number + */ + function toInt (int128 x) internal pure returns (int64) { + unchecked { + return int64 (x >> 64); + } + } + + /** + * Convert unsigned 256-bit integer number into signed 64.64-bit fixed point + * number. Revert on overflow. + * + * @param x unsigned 256-bit integer number + * @return signed 64.64-bit fixed point number + */ + function fromUInt (uint256 x) internal pure returns (int128) { + unchecked { + require (x <= 0x7FFFFFFFFFFFFFFF); + return int128 (int256 (x << 64)); + } + } + + /** + * Convert signed 64.64 fixed point number into unsigned 64-bit integer + * number rounding down. Revert on underflow. + * + * @param x signed 64.64-bit fixed point number + * @return unsigned 64-bit integer number + */ + function toUInt (int128 x) internal pure returns (uint64) { + unchecked { + require (x >= 0); + return uint64 (uint128 (x >> 64)); + } + } + + /** + * Convert signed 128.128 fixed point number into signed 64.64-bit fixed point + * number rounding down. Revert on overflow. + * + * @param x signed 128.128-bin fixed point number + * @return signed 64.64-bit fixed point number + */ + function from128x128 (int256 x) internal pure returns (int128) { + unchecked { + int256 result = x >> 64; + require (result >= MIN_64x64 && result <= MAX_64x64); + return int128 (result); + } + } + + /** + * Convert signed 64.64 fixed point number into signed 128.128 fixed point + * number. + * + * @param x signed 64.64-bit fixed point number + * @return signed 128.128 fixed point number + */ + function to128x128 (int128 x) internal pure returns (int256) { + unchecked { + return int256 (x) << 64; + } + } + + /** + * Calculate x + y. Revert on overflow. + * + * @param x signed 64.64-bit fixed point number + * @param y signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function add (int128 x, int128 y) internal pure returns (int128) { + unchecked { + int256 result = int256(x) + y; + require (result >= MIN_64x64 && result <= MAX_64x64); + return int128 (result); + } + } + + /** + * Calculate x - y. Revert on overflow. + * + * @param x signed 64.64-bit fixed point number + * @param y signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function sub (int128 x, int128 y) internal pure returns (int128) { + unchecked { + int256 result = int256(x) - y; + require (result >= MIN_64x64 && result <= MAX_64x64); + return int128 (result); + } + } + + /** + * Calculate x * y rounding down. Revert on overflow. + * + * @param x signed 64.64-bit fixed point number + * @param y signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function mul (int128 x, int128 y) internal pure returns (int128) { + unchecked { + int256 result = int256(x) * y >> 64; + require (result >= MIN_64x64 && result <= MAX_64x64); + return int128 (result); + } + } + + /** + * Calculate x * y rounding towards zero, where x is signed 64.64 fixed point + * number and y is signed 256-bit integer number. Revert on overflow. + * + * @param x signed 64.64 fixed point number + * @param y signed 256-bit integer number + * @return signed 256-bit integer number + */ + function muli (int128 x, int256 y) internal pure returns (int256) { + unchecked { + if (x == MIN_64x64) { + require (y >= -0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF && + y <= 0x1000000000000000000000000000000000000000000000000); + return -y << 63; + } else { + bool negativeResult = false; + if (x < 0) { + x = -x; + negativeResult = true; + } + if (y < 0) { + y = -y; // We rely on overflow behavior here + negativeResult = !negativeResult; + } + uint256 absoluteResult = mulu (x, uint256 (y)); + if (negativeResult) { + require (absoluteResult <= + 0x8000000000000000000000000000000000000000000000000000000000000000); + return -int256 (absoluteResult); // We rely on overflow behavior here + } else { + require (absoluteResult <= + 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + return int256 (absoluteResult); + } + } + } + } + + /** + * Calculate x * y rounding down, where x is signed 64.64 fixed point number + * and y is unsigned 256-bit integer number. Revert on overflow. + * + * @param x signed 64.64 fixed point number + * @param y unsigned 256-bit integer number + * @return unsigned 256-bit integer number + */ + function mulu (int128 x, uint256 y) internal pure returns (uint256) { + unchecked { + if (y == 0) return 0; + + require (x >= 0); + + uint256 lo = (uint256 (int256 (x)) * (y & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)) >> 64; + uint256 hi = uint256 (int256 (x)) * (y >> 128); + + require (hi <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + hi <<= 64; + + require (hi <= + 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF - lo); + return hi + lo; + } + } + + /** + * Calculate x / y rounding towards zero. Revert on overflow or when y is + * zero. + * + * @param x signed 64.64-bit fixed point number + * @param y signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function div (int128 x, int128 y) internal pure returns (int128) { + unchecked { + require (y != 0); + int256 result = (int256 (x) << 64) / y; + require (result >= MIN_64x64 && result <= MAX_64x64); + return int128 (result); + } + } + + /** + * Calculate x / y rounding towards zero, where x and y are signed 256-bit + * integer numbers. Revert on overflow or when y is zero. + * + * @param x signed 256-bit integer number + * @param y signed 256-bit integer number + * @return signed 64.64-bit fixed point number + */ + function divi (int256 x, int256 y) internal pure returns (int128) { + unchecked { + require (y != 0); + + bool negativeResult = false; + if (x < 0) { + x = -x; // We rely on overflow behavior here + negativeResult = true; + } + if (y < 0) { + y = -y; // We rely on overflow behavior here + negativeResult = !negativeResult; + } + uint128 absoluteResult = divuu (uint256 (x), uint256 (y)); + if (negativeResult) { + require (absoluteResult <= 0x80000000000000000000000000000000); + return -int128 (absoluteResult); // We rely on overflow behavior here + } else { + require (absoluteResult <= 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + return int128 (absoluteResult); // We rely on overflow behavior here + } + } + } + + /** + * Calculate x / y rounding towards zero, where x and y are unsigned 256-bit + * integer numbers. Revert on overflow or when y is zero. + * + * @param x unsigned 256-bit integer number + * @param y unsigned 256-bit integer number + * @return signed 64.64-bit fixed point number + */ + function divu (uint256 x, uint256 y) internal pure returns (int128) { + unchecked { + require (y != 0); + uint128 result = divuu (x, y); + require (result <= uint128 (MAX_64x64)); + return int128 (result); + } + } + + /** + * Calculate -x. Revert on overflow. + * + * @param x signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function neg (int128 x) internal pure returns (int128) { + unchecked { + require (x != MIN_64x64); + return -x; + } + } + + /** + * Calculate |x|. Revert on overflow. + * + * @param x signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function abs (int128 x) internal pure returns (int128) { + unchecked { + require (x != MIN_64x64); + return x < 0 ? -x : x; + } + } + + /** + * Calculate 1 / x rounding towards zero. Revert on overflow or when x is + * zero. + * + * @param x signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function inv (int128 x) internal pure returns (int128) { + unchecked { + require (x != 0); + int256 result = int256 (0x100000000000000000000000000000000) / x; + require (result >= MIN_64x64 && result <= MAX_64x64); + return int128 (result); + } + } + + /** + * Calculate arithmetics average of x and y, i.e. (x + y) / 2 rounding down. + * + * @param x signed 64.64-bit fixed point number + * @param y signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function avg (int128 x, int128 y) internal pure returns (int128) { + unchecked { + return int128 ((int256 (x) + int256 (y)) >> 1); + } + } + + /** + * Calculate geometric average of x and y, i.e. sqrt (x * y) rounding down. + * Revert on overflow or in case x * y is negative. + * + * @param x signed 64.64-bit fixed point number + * @param y signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function gavg (int128 x, int128 y) internal pure returns (int128) { + unchecked { + int256 m = int256 (x) * int256 (y); + require (m >= 0); + require (m < + 0x4000000000000000000000000000000000000000000000000000000000000000); + return int128 (sqrtu (uint256 (m))); + } + } + + /** + * Calculate x^y assuming 0^0 is 1, where x is signed 64.64 fixed point number + * and y is unsigned 256-bit integer number. Revert on overflow. + * + * @param x signed 64.64-bit fixed point number + * @param y uint256 value + * @return signed 64.64-bit fixed point number + */ + function pow (int128 x, uint256 y) internal pure returns (int128) { + unchecked { + bool negative = x < 0 && y & 1 == 1; + + uint256 absX = uint128 (x < 0 ? -x : x); + uint256 absResult; + absResult = 0x100000000000000000000000000000000; + + if (absX <= 0x10000000000000000) { + absX <<= 63; + while (y != 0) { + if (y & 0x1 != 0) { + absResult = absResult * absX >> 127; + } + absX = absX * absX >> 127; + + if (y & 0x2 != 0) { + absResult = absResult * absX >> 127; + } + absX = absX * absX >> 127; + + if (y & 0x4 != 0) { + absResult = absResult * absX >> 127; + } + absX = absX * absX >> 127; + + if (y & 0x8 != 0) { + absResult = absResult * absX >> 127; + } + absX = absX * absX >> 127; + + y >>= 4; + } + + absResult >>= 64; + } else { + uint256 absXShift = 63; + if (absX < 0x1000000000000000000000000) { absX <<= 32; absXShift -= 32; } + if (absX < 0x10000000000000000000000000000) { absX <<= 16; absXShift -= 16; } + if (absX < 0x1000000000000000000000000000000) { absX <<= 8; absXShift -= 8; } + if (absX < 0x10000000000000000000000000000000) { absX <<= 4; absXShift -= 4; } + if (absX < 0x40000000000000000000000000000000) { absX <<= 2; absXShift -= 2; } + if (absX < 0x80000000000000000000000000000000) { absX <<= 1; absXShift -= 1; } + + uint256 resultShift = 0; + while (y != 0) { + require (absXShift < 64); + + if (y & 0x1 != 0) { + absResult = absResult * absX >> 127; + resultShift += absXShift; + if (absResult > 0x100000000000000000000000000000000) { + absResult >>= 1; + resultShift += 1; + } + } + absX = absX * absX >> 127; + absXShift <<= 1; + if (absX >= 0x100000000000000000000000000000000) { + absX >>= 1; + absXShift += 1; + } + + y >>= 1; + } + + require (resultShift < 64); + absResult >>= 64 - resultShift; + } + int256 result = negative ? -int256 (absResult) : int256 (absResult); + require (result >= MIN_64x64 && result <= MAX_64x64); + return int128 (result); + } + } + + /** + * Calculate sqrt (x) rounding down. Revert if x < 0. + * + * @param x signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function sqrt (int128 x) internal pure returns (int128) { + unchecked { + require (x >= 0); + return int128 (sqrtu (uint256 (int256 (x)) << 64)); + } + } + + /** + * Calculate binary logarithm of x. Revert if x <= 0. + * + * @param x signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function log_2 (int128 x) internal pure returns (int128) { + unchecked { + require (x > 0); + + int256 msb = 0; + int256 xc = x; + if (xc >= 0x10000000000000000) { xc >>= 64; msb += 64; } + if (xc >= 0x100000000) { xc >>= 32; msb += 32; } + if (xc >= 0x10000) { xc >>= 16; msb += 16; } + if (xc >= 0x100) { xc >>= 8; msb += 8; } + if (xc >= 0x10) { xc >>= 4; msb += 4; } + if (xc >= 0x4) { xc >>= 2; msb += 2; } + if (xc >= 0x2) msb += 1; // No need to shift xc anymore + + int256 result = msb - 64 << 64; + uint256 ux = uint256 (int256 (x)) << uint256 (127 - msb); + for (int256 bit = 0x8000000000000000; bit > 0; bit >>= 1) { + ux *= ux; + uint256 b = ux >> 255; + ux >>= 127 + b; + result += bit * int256 (b); + } + + return int128 (result); + } + } + + /** + * Calculate natural logarithm of x. Revert if x <= 0. + * + * @param x signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function ln (int128 x) internal pure returns (int128) { + unchecked { + require (x > 0); + + return int128 (int256 ( + uint256 (int256 (log_2 (x))) * 0xB17217F7D1CF79ABC9E3B39803F2F6AF >> 128)); + } + } + + /** + * Calculate binary exponent of x. Revert on overflow. + * + * @param x signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function exp_2 (int128 x) internal pure returns (int128) { + unchecked { + require (x < 0x400000000000000000); // Overflow + + if (x < -0x400000000000000000) return 0; // Underflow + + uint256 result = 0x80000000000000000000000000000000; + + if (x & 0x8000000000000000 > 0) + result = result * 0x16A09E667F3BCC908B2FB1366EA957D3E >> 128; + if (x & 0x4000000000000000 > 0) + result = result * 0x1306FE0A31B7152DE8D5A46305C85EDEC >> 128; + if (x & 0x2000000000000000 > 0) + result = result * 0x1172B83C7D517ADCDF7C8C50EB14A791F >> 128; + if (x & 0x1000000000000000 > 0) + result = result * 0x10B5586CF9890F6298B92B71842A98363 >> 128; + if (x & 0x800000000000000 > 0) + result = result * 0x1059B0D31585743AE7C548EB68CA417FD >> 128; + if (x & 0x400000000000000 > 0) + result = result * 0x102C9A3E778060EE6F7CACA4F7A29BDE8 >> 128; + if (x & 0x200000000000000 > 0) + result = result * 0x10163DA9FB33356D84A66AE336DCDFA3F >> 128; + if (x & 0x100000000000000 > 0) + result = result * 0x100B1AFA5ABCBED6129AB13EC11DC9543 >> 128; + if (x & 0x80000000000000 > 0) + result = result * 0x10058C86DA1C09EA1FF19D294CF2F679B >> 128; + if (x & 0x40000000000000 > 0) + result = result * 0x1002C605E2E8CEC506D21BFC89A23A00F >> 128; + if (x & 0x20000000000000 > 0) + result = result * 0x100162F3904051FA128BCA9C55C31E5DF >> 128; + if (x & 0x10000000000000 > 0) + result = result * 0x1000B175EFFDC76BA38E31671CA939725 >> 128; + if (x & 0x8000000000000 > 0) + result = result * 0x100058BA01FB9F96D6CACD4B180917C3D >> 128; + if (x & 0x4000000000000 > 0) + result = result * 0x10002C5CC37DA9491D0985C348C68E7B3 >> 128; + if (x & 0x2000000000000 > 0) + result = result * 0x1000162E525EE054754457D5995292026 >> 128; + if (x & 0x1000000000000 > 0) + result = result * 0x10000B17255775C040618BF4A4ADE83FC >> 128; + if (x & 0x800000000000 > 0) + result = result * 0x1000058B91B5BC9AE2EED81E9B7D4CFAB >> 128; + if (x & 0x400000000000 > 0) + result = result * 0x100002C5C89D5EC6CA4D7C8ACC017B7C9 >> 128; + if (x & 0x200000000000 > 0) + result = result * 0x10000162E43F4F831060E02D839A9D16D >> 128; + if (x & 0x100000000000 > 0) + result = result * 0x100000B1721BCFC99D9F890EA06911763 >> 128; + if (x & 0x80000000000 > 0) + result = result * 0x10000058B90CF1E6D97F9CA14DBCC1628 >> 128; + if (x & 0x40000000000 > 0) + result = result * 0x1000002C5C863B73F016468F6BAC5CA2B >> 128; + if (x & 0x20000000000 > 0) + result = result * 0x100000162E430E5A18F6119E3C02282A5 >> 128; + if (x & 0x10000000000 > 0) + result = result * 0x1000000B1721835514B86E6D96EFD1BFE >> 128; + if (x & 0x8000000000 > 0) + result = result * 0x100000058B90C0B48C6BE5DF846C5B2EF >> 128; + if (x & 0x4000000000 > 0) + result = result * 0x10000002C5C8601CC6B9E94213C72737A >> 128; + if (x & 0x2000000000 > 0) + result = result * 0x1000000162E42FFF037DF38AA2B219F06 >> 128; + if (x & 0x1000000000 > 0) + result = result * 0x10000000B17217FBA9C739AA5819F44F9 >> 128; + if (x & 0x800000000 > 0) + result = result * 0x1000000058B90BFCDEE5ACD3C1CEDC823 >> 128; + if (x & 0x400000000 > 0) + result = result * 0x100000002C5C85FE31F35A6A30DA1BE50 >> 128; + if (x & 0x200000000 > 0) + result = result * 0x10000000162E42FF0999CE3541B9FFFCF >> 128; + if (x & 0x100000000 > 0) + result = result * 0x100000000B17217F80F4EF5AADDA45554 >> 128; + if (x & 0x80000000 > 0) + result = result * 0x10000000058B90BFBF8479BD5A81B51AD >> 128; + if (x & 0x40000000 > 0) + result = result * 0x1000000002C5C85FDF84BD62AE30A74CC >> 128; + if (x & 0x20000000 > 0) + result = result * 0x100000000162E42FEFB2FED257559BDAA >> 128; + if (x & 0x10000000 > 0) + result = result * 0x1000000000B17217F7D5A7716BBA4A9AE >> 128; + if (x & 0x8000000 > 0) + result = result * 0x100000000058B90BFBE9DDBAC5E109CCE >> 128; + if (x & 0x4000000 > 0) + result = result * 0x10000000002C5C85FDF4B15DE6F17EB0D >> 128; + if (x & 0x2000000 > 0) + result = result * 0x1000000000162E42FEFA494F1478FDE05 >> 128; + if (x & 0x1000000 > 0) + result = result * 0x10000000000B17217F7D20CF927C8E94C >> 128; + if (x & 0x800000 > 0) + result = result * 0x1000000000058B90BFBE8F71CB4E4B33D >> 128; + if (x & 0x400000 > 0) + result = result * 0x100000000002C5C85FDF477B662B26945 >> 128; + if (x & 0x200000 > 0) + result = result * 0x10000000000162E42FEFA3AE53369388C >> 128; + if (x & 0x100000 > 0) + result = result * 0x100000000000B17217F7D1D351A389D40 >> 128; + if (x & 0x80000 > 0) + result = result * 0x10000000000058B90BFBE8E8B2D3D4EDE >> 128; + if (x & 0x40000 > 0) + result = result * 0x1000000000002C5C85FDF4741BEA6E77E >> 128; + if (x & 0x20000 > 0) + result = result * 0x100000000000162E42FEFA39FE95583C2 >> 128; + if (x & 0x10000 > 0) + result = result * 0x1000000000000B17217F7D1CFB72B45E1 >> 128; + if (x & 0x8000 > 0) + result = result * 0x100000000000058B90BFBE8E7CC35C3F0 >> 128; + if (x & 0x4000 > 0) + result = result * 0x10000000000002C5C85FDF473E242EA38 >> 128; + if (x & 0x2000 > 0) + result = result * 0x1000000000000162E42FEFA39F02B772C >> 128; + if (x & 0x1000 > 0) + result = result * 0x10000000000000B17217F7D1CF7D83C1A >> 128; + if (x & 0x800 > 0) + result = result * 0x1000000000000058B90BFBE8E7BDCBE2E >> 128; + if (x & 0x400 > 0) + result = result * 0x100000000000002C5C85FDF473DEA871F >> 128; + if (x & 0x200 > 0) + result = result * 0x10000000000000162E42FEFA39EF44D91 >> 128; + if (x & 0x100 > 0) + result = result * 0x100000000000000B17217F7D1CF79E949 >> 128; + if (x & 0x80 > 0) + result = result * 0x10000000000000058B90BFBE8E7BCE544 >> 128; + if (x & 0x40 > 0) + result = result * 0x1000000000000002C5C85FDF473DE6ECA >> 128; + if (x & 0x20 > 0) + result = result * 0x100000000000000162E42FEFA39EF366F >> 128; + if (x & 0x10 > 0) + result = result * 0x1000000000000000B17217F7D1CF79AFA >> 128; + if (x & 0x8 > 0) + result = result * 0x100000000000000058B90BFBE8E7BCD6D >> 128; + if (x & 0x4 > 0) + result = result * 0x10000000000000002C5C85FDF473DE6B2 >> 128; + if (x & 0x2 > 0) + result = result * 0x1000000000000000162E42FEFA39EF358 >> 128; + if (x & 0x1 > 0) + result = result * 0x10000000000000000B17217F7D1CF79AB >> 128; + + result >>= uint256 (int256 (63 - (x >> 64))); + require (result <= uint256 (int256 (MAX_64x64))); + + return int128 (int256 (result)); + } + } + + /** + * Calculate natural exponent of x. Revert on overflow. + * + * @param x signed 64.64-bit fixed point number + * @return signed 64.64-bit fixed point number + */ + function exp (int128 x) internal pure returns (int128) { + unchecked { + require (x < 0x400000000000000000); // Overflow + + if (x < -0x400000000000000000) return 0; // Underflow + + return exp_2 ( + int128 (int256 (x) * 0x171547652B82FE1777D0FFDA0D23A7D12 >> 128)); + } + } + + /** + * Calculate x / y rounding towards zero, where x and y are unsigned 256-bit + * integer numbers. Revert on overflow or when y is zero. + * + * @param x unsigned 256-bit integer number + * @param y unsigned 256-bit integer number + * @return unsigned 64.64-bit fixed point number + */ + function divuu (uint256 x, uint256 y) private pure returns (uint128) { + unchecked { + require (y != 0); + + uint256 result; + + if (x <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF) + result = (x << 64) / y; + else { + uint256 msb = 192; + uint256 xc = x >> 192; + if (xc >= 0x100000000) { xc >>= 32; msb += 32; } + if (xc >= 0x10000) { xc >>= 16; msb += 16; } + if (xc >= 0x100) { xc >>= 8; msb += 8; } + if (xc >= 0x10) { xc >>= 4; msb += 4; } + if (xc >= 0x4) { xc >>= 2; msb += 2; } + if (xc >= 0x2) msb += 1; // No need to shift xc anymore + + result = (x << 255 - msb) / ((y - 1 >> msb - 191) + 1); + require (result <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + + uint256 hi = result * (y >> 128); + uint256 lo = result * (y & 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + + uint256 xh = x >> 192; + uint256 xl = x << 64; + + if (xl < lo) xh -= 1; + xl -= lo; // We rely on overflow behavior here + lo = hi << 128; + if (xl < lo) xh -= 1; + xl -= lo; // We rely on overflow behavior here + + assert (xh == hi >> 128); + + result += xl / y; + } + + require (result <= 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + return uint128 (result); + } + } + + /** + * Calculate sqrt (x) rounding down, where x is unsigned 256-bit integer + * number. + * + * @param x unsigned 256-bit integer number + * @return unsigned 128-bit integer number + */ + function sqrtu (uint256 x) private pure returns (uint128) { + unchecked { + if (x == 0) return 0; + else { + uint256 xx = x; + uint256 r = 1; + if (xx >= 0x100000000000000000000000000000000) { xx >>= 128; r <<= 64; } + if (xx >= 0x10000000000000000) { xx >>= 64; r <<= 32; } + if (xx >= 0x100000000) { xx >>= 32; r <<= 16; } + if (xx >= 0x10000) { xx >>= 16; r <<= 8; } + if (xx >= 0x100) { xx >>= 8; r <<= 4; } + if (xx >= 0x10) { xx >>= 4; r <<= 2; } + if (xx >= 0x4) { r <<= 1; } + r = (r + x / r) >> 1; + r = (r + x / r) >> 1; + r = (r + x / r) >> 1; + r = (r + x / r) >> 1; + r = (r + x / r) >> 1; + r = (r + x / r) >> 1; + r = (r + x / r) >> 1; // Seven iterations should be enough + uint256 r1 = x / r; + return uint128 (r < r1 ? r : r1); + } + } + } +}