Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.1+commit.c8a2cb62.js&optimize=false&gist=
pragma solidity ^0.5.0;
contract RiskOrder {
event OrderCreated(address _customer, bytes32 _metaDataHash, string _description);
event PoolCreated(string _riskPoolName, uint256 _maxVolume);
event RiskCapitalProviderCreated(string _providerName, uint256 _maxVolume);
event RiskBoundToPool(bytes32 _metaDataHash, string _riskPoolName, string _requestedRisk, uint256 _requestedVolume);
event RiskCapitalProviderBoundToPool(string _providerName, string _riskPoolName, string _risk, uint256 _volume);
enum orderState {initial, open, quoted, applied, bound}
enum poolState {initial, open, closed}
enum providerState {initial, open, bound, closed}
struct riskOrder {
orderState state;
address customer;
bytes32 riskPool;
string requestedRisk;
uint256 requestedVolume;
}
struct riskPool {
poolState state;
string poolName;
address keeper;
string risk;
uint256 maxVolume;
uint256 usedVolume;
uint256 providedVolume;
}
struct riskCapitalProvider {
providerState state;
string providerName;
address investor;
bytes32 riskPool;
string allowedRisk;
uint256 maxVolume;
uint256 usedVolume;
}
mapping (bytes32 => riskOrder) public riskOrders;
mapping (bytes32 => riskPool) public riskPools;
mapping (bytes32 => riskCapitalProvider) public riskCapitalProviders;
/**
* @dev Creates a Risk Order.
* @param _metaDataHash is a IPFS hash which points to some policy/risk metadata.
*/
function createOrder(bytes32 _metaDataHash, string memory _requestedRisk, uint256 _requestedVolume) public {
require(riskOrders[_metaDataHash].state == orderState.initial);
riskOrders[_metaDataHash] = riskOrder({
state: orderState.open,
customer: msg.sender,
riskPool: 0x0,
requestedRisk: _requestedRisk,
requestedVolume: _requestedVolume
});
emit OrderCreated(msg.sender, _metaDataHash, _requestedRisk);
}
/**
* Customer confirms that he's willing to enter in the contract
*/
function applyOrder(bytes32 _metaDataHash) public {
require(riskOrders[_metaDataHash].customer == msg.sender);
require(riskOrders[_metaDataHash].state == orderState.open);
riskOrders[_metaDataHash].state = orderState.applied;
}
/**
* @dev view RiskOrder
* @param _metaDataHash the hash
*/
function getOrder(bytes32 _metaDataHash) public view returns (
orderState _state,
address _customer,
bytes32 _riskPool,
string memory _requestedRisk) {
riskOrder memory ro;
ro = riskOrders[_metaDataHash];
return (ro.state, ro.customer, ro.riskPool, ro.requestedRisk);
}
/**
* here come the riskPool functions
*/
function createRiskPool(string memory _riskPoolName, string memory _risk, uint256 _maxVolume) public {
bytes32 hash = keccak256(abi.encode(_riskPoolName));
require(riskPools[hash].state == poolState.initial);
riskPools[hash] = riskPool({
state: poolState.open,
poolName: _riskPoolName,
risk: _risk,
keeper: msg.sender,
maxVolume: _maxVolume,
usedVolume: 0,
providedVolume: 0
});
emit PoolCreated(_riskPoolName, _maxVolume);
}
function bindRiskOrderToRiskPool(bytes32 _metaDataHash, string memory _riskPoolName) public {
bytes32 poolHash = keccak256(abi.encode(_riskPoolName));
require(riskOrders[_metaDataHash].state == orderState.applied, "Risk must be applied to be bound to riskpool");
require(riskPools[poolHash].state == poolState.open, "Risk Pool must be open to accept risks");
require(riskPools[poolHash].keeper == msg.sender, "Only keeper can bind risk to riskpool");
require(riskPools[poolHash].usedVolume + riskOrders[_metaDataHash].requestedVolume
< riskPools[poolHash].maxVolume, "Pool does not have enough free volume");
require(riskMatch(riskPools[poolHash].risk, riskOrders[_metaDataHash].requestedRisk), "Risks must match");
riskOrders[_metaDataHash].riskPool = poolHash;
riskOrders[_metaDataHash].state = orderState.bound;
riskPools[poolHash].usedVolume += riskOrders[_metaDataHash].requestedVolume;
emit RiskBoundToPool(
_metaDataHash,
_riskPoolName,
riskOrders[_metaDataHash].requestedRisk,
riskOrders[_metaDataHash].requestedVolume
);
}
/**
* @dev bind a RiskCapitalProvider to a certain riskPool
*/
function bindRiskCapitalProviderToRiskPool(string memory _providerName, string memory _poolName) public {
bytes32 poolHash = keccak256(abi.encode(_poolName));
bytes32 providerHash = keccak256(abi.encode(_providerName));
require(riskPools[poolHash].state == poolState.open, "Pool must be open to bind riskCapitalProviders");
require(riskPools[poolHash].keeper == msg.sender, "Only keeper can bind riskCapitalProviders to pool");
require(riskCapitalProviders[providerHash].state == providerState.open, "riskCapitalProviders must be open to be bound");
require(riskMatch(riskCapitalProviders[providerHash].allowedRisk, riskPools[poolHash].risk), "Risks must match");
riskCapitalProviders[providerHash].riskPool = poolHash;
riskCapitalProviders[providerHash].state = providerState.bound;
riskPools[poolHash].providedVolume = riskCapitalProviders[providerHash].maxVolume;
// now min(usedVolume, providedVolume) can be underwritten.
}
function closeRiskPool(string memory _riskPoolName) public {
// TODO: implement
}
/**
* here come the riskCapitalProviders function
*/
function createRiskCapitalProvider(
string memory _providerName,
uint256 _maxVolume,
string memory _allowedRisk
) public {
bytes32 hash = keccak256(abi.encode(_providerName));
require(riskCapitalProviders[hash].state == providerState.initial);
riskCapitalProviders[hash] = riskCapitalProvider({
state: providerState.open,
providerName: _providerName,
maxVolume: _maxVolume,
allowedRisk: _allowedRisk,
usedVolume: 0,
investor: msg.sender,
riskPool: 0x00
});
emit RiskCapitalProviderCreated(_providerName, _maxVolume);
}
/**
* @dev Close a RiskCapitalProvider. Can only be done if not bound to risks.
* Can only be done by investor
*/
function closeRiskCapitalProvider(
string memory _providerName
) public {
// TODO: Implement
}
/**
* @dev check if requested and offered risks match.
*/
function riskMatch(string memory _requestedRisk, string memory _allowedRisk) public returns (bool _result) {
_result = true; // just for the mockup. In practice, match requested against allowed risk.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.