Skip to content

Commit

Permalink
add bundle management to riskpool
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiaszimmermann committed Jul 17, 2022
1 parent 259fb5c commit 4d8f578
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 2 deletions.
6 changes: 6 additions & 0 deletions contracts/components/IRiskpool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
49 changes: 49 additions & 0 deletions contracts/components/Riskpool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions contracts/modules/IBundle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions contracts/modules/IPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 6 additions & 0 deletions contracts/modules/ITreasury.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 2 additions & 0 deletions contracts/services/IInstanceService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);

Expand Down
6 changes: 6 additions & 0 deletions contracts/services/IRiskpoolService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 4d8f578

Please sign in to comment.