@ensuro/core 1.2.7 → 2.0.0-beta10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +63 -27
- package/build/contracts/AccessManager.sol/AccessManager.json +559 -0
- package/build/contracts/ERC4626AssetManager.sol/ERC4626AssetManager.json +219 -0
- package/build/contracts/EToken.sol/EToken.json +217 -190
- package/build/contracts/LPManualWhitelist.sol/LPManualWhitelist.json +17 -4
- package/build/contracts/{extras/IPriceRiskModule.sol/IPriceRiskModule.json → LiquidityThresholdAssetManager.sol/LiquidityThresholdAssetManager.json} +81 -131
- package/build/contracts/Policy.sol/Policy.json +2 -2
- package/build/contracts/PolicyPool.sol/PolicyPool.json +425 -295
- package/build/contracts/PolicyPoolComponent.sol/PolicyPoolComponent.json +15 -2
- package/build/contracts/PremiumsAccount.sol/PremiumsAccount.json +868 -0
- package/build/contracts/{BaseAssetManager.sol/BaseAssetManager.json → Reserve.sol/Reserve.json} +37 -166
- package/build/contracts/RiskModule.sol/RiskModule.json +85 -312
- package/build/contracts/SignedQuoteRiskModule.sol/SignedQuoteRiskModule.json +960 -0
- package/build/contracts/{DataTypes.sol/DataTypes.json → TimeScaled.sol/TimeScaled.json} +4 -4
- package/build/contracts/TrustfulRiskModule.sol/TrustfulRiskModule.json +134 -336
- package/build/contracts/dependencies/ERC1967Proxy.sol/ERC1967Proxy.json +64 -0
- package/build/{interfaces/IExchange.sol/IExchange.json → contracts/dependencies/IERC20Metadata.sol/IERC20Metadata.json} +83 -79
- package/build/contracts/{WadRayMath.sol → dependencies/WadRayMath.sol}/WadRayMath.json +3 -3
- package/build/{interfaces/IPolicyPoolConfig.sol/IPolicyPoolConfig.json → contracts/interfaces/IAccessManager.sol/IAccessManager.json} +79 -77
- package/build/contracts/interfaces/IAssetManager.sol/IAssetManager.json +109 -0
- package/build/{interfaces → contracts/interfaces}/IEToken.sol/IEToken.json +39 -91
- package/build/{interfaces → contracts/interfaces}/ILPWhitelist.sol/ILPWhitelist.json +1 -1
- package/build/{interfaces → contracts/interfaces}/IPolicyHolder.sol/IPolicyHolder.json +1 -1
- package/build/{interfaces → contracts/interfaces}/IPolicyPool.sol/IPolicyPool.json +137 -100
- package/build/{interfaces → contracts/interfaces}/IPolicyPoolComponent.sol/IPolicyPoolComponent.json +1 -1
- package/build/contracts/interfaces/IPremiumsAccount.sol/IPremiumsAccount.json +315 -0
- package/build/{interfaces → contracts/interfaces}/IRiskModule.sol/IRiskModule.json +58 -34
- package/build/contracts/mocks/FixedRateVault.sol/FixedRateVault.json +736 -0
- package/build/contracts/mocks/ForwardProxy.sol/ForwardProxy.json +43 -0
- package/build/{interfaces/IInsolvencyHook.sol/IInsolvencyHook.json → contracts/mocks/IMintableERC20.sol/IMintableERC20.json} +13 -8
- package/build/contracts/mocks/PolicyHolderMock.sol/PolicyHolderMock.json +228 -0
- package/build/contracts/mocks/PolicyPoolComponentMock.sol/PolicyPoolComponentMock.json +35 -0
- package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMock.json +702 -0
- package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMockForward.json +92 -0
- package/build/contracts/mocks/RiskModuleMock.sol/RiskModuleMock.json +764 -0
- package/build/contracts/mocks/TestCurrency.sol/TestCurrency.json +343 -0
- package/build/contracts/mocks/TestNFT.sol/TestNFT.json +388 -0
- package/contracts/AccessManager.sol +103 -0
- package/contracts/ERC4626AssetManager.sol +52 -0
- package/contracts/EToken.sol +254 -299
- package/contracts/LPManualWhitelist.sol +4 -4
- package/contracts/LiquidityThresholdAssetManager.sol +160 -0
- package/contracts/Policy.sol +49 -45
- package/contracts/PolicyPool.sol +379 -514
- package/contracts/PolicyPoolComponent.sol +41 -41
- package/contracts/PremiumsAccount.sol +480 -0
- package/contracts/Reserve.sol +213 -0
- package/contracts/RiskModule.sol +233 -174
- package/contracts/SignedQuoteRiskModule.sol +241 -0
- package/contracts/TimeScaled.sol +138 -0
- package/contracts/TrustfulRiskModule.sol +85 -23
- package/contracts/dependencies/ERC1967Proxy.sol +19 -0
- package/contracts/dependencies/IERC20Metadata.sol +19 -0
- package/contracts/dependencies/WadRayMath.sol +126 -0
- package/contracts/interfaces/IAccessManager.sol +148 -0
- package/contracts/interfaces/IAssetManager.sol +84 -0
- package/contracts/interfaces/IEToken.sol +195 -0
- package/contracts/interfaces/ILPWhitelist.sol +40 -0
- package/{interfaces → contracts/interfaces}/IPolicyHolder.sol +0 -0
- package/contracts/interfaces/IPolicyPool.sol +145 -0
- package/{interfaces → contracts/interfaces}/IPolicyPoolComponent.sol +3 -0
- package/contracts/interfaces/IPremiumsAccount.sol +76 -0
- package/contracts/interfaces/IRiskModule.sol +123 -0
- package/contracts/mocks/FixedRateVault.sol +85 -0
- package/contracts/mocks/ForwardProxy.sol +66 -0
- package/contracts/mocks/IMintableERC20.sol +13 -0
- package/contracts/mocks/PolicyHolderMock.sol +89 -0
- package/contracts/mocks/PolicyPoolComponentMock.sol +17 -0
- package/contracts/mocks/PolicyPoolMock.sol +148 -0
- package/contracts/mocks/RiskModuleMock.sol +75 -0
- package/contracts/mocks/TestCurrency.sol +35 -0
- package/contracts/mocks/TestNFT.sol +23 -0
- package/js/test-utils.js +356 -0
- package/package.json +2 -2
- package/build/contracts/AaveAssetManager.sol/AaveAssetManager.json +0 -742
- package/build/contracts/Exchange.sol/Exchange.json +0 -591
- package/build/contracts/FlightDelayRiskModule.sol/FlightDelayRiskModule.json +0 -1112
- package/build/contracts/PolicyNFT.sol/PolicyNFT.json +0 -570
- package/build/contracts/PolicyPoolConfig.sol/PolicyPoolConfig.json +0 -686
- package/build/contracts/dependencies/ChainlinkClientUpgradeable.sol/ChainlinkClientUpgradeable.json +0 -50
- package/build/contracts/extras/PriceRiskModule.sol/PriceRiskModule.json +0 -1055
- package/build/contracts/upgraded/IPolicyNFTv1.sol/IPolicyNFTv1.json +0 -322
- package/build/contracts/upgraded/PolicyNFTv1.sol/PolicyNFTv1.json +0 -571
- package/build/contracts/upgraded/PolicyNFTv1.sol/PolicyNFTv1_Upgrade.json +0 -602
- package/build/interfaces/IAssetManager.sol/IAssetManager.json +0 -31
- package/build/interfaces/IExchange.sol/IPriceOracle.json +0 -30
- package/build/interfaces/IPolicyNFT.sol/IPolicyNFT.json +0 -321
- package/contracts/AaveAssetManager.sol +0 -298
- package/contracts/BaseAssetManager.sol +0 -301
- package/contracts/DataTypes.sol +0 -287
- package/contracts/Exchange.sol +0 -189
- package/contracts/FlightDelayRiskModule.sol +0 -208
- package/contracts/PolicyNFT.sol +0 -96
- package/contracts/PolicyPoolConfig.sol +0 -247
- package/contracts/WadRayMath.sol +0 -135
- package/contracts/dependencies/ChainlinkClientUpgradeable.sol +0 -371
- package/contracts/extras/IPriceRiskModule.sol +0 -41
- package/contracts/extras/PriceRiskModule.sol +0 -243
- package/contracts/upgraded/IPolicyNFTv1.sol +0 -15
- package/contracts/upgraded/PolicyNFTv1.sol +0 -126
- package/interfaces/IAssetManager.sol +0 -23
- package/interfaces/IEToken.sol +0 -48
- package/interfaces/IExchange.sol +0 -59
- package/interfaces/IInsolvencyHook.sol +0 -27
- package/interfaces/ILPWhitelist.sol +0 -23
- package/interfaces/IPolicyNFT.sol +0 -15
- package/interfaces/IPolicyPool.sol +0 -44
- package/interfaces/IPolicyPoolConfig.sol +0 -87
- package/interfaces/IRiskModule.sol +0 -29
package/contracts/Exchange.sol
DELETED
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
-
pragma solidity ^0.8.0;
|
|
3
|
-
|
|
4
|
-
import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
|
|
5
|
-
import {IUniswapV2Router02} from "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
|
|
6
|
-
import {IPolicyPool} from "../interfaces/IPolicyPool.sol";
|
|
7
|
-
import {IPolicyPoolConfig} from "../interfaces/IPolicyPoolConfig.sol";
|
|
8
|
-
import {PolicyPoolComponent} from "./PolicyPoolComponent.sol";
|
|
9
|
-
import {IExchange, IPriceOracle} from "../interfaces/IExchange.sol";
|
|
10
|
-
import {WadRayMath} from "./WadRayMath.sol";
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* @title Exchange contract
|
|
14
|
-
* @dev Helper contract that handles exchange operations between assets
|
|
15
|
-
* @custom:security-contact security@ensuro.co
|
|
16
|
-
* @author Ensuro
|
|
17
|
-
*/
|
|
18
|
-
contract Exchange is IExchange, PolicyPoolComponent {
|
|
19
|
-
using WadRayMath for uint256;
|
|
20
|
-
|
|
21
|
-
IPriceOracle internal _oracle;
|
|
22
|
-
IUniswapV2Router02 internal _swapRouter; // We will use SushiSwap in Polygon
|
|
23
|
-
uint256 internal _maxSlippage; // Maximum slippage in WAD
|
|
24
|
-
|
|
25
|
-
/// @custom:oz-upgrades-unsafe-allow constructor
|
|
26
|
-
// solhint-disable-next-line no-empty-blocks
|
|
27
|
-
constructor(IPolicyPool policyPool_) PolicyPoolComponent(policyPool_) {}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* @dev Initializes the RiskModule
|
|
31
|
-
* @param oracle_ Oracle that provides the price of the assets
|
|
32
|
-
* @param swapRouter_ Uniswap compatible exchange
|
|
33
|
-
* @param maxSlippage_ Maximum slippage admitted for exchange operations
|
|
34
|
-
*/
|
|
35
|
-
function initialize(
|
|
36
|
-
IPriceOracle oracle_,
|
|
37
|
-
IUniswapV2Router02 swapRouter_,
|
|
38
|
-
uint256 maxSlippage_
|
|
39
|
-
) public initializer {
|
|
40
|
-
__PolicyPoolComponent_init();
|
|
41
|
-
__Exchange_init(oracle_, swapRouter_, maxSlippage_);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// solhint-disable-next-line func-name-mixedcase
|
|
45
|
-
function __Exchange_init(
|
|
46
|
-
IPriceOracle oracle_,
|
|
47
|
-
IUniswapV2Router02 swapRouter_,
|
|
48
|
-
uint256 maxSlippage_
|
|
49
|
-
) internal initializer {
|
|
50
|
-
_oracle = oracle_;
|
|
51
|
-
_swapRouter = swapRouter_;
|
|
52
|
-
_maxSlippage = maxSlippage_;
|
|
53
|
-
_validateParameters();
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function _validateParameters() internal view override {
|
|
57
|
-
require(_maxSlippage <= 1e17, "maxSlippage can't be more than 10%");
|
|
58
|
-
require(address(_oracle) != address(0), "I need a price oracle");
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function convert(
|
|
62
|
-
address assetFrom,
|
|
63
|
-
address assetTo,
|
|
64
|
-
uint256 amount
|
|
65
|
-
) public view override returns (uint256) {
|
|
66
|
-
return amount.wadMul(getExchangeRate(assetFrom, assetTo));
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function getExchangeRate(address asset, address expressedInAsset)
|
|
70
|
-
public
|
|
71
|
-
view
|
|
72
|
-
override
|
|
73
|
-
returns (uint256)
|
|
74
|
-
{
|
|
75
|
-
uint256 exchangeRate = _oracle.getAssetPrice(asset).wadDiv(
|
|
76
|
-
_oracle.getAssetPrice(expressedInAsset)
|
|
77
|
-
);
|
|
78
|
-
uint8 decFrom = IERC20Metadata(asset).decimals();
|
|
79
|
-
uint8 decTo = IERC20Metadata(expressedInAsset).decimals();
|
|
80
|
-
if (decFrom > decTo) {
|
|
81
|
-
exchangeRate /= 10**(decFrom - decTo);
|
|
82
|
-
} else {
|
|
83
|
-
exchangeRate *= 10**(decTo - decFrom);
|
|
84
|
-
}
|
|
85
|
-
return exchangeRate;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function getAmountIn(
|
|
89
|
-
address assetIn,
|
|
90
|
-
address assetOut,
|
|
91
|
-
uint256 amountOut
|
|
92
|
-
) external view override returns (uint256) {
|
|
93
|
-
return _swapRouter.getAmountsIn(amountOut, _exchangePath(assetIn, assetOut))[0];
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
function getSwapRouter() external view override returns (address) {
|
|
97
|
-
return address(_swapRouter);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
function getPriceOracle() external view override returns (IPriceOracle) {
|
|
101
|
-
return IPriceOracle(_oracle);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
function _exchangePath(address assetIn, address assetOut)
|
|
105
|
-
internal
|
|
106
|
-
pure
|
|
107
|
-
returns (address[] memory)
|
|
108
|
-
{
|
|
109
|
-
address[] memory path = new address[](2);
|
|
110
|
-
path[0] = assetIn;
|
|
111
|
-
path[1] = assetOut;
|
|
112
|
-
return path;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
function sell(
|
|
116
|
-
address assetIn,
|
|
117
|
-
address assetOut,
|
|
118
|
-
uint256 amountInExact,
|
|
119
|
-
address outAddr,
|
|
120
|
-
uint256 deadline
|
|
121
|
-
) external view override returns (bytes memory) {
|
|
122
|
-
uint256 amountOutMin = convert(assetIn, assetOut, amountInExact).wadMul(1e18 - _maxSlippage);
|
|
123
|
-
|
|
124
|
-
return
|
|
125
|
-
abi.encodeWithSignature(
|
|
126
|
-
"swapExactTokensForTokens(uint256,uint256,address[],address,uint256)",
|
|
127
|
-
amountInExact,
|
|
128
|
-
amountOutMin,
|
|
129
|
-
_exchangePath(assetIn, assetOut),
|
|
130
|
-
outAddr,
|
|
131
|
-
deadline
|
|
132
|
-
);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function buy(
|
|
136
|
-
address assetIn,
|
|
137
|
-
address assetOut,
|
|
138
|
-
uint256 amountOutExact,
|
|
139
|
-
address outAddr,
|
|
140
|
-
uint256 deadline
|
|
141
|
-
) external view override returns (bytes memory) {
|
|
142
|
-
uint256 amountInMax = convert(assetOut, assetIn, amountOutExact).wadMul(1e18 + _maxSlippage);
|
|
143
|
-
|
|
144
|
-
return
|
|
145
|
-
abi.encodeWithSignature(
|
|
146
|
-
"swapTokensForExactTokens(uint256,uint256,address[],address,uint256)",
|
|
147
|
-
amountOutExact,
|
|
148
|
-
amountInMax,
|
|
149
|
-
_exchangePath(assetIn, assetOut),
|
|
150
|
-
outAddr,
|
|
151
|
-
deadline
|
|
152
|
-
);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
function decodeSwapOut(bytes memory responseData) external pure override returns (uint256) {
|
|
156
|
-
return abi.decode(responseData, (uint256[]))[1];
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
function maxSlippage() external view override returns (uint256) {
|
|
160
|
-
return _maxSlippage;
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
function setMaxSlippage(uint256 newValue) external onlyPoolRole2(LEVEL2_ROLE, LEVEL3_ROLE) {
|
|
164
|
-
require(newValue <= 1e17, "maxSlippage can't be more than 10%");
|
|
165
|
-
bool tweak = !hasPoolRole(LEVEL2_ROLE);
|
|
166
|
-
require(
|
|
167
|
-
!tweak || _isTweakWad(_maxSlippage, newValue, 3e26),
|
|
168
|
-
"Tweak exceeded: maxSlippage tweaks only up to 30%"
|
|
169
|
-
);
|
|
170
|
-
_maxSlippage = newValue;
|
|
171
|
-
_parameterChanged(IPolicyPoolConfig.GovernanceActions.setMaxSlippage, newValue, tweak);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
function setPriceOracle(IPriceOracle newPriceOracle)
|
|
175
|
-
external
|
|
176
|
-
onlyPoolRole2(LEVEL1_ROLE, LEVEL2_ROLE)
|
|
177
|
-
{
|
|
178
|
-
_oracle = newPriceOracle;
|
|
179
|
-
_componentChanged(IPolicyPoolConfig.GovernanceActions.setPriceOracle, address(newPriceOracle));
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
function setSwapRouter(IUniswapV2Router02 newSwapRouter)
|
|
183
|
-
external
|
|
184
|
-
onlyPoolRole2(LEVEL1_ROLE, LEVEL2_ROLE)
|
|
185
|
-
{
|
|
186
|
-
_swapRouter = newSwapRouter;
|
|
187
|
-
_componentChanged(IPolicyPoolConfig.GovernanceActions.setSwapRouter, address(newSwapRouter));
|
|
188
|
-
}
|
|
189
|
-
}
|
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
pragma solidity ^0.8.0;
|
|
3
|
-
|
|
4
|
-
import {IPolicyPool} from "../interfaces/IPolicyPool.sol";
|
|
5
|
-
import {Policy} from "./Policy.sol";
|
|
6
|
-
import {RiskModule} from "./RiskModule.sol";
|
|
7
|
-
import {Chainlink} from "@chainlink/contracts/src/v0.8/Chainlink.sol";
|
|
8
|
-
import {ChainlinkClientUpgradeable} from "./dependencies/ChainlinkClientUpgradeable.sol";
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @title Flight Delay Risk Module
|
|
12
|
-
* @dev Risk Module that resolves policy based in actualarrivaldate of flight
|
|
13
|
-
* @custom:security-contact security@ensuro.co
|
|
14
|
-
* @author Ensuro
|
|
15
|
-
*/
|
|
16
|
-
contract FlightDelayRiskModule is RiskModule, ChainlinkClientUpgradeable {
|
|
17
|
-
using Chainlink for Chainlink.Request;
|
|
18
|
-
|
|
19
|
-
bytes32 public constant PRICER_ROLE = keccak256("PRICER_ROLE");
|
|
20
|
-
bytes32 public constant ORACLE_ADMIN_ROLE = keccak256("ORACLE_ADMIN_ROLE");
|
|
21
|
-
// Multiplier to calculate expiration = expectedArrival + tolerance + delayTime * DELAY_EXPIRATION_TIMES
|
|
22
|
-
uint40 public constant DELAY_EXPIRATION_TIMES = 5;
|
|
23
|
-
|
|
24
|
-
struct PolicyData {
|
|
25
|
-
Policy.PolicyData ensuroPolicy;
|
|
26
|
-
string flight;
|
|
27
|
-
uint40 departure;
|
|
28
|
-
uint40 expectedArrival;
|
|
29
|
-
uint40 tolerance;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
struct OracleParams {
|
|
33
|
-
address oracle;
|
|
34
|
-
uint96 delayTime;
|
|
35
|
-
uint256 fee;
|
|
36
|
-
bytes16 dataJobId;
|
|
37
|
-
bytes16 sleepJobId;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
OracleParams internal _oracleParams;
|
|
41
|
-
|
|
42
|
-
mapping(bytes32 => uint256) internal _pendingQueries;
|
|
43
|
-
mapping(uint256 => PolicyData) internal _policies;
|
|
44
|
-
|
|
45
|
-
/// @custom:oz-upgrades-unsafe-allow constructor
|
|
46
|
-
// solhint-disable-next-line no-empty-blocks
|
|
47
|
-
constructor(IPolicyPool policyPool_) RiskModule(policyPool_) {}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* @dev Initializes the RiskModule
|
|
51
|
-
* @param name_ Name of the Risk Module
|
|
52
|
-
* @param scrPercentage_ Solvency Capital Requirement percentage, to calculate
|
|
53
|
-
capital requirement as % of (payout - premium) (in ray)
|
|
54
|
-
* @param ensuroFee_ % of premium that will go for Ensuro treasury (in ray)
|
|
55
|
-
* @param scrInterestRate_ cost of capital (in ray)
|
|
56
|
-
* @param maxScrPerPolicy_ Max SCR to be allocated to this module (in wad)
|
|
57
|
-
* @param scrLimit_ Max SCR to be allocated to this module (in wad)
|
|
58
|
-
* @param wallet_ Address of the RiskModule provider
|
|
59
|
-
* @param linkToken_ Address of ChainLink LINK token
|
|
60
|
-
* @param oracleParams_ Parameters of the Oracle
|
|
61
|
-
*/
|
|
62
|
-
function initialize(
|
|
63
|
-
string memory name_,
|
|
64
|
-
uint256 scrPercentage_,
|
|
65
|
-
uint256 ensuroFee_,
|
|
66
|
-
uint256 scrInterestRate_,
|
|
67
|
-
uint256 maxScrPerPolicy_,
|
|
68
|
-
uint256 scrLimit_,
|
|
69
|
-
address wallet_,
|
|
70
|
-
address linkToken_,
|
|
71
|
-
OracleParams memory oracleParams_
|
|
72
|
-
) public initializer {
|
|
73
|
-
__RiskModule_init(
|
|
74
|
-
name_,
|
|
75
|
-
scrPercentage_,
|
|
76
|
-
ensuroFee_,
|
|
77
|
-
scrInterestRate_,
|
|
78
|
-
maxScrPerPolicy_,
|
|
79
|
-
scrLimit_,
|
|
80
|
-
wallet_
|
|
81
|
-
);
|
|
82
|
-
__ChainlinkClient_init();
|
|
83
|
-
__FlightDelayRiskModule_init_unchained(linkToken_, oracleParams_);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
// solhint-disable-next-line func-name-mixedcase
|
|
87
|
-
function __FlightDelayRiskModule_init_unchained(
|
|
88
|
-
address linkToken_,
|
|
89
|
-
OracleParams memory oracleParams_
|
|
90
|
-
) internal initializer {
|
|
91
|
-
setChainlinkToken(linkToken_);
|
|
92
|
-
_oracleParams = oracleParams_;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function setOracleParams(OracleParams memory newParams) external onlyRole(ORACLE_ADMIN_ROLE) {
|
|
96
|
-
_oracleParams = newParams;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function oracleParams() external view returns (OracleParams memory) {
|
|
100
|
-
return _oracleParams;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* @dev Creates a new policy
|
|
105
|
-
* @param flight Flight Number as String (ex: NAX105)
|
|
106
|
-
* @param departure Departure in epoch seconds (ex: 1631817600)
|
|
107
|
-
* @param expectedArrival Expected arrival in epoch seconds (ex: 1631824800)
|
|
108
|
-
* @param tolerance In seconds, the tolerance margin after expectedArrival before trigger the policy
|
|
109
|
-
* @param payout Payout for customer in case policy is triggered
|
|
110
|
-
* @param premium Premium the customer pays
|
|
111
|
-
* @param lossProb Probability of policy being triggered
|
|
112
|
-
* @param customer Customer address (to take premium from and send payout)
|
|
113
|
-
*/
|
|
114
|
-
function newPolicy(
|
|
115
|
-
string memory flight,
|
|
116
|
-
uint40 departure,
|
|
117
|
-
uint40 expectedArrival,
|
|
118
|
-
uint40 tolerance,
|
|
119
|
-
uint256 payout,
|
|
120
|
-
uint256 premium,
|
|
121
|
-
uint256 lossProb,
|
|
122
|
-
address customer,
|
|
123
|
-
uint96 internalId
|
|
124
|
-
) external onlyRole(PRICER_ROLE) returns (uint256) {
|
|
125
|
-
require(expectedArrival > block.timestamp, "expectedArrival can't be in the past");
|
|
126
|
-
require(departure != 0 && expectedArrival > departure, "expectedArrival <= departure!");
|
|
127
|
-
uint40 expiration = expectedArrival +
|
|
128
|
-
tolerance +
|
|
129
|
-
uint40(_oracleParams.delayTime) *
|
|
130
|
-
DELAY_EXPIRATION_TIMES;
|
|
131
|
-
Policy.PolicyData memory ensuroPolicy = _newPolicy(
|
|
132
|
-
payout,
|
|
133
|
-
premium,
|
|
134
|
-
lossProb,
|
|
135
|
-
expiration,
|
|
136
|
-
customer,
|
|
137
|
-
internalId
|
|
138
|
-
);
|
|
139
|
-
PolicyData storage policy = _policies[ensuroPolicy.id];
|
|
140
|
-
policy.ensuroPolicy = ensuroPolicy;
|
|
141
|
-
policy.flight = flight;
|
|
142
|
-
policy.departure = departure;
|
|
143
|
-
policy.expectedArrival = expectedArrival;
|
|
144
|
-
policy.tolerance = tolerance;
|
|
145
|
-
|
|
146
|
-
uint256 until = expectedArrival + tolerance + uint256(_oracleParams.delayTime);
|
|
147
|
-
_chainlinkRequest(ensuroPolicy.id, policy, until);
|
|
148
|
-
return ensuroPolicy.id;
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
function _chainlinkRequest(
|
|
152
|
-
uint256 policyId,
|
|
153
|
-
PolicyData storage policy,
|
|
154
|
-
uint256 until
|
|
155
|
-
) internal {
|
|
156
|
-
// request takes a JobID, a callback address, and callback function as input
|
|
157
|
-
Chainlink.Request memory req = buildChainlinkRequest(
|
|
158
|
-
until == 0 ? _oracleParams.dataJobId : _oracleParams.sleepJobId,
|
|
159
|
-
address(this),
|
|
160
|
-
this.fulfill.selector
|
|
161
|
-
);
|
|
162
|
-
req.add("flight", policy.flight);
|
|
163
|
-
req.add("endpoint", "actualarrivaldate");
|
|
164
|
-
req.addUint("departure", policy.departure);
|
|
165
|
-
if (until > 0) {
|
|
166
|
-
req.addUint("until", until);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
// Sends the request with the amount of payment specified to the oracle
|
|
170
|
-
// (results will arrive with the callback = later)
|
|
171
|
-
bytes32 queryId = sendChainlinkRequestTo(_oracleParams.oracle, req, _oracleParams.fee);
|
|
172
|
-
_pendingQueries[queryId] = policyId;
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/**
|
|
176
|
-
* @dev Forces the resolution of the policy (without waiting Chainlink scheduled on creation)
|
|
177
|
-
* @param policyId The id of the policy previously created (in newPolicy)
|
|
178
|
-
*/
|
|
179
|
-
function resolvePolicy(uint256 policyId) external onlyRole(PRICER_ROLE) returns (uint256) {
|
|
180
|
-
PolicyData storage policy = _policies[policyId];
|
|
181
|
-
require(policy.expectedArrival != 0, "Policy not found!");
|
|
182
|
-
_chainlinkRequest(policyId, policy, 0);
|
|
183
|
-
return policyId;
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
function fulfill(bytes32 queryId, int256 actualArrivalDate)
|
|
187
|
-
public
|
|
188
|
-
recordChainlinkFulfillment(queryId)
|
|
189
|
-
{
|
|
190
|
-
uint256 policyId = _pendingQueries[queryId];
|
|
191
|
-
require(policyId != 0, "queryId not found!");
|
|
192
|
-
PolicyData storage policy = _policies[policyId];
|
|
193
|
-
|
|
194
|
-
if (actualArrivalDate == 0) {
|
|
195
|
-
if (block.timestamp > (policy.expectedArrival + policy.tolerance)) {
|
|
196
|
-
// Treat as arrived after tolerance
|
|
197
|
-
actualArrivalDate = int256(uint256((policy.expectedArrival + policy.tolerance) + 1));
|
|
198
|
-
} else {
|
|
199
|
-
// Not arrived yet
|
|
200
|
-
return;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
bool customerWon = (actualArrivalDate <= 0 || // cancelled
|
|
204
|
-
uint256(actualArrivalDate) > uint256(policy.expectedArrival + policy.tolerance)); // arrived after tolerance
|
|
205
|
-
|
|
206
|
-
_policyPool.resolvePolicyFullPayout(policy.ensuroPolicy, customerWon);
|
|
207
|
-
}
|
|
208
|
-
}
|
package/contracts/PolicyNFT.sol
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
pragma solidity ^0.8.2;
|
|
3
|
-
|
|
4
|
-
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
|
|
5
|
-
import {ERC721Upgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol";
|
|
6
|
-
import {PausableUpgradeable} from "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";
|
|
7
|
-
import {IPolicyPool} from "../interfaces/IPolicyPool.sol";
|
|
8
|
-
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
|
|
9
|
-
import {IPolicyNFT} from "../interfaces/IPolicyNFT.sol";
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* @title PolicyNFT - NFT that keeps track of issued policies and its owners
|
|
13
|
-
* @dev Every time a new policy is accepted by the PolicyPool, a new NFT is minted generating a new
|
|
14
|
-
* policyId owned by the customer. Only the PolicyPool can mint NFTs.
|
|
15
|
-
* After creation, NFTs can be transferred in the ERC721 standard way and that changes the policy holder.
|
|
16
|
-
* @custom:security-contact security@ensuro.co
|
|
17
|
-
* @author Ensuro
|
|
18
|
-
*/
|
|
19
|
-
contract PolicyNFT is UUPSUpgradeable, ERC721Upgradeable, PausableUpgradeable, IPolicyNFT {
|
|
20
|
-
bytes32 public constant GUARDIAN_ROLE = keccak256("GUARDIAN_ROLE");
|
|
21
|
-
bytes32 public constant LEVEL1_ROLE = keccak256("LEVEL1_ROLE");
|
|
22
|
-
|
|
23
|
-
IPolicyPool internal _policyPool;
|
|
24
|
-
|
|
25
|
-
modifier onlyPolicyPool() {
|
|
26
|
-
require(_msgSender() == address(_policyPool), "The caller must be the PolicyPool");
|
|
27
|
-
_;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
modifier onlyPoolRole2(bytes32 role1, bytes32 role2) {
|
|
31
|
-
_policyPool.config().checkRole2(role1, role2, msg.sender);
|
|
32
|
-
_;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
modifier onlyPoolRole(bytes32 role) {
|
|
36
|
-
_policyPool.config().checkRole(role, msg.sender);
|
|
37
|
-
_;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function initialize(
|
|
41
|
-
string memory name_,
|
|
42
|
-
string memory symbol_,
|
|
43
|
-
IPolicyPool policyPool_
|
|
44
|
-
) public initializer {
|
|
45
|
-
__UUPSUpgradeable_init();
|
|
46
|
-
__Pausable_init();
|
|
47
|
-
__ERC721_init(name_, symbol_);
|
|
48
|
-
__PolicyNFT_init_unchained(policyPool_);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// solhint-disable-next-line func-name-mixedcase
|
|
52
|
-
function __PolicyNFT_init_unchained(IPolicyPool policyPool_) internal initializer {
|
|
53
|
-
_policyPool = policyPool_;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// solhint-disable-next-line no-empty-blocks
|
|
57
|
-
function _authorizeUpgrade(address) internal override onlyPoolRole2(GUARDIAN_ROLE, LEVEL1_ROLE) {}
|
|
58
|
-
|
|
59
|
-
function pause() public onlyPoolRole(GUARDIAN_ROLE) {
|
|
60
|
-
_pause();
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function unpause() public onlyPoolRole2(GUARDIAN_ROLE, LEVEL1_ROLE) {
|
|
64
|
-
_unpause();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* @dev This function can be called only once in contract's lifetime. It links the NFT with the
|
|
69
|
-
* PolicyPool contract. It's called in PolicyPool initialization.
|
|
70
|
-
*/
|
|
71
|
-
function connect() external override {
|
|
72
|
-
require(
|
|
73
|
-
address(_policyPool) == address(0) || address(_policyPool) == _msgSender(),
|
|
74
|
-
"PolicyPool already connected"
|
|
75
|
-
);
|
|
76
|
-
_policyPool = IPolicyPool(_msgSender());
|
|
77
|
-
// Not possible to do this validation because connect is called in _policyPool initialize :'(
|
|
78
|
-
// require(_policyPool.policyNFT() == address(this), "PolicyPool not connected to this config");
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function policyPool() external view returns (IPolicyPool) {
|
|
82
|
-
return _policyPool;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function safeMint(address to, uint256 policyId) external override onlyPolicyPool whenNotPaused {
|
|
86
|
-
_safeMint(to, policyId, "");
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function _beforeTokenTransfer(
|
|
90
|
-
address from,
|
|
91
|
-
address to,
|
|
92
|
-
uint256 tokenId
|
|
93
|
-
) internal override whenNotPaused {
|
|
94
|
-
super._beforeTokenTransfer(from, to, tokenId);
|
|
95
|
-
}
|
|
96
|
-
}
|