diff --git a/contracts/components/IRiskpool.sol b/contracts/components/IRiskpool.sol index c54dd19..97f2060 100644 --- a/contracts/components/IRiskpool.sol +++ b/contracts/components/IRiskpool.sol @@ -21,6 +21,12 @@ interface IRiskpool is IComponent { event LogRiskpoolBalanceDecreased(bytes32 processId, uint256 amount, uint256 newBalance); function createBundle(bytes calldata filter, uint256 initialAmount) external returns(uint256 bundleId); + function fundBundle(uint256 bundleId, uint256 amount) external; + function defundBundle(uint256 bundleId, uint256 amount) external; + + function lockBundle(uint256 bundleId) external; + function unlockBundle(uint256 bundleId) external; + function closeBundle(uint256 bundleId) external; function collateralizePolicy(bytes32 processId) external returns(bool isSecured); function expirePolicy(bytes32 processId) external; diff --git a/contracts/components/Riskpool.sol b/contracts/components/Riskpool.sol index e9f2f5f..b4e4a18 100644 --- a/contracts/components/Riskpool.sol +++ b/contracts/components/Riskpool.sol @@ -10,6 +10,8 @@ import "../modules/IPolicy.sol"; import "../services/IInstanceService.sol"; import "../services/IRiskpoolService.sol"; +import "@openzeppelin/contracts/token/ERC721/IERC721.sol"; + // TODO consider to move bunlde per riskpool book keeping to bundle controller abstract contract Riskpool is IRiskpool, @@ -24,6 +26,7 @@ abstract contract Riskpool is IInstanceService internal _instanceService; IRiskpoolService internal _riskpoolService; + IERC721 internal _bundleToken; // keep track of bundles associated with this riskpool uint256 [] internal _bundleIds; @@ -42,6 +45,17 @@ abstract contract Riskpool is _; } + modifier onlyBundleOwner(uint256 bundleId) { + IBundle.Bundle memory bundle = _instanceService.getBundle(bundleId); + address bundleOwner = _bundleToken.ownerOf(bundle.tokenId); + + require( + _msgSender() == bundleOwner, + "ERROR:BUC-001:NOT_BUNDLE_OWNER" + ); + _; + } + constructor( bytes32 name, uint256 collateralization, @@ -58,6 +72,7 @@ abstract contract Riskpool is _instanceService = IInstanceService(_getContractAddress("InstanceService")); _riskpoolService = IRiskpoolService(_getContractAddress("RiskpoolService")); + _bundleToken = _instanceService.getBundleToken(); } // TODO decide on authz for bundle creation @@ -81,6 +96,40 @@ abstract contract Riskpool is emit LogRiskpoolBundleCreated(bundleId, initialAmount); } + function fundBundle(uint256 bundleId, uint256 amount) + external override + onlyBundleOwner(bundleId) + { + _riskpoolService.fundBundle(bundleId, amount); + } + + function defundBundle(uint256 bundleId, uint256 amount) + external override + onlyBundleOwner(bundleId) + { + _riskpoolService.defundBundle(bundleId, amount); + } + + function lockBundle(uint256 bundleId) + external override + onlyBundleOwner(bundleId) + { + _riskpoolService.lockBundle(bundleId); + } + + function unlockBundle(uint256 bundleId) + external override + onlyBundleOwner(bundleId) + { + _riskpoolService.unlockBundle(bundleId); + } + + function closeBundle(uint256 bundleId) + external override + onlyBundleOwner(bundleId) + { + _riskpoolService.closeBundle(bundleId); + } function collateralizePolicy(bytes32 processId) external override diff --git a/contracts/modules/IBundle.sol b/contracts/modules/IBundle.sol index f456eff..92f07c2 100644 --- a/contracts/modules/IBundle.sol +++ b/contracts/modules/IBundle.sol @@ -40,13 +40,15 @@ interface IBundle { function create(address owner_, uint256 riskpoolId_, bytes calldata filter_, uint256 amount_) external returns(uint256 bundleId); function fund(uint256 bundleId, uint256 amount) external; - function withdraw(uint256 bundleId, uint256 amount) external; + function defund(uint256 bundleId, uint256 amount) external; function lock(uint256 bundleId) external; function unlock(uint256 bundleId) external; function close(uint256 bundleId) external; function collateralizePolicy(uint256 bundleId, bytes32 processId, uint256 collateralAmount) external; - function addPremiumToBalance(uint256 bundleId, bytes32 processId, uint256 premiumAmount) external; function expirePolicy(uint256 bundleId, bytes32 processId) external returns(uint256 collateralAmount); + + function increaseBalance(uint256 bundleId, bytes32 processId, uint256 amount) external; + function decreaseBalance(uint256 bundleId, bytes32 processId, uint256 amount) external; } diff --git a/contracts/modules/IPool.sol b/contracts/modules/IPool.sol index eb950f6..5ced7e6 100644 --- a/contracts/modules/IPool.sol +++ b/contracts/modules/IPool.sol @@ -9,4 +9,7 @@ interface IPool { function setRiskpoolForProduct(uint256 productId, uint256 riskpoolId) external; function underwrite(bytes32 processId) external returns(bool success); function expire(bytes32 processId) external; + + function increaseBalance(bytes32 processId, uint256 amount) external; + function decreaseBalance(bytes32 processId, uint256 amount) external; } diff --git a/contracts/modules/ITreasury.sol b/contracts/modules/ITreasury.sol index 3c2ee10..3b2ce5f 100644 --- a/contracts/modules/ITreasury.sol +++ b/contracts/modules/ITreasury.sol @@ -56,6 +56,12 @@ interface ITreasury { uint256 netCapitalAmount ); + function processWithdrawl(uint256 bundleId, uint256 amount) external + returns( + bool success, + uint256 netAmount + ); + function getComponentToken(uint256 componentId) external view returns(IERC20 token); function getFeeSpecification(uint256 componentId) external view returns(FeeSpecification memory feeSpecification); diff --git a/contracts/services/IInstanceService.sol b/contracts/services/IInstanceService.sol index 16451b1..08cd85d 100644 --- a/contracts/services/IInstanceService.sol +++ b/contracts/services/IInstanceService.sol @@ -10,6 +10,7 @@ import "./IProductService.sol"; import "./IRiskpoolService.sol"; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; +import "@openzeppelin/contracts/token/ERC721/IERC721.sol"; interface IInstanceService { @@ -30,6 +31,7 @@ interface IInstanceService { function hasRole(bytes32 role, address principal) external view returns (bool roleIsAssigned); // bundles + function getBundleToken() external view returns(IERC721 token); function bundles() external view returns(uint256 numberOfBundles); function getBundle(uint256 bundleId) external view returns(IBundle.Bundle memory bundle); diff --git a/contracts/services/IRiskpoolService.sol b/contracts/services/IRiskpoolService.sol index f9dd9c5..b1ddd24 100644 --- a/contracts/services/IRiskpoolService.sol +++ b/contracts/services/IRiskpoolService.sol @@ -4,6 +4,12 @@ pragma solidity ^0.8.0; interface IRiskpoolService { function createBundle(address owner_, bytes calldata filter_, uint256 amount_) external returns(uint256 bundleId); + function fundBundle(uint256 bundleId, uint256 amount) external; + function defundBundle(uint256 bundleId, uint256 amount) external; + + function lockBundle(uint256 bundleId) external; + function unlockBundle(uint256 bundleId) external; + function closeBundle(uint256 bundleId) external; function collateralizePolicy(uint256 bundleId, bytes32 processId, uint256 collateralAmount) external; function expirePolicy(uint256 bundleId, bytes32 processId) external returns(uint256 collateralAmount);