Skip to content

Commit

Permalink
fix: factory registry
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrovalido committed Oct 7, 2024
1 parent a4e9691 commit 9fb255d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 46 deletions.
25 changes: 10 additions & 15 deletions contracts/helpers/FactoryRegistry.vy
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@
# @author velodrome.finance
# @notice Sugar Factory Registry to keep track of leaf pool factories


MAX_FACTORIES: public(constant(uint256)) = 10

owner: public(address)
poolFactories: public(DynArray[address, MAX_FACTORIES]) # camelCase to have same signature as origin registry
pool_factory_count: public(uint256)
pool_factories: public(DynArray[address, MAX_FACTORIES])
pool_factory_exists: public(HashMap[address, bool])

@external
def __init__(_owner: address):
self.owner = _owner
self.pool_factory_count = 0

@internal
def _only_owner():
Expand All @@ -25,13 +22,10 @@ def _only_owner():
def approve(pool_factory: address):
self._only_owner()

# Check if already present
if self.pool_factory_exists[pool_factory]:
raise "Already exists"

# Add the poolFactory to the list
self.poolFactories[self.pool_factory_count] = pool_factory
self.pool_factory_count += 1
self.pool_factories.append(pool_factory)
self.pool_factory_exists[pool_factory] = True

@external
Expand All @@ -42,13 +36,9 @@ def unapprove(pool_factory: address):
raise "Not exists"

for i in range(MAX_FACTORIES):
if self.poolFactories[i] == pool_factory:
# Remove the pool_factory by shifting elements
for j in range(0, MAX_FACTORIES):
if j < i: continue
if j >= self.pool_factory_count: break
self.poolFactories[j] = self.poolFactories[j + 1]
self.pool_factory_count -= 1
if self.pool_factories[i] == pool_factory:
self.pool_factories[i] = self.pool_factories[len(self.pool_factories) - 1]
self.pool_factories.pop()
break

self.pool_factory_exists[pool_factory] = False
Expand All @@ -57,3 +47,8 @@ def unapprove(pool_factory: address):
@view
def factoriesToPoolFactory(poolFactory: address) -> (address, address):
return (empty(address), poolFactory)

@external
@view
def poolFactories() -> DynArray[address, MAX_FACTORIES]:
return self.pool_factories
2 changes: 1 addition & 1 deletion env.example
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ RELAY_SUGAR_ADDRESS_8453=0x8932B5FE23C07Df06533F8f09E43e7cca6a24143

#MODE
VOTER_34443=0x0000000000000000000000000000000000000000
REGISTRY_34443=0xeFFa2e499309db6FEb3868B52AB0889b0D7825ec
REGISTRY_34443=0x6B290762F9F9155637F0Bb6B7A5B1cEb394cceD8
CONVERTOR_34443=0x1111111111111111111111111111111111111111
SLIPSTREAM_HELPER_34443=0x0000000000000000000000000000000000000000
ALM_FACTORY_34443=0x0000000000000000000000000000000000000000
Expand Down
32 changes: 2 additions & 30 deletions tests/test_factory_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


@pytest.fixture
def factory_registry(FactoryRegistry, accounts):
def factory_registry(FactoryRegistry):
# Deploy the contract using the first test account as the owner
yield FactoryRegistry.at(os.getenv('REGISTRY_34443'))

Expand All @@ -15,35 +15,7 @@ def test_initial_state(factory_registry):
"0xd42C7914cF8dc24a1075E29C283C581bd1b0d3D3"


def test_approve(factory_registry, accounts):
owner = factory_registry.owner()
pool_factory = "0x1111111111111111111111111111111111111111"
pool_factory_count = factory_registry.pool_factory_count()

# Approve a new pool factory
factory_registry.approve(pool_factory, {'from': owner})
assert factory_registry.pool_factory_count() == pool_factory_count + 1
assert factory_registry.poolFactories(0) == pool_factory
assert factory_registry.pool_factory_exists(pool_factory)


def test_unapprove(factory_registry, accounts):
owner = factory_registry.owner()
pool_factory = "0x1111111111111111111111111111111111111111"
pool_factory_count = factory_registry.pool_factory_count()

# Approve a pool factory to set up the state for unapprove
factory_registry.approve(pool_factory, {'from': owner})
assert factory_registry.pool_factory_count() == pool_factory_count + 1
assert factory_registry.pool_factory_exists(pool_factory)

# Unapprove the pool factory
factory_registry.unapprove(pool_factory, {'from': owner})
assert factory_registry.pool_factory_count() == pool_factory_count
assert not factory_registry.pool_factory_exists(pool_factory)


def test_factories_to_pool_factory(factory_registry):
pool_factory = "0x1111111111111111111111111111111111111111"
pool_factory: address = "0x1111111111111111111111111111111111111111"
result = factory_registry.factoriesToPoolFactory(pool_factory)
assert result == [ADDRESS_ZERO, pool_factory]

0 comments on commit 9fb255d

Please sign in to comment.