diff --git a/contracts/examples/crop/CropProduct.sol b/contracts/examples/crop/CropProduct.sol index d9a6880b8..4e161a683 100644 --- a/contracts/examples/crop/CropProduct.sol +++ b/contracts/examples/crop/CropProduct.sol @@ -195,7 +195,6 @@ contract CropProduct is if (seasonEndAt < TimestampLib.current()) { revert ErrorInvalidSeasonEndAt(seasonEndAt); } // create risk, if new - bytes32 riskKey = keccak256(abi.encode(id)); CropRisk memory cropRisk = CropRisk({ seasonId: seasonId, locationId: locationId, @@ -205,7 +204,7 @@ contract CropProduct is payoutDefined: false }); - riskId = _createRisk(riskKey, abi.encode(cropRisk)); + riskId = _createRisk(abi.encode(cropRisk)); _riskId[id] = riskId; } diff --git a/contracts/examples/fire/FireProduct.sol b/contracts/examples/fire/FireProduct.sol index 3e7c09458..4bd1e2e44 100644 --- a/contracts/examples/fire/FireProduct.sol +++ b/contracts/examples/fire/FireProduct.sol @@ -248,7 +248,7 @@ contract FireProduct is return _riskMapping[cityName]; } _cities.push(cityName); - risk = _createRisk(bytes32(abi.encodePacked(cityName)), ""); + risk = _createRisk(""); _riskMapping[cityName] = risk; } diff --git a/contracts/examples/flight/FlightLib.sol b/contracts/examples/flight/FlightLib.sol index 599f58891..54b27d6c4 100644 --- a/contracts/examples/flight/FlightLib.sol +++ b/contracts/examples/flight/FlightLib.sol @@ -243,8 +243,6 @@ library FlightLib { function getFlightRisk( - InstanceReader reader, - NftId productNftId, Str flightData, Timestamp departureTime, string memory departureTimeLocal, @@ -254,28 +252,20 @@ library FlightLib { public view returns ( - RiskId riskId, - bool exists, FlightProduct.FlightRisk memory flightRisk ) { - riskId = getRiskId(productNftId, flightData); - (exists, flightRisk) = getFlightRisk(reader, productNftId, riskId, false); - - // create new risk if not existing - if (!exists) { - flightRisk = FlightProduct.FlightRisk({ - flightData: flightData, - departureTime: departureTime, - departureTimeLocal: departureTimeLocal, - arrivalTime: arrivalTime, - arrivalTimeLocal: arrivalTimeLocal, - sumOfSumInsuredAmounts: AmountLib.toAmount(0), - status: bytes1(0), - delayMinutes: 0, - payoutOption: uint8(0), - statusUpdatedAt: TimestampLib.zero()}); - } + flightRisk = FlightProduct.FlightRisk({ + flightData: flightData, + departureTime: departureTime, + departureTimeLocal: departureTimeLocal, + arrivalTime: arrivalTime, + arrivalTimeLocal: arrivalTimeLocal, + sumOfSumInsuredAmounts: AmountLib.toAmount(0), + status: bytes1(0), + delayMinutes: 0, + payoutOption: uint8(0), + statusUpdatedAt: TimestampLib.zero()}); } @@ -306,20 +296,6 @@ library FlightLib { } } - - function getRiskId( - NftId productNftId, - Str flightData - ) - public - view - returns (RiskId riskId) - { - bytes32 riskKey = getRiskKey(flightData); - riskId = getRiskId(productNftId, riskKey); - } - - function getRiskKey( Str flightData ) @@ -330,8 +306,4 @@ library FlightLib { return keccak256(abi.encode(flightData)); } - - function getRiskId(NftId productNftId, bytes32 riskKey) internal view returns (RiskId riskId) { - return RiskIdLib.toRiskId(productNftId, riskKey); - } } \ No newline at end of file diff --git a/contracts/examples/flight/FlightProduct.sol b/contracts/examples/flight/FlightProduct.sol index aed5af67a..6f9e1704b 100644 --- a/contracts/examples/flight/FlightProduct.sol +++ b/contracts/examples/flight/FlightProduct.sol @@ -67,6 +67,7 @@ contract FlightProduct is bool internal _testMode; mapping(RiskId riskId => RequestId requestId) internal _requests; + mapping(bytes32 encodedRisk => RiskId) internal _risks; // GIF V3 specifics NftId internal _defaultBundleNftId; @@ -507,20 +508,20 @@ contract FlightProduct is returns (RiskId riskId) { bool exists; - FlightRisk memory flightRisk; - (riskId, exists, flightRisk) = FlightLib.getFlightRisk( - _getInstanceReader(), - getNftId(), + FlightRisk memory flightRisk = FlightLib.getFlightRisk( flightData, departureTime, departureTimeLocal, arrivalTime, arrivalTimeLocal); + bytes32 riskKey = FlightLib.getRiskKey(flightData); + exists = _risks[riskKey].gtz(); + // create risk, if new if (!exists) { - bytes32 riskKey = FlightLib.getRiskKey(flightData); - _createRisk(riskKey, abi.encode(flightRisk)); + riskId = _createRisk(abi.encode(flightRisk)); + _risks[riskKey] = riskId; } FlightLib.checkClusterRisk( diff --git a/contracts/examples/unpermissioned/SimpleProduct.sol b/contracts/examples/unpermissioned/SimpleProduct.sol index cf62e20cd..61bd04b05 100644 --- a/contracts/examples/unpermissioned/SimpleProduct.sol +++ b/contracts/examples/unpermissioned/SimpleProduct.sol @@ -81,13 +81,9 @@ contract SimpleProduct is function createRisk( - string memory id, bytes memory data ) public returns (RiskId) { - return _createRisk( - bytes32(abi.encodePacked(id)), - data - ); + return _createRisk(data); } function updateRisk( diff --git a/contracts/instance/ProductStore.sol b/contracts/instance/ProductStore.sol index 4cbe92f63..29b8e11c3 100644 --- a/contracts/instance/ProductStore.sol +++ b/contracts/instance/ProductStore.sol @@ -113,7 +113,8 @@ contract ProductStore is } //--- Risk --------------------------------------------------------------// - function createRisk(RiskId riskId, IRisk.RiskInfo memory info) external restricted() { + function createRisk(IRisk.RiskInfo memory info) external restricted() returns (RiskId riskId) { + riskId = _createNextRiskId(info.productNftId); Key32 key = riskId.toKey32(); _createMetadata(key); _risks[key] = info; diff --git a/contracts/instance/base/ObjectCounter.sol b/contracts/instance/base/ObjectCounter.sol index 84a096c1c..ece3f65ab 100644 --- a/contracts/instance/base/ObjectCounter.sol +++ b/contracts/instance/base/ObjectCounter.sol @@ -1,20 +1,22 @@ // SPDX-License-Identifier: Apache-2.0 pragma solidity ^0.8.20; -import {Blocknumber, BlocknumberLib} from "../../type/Blocknumber.sol"; import {NftId} from "../../type/NftId.sol"; -import {Amount} from "../../type/Amount.sol"; import {RequestId, RequestIdLib} from "../../type/RequestId.sol"; +import {RiskId, RiskIdLib} from "../../type/RiskId.sol"; contract ObjectCounter { - // TODO refactor risk id - // mapping(NftId productNftId => uint64 risks) private _riskCounter; - + mapping(NftId productNftId => uint64 risks) private _riskCounter; uint256 private _requestCounter = 0; function _createNextRequestId() internal returns (RequestId requestId) { _requestCounter++; requestId = RequestIdLib.toRequestId(_requestCounter); } + + function _createNextRiskId(NftId productNftId) internal returns (RiskId riskId) { + _riskCounter[productNftId]++; + riskId = RiskIdLib.toRiskId(_riskCounter[productNftId]); + } } diff --git a/contracts/product/IRiskService.sol b/contracts/product/IRiskService.sol index a7cb852e2..b1800da80 100644 --- a/contracts/product/IRiskService.sol +++ b/contracts/product/IRiskService.sol @@ -23,7 +23,6 @@ interface IRiskService is IService { /// The key of the risk derived from the risk id in comination with the product NftId. /// Risk data is stored in the instance store. function createRisk( - bytes32 id, bytes memory data ) external returns (RiskId riskId); diff --git a/contracts/product/Product.sol b/contracts/product/Product.sol index 4a7382e07..74a86ab84 100644 --- a/contracts/product/Product.sol +++ b/contracts/product/Product.sol @@ -181,17 +181,13 @@ abstract contract Product is function _createRisk( - bytes32 id, bytes memory data ) internal virtual returns (RiskId riskId) { - return _getProductStorage()._riskService.createRisk( - id, - data - ); + return _getProductStorage()._riskService.createRisk(data); } function _updateRisk( diff --git a/contracts/product/RiskService.sol b/contracts/product/RiskService.sol index a9b83e4f5..80e8655c6 100644 --- a/contracts/product/RiskService.sol +++ b/contracts/product/RiskService.sol @@ -40,7 +40,6 @@ contract RiskService is /// @inheritdoc IRiskService function createRisk( - bytes32 id, bytes memory data ) external @@ -52,16 +51,12 @@ contract RiskService is (NftId productNftId, IInstance instance) = _getAndVerifyActiveComponent(PRODUCT()); // effects - riskId = RiskIdLib.toRiskId(productNftId, id); IRisk.RiskInfo memory riskInfo = IRisk.RiskInfo({ productNftId: productNftId, createdAt: TimestampLib.current(), data: data}); - instance.getProductStore().createRisk( - riskId, - riskInfo - ); + riskId = instance.getProductStore().createRisk(riskInfo); // add risk to RiskSet RiskSet riskSet = instance.getRiskSet(); diff --git a/contracts/type/RiskId.sol b/contracts/type/RiskId.sol index d06dbaad5..8ff08b096 100644 --- a/contracts/type/RiskId.sol +++ b/contracts/type/RiskId.sol @@ -2,10 +2,9 @@ pragma solidity ^0.8.20; import {Key32, KeyId, Key32Lib} from "./Key32.sol"; -import {NftId} from "./NftId.sol"; import {RISK} from "./ObjectType.sol"; -type RiskId is bytes8; +type RiskId is uint64; // type bindings using { @@ -33,7 +32,7 @@ function neRiskId(RiskId a, RiskId b) pure returns (bool isDifferent) { library RiskIdLib { function zero() public pure returns (RiskId) { - return RiskId.wrap(bytes8(0)); + return RiskId.wrap(uint64(0)); } // @dev Converts a risk id into a uint256. @@ -42,8 +41,8 @@ library RiskIdLib { } // @dev Converts a risk id string with a product NftId into a risk id. - function toRiskId(NftId productNftId, bytes32 risk) public pure returns (RiskId) { - return RiskId.wrap(bytes8(keccak256(abi.encode(productNftId, risk)))); + function toRiskId(uint256 id) public pure returns (RiskId) { + return RiskId.wrap(uint64(id)); } /// @dev Returns the key32 value for the specified risk id. @@ -53,12 +52,13 @@ library RiskIdLib { /// @dev Returns the key id value for the specified nft id function toKeyId(RiskId id) public pure returns (KeyId keyId) { - return KeyId.wrap(bytes31(RiskId.unwrap(id))); + return KeyId.wrap(bytes31(uint248(RiskId.unwrap(id)))); } function toRiskId(KeyId keyId) public pure returns (RiskId riskId) { - riskId = RiskId.wrap(bytes8(KeyId.unwrap(keyId))); - assert(toInt(riskId) < 2**64); + uint248 keyIdInt = uint248(bytes31(KeyId.unwrap(keyId))); + assert(keyIdInt < type(uint64).max); + return RiskId.wrap(uint64(keyIdInt)); } function eq(RiskId a, RiskId b) public pure returns (bool isSame) {