Skip to content

Commit

Permalink
fixup! contracts: migration simulation: Test migration simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
bingen committed Mar 23, 2021
1 parent 7b233df commit 12140d1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 30 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,8 @@ brownie pm install OpenZeppelin/openzeppelin-contracts@3.3.0

Run, from `packages/contracts/`:
```
brownie test -s
brownie test tests/simulation_test.py -s
brownie test tests/migration_test.py -s
```

### OpenEthereum
Expand All @@ -644,7 +645,8 @@ yarn start-dev-chain:openethereum

Then, again from `packages/contracts/`, run it with:
```
brownie test -s --network openethereum
brownie test tests/simulation_test.py -s --network openethereum
brownie test tests/migration_test.py -s --network openethereum
```

To stop the Openethereum node, you can do it with:
Expand Down
14 changes: 8 additions & 6 deletions packages/contracts/tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from brownie import Wei
import pytest

from helpers import *
from brownie import *
from accounts import *

ZERO_ADDRESS = '0x' + '0'.zfill(40)
MAX_BYTES_32 = '0x' + 'F' * 64
MAX_FEE = Wei(1e18)

class Contracts: pass

def floatToWei(amount):
return Wei(amount * 1e18)

Expand Down Expand Up @@ -127,8 +130,7 @@ def setAddresses(contracts):
{ 'from': accounts[0] }
)

@pytest.fixture
def contracts():
def deploy_contracts():
contracts = Contracts()

contracts.priceFeedTestnet = PriceFeedTestnet.deploy({ 'from': accounts[0] })
Expand All @@ -155,8 +157,8 @@ def contracts():
contracts.communityIssuance.address,
contracts.lqtyStaking.address,
contracts.lockupContractFactory.address,
accounts[0], # bountyAddress
accounts[0], # lpRewardsAddress
accounts[1], # bountyAddress
accounts[1], # lpRewardsAddress
{ 'from': accounts[0] }
)

Expand Down
29 changes: 20 additions & 9 deletions packages/contracts/tests/migration_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,37 @@
ETHER_PRICE = Wei(1000 * 1e18)

def flesh_out_system(accounts, contracts):
LUSD_GAS_COMPENSATION = contracts.troveManager.LUSD_GAS_COMPENSATION()

MIN_NET_DEBT = contracts.troveManager.MIN_NET_DEBT()
MIN_LUSD_AMOUNT = contracts.troveManager.getBorrowingFeeWithDecay(MIN_NET_DEBT)
borrowing_rate = contracts.troveManager.getBorrowingRateWithDecay()
MIN_LUSD_AMOUNT = Wei(MIN_NET_DEBT * Wei(1e18) / (Wei(1e18) + borrowing_rate)) + Wei(1e18)

# Account 0 will always be the last one, as one has to remain in the system
collateral = MIN_LUSD_AMOUNT * 3 / ETHER_PRICE # ICR: 300%
collateral = Wei(MIN_NET_DEBT * 6 * 1e18 / ETHER_PRICE) # ICR: ~600% (missing gas reserve)
contracts.borrowerOperations.openTrove(MAX_FEE, MIN_LUSD_AMOUNT, ZERO_ADDRESS, ZERO_ADDRESS,
{ 'from': accounts[0], 'value': collateral })

# send 1 lqty token to account 1 (deployer cannot stake during 1st year),
# so it can stake and earn borrowing fees
contracts.lqtyToken.transfer(accounts[1], 1, { 'from': accounts[0] })
#contracts.lqtyToken.transfer(accounts[1], 1, { 'from': accounts[0] })
# account 1 stakes to earn borrowing fees
contracts.lqtyStaking.stake(1, { 'from': accounts[1] })

# open trove in ICR descending order
ICR = 500
for i in range(1, 1000):
lusdAmount = MIN_LUSD_AMOUNT + floatToWei(1000000 * random.random()) # lusd ranging from min to 1M
ICR = ICR - 0.38 * random.random() # last trove ICR should be at ~ 500 - 999*0.38 = 120.38
coll = lusdAmount * floatToWei(ICR / 100) / Wei(1e18) / ETHER_PRICE
contracts.borrowerOperations.openTrove(MAX_FEE, lusdAmount, accounts[i-1], ZERO_ADDRESS,
lusd_amount = MIN_LUSD_AMOUNT + floatToWei(1000000 * random.random()) # lusd ranging from min to 1M
borrowing_fee = contracts.troveManager.getBorrowingFeeWithDecay(lusd_amount)
ICR = ICR - 0.76 * random.random() # last trove ICR should be at ~ 500 - 999*0.76*0.5 = 120.38
coll = Wei((lusd_amount + borrowing_fee + LUSD_GAS_COMPENSATION) * floatToWei(ICR / 100) / ETHER_PRICE)
"""
print(f"i: {i}")
print(f"account: {accounts[i]}")
print(f"ICR: {ICR}")
print(f"coll: {coll}")
print(f"debt: {lusd_amount}")
"""
contracts.borrowerOperations.openTrove(MAX_FEE, lusd_amount, accounts[i-1], ZERO_ADDRESS,
{ 'from': accounts[i], 'value': coll })

def get_lusd_to_repay(accounts, contracts, account, debt):
Expand All @@ -44,7 +55,7 @@ def get_lusd_to_repay(accounts, contracts, account, debt):
contracts.lusdToken.transfer(account, pending, { 'from': accounts[1] })

def migrate_trove(accounts, contracts1, contracts2, i):
[debt, coll] = contracts.troveManager.getEntireDebtAndColl(account)[0]
[debt, coll] = contracts1.troveManager.getEntireDebtAndColl(account)[0]
get_lusd_to_repay(accounts, contracts1, accounts[i], debt)
# open trove in old system
contracts1.borrowerOperations.closeTrove({ 'from': accounts[i] })
Expand Down
28 changes: 18 additions & 10 deletions packages/contracts/tests/migration_test.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,50 @@
import pytest

from helpers import *
from simulation_helpers import *
from migration_helpers import *

def setup():
contracts1 = contracts()
flesh_out_system(contracts1)
contracts1 = deploy_contracts()
contracts1.priceFeedTestnet.setPrice(ETHER_PRICE, { 'from': accounts[0] })

flesh_out_system(accounts, contracts1)

logGlobalState(contracts1)

contracts2 = contracts()
contracts2 = deploy_contracts()
contracts2.priceFeedTestnet.setPrice(ETHER_PRICE, { 'from': accounts[0] })

logGlobalState(contracts2)

return [contracts1, contracts2]

def test_run_migration_desc(add_accounts):
[contracts1, contracts2] = setup
[contracts1, contracts2] = setup()

# migrate troves in ICR descending order
run_migration_desc()
run_migration_desc(accounts, contracts1, contracts2)

logGlobalState(contracts1)
logGlobalState(contracts2)

'''
def test_run_migration_asc(add_accounts):
[contracts1, contracts2] = setup
[contracts1, contracts2] = setup()
# migrate troves in ICR ascending order
run_migration_asc()
run_migration_asc(accounts, contracts1, contracts2)
logGlobalState(contracts1)
logGlobalState(contracts2)
def test_run_migration_rand(add_accounts):
[contracts1, contracts2] = setup
[contracts1, contracts2] = setup()
# migrate troves in ICR random order
run_migration_rand()
run_migration_rand(accounts, contracts1, contracts2)
logGlobalState(contracts1)
logGlobalState(contracts2)
'''

# TODO: try with only redemptions
8 changes: 5 additions & 3 deletions packages/contracts/tests/simulation_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import pytest

from brownie import *
from accounts import *

from helpers import *
from simulation_helpers import *

class Contracts: pass

@pytest.fixture
def print_expectations():
ether_price_one_year = price_ether_initial * (1 + drift_ether)**8760
Expand All @@ -21,6 +19,10 @@ def print_expectations():
print("SD(tau) = ", rational_inattention_gamma_k**(0.5) * rational_inattention_gamma_theta * 100, "%")
print("\n")

@pytest.fixture
def contracts():
return deploy_contracts()

def _test_test(contracts):
print(len(accounts))
contracts.borrowerOperations.openTrove(Wei(1e18), Wei(2000e18), ZERO_ADDRESS, ZERO_ADDRESS,
Expand Down

0 comments on commit 12140d1

Please sign in to comment.