Skip to content

Commit

Permalink
Add fixtures for mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
bout3fiddy committed Feb 20, 2024
1 parent cde0bad commit 1a8ad55
Show file tree
Hide file tree
Showing 6 changed files with 569 additions and 0 deletions.
74 changes: 74 additions & 0 deletions contracts/mocks/ERC20.vy
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# pragma version 0.3.10

"""
@notice Mock ERC20 for testing
"""


event Transfer:
_from: indexed(address)
_to: indexed(address)
_value: uint256


event Approval:
_owner: indexed(address)
_spender: indexed(address)
_value: uint256


name: public(String[64])
symbol: public(String[32])
decimals: public(uint256)
balanceOf: public(HashMap[address, uint256])
allowances: HashMap[address, HashMap[address, uint256]]
totalSupply: public(uint256)

# asset type
asset_type: public(constant(uint8)) = 0


@external
def __init__(_name: String[64], _symbol: String[32], _decimals: uint256):
self.name = _name
self.symbol = _symbol
self.decimals = _decimals


@external
@view
def allowance(_owner: address, _spender: address) -> uint256:
return self.allowances[_owner][_spender]


@external
def transfer(_to: address, _value: uint256) -> bool:
self.balanceOf[msg.sender] -= _value
self.balanceOf[_to] += _value
log Transfer(msg.sender, _to, _value)
return True


@external
def transferFrom(_from: address, _to: address, _value: uint256) -> bool:
self.balanceOf[_from] -= _value
self.balanceOf[_to] += _value
self.allowances[_from][msg.sender] -= _value
log Transfer(_from, _to, _value)
return True


@external
def approve(_spender: address, _value: uint256) -> bool:
self.allowances[msg.sender][_spender] = _value
log Approval(msg.sender, _spender, _value)
return True


@external
def _mint_for_testing(_target: address, _value: uint256) -> bool:
self.totalSupply += _value
self.balanceOf[_target] += _value
log Transfer(empty(address), _target, _value)

return True
99 changes: 99 additions & 0 deletions contracts/mocks/ERC4626.vy
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# pragma version 0.3.10

"""
@notice Mock ERC20 with oracle
@dev This is for testing only, it is NOT safe for use
"""


event Transfer:
_from: indexed(address)
_to: indexed(address)
_value: uint256


event Approval:
_owner: indexed(address)
_spender: indexed(address)
_value: uint256


name: public(String[64])
symbol: public(String[32])
decimals: public(uint256)
balanceOf: public(HashMap[address, uint256])
allowances: HashMap[address, HashMap[address, uint256]]
totalSupply: public(uint256)

exchange_rate: public(uint256)

# asset type
asset_type: public(constant(uint8)) = 1


@external
def __init__(
_name: String[64],
_symbol: String[32],
_decimals: uint256,
_exchange_rate: uint256
):

self.name = _name
self.symbol = _symbol

assert _decimals == 18, "Decimals must be 18"
self.decimals = _decimals
self.exchange_rate = _exchange_rate


@external
@view
def allowance(_owner: address, _spender: address) -> uint256:
return self.allowances[_owner][_spender]


@external
def transfer(_to: address, _value: uint256) -> bool:
self.balanceOf[msg.sender] -= _value
self.balanceOf[_to] += _value
log Transfer(msg.sender, _to, _value)
return True


@external
def transferFrom(_from: address, _to: address, _value: uint256) -> bool:
self.balanceOf[_from] -= _value
self.balanceOf[_to] += _value
self.allowances[_from][msg.sender] -= _value
log Transfer(_from, _to, _value)
return True


@external
def approve(_spender: address, _value: uint256) -> bool:
self.allowances[msg.sender][_spender] = _value
log Approval(msg.sender, _spender, _value)
return True


@external
@view
def exchangeRate() -> uint256:
rate: uint256 = self.exchange_rate
return rate


@external
def set_exchange_rate(rate: uint256) -> bool:
self.exchange_rate = rate
return True


@external
def _mint_for_testing(_target: address, _value: uint256) -> bool:
self.totalSupply += _value
self.balanceOf[_target] += _value
log Transfer(empty(address), _target, _value)

return True
10 changes: 10 additions & 0 deletions tests/fixtures/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ def owner(accounts):
"AddressProvider", ADDRESS_PROVIDER
)
return address_provider.admin()


@pytest.fixture(scope="module")
def ng_fee_receiver():
return boa.env.generate_address()


@pytest.fixture(scope="module")
def ng_owner():
return boa.env.generate_address()
201 changes: 201 additions & 0 deletions tests/fixtures/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
import boa
import pytest

# ---- Stableswap NG ---- #


@pytest.fixture()
def stableswap_ng_deployer():
return boa.load_partial("contracts/amms/stableswapng/CurveStableSwapNG.vy")


@pytest.fixture()
def stableswap_ng_meta_deployer():
return boa.load_partial(
"contracts/amms/stableswapng/CurveStableSwapMetaNG.vy"
)


@pytest.fixture()
def stableswap_ng_implementation(stableswap_ng_deployer, deployer):
with boa.env.prank(deployer):
return stableswap_ng_deployer.deploy_as_blueprint()


@pytest.fixture()
def stableswap_ng_meta_implementation(stableswap_ng_meta_deployer, deployer):
with boa.env.prank(deployer):
return stableswap_ng_meta_deployer.deploy_as_blueprint()


@pytest.fixture()
def stableswap_ng_views_implementation(deployer):
with boa.env.prank(deployer):
return boa.load_partial(
"contracts/amms/stableswapng/CurveStableSwapNGViews.vy"
).deploy()


@pytest.fixture()
def stableswap_ng_math_implementation(deployer):
with boa.env.prank(deployer):
return boa.load_partial(
"contracts/amms/stableswapng/CurveStableSwapNGMath.vy"
).deploy()


@pytest.fixture()
def stableswap_ng_factory_empty(
deployer,
ng_fee_receiver,
ng_owner,
stableswap_ng_views_implementation,
stableswap_ng_math_implementation,
stableswap_ng_implementation,
stableswap_ng_meta_implementation,
):
with boa.env.prank(deployer):
factory = boa.load_partial(
"contracts/amms/stableswapng/CurveStableSwapFactoryNG.vy"
).deploy(ng_fee_receiver, ng_owner)

with boa.env.prank(ng_owner):
factory.set_views_implementation(
stableswap_ng_views_implementation.address
)
factory.set_math_implementation(
stableswap_ng_math_implementation.address
)
factory.set_pool_implementations(
0, stableswap_ng_implementation.address
)
factory.set_metapool_implementations(
0, stableswap_ng_meta_implementation.address
)

return factory


@pytest.fixture()
def stableswap_ng_factory(
stableswap_ng_factory_empty, stableswap_ng_pool, base_pool_coins, ng_owner
):
stableswap_ng_factory_empty.add_base_pool(
stableswap_ng_pool.address,
stableswap_ng_pool.address,
[0] * len(base_pool_coins),
len(base_pool_coins),
sender=ng_owner,
)
return stableswap_ng_factory_empty


# ---- Twocrypto NG ---- #


@pytest.fixture(scope="module")
def twocrypto_ng_math_contract(deployer):
with boa.env.prank(deployer):
return boa.load(
"contracts/amms/twocryptong/CurveCryptoMathOptimized2.vy"
)


@pytest.fixture(scope="module")
def twocrypto_ng_amm_deployer():
return boa.load_partial(
"contracts/amms/twocryptong/CurveTwocryptoOptimized.vy"
)


@pytest.fixture(scope="module")
def twocrypto_ng_amm_implementation(deployer, twocrypto_ng_amm_deployer):
with boa.env.prank(deployer):
return twocrypto_ng_amm_deployer.deploy_as_blueprint()


@pytest.fixture(scope="module")
def twocrypto_ng_views_contract(deployer):
with boa.env.prank(deployer):
return boa.load(
"contracts/amms/twocryptong/CurveCryptoViews2Optimized.vy"
)


@pytest.fixture(scope="module")
def twocrypto_ng_factory(
deployer,
ng_fee_receiver,
ng_owner,
twocrypto_ng_amm_implementation,
twocrypto_ng_math_contract,
twocrypto_ng_views_contract,
):
with boa.env.prank(deployer):
factory = boa.load(
"contracts/amms/twocryptong/CurveTwocryptoFactory.vy"
)
factory.initialise_ownership(ng_fee_receiver, ng_owner)

with boa.env.prank(ng_owner):
factory.set_pool_implementation(twocrypto_ng_amm_implementation, 0)
factory.set_views_implementation(twocrypto_ng_views_contract)
factory.set_math_implementation(twocrypto_ng_math_contract)

return factory


# ---- Tricrypto NG ---- #


@pytest.fixture(scope="module")
def tricrypto_ng_math_contract(deployer):
with boa.env.prank(deployer):
return boa.load(
"contracts/amms/tricryptong/CurveCryptoMathOptimized3.vy"
)


@pytest.fixture(scope="module")
def tricrypto_ng_amm_deployer():
return boa.load_partial(
"contracts/amms/tricryptong/CurveTricryptoOptimized.vy"
)


@pytest.fixture(scope="module")
def tricrypto_ng_amm_implementation(deployer, tricrypto_ng_amm_deployer):
with boa.env.prank(deployer):
return tricrypto_ng_amm_deployer.deploy_as_blueprint()


@pytest.fixture(scope="module")
def tricrypto_ng_views_contract(deployer):
with boa.env.prank(deployer):
return boa.load(
"contracts/amms/tricryptong/CurveCryptoViews3Optimized.vy"
)


@pytest.fixture(scope="module")
def tricrypto_ng_factory(
deployer,
ng_fee_receiver,
ng_owner,
tricrypto_ng_amm_implementation,
tricrypto_ng_math_contract,
tricrypto_ng_views_contract,
):
with boa.env.prank(deployer):
factory = boa.load(
"contracts/amms/tricryptong/CurveTricryptoFactory.vy",
ng_fee_receiver,
ng_owner,
)

with boa.env.prank(ng_owner):
factory.set_pool_implementation(tricrypto_ng_amm_implementation, 0)
factory.set_views_implementation(tricrypto_ng_views_contract)
factory.set_math_implementation(tricrypto_ng_math_contract)

return factory
Loading

0 comments on commit 1a8ad55

Please sign in to comment.