erc20-demurrage-token

ERC20 token with redistributed continual demurrage
Log | Files | Refs | README

commit acd5eb3d7befce2126049282fb963c87c0ae4444
parent 3ebb645077c41249f5d8df9a1913841e2d404dda
Author: lash <dev@holbrook.no>
Date:   Wed, 22 Mar 2023 10:17:40 +0000

Merge branch lash/readmemore

Diffstat:
MREADME.md | 144++++++++++++++++++++++++++++++++++++++++++++++++++-----------------------------
Mpython/CHANGELOG | 16++++++++++++++++
Mpython/erc20_demurrage_token/__init__.py | 4++++
Apython/erc20_demurrage_token/data/.chainlib | 0
Mpython/erc20_demurrage_token/data/DemurrageTokenSingleNocap.bin | 4++--
Mpython/erc20_demurrage_token/data/DemurrageTokenSingleNocap.json | 2+-
Apython/erc20_demurrage_token/data/DemurrageTokenSingleNocap.metadata.json | 1+
Apython/erc20_demurrage_token/expiry.py | 36++++++++++++++++++++++++++++++++++++
Apython/erc20_demurrage_token/runnable/#publish.py# | 176+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apython/erc20_demurrage_token/runnable/.#publish.py | 2++
Dpython/erc20_demurrage_token/runnable/apply.py | 152-------------------------------------------------------------------------------
Dpython/erc20_demurrage_token/runnable/deploy.py | 126-------------------------------------------------------------------------------
Apython/erc20_demurrage_token/runnable/publish.py | 176+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Apython/erc20_demurrage_token/seal.py | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpython/erc20_demurrage_token/token.py | 290++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------
Mpython/erc20_demurrage_token/unittest/base.py | 192+++++++++++++++++++------------------------------------------------------------
Apython/erc20_demurrage_token/unittest/newbase.py | 146+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpython/requirements.txt | 6++++--
Mpython/run_tests.sh | 48+++++++++---------------------------------------
Mpython/setup.cfg | 5++---
Rpython/tests/bench.py -> python/tests/bench.old | 0
Apython/tests/bench_gas.py | 167+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpython/tests/test_amounts.py | 62+++++++++++++++++++++++++++++++++++++++++++-------------------
Mpython/tests/test_basic.py | 53+++++++++++++++++++----------------------------------
Mpython/tests/test_burn.py | 66++++++++++++++++++++++++++++++------------------------------------
Mpython/tests/test_cap.py | 31+++++++++++++++----------------
Dpython/tests/test_demurrage.py | 133-------------------------------------------------------------------------------
Apython/tests/test_demurrage.py.old | 114+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dpython/tests/test_demurrage_ext.py | 41-----------------------------------------
Apython/tests/test_expiry.py | 98+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpython/tests/test_growth.py | 42+++++++++++++++++++-----------------------
Apython/tests/test_mint.py | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpython/tests/test_period.py | 79++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
Dpython/tests/test_redistribution.py | 328-------------------------------------------------------------------------------
Mpython/tests/test_redistribution_single.py | 23+++++++++++++++--------
Mpython/tests/test_redistribution_unit.py | 29+++++++++++++++++------------
Dpython/tests/test_remainder.py | 70----------------------------------------------------------------------
Apython/tests/test_seal.py | 195+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mpython/tests/test_single.py | 9++++-----
Dsolidity/DemurrageTokenMultiCap.sol | 633-------------------------------------------------------------------------------
Dsolidity/DemurrageTokenMultiNocap.sol | 614-------------------------------------------------------------------------------
Dsolidity/DemurrageTokenSingleCap.sol | 517-------------------------------------------------------------------------------
Msolidity/DemurrageTokenSingleNocap.sol | 418+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------
Msolidity/Makefile | 33+++++++--------------------------
Asolidity/aux/ABDKMath64x64.sol | 752+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asolidity/aux/LICENSE.md | 28++++++++++++++++++++++++++++
46 files changed, 2911 insertions(+), 3293 deletions(-)

diff --git a/README.md b/README.md @@ -1,22 +1,20 @@ # RedistributedDemurrageToken ## Use Case -* Network / Basic Income Token - * 100 Sarafu is distributed to anyone in Kenya after user validation by the owner of a faucet which mints new Sarafu. - * Validated users are those that validate their phone number in Kenya. - * A Sarafu holding tax aka ([demurrage](https://en.wikipedia.org/wiki/Demurrage_(currency))) of 0.000050105908373373% is charged from users per minute - such that over 1 month to total tax would be 2%. - * After 1 week the total amount tax is distributed evenly out to _active_ users. - * any single transaction by a user within that week is considered _active_ (heartbeat) - * This is meant to result in a disincentivization to hold (hodl) the Sarafu token and increase its usage as a medium of exchange rather than a store of value. - * This token can be added to liquidity pools with other ERC20 tokens and or Community Inclusion Currencies (CICs) - and thereby act as a central network token and connect various tokens and CICs together. +* Vouchers + * A Publisher may publish a RedistributedDemurrageToken (Voucher) representing a credit obligation of an Issuer or Association of Issuers that can be redeemed as payment for the products of the Issuer. The Issuer is the entity legally obligated to redeem the voucher as payment. + * Decay: The Publisher can specify an decay rate such as 2% as well as a redistribution period. After the redistribution period such as a month. Assuming an account holder has not had any transfers they will have a new balance of their original balance*2%. Note that the numeric decay will happen continuously by the minute. + * Redistribution: The missing (demurraged) balances will be added to the balance of the SINK address. So once a redistribution period (e.g. once a month) the total supply of all holders including the SINK will return to the minted supply. + * This is meant to result as a disincentivization to hold (hodl) the Voucher without causing price inflation, as the total supply is stable. * Example - - With a demurrage of 2% (net per month) and a reward period of 1 month - If there are 10 users all with balances of 1000 Sarafu and only 2 of them trade that month (assume they trade back and forth with no net balance change). - - Then the resulting balances after one tax period of those two trading would be 1080 Sarafu while the remaining non-active users would be 980 Sarafu. If this behaviour continued in the next tax period, with the same two users only trading (with no net balance changes), they would have 1158.39999968 Sarafu and those users that are not trading would have their balances further reduced to 960.40 Sarafu. If this continued on ~forever those two active trading users would have the entire token supply and the non-trading users would eventually reach a zero balance. - - this example calculation for 3 tax periods can be found here: https://gitlab.com/grassrootseconomics/cic-docs/-/blob/master/demurrage-redist-sarafu.ods + - With a demurrage of 2% (and redistribution period of 1 month) - If there are 10 users all with balances of 100 Vouchers (and only 2 of them trade that month (assume they trade back and forth with no net balance change)). + - Then the resulting balances after one redistribution period of ALL users (regardless of their trading) would be 98 Vouchers and 20 Voucher would be the balance of the SINK address. Assuming the SINK address is redistributed (as a Community Fund) back to users, it’s balance would again reach 20 the next redistribution period. + - Note that after the redistribution the total of all balances will equal the total minted amount. + - Note that all accounts holding such Vouchers are effected by demurrage. ## Nomenclature -* `Demurrage` aka Decay amount: A percentage of token supply that will be charged once per minute and evenly redistributed to _active_ users every Demurrage Period (minutes) +* `Demurrage` aka Decay amount: A percentage of token supply that will gradually be removed over a redstribution period and then redistributed to the SINK account. * Base balance: The inflated balance of each user is stored for bookkeeping. * Sink Token Address: Rounding errors and if no one trades the tax goes to this address * Demurrage Period (minutes)- aka `period`: The number of minutes over which a user must be _active_ to receive tax-redistibution. @@ -30,63 +28,103 @@ ## Mint -* Owner can add minters and remove - - A faucet contract would be a minter and choose the amount of tokens to mint and distribute to new _validated_ users. - - The interface says the amount and is at the caller's discretion per contract call. _validation_ is outside of this contract. -* A minter can remove itself -* Minters can mint any amount +* Minters are called writers. Contract owner can add and remove writers. +* A writer can remove itself +* The interface says the amount and is at the caller's discretion per contract call. _validation_ is outside of this contract. +* Writers can mint any amount. If supply cap is set, minting will be limited to this cap. -## Demurrage -* Holding Tax (`demurrage`) is applied when a **mint** or **transfer**; (it can also be triggered explicitly) - - Note that the token supply _stays the same_ but a virtual _balance output_ is created. - - Updates `demurrageModifier` which represents the accumulated tax value and is an exponential decay step (of size `demurrage`) for each minute that has passed. - - `demurrageModifier = (1-demurrage)^(minute_passed)` - - e.g. a `demurrage` of 2% after the 1st minute would be give a `demurrageModifier = (1-0.02)^1 = 0.98`. - - e.g. a `demurrage` after the 2nd minute would be give a `demurrageModifier = (1-0.02)^2 = 0.9604`. -* All client-facing values (_balance output_ , _transfer inputs_) are adjusted with `demurrageModifier`. - - e.g. `_balance output_ = user_balance - user_balance * demurrageModifier` +## Input parameters + +The redistrbution period is passed to the contract in minutes. E.g. a redistribution period of one month would be approximately 43200 minutes. + +The demurrage level specified as the percentage of continuous growth per minute: + +`(1 - percentage) ^ (1 / period)` + +E.g. A demurrage of 2% monthly would be defined as: + +`(1 - 0.02) ^ (1 / 43200) ~ 0.99999953234484737109` + +The number must be provided to the contract as a 64x64 bit fixed-point number (where the integer part is 0). + +A script is included in the python package to publish the contract which takes the input as a percentage as parts-per-million and converts the correct input argument for the contract. The calculation can be found in the function `process_config_local` in `python/erc20_demurrage_token/runnable/publish.py`. It uses the python module [dexif](https://pypi.org/project/dexif/) to perform the fixed-point conversion. + + +## Demurrage calculation + +The demurrage calculation inside the contract is done by the following formula, where `demurrageLevel` is the demurrage level input parameter of the contract: + +`newDemurrageModifier = currentDemurrageModifier * (e ^ (ln(demurrageLevel) * minutes))` + +Holding Tax (`demurrage`) is applied when a **mint** or **transfer**; (it can also be triggered explicitly) +- Note that the token supply _stays the same_ but a virtual _balance output_ is created. +- Updates `demurrageModifier` which represents the accumulated tax value and is an exponential decay step (of size `demurrage`) for each minute that has passed. + + +All client-facing values (_balance output_ , _transfer inputs_) are adjusted with `demurrageModifier`. + +e.g. `_balance output_ = user_balance - user_balance * demurrageModifier` ## Redistribution * One redistribution entry is added to storage for each `period`; - - When `mint` is triggered, the new totalsupply is stored to the entry - - When `transfer` is triggered, and the account did not yet participate in the `period`, the entry's participant count is incremented. -* Account must have "participated" in a period to be redistribution beneficiary. -* Redistribution is applied when an account triggers a **transfer** for the first time in a new `period`; - - Check if user has participated in `period`. (_active_ user heartbeat) - - Each _active_ user balance in the `period` is increased by `(total supply at end of period * demurrageModifier ) / number_of_active_participants` via minting - - Participation field is zeroed out for that user. -* Fractions must be rounded down - - Remainder is "dust" and should be sent to a dedicated Sink Token Address. - - If no one is _active_ all taxes go to the Sink Token Address. +* When `mint` is triggered, the new totalsupply is stored to the entry +* When `transfer` is triggered, and the account did not yet participate in the `period`, the entry's participant count is incremented. +* Redistributed tokens are added to the balance of the _sink address_ given when the contract is published. +* _sink address_ may be changed. + + +## Data representation +Token parameters are truncated when calculating demurrage and redistribution: -## Data structures +* Redistribution period: 32 bits +* Token supply: 72 bits +* Demurrage modifier: 64 bits -* One word per `account`: - - bits 000-071: value - - bits 072-103: period - - bits 104-255: (Unused) -* One word per `redistributions` period: - - bits 000-031: period - - bits 032-103: supply - - bits 104-139: participant count - - bits 140-159: demurrage modifier - - bits 160-254: (Unused) - - bits 255: Set if individual redistribution amounts are fractions -### Notes +## Expiration -Accumulated demurrage modifier in `demurrageModifier` is 128 bit, but will be _truncated_ do 20 bits in `redistributions`. The 128 bit resolution is to used to reduce the impact of fractional drift of the long-term accumulation of the demurrage modifier. However, the demurrage snapshot values used in `redistributions` are parts-per-million and can be fully contained within a 20-bit value. +A token may set to expire at a certain point in time. After the expiry, no more transfers may be executed. From that point on, balances are frozen and demurrage is halted. + +Expiration may be set in terms of redistribution periods. + +Unless sealed (see below), expiration may be changed at any time to any future redistribution period. However, once expired, expiration may not be changed further. + + +## Supply + +Unless sealed (see below), Supply limit may be set and change at any time. Supply may never be directly set to less than the current supply. However, contract _writers_ may burn tokens in their possession using the `burn()` method, which will effectively reduce the supply. + + +## Mutability + +The following parameters may not be changed after contract is published: + +* Demurrage level +* Redistribution period + +The contract provides a sealing feature which prohibits further changes to parameters that can initially be edited. These include: + +* Adding and removing writers (addresses that may mint tokens) +* Sink addres +* Expiry period +* Supply limit + + +## Gas usage + +The token contract uses the [ADBKMath](https://github.com/abdk-consulting/abdk-libraries-solidity/blob/master/ABDKMath64x64.sol) library to calculate exponentials. + +Gas usage is constant regardless of the amount of time passed between each execution of demurrage and redistribution period calculations. ## QA -* Basic python tests in place -* How to determine and generate sufficient test vectors, and how to adapt them to scripts. -* Audit sources? +* Tests are implemented using the `chaintool` python package suite. + ## Known issues diff --git a/python/CHANGELOG b/python/CHANGELOG @@ -1,3 +1,19 @@ +- 0.4.2 + * Correct burn interface implementation +- 0.4.1 + * Eliminate solidity compiler warnings +- 0.4.0 + * Rename taxLevel to decayLevel in contract + * Add sweep contract method to fully empty one account into another +- 0.3.6 + * Reinstate owner as minter by default +- 0.3.0 + * Smart contracts use abdk math libraries, all exponential operations are static gas cost + * Add expiry features, after which balances are frozen and no more transfers or demurrage will occur + * Add sealable features for supply, sink address, expiry and minters (when sealed cannot be changed) + * Deployer script now takes demurrage amount as ppm instead of literal growth fraction + * Retire old multi and cap contracts + * Replace contract bitfields for redistributions with structs - 0.2.0 * Add token burn function * Fix gas leak when calculating decay on period change diff --git a/python/erc20_demurrage_token/__init__.py b/python/erc20_demurrage_token/__init__.py @@ -1,4 +1,8 @@ from .token import ( DemurrageToken, DemurrageTokenSettings, + DemurrageRedistribution, ) +from .token import create +from .token import bytecode +from .token import args diff --git a/python/erc20_demurrage_token/data/.chainlib b/python/erc20_demurrage_token/data/.chainlib diff --git a/python/erc20_demurrage_token/data/DemurrageTokenSingleNocap.bin b/python/erc20_demurrage_token/data/DemurrageTokenSingleNocap.bin @@ -1 +1 @@ -61010060405264e8d4a510006a52b7d2dcc80cd2e400000062000023919062000349565b6080908152503480156200003657600080fd5b5060405162003df438038062003df483398181016040528101906200005c919062000648565b33600460006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506001600d6000600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff021916908315150217905550856006908162000128919062000969565b5084600790816200013a919062000969565b508360ff166008819055504260038190555060035460a08181525050603c8262000165919062000349565b60c0818152505060646a52b7d2dcc80cd2e400000062000186919062000a50565b600260006101000a8154816fffffffffffffffffffffffffffffffff02191690836fffffffffffffffffffffffffffffffff160217905550826fffffffffffffffffffffffffffffffff1660e0818152505060006200022a6000600260009054906101000a90046fffffffffffffffffffffffffffffffff166fffffffffffffffffffffffffffffffff1660006001620002a3640100000000026401000000009004565b9050600081908060018154018082558091505060019003906000526020600020016000909190919091505581600f60006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505050505050505062000a96565b6000807affffffffffffffffffffffffffff00000000000000000000000000606860ff16869060020a0216600102811790506cffffffffffffffffff00000000602060ff16859060020a02166001028117905063ffffffff83166001028117905080915050949350505050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000620003568262000310565b9150620003638362000310565b9250828202620003738162000310565b915082820484148315176200038d576200038c6200031a565b5b5092915050565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b620003fd82620003b2565b810181811067ffffffffffffffff821117156200041f576200041e620003c3565b5b80604052505050565b60006200043462000394565b9050620004428282620003f2565b919050565b600067ffffffffffffffff821115620004655762000464620003c3565b5b6200047082620003b2565b9050602081019050919050565b60005b838110156200049d57808201518184015260208101905062000480565b60008484015250505050565b6000620004c0620004ba8462000447565b62000428565b905082815260208101848484011115620004df57620004de620003ad565b5b620004ec8482856200047d565b509392505050565b600082601f8301126200050c576200050b620003a8565b5b81516200051e848260208601620004a9565b91505092915050565b600060ff82169050919050565b6200053f8162000527565b81146200054b57600080fd5b50565b6000815190506200055f8162000534565b92915050565b60006fffffffffffffffffffffffffffffffff82169050919050565b6200058c8162000565565b81146200059857600080fd5b50565b600081519050620005ac8162000581565b92915050565b620005bd8162000310565b8114620005c957600080fd5b50565b600081519050620005dd81620005b2565b92915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006200061082620005e3565b9050919050565b620006228162000603565b81146200062e57600080fd5b50565b600081519050620006428162000617565b92915050565b60008060008060008060c087890312156200066857620006676200039e565b5b600087015167ffffffffffffffff811115620006895762000688620003a3565b5b6200069789828a01620004f4565b965050602087015167ffffffffffffffff811115620006bb57620006ba620003a3565b5b620006c989828a01620004f4565b9550506040620006dc89828a016200054e565b9450506060620006ef89828a016200059b565b93505060806200070289828a01620005cc565b92505060a06200071589828a0162000631565b9150509295509295509295565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806200077557607f821691505b6020821081036200078b576200078a6200072d565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b60008160020a8302905092915050565b600060088302620007f87fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82620007b6565b620008048683620007b6565b95508019841693508086168417925050509392505050565b6000819050919050565b600062000847620008416200083b8462000310565b6200081c565b62000310565b9050919050565b6000819050919050565b620008638362000826565b6200087b62000872826200084e565b848454620007c6565b825550505050565b600090565b6200089262000883565b6200089f81848462000858565b505050565b5b81811015620008c757620008bb60008262000888565b600181019050620008a5565b5050565b601f8211156200091657620008e08162000791565b620008eb84620007a6565b81016020851015620008fb578190505b620009136200090a85620007a6565b830182620008a4565b50505b505050565b60008160020a8304905092915050565b60006200093e600019846008026200091b565b1980831691505092915050565b60006200095983836200092b565b9150826002028217905092915050565b620009748262000722565b67ffffffffffffffff81111562000990576200098f620003c3565b5b6200099c82546200075c565b620009a9828285620008cb565b600060209050601f831160018114620009e15760008415620009cc578287015190505b620009d885826200094b565b86555062000a48565b601f198416620009f18662000791565b60005b8281101562000a1b57848901518255600182019150602085019450602081019050620009f4565b8683101562000a3b578489015162000a37601f8916826200092b565b8355505b6001600288020188555050505b505050505050565b600062000a5d8262000565565b915062000a6a8362000565565b925082820262000a7a8162000565565b915080821462000a8f5762000a8e6200031a565b5b5092915050565b60805160a05160c05160e0516132de62000b1660003960008181610eb70152818161119a015261235c0152600081816116ae01528181611caa015281816121c301526123fb0152600081816116da0152818161241c01526124610152600081816117470152818161177f015281816117f7015261219f01526132de6000f3fe608060405234801561001057600080fd5b50600436106102d8576000357c010000000000000000000000000000000000000000000000000000000090048063731f237c1161019f578063a457c2d711610106578063d89135cd116100bf578063e809529511610099578063e8095295146109cf578063eda4e6d6146109ed578063f2fde38b14610a0b578063f83da35d14610a3b576102d8565b8063d89135cd14610951578063e3ab207a1461096f578063e54d63f71461099f576102d8565b8063a457c2d714610867578063a9059cbb14610897578063ab28cea0146108c7578063b470aade146108e5578063c0ab707714610903578063d340ef8a14610933576102d8565b80638da5cb5b116101585780638da5cb5b146107915780638f0b2d5d146107af5780638f1df6bc146107cb57806395d89b41146107e9578063983b2d56146108075780639da7d8ba14610837576102d8565b8063731f237c146106cb57806373f42561146106e957806374bedb951461070757806379ba50971461072557806384dde4af146107435780638d34855a14610761576102d8565b806340452d911161024357806358b2833b116101fc57806358b2833b146105bd5780636721d4ef146105db5780636787a9be1461060b5780636a2d094e1461063b5780636ffa8a051461066b57806370a082311461069b576102d8565b806340452d91146104b157806342966c68146104e1578063449a52f8146104fd57806347a505171461052d5780634abfbba21461055d57806351b4541c1461058d576102d8565b8063229ba19711610295578063229ba197146103b557806323b872dd146103d35780633092afd5146104035780633133348714610433578063313ce567146104635780633950935114610481576102d8565b806301ffc9a7146102dd57806306fdde031461030d578063095ea7b31461032b57806309f28f3c1461035b57806318160ddd146103795780631989c6a814610397575b600080fd5b6102f760048036038101906102f29190612a04565b610a6b565b6040516103049190612a4c565b60405180910390f35b610315610c11565b6040516103229190612af7565b60405180910390f35b61034560048036038101906103409190612bad565b610c9f565b6040516103529190612a4c565b60405180910390f35b610363610e70565b6040516103709190612c18565b60405180910390f35b610381610e92565b60405161038e9190612c42565b60405180910390f35b61039f610ea9565b6040516103ac9190612c42565b60405180910390f35b6103bd610eb5565b6040516103ca9190612c42565b60405180910390f35b6103ed60048036038101906103e89190612c5d565b610ed9565b6040516103fa9190612a4c565b60405180910390f35b61041d60048036038101906104189190612cb0565b61108c565b60405161042a9190612a4c565b60405180910390f35b61044d60048036038101906104489190612cdd565b61117f565b60405161045a9190612c42565b60405180910390f35b61046b611233565b6040516104789190612c42565b60405180910390f35b61049b60048036038101906104969190612bad565b611239565b6040516104a89190612a4c565b60405180910390f35b6104cb60048036038101906104c69190612d1d565b6113cf565b6040516104d89190612d63565b60405180910390f35b6104fb60048036038101906104f69190612d1d565b6113f3565b005b61051760048036038101906105129190612bad565b611521565b6040516105249190612a4c565b60405180910390f35b61054760048036038101906105429190612cb0565b611661565b6040516105549190612c42565b60405180910390f35b61057760048036038101906105729190612d1d565b6116aa565b6040516105849190612c42565b60405180910390f35b6105a760048036038101906105a29190612d1d565b61170a565b6040516105b49190612c42565b60405180910390f35b6105c561172b565b6040516105d29190612c42565b60405180910390f35b6105f560048036038101906105f09190612cdd565b611731565b6040516106029190612c42565b60405180910390f35b61062560048036038101906106209190612d1d565b6117b2565b6040516106329190612c42565b60405180910390f35b61065560048036038101906106509190612d1d565b611832565b6040516106629190612c42565b60405180910390f35b61068560048036038101906106809190612daa565b611853565b6040516106929190612c42565b60405180910390f35b6106b560048036038101906106b09190612cb0565b61188d565b6040516106c29190612c42565b60405180910390f35b6106d3611945565b6040516106e09190612a4c565b60405180910390f35b6106f1611956565b6040516106fe9190612c42565b60405180910390f35b61070f61195c565b60405161071c9190612c42565b60405180910390f35b61072d611962565b60405161073a9190612a4c565b60405180910390f35b61074b611b09565b6040516107589190612de6565b60405180910390f35b61077b60048036038101906107769190612daa565b611b2f565b6040516107889190612c42565b60405180910390f35b610799611b5d565b6040516107a69190612de6565b60405180910390f35b6107c960048036038101906107c49190612cb0565b611b83565b005b6107d3611c21565b6040516107e09190612a4c565b60405180910390f35b6107f1611d71565b6040516107fe9190612af7565b60405180910390f35b610821600480360381019061081c9190612cb0565b611dff565b60405161082e9190612a4c565b60405180910390f35b610851600480360381019061084c9190612daa565b611ebc565b60405161085e9190612c42565b60405180910390f35b610881600480360381019061087c9190612bad565b611ee8565b60405161088e9190612a4c565b60405180910390f35b6108b160048036038101906108ac9190612bad565b612107565b6040516108be9190612a4c565b60405180910390f35b6108cf61219d565b6040516108dc9190612c42565b60405180910390f35b6108ed6121c1565b6040516108fa9190612c42565b60405180910390f35b61091d60048036038101906109189190612d1d565b6121e5565b60405161092a9190612a4c565b60405180910390f35b61093b61231d565b6040516109489190612c42565b60405180910390f35b610959612323565b6040516109669190612c42565b60405180910390f35b61098960048036038101906109849190612daa565b61232d565b6040516109969190612c42565b60405180910390f35b6109b960048036038101906109b49190612cdd565b612341565b6040516109c69190612c42565b60405180910390f35b6109d76123f5565b6040516109e49190612c18565b60405180910390f35b6109f561245f565b604051610a029190612c42565b60405180910390f35b610a256004803603810190610a209190612cb0565b612483565b604051610a329190612a4c565b60405180910390f35b610a556004803603810190610a509190612e01565b612525565b604051610a629190612d63565b60405180910390f35b600063c6bb4b707c010000000000000000000000000000000000000000000000000000000002827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff191603610abf5760019050610c0c565b63449a52f87c010000000000000000000000000000000000000000000000000000000002827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff191603610b115760019050610c0c565b6301ffc9a77c010000000000000000000000000000000000000000000000000000000002827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff191603610b635760019050610c0c565b639493f8b27c010000000000000000000000000000000000000000000000000000000002827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff191603610bb55760019050610c0c565b6337a47be47c010000000000000000000000000000000000000000000000000000000002827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff191603610c075760019050610c0c565b600090505b919050565b60068054610c1e90612e97565b80601f0160208091040260200160405190810160405280929190818152602001828054610c4a90612e97565b8015610c975780601f10610c6c57610100808354040283529160200191610c97565b820191906000526020600020905b815481529060010190602001808311610c7a57829003601f168201915b505050505081565b6000806000600e60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541115610d6b5760008314610d6a576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610d6190612f14565b60405180910390fd5b5b610d73611c21565b50610d7d836117b2565b905080600e60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508373ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b92585604051610e5d9190612c42565b60405180910390a3600191505092915050565b600260009054906101000a90046fffffffffffffffffffffffffffffffff1681565b6000600c54600954610ea49190612f63565b905090565b60008080549050905090565b7f000000000000000000000000000000000000000000000000000000000000000081565b6000806000610ee6611c21565b50610ef0846117b2565b915081600e60008873ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015610f7b57600080fd5b81600e60008873ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282546110079190612f63565b92505081905550611019868684612592565b90508473ffffffffffffffffffffffffffffffffffffffff168673ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef866040516110789190612c42565b60405180910390a380925050509392505050565b6000600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16148061111557503373ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16145b61111e57600080fd5b6000600d60008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff02191690831515021790555060019050919050565b600080600064e8d4a5100091506a52b7d2dcc80cd2e40000007f00000000000000000000000000000000000000000000000000000000000000006111c39190612fc6565b905060005b8481101561120c5764e8d4a5100082846111e29190612ff7565b6111ec9190612fc6565b836111f79190612f63565b9250808061120490613039565b9150506111c8565b5064e8d4a51000858361121f9190612ff7565b6112299190612fc6565b9250505092915050565b60085481565b600080611244611c21565b5061124e836117b2565b905080600e60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282546112dc9190613081565b925050819055508373ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925600e60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008873ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020546040516113bc9190612c42565b60405180910390a3600191505092915050565b600081815481106113df57600080fd5b906000526020600020016000915090505481565b600d60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff1661144957600080fd5b600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205481111561149557600080fd5b60006114a0826117b2565b90506114aa611945565b506114b533826125b7565b5081600c60008282546114c89190613081565b925050819055503373ffffffffffffffffffffffffffffffffffffffff167fcc16f5dbb4873280815c1ee09dbd06736cffcc184412cf7a71a0fdb75d397ca5836040516115159190612c42565b60405180910390a25050565b600080600d60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff166115b0576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016115a790613101565b60405180910390fd5b6115b8611c21565b506115c2836117b2565b905082600960008282546115d69190613081565b925050819055506115e784826126be565b508373ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fab8530f87dc9b59234c4623bf917212bb2536d647574c8e7e5da92c2ede0c9f8856040516116459190612c42565b60405180910390a3611655612782565b50600191505092915050565b6000600160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b60007f0000000000000000000000000000000000000000000000000000000000000000826116d89190612ff7565b7f00000000000000000000000000000000000000000000000000000000000000006117039190613081565b9050919050565b6000603c824261171a9190612f63565b6117249190612fc6565b9050919050565b60035481565b6000806402540be400836117459190612ff7565b7f00000000000000000000000000000000000000000000000000000000000000006117709190612f63565b8461177b9190612ff7565b90507f0000000000000000000000000000000000000000000000000000000000000000816117a99190612fc6565b91505092915050565b60006402540be400600260009054906101000a90046fffffffffffffffffffffffffffffffff166117e39190613121565b6fffffffffffffffffffffffffffffffff167f0000000000000000000000000000000000000000000000000000000000000000836118219190612ff7565b61182b9190612fc6565b9050919050565b6000603c82426118429190612f63565b61184c9190612fc6565b9050919050565b6000606860ff167affffffffffffffffffffffffffff00000000000000000000000000836001900416908060020a82049150509050919050565b60008060008061189c85611661565b92506118a960035461170a565b90506118f56402540be400600260009054906101000a90046fffffffffffffffffffffffffffffffff166118dd9190613121565b6fffffffffffffffffffffffffffffffff168261117f565b6fffffffffffffffffffffffffffffffff16915064e8d4a510006a52b7d2dcc80cd2e40000006119259190612ff7565b82846119319190612ff7565b61193b9190612fc6565b9350505050919050565b600061195160006121e5565b905090565b600c5481565b600b5481565b600080600560009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146119bf57600080fd5b600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050600560009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16600460006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000600560006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a35090565b600f60009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000806000611b3d84611ebc565b9150611b4884611853565b9050611b548282611731565b92505050919050565b600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614611bdd57600080fd5b80600f60006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b6000806000806000806000806000611c37611945565b50611c40612829565b975060006001028803611c5e57600098505050505050505050611d6e565b6000600a5481548110611c7457611c7361315e565b5b90600052602060002001549350611c8a8861232d565b9550600186611c999190613081565b9050611ca484611853565b9450603c7f0000000000000000000000000000000000000000000000000000000000000000611cd39190612fc6565b9150611cdf858361117f565b9250611cf5600084611cef610e92565b84612525565b96506000879080600181540180825580915050600190039060005260206000200160009091909190915055611d2987612915565b507f55d243082e019fce4009ccea5368b92e436c17586a1e793c7deda16df4e5d67581604051611d599190612c42565b60405180910390a16001985050505050505050505b90565b60078054611d7e90612e97565b80601f0160208091040260200160405190810160405280929190818152602001828054611daa90612e97565b8015611df75780601f10611dcc57610100808354040283529160200191611df7565b820191906000526020600020905b815481529060010190602001808311611dda57829003601f168201915b505050505081565b6000600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614611e5b57600080fd5b6001600d60008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff02191690831515021790555060019050919050565b6000602060ff166cffffffffffffffffff00000000836001900416908060020a82049150509050919050565b600080611ef4836117b2565b905080600e60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015611f7f57600080fd5b611f87611c21565b5080600e60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282546120149190612f63565b925050819055508373ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925600e60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008873ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020546040516120f49190612c42565b60405180910390a3600191505092915050565b6000806000612114611c21565b5061211e846117b2565b915061212b338684612592565b90508473ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef8660405161218a9190612c42565b60405180910390a3809250505092915050565b7f000000000000000000000000000000000000000000000000000000000000000081565b7f000000000000000000000000000000000000000000000000000000000000000081565b60008060006121f560035461170a565b91506000820361220a57600092505050612318565b600260009054906101000a90046fffffffffffffffffffffffffffffffff166fffffffffffffffffffffffffffffffff16905060008411801561224c57508184105b15612255578391505b61225f818361117f565b600260006101000a8154816fffffffffffffffffffffffffffffffff02191690836fffffffffffffffffffffffffffffffff160217905550603c826122a49190612ff7565b6003546122b19190613081565b60038190555080826003547fa0717e54e02bd9829db5e6e998aec0ae9de796b8d150a3cc46a92ab869697755600260009054906101000a90046fffffffffffffffffffffffffffffffff1660405161230991906131c8565b60405180910390a46001925050505b919050565b600a5481565b6000600c54905090565b600063ffffffff8260019004169050919050565b600080600064e8d4a5100091506a52b7d2dcc80cd2e40000007f00000000000000000000000000000000000000000000000000000000000000006123859190612fc6565b905060005b848110156123ce5764e8d4a5100082846123a49190612ff7565b6123ae9190612fc6565b836123b99190613081565b925080806123c690613039565b91505061238a565b5064e8d4a5100085836123e19190612ff7565b6123eb9190612fc6565b9250505092915050565b600060017f00000000000000000000000000000000000000000000000000000000000000007f0000000000000000000000000000000000000000000000000000000000000000426124469190612f63565b6124509190612fc6565b61245a9190613081565b905090565b7f000000000000000000000000000000000000000000000000000000000000000081565b6000600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16146124df57600080fd5b81600560006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550919050565b6000807affffffffffffffffffffffffffff00000000000000000000000000606860ff16869060020a0216600102811790506cffffffffffffffffff00000000602060ff16859060020a02166001028117905063ffffffff83166001028117905080915050949350505050565b60008061259f85846125b7565b506125aa84846126be565b5060019150509392505050565b600080600080600160008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490506000850361261357600093505050506126b8565b61261c86611661565b925084831015612661576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016126589061322f565b60405180910390fd5b848361266d9190612f63565b600160008873ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550600193505050505b92915050565b600080600080600160008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490506000850361271a576000935050505061277c565b61272386611661565b925084836127319190613081565b600160008873ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550600193505050505b92915050565b600080600061278f610e92565b9050600060016000805490506127a59190612f63565b815481106127b6576127b561315e565b5b90600052602060002001546001900491506cffffffffffffffffff000000001982169150602060ff16819060020a028217915081600102600060016000805490506128019190612f63565b815481106128125761281161315e565b5b906000526020600020018190555060019250505090565b600080600080600a54815481106128435761284261315e565b5b906000526020600020015491503073ffffffffffffffffffffffffffffffffffffffff1663e80952956040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381865afa1580156128b7573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906128db919061327b565b6fffffffffffffffffffffffffffffffff1690506128f88261232d565b811161290c57600060010292505050612912565b81925050505b90565b600080600061292384611b2f565b9150600b54612931836117b2565b61293b9190612f63565b9050612969600f60009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16826126be565b506001600a600082825461297d9190613081565b9250508190555080600b60008282546129969190613081565b925050819055508192505050919050565b600080fd5b60007fffffffff0000000000000000000000000000000000000000000000000000000082169050919050565b6129e1816129ac565b81146129ec57600080fd5b50565b6000813590506129fe816129d8565b92915050565b600060208284031215612a1a57612a196129a7565b5b6000612a28848285016129ef565b91505092915050565b60008115159050919050565b612a4681612a31565b82525050565b6000602082019050612a616000830184612a3d565b92915050565b600081519050919050565b600082825260208201905092915050565b60005b83811015612aa1578082015181840152602081019050612a86565b60008484015250505050565b6000601f19601f8301169050919050565b6000612ac982612a67565b612ad38185612a72565b9350612ae3818560208601612a83565b612aec81612aad565b840191505092915050565b60006020820190508181036000830152612b118184612abe565b905092915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000612b4482612b19565b9050919050565b612b5481612b39565b8114612b5f57600080fd5b50565b600081359050612b7181612b4b565b92915050565b6000819050919050565b612b8a81612b77565b8114612b9557600080fd5b50565b600081359050612ba781612b81565b92915050565b60008060408385031215612bc457612bc36129a7565b5b6000612bd285828601612b62565b9250506020612be385828601612b98565b9150509250929050565b60006fffffffffffffffffffffffffffffffff82169050919050565b612c1281612bed565b82525050565b6000602082019050612c2d6000830184612c09565b92915050565b612c3c81612b77565b82525050565b6000602082019050612c576000830184612c33565b92915050565b600080600060608486031215612c7657612c756129a7565b5b6000612c8486828701612b62565b9350506020612c9586828701612b62565b9250506040612ca686828701612b98565b9150509250925092565b600060208284031215612cc657612cc56129a7565b5b6000612cd484828501612b62565b91505092915050565b60008060408385031215612cf457612cf36129a7565b5b6000612d0285828601612b98565b9250506020612d1385828601612b98565b9150509250929050565b600060208284031215612d3357612d326129a7565b5b6000612d4184828501612b98565b91505092915050565b6000819050919050565b612d5d81612d4a565b82525050565b6000602082019050612d786000830184612d54565b92915050565b612d8781612d4a565b8114612d9257600080fd5b50565b600081359050612da481612d7e565b92915050565b600060208284031215612dc057612dbf6129a7565b5b6000612dce84828501612d95565b91505092915050565b612de081612b39565b82525050565b6000602082019050612dfb6000830184612dd7565b92915050565b60008060008060808587031215612e1b57612e1a6129a7565b5b6000612e2987828801612b98565b9450506020612e3a87828801612b98565b9350506040612e4b87828801612b98565b9250506060612e5c87828801612b98565b91505092959194509250565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b60006002820490506001821680612eaf57607f821691505b602082108103612ec257612ec1612e68565b5b50919050565b7f5a45524f5f464952535400000000000000000000000000000000000000000000600082015250565b6000612efe600a83612a72565b9150612f0982612ec8565b602082019050919050565b60006020820190508181036000830152612f2d81612ef1565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b6000612f6e82612b77565b9150612f7983612b77565b9250828203905081811115612f9157612f90612f34565b5b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fd5b6000612fd182612b77565b9150612fdc83612b77565b925082612fec57612feb612f97565b5b828204905092915050565b600061300282612b77565b915061300d83612b77565b925082820261301b81612b77565b9150828204841483151761303257613031612f34565b5b5092915050565b600061304482612b77565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff820361307657613075612f34565b5b600182019050919050565b600061308c82612b77565b915061309783612b77565b92508282019050808211156130af576130ae612f34565b5b92915050565b7f4552525f41434345535300000000000000000000000000000000000000000000600082015250565b60006130eb600a83612a72565b91506130f6826130b5565b602082019050919050565b6000602082019050818103600083015261311a816130de565b9050919050565b600061312c82612bed565b915061313783612bed565b925082820261314581612bed565b915080821461315757613156612f34565b5b5092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b6000819050919050565b60006131b26131ad6131a884612bed565b61318d565b612b77565b9050919050565b6131c281613197565b82525050565b60006020820190506131dd60008301846131b9565b92915050565b7f4552525f4f5645525350454e4400000000000000000000000000000000000000600082015250565b6000613219600d83612a72565b9150613224826131e3565b602082019050919050565b600060208201905081810360008301526132488161320c565b9050919050565b61325881612bed565b811461326357600080fd5b50565b6000815190506132758161324f565b92915050565b600060208284031215613291576132906129a7565b5b600061329f84828501613266565b9150509291505056fea2646970667358221220d498bcac54c9f0e4b63eea066f2ad30aea293661380ef849a31efc9b436e3eb064736f6c63430008110033 -\ No newline at end of file +6101006040523480156200001257600080fd5b50604051620062f5380380620062f5833981810160405281019062000038919062000863565b6801000000000000000083600f0b126200005157600080fd5b6200005b62000578565b33600460006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508660069081620000ad919062000b84565b508560079081620000bf919062000b84565b508460ff16608081815250504260038190555060035460a08181525050603c83620000eb919062000c9a565b60c08181525050620001126001620002a96401000000000262002fc2176401000000009004565b600260006101000a8154816fffffffffffffffffffffffffffffffff0219169083600f0b6fffffffffffffffffffffffffffffffff1602179055506200016c84620002d06401000000000262002fe8176401000000009004565b600f0b60e081600f0b81525050620001a86000600260009054906101000a9004600f0b6000600162000324640100000000026401000000009004565b90506000819080600181540180825580915050600190039060005260206000200160009091909190915060008201518160000160006101000a81548163ffffffff021916908363ffffffff16021790555060208201518160000160046101000a81548168ffffffffffffffffff021916908368ffffffffffffffffff160217905550604082015181600001600d6101000a81548167ffffffffffffffff021916908367ffffffffffffffff160217905550505081600e60006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055505050505050505062000ce5565b6000677fffffffffffffff821115620002c157600080fd5b6040829060020a029050919050565b60008082600f0b13620002e257600080fd5b60806fb17217f7d1cf79abc9e3b39803f2f6af6200030f84620003a8640100000000026401000000009004565b600f0b02908060020a82049150509050919050565b6200032e62000578565b6200033862000578565b82816000019063ffffffff16908163ffffffff168152505083816020019068ffffffffffffffffff16908168ffffffffffffffffff168152505067ffffffffffffffff8516816040019067ffffffffffffffff16908167ffffffffffffffff168152505080915050949350505050565b60008082600f0b13620003ba57600080fd5b60008083600f0b9050680100000000000000008112620003f6576040819060008212600003808260020a82851804189250505090506040820191505b640100000000811262000425576020819060008212600003808260020a82851804189250505090506020820191505b62010000811262000452576010819060008212600003808260020a82851804189250505090506010820191505b61010081126200047e576008819060008212600003808260020a82851804189250505090506008820191505b60108112620004a9576004819060008212600003808260020a82851804189250505090506004820191505b60048112620004d4576002819060008212600003808260020a82851804189250505090506002820191505b60028112620004e4576001820191505b600060408084039060020a029050600083607f0386600f0b9060020a029050600067800000000000000090505b60008113156200056b578182029150600060ff83908060020a8204915050905080607f0183908060020a8204915050925080820284019350506001819060008212600003808260020a828518041892505050905062000511565b5081945050505050919050565b6040518060600160405280600063ffffffff168152602001600068ffffffffffffffffff168152602001600067ffffffffffffffff1681525090565b6000604051905090565b600080fd5b600080fd5b600080fd5b600080fd5b6000601f19601f8301169050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6200061d82620005d2565b810181811067ffffffffffffffff821117156200063f576200063e620005e3565b5b80604052505050565b600062000654620005b4565b905062000662828262000612565b919050565b600067ffffffffffffffff821115620006855762000684620005e3565b5b6200069082620005d2565b9050602081019050919050565b60005b83811015620006bd578082015181840152602081019050620006a0565b60008484015250505050565b6000620006e0620006da8462000667565b62000648565b905082815260208101848484011115620006ff57620006fe620005cd565b5b6200070c8482856200069d565b509392505050565b600082601f8301126200072c576200072b620005c8565b5b81516200073e848260208601620006c9565b91505092915050565b600060ff82169050919050565b6200075f8162000747565b81146200076b57600080fd5b50565b6000815190506200077f8162000754565b92915050565b600081600f0b9050919050565b6200079d8162000785565b8114620007a957600080fd5b50565b600081519050620007bd8162000792565b92915050565b6000819050919050565b620007d881620007c3565b8114620007e457600080fd5b50565b600081519050620007f881620007cd565b92915050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b60006200082b82620007fe565b9050919050565b6200083d816200081e565b81146200084957600080fd5b50565b6000815190506200085d8162000832565b92915050565b60008060008060008060c08789031215620008835762000882620005be565b5b600087015167ffffffffffffffff811115620008a457620008a3620005c3565b5b620008b289828a0162000714565b965050602087015167ffffffffffffffff811115620008d657620008d5620005c3565b5b620008e489828a0162000714565b9550506040620008f789828a016200076e565b94505060606200090a89828a01620007ac565b93505060806200091d89828a01620007e7565b92505060a06200093089828a016200084c565b9150509295509295509295565b600081519050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806200099057607f821691505b602082108103620009a657620009a562000948565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b60008160020a8302905092915050565b60006008830262000a137fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82620009d1565b62000a1f8683620009d1565b95508019841693508086168417925050509392505050565b6000819050919050565b600062000a6262000a5c62000a5684620007c3565b62000a37565b620007c3565b9050919050565b6000819050919050565b62000a7e8362000a41565b62000a9662000a8d8262000a69565b848454620009e1565b825550505050565b600090565b62000aad62000a9e565b62000aba81848462000a73565b505050565b5b8181101562000ae25762000ad660008262000aa3565b60018101905062000ac0565b5050565b601f82111562000b315762000afb81620009ac565b62000b0684620009c1565b8101602085101562000b16578190505b62000b2e62000b2585620009c1565b83018262000abf565b50505b505050565b60008160020a8304905092915050565b600062000b596000198460080262000b36565b1980831691505092915050565b600062000b74838362000b46565b9150826002028217905092915050565b62000b8f826200093d565b67ffffffffffffffff81111562000bab5762000baa620005e3565b5b62000bb7825462000977565b62000bc482828562000ae6565b600060209050601f83116001811462000bfc576000841562000be7578287015190505b62000bf3858262000b66565b86555062000c63565b601f19841662000c0c86620009ac565b60005b8281101562000c365784890151825560018201915060208501945060208101905062000c0f565b8683101562000c56578489015162000c52601f89168262000b46565b8355505b6001600288020188555050505b505050505050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600062000ca782620007c3565b915062000cb483620007c3565b925082820262000cc481620007c3565b9150828204841483151762000cde5762000cdd62000c6b565b5b5092915050565b60805160a05160c05160e05161559062000d656000396000818161157a01528181612538015281816127140152612b10015260008181611bfa015281816124fb01528181612a6801528181612cac0152612e94015260008181611c2601528181612cd801528181612eb50152612efa015260006115d801526155906000f3fe608060405234801561001057600080fd5b50600436106103b6576000357c0100000000000000000000000000000000000000000000000000000000900480636f8b44b011610206578063a457c2d71161012c578063d7a52fa9116100ca578063e69571af11610099578063e69571af14610c21578063e809529514610c51578063eda4e6d614610c6f578063f2fde38b14610c8d576103b6565b8063d7a52fa914610b87578063d89135cd14610ba3578063da2824a814610bc1578063e54063a614610bf1576103b6565b8063b470aade11610106578063b470aade14610afd578063c0ab707714610b1b578063d340ef8a14610b4b578063d5abeb0114610b69576103b6565b8063a457c2d714610a7f578063a9059cbb14610aaf578063b1cb0db314610adf576103b6565b806384dde4af116101a45780638f1df6bc116101735780638f1df6bc14610a0757806395d89b4114610a255780639f24880814610a43578063a2309ff814610a61576103b6565b806384dde4af1461097f57806386fe212d1461099d5780638da5cb5b146109cd5780638f0b2d5d146109eb576103b6565b806373f42561116101e057806373f42561146108f55780637445e33a1461091357806374bedb951461094357806379ba509714610961576103b6565b80636f8b44b01461088b57806370a08231146108a7578063731f237c146108d7576103b6565b806331333487116102eb57806347a50517116102895780635ae06f7e116102585780635ae06f7e146107dd5780635f408c041461080d5780636787a9be1461082b5780636a2d094e1461085b576103b6565b806347a505171461072f5780634abfbba21461075f57806351b4541c1461078f57806358b2833b146107bf576103b6565b806339509351116102c5578063395093511461066d57806340452d911461069d57806342966c68146106cf578063449a52f8146106ff576103b6565b80633133348714610601578063313ce5671461063157806331a5995d1461064f576103b6565b806318cbbcfc1161035857806323a859441161033257806323a859441461054157806323b872dd146105715780632a7aec09146105a15780632c1758c1146105d1576103b6565b806318cbbcfc146104d55780631989c6a8146104f3578063213d1e1e14610511576103b6565b8063095ea7b311610394578063095ea7b31461043957806309f28f3c1461046957806311c566151461048757806318160ddd146104b7576103b6565b806301681a62146103bb57806301ffc9a7146103eb57806306fdde031461041b575b600080fd5b6103d560048036038101906103d0919061494a565b610cbd565b6040516103e29190614990565b60405180910390f35b61040560048036038101906104009190614a03565b610da6565b6040516104129190614a4b565b60405180910390f35b610423610efa565b6040516104309190614af6565b60405180910390f35b610453600480360381019061044e9190614b44565b610f88565b6040516104609190614a4b565b60405180910390f35b6104716111c4565b60405161047e9190614ba0565b60405180910390f35b6104a1600480360381019061049c9190614be7565b6111d7565b6040516104ae9190614990565b60405180910390f35b6104bf611227565b6040516104cc9190614990565b60405180910390f35b6104dd61123e565b6040516104ea9190614990565b60405180910390f35b6104fb611243565b6040516105089190614990565b60405180910390f35b61052b60048036038101906105269190614dc8565b61124f565b6040516105389190614990565b60405180910390f35b61055b60048036038101906105569190614dc8565b611268565b6040516105689190614a4b565b60405180910390f35b61058b60048036038101906105869190614df5565b6112d0565b6040516105989190614a4b565b60405180910390f35b6105bb60048036038101906105b69190614dc8565b6114ee565b6040516105c89190614ba0565b60405180910390f35b6105eb60048036038101906105e69190614e48565b611526565b6040516105f89190614a4b565b60405180910390f35b61061b60048036038101906106169190614e75565b611559565b6040516106289190614990565b60405180910390f35b6106396115d6565b6040516106469190614990565b60405180910390f35b6106576115fa565b6040516106649190614990565b60405180910390f35b61068760048036038101906106829190614b44565b611600565b6040516106949190614a4b565b60405180910390f35b6106b760048036038101906106b29190614e48565b611796565b6040516106c693929190614ee2565b60405180910390f35b6106e960048036038101906106e49190614e48565b611805565b6040516106f69190614a4b565b60405180910390f35b61071960048036038101906107149190614b44565b6119d6565b6040516107269190614a4b565b60405180910390f35b6107496004803603810190610744919061494a565b611bad565b6040516107569190614990565b60405180910390f35b61077960048036038101906107749190614e48565b611bf6565b6040516107869190614990565b60405180910390f35b6107a960048036038101906107a49190614e48565b611c56565b6040516107b69190614990565b60405180910390f35b6107c7611c77565b6040516107d49190614990565b60405180910390f35b6107f760048036038101906107f2919061494a565b611c7d565b6040516108049190614a4b565b60405180910390f35b610815611d87565b6040516108229190614f35565b60405180910390f35b61084560048036038101906108409190614e48565b611e54565b6040516108529190614990565b60405180910390f35b61087560048036038101906108709190614e48565b611e95565b6040516108829190614990565b60405180910390f35b6108a560048036038101906108a09190614e48565b611eb6565b005b6108c160048036038101906108bc919061494a565b611f7e565b6040516108ce9190614990565b60405180910390f35b6108df611fe5565b6040516108ec9190614990565b60405180910390f35b6108fd611ff6565b60405161090a9190614990565b60405180910390f35b61092d60048036038101906109289190614dc8565b611ffc565b60405161093a9190614990565b60405180910390f35b61094b61202a565b6040516109589190614990565b60405180910390f35b610969612030565b6040516109769190614a4b565b60405180910390f35b6109876121db565b6040516109949190614f5f565b60405180910390f35b6109b760048036038101906109b29190614e48565b612201565b6040516109c49190614990565b60405180910390f35b6109d56122e8565b6040516109e29190614f5f565b60405180910390f35b610a056004803603810190610a00919061494a565b61230e565b005b610a0f6123c3565b604051610a1c9190614a4b565b60405180910390f35b610a2d612684565b604051610a3a9190614af6565b60405180910390f35b610a4b612712565b604051610a589190614ba0565b60405180910390f35b610a69612736565b604051610a769190614990565b60405180910390f35b610a996004803603810190610a949190614b44565b612740565b604051610aa69190614a4b565b60405180910390f35b610ac96004803603810190610ac49190614b44565b61295f565b604051610ad69190614a4b565b60405180910390f35b610ae7612a60565b604051610af49190614990565b60405180910390f35b610b05612a66565b604051610b129190614990565b60405180910390f35b610b356004803603810190610b309190614e48565b612a8a565b604051610b429190614990565b60405180910390f35b610b53612c11565b604051610b609190614990565b60405180910390f35b610b71612c17565b604051610b7e9190614990565b60405180910390f35b610ba16004803603810190610b9c9190614e48565b612c1d565b005b610bab612d1c565b604051610bb89190614990565b60405180910390f35b610bdb6004803603810190610bd6919061494a565b612d26565b604051610be89190614a4b565b60405180910390f35b610c0b6004803603810190610c069190614dc8565b612dfa565b604051610c189190614990565b60405180910390f35b610c3b6004803603810190610c369190614f7a565b612e0e565b604051610c489190615050565b60405180910390f35b610c59612e8e565b604051610c669190615096565b60405180910390f35b610c77612ef8565b604051610c849190614990565b60405180910390f35b610ca76004803603810190610ca2919061494a565b612f1c565b604051610cb49190614a4b565b60405180910390f35b600080600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490506000600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555080600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254610d9691906150e0565b9250508190555080915050919050565b600063c6bb4b707c010000000000000000000000000000000000000000000000000000000002827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff191603610dfa5760019050610ef5565b63449a52f87c010000000000000000000000000000000000000000000000000000000002827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff191603610e4c5760019050610ef5565b6301ffc9a77c010000000000000000000000000000000000000000000000000000000002827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff191603610e9e5760019050610ef5565b639493f8b27c010000000000000000000000000000000000000000000000000000000002827bffffffffffffffffffffffffffffffffffffffffffffffffffffffff191603610ef05760019050610ef5565b600090505b919050565b60068054610f0790615143565b80601f0160208091040260200160405190810160405280929190818152602001828054610f3390615143565b8015610f805780601f10610f5557610100808354040283529160200191610f80565b820191906000526020600020905b815481529060010190602001808311610f6357829003601f168201915b505050505081565b6000806000610f95611d87565b905060028160ff1603610fad576000925050506111be565b60008160ff161115610ff4576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401610feb906151c0565b60405180910390fd5b6000600d60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205411156110bd57600084146110bc576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016110b39061522c565b60405180910390fd5b5b6110c56123c3565b506110cf84611e54565b915081600d60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508473ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925866040516111af9190614990565b60405180910390a36001925050505b92915050565b600260009054906101000a9004600f0b81565b6000806111fe6111e685612fc2565b6111f96111f36001612fc2565b8661302a565b613091565b905061120981613110565b67ffffffffffffffff168461121e919061524c565b91505092915050565b6000600b54600854611239919061524c565b905090565b600f81565b60008080549050905090565b6000816020015168ffffffffffffffffff169050919050565b600080826000015163ffffffff16111561128557600090506112cb565b6000826020015168ffffffffffffffffff1611156112a657600090506112cb565b6000826040015167ffffffffffffffff1611156112c657600090506112cb565b600190505b919050565b6000806000806112de611d87565b905060028160ff16036112f757600093505050506114e7565b60008160ff16111561133e576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401611335906151c0565b60405180910390fd5b6113466123c3565b5061135085611e54565b925082600d60008973ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410156113db57600080fd5b82600d60008973ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254611467919061524c565b92505081905550611479878785613145565b91508573ffffffffffffffffffffffffffffffffffffffff168773ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef876040516114d89190614990565b60405180910390a38193505050505b9392505050565b60008067ffffffffffffffff836040015160070b169050600081600f0b0361151d5761151a6001612fc2565b90505b80915050919050565b6000600f821061153557600080fd5b6000820361154a57600f601254149050611554565b8160125483161490505b919050565b60008060008061156886612fc2565b925061157385612fc2565b915061159f7f000000000000000000000000000000000000000000000000000000000000000083613091565b90506115aa81613168565b90506115b68382613091565b90506115c181613110565b67ffffffffffffffff16935050505092915050565b7f000000000000000000000000000000000000000000000000000000000000000081565b60125481565b60008061160b6123c3565b5061161583611e54565b905080600d60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282546116a391906150e0565b925050819055508373ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925600d60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008873ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020546040516117839190614990565b60405180910390a3600191505092915050565b600081815481106117a657600080fd5b906000526020600020016000915090508060000160009054906101000a900463ffffffff16908060000160049054906101000a900468ffffffffffffffffff169080600001600d9054906101000a900467ffffffffffffffff16905083565b600080611810611d87565b60ff161461181d57600080fd5b600c60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff16806118c25750600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b611901576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016118f8906152cc565b60405180910390fd5b600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482111561194d57600080fd5b600061195883611e54565b905061196433826131f5565b5082600b600082825461197791906150e0565b925050819055503373ffffffffffffffffffffffffffffffffffffffff167fcc16f5dbb4873280815c1ee09dbd06736cffcc184412cf7a71a0fdb75d397ca5846040516119c49190614990565b60405180910390a26001915050919050565b60008060006119e3611d87565b60ff16146119f057600080fd5b600c60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060009054906101000a900460ff1680611a955750600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16145b611ad4576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401611acb906152cc565b60405180910390fd5b611adc6123c3565b5060006011541115611b055760115483600854611af991906150e0565b1115611b0457600080fd5b5b8260086000828254611b1791906150e0565b92505081905550611b2783611e54565b9050611b3384826132f9565b508373ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fab8530f87dc9b59234c4623bf917212bb2536d647574c8e7e5da92c2ede0c9f885604051611b919190614990565b60405180910390a3611ba16133ba565b50600191505092915050565b6000600160008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020549050919050565b60007f000000000000000000000000000000000000000000000000000000000000000082611c2491906152ec565b7f0000000000000000000000000000000000000000000000000000000000000000611c4f91906150e0565b9050919050565b6000603c8242611c66919061524c565b611c70919061535d565b9050919050565b60035481565b6000611c8c600160ff16611526565b15611c9657600080fd5b600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161480611d1d57503373ffffffffffffffffffffffffffffffffffffffff168273ffffffffffffffffffffffffffffffffffffffff16145b611d2657600080fd5b6000600c60008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff02191690831515021790555060019050919050565b6000601060009054906101000a900460ff1615611da75760019050611e51565b6000600f5403611dba5760009050611e51565b600f544210611e4c57611de7603c600354611dd5919061535d565b600f54611de2919061524c565b612a8a565b506001601060006101000a81548160ff0219169083151502179055507ff80dbaea4785589e52984ca36a31de106adc77759539a5c7d92883bf49692fe942604051611e329190614990565b60405180910390a1611e426123c3565b5060029050611e51565b600090505b90565b600080611e78611e6384612fc2565b600260009054906101000a9004600f0b61357e565b9050611e8381613110565b67ffffffffffffffff16915050919050565b6000603c8242611ea5919061524c565b611eaf919061535d565b9050919050565b611ec3600860ff16611526565b15611ecd57600080fd5b600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614611f2757600080fd5b611f2f611227565b8111611f3a57600080fd5b6011547f9722adea12ab7ef86fc45b88f0e0b567639e8dddaae60261e08c03d747fbbfe682604051611f6c9190614990565b60405180910390a28060118190555050565b600080600080611f95611f9086611bad565b612fc2565b9250611fa2600354611c56565b9050611fbd83600260009054906101000a9004600f0b613091565b9150611fdb611fcb83613110565b67ffffffffffffffff1682611559565b9350505050919050565b6000611ff16000612a8a565b905090565b600b5481565b600080600061200a8461124f565b9150612015846114ee565b905061202182826111d7565b92505050919050565b600a5481565b600080600560009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161461208d57600080fd5b600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff169050600560009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16600460006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506000600560006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e060405160405180910390a3600191505090565b600e60009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600060108210612246576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161223d906153da565b60405180910390fd5b600060125483161461228d576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040161228490615446565b60405180910390fd5b81601260008282541792505081905550600f6012541415157f6b7e2e653f93b645d4ed7292d6429f96637084363e477c8aaea1a43ed13c284e6012546040516122d69190614990565b60405180910390a26012549050919050565b600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b61231b600260ff16611526565b1561232557600080fd5b600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161461237f57600080fd5b80600e60006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050565b60006123cd6148a1565b6123d56148a1565b6123dd6148a1565b60008060008060006123ed611fe5565b506123f661360a565b975061240188611268565b1561241757600098505050505050505050612681565b60006009548154811061242d5761242c615466565b5b906000526020600020016040518060600160405290816000820160009054906101000a900463ffffffff1663ffffffff1663ffffffff1681526020016000820160049054906101000a900468ffffffffffffffffff1668ffffffffffffffffff1668ffffffffffffffffff16815260200160008201600d9054906101000a900467ffffffffffffffff1667ffffffffffffffff1667ffffffffffffffff168152505095506124da88612dfa565b94506001856124e991906150e0565b90506124f4866114ee565b9350603c857f000000000000000000000000000000000000000000000000000000000000000061252491906152ec565b61252e919061535d565b915061256a6125657f000000000000000000000000000000000000000000000000000000000000000061256085612fc2565b613091565b613168565b925061258060008461257a611227565b84612e0e565b96506000879080600181540180825580915050600190039060005260206000200160009091909190915060008201518160000160006101000a81548163ffffffff021916908363ffffffff16021790555060208201518160000160046101000a81548168ffffffffffffffffff021916908368ffffffffffffffffff160217905550604082015181600001600d6101000a81548167ffffffffffffffff021916908367ffffffffffffffff160217905550505061263c876137a0565b507f55d243082e019fce4009ccea5368b92e436c17586a1e793c7deda16df4e5d6758160405161266c9190614990565b60405180910390a16001985050505050505050505b90565b6007805461269190615143565b80601f01602080910402602001604051908101604052809291908181526020018280546126bd90615143565b801561270a5780601f106126df5761010080835404028352916020019161270a565b820191906000526020600020905b8154815290600101906020018083116126ed57829003601f168201915b505050505081565b7f000000000000000000000000000000000000000000000000000000000000000081565b6000600854905090565b60008061274c83611e54565b905080600d60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410156127d757600080fd5b6127df6123c3565b5080600d60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825461286c919061524c565b925050819055508373ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925600d60003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008873ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205460405161294c9190614990565b60405180910390a3600191505092915050565b60008060008061296d611d87565b905060028160ff16036129865760009350505050612a5a565b60008160ff1611156129cd576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016129c4906151c0565b60405180910390fd5b6129d56123c3565b506129df85611e54565b92506129ec338785613145565b91508573ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef87604051612a4b9190614990565b60405180910390a38193505050505b92915050565b600f5481565b7f000000000000000000000000000000000000000000000000000000000000000081565b6000806000806000601060009054906101000a900460ff1615612ab4576000945050505050612c0c565b612abf600354611c56565b925060008303612ad6576000945050505050612c0c565b600260009054906101000a9004600f0b9050600086118015612af757508286105b15612b00578592505b612b0983612fc2565b9150612b357f000000000000000000000000000000000000000000000000000000000000000083613091565b9350612b4084613168565b9350612b5b600260009054906101000a9004600f0b85613091565b600260006101000a8154816fffffffffffffffffffffffffffffffff0219169083600f0b6fffffffffffffffffffffffffffffffff160217905550603c83612ba391906152ec565b600354612bb091906150e0565b60038190555080600f0b836003547f1c9c74563c32efd114cb36fb5e432d9386c8254d08456614804a33a3088ab736600260009054906101000a9004600f0b604051612bfc9190614ba0565b60405180910390a4829450505050505b919050565b60095481565b60115481565b6000612c2c600460ff16611526565b15612c3657600080fd5b601060009054906101000a900460ff1615612c5057600080fd5b600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614612caa57600080fd5b7f000000000000000000000000000000000000000000000000000000000000000082612cd691906152ec565b7f0000000000000000000000000000000000000000000000000000000000000000612d0191906150e0565b9050600f548111612d1157600080fd5b80600f819055505050565b6000600b54905090565b6000612d35600160ff16611526565b15612d3f57600080fd5b600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614612d9957600080fd5b6001600c60008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060006101000a81548160ff02191690831515021790555060019050919050565b6000816000015163ffffffff169050919050565b612e166148a1565b612e1e6148a1565b82816000019063ffffffff16908163ffffffff168152505083816020019068ffffffffffffffffff16908168ffffffffffffffffff168152505067ffffffffffffffff8516816040019067ffffffffffffffff16908167ffffffffffffffff168152505080915050949350505050565b600060017f00000000000000000000000000000000000000000000000000000000000000007f000000000000000000000000000000000000000000000000000000000000000042612edf919061524c565b612ee9919061535d565b612ef391906150e0565b905090565b7f000000000000000000000000000000000000000000000000000000000000000081565b6000600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614612f7857600080fd5b81600560006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555060019050919050565b6000677fffffffffffffff821115612fd957600080fd5b6040829060020a029050919050565b60008082600f0b13612ff957600080fd5b60806fb17217f7d1cf79abc9e3b39803f2f6af613015846138bf565b600f0b02908060020a82049150509050919050565b60008082600f0b84600f0b0390507fffffffffffffffffffffffffffffffff80000000000000000000000000000000600f0b811215801561307e57506f7fffffffffffffffffffffffffffffff600f0b8113155b61308757600080fd5b8091505092915050565b600080604083600f0b85600f0b029060008212600003808260020a82851804189250505090507fffffffffffffffffffffffffffffffff80000000000000000000000000000000600f0b81121580156130fd57506f7fffffffffffffffffffffffffffffff600f0b8113155b61310657600080fd5b8091505092915050565b60008082600f0b121561312257600080fd5b604082600f0b9060008212600003808260020a8285180418925050509050919050565b600061315184836131f5565b5061315c83836132f9565b50600190509392505050565b60006840000000000000000082600f0b1261318257600080fd5b7fffffffffffffffffffffffffffffffffffffffffffffffc0000000000000000082600f0b12156131b657600090506131f0565b6131ed6080700171547652b82fe1777d0ffda0d23a7d1284600f0b029060008212600003808260020a828518041892505050613a85565b90505b919050565b6000806000600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205490506000840361324f576000925050506132f3565b61325885611bad565b91508382101561329d576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401613294906154e1565b60405180910390fd5b83826132a9919061524c565b600160008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055506001925050505b92915050565b6000806000600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054905060008403613353576000925050506133b4565b61335c85611bad565b9150838261336a91906150e0565b600160008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055506001925050505b92915050565b60006133c46148a1565b60006133ce611227565b9050600060016000805490506133e4919061524c565b815481106133f5576133f4615466565b5b906000526020600020016040518060600160405290816000820160009054906101000a900463ffffffff1663ffffffff1663ffffffff1681526020016000820160049054906101000a900468ffffffffffffffffff1668ffffffffffffffffff1668ffffffffffffffffff16815260200160008201600d9054906101000a900467ffffffffffffffff1667ffffffffffffffff1667ffffffffffffffff1681525050915080826020019068ffffffffffffffffff16908168ffffffffffffffffff168152505081600060016000805490506134d0919061524c565b815481106134e1576134e0615466565b5b9060005260206000200160008201518160000160006101000a81548163ffffffff021916908363ffffffff16021790555060208201518160000160046101000a81548168ffffffffffffffffff021916908368ffffffffffffffffff160217905550604082015181600001600d6101000a81548167ffffffffffffffff021916908367ffffffffffffffff16021790555090505060019250505090565b60008082600f0b0361358f57600080fd5b600082600f0b604085600f0b9060020a02816135ae576135ad61532e565b5b0590507fffffffffffffffffffffffffffffffff80000000000000000000000000000000600f0b81121580156135f757506f7fffffffffffffffffffffffffffffff600f0b8113155b61360057600080fd5b8091505092915050565b6136126148a1565b61361a6148a1565b6136226148a1565b6000806009548154811061363957613638615466565b5b906000526020600020016040518060600160405290816000820160009054906101000a900463ffffffff1663ffffffff1663ffffffff1681526020016000820160049054906101000a900468ffffffffffffffffff1668ffffffffffffffffff1668ffffffffffffffffff16815260200160008201600d9054906101000a900467ffffffffffffffff1667ffffffffffffffff1667ffffffffffffffff168152505092503073ffffffffffffffffffffffffffffffffffffffff1663e80952956040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381865afa158015613744573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190613768919061552d565b6fffffffffffffffffffffffffffffffff16905061378583612dfa565b81116137965781935050505061379d565b8293505050505b90565b60008060006137ae84611ffc565b6137b6611227565b6137c0919061524c565b9150600a546137ce83611e54565b6137d8919061524c565b9050613806600e60009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16826132f9565b50836000015163ffffffff16600e60009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff167f9a2a887706623ad3ff7fc85652deeceabe9fe1e00466c597972079ee91ea40d38460405161387a9190614990565b60405180910390a360016009600082825461389591906150e0565b9250508190555080600a60008282546138ae91906150e0565b925050819055508192505050919050565b60008082600f0b136138d057600080fd5b60008083600f0b905068010000000000000000811261390b576040819060008212600003808260020a82851804189250505090506040820191505b6401000000008112613939576020819060008212600003808260020a82851804189250505090506020820191505b620100008112613965576010819060008212600003808260020a82851804189250505090506010820191505b6101008112613990576008819060008212600003808260020a82851804189250505090506008820191505b601081126139ba576004819060008212600003808260020a82851804189250505090506004820191505b600481126139e4576002819060008212600003808260020a82851804189250505090506002820191505b600281126139f3576001820191505b600060408084039060020a029050600083607f0386600f0b9060020a029050600067800000000000000090505b6000811315613a78578182029150600060ff83908060020a8204915050905080607f0183908060020a8204915050925080820284019350506001819060008212600003808260020a8285180418925050509050613a20565b5081945050505050919050565b60006840000000000000000082600f0b12613a9f57600080fd5b7fffffffffffffffffffffffffffffffffffffffffffffffc0000000000000000082600f0b1215613ad3576000905061489c565b60006f80000000000000000000000000000000905060006780000000000000008416600f0b1315613b2157608070016a09e667f3bcc908b2fb1366ea957d3e8202908060020a820491505090505b60006740000000000000008416600f0b1315613b5a5760807001306fe0a31b7152de8d5a46305c85edec8202908060020a820491505090505b60006720000000000000008416600f0b1315613b935760807001172b83c7d517adcdf7c8c50eb14a791f8202908060020a820491505090505b60006710000000000000008416600f0b1315613bcc57608070010b5586cf9890f6298b92b71842a983638202908060020a820491505090505b60006708000000000000008416600f0b1315613c055760807001059b0d31585743ae7c548eb68ca417fd8202908060020a820491505090505b60006704000000000000008416600f0b1315613c3e576080700102c9a3e778060ee6f7caca4f7a29bde88202908060020a820491505090505b60006702000000000000008416600f0b1315613c7757608070010163da9fb33356d84a66ae336dcdfa3f8202908060020a820491505090505b60006701000000000000008416600f0b1315613cb0576080700100b1afa5abcbed6129ab13ec11dc95438202908060020a820491505090505b600066800000000000008416600f0b1315613ce857608070010058c86da1c09ea1ff19d294cf2f679b8202908060020a820491505090505b600066400000000000008416600f0b1315613d205760807001002c605e2e8cec506d21bfc89a23a00f8202908060020a820491505090505b600066200000000000008416600f0b1315613d58576080700100162f3904051fa128bca9c55c31e5df8202908060020a820491505090505b600066100000000000008416600f0b1315613d905760807001000b175effdc76ba38e31671ca9397258202908060020a820491505090505b600066080000000000008416600f0b1315613dc8576080700100058ba01fb9f96d6cacd4b180917c3d8202908060020a820491505090505b600066040000000000008416600f0b1315613e0057608070010002c5cc37da9491d0985c348c68e7b38202908060020a820491505090505b600066020000000000008416600f0b1315613e385760807001000162e525ee054754457d59952920268202908060020a820491505090505b600066010000000000008416600f0b1315613e7057608070010000b17255775c040618bf4a4ade83fc8202908060020a820491505090505b6000658000000000008416600f0b1315613ea75760807001000058b91b5bc9ae2eed81e9b7d4cfab8202908060020a820491505090505b6000654000000000008416600f0b1315613ede576080700100002c5c89d5ec6ca4d7c8acc017b7c98202908060020a820491505090505b6000652000000000008416600f0b1315613f1557608070010000162e43f4f831060e02d839a9d16d8202908060020a820491505090505b6000651000000000008416600f0b1315613f4c576080700100000b1721bcfc99d9f890ea069117638202908060020a820491505090505b6000650800000000008416600f0b1315613f8357608070010000058b90cf1e6d97f9ca14dbcc16288202908060020a820491505090505b6000650400000000008416600f0b1315613fba5760807001000002c5c863b73f016468f6bac5ca2b8202908060020a820491505090505b6000650200000000008416600f0b1315613ff1576080700100000162e430e5a18f6119e3c02282a58202908060020a820491505090505b6000650100000000008416600f0b13156140285760807001000000b1721835514b86e6d96efd1bfe8202908060020a820491505090505b60006480000000008416600f0b131561405e576080700100000058b90c0b48c6be5df846c5b2ef8202908060020a820491505090505b60006440000000008416600f0b131561409457608070010000002c5c8601cc6b9e94213c72737a8202908060020a820491505090505b60006420000000008416600f0b13156140ca5760807001000000162e42fff037df38aa2b219f068202908060020a820491505090505b60006410000000008416600f0b131561410057608070010000000b17217fba9c739aa5819f44f98202908060020a820491505090505b60006408000000008416600f0b13156141365760807001000000058b90bfcdee5acd3c1cedc8238202908060020a820491505090505b60006404000000008416600f0b131561416c576080700100000002c5c85fe31f35a6a30da1be508202908060020a820491505090505b60006402000000008416600f0b13156141a257608070010000000162e42ff0999ce3541b9fffcf8202908060020a820491505090505b60006401000000008416600f0b13156141d8576080700100000000b17217f80f4ef5aadda455548202908060020a820491505090505b600063800000008416600f0b131561420d57608070010000000058b90bfbf8479bd5a81b51ad8202908060020a820491505090505b600063400000008416600f0b13156142425760807001000000002c5c85fdf84bd62ae30a74cc8202908060020a820491505090505b600063200000008416600f0b1315614277576080700100000000162e42fefb2fed257559bdaa8202908060020a820491505090505b600063100000008416600f0b13156142ac5760807001000000000b17217f7d5a7716bba4a9ae8202908060020a820491505090505b600063080000008416600f0b13156142e1576080700100000000058b90bfbe9ddbac5e109cce8202908060020a820491505090505b600063040000008416600f0b131561431657608070010000000002c5c85fdf4b15de6f17eb0d8202908060020a820491505090505b600063020000008416600f0b131561434b5760807001000000000162e42fefa494f1478fde058202908060020a820491505090505b600063010000008416600f0b131561438057608070010000000000b17217f7d20cf927c8e94c8202908060020a820491505090505b6000628000008416600f0b13156143b45760807001000000000058b90bfbe8f71cb4e4b33d8202908060020a820491505090505b6000624000008416600f0b13156143e8576080700100000000002c5c85fdf477b662b269458202908060020a820491505090505b6000622000008416600f0b131561441c57608070010000000000162e42fefa3ae53369388c8202908060020a820491505090505b6000621000008416600f0b1315614450576080700100000000000b17217f7d1d351a389d408202908060020a820491505090505b6000620800008416600f0b131561448457608070010000000000058b90bfbe8e8b2d3d4ede8202908060020a820491505090505b6000620400008416600f0b13156144b85760807001000000000002c5c85fdf4741bea6e77e8202908060020a820491505090505b6000620200008416600f0b13156144ec576080700100000000000162e42fefa39fe95583c28202908060020a820491505090505b6000620100008416600f0b13156145205760807001000000000000b17217f7d1cfb72b45e18202908060020a820491505090505b60006180008416600f0b1315614553576080700100000000000058b90bfbe8e7cc35c3f08202908060020a820491505090505b60006140008416600f0b131561458657608070010000000000002c5c85fdf473e242ea388202908060020a820491505090505b60006120008416600f0b13156145b95760807001000000000000162e42fefa39f02b772c8202908060020a820491505090505b60006110008416600f0b13156145ec57608070010000000000000b17217f7d1cf7d83c1a8202908060020a820491505090505b60006108008416600f0b131561461f5760807001000000000000058b90bfbe8e7bdcbe2e8202908060020a820491505090505b60006104008416600f0b1315614652576080700100000000000002c5c85fdf473dea871f8202908060020a820491505090505b60006102008416600f0b131561468557608070010000000000000162e42fefa39ef44d918202908060020a820491505090505b60006101008416600f0b13156146b8576080700100000000000000b17217f7d1cf79e9498202908060020a820491505090505b600060808416600f0b13156146ea57608070010000000000000058b90bfbe8e7bce5448202908060020a820491505090505b600060408416600f0b131561471c5760807001000000000000002c5c85fdf473de6eca8202908060020a820491505090505b600060208416600f0b131561474e576080700100000000000000162e42fefa39ef366f8202908060020a820491505090505b600060108416600f0b13156147805760807001000000000000000b17217f7d1cf79afa8202908060020a820491505090505b600060088416600f0b13156147b2576080700100000000000000058b90bfbe8e7bcd6d8202908060020a820491505090505b600060048416600f0b13156147e457608070010000000000000002c5c85fdf473de6b28202908060020a820491505090505b600060028416600f0b13156148165760807001000000000000000162e42fefa39ef3588202908060020a820491505090505b600060018416600f0b131561484857608070010000000000000000b17217f7d1cf79ab8202908060020a820491505090505b604083600f0b9060008212600003808260020a828518041892505050603f03600f0b81908060020a820491505090506f7fffffffffffffffffffffffffffffff600f0b81111561489757600080fd5b809150505b919050565b6040518060600160405280600063ffffffff168152602001600068ffffffffffffffffff168152602001600067ffffffffffffffff1681525090565b6000604051905090565b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000614917826148ec565b9050919050565b6149278161490c565b811461493257600080fd5b50565b6000813590506149448161491e565b92915050565b6000602082840312156149605761495f6148e7565b5b600061496e84828501614935565b91505092915050565b6000819050919050565b61498a81614977565b82525050565b60006020820190506149a56000830184614981565b92915050565b60007fffffffff0000000000000000000000000000000000000000000000000000000082169050919050565b6149e0816149ab565b81146149eb57600080fd5b50565b6000813590506149fd816149d7565b92915050565b600060208284031215614a1957614a186148e7565b5b6000614a27848285016149ee565b91505092915050565b60008115159050919050565b614a4581614a30565b82525050565b6000602082019050614a606000830184614a3c565b92915050565b600081519050919050565b600082825260208201905092915050565b60005b83811015614aa0578082015181840152602081019050614a85565b60008484015250505050565b6000601f19601f8301169050919050565b6000614ac882614a66565b614ad28185614a71565b9350614ae2818560208601614a82565b614aeb81614aac565b840191505092915050565b60006020820190508181036000830152614b108184614abd565b905092915050565b614b2181614977565b8114614b2c57600080fd5b50565b600081359050614b3e81614b18565b92915050565b60008060408385031215614b5b57614b5a6148e7565b5b6000614b6985828601614935565b9250506020614b7a85828601614b2f565b9150509250929050565b600081600f0b9050919050565b614b9a81614b84565b82525050565b6000602082019050614bb56000830184614b91565b92915050565b614bc481614b84565b8114614bcf57600080fd5b50565b600081359050614be181614bbb565b92915050565b60008060408385031215614bfe57614bfd6148e7565b5b6000614c0c85828601614b2f565b9250506020614c1d85828601614bd2565b9150509250929050565b600080fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b614c6482614aac565b810181811067ffffffffffffffff82111715614c8357614c82614c2c565b5b80604052505050565b6000614c966148dd565b9050614ca28282614c5b565b919050565b600063ffffffff82169050919050565b614cc081614ca7565b8114614ccb57600080fd5b50565b600081359050614cdd81614cb7565b92915050565b600068ffffffffffffffffff82169050919050565b614d0181614ce3565b8114614d0c57600080fd5b50565b600081359050614d1e81614cf8565b92915050565b600067ffffffffffffffff82169050919050565b614d4181614d24565b8114614d4c57600080fd5b50565b600081359050614d5e81614d38565b92915050565b600060608284031215614d7a57614d79614c27565b5b614d846060614c8c565b90506000614d9484828501614cce565b6000830152506020614da884828501614d0f565b6020830152506040614dbc84828501614d4f565b60408301525092915050565b600060608284031215614dde57614ddd6148e7565b5b6000614dec84828501614d64565b91505092915050565b600080600060608486031215614e0e57614e0d6148e7565b5b6000614e1c86828701614935565b9350506020614e2d86828701614935565b9250506040614e3e86828701614b2f565b9150509250925092565b600060208284031215614e5e57614e5d6148e7565b5b6000614e6c84828501614b2f565b91505092915050565b60008060408385031215614e8c57614e8b6148e7565b5b6000614e9a85828601614b2f565b9250506020614eab85828601614b2f565b9150509250929050565b614ebe81614ca7565b82525050565b614ecd81614ce3565b82525050565b614edc81614d24565b82525050565b6000606082019050614ef76000830186614eb5565b614f046020830185614ec4565b614f116040830184614ed3565b949350505050565b600060ff82169050919050565b614f2f81614f19565b82525050565b6000602082019050614f4a6000830184614f26565b92915050565b614f598161490c565b82525050565b6000602082019050614f746000830184614f50565b92915050565b60008060008060808587031215614f9457614f936148e7565b5b6000614fa287828801614b2f565b9450506020614fb387828801614bd2565b9350506040614fc487828801614b2f565b9250506060614fd587828801614b2f565b91505092959194509250565b614fea81614ca7565b82525050565b614ff981614ce3565b82525050565b61500881614d24565b82525050565b6060820160008201516150246000850182614fe1565b5060208201516150376020850182614ff0565b50604082015161504a6040850182614fff565b50505050565b6000606082019050615065600083018461500e565b92915050565b60006fffffffffffffffffffffffffffffffff82169050919050565b6150908161506b565b82525050565b60006020820190506150ab6000830184615087565b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006150eb82614977565b91506150f683614977565b925082820190508082111561510e5761510d6150b1565b5b92915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000600282049050600182168061515b57607f821691505b60208210810361516e5761516d615114565b5b50919050565b7f4558504952454400000000000000000000000000000000000000000000000000600082015250565b60006151aa600783614a71565b91506151b582615174565b602082019050919050565b600060208201905081810360008301526151d98161519d565b9050919050565b7f5a45524f5f464952535400000000000000000000000000000000000000000000600082015250565b6000615216600a83614a71565b9150615221826151e0565b602082019050919050565b6000602082019050818103600083015261524581615209565b9050919050565b600061525782614977565b915061526283614977565b925082820390508181111561527a576152796150b1565b5b92915050565b7f4552525f41434345535300000000000000000000000000000000000000000000600082015250565b60006152b6600a83614a71565b91506152c182615280565b602082019050919050565b600060208201905081810360008301526152e5816152a9565b9050919050565b60006152f782614977565b915061530283614977565b925082820261531081614977565b91508282048414831517615327576153266150b1565b5b5092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601260045260246000fd5b600061536882614977565b915061537383614977565b9250826153835761538261532e565b5b828204905092915050565b7f4552525f494e56414c49445f5354415445000000000000000000000000000000600082015250565b60006153c4601183614a71565b91506153cf8261538e565b602082019050919050565b600060208201905081810360008301526153f3816153b7565b9050919050565b7f4552525f414c52454144595f4c4f434b45440000000000000000000000000000600082015250565b6000615430601283614a71565b915061543b826153fa565b602082019050919050565b6000602082019050818103600083015261545f81615423565b9050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b7f4552525f4f5645525350454e4400000000000000000000000000000000000000600082015250565b60006154cb600d83614a71565b91506154d682615495565b602082019050919050565b600060208201905081810360008301526154fa816154be565b9050919050565b61550a8161506b565b811461551557600080fd5b50565b60008151905061552781615501565b92915050565b600060208284031215615543576155426148e7565b5b600061555184828501615518565b9150509291505056fea2646970667358221220210fd6113e7b31a5e5704d8e47b5f99156628cc7a960bf596d7189af1987781764736f6c63430008120033 +\ No newline at end of file diff --git a/python/erc20_demurrage_token/data/DemurrageTokenSingleNocap.json b/python/erc20_demurrage_token/data/DemurrageTokenSingleNocap.json @@ -1 +1 @@ -[{"inputs":[{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_symbol","type":"string"},{"internalType":"uint8","name":"_decimals","type":"uint8"},{"internalType":"uint128","name":"_taxLevelMinute","type":"uint128"},{"internalType":"uint256","name":"_periodMinutes","type":"uint256"},{"internalType":"address","name":"_defaultSinkAddress","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_owner","type":"address"},{"indexed":true,"internalType":"address","name":"_spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_burner","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Burn","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes32","name":"_foo","type":"bytes32"}],"name":"Debug","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"_period","type":"uint256"},{"indexed":true,"internalType":"uint256","name":"_periodCount","type":"uint256"},{"indexed":true,"internalType":"uint256","name":"_oldAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_newAmount","type":"uint256"}],"name":"Decayed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_minter","type":"address"},{"indexed":true,"internalType":"address","name":"_beneficiary","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"_period","type":"uint256"}],"name":"Period","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_account","type":"address"},{"indexed":true,"internalType":"uint256","name":"_period","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Redistribution","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_from","type":"address"},{"indexed":true,"internalType":"address","name":"_to","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[],"name":"acceptOwnership","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"actualPeriod","outputs":[{"internalType":"uint128","name":"","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_minter","type":"address"}],"name":"addMinter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"applyDemurrage","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_rounds","type":"uint256"}],"name":"applyDemurrageLimited","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"baseBalanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"burn","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"burned","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"changePeriod","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_value","type":"uint256"},{"internalType":"uint256","name":"_period","type":"uint256"}],"name":"decayBy","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"demurrageAmount","outputs":[{"internalType":"uint128","name":"","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_target","type":"uint256"}],"name":"demurrageCycles","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"demurrageTimestamp","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_supply","type":"uint256"},{"internalType":"uint256","name":"_demurrageAmount","type":"uint256"}],"name":"getDistribution","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_redistribution","type":"bytes32"}],"name":"getDistributionFromRedistribution","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_lastTimestamp","type":"uint256"}],"name":"getMinutesDelta","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_periodCount","type":"uint256"}],"name":"getPeriodTimeDelta","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_value","type":"uint256"},{"internalType":"uint256","name":"_period","type":"uint256"}],"name":"growBy","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"lastPeriod","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_beneficiary","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"mintTo","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"periodDuration","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"periodStart","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"redistributionCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"redistributions","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_minter","type":"address"}],"name":"removeMinter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"resolutionFactor","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_sinkAddress","type":"address"}],"name":"setSinkAddress","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"sinkAddress","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes4","name":"_sum","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"taxLevel","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"toBaseAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_participants","type":"uint256"},{"internalType":"uint256","name":"_demurrageModifierPpm","type":"uint256"},{"internalType":"uint256","name":"_value","type":"uint256"},{"internalType":"uint256","name":"_period","type":"uint256"}],"name":"toRedistribution","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"bytes32","name":"redistribution","type":"bytes32"}],"name":"toRedistributionDemurrageModifier","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"bytes32","name":"redistribution","type":"bytes32"}],"name":"toRedistributionPeriod","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"bytes32","name":"redistribution","type":"bytes32"}],"name":"toRedistributionSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"totalBurned","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSink","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_from","type":"address"},{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_newOwner","type":"address"}],"name":"transferOwnership","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}] +[{"inputs":[{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_symbol","type":"string"},{"internalType":"uint8","name":"_decimals","type":"uint8"},{"internalType":"int128","name":"_decayLevel","type":"int128"},{"internalType":"uint256","name":"_periodMinutes","type":"uint256"},{"internalType":"address","name":"_defaultSinkAddress","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_owner","type":"address"},{"indexed":true,"internalType":"address","name":"_spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_burner","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Burn","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"_oldCap","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_newCap","type":"uint256"}],"name":"Cap","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"int128","name":"_foo","type":"int128"},{"indexed":true,"internalType":"uint256","name":"_bar","type":"uint256"}],"name":"Debug","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"_period","type":"uint256"},{"indexed":true,"internalType":"uint256","name":"_periodCount","type":"uint256"},{"indexed":true,"internalType":"int128","name":"_oldAmount","type":"int128"},{"indexed":false,"internalType":"int128","name":"_newAmount","type":"int128"}],"name":"Decayed","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"_timestamp","type":"uint256"}],"name":"Expired","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_minter","type":"address"},{"indexed":true,"internalType":"address","name":"_beneficiary","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"_period","type":"uint256"}],"name":"Period","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_account","type":"address"},{"indexed":true,"internalType":"uint256","name":"_period","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Redistribution","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bool","name":"_final","type":"bool"},{"indexed":false,"internalType":"uint256","name":"_sealState","type":"uint256"}],"name":"SealStateChange","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_from","type":"address"},{"indexed":true,"internalType":"address","name":"_to","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[],"name":"acceptOwnership","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"actualPeriod","outputs":[{"internalType":"uint128","name":"","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_minter","type":"address"}],"name":"addWriter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"applyDemurrage","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_rounds","type":"uint256"}],"name":"applyDemurrageLimited","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"applyExpiry","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"baseBalanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"burn","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"burned","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"changePeriod","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_value","type":"uint256"},{"internalType":"uint256","name":"_period","type":"uint256"}],"name":"decayBy","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decayLevel","outputs":[{"internalType":"int128","name":"","type":"int128"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_minter","type":"address"}],"name":"deleteWriter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"demurrageAmount","outputs":[{"internalType":"int128","name":"","type":"int128"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_target","type":"uint256"}],"name":"demurrageCycles","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"demurrageTimestamp","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"expires","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_supply","type":"uint256"},{"internalType":"int128","name":"_demurrageAmount","type":"int128"}],"name":"getDistribution","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"components":[{"internalType":"uint32","name":"period","type":"uint32"},{"internalType":"uint72","name":"value","type":"uint72"},{"internalType":"uint64","name":"demurrage","type":"uint64"}],"internalType":"struct DemurrageTokenSingleNocap.redistributionItem","name":"_redistribution","type":"tuple"}],"name":"getDistributionFromRedistribution","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"_lastTimestamp","type":"uint256"}],"name":"getMinutesDelta","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_periodCount","type":"uint256"}],"name":"getPeriodTimeDelta","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint32","name":"period","type":"uint32"},{"internalType":"uint72","name":"value","type":"uint72"},{"internalType":"uint64","name":"demurrage","type":"uint64"}],"internalType":"struct DemurrageTokenSingleNocap.redistributionItem","name":"_redistribution","type":"tuple"}],"name":"isEmptyRedistribution","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"_state","type":"uint256"}],"name":"isSealed","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"lastPeriod","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"maxSealState","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"maxSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_beneficiary","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"mintTo","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"periodDuration","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"periodStart","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"redistributionCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"redistributions","outputs":[{"internalType":"uint32","name":"period","type":"uint32"},{"internalType":"uint72","name":"value","type":"uint72"},{"internalType":"uint64","name":"demurrage","type":"uint64"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_state","type":"uint256"}],"name":"seal","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"sealState","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_expirePeriod","type":"uint256"}],"name":"setExpirePeriod","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_cap","type":"uint256"}],"name":"setMaxSupply","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_sinkAddress","type":"address"}],"name":"setSinkAddress","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"sinkAddress","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes4","name":"_sum","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"sweep","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"toBaseAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_participants","type":"uint256"},{"internalType":"int128","name":"_demurrageModifier","type":"int128"},{"internalType":"uint256","name":"_value","type":"uint256"},{"internalType":"uint256","name":"_period","type":"uint256"}],"name":"toRedistribution","outputs":[{"components":[{"internalType":"uint32","name":"period","type":"uint32"},{"internalType":"uint72","name":"value","type":"uint72"},{"internalType":"uint64","name":"demurrage","type":"uint64"}],"internalType":"struct DemurrageTokenSingleNocap.redistributionItem","name":"","type":"tuple"}],"stateMutability":"pure","type":"function"},{"inputs":[{"components":[{"internalType":"uint32","name":"period","type":"uint32"},{"internalType":"uint72","name":"value","type":"uint72"},{"internalType":"uint64","name":"demurrage","type":"uint64"}],"internalType":"struct DemurrageTokenSingleNocap.redistributionItem","name":"_redistribution","type":"tuple"}],"name":"toRedistributionDemurrageModifier","outputs":[{"internalType":"int128","name":"","type":"int128"}],"stateMutability":"pure","type":"function"},{"inputs":[{"components":[{"internalType":"uint32","name":"period","type":"uint32"},{"internalType":"uint72","name":"value","type":"uint72"},{"internalType":"uint64","name":"demurrage","type":"uint64"}],"internalType":"struct DemurrageTokenSingleNocap.redistributionItem","name":"_redistribution","type":"tuple"}],"name":"toRedistributionPeriod","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"components":[{"internalType":"uint32","name":"period","type":"uint32"},{"internalType":"uint72","name":"value","type":"uint72"},{"internalType":"uint64","name":"demurrage","type":"uint64"}],"internalType":"struct DemurrageTokenSingleNocap.redistributionItem","name":"_redistribution","type":"tuple"}],"name":"toRedistributionSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"totalBurned","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalMinted","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSink","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_from","type":"address"},{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_newOwner","type":"address"}],"name":"transferOwnership","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}] diff --git a/python/erc20_demurrage_token/data/DemurrageTokenSingleNocap.metadata.json b/python/erc20_demurrage_token/data/DemurrageTokenSingleNocap.metadata.json @@ -0,0 +1 @@ +{"compiler":{"version":"0.8.18+commit.87f61d96"},"language":"Solidity","output":{"abi":[{"inputs":[{"internalType":"string","name":"_name","type":"string"},{"internalType":"string","name":"_symbol","type":"string"},{"internalType":"uint8","name":"_decimals","type":"uint8"},{"internalType":"int128","name":"_decayLevel","type":"int128"},{"internalType":"uint256","name":"_periodMinutes","type":"uint256"},{"internalType":"address","name":"_defaultSinkAddress","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_owner","type":"address"},{"indexed":true,"internalType":"address","name":"_spender","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_burner","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Burn","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"_oldCap","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_newCap","type":"uint256"}],"name":"Cap","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"int128","name":"_foo","type":"int128"},{"indexed":true,"internalType":"uint256","name":"_bar","type":"uint256"}],"name":"Debug","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"_period","type":"uint256"},{"indexed":true,"internalType":"uint256","name":"_periodCount","type":"uint256"},{"indexed":true,"internalType":"int128","name":"_oldAmount","type":"int128"},{"indexed":false,"internalType":"int128","name":"_newAmount","type":"int128"}],"name":"Decayed","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"_timestamp","type":"uint256"}],"name":"Expired","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_minter","type":"address"},{"indexed":true,"internalType":"address","name":"_beneficiary","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"_period","type":"uint256"}],"name":"Period","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_account","type":"address"},{"indexed":true,"internalType":"uint256","name":"_period","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Redistribution","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bool","name":"_final","type":"bool"},{"indexed":false,"internalType":"uint256","name":"_sealState","type":"uint256"}],"name":"SealStateChange","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"_from","type":"address"},{"indexed":true,"internalType":"address","name":"_to","type":"address"},{"indexed":false,"internalType":"uint256","name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[],"name":"acceptOwnership","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"actualPeriod","outputs":[{"internalType":"uint128","name":"","type":"uint128"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_minter","type":"address"}],"name":"addWriter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"applyDemurrage","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_rounds","type":"uint256"}],"name":"applyDemurrageLimited","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"applyExpiry","outputs":[{"internalType":"uint8","name":"","type":"uint8"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"approve","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"baseBalanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"burn","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"burned","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"changePeriod","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_value","type":"uint256"},{"internalType":"uint256","name":"_period","type":"uint256"}],"name":"decayBy","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decayLevel","outputs":[{"internalType":"int128","name":"","type":"int128"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"decimals","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"decreaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_minter","type":"address"}],"name":"deleteWriter","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"demurrageAmount","outputs":[{"internalType":"int128","name":"","type":"int128"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_target","type":"uint256"}],"name":"demurrageCycles","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"demurrageTimestamp","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"expires","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_supply","type":"uint256"},{"internalType":"int128","name":"_demurrageAmount","type":"int128"}],"name":"getDistribution","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"components":[{"internalType":"uint32","name":"period","type":"uint32"},{"internalType":"uint72","name":"value","type":"uint72"},{"internalType":"uint64","name":"demurrage","type":"uint64"}],"internalType":"struct DemurrageTokenSingleNocap.redistributionItem","name":"_redistribution","type":"tuple"}],"name":"getDistributionFromRedistribution","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"_lastTimestamp","type":"uint256"}],"name":"getMinutesDelta","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_periodCount","type":"uint256"}],"name":"getPeriodTimeDelta","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_spender","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"increaseAllowance","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"uint32","name":"period","type":"uint32"},{"internalType":"uint72","name":"value","type":"uint72"},{"internalType":"uint64","name":"demurrage","type":"uint64"}],"internalType":"struct DemurrageTokenSingleNocap.redistributionItem","name":"_redistribution","type":"tuple"}],"name":"isEmptyRedistribution","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"_state","type":"uint256"}],"name":"isSealed","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"lastPeriod","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"maxSealState","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"maxSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_beneficiary","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"mintTo","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"periodDuration","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"periodStart","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"redistributionCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"redistributions","outputs":[{"internalType":"uint32","name":"period","type":"uint32"},{"internalType":"uint72","name":"value","type":"uint72"},{"internalType":"uint64","name":"demurrage","type":"uint64"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_state","type":"uint256"}],"name":"seal","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"sealState","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_expirePeriod","type":"uint256"}],"name":"setExpirePeriod","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_cap","type":"uint256"}],"name":"setMaxSupply","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_sinkAddress","type":"address"}],"name":"setSinkAddress","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"sinkAddress","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes4","name":"_sum","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"sweep","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"toBaseAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_participants","type":"uint256"},{"internalType":"int128","name":"_demurrageModifier","type":"int128"},{"internalType":"uint256","name":"_value","type":"uint256"},{"internalType":"uint256","name":"_period","type":"uint256"}],"name":"toRedistribution","outputs":[{"components":[{"internalType":"uint32","name":"period","type":"uint32"},{"internalType":"uint72","name":"value","type":"uint72"},{"internalType":"uint64","name":"demurrage","type":"uint64"}],"internalType":"struct DemurrageTokenSingleNocap.redistributionItem","name":"","type":"tuple"}],"stateMutability":"pure","type":"function"},{"inputs":[{"components":[{"internalType":"uint32","name":"period","type":"uint32"},{"internalType":"uint72","name":"value","type":"uint72"},{"internalType":"uint64","name":"demurrage","type":"uint64"}],"internalType":"struct DemurrageTokenSingleNocap.redistributionItem","name":"_redistribution","type":"tuple"}],"name":"toRedistributionDemurrageModifier","outputs":[{"internalType":"int128","name":"","type":"int128"}],"stateMutability":"pure","type":"function"},{"inputs":[{"components":[{"internalType":"uint32","name":"period","type":"uint32"},{"internalType":"uint72","name":"value","type":"uint72"},{"internalType":"uint64","name":"demurrage","type":"uint64"}],"internalType":"struct DemurrageTokenSingleNocap.redistributionItem","name":"_redistribution","type":"tuple"}],"name":"toRedistributionPeriod","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[{"components":[{"internalType":"uint32","name":"period","type":"uint32"},{"internalType":"uint72","name":"value","type":"uint72"},{"internalType":"uint64","name":"demurrage","type":"uint64"}],"internalType":"struct DemurrageTokenSingleNocap.redistributionItem","name":"_redistribution","type":"tuple"}],"name":"toRedistributionSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"totalBurned","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalMinted","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSink","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalSupply","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_from","type":"address"},{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_newOwner","type":"address"}],"name":"transferOwnership","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"nonpayable","type":"function"}],"devdoc":{"kind":"dev","methods":{},"version":1},"userdoc":{"kind":"user","methods":{"balanceOf(address)":{"notice":"Implements ERC20"}},"version":1}},"settings":{"compilationTarget":{"DemurrageTokenSingleNocap.sol":"DemurrageTokenSingleNocap"},"evmVersion":"byzantium","libraries":{},"metadata":{"bytecodeHash":"ipfs"},"optimizer":{"enabled":false,"runs":200},"remappings":[]},"sources":{"DemurrageTokenSingleNocap.sol":{"keccak256":"0x4d508883f4f742db5cfc19e7f4a96119b3c45cf0b2d0f86b8157c3450141a1ee","license":"GPL-3.0-or-later","urls":["bzz-raw://3e361bbde265bdd3278332e011f18d51aea37970dd2288c977381052af284df8","dweb:/ipfs/QmNTPtMcm382X7VpoY45BBx7DHkdAmdE7f4X3GNcT1V424"]},"aux/ABDKMath64x64.sol":{"keccak256":"0x089b21db72314836305ba36b1773c3f0c2d5ac3a0f418820557c17c0e57c3c50","license":"BSD-4-Clause","urls":["bzz-raw://730ce8d750b0a6588f0d0724ace1c4d6dd90cf6dc0d2805b775b065fce147dfa","dweb:/ipfs/Qmdx7xRKvhXu1JtAVs25bER3LN2oGj2ZjW8jEvJpzQyXhc"]}},"version":1} diff --git a/python/erc20_demurrage_token/expiry.py b/python/erc20_demurrage_token/expiry.py @@ -0,0 +1,36 @@ +# external imports +from chainlib.eth.tx import ( + TxFactory, + TxFormat, + ) +from chainlib.eth.contract import ( + ABIContractEncoder, + ABIContractType, + abi_decode_single, + ) +from chainlib.eth.constant import ZERO_ADDRESS + + +class ExpiryContract(TxFactory): + + def set_expire_period(self, contract_address, sender_address, expire_timestamp, tx_format=TxFormat.JSONRPC): + enc = ABIContractEncoder() + enc.method('setExpirePeriod') + enc.typ(ABIContractType.UINT256) + enc.uint256(expire_timestamp) + data = enc.get() + tx = self.template(sender_address, contract_address, use_nonce=True) + tx = self.set_code(tx, data) + tx = self.finalize(tx, tx_format) + return tx + + + def expires(self, contract_address, sender_address=ZERO_ADDRESS): + return self.call_noarg('expires', contract_address, sender_address=sender_address) + + + @classmethod + def parse_expires(self, v): + return abi_decode_single(ABIContractType.UINT256, v) + + diff --git a/python/erc20_demurrage_token/runnable/#publish.py# b/python/erc20_demurrage_token/runnable/#publish.py# @@ -0,0 +1,176 @@ +"""Deploy sarafu token + +.. moduleauthor:: Louis Holbrook <dev@holbrook.no> +.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746 + +""" + +# standard imports +import sys +import os +import json +import argparse +import logging + +# external imports +import confini +from funga.eth.signer import EIP155Signer +from funga.eth.keystore.dict import DictKeystore +from chainlib.chain import ChainSpec +from chainlib.eth.nonce import ( + RPCNonceOracle, + OverrideNonceOracle, + ) +from chainlib.eth.gas import ( + RPCGasOracle, + OverrideGasOracle, + ) +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 +from erc20_demurrage_token import ( + DemurrageToken, + DemurrageTokenSettings, + ) + +logg = logging.getLogger() + +script_dir = os.path.dirname(__file__) +data_dir = os.path.join(script_dir, '..', 'data') + +config_dir = os.path.join(data_dir, 'config') + + +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 = (1 - (args.demurrage_level / 1000000)) ** (1 / config.get('TOKEN_REDISTRIBUTION_PERIOD')) + if v >= 1.0: + raise ValueError('demurrage level must be less than 100%') + demurrage_level = to_fixed(v) + logg.info('v {} demurrage level {}'.format(v, demurrage_level)) + 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('--redistribution-period', type=int, help='redistribution period, minutes (0 = deactivate)') # default 10080 = week +argparser.add_argument('--demurrage-level', dest='demurrage_level', type=int, help='demurrage level, ppm per period') +args = argparser.parse_args() + +logg = process_log(args, logg) + +config = Config() +config = process_config(config, arg, args, flags) +config = process_config_local(config, arg, args, flags) +logg.debug('config loaded:\n{}'.format(config)) + +settings = ChainSettings() +settings = process_settings(settings, config) +logg.debug('settings loaded:\n{}'.format(settings)) + + +#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(): + 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( + settings.get('SENDER_ADDRESS'), + token_settings, + ) + if settings.get('RPC_SEND'): + conn.do(o) + 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') + sys.exit(1) + # TODO: pass through translator for keys (evm tester uses underscore instead of camelcase) + address = r['contractAddress'] + + print(address) + else: + print(tx_hash_hex) + + else: + print(o) + + +if __name__ == '__main__': + main() diff --git a/python/erc20_demurrage_token/runnable/.#publish.py b/python/erc20_demurrage_token/runnable/.#publish.py @@ -0,0 +1 @@ +wor@gecon.733148:1676287007 +\ No newline at end of file diff --git a/python/erc20_demurrage_token/runnable/apply.py b/python/erc20_demurrage_token/runnable/apply.py @@ -1,152 +0,0 @@ -"""Deploy sarafu token - -.. moduleauthor:: Louis Holbrook <dev@holbrook.no> -.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746 - -""" - -# standard imports -import sys -import os -import json -import argparse -import logging -import datetime -import math - -# external imports -import confini -import chainlib.eth.cli -from chainlib.eth.block import ( - block_latest, - block_by_number, - Block, - ) -from chainlib.eth.connection import EthHTTPConnection -from chainlib.eth.tx import receipt -from chainlib.eth.constant import ZERO_ADDRESS -from hexathon import to_int as hex_to_int -import chainlib.eth.cli -from chainlib.eth.settings import process_settings -from chainlib.settings import ChainSettings -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 - -# local imports -import erc20_demurrage_token -from erc20_demurrage_token import ( - DemurrageToken, - DemurrageTokenSettings, - ) - -logg = logging.getLogger() - - -def process_config_local(config, arg, args, flags): - config.add(args.steps, '_STEPS', False) - return config - - -arg_flags = ArgFlag() -arg = Arg(arg_flags) -flags = arg_flags.STD_WRITE | arg_flags.EXEC | arg_flags.WALLET - -argparser = chainlib.eth.cli.ArgumentParser() -argparser = process_args(argparser, arg, flags) -argparser.add_argument('--steps', type=int, default=0, help='Max demurrage steps to apply per round') -args = argparser.parse_args() - -logg = process_log(args, logg) - -config = Config() -config = process_config(config, arg, args, flags) -config = process_config_local(config, arg, args, flags) -logg.debug('config loaded:\n{}'.format(config)) - -settings = ChainSettings() -settings = process_settings(settings, config) -logg.debug('settings loaded:\n{}'.format(settings)) - - -def main(): - chain_spec = settings.get('CHAIN_SPEC') - conn = settings.get('CONN') - o = block_latest() - r = conn.do(o) - - block_start_number = None - try: - block_start_number = hex_to_int(r) - except TypeError: - block_start_number = int(r) - - o = block_by_number(block_start_number) - r = conn.do(o) - - block_start = Block(r) - block_start_timestamp = block_start.timestamp - block_start_datetime = datetime.datetime.fromtimestamp(block_start_timestamp) - - gas_oracle = settings.get('FEE_ORACLE') - c = DemurrageToken(chain_spec, gas_oracle=gas_oracle) - o = c.demurrage_timestamp(settings.get('EXEC')) - r = conn.do(o) - - demurrage_timestamp = None - try: - demurrage_timestamp = hex_to_int(r) - except TypeError: - demurrage_timestamp = int(r) - demurrage_datetime = datetime.datetime.fromtimestamp(demurrage_timestamp) - - total_seconds = block_start_timestamp - demurrage_timestamp - total_steps = total_seconds / 60 - - if total_steps < 1.0: - logg.error('only {} seconds since last demurrage application, skipping'.format(total_seconds)) - return - - logg.debug('block start is at {} demurrage is at {} -> {} minutes'.format( - block_start_datetime, - demurrage_datetime, - total_steps, - )) - - rounds = 1 - if config.get('_STEPS') > 0: - rounds = math.ceil(total_steps / config.get('_STEPS')) - - logg.info('will perform {} rounds of {} steps'.format(rounds, config.get('_STEPS'))) - - last_tx_hash = None - for i in range(rounds): - signer = settings.get('SIGNER') - signer_address = settings.get('SENDER_ADDRESS') - - nonce_oracle = settings.get('NONCE_ORACLE') - - c = DemurrageToken(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=nonce_oracle) - (tx_hash_hex, o) = c.apply_demurrage(config.get('_EXEC_ADDRESS'), signer_address, limit=config.get('_STEPS')) - if settings.get('RPC_SEND'): - print(tx_hash_hex) - conn.do(o) - if config.true('_WAIT_ALL') or (i == rounds - 1 and 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') - sys.exit(1) - else: - print(o) - - - -if __name__ == '__main__': - main() diff --git a/python/erc20_demurrage_token/runnable/deploy.py b/python/erc20_demurrage_token/runnable/deploy.py @@ -1,126 +0,0 @@ -"""Deploy sarafu token - -.. moduleauthor:: Louis Holbrook <dev@holbrook.no> -.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746 - -""" - -# standard imports -import sys -import os -import json -import argparse -import logging - -# external imports -import confini -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.settings import process_settings -from chainlib.settings import ChainSettings -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 - -# local imports -import erc20_demurrage_token -from erc20_demurrage_token import ( - DemurrageToken, - DemurrageTokenSettings, - ) - -logg = logging.getLogger() - - -def process_config_local(config, arg, args, flags): - config.add(args.token_name, 'TOKEN_NAME', False) - config.add(args.token_symbol, 'TOKEN_SYMBOL', False) - config.add(args.token_decimals, 'TOKEN_DECIMALS', False) - config.add(args.sink_address, 'TOKEN_SINK_ADDRESS', False) - config.add(args.redistribution_period, 'TOKEN_REDISTRIBUTION_PERIOD', False) - config.add(args.demurrage_level, 'TOKEN_DEMURRAGE_LEVEL', False) - config.add(0, 'TOKEN_SUPPLY_LIMIT', False) - return config - - -arg_flags = ArgFlag() -arg = Arg(arg_flags) -flags = arg_flags.STD_WRITE | arg_flags.EXEC | arg_flags.WALLET - -argparser = chainlib.eth.cli.ArgumentParser() -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', dest='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') -args = argparser.parse_args() - -logg = process_log(args, logg) - -config = Config() -config = process_config(config, arg, args, flags) -config = process_config_local(config, arg, args, flags) -logg.debug('config loaded:\n{}'.format(config)) - -settings = ChainSettings() -settings = process_settings(settings, config) -logg.debug('settings loaded:\n{}'.format(settings)) - - -def main(): - chain_spec = settings.get('CHAIN_SPEC') - conn = settings.get('CONN') - signer = settings.get('SIGNER') - signer_address = settings.get('SENDER_ADDRESS') - - gas_oracle = settings.get('FEE_ORACLE') - nonce_oracle = settings.get('NONCE_ORACLE') - - c = DemurrageToken(chain_spec, signer=signer, gas_oracle=gas_oracle, nonce_oracle=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, - token_settings, - redistribute=config.true('_MULTI'), - cap=int(config.get('TOKEN_SUPPLY_LIMIT')), - ) - if settings.get('RPC_SEND'): - conn.do(o) - 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') - sys.exit(1) - # TODO: pass through translator for keys (evm tester uses underscore instead of camelcase) - address = r['contractAddress'] - - print(address) - else: - print(tx_hash_hex) - - else: - print(o) - - -if __name__ == '__main__': - main() diff --git a/python/erc20_demurrage_token/runnable/publish.py b/python/erc20_demurrage_token/runnable/publish.py @@ -0,0 +1,176 @@ +"""Deploy sarafu token + +.. moduleauthor:: Louis Holbrook <dev@holbrook.no> +.. pgp:: 0826EDA1702D1E87C6E2875121D2E7BB88C2A746 + +""" + +# standard imports +import sys +import os +import json +import argparse +import logging + +# external imports +import confini +from funga.eth.signer import EIP155Signer +from funga.eth.keystore.dict import DictKeystore +from chainlib.chain import ChainSpec +from chainlib.eth.nonce import ( + RPCNonceOracle, + OverrideNonceOracle, + ) +from chainlib.eth.gas import ( + RPCGasOracle, + OverrideGasOracle, + ) +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 +from erc20_demurrage_token import ( + DemurrageToken, + DemurrageTokenSettings, + ) + +logg = logging.getLogger() + +script_dir = os.path.dirname(__file__) +data_dir = os.path.join(script_dir, '..', 'data') + +config_dir = os.path.join(data_dir, 'config') + + +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 = (1 - (args.demurrage_level / 1000000)) ** (1 / config.get('TOKEN_REDISTRIBUTION_PERIOD')) + if v >= 1.0: + raise ValueError('demurrage level must be less than 100%') + demurrage_level = to_fixed(v) + logg.info('v {} demurrage level {}'.format(v, demurrage_level)) + 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('--redistribution-period', type=int, help='redistribution period, minutes (0 = deactivate)') # default 10080 = week +argparser.add_argument('--demurrage-level', dest='demurrage_level', type=int, help='demurrage level, ppm per period') +args = argparser.parse_args() + +logg = process_log(args, logg) + +config = Config() +config = process_config(config, arg, args, flags) +config = process_config_local(config, arg, args, flags) +logg.debug('config loaded:\n{}'.format(config)) + +settings = ChainSettings() +settings = process_settings(settings, config) +logg.debug('settings loaded:\n{}'.format(settings)) + + +#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(): + 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( + settings.get('SENDER_ADDRESS'), + token_settings, + ) + if settings.get('RPC_SEND'): + conn.do(o) + 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') + sys.exit(1) + # TODO: pass through translator for keys (evm tester uses underscore instead of camelcase) + address = r['contractAddress'] + + print(address) + else: + print(tx_hash_hex) + + else: + print(o) + + +if __name__ == '__main__': + main() diff --git a/python/erc20_demurrage_token/seal.py b/python/erc20_demurrage_token/seal.py @@ -0,0 +1,66 @@ +# standard imports +import enum + +# external imports +from chainlib.eth.constant import ZERO_ADDRESS +from chainlib.jsonrpc import JSONRPCRequest +from chainlib.eth.tx import ( + TxFactory, + TxFormat, + ) +from chainlib.eth.contract import ( + ABIContractEncoder, + ABIContractType, + abi_decode_single, + ) +from hexathon import ( + add_0x, + ) + +class ContractState(enum.IntEnum): + MINTER_STATE = 1 + SINK_STATE = 2 + EXPIRY_STATE = 4 + CAP_STATE = 8 + +CONTRACT_SEAL_STATE_MAX = 0 + +for v in dir(ContractState): + if len(v) > 6 and v[-6:] == '_STATE': + CONTRACT_SEAL_STATE_MAX += getattr(ContractState, v).value + + +class SealedContract(TxFactory): + + def seal(self, contract_address, sender_address, seal, tx_format=TxFormat.JSONRPC): + enc = ABIContractEncoder() + enc.method('seal') + enc.typ(ABIContractType.UINT256) + enc.uint256(seal) + data = enc.get() + tx = self.template(sender_address, contract_address, use_nonce=True) + tx = self.set_code(tx, data) + tx = self.finalize(tx, tx_format) + return tx + + + def is_sealed(self, contract_address, v, sender_address=ZERO_ADDRESS, id_generator=None): + j = JSONRPCRequest(id_generator) + o = j.template() + o['method'] = 'eth_call' + enc = ABIContractEncoder() + enc.method('isSealed') + enc.typ(ABIContractType.UINT256) + enc.uint256(v) + data = add_0x(enc.get()) + tx = self.template(sender_address, contract_address) + tx = self.set_code(tx, data) + o['params'].append(self.normalize(tx)) + o['params'].append('latest') + o = j.finalize(o) + return o + + + @classmethod + def parse_is_sealed(self, v): + return abi_decode_single(ABIContractType.BOOLEAN, v) diff --git a/python/erc20_demurrage_token/token.py b/python/erc20_demurrage_token/token.py @@ -10,6 +10,7 @@ from chainlib.eth.tx import ( from chainlib.hash import keccak256_string_to_hex from chainlib.eth.contract import ( ABIContractEncoder, + ABIContractDecoder, ABIContractType, abi_decode_single, ) @@ -20,13 +21,38 @@ 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.seal import SealedContract +from erc20_demurrage_token.expiry import ExpiryContract logg = logging.getLogger(__name__) +class DemurrageRedistribution: + + def __init__(self, v): + d = ABIContractDecoder() + v = strip_0x(v) + d.typ(ABIContractType.UINT256) + d.typ(ABIContractType.UINT256) + d.typ(ABIContractType.BYTES32) + d.val(v[:64]) + d.val(v[64:128]) + d.val(v[128:192]) + r = d.decode() + + self.period = r[0] + self.value = r[1] + self.demurrage = from_fixed(r[2]) + + + def __str__(self): + return 'period {} value {} demurrage {}'.format(self.period, self.value, self.demurrage) + + class DemurrageTokenSettings: def __init__(self): @@ -47,59 +73,40 @@ class DemurrageTokenSettings: ) -class DemurrageToken(ERC20): +class DemurrageToken(ERC20, SealedContract, ExpiryContract): __abi = {} __bytecode = {} - valid_modes = [ - 'MultiNocap', - 'SingleNocap', - 'MultiCap', - 'SingleCap', - ] - - def constructor(self, sender_address, settings, redistribute=True, 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) - enc = ABIContractEncoder() - enc.string(settings.name) - enc.string(settings.symbol) - enc.uint256(settings.decimals) - enc.uint256(settings.demurrage_level) - enc.uint256(settings.period_minutes) - enc.address(settings.sink_address) - if cap > 0: - enc.uint256(cap) - code += enc.get() + + def constructor(self, sender_address, settings, tx_format=TxFormat.JSONRPC, version=None): + code = self.cargs(settings.name, settings.symbol, settings.decimals, settings.demurrage_level, settings.period_minutes, settings.sink_address, version=version) tx = self.template(sender_address, None, use_nonce=True) tx = self.set_code(tx, code) return self.finalize(tx, tx_format) @staticmethod - def gas(code=None): - return 4000000 + def cargs(name, symbol, decimals, demurrage_level, period_minutes, sink_address, version=None): + code = DemurrageToken.bytecode() + enc = ABIContractEncoder() + enc.string(name) + enc.string(symbol) + enc.uint256(decimals) + enc.uint256(demurrage_level) + enc.uint256(period_minutes) + enc.address(sink_address) + code += enc.get() + return code @staticmethod - def __to_contract_name(multi, cap): - name = 'DemurrageToken' - if multi: - name += 'Multi' - else: - name += 'Single' - if cap: - name += 'Cap' - else: - name += 'Nocap' - logg.debug('bytecode name {}'.format(name)) - return name + def gas(code=None): + return 7000000 @staticmethod - def abi(multi=True, cap=False): - name = DemurrageToken.__to_contract_name(multi, cap) + def abi(): + name = 'DemurrageTokenSingleNocap' if DemurrageToken.__abi.get(name) == None: f = open(os.path.join(data_dir, name + '.json'), 'r') DemurrageToken.__abi[name] = json.load(f) @@ -108,8 +115,8 @@ class DemurrageToken(ERC20): @staticmethod - def bytecode(multi=True, cap=False): - name = DemurrageToken.__to_contract_name(multi, cap) + def bytecode(version=None): + name = 'DemurrageTokenSingleNocap' if DemurrageToken.__bytecode.get(name) == None: f = open(os.path.join(data_dir, name + '.bin'), 'r') DemurrageToken.__bytecode[name] = f.read() @@ -145,9 +152,14 @@ class DemurrageToken(ERC20): return tx + # backwards compatibility def add_minter(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC): + return self.add_writer(contract_address, sender_address, address, tx_format=tx_format) + + + def add_writer(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC): enc = ABIContractEncoder() - enc.method('addMinter') + enc.method('addWriter') enc.typ(ABIContractType.ADDRESS) enc.address(address) data = enc.get() @@ -157,9 +169,26 @@ class DemurrageToken(ERC20): return tx + def set_max_supply(self, contract_address, sender_address, cap, tx_format=TxFormat.JSONRPC): + enc = ABIContractEncoder() + enc.method('setMaxSupply') + enc.typ(ABIContractType.UINT256) + enc.uint256(cap) + data = enc.get() + tx = self.template(sender_address, contract_address, use_nonce=True) + tx = self.set_code(tx, data) + tx = self.finalize(tx, tx_format) + return tx + + + # backwards compatibility def remove_minter(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC): + return self.delete_writer(contract_address, sender_address, address, tx_format=tx_format) + + + def delete_writer(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC): enc = ABIContractEncoder() - enc.method('removeMinter') + enc.method('deleteWriter') enc.typ(ABIContractType.ADDRESS) enc.address(address) data = enc.get() @@ -280,18 +309,18 @@ class DemurrageToken(ERC20): return o - def to_redistribution(self, contract_address, participants, demurrage_modifier_ppm, value, period, sender_address=ZERO_ADDRESS, id_generator=None): + def to_redistribution(self, contract_address, participants, demurrage_modifier, value, period, sender_address=ZERO_ADDRESS, id_generator=None): j = JSONRPCRequest(id_generator) o = j.template() o['method'] = 'eth_call' enc = ABIContractEncoder() enc.method('toRedistribution') enc.typ(ABIContractType.UINT256) - enc.typ(ABIContractType.UINT256) + enc.typ_literal('int128') enc.typ(ABIContractType.UINT256) enc.typ(ABIContractType.UINT256) enc.uint256(participants) - enc.uint256(demurrage_modifier_ppm) + enc.uint256(demurrage_modifier) enc.uint256(value) enc.uint256(period) data = add_0x(enc.get()) @@ -310,8 +339,11 @@ class DemurrageToken(ERC20): o['method'] = 'eth_call' enc = ABIContractEncoder() enc.method('toRedistributionPeriod') - enc.typ(ABIContractType.BYTES32) - enc.bytes32(redistribution) + v = strip_0x(redistribution) + enc.typ_literal('(uint32,uint72,uint64)') + enc.bytes32(v[:64]) + enc.bytes32(v[64:128]) + enc.bytes32(v[128:192]) data = add_0x(enc.get()) tx = self.template(sender_address, contract_address) tx = self.set_code(tx, data) @@ -321,22 +353,26 @@ class DemurrageToken(ERC20): return o - def to_redistribution_participants(self, contract_address, redistribution, sender_address=ZERO_ADDRESS, id_generator=None): - j = JSONRPCRequest(id_generator) - o = j.template() - o['method'] = 'eth_call' - enc = ABIContractEncoder() - enc.method('toRedistributionParticipants') - enc.typ(ABIContractType.BYTES32) - enc.bytes32(redistribution) - data = add_0x(enc.get()) - tx = self.template(sender_address, contract_address) - tx = self.set_code(tx, data) - o['params'].append(self.normalize(tx)) - o['params'].append('latest') - o = j.finalize(o) - return o - +# def to_redistribution_participants(self, contract_address, redistribution, sender_address=ZERO_ADDRESS, id_generator=None): +# j = JSONRPCRequest(id_generator) +# o = j.template() +# o['method'] = 'eth_call' +# enc = ABIContractEncoder() +# enc.method('toRedistributionParticipants') +# v = strip_0x(redistribution) +# enc.typ_literal('(uint32,uint72,uint104)') +# #enc.typ(ABIContractType.BYTES32) +# enc.bytes32(v[:64]) +# enc.bytes32(v[64:128]) +# enc.bytes32(v[128:192]) +# data = add_0x(enc.get()) +# tx = self.template(sender_address, contract_address) +# tx = self.set_code(tx, data) +# o['params'].append(self.normalize(tx)) +# o['params'].append('latest') +# o = j.finalize(o) +# return o +# def to_redistribution_supply(self, contract_address, redistribution, sender_address=ZERO_ADDRESS, id_generator=None): j = JSONRPCRequest(id_generator) @@ -344,8 +380,11 @@ class DemurrageToken(ERC20): o['method'] = 'eth_call' enc = ABIContractEncoder() enc.method('toRedistributionSupply') - enc.typ(ABIContractType.BYTES32) - enc.bytes32(redistribution) + v = strip_0x(redistribution) + enc.typ_literal('(uint32,uint72,uint64)') + enc.bytes32(v[:64]) + enc.bytes32(v[64:128]) + enc.bytes32(v[128:192]) data = add_0x(enc.get()) tx = self.template(sender_address, contract_address) tx = self.set_code(tx, data) @@ -361,8 +400,11 @@ class DemurrageToken(ERC20): o['method'] = 'eth_call' enc = ABIContractEncoder() enc.method('toRedistributionDemurrageModifier') - enc.typ(ABIContractType.BYTES32) - enc.bytes32(redistribution) + v = strip_0x(redistribution) + enc.typ_literal('(uint32,uint72,uint64)') + enc.bytes32(v[:64]) + enc.bytes32(v[64:128]) + enc.bytes32(v[128:192]) data = add_0x(enc.get()) tx = self.template(sender_address, contract_address) tx = self.set_code(tx, data) @@ -389,6 +431,31 @@ class DemurrageToken(ERC20): return o + def set_sink_address(self, contract_address, sender_address, address, tx_format=TxFormat.JSONRPC): + enc = ABIContractEncoder() + enc.method('setSinkAddress') + enc.typ(ABIContractType.ADDRESS) + enc.address(address) + data = enc.get() + tx = self.template(sender_address, contract_address, use_nonce=True) + tx = self.set_code(tx, data) + tx = self.finalize(tx, tx_format) + return tx + + + def sweep(self, contract_address, sender_address, recipient_address, tx_format=TxFormat.JSONRPC): + enc = ABIContractEncoder() + enc.method('sweep') + enc.typ(ABIContractType.ADDRESS) + enc.address(recipient_address) + data = enc.get() + tx = self.template(sender_address, contract_address, use_nonce=True) + tx = self.set_code(tx, data) + tx = self.finalize(tx, tx_format) + return tx + + + def apply_demurrage(self, contract_address, sender_address, limit=0, tx_format=TxFormat.JSONRPC): if limit == 0: return self.transact_noarg('applyDemurrage', contract_address, sender_address) @@ -420,8 +487,8 @@ class DemurrageToken(ERC20): return tx - def tax_level(self, contract_address, sender_address=ZERO_ADDRESS): - return self.call_noarg('taxLevel', contract_address, sender_address=sender_address) + def decay_level(self, contract_address, sender_address=ZERO_ADDRESS): + return self.call_noarg('decayLevel', contract_address, sender_address=sender_address) def resolution_factor(self, contract_address, sender_address=ZERO_ADDRESS): @@ -448,28 +515,28 @@ class DemurrageToken(ERC20): return self.call_noarg('demurrageTimestamp', contract_address, sender_address=sender_address) - def supply_cap(self, contract_address, sender_address=ZERO_ADDRESS): - return self.call_noarg('supplyCap', contract_address, sender_address=sender_address) - - - def grow_by(self, contract_address, value, period, sender_address=ZERO_ADDRESS, id_generator=None): - j = JSONRPCRequest(id_generator) - o = j.template() - o['method'] = 'eth_call' - enc = ABIContractEncoder() - enc.method('growBy') - enc.typ(ABIContractType.UINT256) - enc.typ(ABIContractType.UINT256) - enc.uint256(value) - enc.uint256(period) - data = add_0x(enc.get()) - tx = self.template(sender_address, contract_address) - tx = self.set_code(tx, data) - o['params'].append(self.normalize(tx)) - o['params'].append('latest') - o = j.finalize(o) - return o - + def max_supply(self, contract_address, sender_address=ZERO_ADDRESS): + return self.call_noarg('maxSupply', contract_address, sender_address=sender_address) + + +# def grow_by(self, contract_address, value, period, sender_address=ZERO_ADDRESS, id_generator=None): +# j = JSONRPCRequest(id_generator) +# o = j.template() +# o['method'] = 'eth_call' +# enc = ABIContractEncoder() +# enc.method('growBy') +# enc.typ(ABIContractType.UINT256) +# enc.typ(ABIContractType.UINT256) +# enc.uint256(value) +# enc.uint256(period) +# data = add_0x(enc.get()) +# tx = self.template(sender_address, contract_address) +# tx = self.set_code(tx, data) +# o['params'].append(self.normalize(tx)) +# o['params'].append('latest') +# o = j.finalize(o) +# return o +# def decay_by(self, contract_address, value, period, sender_address=ZERO_ADDRESS, id_generator=None): j = JSONRPCRequest(id_generator) @@ -497,7 +564,7 @@ class DemurrageToken(ERC20): enc = ABIContractEncoder() enc.method('getDistribution') enc.typ(ABIContractType.UINT256) - enc.typ(ABIContractType.UINT256) + enc.typ_literal('int128') enc.uint256(supply) enc.uint256(demurrage_amount) data = add_0x(enc.get()) @@ -515,8 +582,11 @@ class DemurrageToken(ERC20): o['method'] = 'eth_call' enc = ABIContractEncoder() enc.method('getDistributionFromRedistribution') - enc.typ(ABIContractType.BYTES32) - enc.bytes32(redistribution) + v = strip_0x(redistribution) + enc.typ_literal('(uint32,uint72,uint64)') + enc.bytes32(v[:64]) + enc.bytes32(v[64:128]) + enc.bytes32(v[128:192]) data = add_0x(enc.get()) tx = self.template(sender_address, contract_address) tx = self.set_code(tx, data) @@ -544,7 +614,8 @@ class DemurrageToken(ERC20): @classmethod def parse_demurrage_amount(self, v): - return abi_decode_single(ABIContractType.UINT256, v) + # return abi_decode_single(ABIContractType.UINT256, v) + return from_fixed(v) @classmethod @@ -559,8 +630,8 @@ class DemurrageToken(ERC20): @classmethod def parse_redistributions(self, v): - return abi_decode_single(ABIContractType.BYTES32, v) - + return strip_0x(v) + @classmethod def parse_account_period(self, v): @@ -598,7 +669,7 @@ class DemurrageToken(ERC20): @classmethod - def parse_tax_level(self, v): + def parse_decay_level(self, v): return abi_decode_single(ABIContractType.UINT256, v) @@ -610,3 +681,26 @@ class DemurrageToken(ERC20): @classmethod def parse_total_burned(self, v): return abi_decode_single(ABIContractType.UINT256, v) + + +def bytecode(**kwargs): + return DemurrageToken.bytecode(version=kwargs.get('version')) + + +def create(**kwargs): + return DemurrageToken.cargs( + kwargs['name'], + kwargs['symbol'], + kwargs['decimals'], + kwargs['demurragelevel'], + kwargs['redistributionperiod'], + kwargs['sinkaddress'], + version=kwargs.get('version')) + + +def args(v): + if v == 'create': + return (['name', 'symbol', 'decimals', 'demurragelevel', 'redistributionperiod', 'sinkaddress'], ['version'],) + elif v == 'default' or v == 'bytecode': + return ([], ['version'],) + raise ValueError('unknown command: ' + v) diff --git 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,20 +20,21 @@ from erc20_demurrage_token import ( DemurrageTokenSettings, DemurrageToken, ) +from dexif import * -logg = logging.getLogger(__name__) +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 = int(60/BLOCKTIME) * 60 * 24 * 30 # month -PERIOD = 10 +PERIOD = 43200 class TestTokenDeploy: - def __init__(self, rpc, token_symbol='FOO', token_name='Foo Token', sink_address=ZERO_ADDRESS, supply=10**12, tax_level=TAX_LEVEL, period=PERIOD): + """tax level is ppm, 1000000 = 100%""" + def __init__(self, rpc, token_symbol='FOO', token_name='Foo Token', sink_address=ZERO_ADDRESS, 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 @@ -57,25 +60,11 @@ class TestTokenDeploy: except TypeError: self.start_time = int(r['timestamp']) - self.default_supply = supply - self.default_supply_cap = int(self.default_supply * 10) - - def deploy(self, rpc, deployer_address, interface, mode, supply_cap=10**12): + def publish(self, rpc, publisher_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(publisher_address, self.settings) r = rpc.do(o) o = receipt(tx_hash) @@ -100,25 +89,43 @@ class TestDemurrage(EthTesterCase): period = getattr(self, 'period') except AttributeError as e: pass - self.deployer = TestTokenDeploy(self.rpc, period=period) - self.default_supply = self.deployer.default_supply - self.default_supply_cap = self.deployer.default_supply_cap + self.publisher = TestTokenDeploy(self.rpc, period=period, sink_address=self.accounts[9]) + self.default_supply = 0 + self.default_supply_cap = 0 self.start_block = None self.address = None 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) - self.start_block = self.deployer.start_block - self.start_time = self.deployer.start_time - self.tax_level = self.deployer.tax_level - self.period_seconds = self.deployer.period_seconds - self.sink_address = self.deployer.sink_address + def publish(self, interface): + self.address = self.publisher.publish(self.rpc, self.accounts[0], interface, supply_cap=self.default_supply_cap) + self.start_block = self.publisher.start_block + self.start_time = self.publisher.start_time + self.tax_level = self.publisher.tax_level + self.period_seconds = self.publisher.period_seconds + self.sink_address = self.publisher.sink_address 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_greater(self, v, target, tolerance_ppm): + greater_target = target + (target * (tolerance_ppm / 1000000)) + self.assertLessEqual(v, greater_target) + self.assertGreaterEqual(v, target) + logg.debug('asserted within greater {} >= {} >= {}'.format(greater_target, v, target)) + + def assert_within_lower(self, v, target, tolerance_ppm): lower_target = target - (target * (tolerance_ppm / 1000000)) self.assertGreaterEqual(v, lower_target) @@ -126,11 +133,10 @@ class TestDemurrage(EthTesterCase): logg.debug('asserted within lower {} <= {} <= {}'.format(lower_target, v, target)) - def assert_within_greater(self, v, target, tolerance_ppm): - higher_target = target + (target * (tolerance_ppm / 1000000)) - self.assertLessEqual(v, higher_target) - self.assertGreaterEqual(v, target) - logg.debug('asserted within lower {} <= {} <= {}'.format(target, v, higher_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): @@ -145,115 +151,7 @@ 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.publish(c) - 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.default_supply = 10**12 + self.default_supply_cap = self.default_supply diff --git a/python/erc20_demurrage_token/unittest/newbase.py b/python/erc20_demurrage_token/unittest/newbase.py @@ -0,0 +1,146 @@ +# standard imports +import logging +import os +import math + +# external imports +from chainlib.eth.unittest.ethtester import EthTesterCase +from chainlib.eth.tx import ( + receipt, + ) +from chainlib.eth.block import ( + block_latest, + block_by_number, + ) +from chainlib.eth.nonce import RPCNonceOracle +from chainlib.eth.constant import ZERO_ADDRESS + +# local imports +from erc20_demurrage_token import ( + DemurrageTokenSettings, + DemurrageToken, + ) +from dexif import * + +logg = logging.getLogger() + +TAX_LEVEL = int(10000 * 2) # 2% +PERIOD = 43200 # 30 days in minutes + +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 + + self.settings = DemurrageTokenSettings() + self.settings.name = token_name + self.settings.symbol = token_symbol + self.settings.decimals = 6 + 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 + logg.debug('using demurrage token settings: {}'.format(self.settings)) + + o = block_latest() + self.start_block = rpc.do(o) + + o = block_by_number(self.start_block, include_tx=False) + r = rpc.do(o) + + try: + self.start_time = int(r['timestamp'], 16) + except TypeError: + self.start_time = int(r['timestamp']) + + self.default_supply = supply + self.default_supply_cap = 0 + + + def deploy(self, rpc, deployer_address, interface, supply_cap=0): + tx_hash = None + o = None + (tx_hash, o) = interface.constructor(deployer_address, self.settings, redistribute=False, cap=0) + + r = rpc.do(o) + o = receipt(tx_hash) + r = rpc.do(o) + assert r['status'] == 1 + self.start_block = r['block_number'] + self.address = r['contract_address'] + + o = block_by_number(r['block_number']) + r = rpc.do(o) + self.start_time = r['timestamp'] + + return self.address + + +class TestDemurrage(EthTesterCase): + + def setUp(self): + super(TestDemurrage, self).setUp() + period = PERIOD + try: + period = getattr(self, 'period') + except AttributeError as e: + pass + self.deployer = TestTokenDeploy(self.rpc, period=period) + self.default_supply = self.deployer.default_supply + self.default_supply_cap = self.deployer.default_supply_cap + self.start_block = None + self.address = None + self.start_time = None + + + 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 + self.period_seconds = self.deployer.period_seconds + self.sink_address = self.deployer.sink_address + + 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) + self.assertLessEqual(v, target) + 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 + + +class TestDemurrageDefault(TestDemurrage): + + def setUp(self): + super(TestDemurrageDefault, self).setUp() + + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + + self.deploy(c) diff --git a/python/requirements.txt b/python/requirements.txt @@ -1,3 +1,5 @@ -chainlib-eth~=0.4.6 -eth-erc20~=0.5.1 +chainlib-eth~=0.4.11 +chainlib~=0.4.8 +eth-erc20~=0.5.5 funga-eth~=0.6.0 +dexif~=0.0.2 diff --git a/python/run_tests.sh b/python/run_tests.sh @@ -1,44 +1,14 @@ #!/bin/bash -set -x +set -a set -e - -export PYTHONPATH=. - -#modes=(MultiNocap MultiCap SingleCap SingleNocap) -#modes=(SingleCap SingleNocap) # other contracts need to be updted -modes=(SingleNocap) # other contracts need to be updted -for m in ${modes[@]}; do - ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_basic.py - ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_growth.py - ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_amounts.py - ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_single.py - ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_burn.py -done - -#modes=(SingleCap) # other contracts need to be updted -modes=() -for m in ${modes[@]}; do - ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_period.py -done - -modes=(SingleNocap) # other contracts need to be updted -for m in ${modes[@]}; do - ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_redistribution_unit.py - ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_redistribution_single.py -done - -#modes=(MultiCap SingleCap) -modes=() -for m in ${modes[@]}; do - ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_cap.py +set -x +default_pythonpath=$PYTHONPATH:. +export PYTHONPATH=${default_pythonpath:-.} +>&2 echo using pythonpath $PYTHONPATH +for f in `ls tests/test_*.py`; do + python $f done - -#modes=(MultiCap MultiNocap) -#for m in ${modes[@]}; do -# ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_remainder.py -# ERC20_DEMURRAGE_TOKEN_TEST_MODE=$m python tests/test_redistribution.py -#done - -set +e set +x +set +e +set +a diff --git a/python/setup.cfg b/python/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = erc20-demurrage-token -version = 0.2.0 +version = 0.4.2 description = ERC20 token with redistributed continual demurrage author = Louis Holbrook author_email = dev@holbrook.no @@ -40,5 +40,4 @@ packages = [options.entry_points] console_scripts = - erc20-demurrage-token-deploy = erc20_demurrage_token.runnable.deploy:main - erc20-demurrage-token-apply = erc20_demurrage_token.runnable.apply:main + erc20-demurrage-token-publish = erc20_demurrage_token.runnable.publish:main diff --git a/python/tests/bench.py b/python/tests/bench.old diff --git a/python/tests/bench_gas.py b/python/tests/bench_gas.py @@ -0,0 +1,167 @@ +# standard imports +import os +import unittest +import json +import logging +import datetime + +# external imports +from chainlib.eth.constant import ZERO_ADDRESS +from chainlib.eth.nonce import RPCNonceOracle +from chainlib.eth.tx import receipt +from chainlib.eth.block import ( + block_latest, + block_by_number, + ) + +# local imports +from erc20_demurrage_token import DemurrageToken + +# test imports +from erc20_demurrage_token.unittest import TestDemurrageDefault + +logging.basicConfig(level=logging.INFO) +logg = logging.getLogger() + +testdir = os.path.dirname(__file__) + + + +class BenchBasic(TestDemurrageDefault): + + def setUp(self): + super(BenchBasic, self).setUp() + self.bench = { + 'mint': None, + 'transfer_light': None, + 'transfer_heavy': None, + 'approve': None, + 'transfer_from': None, + 'period_light': None, + 'period_heavy': None, + 'period_catchup': None, + 'demurrage': None, + } + + + def test_bench_min(self): + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1024) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + self.bench['mint'] = r['gas_used'] + + nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash, o) = c.transfer(self.address, self.accounts[1], self.accounts[2], 512) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + self.bench['transfer_light'] = r['gas_used'] + + (tx_hash, o) = c.approve(self.address, self.accounts[1], self.accounts[0], 512) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + self.bench['approve'] = r['gas_used'] + + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash, o) = c.transfer_from(self.address, self.accounts[0], self.accounts[1], self.accounts[3], 256) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + self.bench['transfer_from'] = r['gas_used'] + + z = 0 + for i in range(100): + self.backend.time_travel(self.start_time + int(self.period_seconds / 2) + (10 * (i * (i + 1)))) + (tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0]) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + z += r['gas_used'] + logg.info('demurrage round {} gas {}'.format(i, r['gas_used'])) + z /= 100 + self.bench['demurrage'] = int(z) + + z = 0 + for i in range(100): + self.backend.time_travel(self.start_time + (self.period_seconds * (i + 1))) + (tx_hash, o) = c.change_period(self.address, self.accounts[0]) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + z += r['gas_used'] + logg.info('period with demurrage round {} gas {}'.format(i, r['gas_used'])) + + z /= 100 + self.bench['period_heavy'] = int(z) + + z = 0 + for i in range(100): + self.backend.time_travel(self.start_time + (self.period_seconds * ((i + 101)))) + (tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0]) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + (tx_hash, o) = c.change_period(self.address, self.accounts[0]) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + z += r['gas_used'] + logg.info('period without demurrage round {} gas {}'.format(i, r['gas_used'])) + + z /= 100 + self.bench['period_light'] = int(z) + + z = 0 + self.backend.time_travel(self.start_time + (self.period_seconds * 401)) + for i in range(100): + (tx_hash, o) = c.change_period(self.address, self.accounts[0]) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + z += r['gas_used'] + logg.info('period catchup round {} gas {}'.format(i, r['gas_used'])) + + z /= 100 + self.bench['period_catchup'] = int(z) + + self.backend.time_travel(self.start_time + (self.period_seconds * 501)) + (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[2], 1024) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + self.backend.time_travel(self.start_time + (self.period_seconds * 502)) + + nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash, o) = c.transfer(self.address, self.accounts[2], self.accounts[4], 1) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + self.bench['transfer_heavy'] = r['gas_used'] + + print(json.dumps(self.bench)) + + + + + +if __name__ == '__main__': + unittest.main() diff --git a/python/tests/test_amounts.py b/python/tests/test_amounts.py @@ -13,7 +13,7 @@ from chainlib.eth.tx import receipt from erc20_demurrage_token import DemurrageToken # test imports -from erc20_demurrage_token.unittest.base import TestDemurrageDefault +from erc20_demurrage_token.unittest import TestDemurrageDefault logging.basicConfig(level=logging.DEBUG) logg = logging.getLogger() @@ -27,6 +27,9 @@ class TestAmounts(TestDemurrageDefault): c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1000) r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) self.backend.time_travel(self.start_time + self.period_seconds) @@ -36,7 +39,7 @@ class TestAmounts(TestDemurrageDefault): o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) r = self.rpc.do(o) balance = c.parse_balance(r) - self.assertEqual(balance, 817) + self.assertEqual(balance, 980) (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1000) r = self.rpc.do(o) @@ -44,7 +47,7 @@ class TestAmounts(TestDemurrageDefault): o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) r = self.rpc.do(o) balance = c.parse_balance(r) - self.assert_within_lower(balance, 1817, 750) + self.assert_within_lower(balance, 1980, 750) self.backend.time_travel(self.start_time + self.period_seconds * 2) @@ -57,8 +60,8 @@ class TestAmounts(TestDemurrageDefault): expected_balance = ((1 - self.tax_level / 1000000) ** 10) * 1000 expected_balance += ((1 - self.tax_level / 1000000) ** 20) * 1000 - self.assert_within_lower(balance, expected_balance, 500) - + #self.assert_within_lower(balance, expected_balance, 500) + self.assertEqual(balance, 1940) def test_transfers(self): nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) @@ -74,7 +77,7 @@ class TestAmounts(TestDemurrageDefault): o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) r = self.rpc.do(o) balance = c.parse_balance(r) - self.assertEqual(balance, 1634) + self.assertEqual(balance, 1960) nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) @@ -84,7 +87,7 @@ class TestAmounts(TestDemurrageDefault): o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) r = self.rpc.do(o) balance = c.parse_balance(r) - self.assertEqual(balance, 1134) + self.assertEqual(balance, 1460) o = c.balance_of(self.address, self.accounts[2], sender_address=self.accounts[0]) r = self.rpc.do(o) @@ -99,25 +102,46 @@ class TestAmounts(TestDemurrageDefault): r = self.rpc.do(o) cases = [ - (61, 1960), - (121, 1920), - (181, 1882), - (241, 1844), - (301, 1807), - (361, 1771), - (421, 1736), - (481, 1701), - (541, 1667), - (601, 1634), + (60, 1960), + (120, 1920), + (180, 1882), + (240, 1844), + (300, 1807), + (360, 1771), + (420, 1736), + (480, 1701), + (540, 1667), + (600, 1634), ] for case in cases: - self.backend.time_travel(self.start_time + case[0]) + self.backend.time_travel(self.start_time + int(case[0] * (self.period_seconds / 60))) o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) r = self.rpc.do(o) balance = c.parse_balance(r) - self.assertEqual(balance, case[1]) + self.assert_within_lower(balance, case[1], 10000) + + + def test_sweep(self): + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[0], 2000) + r = self.rpc.do(o) + (tx_hash, o) = c.sweep(self.address, self.accounts[0], self.accounts[1]) + self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + o = c.balance_of(self.address, self.accounts[0], sender_address=self.accounts[0]) + r = self.rpc.do(o) + self.assertEqual(c.parse_balance(r), 0) + + o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) + r = self.rpc.do(o) + self.assert_within(c.parse_balance(r), 2000, 1) + if __name__ == '__main__': unittest.main() diff --git a/python/tests/test_basic.py b/python/tests/test_basic.py @@ -18,7 +18,7 @@ from chainlib.eth.block import ( from erc20_demurrage_token import DemurrageToken # test imports -from erc20_demurrage_token.unittest.base import TestDemurrageDefault +from erc20_demurrage_token.unittest import TestDemurrageDefault logging.basicConfig(level=logging.DEBUG) logg = logging.getLogger() @@ -55,7 +55,8 @@ class TestBasic(TestDemurrageDefault): def test_apply_demurrage_limited(self): - modifier = (10 ** 28) + #modifier = (10 ** 28) + modifier = 1 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) @@ -65,8 +66,8 @@ class TestBasic(TestDemurrageDefault): demurrage_amount = c.parse_demurrage_amount(r) self.assertEqual(modifier, demurrage_amount) - self.backend.time_travel(self.start_time + 120) - (tx_hash, o) = c.apply_demurrage(self.address, sender_address=self.accounts[0], limit=1) + self.backend.time_travel(self.start_time + (60 * 43200)) + (tx_hash, o) = c.apply_demurrage(self.address, sender_address=self.accounts[0], limit=20000) r = self.rpc.do(o) o = receipt(tx_hash) r = self.rpc.do(o) @@ -75,13 +76,12 @@ class TestBasic(TestDemurrageDefault): o = c.demurrage_amount(self.address, sender_address=self.accounts[0]) r = self.rpc.do(o) demurrage_amount = c.parse_demurrage_amount(r) - modifier_base = 1000000 - self.tax_level - modifier = int(modifier_base * (10 ** 22)) # 38 decimal places minus 6 (1000000) - self.assertEqual(modifier, demurrage_amount) + self.assert_equal_decimals(0.9906, demurrage_amount, 4) def test_apply_demurrage(self): - modifier = (10 ** 28) + #modifier = (10 ** 28) + modifier = 1 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) @@ -97,7 +97,7 @@ class TestBasic(TestDemurrageDefault): b = self.rpc.do(o) logg.debug('block {} start {}'.format(b['timestamp'], self.start_time)) - self.backend.time_travel(self.start_time + 2) + self.backend.time_travel(self.start_time + (60 * 43200)) (tx_hash, o) = c.apply_demurrage(self.address, sender_address=self.accounts[0]) r = self.rpc.do(o) o = receipt(tx_hash) @@ -107,9 +107,9 @@ class TestBasic(TestDemurrageDefault): o = c.demurrage_amount(self.address, sender_address=self.accounts[0]) r = self.rpc.do(o) demurrage_amount = c.parse_demurrage_amount(r) - self.assertEqual(modifier, demurrage_amount) + self.assert_equal_decimals(0.98, demurrage_amount, 2) - self.backend.time_travel(self.start_time + 61) + self.backend.time_travel(self.start_time + (60 * 43200 * 2)) (tx_hash, o) = c.apply_demurrage(self.address, sender_address=self.accounts[0]) r = self.rpc.do(o) o = receipt(tx_hash) @@ -120,28 +120,10 @@ class TestBasic(TestDemurrageDefault): demurrage_amount = c.parse_demurrage_amount(r) modifier_base = 1000000 - self.tax_level modifier = int(modifier_base * (10 ** 22)) # 38 decimal places minus 6 (1000000) - self.assertEqual(modifier, demurrage_amount) - - self.backend.time_travel(self.start_time + 601) - (tx_hash, o) = c.apply_demurrage(self.address, sender_address=self.accounts[0]) - r = self.rpc.do(o) - o = receipt(tx_hash) - r = self.rpc.do(o) - self.assertEqual(r['status'], 1) - o = c.demurrage_amount(self.address, sender_address=self.accounts[0]) - r = self.rpc.do(o) - demurrage_amount = c.parse_demurrage_amount(r) - modifier_base = ((1000000 - self.tax_level) / 1000000) ** 10 - logg.warning('mod base {}'.format(modifier_base)) - modifier = int(modifier_base * (10 ** 12)) - - rounding_tolerance_nano = 4000000 # 0.000004% precision - demurrage_amount_truncate = int(demurrage_amount / (10 ** 16)) # equals 38 decimal places - 14 for the modifier magniture - 2 for percent int calc + 6 for token decimals <- TODO verify this calc - self.assertGreaterEqual(modifier, demurrage_amount_truncate - rounding_tolerance_nano) - self.assertLessEqual(modifier, demurrage_amount_truncate) + self.assert_equal_decimals(0.9604, demurrage_amount, 4) - def test_mint(self): + def test_mint_balance(self): nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1024) @@ -160,16 +142,19 @@ class TestBasic(TestDemurrageDefault): o = receipt(tx_hash) r = self.rpc.do(o) self.assertEqual(r['status'], 1) - o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) r = self.rpc.do(o) balance = c.parse_balance_of(r) self.assertEqual(balance, 2000) - self.backend.time_travel(self.start_time + 61) + self.backend.time_travel(self.start_time + (60 * 43200)) (tx_hash, o) = c.apply_demurrage(self.address, sender_address=self.accounts[0]) r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) r = self.rpc.do(o) balance = c.parse_balance_of(r) @@ -234,7 +219,7 @@ class TestBasic(TestDemurrageDefault): (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1024) self.rpc.do(o) - self.backend.time_travel(self.start_time + 61) + self.backend.time_travel(self.start_time + (60 * 43200)) (tx_hash, o) = c.apply_demurrage(self.address, sender_address=self.accounts[0]) r = self.rpc.do(o) o = c.to_base_amount(self.address, 1000, sender_address=self.accounts[0]) diff --git a/python/tests/test_burn.py b/python/tests/test_burn.py @@ -18,42 +18,34 @@ from chainlib.eth.block import ( from erc20_demurrage_token import DemurrageToken # test imports -from erc20_demurrage_token.unittest.base import TestDemurrage +from erc20_demurrage_token.unittest.base import TestDemurrageDefault logging.basicConfig(level=logging.INFO) logg = logging.getLogger() testdir = os.path.dirname(__file__) -TAX_LEVEL = 2 +#TAX_LEVEL = 2 -class TestBurn(TestDemurrage): +class TestBurn(TestDemurrageDefault): def setUp(self): super(TestBurn, self).setUp() - - def deploy(self, tax_level=None): - 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)) - - if tax_level != None: - self.deployer.settings.demurrage_level = tax_level * (10 ** 32) - self.deployer.settings.sink_address = self.accounts[9] - self.deployer.sink_address = self.accounts[9] - super(TestBurn, self).deploy(c, self.mode) - - logg.info('deployed with mode {}'.format(self.mode)) +# +# def publish(self, tax_level=None): +# nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) +# c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) +# +# if tax_level != None: +# self.publisher.settings.demurrage_level = tax_level * (10 ** 32) +# self.publisher.settings.sink_address = self.accounts[9] +# self.publisher.sink_address = self.accounts[9] +# super(TestBurn, self).publish(c) # Burn tokens and immediately check balances and supply def test_burn_basic(self): - self.deploy() nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) @@ -94,7 +86,6 @@ class TestBurn(TestDemurrage): # burn tokens and check sink balance and supply after first redistribution period def test_burned_redistribution(self): - self.deploy() nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) @@ -102,8 +93,8 @@ class TestBurn(TestDemurrage): r = self.rpc.do(o) (tx_hash, o) = c.burn(self.address, self.accounts[0], 500000000) - r = self.rpc.do(o) - + self.rpc.do(o) + (tx_hash, o) = c.transfer(self.address, self.accounts[0], self.sink_address, 500000000) r = self.rpc.do(o) @@ -112,7 +103,7 @@ class TestBurn(TestDemurrage): o = c.balance(self.address, self.sink_address, sender_address=self.accounts[0]) r = self.rpc.do(o) bal = c.parse_balance(r) - self.assertEqual(bal, 416873881) # 9 periods demurrage + self.assert_within(bal, 490000000, 1) # 2% == 10000000 (tx_hash, o) = c.change_period(self.address, self.accounts[0]) r = self.rpc.do(o) @@ -128,7 +119,7 @@ class TestBurn(TestDemurrage): o = c.balance(self.address, self.sink_address, sender_address=self.accounts[0]) r = self.rpc.do(o) bal = c.parse_balance(r) - self.assert_within_lower(bal, 500000000, 0.0025) + self.assert_within(bal, 500000000, 1) self.backend.time_travel(self.start_time + (self.period_seconds * 2)) @@ -147,12 +138,11 @@ class TestBurn(TestDemurrage): o = c.balance(self.address, self.sink_address, sender_address=self.accounts[0]) r = self.rpc.do(o) bal = c.parse_balance(r) - self.assert_within_lower(bal, 500000000, 0.0025) + self.assert_within_lower(bal, 500000000, 1) # burn tokens and check sink and taxed balance and supply after first redistribution period def test_burned_other_redistribution(self): - self.deploy() nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) @@ -170,7 +160,8 @@ class TestBurn(TestDemurrage): o = c.balance(self.address, self.accounts[1], sender_address=self.accounts[0]) r = self.rpc.do(o) bal = c.parse_balance(r) - self.assertEqual(bal, 416873881) # 9 periods demurrage + #self.assertEqual(bal, 416873881) # 9 periods demurrage + self.assert_within(bal, 490000000, 1) (tx_hash, o) = c.change_period(self.address, self.accounts[0]) r = self.rpc.do(o) @@ -186,12 +177,12 @@ class TestBurn(TestDemurrage): o = c.balance(self.address, self.accounts[1], sender_address=self.accounts[0]) r = self.rpc.do(o) bal = c.parse_balance(r) - self.assertEqual(bal, 408536403) # 9 periods demurrage + self.assert_within(bal, 490000000, 1) o = c.balance(self.address, self.sink_address, sender_address=self.accounts[0]) r = self.rpc.do(o) sink_bal = c.parse_balance(r) - self.assert_within_lower(sink_bal, 500000000 - 408536403, 0.09) # TODO is this ok variance, 1.0 is ppm? + self.assert_within_lower(sink_bal, 10000000, 1) # TODO is this ok variance, 1.0 is ppm? self.backend.time_travel(self.start_time + (self.period_seconds * 2)) @@ -209,7 +200,7 @@ class TestBurn(TestDemurrage): o = c.balance(self.address, self.accounts[1], sender_address=self.accounts[0]) r = self.rpc.do(o) next_bal = c.parse_balance(r) - self.assertEqual(next_bal, 333803985) # 9 periods demurrage + self.assert_within(next_bal, 480200000, 0.01) o = c.balance(self.address, self.sink_address, sender_address=self.accounts[0]) r = self.rpc.do(o) @@ -221,7 +212,6 @@ class TestBurn(TestDemurrage): # verify expected results of balance and supply after multiple redistribution periods def test_burn_accumulate(self): - self.deploy(tax_level=2/1000) nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) @@ -251,6 +241,10 @@ class TestBurn(TestDemurrage): bob_bal = c.parse_balance(r) prev_bob_bal = bob_bal + o = c.balance(self.address, self.sink_address, sender_address=self.accounts[0]) + r = self.rpc.do(o) + logg.info('sink has balance {}'.format(c.parse_balance(r))) + iterations = 100 for i in range(1, iterations + 1): @@ -307,11 +301,11 @@ class TestBurn(TestDemurrage): sum_supply = burner_bal + bob_bal logg.debug('balances sink {} bob {} total {} supply real {} original {}'.format(sink_bal, bob_bal, sum_supply, new_supply, self.default_supply)) - self.assert_within_lower(sum_supply, new_supply, 0.00001) - self.assert_within_greater(burner_bal, balance_share - total_burned, 0.1) + self.assert_within_lower(sum_supply, new_supply, 1) + self.assert_within_lower(burner_bal, balance_share - total_burned + bob_refund, 1) bob_delta = self.default_supply * ((2 / 1000000) / 1000) - self.assert_within_lower(bob_bal, balance_share - bob_delta, 0.1) + self.assert_within_greater(bob_bal, balance_share - bob_delta - bob_refund, 1) self.assertEqual(total_burned, iterations * burn_rate) diff --git a/python/tests/test_cap.py b/python/tests/test_cap.py @@ -18,7 +18,7 @@ from hexathon import ( from erc20_demurrage_token import DemurrageToken # test imports -from erc20_demurrage_token.unittest.base import TestDemurrageCap +from erc20_demurrage_token.unittest import TestDemurrageDefault logging.basicConfig(level=logging.DEBUG) logg = logging.getLogger() @@ -26,18 +26,27 @@ logg = logging.getLogger() testdir = os.path.dirname(__file__) -class TestCap(TestDemurrageCap): +class TestCap(TestDemurrageDefault): - def test_cap_set(self): + def test_cap(self): nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) - o = c.supply_cap(self.address, sender_address=self.accounts[0]) + + o = c.total_supply(self.address, sender_address=self.accounts[0]) + r = self.rpc.do(o) + logg.debug('r {}'.format(r)) + + (tx_hash, o) = c.set_max_supply(self.address, self.accounts[0], self.default_supply_cap) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + o = c.max_supply(self.address, sender_address=self.accounts[0]) r = self.rpc.do(o) cap = c.parse_supply_cap(r) self.assertEqual(cap, self.default_supply_cap) - - def test_cap(self): nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], self.default_supply_cap) @@ -53,15 +62,5 @@ class TestCap(TestDemurrageCap): self.assertEqual(r['status'], 0) - def test_cap_first(self): - nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) - c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) - (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], self.default_supply_cap + 1) - r = self.rpc.do(o) - o = receipt(tx_hash) - r = self.rpc.do(o) - self.assertEqual(r['status'], 0) - - if __name__ == '__main__': unittest.main() diff --git a/python/tests/test_demurrage.py b/python/tests/test_demurrage.py @@ -1,133 +0,0 @@ -# standard imports -import datetime -import unittest -import logging -import os - -# external imports -from chainlib.eth.nonce import RPCNonceOracle -from chainlib.eth.tx import receipt - -# local imports -from erc20_demurrage_token import DemurrageToken -from erc20_demurrage_token.demurrage import DemurrageCalculator - -# test imports -from erc20_demurrage_token.unittest.base import TestDemurrage - -logging.basicConfig(level=logging.INFO) -logg = logging.getLogger() - - -class TestDemurragePeriods(TestDemurrage): - - def setUp(self): - super(TestDemurragePeriods, 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') - if self.mode == None: - self.mode = 'MultiNocap' - logg.debug('executing test setup default mode {}'.format(self.mode)) - - self.deployer.settings.demurrage_level = (2 / 1000) * (10 ** 32) - self.deployer.settings.sink_address = self.accounts[9] - self.deployer.sink_address = self.accounts[9] - self.deploy(c, self.mode) - - logg.info('deployed with mode {}'.format(self.mode)) - - - # verify that tax level calculation is in ppm as expected - def test_ppm(self): - nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) - c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) - - (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], self.default_supply) - r = self.rpc.do(o) - - self.backend.time_travel(self.start_time + 60) - - (tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0]) - self.rpc.do(o) - o = receipt(tx_hash) - r = self.rpc.do(o) - self.assertEqual(r['status'], 1) - - o = c.balance(self.address, self.accounts[1], sender_address=self.accounts[0]) - r = self.rpc.do(o) - bob_bal = c.parse_balance(r) - - o = c.balance(self.address, self.sink_address, sender_address=self.accounts[0]) - r = self.rpc.do(o) - sink_bal = c.parse_balance(r) - - o = c.total_supply(self.address, sender_address=self.accounts[0]) - r = self.rpc.do(o) - new_supply = c.parse_total_supply(r) - - balance_delta = self.default_supply * ((2 / 1000000) / 1000) - self.assertEqual(bob_bal, self.default_supply - balance_delta) - - - - # verify balances and supply after multiple demurrage periods - def test_over_time(self): - nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) - c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) - - (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], self.default_supply) - r = self.rpc.do(o) - - o = c.balance(self.address, self.accounts[1], sender_address=self.accounts[0]) - r = self.rpc.do(o) - bob_bal = c.parse_balance(r) - prev_bob_bal = bob_bal - - nonce_oracle = RPCNonceOracle(self.sink_address, self.rpc) - c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) - - iterations = 100 - - for i in range(1, iterations + 1): - self.backend.time_travel(self.start_time + (self.period_seconds * i)) - - (tx_hash, o) = c.transfer(self.address, self.sink_address, self.accounts[1], prev_bob_bal - bob_bal) - r = self.rpc.do(o) - o = receipt(tx_hash) - r = self.rpc.do(o) - self.assertEqual(r['status'], 1) - - (tx_hash, o) = c.apply_demurrage(self.address, self.sink_address) - self.rpc.do(o) - o = receipt(tx_hash) - r = self.rpc.do(o) - self.assertEqual(r['status'], 1) - - o = c.balance(self.address, self.accounts[1], sender_address=self.accounts[0]) - r = self.rpc.do(o) - bob_bal = c.parse_balance(r) - - o = c.balance(self.address, self.sink_address, sender_address=self.accounts[0]) - r = self.rpc.do(o) - sink_bal = c.parse_balance(r) - - o = c.total_supply(self.address, sender_address=self.accounts[0]) - r = self.rpc.do(o) - new_supply = c.parse_total_supply(r) - - logg.info('round {} supply {} balance sink {} bob {}'.format(i, new_supply, sink_bal, bob_bal)) - - sum_supply = sink_bal + bob_bal - - bob_delta = self.default_supply * ((2 / 1000000) / 100) - - self.assert_within_lower(sum_supply, new_supply, 0.00001) - self.assert_within_greater(bob_bal, self.default_supply - bob_delta, 0.001) - self.assert_within_lower(sink_bal, bob_delta, 1000) - - -if __name__ == '__main__': - unittest.main() diff --git a/python/tests/test_demurrage.py.old b/python/tests/test_demurrage.py.old @@ -0,0 +1,114 @@ +# standard imports +import datetime +import unittest +import logging +import os + +# external imports +from chainlib.eth.nonce import RPCNonceOracle +from chainlib.eth.tx import receipt + +# local imports +from erc20_demurrage_token import DemurrageToken +from erc20_demurrage_token.demurrage import DemurrageCalculator + +# test imports +from erc20_demurrage_token.unittest import TestDemurrageDefault + +logging.basicConfig(level=logging.INFO) +logg = logging.getLogger() + + +class TestDemurragePeriods(TestDemurrageDefault): + + # verify that tax level calculation is in ppm as expected + def test_ppm(self): + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + + (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], self.default_supply) + r = self.rpc.do(o) + + self.backend.time_travel(self.start_time + 60) + + (tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0]) + self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + o = c.balance(self.address, self.accounts[1], sender_address=self.accounts[0]) + r = self.rpc.do(o) + bob_bal = c.parse_balance(r) + + o = c.balance(self.address, self.sink_address, sender_address=self.accounts[0]) + r = self.rpc.do(o) + sink_bal = c.parse_balance(r) + + o = c.total_supply(self.address, sender_address=self.accounts[0]) + r = self.rpc.do(o) + new_supply = c.parse_total_supply(r) + + balance_delta = self.default_supply * ((2 / 1000000) / 1000) + self.assertEqual(bob_bal, self.default_supply - balance_delta) + + + + # verify balances and supply after multiple demurrage periods + def test_over_time(self): + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + + (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], self.default_supply) + r = self.rpc.do(o) + + o = c.balance(self.address, self.accounts[1], sender_address=self.accounts[0]) + r = self.rpc.do(o) + bob_bal = c.parse_balance(r) + prev_bob_bal = bob_bal + + nonce_oracle = RPCNonceOracle(self.sink_address, self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + + iterations = 100 + + for i in range(1, iterations + 1): + self.backend.time_travel(self.start_time + (self.period_seconds * i)) + + (tx_hash, o) = c.transfer(self.address, self.sink_address, self.accounts[1], prev_bob_bal - bob_bal) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + (tx_hash, o) = c.apply_demurrage(self.address, self.sink_address) + self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + o = c.balance(self.address, self.accounts[1], sender_address=self.accounts[0]) + r = self.rpc.do(o) + bob_bal = c.parse_balance(r) + + o = c.balance(self.address, self.sink_address, sender_address=self.accounts[0]) + r = self.rpc.do(o) + sink_bal = c.parse_balance(r) + + o = c.total_supply(self.address, sender_address=self.accounts[0]) + r = self.rpc.do(o) + new_supply = c.parse_total_supply(r) + + logg.info('round {} supply {} balance sink {} bob {}'.format(i, new_supply, sink_bal, bob_bal)) + + sum_supply = sink_bal + bob_bal + + bob_delta = self.default_supply * ((2 / 1000000) / 100) + + self.assert_within_lower(sum_supply, new_supply, 0.00001) + self.assert_within_greater(bob_bal, self.default_supply - bob_delta, 0.001) + self.assert_within_lower(sink_bal, bob_delta, 1000) + + +if __name__ == '__main__': + unittest.main() diff --git a/python/tests/test_demurrage_ext.py b/python/tests/test_demurrage_ext.py @@ -1,41 +0,0 @@ -# standard imports -import datetime -import unittest - -# external imports -from chainlib.eth.nonce import RPCNonceOracle - -# local imports -from erc20_demurrage_token import DemurrageToken -from erc20_demurrage_token.demurrage import DemurrageCalculator - -# test imports -from erc20_demurrage_token.unittest.base import TestDemurrage - - -class TestEmulate(TestDemurrage): - - def test_amount_since(self): - d = datetime.datetime.utcnow() - datetime.timedelta(seconds=29, hours=5, minutes=3, days=4) - c = DemurrageCalculator(0.00000050105908373373) - a = c.amount_since(100, d.timestamp()) - self.assert_within_lower(a, 99.69667, 0.1) - - - def test_amount_since_slow(self): - d = datetime.datetime.utcnow() - datetime.timedelta(seconds=29, hours=5, minutes=3, days=4) - c = DemurrageCalculator(0.00000050105908373373) - a = c.amount_since_slow(100, d.timestamp()) - self.assert_within_lower(a, 99.69667, 0.1) - - - def test_from_contract(self): - nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) - c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) - self.deploy(c, 'SingleNocap') - dc = DemurrageCalculator.from_contract(self.rpc, self.chain_spec, self.address, sender_address=self.accounts[0]) - self.assertEqual(dc.r_min, 0.02) - - -if __name__ == '__main__': - unittest.main() diff --git a/python/tests/test_expiry.py b/python/tests/test_expiry.py @@ -0,0 +1,98 @@ +# standard imports +import os +import unittest +import json +import logging +import datetime + +# external imports +from chainlib.eth.constant import ZERO_ADDRESS +from chainlib.eth.nonce import RPCNonceOracle +from chainlib.eth.tx import receipt +from chainlib.eth.block import ( + block_latest, + block_by_number, + ) + +# local imports +from erc20_demurrage_token import DemurrageToken + +# test imports +from erc20_demurrage_token.unittest import TestDemurrageDefault + +logging.basicConfig(level=logging.DEBUG) +logg = logging.getLogger() + +testdir = os.path.dirname(__file__) + + +class TestExpire(TestDemurrageDefault): + + def test_expires(self): + mint_amount = self.default_supply + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + + for i in range(3): + (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[i+1], mint_amount) + r = self.rpc.do(o) + + (tx_hash, o) = c.set_expire_period(self.address, self.accounts[0], 2) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + o = c.expires(self.address, sender_address=self.accounts[0]) + r = self.rpc.do(o) + expiry_time = c.parse_expires(r) + + self.backend.time_travel(expiry_time + 60) + o = block_latest() + r = self.rpc.do(o) + o = block_by_number(r) + r = self.rpc.do(o) + self.assertGreaterEqual(r['timestamp'], expiry_time) + + nonce_oracle = RPCNonceOracle(self.sink_address, self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash, o) = c.transfer(self.address, self.sink_address, self.accounts[2], 1) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + (tx_hash, o) = c.transfer(self.address, self.sink_address, self.accounts[2], 1) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 0) + + o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) + r = self.rpc.do(o) + balance = c.parse_balance(r) + self.assert_within(balance, 0.9604 * mint_amount, 1) + + o = c.total_supply(self.address, sender_address=self.accounts[0]) + r = self.rpc.do(o) + supply = c.parse_balance(r) + + (tx_hash, o) = c.change_period(self.address, self.sink_address) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + o = c.balance_of(self.address, self.sink_address, sender_address=self.accounts[0]) + r = self.rpc.do(o) + balance = c.parse_balance(r) + + o = c.decay_by(self.address, supply, int((expiry_time - self.start_time) / 60), sender_address=self.sink_address) + r = self.rpc.do(o) + target_balance = c.parse_balance(r) + + self.assert_within_lower(balance, supply - target_balance, 0.0001) + + +if __name__ == '__main__': + unittest.main() diff --git a/python/tests/test_growth.py b/python/tests/test_growth.py @@ -18,7 +18,7 @@ from chainlib.eth.block import ( from erc20_demurrage_token import DemurrageToken # test imports -from erc20_demurrage_token.unittest.base import TestDemurrageDefault +from erc20_demurrage_token.unittest import TestDemurrageDefault logging.basicConfig(level=logging.DEBUG) logg = logging.getLogger() @@ -28,42 +28,38 @@ testdir = os.path.dirname(__file__) class TestGrowth(TestDemurrageDefault): - def test_grow_by(self): + def test_decay_by(self): nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) - growth_factor = (1000000 + self.tax_level) / 1000000 v = 1000000000 - o = c.grow_by(self.address, v, 1, sender_address=self.accounts[0]) + o = c.decay_by(self.address, v, 20000, sender_address=self.accounts[0]) r = self.rpc.do(o) - g = c.parse_grow_by(r) - self.assertEqual(int(v * growth_factor), g) - - period = 10 - growth_factor = (1 + (self.tax_level) / 1000000) ** period - o = c.grow_by(self.address, v, period, sender_address=self.accounts[0]) + g = c.parse_decay_by(r) + self.assertEqual(int(g), 990690498) + + o = c.decay_by(self.address, v, 43200, sender_address=self.accounts[0]) r = self.rpc.do(o) - g = c.parse_grow_by(r) - self.assertEqual(int(v * growth_factor), g) + g = c.parse_decay_by(r) + self.assertEqual(int(g), 980000000) - def test_decay_by(self): + def test_decay_steps(self): nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) - growth_factor = (1000000 - self.tax_level) / 1000000 v = 1000000000 - o = c.decay_by(self.address, v, 1, sender_address=self.accounts[0]) + o = c.decay_by(self.address, v, 43200, sender_address=self.accounts[0]) r = self.rpc.do(o) - g = c.parse_decay_by(r) - self.assertEqual(int(v * growth_factor), g) + gr = c.parse_decay_by(r) - period = 10 - growth_factor = (1 - (self.tax_level) / 1000000) ** period - o = c.decay_by(self.address, v, period, sender_address=self.accounts[0]) - r = self.rpc.do(o) - g = c.parse_decay_by(r) - self.assertEqual(int(v * growth_factor), g) + v = 1000000000 + for i in range(100): + o = c.decay_by(self.address, v, 432, sender_address=self.accounts[0]) + r = self.rpc.do(o) + v = c.parse_decay_by(r) + + self.assert_within_lower(int(v), int(gr), 0.1) if __name__ == '__main__': diff --git a/python/tests/test_mint.py b/python/tests/test_mint.py @@ -0,0 +1,77 @@ +# standard imports +import os +import unittest +import json +import logging + +# external imports +from chainlib.eth.constant import ZERO_ADDRESS +from chainlib.eth.nonce import RPCNonceOracle +from chainlib.eth.tx import receipt +from chainlib.eth.block import block_latest +from chainlib.eth.block import block_by_number + +# local imports +from erc20_demurrage_token import DemurrageToken + +# test imports +from erc20_demurrage_token.unittest import TestDemurrageDefault + +logging.basicConfig(level=logging.DEBUG) +logg = logging.getLogger() + +testdir = os.path.dirname(__file__) + +class TestAmounts(TestDemurrageDefault): + + def test_mint(self): + o = block_latest() + r = self.rpc.do(o) + o = block_by_number(r) + r = self.rpc.do(o) + tb = r['timestamp'] + self.backend.time_travel(self.start_time + 800) + o = block_latest() + r = self.rpc.do(o) + o = block_by_number(r) + r = self.rpc.do(o) + ta = r['timestamp'] + + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1000) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + + def test_writer(self): + nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash, o) = c.mint_to(self.address, self.accounts[1], self.accounts[1], 1000) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 0) + + + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash, o) = c.add_writer(self.address, self.accounts[0], self.accounts[1]) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash, o) = c.mint_to(self.address, self.accounts[1], self.accounts[1], 1000) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + +if __name__ == '__main__': + unittest.main() diff --git a/python/tests/test_period.py b/python/tests/test_period.py @@ -16,12 +16,16 @@ from chainlib.eth.contract import ( ABIContractEncoder, ABIContractType, ) +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 # test imports -from erc20_demurrage_token.unittest.base import TestDemurrageDefault +from erc20_demurrage_token.unittest import TestDemurrageDefault logging.basicConfig(level=logging.DEBUG) logg = logging.getLogger() @@ -30,7 +34,7 @@ testdir = os.path.dirname(__file__) class TestPeriod(TestDemurrageDefault): - def test_period(self): + def test_period_and_amount(self): nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1024) @@ -39,23 +43,63 @@ class TestPeriod(TestDemurrageDefault): r = self.rpc.do(o) self.assertEqual(r['status'], 1) - self.backend.time_travel(self.start_time + self.period_seconds) + for i in range(100): + self.backend.time_travel(self.start_time + int((self.period_seconds / 100) * (i + 1))) + (tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0], 0) + self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + for lg in r['logs']: + if hex_same(lg['topics'][0], '1c9c74563c32efd114cb36fb5e432d9386c8254d08456614804a33a3088ab736'): + self.assert_equal_decimals(0.98, from_fixed(strip_0x(lg['data'])), 2) + + def test_period_demurrage(self): + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) - (tx_hash, o) = c.change_period(self.address, self.accounts[0]) + (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1024) + r = self.rpc.do(o) + o = receipt(tx_hash) r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + self.backend.time_travel(self.start_time + self.period_seconds + int(self.period_seconds / 2)) + (tx_hash, o) = c.change_period(self.address, self.accounts[0]) + self.rpc.do(o) o = receipt(tx_hash) r = self.rpc.do(o) self.assertEqual(r['status'], 1) + for lg in r['logs']: + if hex_same(lg['topics'][0], '1c9c74563c32efd114cb36fb5e432d9386c8254d08456614804a33a3088ab736'): + self.assert_equal_decimals(0.9701, from_fixed(strip_0x(lg['data'])), 4) + o = c.redistributions(self.address, 1, sender_address=self.accounts[0]) r = self.rpc.do(o) - redistribution = c.parse_redistributions(r) + redistribution_data = c.parse_redistributions(r) + redistribution = DemurrageRedistribution(redistribution_data) + logg.debug('fixxx {}'.format(redistribution.demurrage)) - o = c.to_redistribution_period(self.address, redistribution, sender_address=self.accounts[0]) + + def test_period(self): + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1024) r = self.rpc.do(o) - period = c.parse_to_redistribution_period(r) - self.assertEqual(2, period) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + self.backend.time_travel(self.start_time + self.period_seconds) + + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + (tx_hash, o) = c.change_period(self.address, self.accounts[0]) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) o = c.redistributions(self.address, 1, sender_address=self.accounts[0]) r = self.rpc.do(o) @@ -73,16 +117,9 @@ class TestPeriod(TestDemurrageDefault): o = c.to_redistribution_demurrage_modifier(self.address, redistribution, sender_address=self.accounts[0]) r = self.rpc.do(o) - period = c.parse_to_redistribution_item(r) - - # allow test code float rounding error to billionth - modifier = (1 - (self.tax_level / 1000000)) ** (self.period_seconds / 60) - modifier *= 10 ** 9 - modifier = int(modifier) * (10 ** (28 - 9)) - - period /= (10 ** (28 - 9)) - period = int(period) * (10 ** (28 - 9)) - self.assertEqual(modifier, period) + period = from_fixed(r) + redistro = DemurrageRedistribution(redistribution) + logg.debug('redistro {} {}'.format(redistro, period)) self.backend.time_travel(self.start_time + self.period_seconds * 2) @@ -99,7 +136,7 @@ class TestPeriod(TestDemurrageDefault): o = c.to_redistribution_demurrage_modifier(self.address, redistribution, sender_address=self.accounts[0]) r = self.rpc.do(o) - period = c.parse_to_redistribution_item(r) + period = from_fixed(r) # allow test code float rounding error to billionth modifier = (1 - (self.tax_level / 1000000)) ** ((self.period_seconds * 2) / 60) @@ -135,7 +172,7 @@ class TestPeriod(TestDemurrageDefault): r = self.rpc.do(o) self.assertEqual(r['status'], 1) - o = c.balance_of(self.address, ZERO_ADDRESS, sender_address=self.accounts[0]) + o = c.balance_of(self.address, self.sink_address, sender_address=self.accounts[0]) r = self.rpc.do(o) balance = c.parse_balance_of(r) self.assertGreater(balance, 0) @@ -185,7 +222,7 @@ class TestPeriod(TestDemurrageDefault): r = self.rpc.do(o) self.assertEqual(r['status'], 1) - o = c.balance_of(self.address, ZERO_ADDRESS, sender_address=self.accounts[0]) + o = c.balance_of(self.address, self.sink_address, sender_address=self.accounts[0]) r = self.rpc.do(o) balance = c.parse_balance_of(r) self.assertLess(balance, old_sink_balance) diff --git a/python/tests/test_redistribution.py b/python/tests/test_redistribution.py @@ -1,328 +0,0 @@ -# standard imports -import os -import unittest -import json -import logging - -# external imports -from chainlib.eth.constant import ZERO_ADDRESS -from chainlib.eth.nonce import RPCNonceOracle -from chainlib.eth.tx import receipt -from chainlib.eth.block import ( - block_latest, - block_by_number, - ) -from chainlib.eth.address import to_checksum_address -from hexathon import ( - strip_0x, - add_0x, - ) - -# local imports -from erc20_demurrage_token import DemurrageToken - -# test imports -from erc20_demurrage_token.unittest.base import TestDemurrageDefault - -logging.basicConfig(level=logging.DEBUG) -logg = logging.getLogger() - -testdir = os.path.dirname(__file__) - -class TestRedistribution(TestDemurrageDefault): - - - - def test_whole_is_parts(self): - nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) - c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) - - (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 100000000) - self.rpc.do(o) - o = receipt(tx_hash) - r = self.rpc.do(o) - self.assertEqual(r['status'], 1) - - (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[2], 100000000) - self.rpc.do(o) - o = receipt(tx_hash) - r = self.rpc.do(o) - self.assertEqual(r['status'], 1) - - nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) - c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) - (tx_hash, o) = c.transfer(self.address, self.accounts[1], self.accounts[3], 50000000) - r = self.rpc.do(o) - o = receipt(tx_hash) - r = self.rpc.do(o) - self.assertEqual(r['status'], 1) - - self.backend.time_travel(self.start_time + self.period_seconds + 1) - - o = block_latest() - r = self.rpc.do(o) - o = block_by_number(r) - r = self.rpc.do(o) - self.assertEqual(r['timestamp'], self.start_time + self.period_seconds) - - (tx_hash, o) = c.change_period(self.address, self.accounts[1]) - r = self.rpc.do(o) - o = receipt(tx_hash) - r = self.rpc.do(o) - self.assertEqual(r['status'], 1) - - (tx_hash, o) = c.apply_redistribution_on_account(self.address, self.accounts[1], self.accounts[1]) - r = self.rpc.do(o) - o = receipt(tx_hash) - r = self.rpc.do(o) - self.assertEqual(r['status'], 1) - - balance = 0 - for i in range(3): - o = c.balance_of(self.address, self.accounts[i+1], sender_address=self.accounts[0]) - r = self.rpc.do(o) - balance_item = c.parse_balance_of(r) - balance += balance_item - logg.debug('balance {} {} total {}'.format(i, balance_item, balance)) - - o = c.balance_of(self.address, self.sink_address, sender_address=self.accounts[0]) - r = self.rpc.do(o) - balance_item = c.parse_balance_of(r) - balance += balance_item - - self.assertEqual(balance, 200000000) - - -# def test_debug_periods(self): -# nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) -# c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) -# -# o = c.actual_period(self.address, sender_address=self.accounts[0]) -# r = self.rpc.do(o) -# pactual = c.parse_actual_period(r) -# -# o = c.period_start(self.address, sender_address=self.accounts[0]) -# r = self.rpc.do(o) -# pstart = c.parse_actual_period(r) -# -# o = c.period_duration(self.address, sender_address=self.accounts[0]) -# r = self.rpc.do(o) -# pduration = c.parse_actual_period(r) -# -# o = block_latest() -# blocknumber = self.rpc.do(o) -# -# logg.debug('actual {} start {} duration {} blocknumber {}'.format(pactual, pstart, pduration, blocknumber)) -# -# -# # TODO: check receipt log outputs -# def test_redistribution_storage(self): -# nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) -# c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) -# o = c.redistributions(self.address, 0, sender_address=self.accounts[0]) -# r = self.rpc.do(o) -# self.assertEqual(strip_0x(r), '000000000000000000000000f424000000000000000000000000000000000001') -# -# (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], 1000000) -# r = self.rpc.do(o) -# -# (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[2], 1000000) -# r = self.rpc.do(o) -# -# external_address = to_checksum_address('0x' + os.urandom(20).hex()) -# -# nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) -# c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) -# (tx_hash, o) = c.transfer(self.address, self.accounts[2], external_address, 1000000) -# r = self.rpc.do(o) -# -# nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) -# c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) -# (tx_hash, o) = c.transfer(self.address, self.accounts[1], external_address, 999999) -# r = self.rpc.do(o) -# -# self.backend.time_travel(self.start_time + self.period_seconds + 1) -# -# o = c.redistributions(self.address, 0, sender_address=self.accounts[0]) -# r = self.rpc.do(o) -# self.assertEqual(strip_0x(r), '000000000000000000000000f42400000000010000000000001e848000000001') -# -# o = c.redistributions(self.address, 0, sender_address=self.accounts[0]) -# r = self.rpc.do(o) -# self.assertEqual(strip_0x(r), '000000000000000000000000f42400000000010000000000001e848000000001') -# -# -# nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) -# c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) -# (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[0], 1000000) -# r = self.rpc.do(o) -# -# o = c.redistributions(self.address, 1, sender_address=self.accounts[0]) -# r = self.rpc.do(o) -# self.assertEqual(strip_0x(r), '000000000000000000000000ef4200000000000000000000002dc6c000000002') -# -# -# def test_redistribution_balance_on_zero_participants(self): -# supply = self.default_supply -# -# nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) -# c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) -# (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], supply) -# r = self.rpc.do(o) -# -# self.backend.time_travel(self.start_time + self.period_seconds + 1) -# (tx_hash, o) = c.apply_demurrage(self.address, self.accounts[0]) -# self.rpc.do(o) -# o = receipt(tx_hash) -# rcpt = self.rpc.do(o) -# self.assertEqual(rcpt['status'], 1) -# -# (tx_hash, o) = c.change_period(self.address, self.accounts[0]) -# self.rpc.do(o) -# o = receipt(tx_hash) -# r = self.rpc.do(o) -# self.assertEqual(r['status'], 1) -# -# o = c.total_supply(self.address, sender_address=self.accounts[0]) -# r = self.rpc.do(o) -# total_supply = c.parse_total_supply(r) -# sink_increment = int(total_supply * (self.tax_level / 1000000)) -# self.assertEqual(supply, total_supply) -# -# for l in rcpt['logs']: -# if l['topics'][0] == '0xa0717e54e02bd9829db5e6e998aec0ae9de796b8d150a3cc46a92ab869697755': # event Decayed(uint256,uint256,uint256,uint256) -# period = int.from_bytes(bytes.fromhex(strip_0x(l['topics'][1])), 'big') -# self.assertEqual(period, 2) -# b = bytes.fromhex(strip_0x(l['data'])) -# remainder = int.from_bytes(b, 'big') -# self.assertEqual(remainder, int((1000000 - self.tax_level) * (10 ** 32))) -# -# o = c.balance_of(self.address, self.sink_address, sender_address=self.accounts[0]) -# r = self.rpc.do(o) -# sink_balance = c.parse_balance_of(r) -# -# self.assertEqual(sink_balance, int(sink_increment * 0.98)) -# self.assertEqual(sink_balance, int(sink_increment * (1000000 - self.tax_level) / 1000000)) -# -# o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) -# r = self.rpc.do(o) -# balance = c.parse_balance_of(r) -# self.assertEqual(balance, supply - sink_increment) -# -# -# def test_redistribution_two_of_ten(self): -# mint_amount = 100000000 -# nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) -# c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) -# z = 0 -# for i in range(10): -# (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[i], mint_amount) -# self.rpc.do(o) -# z += mint_amount -# -# o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) -# r = self.rpc.do(o) -# initial_balance = c.parse_balance_of(r) -# -# spend_amount = 1000000 -# external_address = to_checksum_address('0x' + os.urandom(20).hex()) -# -# nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) -# c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) -# (tx_hash, o) = c.transfer(self.address, self.accounts[1], external_address, spend_amount) -# self.rpc.do(o) -# o = receipt(tx_hash) -# r = self.rpc.do(o) -# self.assertEqual(r['status'], 1) -# -# nonce_oracle = RPCNonceOracle(self.accounts[2], self.rpc) -# c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) -# (tx_hash, o) = c.transfer(self.address, self.accounts[2], external_address, spend_amount) -# self.rpc.do(o) -# o = receipt(tx_hash) -# r = self.rpc.do(o) -# self.assertEqual(r['status'], 1) -# -# # No cheating! -# nonce_oracle = RPCNonceOracle(self.accounts[3], self.rpc) -# c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) -# (tx_hash, o) = c.transfer(self.address, self.accounts[3], self.accounts[3], spend_amount) -# self.rpc.do(o) -# o = receipt(tx_hash) -# r = self.rpc.do(o) -# self.assertEqual(r['status'], 1) -# -# # No cheapskating! -# nonce_oracle = RPCNonceOracle(self.accounts[4], self.rpc) -# c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) -# (tx_hash, o) = c.transfer(self.address, self.accounts[4], external_address, spend_amount-1) -# self.rpc.do(o) -# o = receipt(tx_hash) -# r = self.rpc.do(o) -# self.assertEqual(r['status'], 1) -# -# -# self.backend.time_travel(self.start_time + self.period_seconds + 1) -# -# (tx_hash, o) = c.apply_demurrage(self.address, self.accounts[4]) -# self.rpc.do(o) -# -# (tx_hash, o) = c.change_period(self.address, self.accounts[4]) -# self.rpc.do(o) -# -# o = c.balance_of(self.address, self.accounts[3], sender_address=self.accounts[0]) -# r = self.rpc.do(o) -# bummer_balance = c.parse_balance_of(r) -# -# self.assertEqual(bummer_balance, mint_amount - (mint_amount * (self.tax_level / 1000000))) -# logg.debug('bal {} '.format(bummer_balance)) -# -# o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) -# r = self.rpc.do(o) -# bummer_balance = c.parse_balance_of(r) -# spender_balance = mint_amount - spend_amount -# spender_decayed_balance = int(spender_balance - (spender_balance * (self.tax_level / 1000000))) -# self.assertEqual(bummer_balance, spender_decayed_balance) -# logg.debug('bal {} '.format(bummer_balance)) -# -# (tx_hash, o) = c.apply_redistribution_on_account(self.address, self.accounts[4], self.accounts[1]) -# self.rpc.do(o) -# o = receipt(tx_hash) -# r = self.rpc.do(o) -# self.assertEqual(r['status'], 1) -# -# (tx_hash, o) = c.apply_redistribution_on_account(self.address, self.accounts[4], self.accounts[2]) -# self.rpc.do(o) -# o = receipt(tx_hash) -# r = self.rpc.do(o) -# self.assertEqual(r['status'], 1) -# -# o = c.redistributions(self.address, 0, sender_address=self.accounts[0]) -# r = self.rpc.do(o) -# redistribution_data = c.parse_redistributions(r) -# logg.debug('redist data {}'.format(redistribution_data)) -# -# o = c.account_period(self.address, self.accounts[1], sender_address=self.accounts[0]) -# r = self.rpc.do(o) -# account_period_data = c.parse_account_period(r) -# logg.debug('account period {}'.format(account_period_data)) -# -# o = c.actual_period(self.address, sender_address=self.accounts[0]) -# r = self.rpc.do(o) -# actual_period = c.parse_actual_period(r) -# logg.debug('period {}'.format(actual_period)) -# -# redistribution = int((z / 2) * (self.tax_level / 1000000)) -# spender_new_base_balance = ((mint_amount - spend_amount) + redistribution) -# spender_new_decayed_balance = int(spender_new_base_balance - (spender_new_base_balance * (self.tax_level / 1000000))) -# -# o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) -# r = self.rpc.do(o) -# spender_actual_balance = c.parse_balance_of(r) -# logg.debug('rrr {} {}'.format(redistribution, spender_new_decayed_balance)) -# -# self.assertEqual(spender_actual_balance, spender_new_decayed_balance) -# - -if __name__ == '__main__': - unittest.main() diff --git a/python/tests/test_redistribution_single.py b/python/tests/test_redistribution_single.py @@ -16,13 +16,15 @@ from chainlib.eth.address import to_checksum_address from hexathon import ( strip_0x, add_0x, + same as hex_same, ) +from dexif import from_fixed # local imports from erc20_demurrage_token import DemurrageToken # test imports -from erc20_demurrage_token.unittest.base import TestDemurrageDefault +from erc20_demurrage_token.unittest import TestDemurrageDefault logging.basicConfig(level=logging.INFO) logg = logging.getLogger() @@ -42,8 +44,8 @@ class TestRedistribution(TestDemurrageDefault): (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[0], supply) self.rpc.do(o) - for i in range(1, 10): - logg.debug('execute time travel to period {}'.format(i)) + for i in range(1, 100): + logg.info('execute time travel to period {}'.format(i)) self.backend.time_travel(self.start_time + (self.period_seconds * i)) (tx_hash, o) = c.change_period(self.address, self.accounts[0]) self.rpc.do(o) @@ -51,19 +53,24 @@ class TestRedistribution(TestDemurrageDefault): r = self.rpc.do(o) self.assertEqual(r['status'], 1) + redistribution_value = 0 + for lg in r['logs']: + if hex_same(lg['topics'][0], '0x9a2a887706623ad3ff7fc85652deeceabe9fe1e00466c597972079ee91ea40d3'): + redistribution_value = int(strip_0x(lg['data']), 16) + o = c.redistributions(self.address, i, sender_address=self.accounts[0]) redistribution = self.rpc.do(o) 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]) redistribution = self.rpc.do(o) 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]) r = self.rpc.do(o) @@ -73,9 +80,9 @@ class TestRedistribution(TestDemurrageDefault): r = self.rpc.do(o) balance_minter = c.parse_balance(r) - logg.debug('testing sink {} mint {} adds up to supply {} with demurrage between {} and {}'.format(balance_sink, balance_minter, supply, demurrage_previous, demurrage)) + logg.info('testing redistro {} sink {} mint {} adds up to {} supply {} with demurrage between {} and {}'.format(redistribution_value, balance_sink, balance_minter, balance_sink + balance_minter, supply, demurrage_previous, demurrage)) - self.assert_within_lower(balance_minter + balance_sink, supply, 0.001) + self.assert_within(balance_minter + balance_sink, supply, 10) def test_redistribution_catchup_periods(self): @@ -137,7 +144,7 @@ class TestRedistribution(TestDemurrageDefault): logg.debug('testing sink {} mint {} adds up to supply {} with demurrage between {} and {}'.format(balance_sink, balance_minter, real_supply, demurrage_previous, demurrage)) - self.assert_within_lower(balance_minter + balance_sink, supply, 0.001) + self.assert_within_lower(balance_minter + balance_sink, supply, 0.1) # def test_redistribution_boundaries(self): diff --git a/python/tests/test_redistribution_unit.py b/python/tests/test_redistribution_unit.py @@ -16,12 +16,14 @@ from hexathon import ( strip_0x, add_0x, ) +from dexif import to_fixed # local imports from erc20_demurrage_token import DemurrageToken +from erc20_demurrage_token import DemurrageRedistribution # test imports -from erc20_demurrage_token.unittest.base import TestDemurrageUnit +from erc20_demurrage_token.unittest import TestDemurrageDefault logging.basicConfig(level=logging.DEBUG) logg = logging.getLogger() @@ -29,7 +31,8 @@ logg = logging.getLogger() testdir = os.path.dirname(__file__) -class TestRedistribution(TestDemurrageUnit): +class TestRedistribution(TestDemurrageDefault): + # TODO: move to "pure" test file when getdistribution is implemented in all contracts @@ -40,11 +43,12 @@ class TestRedistribution(TestDemurrageUnit): demurrage = (1 - (self.tax_level / 1000000)) * (10**28) supply = self.default_supply - o = c.get_distribution(self.address, supply, demurrage, sender_address=self.accounts[0]) + #o = c.get_distribution(self.address, supply, demurrage, sender_address=self.accounts[0]) + o = c.get_distribution(self.address, supply, to_fixed(self.tax_level / 1000000), sender_address=self.accounts[0]) r = self.rpc.do(o) distribution = c.parse_get_distribution(r) expected_distribution = self.default_supply * (self.tax_level / 1000000) - self.assert_within_lower(distribution, expected_distribution, 1000) + self.assert_within(distribution, expected_distribution, 100) def test_distribution_from_redistribution(self): @@ -53,18 +57,17 @@ class TestRedistribution(TestDemurrageUnit): demurrage = (1 - (self.tax_level / 100000)) * (10**28) - logg.debug('demurrage {}'.format(demurrage)) supply = self.default_supply - o = c.to_redistribution(self.address, 0, demurrage, supply, 2, sender_address=self.accounts[0]) + o = c.to_redistribution(self.address, 0, to_fixed(self.tax_level / 1000000), supply, 2, sender_address=self.accounts[0]) redistribution = self.rpc.do(o) o = c.get_distribution_from_redistribution(self.address, redistribution, self.accounts[0]) r = self.rpc.do(o) distribution = c.parse_get_distribution(r) - expected_distribution = (self.default_supply * self.tax_level) / 100000 + expected_distribution = (self.default_supply * (self.tax_level / 1000000)) logg.debug('distribution {} supply {}'.format(distribution, self.default_supply)) - self.assert_within_lower(distribution, expected_distribution, 1000) + self.assert_within(distribution, expected_distribution, 1000) def test_single_step_basic(self): @@ -124,7 +127,7 @@ class TestRedistribution(TestDemurrageUnit): nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) - mint_amount = 100000000 + mint_amount = self.default_supply half_mint_amount = int(mint_amount / 2) (tx_hash, o) = c.mint_to(self.address, self.accounts[0], self.accounts[1], mint_amount) @@ -156,7 +159,7 @@ class TestRedistribution(TestDemurrageUnit): o = c.balance_of(self.address, self.accounts[2], sender_address=self.accounts[0]) r = self.rpc.do(o) balance = c.parse_balance(r) - self.assertEqual(balance, expected_balance) + self.assert_within(balance, expected_balance, 10) half_demurrage_amount = int((self.tax_level / 1000000) * half_mint_amount) @@ -182,8 +185,10 @@ class TestRedistribution(TestDemurrageUnit): supply = c.parse_to_redistribution_item(r) 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) - logg.debug('\nrediistribution {}\ndemurrage {}\nsupply {}'.format(redistribution, demurrage, supply)) + #demurrage = c.parse_to_redistribution_item(r) + #logg.debug('\nrediistribution {}\ndemurrage {}\nsupply {}'.format(redistribution, demurrage, supply)) + redistro_item = DemurrageRedistribution(redistribution) + logg.debug('redistribution {}'.format(redistro_item)) expected_balance = int(supply * (self.tax_level / 1000000)) expected_balance_tolerance = 1 diff --git a/python/tests/test_remainder.py b/python/tests/test_remainder.py @@ -1,70 +0,0 @@ -# standard imports -import os -import unittest -import json -import logging -import math - -# external imports -from chainlib.eth.constant import ZERO_ADDRESS -from chainlib.eth.nonce import RPCNonceOracle -from chainlib.eth.tx import receipt -from chainlib.error import JSONRPCException -import eth_tester - -# local imports -from erc20_demurrage_token import DemurrageToken - -# test imports -from erc20_demurrage_token.unittest.base import TestDemurrageDefault - -logging.basicConfig(level=logging.DEBUG) -logg = logging.getLogger() - -testdir = os.path.dirname(__file__) - - -class Test(TestDemurrageDefault): - - def test_fractional_state(self): - nonce_oracle = RPCNonceOracle(self.accounts[1], self.rpc) - c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) - - with self.assertRaises(JSONRPCException): - o = c.remainder(self.address, 2, 1, sender_address=self.accounts[0]) - self.rpc.do(o) - - with self.assertRaises(JSONRPCException): - o = c.remainder(self.address, 0, 100001, sender_address=self.accounts[0]) - self.rpc.do(o) - - o = c.remainder(self.address, 1, 2, sender_address=self.accounts[0]) - r = self.rpc.do(o) - remainder = c.parse_remainder(r) - self.assertEqual(remainder, 0); - - whole = 5000001 - parts = 20000 - expect = whole - (math.floor(whole/parts) * parts) - o = c.remainder(self.address, parts, whole, sender_address=self.accounts[0]) - r = self.rpc.do(o) - remainder = c.parse_remainder(r) - self.assertEqual(remainder, expect) - - parts = 30000 - expect = whole - (math.floor(whole/parts) * parts) - o = c.remainder(self.address, parts, whole, sender_address=self.accounts[0]) - r = self.rpc.do(o) - remainder = c.parse_remainder(r) - self.assertEqual(remainder, expect) - - parts = 40001 - expect = whole - (math.floor(whole/parts) * parts) - o = c.remainder(self.address, parts, whole, sender_address=self.accounts[0]) - r = self.rpc.do(o) - remainder = c.parse_remainder(r) - self.assertEqual(remainder, expect) - - -if __name__ == '__main__': - unittest.main() diff --git a/python/tests/test_seal.py b/python/tests/test_seal.py @@ -0,0 +1,195 @@ +# standard imports +import os +import unittest +import json +import logging +import datetime + +# external imports +from chainlib.eth.constant import ZERO_ADDRESS +from chainlib.eth.nonce import RPCNonceOracle +from chainlib.eth.tx import receipt +from chainlib.eth.block import ( + block_latest, + block_by_number, + ) + +# local imports +from erc20_demurrage_token import DemurrageToken +from erc20_demurrage_token.seal import ContractState +from erc20_demurrage_token.seal import CONTRACT_SEAL_STATE_MAX + +# test imports +from erc20_demurrage_token.unittest import TestDemurrageDefault + +logging.basicConfig(level=logging.DEBUG) +logg = logging.getLogger() + +testdir = os.path.dirname(__file__) + + + +class TestSeal(TestDemurrageDefault): + + def test_seal_dup(self): + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + + (tx_hash, o) = c.seal(self.address, self.accounts[0], 1) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + (tx_hash, o) = c.seal(self.address, self.accounts[0], 1) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 0) + + + def test_seal_all(self): + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + + (tx_hash, o) = c.seal(self.address, self.accounts[0], CONTRACT_SEAL_STATE_MAX) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + o = c.is_sealed(self.address, 0, sender_address=self.accounts[0]) + r = self.rpc.do(o) + self.assertTrue(c.parse_is_sealed(r)) + + + def test_seal_minter(self): + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + + (tx_hash, o) = c.add_minter(self.address, self.accounts[0], self.accounts[1]) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + (tx_hash, o) = c.seal(self.address, self.accounts[0], ContractState.MINTER_STATE) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + (tx_hash, o) = c.add_minter(self.address, self.accounts[0], self.accounts[2]) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 0) + + o = c.is_sealed(self.address, ContractState.MINTER_STATE, sender_address=self.accounts[0]) + r = self.rpc.do(o) + self.assertTrue(c.parse_is_sealed(r)) + + + def test_seal_expiry(self): + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + + (tx_hash, o) = c.set_expire_period(self.address, self.accounts[0], 10) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + (tx_hash, o) = c.set_expire_period(self.address, self.accounts[0], 20) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + (tx_hash, o) = c.seal(self.address, self.accounts[0], ContractState.EXPIRY_STATE) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + (tx_hash, o) = c.set_expire_period(self.address, self.accounts[0], 21) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 0) + + o = c.is_sealed(self.address, ContractState.EXPIRY_STATE, sender_address=self.accounts[0]) + r = self.rpc.do(o) + self.assertTrue(c.parse_is_sealed(r)) + + + def test_seal_set_sink_address(self): + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + + (tx_hash, o) = c.set_sink_address(self.address, self.accounts[0], self.accounts[3]) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + (tx_hash, o) = c.set_sink_address(self.address, self.accounts[0], self.accounts[4]) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + (tx_hash, o) = c.seal(self.address, self.accounts[0], ContractState.SINK_STATE) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + (tx_hash, o) = c.set_sink_address(self.address, self.accounts[0], self.accounts[5]) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 0) + + o = c.is_sealed(self.address, ContractState.SINK_STATE, sender_address=self.accounts[0]) + r = self.rpc.do(o) + self.assertTrue(c.parse_is_sealed(r)) + + + def test_seal_cap(self): + nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) + c = DemurrageToken(self.chain_spec, signer=self.signer, nonce_oracle=nonce_oracle) + + (tx_hash, o) = c.set_max_supply(self.address, self.accounts[0], 100) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + (tx_hash, o) = c.set_max_supply(self.address, self.accounts[0], 200) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + (tx_hash, o) = c.seal(self.address, self.accounts[0], ContractState.CAP_STATE) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 1) + + (tx_hash, o) = c.set_max_supply(self.address, self.accounts[0], 300) + r = self.rpc.do(o) + o = receipt(tx_hash) + r = self.rpc.do(o) + self.assertEqual(r['status'], 0) + + o = c.is_sealed(self.address, ContractState.CAP_STATE, sender_address=self.accounts[0]) + r = self.rpc.do(o) + self.assertTrue(c.parse_is_sealed(r)) + + +if __name__ == '__main__': + unittest.main() + + diff --git a/python/tests/test_single.py b/python/tests/test_single.py @@ -13,12 +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.base import TestDemurrageSingle +from erc20_demurrage_token.unittest import TestDemurrageDefault logging.basicConfig(level=logging.DEBUG) logg = logging.getLogger() @@ -26,11 +27,9 @@ logg = logging.getLogger() testdir = os.path.dirname(__file__) -class TestRedistributionSingle(TestDemurrageSingle): - +class TestRedistributionSingle(TestDemurrageDefault): def test_single_even_if_multiple(self): - mint_amount = 100000000 nonce_oracle = RPCNonceOracle(self.accounts[0], self.rpc) @@ -64,7 +63,7 @@ class TestRedistributionSingle(TestDemurrageSingle): r = self.rpc.do(o) self.assertEqual(r['status'], 1) - tax_modifier = (1 - (self.tax_level / 1000000)) ** 10 + tax_modifier = 0.98 o = c.balance_of(self.address, self.accounts[1], sender_address=self.accounts[0]) r = self.rpc.do(o) balance = c.parse_balance(r) diff --git a/solidity/DemurrageTokenMultiCap.sol b/solidity/DemurrageTokenMultiCap.sol @@ -1,633 +0,0 @@ -pragma solidity > 0.6.11; - -// SPDX-License-Identifier: GPL-3.0-or-later - -contract DemurrageTokenMultiCap { - - // Redistribution bit field, with associated shifts and masks - // (Uses sub-byte boundaries) - bytes32[] public redistributions; // uint1(isFractional) | uint95(unused) | uint20(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period) - uint8 constant shiftRedistributionPeriod = 0; - uint256 constant maskRedistributionPeriod = 0x00000000000000000000000000000000000000000000000000000000ffffffff; // (1 << 32) - 1 - uint8 constant shiftRedistributionValue = 32; - uint256 constant maskRedistributionValue = 0x00000000000000000000000000000000000000ffffffffffffffffff00000000; // ((1 << 72) - 1) << 32 - uint8 constant shiftRedistributionParticipants = 104; - uint256 constant maskRedistributionParticipants = 0x00000000000000000000000000000fffffffff00000000000000000000000000; // ((1 << 36) - 1) << 104 - uint8 constant shiftRedistributionDemurrage = 140; - uint256 constant maskRedistributionDemurrage = 0x000000000000000000000000fffff00000000000000000000000000000000000; // ((1 << 20) - 1) << 140 - uint8 constant shiftRedistributionIsFractional = 255; - uint256 constant maskRedistributionIsFractional = 0x8000000000000000000000000000000000000000000000000000000000000000; // 1 << 255 - - // Account bit field, with associated shifts and masks - // Mirrors structure of redistributions for consistency - mapping (address => bytes32) account; // uint152(unused) | uint32(period) | uint72(value) - uint8 constant shiftAccountValue = 0; - uint256 constant maskAccountValue = 0x0000000000000000000000000000000000000000000000ffffffffffffffffff; // (1 << 72) - 1 - uint8 constant shiftAccountPeriod = 72; - uint256 constant maskAccountPeriod = 0x00000000000000000000000000000000000000ffffffff000000000000000000; // ((1 << 32) - 1) << 72 - - // Cached demurrage amount, ppm with 38 digit resolution - uint128 public demurrageAmount; - - // Cached demurrage period; the period for which demurrageAmount was calculated - //uint128 public demurragePeriod; - // Cached demurrage timestamp; the timestamp for which demurrageAmount was last calculated - uint256 public demurrageTimestamp; - - // Implements EIP172 - address public owner; - - address newOwner; - - // Implements ERC20 - string public name; - - // Implements ERC20 - string public symbol; - - // Implements ERC20 - uint256 public decimals; - - // Implements ERC20 - uint256 public totalSupply; - - // Maximum amount of tokens that can be minted - uint256 public supplyCap; - - // Minimum amount of (demurraged) tokens an account must spend to participate in redistribution for a particular period - uint256 public minimumParticipantSpend; - - // 128 bit resolution of the demurrage divisor - // (this constant x 1000000 is contained within 128 bits) - uint256 constant ppmDivider = 100000000000000000000000000000000; - - // demurrage decimal width; 38 places - uint256 public immutable resolutionFactor = ppmDivider * 1000000; - - // Timestamp of start of periods (time which contract constructor was called) - uint256 public immutable periodStart; - - // Duration of a single redistribution period in seconds - uint256 public immutable periodDuration; - - // Demurrage in ppm per minute - uint256 public immutable taxLevel; - - // Addresses allowed to mint new tokens - mapping (address => bool) minter; - - // Storage for ERC20 approve/transferFrom methods - mapping (address => mapping (address => uint256 ) ) allowance; // holder -> spender -> amount (amount is subject to demurrage) - - // Address to send unallocated redistribution tokens - address sinkAddress; - - // Implements ERC20 - event Transfer(address indexed _from, address indexed _to, uint256 _value); - - // Implements ERC20 - event Approval(address indexed _owner, address indexed _spender, uint256 _value); - - // New tokens minted - event Mint(address indexed _minter, address indexed _beneficiary, uint256 _value); - - // New demurrage cache milestone calculated - event Decayed(uint256 indexed _period, uint256 indexed _periodCount, uint256 indexed _oldAmount, uint256 _newAmount); - - // When a new period threshold has been crossed - event Period(uint256 _period); - - // Redistribution applied on a single eligible account - event Redistribution(address indexed _account, uint256 indexed _period, uint256 _value); - - // Temporary event used in development, will be removed on prod - event Debug(bytes32 _foo); - - // EIP173 - event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); // EIP173 - - constructor(string memory _name, string memory _symbol, uint8 _decimals, uint256 _taxLevelMinute, uint256 _periodMinutes, address _defaultSinkAddress, uint256 _supplyCap) public { - // ACL setup - owner = msg.sender; - minter[owner] = true; - - // ERC20 setup - name = _name; - symbol = _symbol; - decimals = _decimals; - - // Demurrage setup - demurrageTimestamp = block.timestamp; - periodStart = demurrageTimestamp; - periodDuration = _periodMinutes * 60; - demurrageAmount = uint128(ppmDivider * 1000000); // Represents 38 decimal places - //demurragePeriod = 1; - taxLevel = _taxLevelMinute; // Represents 38 decimal places - bytes32 initialRedistribution = toRedistribution(0, 1000000, 0, 1); - redistributions.push(initialRedistribution); - - // Misc settings - supplyCap = _supplyCap; - sinkAddress = _defaultSinkAddress; - minimumParticipantSpend = 10 ** uint256(_decimals); - } - - // Given address will be allowed to call the mintTo() function - function addMinter(address _minter) public returns (bool) { - require(msg.sender == owner); - minter[_minter] = true; - return true; - } - - // Given address will no longer be allowed to call the mintTo() function - function removeMinter(address _minter) public returns (bool) { - require(msg.sender == owner || _minter == msg.sender); - minter[_minter] = false; - return true; - } - - /// Implements ERC20 - function balanceOf(address _account) public view returns (uint256) { - uint256 baseBalance; - uint256 currentDemurragedAmount; - uint256 periodCount; - - baseBalance = baseBalanceOf(_account); - - //periodCount = actualPeriod() - demurragePeriod; - periodCount = getMinutesDelta(demurrageTimestamp); - - currentDemurragedAmount = uint128(decayBy(demurrageAmount, periodCount)); - - return (baseBalance * currentDemurragedAmount) / (ppmDivider * 1000000); - } - - /// Balance unmodified by demurrage - function baseBalanceOf(address _account) public view returns (uint256) { - return uint256(account[_account]) & maskAccountValue; - } - - /// Increases base balance for a single account - function increaseBaseBalance(address _account, uint256 _delta) private returns (bool) { - uint256 oldBalance; - uint256 newBalance; - uint256 workAccount; - - workAccount = uint256(account[_account]); - - if (_delta == 0) { - return false; - } - - oldBalance = baseBalanceOf(_account); - newBalance = oldBalance + _delta; - require(uint160(newBalance) > uint160(oldBalance), 'ERR_WOULDWRAP'); // revert if increase would result in a wrapped value - workAccount &= (~maskAccountValue); - workAccount |= (newBalance & maskAccountValue); - account[_account] = bytes32(workAccount); - return true; - } - - /// Decreases base balance for a single account - function decreaseBaseBalance(address _account, uint256 _delta) private returns (bool) { - uint256 oldBalance; - uint256 newBalance; - uint256 workAccount; - - workAccount = uint256(account[_account]); - - if (_delta == 0) { - return false; - } - - oldBalance = baseBalanceOf(_account); - require(oldBalance >= _delta, 'ERR_OVERSPEND'); // overspend guard - newBalance = oldBalance - _delta; - workAccount &= (~maskAccountValue); - workAccount |= (newBalance & maskAccountValue); - account[_account] = bytes32(workAccount); - return true; - } - - // Creates new tokens out of thin air, and allocates them to the given address - // Triggers tax - function mintTo(address _beneficiary, uint256 _amount) external returns (bool) { - uint256 baseAmount; - - require(minter[msg.sender]); - require(_amount + totalSupply <= supplyCap); - - changePeriod(); - baseAmount = toBaseAmount(_amount); - totalSupply += _amount; - increaseBaseBalance(_beneficiary, baseAmount); - emit Mint(msg.sender, _beneficiary, _amount); - saveRedistributionSupply(); - return true; - } - - // Deserializes the redistribution word - // uint1(isFractional) | uint95(unused) | uint20(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period) - function toRedistribution(uint256 _participants, uint256 _demurrageModifierPpm, uint256 _value, uint256 _period) private pure returns(bytes32) { - bytes32 redistribution; - - redistribution |= bytes32((_demurrageModifierPpm << shiftRedistributionDemurrage) & maskRedistributionDemurrage); - redistribution |= bytes32((_participants << shiftRedistributionParticipants) & maskRedistributionParticipants); - redistribution |= bytes32((_value << shiftRedistributionValue) & maskRedistributionValue); - redistribution |= bytes32(_period & maskRedistributionPeriod); - return redistribution; - } - - // Serializes the demurrage period part of the redistribution word - function toRedistributionPeriod(bytes32 redistribution) public pure returns (uint256) { - return uint256(redistribution) & maskRedistributionPeriod; - } - - // Serializes the supply part of the redistribution word - function toRedistributionSupply(bytes32 redistribution) public pure returns (uint256) { - return (uint256(redistribution) & maskRedistributionValue) >> shiftRedistributionValue; - } - - // Serializes the number of participants part of the redistribution word - function toRedistributionParticipants(bytes32 redistribution) public pure returns (uint256) { - return (uint256(redistribution) & maskRedistributionParticipants) >> shiftRedistributionParticipants; - } - - // Serializes the demurrage modifier part of the redistribution word - function toRedistributionDemurrageModifier(bytes32 redistribution) public pure returns (uint256) { - return (uint256(redistribution) & maskRedistributionDemurrage) >> shiftRedistributionDemurrage; - } - - // Client accessor to the redistributions array length - function redistributionCount() public view returns (uint256) { - return redistributions.length; - } - - // Add number of participants for the current redistribution period by one - function incrementRedistributionParticipants() private returns (bool) { - bytes32 currentRedistribution; - uint256 tmpRedistribution; - uint256 participants; - - currentRedistribution = redistributions[redistributions.length-1]; - participants = toRedistributionParticipants(currentRedistribution) + 1; - tmpRedistribution = uint256(currentRedistribution); - tmpRedistribution &= (~maskRedistributionParticipants); - tmpRedistribution |= ((participants << shiftRedistributionParticipants) & maskRedistributionParticipants); - - redistributions[redistributions.length-1] = bytes32(tmpRedistribution); - - return true; - } - - // Save the current total supply amount to the current redistribution period - function saveRedistributionSupply() private returns (bool) { - uint256 currentRedistribution; - - currentRedistribution = uint256(redistributions[redistributions.length-1]); - currentRedistribution &= (~maskRedistributionValue); - currentRedistribution |= (totalSupply << shiftRedistributionValue); - - redistributions[redistributions.length-1] = bytes32(currentRedistribution); - return true; - } - - // Get the demurrage period of the current block number - function actualPeriod() public view returns (uint128) { - return uint128((block.timestamp - periodStart) / periodDuration + 1); - } - - // Add an entered demurrage period to the redistribution array - function checkPeriod() private view returns (bytes32) { - bytes32 lastRedistribution; - uint256 currentPeriod; - - lastRedistribution = redistributions[redistributions.length-1]; - currentPeriod = this.actualPeriod(); - if (currentPeriod <= toRedistributionPeriod(lastRedistribution)) { - return bytes32(0x00); - } - return lastRedistribution; - } - - // Deserialize the pemurrage period for the given account is participating in - function accountPeriod(address _account) public view returns (uint256) { - return (uint256(account[_account]) & maskAccountPeriod) >> shiftAccountPeriod; - } - - // Save the given demurrage period as the currently participation period for the given address - function registerAccountPeriod(address _account, uint256 _period) private returns (bool) { - account[_account] &= bytes32(~maskAccountPeriod); - account[_account] |= bytes32((_period << shiftAccountPeriod) & maskAccountPeriod); - incrementRedistributionParticipants(); - return true; - } - - // Determine whether the unit number is rounded down, rounded up or evenly divides. - // Returns 0 if evenly distributed, or the remainder as a positive number - // A _numParts value 0 will be interpreted as the value 1 - function remainder(uint256 _numParts, uint256 _sumWhole) public pure returns (uint256) { - uint256 unit; - uint256 truncatedResult; - - if (_numParts == 0) { // no division by zero please - revert('ERR_NUMPARTS_ZERO'); - } - require(_numParts < _sumWhole); // At least you are never LESS than the sum of your parts. Think about that. - - unit = _sumWhole / _numParts; - truncatedResult = unit * _numParts; - return _sumWhole - truncatedResult; - } - - // Called in the edge case where participant number is 0. It will override the participant count to 1. - // Returns the remainder sent to the sink address - function applyDefaultRedistribution(bytes32 _redistribution) private returns (uint256) { - uint256 redistributionSupply; - uint256 redistributionPeriod; - uint256 unit; - uint256 truncatedResult; - - redistributionSupply = toRedistributionSupply(_redistribution); - - unit = (redistributionSupply * taxLevel) / 1000000; - truncatedResult = (unit * 1000000) / taxLevel; - - if (truncatedResult < redistributionSupply) { - redistributionPeriod = toRedistributionPeriod(_redistribution); // since we reuse period here, can possibly be optimized by passing period instead - redistributions[redistributionPeriod-1] &= bytes32(~maskRedistributionParticipants); // just to be safe, zero out all participant count data, in this case there will be only one - redistributions[redistributionPeriod-1] |= bytes32(maskRedistributionIsFractional | (1 << shiftRedistributionParticipants)); - } - - increaseBaseBalance(sinkAddress, unit / ppmDivider); - return unit; - } - - // sets the remainder bit for the given period and books the remainder to the sink address balance - // returns false if no change was made - function applyRemainderOnPeriod(uint256 _remainder, uint256 _period) private returns (bool) { - uint256 periodSupply; - - if (_remainder == 0) { - return false; - } - - // TODO: is this needed? - redistributions[_period-1] |= bytes32(maskRedistributionIsFractional); - - periodSupply = toRedistributionSupply(redistributions[_period-1]); - increaseBaseBalance(sinkAddress, periodSupply - _remainder); - return true; - } - - - // Calculate the time delta in whole minutes passed between given timestamp and current timestamp - function getMinutesDelta(uint256 _lastTimestamp) public view returns (uint256) { - return (block.timestamp - _lastTimestamp) / 60; - } - - // Calculate and cache the demurrage value corresponding to the (period of the) time of the method call - function applyDemurrage() public returns (bool) { - //uint128 epochPeriodCount; - uint256 periodCount; - uint256 lastDemurrageAmount; - uint256 newDemurrageAmount; - - //epochPeriodCount = actualPeriod(); - //periodCount = epochPeriodCount - demurragePeriod; - periodCount = getMinutesDelta(demurrageTimestamp); - if (periodCount == 0) { - return false; - } - lastDemurrageAmount = demurrageAmount; - demurrageAmount = uint128(decayBy(lastDemurrageAmount, periodCount)); - //demurragePeriod = epochPeriodCount; - demurrageTimestamp = demurrageTimestamp + (periodCount * 60); - //emit Decayed(epochPeriodCount, periodCount, lastDemurrageAmount, demurrageAmount); - emit Decayed(demurrageTimestamp, periodCount, lastDemurrageAmount, demurrageAmount); - return true; - } - - // Return timestamp of start of period threshold - function getPeriodTimeDelta(uint256 _periodCount) public view returns (uint256) { - return periodStart + (_periodCount * periodDuration); - } - - // Amount of demurrage cycles inbetween the current timestamp and the given target time - function demurrageCycles(uint256 _target) public view returns (uint256) { - return (block.timestamp - _target) / 60; - } - - // Recalculate the demurrage modifier for the new period - // After this, all REPORTED balances will have been reduced by the corresponding ratio (but the effecive totalsupply stays the same) - function changePeriod() public returns (bool) { - bytes32 currentRedistribution; - bytes32 nextRedistribution; - uint256 currentPeriod; - uint256 currentParticipants; - uint256 currentRemainder; - uint256 currentDemurrageAmount; - uint256 nextRedistributionDemurrage; - uint256 demurrageCounts; - uint256 periodTimestamp; - uint256 nextPeriod; - - applyDemurrage(); - - currentRedistribution = checkPeriod(); - if (currentRedistribution == bytes32(0x00)) { - return false; - } - - currentPeriod = toRedistributionPeriod(currentRedistribution); - nextPeriod = currentPeriod + 1; - periodTimestamp = getPeriodTimeDelta(currentPeriod); - - //applyDemurrage(); - currentDemurrageAmount = demurrageAmount; - - demurrageCounts = demurrageCycles(periodTimestamp); - if (demurrageCounts > 0) { - nextRedistributionDemurrage = growBy(currentDemurrageAmount, demurrageCounts) / ppmDivider; - } else { - nextRedistributionDemurrage = currentDemurrageAmount / ppmDivider; - } - - nextRedistribution = toRedistribution(0, nextRedistributionDemurrage, totalSupply, nextPeriod); - redistributions.push(nextRedistribution); - - currentParticipants = toRedistributionParticipants(currentRedistribution); - if (currentParticipants == 0) { - currentRemainder = applyDefaultRedistribution(currentRedistribution); - } else { - currentRemainder = remainder(currentParticipants, totalSupply); // we can use totalSupply directly because it will always be the same as the recorded supply on the current redistribution - applyRemainderOnPeriod(currentRemainder, currentPeriod); - } - emit Period(nextPeriod); - return true; - } - - // Reverse a value reduced by demurrage by the given period to its original value - function growBy(uint256 _value, uint256 _period) public view returns (uint256) { - uint256 valueFactor; - uint256 truncatedTaxLevel; - - valueFactor = 1000000; - truncatedTaxLevel = taxLevel / ppmDivider; - - for (uint256 i = 0; i < _period; i++) { - valueFactor = valueFactor + ((valueFactor * truncatedTaxLevel) / 1000000); - } - return (valueFactor * _value) / 1000000; - } - - // Calculate a value reduced by demurrage by the given period - // TODO: higher precision if possible - function decayBy(uint256 _value, uint256 _period) public view returns (uint256) { - uint256 valueFactor; - uint256 truncatedTaxLevel; - - valueFactor = 1000000; - truncatedTaxLevel = taxLevel / ppmDivider; - - for (uint256 i = 0; i < _period; i++) { - valueFactor = valueFactor - ((valueFactor * truncatedTaxLevel) / 1000000); - } - return (valueFactor * _value) / 1000000; - } - - // If the given account is participating in a period and that period has been crossed - // THEN increase the base value of the account with its share of the value reduction of the period - function applyRedistributionOnAccount(address _account) public returns (bool) { - bytes32 periodRedistribution; - uint256 supply; - uint256 participants; - uint256 baseValue; - uint256 value; - uint256 period; - uint256 demurrage; - - period = accountPeriod(_account); - if (period == 0 || period >= actualPeriod()) { - return false; - } - periodRedistribution = redistributions[period-1]; - participants = toRedistributionParticipants(periodRedistribution); - if (participants == 0) { - return false; - } - - supply = toRedistributionSupply(periodRedistribution); - demurrage = toRedistributionDemurrageModifier(periodRedistribution); - baseValue = ((supply / participants) * (taxLevel / 1000000)) / ppmDivider; - value = (baseValue * demurrage) / 1000000; - - // zero out period for the account - account[_account] &= bytes32(~maskAccountPeriod); - increaseBaseBalance(_account, value); - - emit Redistribution(_account, period, value); - return true; - } - - // Inflates the given amount according to the current demurrage modifier - function toBaseAmount(uint256 _value) public view returns (uint256) { - //return (_value * ppmDivider * 1000000) / toDemurrageAmount(demurrageModifier); - return (_value * ppmDivider * 1000000) / demurrageAmount; - } - - // Implements ERC20, triggers tax and/or redistribution - function approve(address _spender, uint256 _value) public returns (bool) { - uint256 baseValue; - - changePeriod(); - applyRedistributionOnAccount(msg.sender); - - baseValue = toBaseAmount(_value); - allowance[msg.sender][_spender] += baseValue; - emit Approval(msg.sender, _spender, _value); - return true; - } - - // Implements ERC20, triggers tax and/or redistribution - function transfer(address _to, uint256 _value) public returns (bool) { - uint256 baseValue; - bool result; - - changePeriod(); - applyRedistributionOnAccount(msg.sender); - - baseValue = toBaseAmount(_value); - result = transferBase(msg.sender, _to, baseValue); - emit Transfer(msg.sender, _to, _value); - return result; - } - - - // Implements ERC20, triggers tax and/or redistribution - function transferFrom(address _from, address _to, uint256 _value) public returns (bool) { - uint256 baseValue; - bool result; - - changePeriod(); - applyRedistributionOnAccount(msg.sender); - - baseValue = toBaseAmount(_value); - require(allowance[_from][msg.sender] >= baseValue); - - result = transferBase(_from, _to, baseValue); - emit Transfer(_from, _to, _value); - return result; - } - - // ERC20 transfer backend for transfer, transferFrom - function transferBase(address _from, address _to, uint256 _value) private returns (bool) { - uint256 period; - - decreaseBaseBalance(_from, _value); - increaseBaseBalance(_to, _value); - - period = actualPeriod(); - if (_value >= minimumParticipantSpend && accountPeriod(_from) != period && _from != _to) { - registerAccountPeriod(_from, period); - } - return true; - } - - // Implements EIP173 - function transferOwnership(address _newOwner) public returns (bool) { - require(msg.sender == owner); - newOwner = _newOwner; - } - - // Implements OwnedAccepter - function acceptOwnership() public returns (bool) { - address oldOwner; - - require(msg.sender == newOwner); - oldOwner = owner; - owner = newOwner; - newOwner = address(0); - emit OwnershipTransferred(oldOwner, owner); - } - - // Implements EIP165 - function supportsInterface(bytes4 _sum) public pure returns (bool) { - if (_sum == 0xc6bb4b70) { // ERC20 - return true; - } - if (_sum == 0x449a52f8) { // Minter - return true; - } - if (_sum == 0x01ffc9a7) { // EIP165 - return true; - } - if (_sum == 0x9493f8b2) { // EIP173 - return true; - } - if (_sum == 0x37a47be4) { // OwnedAccepter - return true; - } - return false; - } -} diff --git a/solidity/DemurrageTokenMultiNocap.sol b/solidity/DemurrageTokenMultiNocap.sol @@ -1,614 +0,0 @@ -pragma solidity > 0.6.11; - -// SPDX-License-Identifier: GPL-3.0-or-later - -contract DemurrageTokenMultiNocap { - - // Redistribution bit field, with associated shifts and masks - // (Uses sub-byte boundaries) - bytes32[] public redistributions; // uint1(isFractional) | uint95(unused) | uint20(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period) - uint8 constant shiftRedistributionPeriod = 0; - uint256 constant maskRedistributionPeriod = 0x00000000000000000000000000000000000000000000000000000000ffffffff; // (1 << 32) - 1 - uint8 constant shiftRedistributionValue = 32; - uint256 constant maskRedistributionValue = 0x00000000000000000000000000000000000000ffffffffffffffffff00000000; // ((1 << 72) - 1) << 32 - uint8 constant shiftRedistributionParticipants = 104; - uint256 constant maskRedistributionParticipants = 0x00000000000000000000000000000fffffffff00000000000000000000000000; // ((1 << 36) - 1) << 104 - uint8 constant shiftRedistributionDemurrage = 140; - uint256 constant maskRedistributionDemurrage = 0x000000000000000000000000fffff00000000000000000000000000000000000; // ((1 << 20) - 1) << 140 - uint8 constant shiftRedistributionIsFractional = 255; - uint256 constant maskRedistributionIsFractional = 0x8000000000000000000000000000000000000000000000000000000000000000; // 1 << 255 - - // Account bit field, with associated shifts and masks - // Mirrors structure of redistributions for consistency - mapping (address => bytes32) account; // uint152(unused) | uint32(period) | uint72(value) - uint8 constant shiftAccountValue = 0; - uint256 constant maskAccountValue = 0x0000000000000000000000000000000000000000000000ffffffffffffffffff; // (1 << 72) - 1 - uint8 constant shiftAccountPeriod = 72; - uint256 constant maskAccountPeriod = 0x00000000000000000000000000000000000000ffffffff000000000000000000; // ((1 << 32) - 1) << 72 - - // Cached demurrage amount, ppm with 38 digit resolution - uint128 public demurrageAmount; - - // Cached demurrage period; the period for which demurrageAmount was calculated - uint128 public demurragePeriod; - - // Implements EIP172 - address public owner; - - address newOwner; - - // Implements ERC20 - string public name; - - // Implements ERC20 - string public symbol; - - // Implements ERC20 - uint256 public decimals; - - // Implements ERC20 - uint256 public totalSupply; - - // Minimum amount of (demurraged) tokens an account must spend to participate in redistribution for a particular period - uint256 public minimumParticipantSpend; - - // 128 bit resolution of the demurrage divisor - // (this constant x 1000000 is contained within 128 bits) - uint256 constant ppmDivider = 100000000000000000000000000000000; - - // demurrage decimal width; 38 places - uint256 public immutable resolutionFactor = ppmDivider * 1000000; - - // Timestamp of start of periods (time which contract constructor was called) - uint256 public immutable periodStart; - - // Duration of a single redistribution period in seconds - uint256 public immutable periodDuration; - - // Demurrage in ppm per minute - uint256 public immutable taxLevel; - - // Addresses allowed to mint new tokens - mapping (address => bool) minter; - - // Storage for ERC20 approve/transferFrom methods - mapping (address => mapping (address => uint256 ) ) allowance; // holder -> spender -> amount (amount is subject to demurrage) - - // Address to send unallocated redistribution tokens - address sinkAddress; - - // Implements ERC20 - event Transfer(address indexed _from, address indexed _to, uint256 _value); - - // Implements ERC20 - event Approval(address indexed _owner, address indexed _spender, uint256 _value); - - // New tokens minted - event Mint(address indexed _minter, address indexed _beneficiary, uint256 _value); - - // New demurrage cache milestone calculated - event Decayed(uint256 indexed _period, uint256 indexed _periodCount, uint256 indexed _oldAmount, uint256 _newAmount); - - // When a new period threshold has been crossed - event Period(uint256 _period); - - // Redistribution applied on a single eligible account - event Redistribution(address indexed _account, uint256 indexed _period, uint256 _value); - - // Temporary event used in development, will be removed on prod - event Debug(bytes32 _foo); - - // EIP173 - event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); // EIP173 - - constructor(string memory _name, string memory _symbol, uint8 _decimals, uint256 _taxLevelMinute, uint256 _periodMinutes, address _defaultSinkAddress) public { - // ACL setup - owner = msg.sender; - minter[owner] = true; - - // ERC20 setup - name = _name; - symbol = _symbol; - decimals = _decimals; - - // Demurrage setup - periodStart = block.timestamp; - periodDuration = _periodMinutes * 60; - demurrageAmount = uint128(ppmDivider * 1000000); // Represents 38 decimal places - demurragePeriod = 1; - taxLevel = _taxLevelMinute; // Represents 38 decimal places - bytes32 initialRedistribution = toRedistribution(0, 1000000, 0, 1); - redistributions.push(initialRedistribution); - - // Misc settings - sinkAddress = _defaultSinkAddress; - minimumParticipantSpend = 10 ** uint256(_decimals); - } - - // Given address will be allowed to call the mintTo() function - function addMinter(address _minter) public returns (bool) { - require(msg.sender == owner); - minter[_minter] = true; - return true; - } - - // Given address will no longer be allowed to call the mintTo() function - function removeMinter(address _minter) public returns (bool) { - require(msg.sender == owner || _minter == msg.sender); - minter[_minter] = false; - return true; - } - - /// Implements ERC20 - function balanceOf(address _account) public view returns (uint256) { - uint256 baseBalance; - uint256 currentDemurragedAmount; - uint256 periodCount; - - baseBalance = baseBalanceOf(_account); - - periodCount = actualPeriod() - demurragePeriod; - - currentDemurragedAmount = uint128(decayBy(demurrageAmount, periodCount)); - - return (baseBalance * currentDemurragedAmount) / (ppmDivider * 1000000); - } - - /// Balance unmodified by demurrage - function baseBalanceOf(address _account) public view returns (uint256) { - return uint256(account[_account]) & maskAccountValue; - } - - /// Increases base balance for a single account - function increaseBaseBalance(address _account, uint256 _delta) private returns (bool) { - uint256 oldBalance; - uint256 newBalance; - uint256 workAccount; - - workAccount = uint256(account[_account]); - - if (_delta == 0) { - return false; - } - - oldBalance = baseBalanceOf(_account); - newBalance = oldBalance + _delta; - require(uint160(newBalance) > uint160(oldBalance), 'ERR_WOULDWRAP'); // revert if increase would result in a wrapped value - workAccount &= (~maskAccountValue); - workAccount |= (newBalance & maskAccountValue); - account[_account] = bytes32(workAccount); - return true; - } - - /// Decreases base balance for a single account - function decreaseBaseBalance(address _account, uint256 _delta) private returns (bool) { - uint256 oldBalance; - uint256 newBalance; - uint256 workAccount; - - workAccount = uint256(account[_account]); - - if (_delta == 0) { - return false; - } - - oldBalance = baseBalanceOf(_account); - require(oldBalance >= _delta, 'ERR_OVERSPEND'); // overspend guard - newBalance = oldBalance - _delta; - workAccount &= (~maskAccountValue); - workAccount |= (newBalance & maskAccountValue); - account[_account] = bytes32(workAccount); - return true; - } - - // Creates new tokens out of thin air, and allocates them to the given address - // Triggers tax - function mintTo(address _beneficiary, uint256 _amount) external returns (bool) { - uint256 baseAmount; - - require(minter[msg.sender]); - - changePeriod(); - baseAmount = _amount; - totalSupply += _amount; - increaseBaseBalance(_beneficiary, baseAmount); - emit Mint(msg.sender, _beneficiary, _amount); - saveRedistributionSupply(); - return true; - } - - // Deserializes the redistribution word - // uint1(isFractional) | uint95(unused) | uint20(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period) - function toRedistribution(uint256 _participants, uint256 _demurrageModifierPpm, uint256 _value, uint256 _period) private pure returns(bytes32) { - bytes32 redistribution; - - redistribution |= bytes32((_demurrageModifierPpm << shiftRedistributionDemurrage) & maskRedistributionDemurrage); - redistribution |= bytes32((_participants << shiftRedistributionParticipants) & maskRedistributionParticipants); - redistribution |= bytes32((_value << shiftRedistributionValue) & maskRedistributionValue); - redistribution |= bytes32(_period & maskRedistributionPeriod); - return redistribution; - } - - // Serializes the demurrage period part of the redistribution word - function toRedistributionPeriod(bytes32 redistribution) public pure returns (uint256) { - return uint256(redistribution) & maskRedistributionPeriod; - } - - // Serializes the supply part of the redistribution word - function toRedistributionSupply(bytes32 redistribution) public pure returns (uint256) { - return (uint256(redistribution) & maskRedistributionValue) >> shiftRedistributionValue; - } - - // Serializes the number of participants part of the redistribution word - function toRedistributionParticipants(bytes32 redistribution) public pure returns (uint256) { - return (uint256(redistribution) & maskRedistributionParticipants) >> shiftRedistributionParticipants; - } - - // Serializes the number of participants part of the redistribution word - function toRedistributionDemurrageModifier(bytes32 redistribution) public pure returns (uint256) { - return (uint256(redistribution) & maskRedistributionDemurrage) >> shiftRedistributionDemurrage; - } - - // Client accessor to the redistributions array length - function redistributionCount() public view returns (uint256) { - return redistributions.length; - } - - // Add number of participants for the current redistribution period by one - function incrementRedistributionParticipants() private returns (bool) { - bytes32 currentRedistribution; - uint256 tmpRedistribution; - uint256 participants; - - currentRedistribution = redistributions[redistributions.length-1]; - participants = toRedistributionParticipants(currentRedistribution) + 1; - tmpRedistribution = uint256(currentRedistribution); - tmpRedistribution &= (~maskRedistributionParticipants); - tmpRedistribution |= ((participants << shiftRedistributionParticipants) & maskRedistributionParticipants); - - redistributions[redistributions.length-1] = bytes32(tmpRedistribution); - - return true; - } - - // Save the current total supply amount to the current redistribution period - function saveRedistributionSupply() private returns (bool) { - uint256 currentRedistribution; - - currentRedistribution = uint256(redistributions[redistributions.length-1]); - currentRedistribution &= (~maskRedistributionValue); - currentRedistribution |= (totalSupply << shiftRedistributionValue); - - redistributions[redistributions.length-1] = bytes32(currentRedistribution); - return true; - } - - // Get the demurrage period of the current block number - function actualPeriod() public view returns (uint128) { - return uint128((block.timestamp - periodStart) / periodDuration + 1); - } - - // Add an entered demurrage period to the redistribution array - function checkPeriod() private view returns (bytes32) { - bytes32 lastRedistribution; - uint256 currentPeriod; - - lastRedistribution = redistributions[redistributions.length-1]; - currentPeriod = this.actualPeriod(); - if (currentPeriod <= toRedistributionPeriod(lastRedistribution)) { - return bytes32(0x00); - } - return lastRedistribution; - } - - // Deserialize the pemurrage period for the given account is participating in - function accountPeriod(address _account) public view returns (uint256) { - return (uint256(account[_account]) & maskAccountPeriod) >> shiftAccountPeriod; - } - - // Save the given demurrage period as the currently participation period for the given address - function registerAccountPeriod(address _account, uint256 _period) private returns (bool) { - account[_account] &= bytes32(~maskAccountPeriod); - account[_account] |= bytes32((_period << shiftAccountPeriod) & maskAccountPeriod); - incrementRedistributionParticipants(); - return true; - } - - // Determine whether the unit number is rounded down, rounded up or evenly divides. - // Returns 0 if evenly distributed, or the remainder as a positive number - // A _numParts value 0 will be interpreted as the value 1 - function remainder(uint256 _numParts, uint256 _sumWhole) public pure returns (uint256) { - uint256 unit; - uint256 truncatedResult; - - if (_numParts == 0) { // no division by zero please - revert('ERR_NUMPARTS_ZERO'); - } - require(_numParts < _sumWhole); // At least you are never LESS than the sum of your parts. Think about that. - - unit = _sumWhole / _numParts; - truncatedResult = unit * _numParts; - return _sumWhole - truncatedResult; - } - - // Called in the edge case where participant number is 0. It will override the participant count to 1. - // Returns the remainder sent to the sink address - function applyDefaultRedistribution(bytes32 _redistribution) private returns (uint256) { - uint256 redistributionSupply; - uint256 redistributionPeriod; - uint256 unit; - uint256 truncatedResult; - - redistributionSupply = toRedistributionSupply(_redistribution); - - unit = (redistributionSupply * taxLevel) / 1000000; - truncatedResult = (unit * 1000000) / taxLevel; - - if (truncatedResult < redistributionSupply) { - redistributionPeriod = toRedistributionPeriod(_redistribution); // since we reuse period here, can possibly be optimized by passing period instead - redistributions[redistributionPeriod-1] &= bytes32(~maskRedistributionParticipants); // just to be safe, zero out all participant count data, in this case there will be only one - redistributions[redistributionPeriod-1] |= bytes32(maskRedistributionIsFractional | (1 << shiftRedistributionParticipants)); - } - - increaseBaseBalance(sinkAddress, unit / ppmDivider); - return unit; - } - - // sets the remainder bit for the given period and books the remainder to the sink address balance - // returns false if no change was made - function applyRemainderOnPeriod(uint256 _remainder, uint256 _period) private returns (bool) { - uint256 periodSupply; - - if (_remainder == 0) { - return false; - } - - // TODO: is this needed? - redistributions[_period-1] |= bytes32(maskRedistributionIsFractional); - - periodSupply = toRedistributionSupply(redistributions[_period-1]); - increaseBaseBalance(sinkAddress, periodSupply - _remainder); - return true; - } - - - // Calculate and cache the demurrage value corresponding to the (period of the) time of the method call - function applyDemurrage() public returns (bool) { - uint128 epochPeriodCount; - uint128 periodCount; - uint256 lastDemurrageAmount; - uint256 newDemurrageAmount; - - epochPeriodCount = actualPeriod(); - periodCount = epochPeriodCount - demurragePeriod; - if (periodCount == 0) { - return false; - } - lastDemurrageAmount = demurrageAmount; - demurrageAmount = uint128(decayBy(lastDemurrageAmount, periodCount)); - demurragePeriod = epochPeriodCount; - emit Decayed(epochPeriodCount, periodCount, lastDemurrageAmount, demurrageAmount); - return true; - } - - // Return timestamp of start of period threshold - function getPeriodTimeDelta(uint256 _periodCount) public view returns (uint256) { - return periodStart + (_periodCount * periodDuration); - } - - // Amount of demurrage cycles inbetween the current timestamp and the given target time - function demurrageCycles(uint256 _target) public view returns (uint256) { - return (block.timestamp - _target) / 60; - } - - // Recalculate the demurrage modifier for the new period - // After this, all REPORTED balances will have been reduced by the corresponding ratio (but the effecive totalsupply stays the same) - function changePeriod() public returns (bool) { - bytes32 currentRedistribution; - bytes32 nextRedistribution; - uint256 currentPeriod; - uint256 currentParticipants; - uint256 currentRemainder; - uint256 currentDemurrageAmount; - uint256 nextRedistributionDemurrage; - uint256 demurrageCounts; - uint256 periodTimestamp; - uint256 nextPeriod; - - currentRedistribution = checkPeriod(); - if (currentRedistribution == bytes32(0x00)) { - return false; - } - - currentPeriod = toRedistributionPeriod(currentRedistribution); - nextPeriod = currentPeriod + 1; - periodTimestamp = getPeriodTimeDelta(currentPeriod); - - applyDemurrage(); - currentDemurrageAmount = demurrageAmount; - - demurrageCounts = demurrageCycles(periodTimestamp); - if (demurrageCounts > 0) { - nextRedistributionDemurrage = growBy(currentDemurrageAmount, demurrageCounts) / ppmDivider; - } else { - nextRedistributionDemurrage = currentDemurrageAmount / ppmDivider; - } - - nextRedistribution = toRedistribution(0, nextRedistributionDemurrage, totalSupply, nextPeriod); - redistributions.push(nextRedistribution); - - currentParticipants = toRedistributionParticipants(currentRedistribution); - if (currentParticipants == 0) { - currentRemainder = applyDefaultRedistribution(currentRedistribution); - } else { - currentRemainder = remainder(currentParticipants, totalSupply); // we can use totalSupply directly because it will always be the same as the recorded supply on the current redistribution - applyRemainderOnPeriod(currentRemainder, currentPeriod); - } - emit Period(nextPeriod); - return true; - } - - // Reverse a value reduced by demurrage by the given period to its original value - function growBy(uint256 _value, uint256 _period) public view returns (uint256) { - uint256 valueFactor; - uint256 truncatedTaxLevel; - - valueFactor = 1000000; - truncatedTaxLevel = taxLevel / ppmDivider; - - for (uint256 i = 0; i < _period; i++) { - valueFactor = valueFactor + ((valueFactor * truncatedTaxLevel) / 1000000); - } - return (valueFactor * _value) / 1000000; - } - - // Calculate a value reduced by demurrage by the given period - // TODO: higher precision if possible - function decayBy(uint256 _value, uint256 _period) public view returns (uint256) { - uint256 valueFactor; - uint256 truncatedTaxLevel; - - valueFactor = 1000000; - truncatedTaxLevel = taxLevel / ppmDivider; - - for (uint256 i = 0; i < _period; i++) { - valueFactor = valueFactor - ((valueFactor * truncatedTaxLevel) / 1000000); - } - return (valueFactor * _value) / 1000000; - } - - // If the given account is participating in a period and that period has been crossed - // THEN increase the base value of the account with its share of the value reduction of the period - function applyRedistributionOnAccount(address _account) public returns (bool) { - bytes32 periodRedistribution; - uint256 supply; - uint256 participants; - uint256 baseValue; - uint256 value; - uint256 period; - uint256 demurrage; - - period = accountPeriod(_account); - if (period == 0 || period >= actualPeriod()) { - return false; - } - periodRedistribution = redistributions[period-1]; - participants = toRedistributionParticipants(periodRedistribution); - if (participants == 0) { - return false; - } - - supply = toRedistributionSupply(periodRedistribution); - demurrage = toRedistributionDemurrageModifier(periodRedistribution); - baseValue = ((supply / participants) * (taxLevel / 1000000)) / ppmDivider; - value = (baseValue * demurrage) / 1000000; - - // zero out period for the account - account[_account] &= bytes32(~maskAccountPeriod); - increaseBaseBalance(_account, value); - - emit Redistribution(_account, period, value); - return true; - } - - // Inflates the given amount according to the current demurrage modifier - function toBaseAmount(uint256 _value) public view returns (uint256) { - //return (_value * ppmDivider * 1000000) / toDemurrageAmount(demurrageModifier); - return (_value * ppmDivider * 1000000) / demurrageAmount; - } - - // Implements ERC20, triggers tax and/or redistribution - function approve(address _spender, uint256 _value) public returns (bool) { - uint256 baseValue; - - changePeriod(); - applyRedistributionOnAccount(msg.sender); - - baseValue = toBaseAmount(_value); - allowance[msg.sender][_spender] += baseValue; - emit Approval(msg.sender, _spender, _value); - return true; - } - - // Implements ERC20, triggers tax and/or redistribution - function transfer(address _to, uint256 _value) public returns (bool) { - uint256 baseValue; - bool result; - - changePeriod(); - applyRedistributionOnAccount(msg.sender); - - baseValue = toBaseAmount(_value); - result = transferBase(msg.sender, _to, baseValue); - emit Transfer(msg.sender, _to, _value); - return result; - } - - - // Implements ERC20, triggers tax and/or redistribution - function transferFrom(address _from, address _to, uint256 _value) public returns (bool) { - uint256 baseValue; - bool result; - - changePeriod(); - applyRedistributionOnAccount(msg.sender); - - baseValue = toBaseAmount(_value); - require(allowance[_from][msg.sender] >= baseValue); - - result = transferBase(_from, _to, baseValue); - emit Transfer(_from, _to, _value); - return result; - } - - // ERC20 transfer backend for transfer, transferFrom - function transferBase(address _from, address _to, uint256 _value) private returns (bool) { - uint256 period; - - decreaseBaseBalance(_from, _value); - increaseBaseBalance(_to, _value); - - period = actualPeriod(); - if (_value >= minimumParticipantSpend && accountPeriod(_from) != period && _from != _to) { - registerAccountPeriod(_from, period); - } - return true; - } - - // Implements EIP173 - function transferOwnership(address _newOwner) public returns (bool) { - require(msg.sender == owner); - newOwner = _newOwner; - } - - // Implements OwnedAccepter - function acceptOwnership() public returns (bool) { - address oldOwner; - - require(msg.sender == newOwner); - oldOwner = owner; - owner = newOwner; - newOwner = address(0); - emit OwnershipTransferred(oldOwner, owner); - } - - // Implements EIP165 - function supportsInterface(bytes4 _sum) public pure returns (bool) { - if (_sum == 0xc6bb4b70) { // ERC20 - return true; - } - if (_sum == 0x449a52f8) { // Minter - return true; - } - if (_sum == 0x01ffc9a7) { // EIP165 - return true; - } - if (_sum == 0x9493f8b2) { // EIP173 - return true; - } - if (_sum == 0x37a47be4) { // OwnedAccepter - return true; - } - return false; - } -} diff --git a/solidity/DemurrageTokenSingleCap.sol b/solidity/DemurrageTokenSingleCap.sol @@ -1,517 +0,0 @@ -pragma solidity > 0.6.11; - -// SPDX-License-Identifier: GPL-3.0-or-later - -contract DemurrageTokenSingleCap { - - // Redistribution bit field, with associated shifts and masks - // (Uses sub-byte boundaries) - bytes32[] public redistributions; // uint51(unused) | uint64(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period) - uint8 constant shiftRedistributionPeriod = 0; - uint256 constant maskRedistributionPeriod = 0x00000000000000000000000000000000000000000000000000000000ffffffff; // (1 << 32) - 1 - uint8 constant shiftRedistributionValue = 32; - uint256 constant maskRedistributionValue = 0x00000000000000000000000000000000000000ffffffffffffffffff00000000; // ((1 << 72) - 1) << 32 - uint8 constant shiftRedistributionDemurrage = 104; - uint256 constant maskRedistributionDemurrage = 0x0000000000ffffffffffffffffffffffffffff00000000000000000000000000; // ((1 << 20) - 1) << 140 - - // Account balances - mapping (address => uint256) account; - - // Cached demurrage amount, ppm with 38 digit resolution - uint128 public demurrageAmount; - - // Cached demurrage period; the period for which demurrageAmount was calculated - //uint128 public demurragePeriod; - // Cached demurrage timestamp; the timestamp for which demurrageAmount was last calculated - uint256 public demurrageTimestamp; - - // Implements EIP172 - address public owner; - - address newOwner; - - // Implements ERC20 - string public name; - - // Implements ERC20 - string public symbol; - - // Implements ERC20 - uint256 public decimals; - - // Implements ERC20 - uint256 public totalSupply; - - // Maximum amount of tokens that can be minted - uint256 public supplyCap; - - // Minimum amount of (demurraged) tokens an account must spend to participate in redistribution for a particular period - uint256 public minimumParticipantSpend; - - // 128 bit resolution of the demurrage divisor - // (this constant x 1000000 is contained within 128 bits) - uint256 constant nanoDivider = 100000000000000000000000000; // now nanodivider, 6 zeros less - - // remaining decimal positions of nanoDivider to reach 38, equals precision in growth and decay - uint256 constant growthResolutionFactor = 1000000000000; - - // demurrage decimal width; 38 places - uint256 public immutable resolutionFactor = nanoDivider * growthResolutionFactor; - - // Timestamp of start of periods (time which contract constructor was called) - uint256 public immutable periodStart; - - // Duration of a single redistribution period in seconds - uint256 public immutable periodDuration; - - // Demurrage in ppm per minute - uint256 public immutable taxLevel; - - // Addresses allowed to mint new tokens - mapping (address => bool) minter; - - // Storage for ERC20 approve/transferFrom methods - mapping (address => mapping (address => uint256 ) ) allowance; // holder -> spender -> amount (amount is subject to demurrage) - - // Address to send unallocated redistribution tokens - address sinkAddress; - - // Implements ERC20 - event Transfer(address indexed _from, address indexed _to, uint256 _value); - - // Implements ERC20 - event Approval(address indexed _owner, address indexed _spender, uint256 _value); - - // New tokens minted - event Mint(address indexed _minter, address indexed _beneficiary, uint256 _value); - - // New demurrage cache milestone calculated - event Decayed(uint256 indexed _period, uint256 indexed _periodCount, uint256 indexed _oldAmount, uint256 _newAmount); - - // When a new period threshold has been crossed - event Period(uint256 _period); - - // Redistribution applied on a single eligible account - event Redistribution(address indexed _account, uint256 indexed _period, uint256 _value); - - // Temporary event used in development, will be removed on prod - event Debug(bytes32 _foo); - - // EIP173 - event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); // EIP173 - - constructor(string memory _name, string memory _symbol, uint8 _decimals, uint128 _taxLevelMinute, uint256 _periodMinutes, address _defaultSinkAddress, uint256 _supplyCap) public { - // ACL setup - owner = msg.sender; - minter[owner] = true; - - // ERC20 setup - name = _name; - symbol = _symbol; - decimals = _decimals; - - // Demurrage setup - demurrageTimestamp = block.timestamp; - periodStart = demurrageTimestamp; - periodDuration = _periodMinutes * 60; - //demurrageAmount = 100000000000000000000000000000000000000 - _taxLevelMinute; // Represents 38 decimal places, same as resolutionFactor - //demurrageAmount = 100000000000000000000000000000000000000; - demurrageAmount = 10000000000000000000000000000; - //demurragePeriod = 1; - taxLevel = _taxLevelMinute; // Represents 38 decimal places - bytes32 initialRedistribution = toRedistribution(0, demurrageAmount, 0, 1); - redistributions.push(initialRedistribution); - - // Misc settings - supplyCap = _supplyCap; - sinkAddress = _defaultSinkAddress; - minimumParticipantSpend = 10 ** uint256(_decimals); - } - - // Change sink address for redistribution - function setSinkAddress(address _sinkAddress) public { - require(msg.sender == owner); - sinkAddress = _sinkAddress; - } - - // Given address will be allowed to call the mintTo() function - function addMinter(address _minter) public returns (bool) { - require(msg.sender == owner); - minter[_minter] = true; - return true; - } - - // Given address will no longer be allowed to call the mintTo() function - function removeMinter(address _minter) public returns (bool) { - require(msg.sender == owner || _minter == msg.sender); - minter[_minter] = false; - return true; - } - - /// Implements ERC20 - function balanceOf(address _account) public view returns (uint256) { - uint256 baseBalance; - uint256 currentDemurragedAmount; - uint256 periodCount; - - baseBalance = baseBalanceOf(_account); - - //periodCount = actualPeriod() - demurragePeriod; - periodCount = getMinutesDelta(demurrageTimestamp); - - currentDemurragedAmount = uint128(decayBy(demurrageAmount * 10000000000, periodCount)); - - return (baseBalance * currentDemurragedAmount) / (nanoDivider * 1000000000000); - } - - /// Balance unmodified by demurrage - function baseBalanceOf(address _account) public view returns (uint256) { - return account[_account]; - } - - /// Increases base balance for a single account - function increaseBaseBalance(address _account, uint256 _delta) private returns (bool) { - uint256 oldBalance; - uint256 newBalance; - uint256 workAccount; - - workAccount = uint256(account[_account]); - - if (_delta == 0) { - return false; - } - - oldBalance = baseBalanceOf(_account); - account[_account] = oldBalance + _delta; - return true; - } - - /// Decreases base balance for a single account - function decreaseBaseBalance(address _account, uint256 _delta) private returns (bool) { - uint256 oldBalance; - uint256 newBalance; - uint256 workAccount; - - workAccount = uint256(account[_account]); - - if (_delta == 0) { - return false; - } - - oldBalance = baseBalanceOf(_account); - require(oldBalance >= _delta, 'ERR_OVERSPEND'); // overspend guard - account[_account] = oldBalance - _delta; - return true; - } - - // Creates new tokens out of thin air, and allocates them to the given address - // Triggers tax - function mintTo(address _beneficiary, uint256 _amount) external returns (bool) { - uint256 baseAmount; - - require(minter[msg.sender], 'ERR_ACCESS'); - require(_amount + totalSupply <= supplyCap, 'ERR_CAP'); - - changePeriod(); - baseAmount = toBaseAmount(_amount); - totalSupply += _amount; - increaseBaseBalance(_beneficiary, baseAmount); - emit Mint(msg.sender, _beneficiary, _amount); - saveRedistributionSupply(); - return true; - } - - // Deserializes the redistribution word - // uint95(unused) | uint20(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period) - function toRedistribution(uint256 _participants, uint256 _demurrageModifierPpm, uint256 _value, uint256 _period) public pure returns(bytes32) { - bytes32 redistribution; - - redistribution |= bytes32((_demurrageModifierPpm << shiftRedistributionDemurrage) & maskRedistributionDemurrage); - redistribution |= bytes32((_value << shiftRedistributionValue) & maskRedistributionValue); - redistribution |= bytes32(_period & maskRedistributionPeriod); - return redistribution; - } - - // Serializes the demurrage period part of the redistribution word - function toRedistributionPeriod(bytes32 redistribution) public pure returns (uint256) { - return uint256(redistribution) & maskRedistributionPeriod; - } - - // Serializes the supply part of the redistribution word - function toRedistributionSupply(bytes32 redistribution) public pure returns (uint256) { - return (uint256(redistribution) & maskRedistributionValue) >> shiftRedistributionValue; - } - - // Serializes the number of participants part of the redistribution word - function toRedistributionDemurrageModifier(bytes32 redistribution) public pure returns (uint256) { - return (uint256(redistribution) & maskRedistributionDemurrage) >> shiftRedistributionDemurrage; - } - - // Client accessor to the redistributions array length - function redistributionCount() public view returns (uint256) { - return redistributions.length; - } - - // Save the current total supply amount to the current redistribution period - function saveRedistributionSupply() private returns (bool) { - uint256 currentRedistribution; - uint256 grownSupply; - - //grownSupply = growBy(totalSupply, 1); - grownSupply = totalSupply; - currentRedistribution = uint256(redistributions[redistributions.length-1]); - currentRedistribution &= (~maskRedistributionValue); - currentRedistribution |= (grownSupply << shiftRedistributionValue); - - redistributions[redistributions.length-1] = bytes32(currentRedistribution); - return true; - } - - // Get the demurrage period of the current block number - function actualPeriod() public view returns (uint128) { - return uint128((block.timestamp - periodStart) / periodDuration + 1); - } - - // Add an entered demurrage period to the redistribution array - function checkPeriod() private view returns (bytes32) { - bytes32 lastRedistribution; - uint256 currentPeriod; - - lastRedistribution = redistributions[redistributions.length-1]; - currentPeriod = this.actualPeriod(); - if (currentPeriod <= toRedistributionPeriod(lastRedistribution)) { - return bytes32(0x00); - } - return lastRedistribution; - } - - function getDistribution(uint256 _supply, uint256 _demurrageAmount) public view returns (uint256) { - uint256 difference; - - difference = _supply * (resolutionFactor - (_demurrageAmount * 10000000000)); //(nanoDivider - ((resolutionFactor - _demurrageAmount) / nanoDivider)); - return difference / resolutionFactor; - } - - function getDistributionFromRedistribution(bytes32 _redistribution) public returns (uint256) { - uint256 redistributionSupply; - uint256 redistributionDemurrage; - - redistributionSupply = toRedistributionSupply(_redistribution); - redistributionDemurrage = toRedistributionDemurrageModifier(_redistribution); - return getDistribution(redistributionSupply, redistributionDemurrage); - } - - // Returns the amount sent to the sink address - function applyDefaultRedistribution(bytes32 _redistribution) private returns (uint256) { - uint256 unit; - - unit = getDistributionFromRedistribution(_redistribution); - increaseBaseBalance(sinkAddress, toBaseAmount(unit)); - return unit; - } - - // Calculate the time delta in whole minutes passed between given timestamp and current timestamp - function getMinutesDelta(uint256 _lastTimestamp) public view returns (uint256) { - return (block.timestamp - _lastTimestamp) / 60; - } - - // Calculate and cache the demurrage value corresponding to the (period of the) time of the method call - function applyDemurrage() public returns (bool) { - return applyDemurrageLimited(0); - } - - function applyDemurrageLimited(uint256 _rounds) public returns (bool) { - //uint128 epochPeriodCount; - uint256 periodCount; - uint256 lastDemurrageAmount; - - //epochPeriodCount = actualPeriod(); - //periodCount = epochPeriodCount - demurragePeriod; - - periodCount = getMinutesDelta(demurrageTimestamp); - if (periodCount == 0) { - return false; - } - lastDemurrageAmount = demurrageAmount; - // safety limit for exponential calculation to ensure that we can always - // execute this code no matter how much time passes. - if (_rounds > 0 && _rounds < periodCount) { - periodCount = _rounds; - } - - demurrageAmount = uint128(decayBy(lastDemurrageAmount, periodCount)); - //demurragePeriod = epochPeriodCount; - demurrageTimestamp = demurrageTimestamp + (periodCount * 60); - emit Decayed(demurrageTimestamp, periodCount, lastDemurrageAmount, demurrageAmount); - return true; - } - - // Return timestamp of start of period threshold - function getPeriodTimeDelta(uint256 _periodCount) public view returns (uint256) { - return periodStart + (_periodCount * periodDuration); - } - - // Amount of demurrage cycles inbetween the current timestamp and the given target time - function demurrageCycles(uint256 _target) public view returns (uint256) { - return (block.timestamp - _target) / 60; - } - - // Recalculate the demurrage modifier for the new period - function changePeriod() public returns (bool) { - bytes32 currentRedistribution; - bytes32 nextRedistribution; - uint256 currentPeriod; - uint256 currentDemurrageAmount; - uint256 nextRedistributionDemurrage; - uint256 demurrageCounts; - uint256 periodTimestamp; - uint256 nextPeriod; - - applyDemurrage(); - currentRedistribution = checkPeriod(); - if (currentRedistribution == bytes32(0x00)) { - return false; - } - - currentPeriod = toRedistributionPeriod(currentRedistribution); - nextPeriod = currentPeriod + 1; - periodTimestamp = getPeriodTimeDelta(currentPeriod); - - currentDemurrageAmount = demurrageAmount; - - demurrageCounts = demurrageCycles(periodTimestamp); - if (demurrageCounts > 0) { - nextRedistributionDemurrage = growBy(currentDemurrageAmount, demurrageCounts); - } else { - nextRedistributionDemurrage = currentDemurrageAmount; - } - - nextRedistribution = toRedistribution(0, nextRedistributionDemurrage, totalSupply, nextPeriod); - redistributions.push(nextRedistribution); - - applyDefaultRedistribution(nextRedistribution); - emit Period(nextPeriod); - return true; - } - - // Reverse a value reduced by demurrage by the given period to its original value - function growBy(uint256 _value, uint256 _period) public view returns (uint256) { - uint256 valueFactor; - uint256 truncatedTaxLevel; - - valueFactor = growthResolutionFactor; - truncatedTaxLevel = taxLevel / nanoDivider; - - for (uint256 i = 0; i < _period; i++) { - valueFactor = valueFactor + ((valueFactor * truncatedTaxLevel) / growthResolutionFactor); - } - return (valueFactor * _value) / growthResolutionFactor; - } - - // Calculate a value reduced by demurrage by the given period - function decayBy(uint256 _value, uint256 _period) public view returns (uint256) { - uint256 valueFactor; - uint256 truncatedTaxLevel; - - valueFactor = growthResolutionFactor; - truncatedTaxLevel = taxLevel / nanoDivider; - - for (uint256 i = 0; i < _period; i++) { - valueFactor = valueFactor - ((valueFactor * truncatedTaxLevel) / growthResolutionFactor); - } - return (valueFactor * _value) / growthResolutionFactor; - } - - // Inflates the given amount according to the current demurrage modifier - function toBaseAmount(uint256 _value) public view returns (uint256) { - return (_value * resolutionFactor) / (demurrageAmount * 10000000000); - } - - // Implements ERC20, triggers tax and/or redistribution - function approve(address _spender, uint256 _value) public returns (bool) { - uint256 baseValue; - - changePeriod(); - - baseValue = toBaseAmount(_value); - allowance[msg.sender][_spender] += baseValue; - emit Approval(msg.sender, _spender, _value); - return true; - } - - // Implements ERC20, triggers tax and/or redistribution - function transfer(address _to, uint256 _value) public returns (bool) { - uint256 baseValue; - bool result; - - changePeriod(); - - baseValue = toBaseAmount(_value); - result = transferBase(msg.sender, _to, baseValue); - emit Transfer(msg.sender, _to, _value); - return result; - } - - // Implements ERC20, triggers tax and/or redistribution - function transferFrom(address _from, address _to, uint256 _value) public returns (bool) { - uint256 baseValue; - bool result; - - changePeriod(); - - baseValue = toBaseAmount(_value); - require(allowance[_from][msg.sender] >= baseValue); - - result = transferBase(_from, _to, baseValue); - emit Transfer(_from, _to, _value); - return result; - } - - // ERC20 transfer backend for transfer, transferFrom - function transferBase(address _from, address _to, uint256 _value) private returns (bool) { - uint256 period; - - decreaseBaseBalance(_from, _value); - increaseBaseBalance(_to, _value); - - //period = actualPeriod(); - return true; - } - - // Implements EIP173 - function transferOwnership(address _newOwner) public returns (bool) { - require(msg.sender == owner); - newOwner = _newOwner; - } - - // Implements OwnedAccepter - function acceptOwnership() public returns (bool) { - address oldOwner; - - require(msg.sender == newOwner); - oldOwner = owner; - owner = newOwner; - newOwner = address(0); - emit OwnershipTransferred(oldOwner, owner); - } - - // Implements EIP165 - function supportsInterface(bytes4 _sum) public pure returns (bool) { - if (_sum == 0xc6bb4b70) { // ERC20 - return true; - } - if (_sum == 0x449a52f8) { // Minter - return true; - } - if (_sum == 0x01ffc9a7) { // EIP165 - return true; - } - if (_sum == 0x9493f8b2) { // EIP173 - return true; - } - if (_sum == 0x37a47be4) { // OwnedAccepter - return true; - } - return false; - } -} diff --git a/solidity/DemurrageTokenSingleNocap.sol b/solidity/DemurrageTokenSingleNocap.sol @@ -1,23 +1,24 @@ pragma solidity >= 0.8.0; + +import "aux/ABDKMath64x64.sol"; + // SPDX-License-Identifier: GPL-3.0-or-later -contract DemurrageTokenSingleCap { - - // Redistribution bit field, with associated shifts and masks - // (Uses sub-byte boundaries) - bytes32[] public redistributions; // uint51(unused) | uint64(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period) - uint8 constant shiftRedistributionPeriod = 0; - uint256 constant maskRedistributionPeriod = 0x00000000000000000000000000000000000000000000000000000000ffffffff; // (1 << 32) - 1 - uint8 constant shiftRedistributionValue = 32; - uint256 constant maskRedistributionValue = 0x00000000000000000000000000000000000000ffffffffffffffffff00000000; // ((1 << 72) - 1) << 32 - uint8 constant shiftRedistributionDemurrage = 104; - uint256 constant maskRedistributionDemurrage = 0x0000000000ffffffffffffffffffffffffffff00000000000000000000000000; // ((1 << 36) - 1) << 140 +contract DemurrageTokenSingleNocap { + + struct redistributionItem { + uint32 period; + uint72 value; + uint64 demurrage; + } + redistributionItem[] public redistributions; // uint51(unused) | uint64(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period) // Account balances mapping (address => uint256) account; // Cached demurrage amount, ppm with 38 digit resolution - uint128 public demurrageAmount; + //uint128 public demurrageAmount; + int128 public demurrageAmount; // Cached demurrage timestamp; the timestamp for which demurrageAmount was last calculated uint256 public demurrageTimestamp; @@ -34,7 +35,7 @@ contract DemurrageTokenSingleCap { string public symbol; // Implements ERC20 - uint256 public decimals; + uint256 public immutable decimals; // Implements ERC20 //uint256 public totalSupply; @@ -51,13 +52,13 @@ contract DemurrageTokenSingleCap { // 128 bit resolution of the demurrage divisor // (this constant x 1000000 is contained within 128 bits) - uint256 constant nanoDivider = 100000000000000000000000000; // now nanodivider, 6 zeros less + //uint256 constant nanoDivider = 100000000000000000000000000; // now nanodivider, 6 zeros less // remaining decimal positions of nanoDivider to reach 38, equals precision in growth and decay - uint256 constant growthResolutionFactor = 1000000000000; + //uint256 constant growthResolutionFactor = 1000000000000; // demurrage decimal width; 38 places - uint256 public immutable resolutionFactor = nanoDivider * growthResolutionFactor; + //uint256 public immutable resolutionFactor = nanoDivider * growthResolutionFactor; // Timestamp of start of periods (time which contract constructor was called) uint256 public immutable periodStart; @@ -66,7 +67,9 @@ contract DemurrageTokenSingleCap { uint256 public immutable periodDuration; // Demurrage in ppm per minute - uint256 public immutable taxLevel; + //uint256 public immutable decayLevel; + // 64x64 + int128 public immutable decayLevel; // Addresses allowed to mint new tokens mapping (address => bool) minter; @@ -77,6 +80,13 @@ contract DemurrageTokenSingleCap { // Address to send unallocated redistribution tokens address public sinkAddress; + // timestamp when token contract expires + uint256 public expires; + bool expired; + + // supply xap + uint256 public maxSupply; + // Implements ERC20 event Transfer(address indexed _from, address indexed _to, uint256 _value); @@ -87,7 +97,7 @@ contract DemurrageTokenSingleCap { event Mint(address indexed _minter, address indexed _beneficiary, uint256 _value); // New demurrage cache milestone calculated - event Decayed(uint256 indexed _period, uint256 indexed _periodCount, uint256 indexed _oldAmount, uint256 _newAmount); + event Decayed(uint256 indexed _period, uint256 indexed _periodCount, int128 indexed _oldAmount, int128 _newAmount); // When a new period threshold has been crossed event Period(uint256 _period); @@ -96,7 +106,8 @@ contract DemurrageTokenSingleCap { event Redistribution(address indexed _account, uint256 indexed _period, uint256 _value); // Temporary event used in development, will be removed on prod - event Debug(bytes32 _foo); + //event Debug(bytes32 _foo); + event Debug(int128 indexed _foo, uint256 indexed _bar); // Emitted when tokens are burned event Burn(address indexed _burner, uint256 _value); @@ -104,10 +115,28 @@ contract DemurrageTokenSingleCap { // EIP173 event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); // EIP173 - constructor(string memory _name, string memory _symbol, uint8 _decimals, uint128 _taxLevelMinute, uint256 _periodMinutes, address _defaultSinkAddress) public { + event Expired(uint256 _timestamp); + + event Cap(uint256 indexed _oldCap, uint256 _newCap); + + // Implements Sealer + uint256 public sealState; + uint8 constant WRITER_STATE = 1; + uint8 constant SINK_STATE = 2; + uint8 constant EXPIRY_STATE = 4; + uint8 constant CAP_STATE = 8; + uint256 constant public maxSealState = 15; + event SealStateChange(bool indexed _final, uint256 _sealState); + + + constructor(string memory _name, string memory _symbol, uint8 _decimals, int128 _decayLevel, uint256 _periodMinutes, address _defaultSinkAddress) { + require(_decayLevel < (1 << 64)); + redistributionItem memory initialRedistribution; + + //require(ABDKMath64x64.toUInt(_decayLevel) == 0); + // ACL setup owner = msg.sender; - minter[owner] = true; // ERC20 setup name = _name; @@ -118,31 +147,91 @@ contract DemurrageTokenSingleCap { demurrageTimestamp = block.timestamp; periodStart = demurrageTimestamp; periodDuration = _periodMinutes * 60; - demurrageAmount = uint128(nanoDivider) * 100; - taxLevel = _taxLevelMinute; // Represents 38 decimal places - bytes32 initialRedistribution = toRedistribution(0, demurrageAmount, 0, 1); + demurrageAmount = ABDKMath64x64.fromUInt(1); + + decayLevel = ABDKMath64x64.ln(_decayLevel); + initialRedistribution = toRedistribution(0, demurrageAmount, 0, 1); redistributions.push(initialRedistribution); // Misc settings sinkAddress = _defaultSinkAddress; } + // Implements Sealer + function seal(uint256 _state) public returns(uint256) { + require(_state < 16, 'ERR_INVALID_STATE'); + require(_state & sealState == 0, 'ERR_ALREADY_LOCKED'); + sealState |= _state; + emit SealStateChange(sealState == maxSealState, sealState); + return uint256(sealState); + } + + // Implements Sealer + function isSealed(uint256 _state) public view returns(bool) { + require(_state < maxSealState); + if (_state == 0) { + return sealState == maxSealState; + } + return _state & sealState == _state; + } + + function setExpirePeriod(uint256 _expirePeriod) public { + uint256 r; + + require(!isSealed(EXPIRY_STATE)); + require(!expired); + require(msg.sender == owner); + r = periodStart + (_expirePeriod * periodDuration); + require(r > expires); + expires = r; + } + + function setMaxSupply(uint256 _cap) public { + require(!isSealed(CAP_STATE)); + require(msg.sender == owner); + require(_cap > totalSupply()); + emit Cap(maxSupply, _cap); + maxSupply = _cap; + } // Change sink address for redistribution function setSinkAddress(address _sinkAddress) public { + require(!isSealed(SINK_STATE)); require(msg.sender == owner); sinkAddress = _sinkAddress; } + // Expire the contract if expire is set and we have gone over the threshold. + // Finalizes demurrage up to the timestamp of the expiry. + // The first approve, transfer or transferFrom call that hits the ex == 2 will get the tx mined. but without the actual effect. Otherwise we would have to wait until an external egent called applyExpiry to get the correct final balance. + function applyExpiry() public returns(uint8) { + if (expired) { + return 1; + } + if (expires == 0) { + return 0; + } + if (block.timestamp >= expires) { + applyDemurrageLimited(expires - demurrageTimestamp / 60); + expired = true; + emit Expired(block.timestamp); + changePeriod(); + return 2; + } + return 0; + } + // Given address will be allowed to call the mintTo() function - function addMinter(address _minter) public returns (bool) { + function addWriter(address _minter) public returns (bool) { + require(!isSealed(WRITER_STATE)); require(msg.sender == owner); minter[_minter] = true; return true; } // Given address will no longer be allowed to call the mintTo() function - function removeMinter(address _minter) public returns (bool) { + function deleteWriter(address _minter) public returns (bool) { + require(!isSealed(WRITER_STATE)); require(msg.sender == owner || _minter == msg.sender); minter[_minter] = false; return true; @@ -150,20 +239,19 @@ contract DemurrageTokenSingleCap { /// Implements ERC20 function balanceOf(address _account) public view returns (uint256) { - uint256 baseBalance; - uint256 currentDemurragedAmount; + int128 baseBalance; + int128 currentDemurragedAmount; uint256 periodCount; - baseBalance = baseBalanceOf(_account); + baseBalance = ABDKMath64x64.fromUInt(baseBalanceOf(_account)); periodCount = getMinutesDelta(demurrageTimestamp); - currentDemurragedAmount = uint128(decayBy(demurrageAmount * 10000000000, periodCount)); - - return (baseBalance * currentDemurragedAmount) / (nanoDivider * 1000000000000); + currentDemurragedAmount = ABDKMath64x64.mul(baseBalance, demurrageAmount); + return decayBy(ABDKMath64x64.toUInt(currentDemurragedAmount), periodCount); } - /// Balance unmodified by demurrage + // Balance unmodified by demurrage function baseBalanceOf(address _account) public view returns (uint256) { return account[_account]; } @@ -171,7 +259,6 @@ contract DemurrageTokenSingleCap { /// Increases base balance for a single account function increaseBaseBalance(address _account, uint256 _delta) private returns (bool) { uint256 oldBalance; - uint256 newBalance; uint256 workAccount; workAccount = uint256(account[_account]); @@ -188,7 +275,6 @@ contract DemurrageTokenSingleCap { /// Decreases base balance for a single account function decreaseBaseBalance(address _account, uint256 _delta) private returns (bool) { uint256 oldBalance; - uint256 newBalance; uint256 workAccount; workAccount = uint256(account[_account]); @@ -203,16 +289,31 @@ contract DemurrageTokenSingleCap { return true; } + // Send full balance of one account to another + function sweep(address _account) public returns (uint256) { + uint256 v; + + v = account[msg.sender]; + account[msg.sender] = 0; + account[_account] += v; + return v; + } + // Creates new tokens out of thin air, and allocates them to the given address // Triggers tax function mintTo(address _beneficiary, uint256 _amount) external returns (bool) { uint256 baseAmount; - require(minter[msg.sender], 'ERR_ACCESS'); + require(applyExpiry() == 0); + require(minter[msg.sender] || msg.sender == owner, 'ERR_ACCESS'); changePeriod(); - baseAmount = toBaseAmount(_amount); + if (maxSupply > 0) { + require(supply + _amount <= maxSupply); + } supply += _amount; + + baseAmount = toBaseAmount(_amount); increaseBaseBalance(_beneficiary, baseAmount); emit Mint(msg.sender, _beneficiary, _amount); saveRedistributionSupply(); @@ -220,29 +321,36 @@ contract DemurrageTokenSingleCap { } // Deserializes the redistribution word - // uint95(unused) | uint20(demurrageModifier) | uint36(participants) | uint72(value) | uint32(period) - function toRedistribution(uint256 _participants, uint256 _demurrageModifierPpm, uint256 _value, uint256 _period) public pure returns(bytes32) { - bytes32 redistribution; + function toRedistribution(uint256 _participants, int128 _demurrageModifier, uint256 _value, uint256 _period) public pure returns(redistributionItem memory) { + redistributionItem memory redistribution; - redistribution |= bytes32((_demurrageModifierPpm << shiftRedistributionDemurrage) & maskRedistributionDemurrage); - redistribution |= bytes32((_value << shiftRedistributionValue) & maskRedistributionValue); - redistribution |= bytes32(_period & maskRedistributionPeriod); + _participants; + redistribution.period = uint32(_period); + redistribution.value = uint72(_value); + redistribution.demurrage = uint64(uint128(_demurrageModifier) & 0xffffffffffffffff); return redistribution; + } // Serializes the demurrage period part of the redistribution word - function toRedistributionPeriod(bytes32 redistribution) public pure returns (uint256) { - return uint256(redistribution) & maskRedistributionPeriod; + function toRedistributionPeriod(redistributionItem memory _redistribution) public pure returns (uint256) { + return uint256(_redistribution.period); } // Serializes the supply part of the redistribution word - function toRedistributionSupply(bytes32 redistribution) public pure returns (uint256) { - return (uint256(redistribution) & maskRedistributionValue) >> shiftRedistributionValue; + function toRedistributionSupply(redistributionItem memory _redistribution) public pure returns (uint256) { + return uint256(_redistribution.value); } // Serializes the number of participants part of the redistribution word - function toRedistributionDemurrageModifier(bytes32 redistribution) public pure returns (uint256) { - return (uint256(redistribution) & maskRedistributionDemurrage) >> shiftRedistributionDemurrage; + function toRedistributionDemurrageModifier(redistributionItem memory _redistribution) public pure returns (int128) { + int128 r; + + r = int128(int64(_redistribution.demurrage) & int128(0x0000000000000000ffffffffffffffff)); + if (r == 0) { + r = ABDKMath64x64.fromUInt(1); + } + return r; } // Client accessor to the redistributions array length @@ -252,15 +360,14 @@ contract DemurrageTokenSingleCap { // Save the current total supply amount to the current redistribution period function saveRedistributionSupply() private returns (bool) { - uint256 currentRedistribution; + redistributionItem memory currentRedistribution; uint256 grownSupply; grownSupply = totalSupply(); - currentRedistribution = uint256(redistributions[redistributions.length-1]); - currentRedistribution &= (~maskRedistributionValue); - currentRedistribution |= (grownSupply << shiftRedistributionValue); + currentRedistribution = redistributions[redistributions.length-1]; + currentRedistribution.value = uint72(grownSupply); - redistributions[redistributions.length-1] = bytes32(currentRedistribution); + redistributions[redistributions.length-1] = currentRedistribution; return true; } @@ -270,28 +377,30 @@ contract DemurrageTokenSingleCap { } // Retrieve next redistribution if the period threshold has been crossed - function checkPeriod() private view returns (bytes32) { - bytes32 lastRedistribution; + function checkPeriod() private view returns (redistributionItem memory) { + redistributionItem memory lastRedistribution; + redistributionItem memory emptyRedistribution; uint256 currentPeriod; lastRedistribution = redistributions[lastPeriod]; currentPeriod = this.actualPeriod(); if (currentPeriod <= toRedistributionPeriod(lastRedistribution)) { - return bytes32(0x00); + return emptyRedistribution; } return lastRedistribution; } - function getDistribution(uint256 _supply, uint256 _demurrageAmount) public view returns (uint256) { - uint256 difference; + function getDistribution(uint256 _supply, int128 _demurrageAmount) public pure returns (uint256) { + int128 difference; - difference = _supply * (resolutionFactor - (_demurrageAmount * 10000000000)); - return difference / resolutionFactor; + difference = ABDKMath64x64.mul(ABDKMath64x64.fromUInt(_supply), ABDKMath64x64.sub(ABDKMath64x64.fromUInt(1), _demurrageAmount)); + return _supply - ABDKMath64x64.toUInt(difference); + } - function getDistributionFromRedistribution(bytes32 _redistribution) public returns (uint256) { + function getDistributionFromRedistribution(redistributionItem memory _redistribution) public pure returns (uint256) { uint256 redistributionSupply; - uint256 redistributionDemurrage; + int128 redistributionDemurrage; redistributionSupply = toRedistributionSupply(_redistribution); redistributionDemurrage = toRedistributionDemurrageModifier(_redistribution); @@ -299,35 +408,77 @@ contract DemurrageTokenSingleCap { } // Returns the amount sent to the sink address - function applyDefaultRedistribution(bytes32 _redistribution) private returns (uint256) { + function applyDefaultRedistribution(redistributionItem memory _redistribution) private returns (uint256) { uint256 unit; uint256 baseUnit; - unit = getDistributionFromRedistribution(_redistribution); + unit = totalSupply() - getDistributionFromRedistribution(_redistribution); baseUnit = toBaseAmount(unit) - totalSink; increaseBaseBalance(sinkAddress, baseUnit); + emit Redistribution(sinkAddress, _redistribution.period, unit); lastPeriod += 1; totalSink += baseUnit; return unit; } + // Recalculate the demurrage modifier for the new period + // Note that the supply for the consecutive period will be taken at the time of code execution, and thus not necessarily at the time when the redistribution period threshold was crossed. + function changePeriod() public returns (bool) { + redistributionItem memory currentRedistribution; + redistributionItem memory nextRedistribution; + redistributionItem memory lastRedistribution; + uint256 currentPeriod; + int128 lastDemurrageAmount; + int128 nextRedistributionDemurrage; + uint256 demurrageCounts; + uint256 nextPeriod; + + applyDemurrage(); + currentRedistribution = checkPeriod(); + if (isEmptyRedistribution(currentRedistribution)) { + return false; + } + + // calculate the decay from previous redistributino + lastRedistribution = redistributions[lastPeriod]; + currentPeriod = toRedistributionPeriod(currentRedistribution); + nextPeriod = currentPeriod + 1; + lastDemurrageAmount = toRedistributionDemurrageModifier(lastRedistribution); + demurrageCounts = (periodDuration * currentPeriod) / 60; + // TODO refactor decayby to take int128 then DRY with it + nextRedistributionDemurrage = ABDKMath64x64.exp(ABDKMath64x64.mul(decayLevel, ABDKMath64x64.fromUInt(demurrageCounts))); + nextRedistribution = toRedistribution(0, nextRedistributionDemurrage, totalSupply(), nextPeriod); + redistributions.push(nextRedistribution); + + applyDefaultRedistribution(nextRedistribution); + emit Period(nextPeriod); + return true; + } + // Calculate the time delta in whole minutes passed between given timestamp and current timestamp function getMinutesDelta(uint256 _lastTimestamp) public view returns (uint256) { return (block.timestamp - _lastTimestamp) / 60; } // Calculate and cache the demurrage value corresponding to the (period of the) time of the method call - function applyDemurrage() public returns (bool) { + function applyDemurrage() public returns (uint256) { return applyDemurrageLimited(0); } - function applyDemurrageLimited(uint256 _rounds) public returns (bool) { + // returns true if expired + function applyDemurrageLimited(uint256 _rounds) public returns (uint256) { + int128 v; uint256 periodCount; - uint256 lastDemurrageAmount; + int128 periodPoint; + int128 lastDemurrageAmount; + + if (expired) { + return 0; + } periodCount = getMinutesDelta(demurrageTimestamp); if (periodCount == 0) { - return false; + return 0; } lastDemurrageAmount = demurrageAmount; @@ -337,11 +488,14 @@ contract DemurrageTokenSingleCap { periodCount = _rounds; } - demurrageAmount = uint128(decayBy(lastDemurrageAmount, periodCount)); - //demurragePeriod = epochPeriodCount; + periodPoint = ABDKMath64x64.fromUInt(periodCount); + v = ABDKMath64x64.mul(decayLevel, periodPoint); + v = ABDKMath64x64.exp(v); + demurrageAmount = ABDKMath64x64.mul(demurrageAmount, v); + demurrageTimestamp = demurrageTimestamp + (periodCount * 60); emit Decayed(demurrageTimestamp, periodCount, lastDemurrageAmount, demurrageAmount); - return true; + return periodCount; } // Return timestamp of start of period threshold @@ -354,77 +508,54 @@ contract DemurrageTokenSingleCap { return (block.timestamp - _target) / 60; } - // Recalculate the demurrage modifier for the new period - // Note that the supply for the consecutive period will be taken at the time of code execution, and thus not necessarily at the time when the redistribution period threshold was crossed. - function changePeriod() public returns (bool) { - bytes32 currentRedistribution; - bytes32 nextRedistribution; - uint256 currentPeriod; - uint256 lastDemurrageAmount; - bytes32 lastRedistribution; - uint256 nextRedistributionDemurrage; - uint256 demurrageCounts; - uint256 nextPeriod; - - applyDemurrage(); - currentRedistribution = checkPeriod(); - if (currentRedistribution == bytes32(0x00)) { + function isEmptyRedistribution(redistributionItem memory _redistribution) public pure returns(bool) { + if (_redistribution.period > 0) { + return false; + } + if (_redistribution.value > 0) { + return false; + } + if (_redistribution.demurrage > 0) { return false; } - - // calculate the decay from previous redistributino - lastRedistribution = redistributions[lastPeriod]; - currentPeriod = toRedistributionPeriod(currentRedistribution); - nextPeriod = currentPeriod + 1; - lastDemurrageAmount = toRedistributionDemurrageModifier(lastRedistribution); - demurrageCounts = periodDuration / 60; - nextRedistributionDemurrage = decayBy(lastDemurrageAmount, demurrageCounts); - - nextRedistribution = toRedistribution(0, nextRedistributionDemurrage, totalSupply(), nextPeriod); - redistributions.push(nextRedistribution); - - applyDefaultRedistribution(nextRedistribution); - emit Period(nextPeriod); return true; } - // Reverse a value reduced by demurrage by the given period to its original value - function growBy(uint256 _value, uint256 _period) public view returns (uint256) { - uint256 valueFactor; - uint256 truncatedTaxLevel; - - valueFactor = growthResolutionFactor; - truncatedTaxLevel = taxLevel / nanoDivider; - - for (uint256 i = 0; i < _period; i++) { - valueFactor = valueFactor + ((valueFactor * truncatedTaxLevel) / growthResolutionFactor); - } - return (valueFactor * _value) / growthResolutionFactor; - } // Calculate a value reduced by demurrage by the given period - function decayBy(uint256 _value, uint256 _period) public view returns (uint256) { - uint256 valueFactor; - uint256 truncatedTaxLevel; - - valueFactor = growthResolutionFactor; - truncatedTaxLevel = taxLevel / nanoDivider; - - for (uint256 i = 0; i < _period; i++) { - valueFactor = valueFactor - ((valueFactor * truncatedTaxLevel) / growthResolutionFactor); - } - return (valueFactor * _value) / growthResolutionFactor; + function decayBy(uint256 _value, uint256 _period) public view returns (uint256) { + int128 valuePoint; + int128 periodPoint; + int128 v; + + valuePoint = ABDKMath64x64.fromUInt(_value); + periodPoint = ABDKMath64x64.fromUInt(_period); + + v = ABDKMath64x64.mul(decayLevel, periodPoint); + v = ABDKMath64x64.exp(v); + v = ABDKMath64x64.mul(valuePoint, v); + return ABDKMath64x64.toUInt(v); } + // Inflates the given amount according to the current demurrage modifier function toBaseAmount(uint256 _value) public view returns (uint256) { - return (_value * resolutionFactor) / (demurrageAmount * 10000000000); + int128 r; + r = ABDKMath64x64.div(ABDKMath64x64.fromUInt(_value), demurrageAmount); + return ABDKMath64x64.toUInt(r); } // Implements ERC20, triggers tax and/or redistribution function approve(address _spender, uint256 _value) public returns (bool) { uint256 baseValue; + uint8 ex; + ex = applyExpiry(); + if (ex == 2) { + return false; + } else if (ex > 0) { + revert('EXPIRED'); + } if (allowance[msg.sender][_spender] > 0) { require(_value == 0, 'ZERO_FIRST'); } @@ -468,7 +599,14 @@ contract DemurrageTokenSingleCap { function transfer(address _to, uint256 _value) public returns (bool) { uint256 baseValue; bool result; + uint8 ex; + ex = applyExpiry(); + if (ex == 2) { + return false; + } else if (ex > 0) { + revert('EXPIRED'); + } changePeriod(); baseValue = toBaseAmount(_value); @@ -481,7 +619,14 @@ contract DemurrageTokenSingleCap { function transferFrom(address _from, address _to, uint256 _value) public returns (bool) { uint256 baseValue; bool result; + uint8 ex; + ex = applyExpiry(); + if (ex == 2) { + return false; + } else if (ex > 0) { + revert('EXPIRED'); + } changePeriod(); baseValue = toBaseAmount(_value); @@ -496,8 +641,6 @@ contract DemurrageTokenSingleCap { // ERC20 transfer backend for transfer, transferFrom function transferBase(address _from, address _to, uint256 _value) private returns (bool) { - uint256 period; - decreaseBaseBalance(_from, _value); increaseBaseBalance(_to, _value); @@ -508,6 +651,7 @@ contract DemurrageTokenSingleCap { function transferOwnership(address _newOwner) public returns (bool) { require(msg.sender == owner); newOwner = _newOwner; + return true; } // Implements OwnedAccepter @@ -519,19 +663,22 @@ contract DemurrageTokenSingleCap { owner = newOwner; newOwner = address(0); emit OwnershipTransferred(oldOwner, owner); + return true; } // Explicitly and irretrievably burn tokens // Only token minters can burn tokens - function burn(uint256 _value) public { - require(minter[msg.sender]); + function burn(uint256 _value) public returns (bool) { + require(applyExpiry() == 0); + require(minter[msg.sender] || msg.sender == owner, 'ERR_ACCESS'); require(_value <= account[msg.sender]); uint256 _delta = toBaseAmount(_value); - applyDemurrage(); + //applyDemurrage(); decreaseBaseBalance(msg.sender, _delta); burned += _value; emit Burn(msg.sender, _value); + return true; } // Implements ERC20 @@ -544,6 +691,12 @@ contract DemurrageTokenSingleCap { return burned; } + // Return total number of tokens ever minted + function totalMinted() public view returns (uint256) { + return supply; + } + + // Implements EIP165 function supportsInterface(bytes4 _sum) public pure returns (bool) { if (_sum == 0xc6bb4b70) { // ERC20 @@ -558,9 +711,6 @@ contract DemurrageTokenSingleCap { if (_sum == 0x9493f8b2) { // EIP173 return true; } - if (_sum == 0x37a47be4) { // OwnedAccepter - return true; - } return false; } } diff --git a/solidity/Makefile b/solidity/Makefile @@ -1,30 +1,14 @@ SOLC = /usr/bin/solc -all: multi single - -multi_nocap: - $(SOLC) DemurrageTokenMultiNocap.sol --abi --evm-version byzantium | awk 'NR>3' > DemurrageTokenMultiNocap.json - $(SOLC) DemurrageTokenMultiNocap.sol --bin --evm-version byzantium | awk 'NR>3' > DemurrageTokenMultiNocap.bin - truncate -s -1 DemurrageTokenMultiNocap.bin - -multi_cap: - $(SOLC) DemurrageTokenMultiCap.sol --abi --evm-version byzantium | awk 'NR>3' > DemurrageTokenMultiCap.json - $(SOLC) DemurrageTokenMultiCap.sol --bin --evm-version byzantium | awk 'NR>3' > DemurrageTokenMultiCap.bin - truncate -s -1 DemurrageTokenMultiCap.bin - -multi: multi_nocap multi_cap +all: single_nocap single_nocap: - $(SOLC) DemurrageTokenSingleNocap.sol --abi --evm-version byzantium | awk 'NR>3' > DemurrageTokenSingleNocap.json - $(SOLC) DemurrageTokenSingleNocap.sol --bin --evm-version byzantium | awk 'NR>3' > DemurrageTokenSingleNocap.bin + $(SOLC) DemurrageTokenSingleNocap.sol --abi --evm-version byzantium | awk 'NR==4' > DemurrageTokenSingleNocap.json + $(SOLC) DemurrageTokenSingleNocap.sol --bin --evm-version byzantium | awk 'NR==4' > DemurrageTokenSingleNocap.bin + $(SOLC) DemurrageTokenSingleNocap.sol --metadata --evm-version byzantium | awk 'NR==4' > DemurrageTokenSingleNocap.metadata.json truncate -s -1 DemurrageTokenSingleNocap.bin -single_cap: - $(SOLC) DemurrageTokenSingleCap.sol --abi --evm-version byzantium | awk 'NR>3' > DemurrageTokenSingleCap.json - $(SOLC) DemurrageTokenSingleCap.sol --bin --evm-version byzantium | awk 'NR>3' > DemurrageTokenSingleCap.bin - truncate -s -1 DemurrageTokenSingleCap.bin - -single: single_nocap single_cap +single: single_nocap test: all python ../python/tests/test_basic.py @@ -33,10 +17,7 @@ test: all python ../python/tests/test_pure.py install: all - #cp -v DemurrageToken*.{json,bin} ../python/erc20_demurrage_token/data/ - cp -v DemurrageTokenSingleNocap.{json,bin} ../python/erc20_demurrage_token/data/ - -install-broken: all - cp -v DemurrageToken*.{json,bin} ../python/erc20_demurrage_token/data/ + cp -v DemurrageToken*.json ../python/erc20_demurrage_token/data/ + cp -v DemurrageToken*.bin ../python/erc20_demurrage_token/data/ .PHONY: test install diff --git a/solidity/aux/ABDKMath64x64.sol 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 <mikhail.vladimirov@gmail.com> + */ +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); + } + } + } +} diff --git a/solidity/aux/LICENSE.md b/solidity/aux/LICENSE.md @@ -0,0 +1,28 @@ +Copyright (c) 2019, [ABDK Consulting](https://abdk.consulting/) + +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. +3. All advertising materials mentioning features or use of this software must + display the following acknowledgement: This product includes software + developed by ABDK Consulting. +4. Neither the name of ABDK Consulting nor the names of its contributors may be + used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY ABDK CONSULTING ''AS IS'' AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT +SHALL ABDK CONSULTING BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT +OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING +IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY +OF SUCH DAMAGE.