Skip to content

Commit

Permalink
Merge pull request #23 from etherisc/feature/allow-multiple-products-…
Browse files Browse the repository at this point in the history
…on-same-riskpool

Allow to create product on same riskpool only when token match
  • Loading branch information
doerfli committed Aug 12, 2022
2 parents 5994ad2 + eb91f6a commit 9e7827c
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 9 deletions.
13 changes: 9 additions & 4 deletions contracts/modules/TreasuryModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "./PoolController.sol";
import "../shared/CoreController.sol";

import "@etherisc/gif-interface/contracts/components/IComponent.sol";
import "@etherisc/gif-interface/contracts/components/IProduct.sol";
import "@etherisc/gif-interface/contracts/modules/IPolicy.sol";
import "@etherisc/gif-interface/contracts/modules/ITreasury.sol";

Expand Down Expand Up @@ -72,8 +73,12 @@ contract TreasuryModule is
require(address(_componentToken[productId]) == address(0), "ERROR:TRS-012:PRODUCT_TOKEN_ALREADY_SET");

uint256 riskpoolId = _pool.getRiskPoolForProduct(productId);
require(address(_componentToken[riskpoolId]) == address(0), "ERROR:TRS-013:RISKPOOL_TOKEN_ALREADY_SET");

// require if riskpool token is already set and product token does match riskpool token
require(address(_componentToken[riskpoolId]) == address(0)
|| address(_componentToken[riskpoolId]) == address(IProduct(address(component)).getToken()),
"ERROR:TRS-014:TOKEN_ADDRESS_NOT_MACHING");

_componentToken[productId] = IERC20(erc20Address);
_componentToken[riskpoolId] = IERC20(erc20Address);

Expand All @@ -84,7 +89,7 @@ contract TreasuryModule is
external override
onlyInstanceOperator
{
require(instanceWalletAddress != address(0), "ERROR:TRS-014:WALLET_ADDRESS_ZERO");
require(instanceWalletAddress != address(0), "ERROR:TRS-015:WALLET_ADDRESS_ZERO");
_instanceWalletAddress = instanceWalletAddress;

emit LogTreasuryInstanceWalletSet (instanceWalletAddress);
Expand All @@ -95,8 +100,8 @@ contract TreasuryModule is
onlyInstanceOperator
{
IComponent component = _component.getComponent(riskpoolId);
require(component.isRiskpool(), "ERROR:TRS-015:NOT_RISKPOOL");
require(riskpoolWalletAddress != address(0), "ERROR:TRS-016:WALLET_ADDRESS_ZERO");
require(component.isRiskpool(), "ERROR:TRS-016:NOT_RISKPOOL");
require(riskpoolWalletAddress != address(0), "ERROR:TRS-017:WALLET_ADDRESS_ZERO");
_riskpoolWallet[riskpoolId] = riskpoolWalletAddress;

emit LogTreasuryRiskpoolWalletSet (riskpoolId, riskpoolWalletAddress);
Expand Down
19 changes: 18 additions & 1 deletion contracts/test/TestCoin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,21 @@ contract TestCoin is ERC20 {
INITIAL_SUPPLY
);
}
}
}

contract TestCoinX is ERC20 {

string public constant NAME = "Test Dummy X";
string public constant SYMBOL = "TDX";

uint256 public constant INITIAL_SUPPLY = 10**24;

constructor()
ERC20(NAME, SYMBOL)
{
_mint(
_msgSender(),
INITIAL_SUPPLY
);
}
}
2 changes: 1 addition & 1 deletion scripts/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,4 @@ def getContract(self) -> TestProduct:
return self.product

def getPolicy(self, policyId: str):
return self.policy.getPolicy(policyId)
return self.policy.getPolicy(policyId)
5 changes: 5 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
InstanceOperatorService,
InstanceService,
TestCoin,
TestCoinX,
TestRiskpool,
TestOracle,
TestProduct,
Expand Down Expand Up @@ -238,6 +239,10 @@ def registry(registryController, owner) -> RegistryController:
def testCoin(owner) -> TestCoin:
return TestCoin.deploy({'from': owner})

@pytest.fixture(scope="module")
def testCoinX(owner) -> TestCoinX:
return TestCoinX.deploy({'from': owner})

@pytest.fixture(scope="module")
def bundleToken(owner) -> BundleToken:
return BundleToken.deploy({'from': owner})
Expand Down
92 changes: 89 additions & 3 deletions tests/test_treasury_module.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import brownie
import pytest

from brownie.network.account import Account

Expand All @@ -8,6 +9,16 @@
from scripts.product import GifTestOracle, GifTestProduct, GifTestRiskpool
from scripts.util import b2s

from scripts.setup import (
fund_riskpool,
apply_for_policy,
)

# enforce function isolation for tests below
@pytest.fixture(autouse=True)
def isolation(fn_isolation):
pass

def test_bundle_creation_with_instance_wallet_not_set(
instanceNoInstanceWallet: GifInstance,
owner: Account,
Expand All @@ -18,7 +29,7 @@ def test_bundle_creation_with_instance_wallet_not_set(
capitalOwner: Account,
):
withRiskpoolWallet = True
(gifProduct, gifRiskpool) = getProductAndRiskpool(
(gifProduct, gifRiskpool, gifOracle) = getProductAndRiskpool(
instanceNoInstanceWallet,
owner,
testCoin,
Expand Down Expand Up @@ -61,7 +72,7 @@ def test_bundle_creation_with_riskpool_wallet_not_set(
feeOwner: Account,
):
withRiskpoolWallet = False
(gifProduct, gifRiskpool) = getProductAndRiskpool(
(gifProduct, gifRiskpool, gifOracle) = getProductAndRiskpool(
instance,
owner,
testCoin,
Expand Down Expand Up @@ -94,6 +105,80 @@ def test_bundle_creation_with_riskpool_wallet_not_set(
{'from': bundleOwner})


def test_two_products_different_coin_same_riskpool(
instance: GifInstance,
owner: Account,
testCoin,
testCoinX,
productOwner: Account,
oracleProvider: Account,
riskpoolKeeper: Account,
capitalOwner: Account,
):
withRiskpoolWallet = True

# setup riskpool with a product
(gifProduct, gifRiskpool, gifOracle) = getProductAndRiskpool(
instance,
owner,
testCoin,
productOwner,
oracleProvider,
riskpoolKeeper,
capitalOwner,
withRiskpoolWallet
)

# ensure that creating of another product with different token is not allowed
with brownie.reverts("ERROR:TRS-014:TOKEN_ADDRESS_NOT_MACHING"):
GifTestProduct(
instance,
testCoinX,
capitalOwner,
productOwner,
gifOracle,
gifRiskpool,
'Test.Product2')


def test_two_products_same_riskpool(
instance: GifInstance,
owner: Account,
testCoin,
productOwner: Account,
oracleProvider: Account,
riskpoolKeeper: Account,
capitalOwner: Account,
customer: Account,
):
withRiskpoolWallet = True

# setup riskpool with a product
(gifProduct, gifRiskpool, gifOracle) = getProductAndRiskpool(
instance,
owner,
testCoin,
productOwner,
oracleProvider,
riskpoolKeeper,
capitalOwner,
withRiskpoolWallet
)

# ensure that creating of another product with different token succeeds
gifProduct2 = GifTestProduct(
instance,
testCoin,
capitalOwner,
productOwner,
gifOracle,
gifRiskpool,
'Test.Product2')

# ensure the two products are different
assert gifProduct2.getContract().getId() != gifProduct.getContract().getId()


def getProductAndRiskpool(
instance: GifInstance,
owner: Account,
Expand Down Expand Up @@ -127,5 +212,6 @@ def getProductAndRiskpool(

return (
gifProduct,
gifRiskpool
gifRiskpool,
gifOracle
)

0 comments on commit 9e7827c

Please sign in to comment.