From 351b75266f67477f6936ecb93f000d5f18d4d9eb Mon Sep 17 00:00:00 2001 From: rapiddenis <41779817+rapidddenis@users.noreply.github.com> Date: Wed, 27 Mar 2024 13:19:17 +0000 Subject: [PATCH] change require to error --- contracts/instance/Instance.sol | 44 +++++++++++++++---- contracts/instance/InstanceReader.sol | 14 ++++-- contracts/instance/service/BundleService.sol | 8 ++-- contracts/instance/service/IBundleService.sol | 1 + contracts/instance/service/IPolicyService.sol | 2 + contracts/instance/service/PolicyService.sol | 16 +++++-- 6 files changed, 66 insertions(+), 19 deletions(-) diff --git a/contracts/instance/Instance.sol b/contracts/instance/Instance.sol index 1c79b37c4..89e1f2527 100644 --- a/contracts/instance/Instance.sol +++ b/contracts/instance/Instance.sol @@ -41,6 +41,18 @@ contract Instance is AccessManagedUpgradeable, Registerable { + error InstanceErrorReaderInstanceMismatch(address expectedInstance, address foundInstance); + + error InstanceErrorBundleManagerAlreadySet(); + error InstanceErrorBundleManagerAuthorityMismatch(); + error InstanceErrorBundleManagerInstanceMismatch(address expectedInstance, address foundInstance); + + error InstanceErrorAccessManagerAlreadySet(); + error InstanceErrorAccessManagerAuthorityMismatch(); + + error InstanceErrorInstanceStoreAlreadySet(); + error InstanceErrorInstanceStoreAuthorityMismatch(); + uint256 public constant GIF_MAJOR_VERSION = 3; bool private _initialized; @@ -152,7 +164,9 @@ contract Instance is } function setInstanceReader(InstanceReader instanceReader) external restricted() { - require(instanceReader.getInstance() == Instance(this), "InstanceReader instance mismatch"); + if(instanceReader.getInstance() != Instance(this)) { + revert InstanceErrorReaderInstanceMismatch(address(this), address(instanceReader.getInstance())); + } _instanceReader = instanceReader; } @@ -165,9 +179,15 @@ contract Instance is } function setBundleManager(BundleManager bundleManager) external restricted() { - require(address(_bundleManager) == address(0), "BundleManager is set"); - require(bundleManager.getInstance() == Instance(this), "BundleManager instance mismatch"); - require(bundleManager.authority() == authority(), "BundleManager authority mismatch"); + if(address(_bundleManager) != address(0)) { + revert InstanceErrorBundleManagerAlreadySet(); + } + if(bundleManager.getInstance() != Instance(this)) { + revert InstanceErrorBundleManagerInstanceMismatch(address(this), address(bundleManager.getInstance())); + } + if(bundleManager.authority() != authority()) { + revert InstanceErrorBundleManagerAuthorityMismatch(); + } _bundleManager = bundleManager; } @@ -176,8 +196,12 @@ contract Instance is } function setInstanceAccessManager(InstanceAccessManager accessManager) external restricted { - require(address(_accessManager) == address(0), "InstanceAccessManager is set"); - require(accessManager.authority() == authority(), "InstanceAccessManager authority mismatch"); + if(address(_accessManager) != address(0)) { + revert InstanceErrorAccessManagerAlreadySet(); + } + if(accessManager.authority() != authority()) { + revert InstanceErrorAccessManagerAuthorityMismatch(); + } _accessManager = accessManager; } @@ -186,8 +210,12 @@ contract Instance is } function setInstanceStore(InstanceStore instanceStore) external restricted { - require(address(_instanceStore) == address(0), "InstanceStore is set"); - require(instanceStore.authority() == authority(), "InstanceStore authority mismatch"); + if(address(_instanceStore) != address(0)) { + revert InstanceErrorInstanceStoreAlreadySet(); + } + if(instanceStore.authority() != authority()) { + revert InstanceErrorInstanceStoreAuthorityMismatch(); + } _instanceStore = instanceStore; } diff --git a/contracts/instance/InstanceReader.sol b/contracts/instance/InstanceReader.sol index 60550d3b5..a80c18bc8 100644 --- a/contracts/instance/InstanceReader.sol +++ b/contracts/instance/InstanceReader.sol @@ -31,17 +31,23 @@ import {InstanceStore} from "./InstanceStore.sol"; contract InstanceReader { + + error ErrorInstanceReaderAlreadyInitialized(); + error ErrorInstanceReaderInstanceZero(); + bool private _initialized; IInstance internal _instance; IKeyValueStore internal _store; function initialize(address instance) public { - require(!_initialized, "ERROR:CRD-000:ALREADY_INITIALIZED"); + if(_initialized) { + revert ErrorInstanceReaderAlreadyInitialized(); + } - require( - address(instance) != address(0), - "ERROR:CRD-001:INSTANCE_ZERO"); + if(instance == address(0)) { + revert ErrorInstanceReaderInstanceZero(); + } _instance = IInstance(instance); _store = _instance.getInstanceStore(); diff --git a/contracts/instance/service/BundleService.sol b/contracts/instance/service/BundleService.sol index 7de5e53aa..169d6db49 100644 --- a/contracts/instance/service/BundleService.sol +++ b/contracts/instance/service/BundleService.sol @@ -180,13 +180,13 @@ contract BundleService is { (NftId poolNftId,, IInstance instance) = _getAndVerifyCallingComponentAndInstance(POOL()); InstanceReader instanceReader = instance.getInstanceReader(); - IBundle.BundleInfo memory bundleInfo = instanceReader.getBundleInfo(bundleNftId); - require(bundleInfo.poolNftId.gtz(), "ERROR:PLS-010:BUNDLE_UNKNOWN"); - require(poolNftId == bundleInfo.poolNftId, "ERROR:PLS-011:BUNDLE_POOL_MISMATCH"); - bundleInfo.fee = fee; + if(bundleInfo.poolNftId != poolNftId) { + revert ErrorBundleServiceBundlePoolMismatch(bundleNftId, bundleInfo.poolNftId, poolNftId); + } + bundleInfo.fee = fee; instance.getInstanceStore().updateBundle(bundleNftId, bundleInfo, KEEP_STATE()); } diff --git a/contracts/instance/service/IBundleService.sol b/contracts/instance/service/IBundleService.sol index a9f7e2c5d..2f8cebf72 100644 --- a/contracts/instance/service/IBundleService.sol +++ b/contracts/instance/service/IBundleService.sol @@ -16,6 +16,7 @@ interface IBundleService is IService { event LogBundleServiceBundleActivated(NftId bundleNftId); event LogBundleServiceBundleLocked(NftId bundleNftId); + error ErrorBundleServiceBundlePoolMismatch(NftId bundleNftId, NftId bundlePoolNftId, NftId poolNftId); error ErrorBundleServiceInsufficientAllowance(address bundleOwner, address tokenHandlerAddress, uint256 amount); error ErrorBundleServiceBundleNotOpen(NftId bundleNftId, StateId state, Timestamp expiredAt); error ErrorBundleServiceCapacityInsufficient(NftId bundleNftId, uint capacityAmount, uint collateralAmount); diff --git a/contracts/instance/service/IPolicyService.sol b/contracts/instance/service/IPolicyService.sol index ec6581da5..be0d4b426 100644 --- a/contracts/instance/service/IPolicyService.sol +++ b/contracts/instance/service/IPolicyService.sol @@ -25,6 +25,8 @@ interface IPolicyService is IService { error ErrorIPolicyServicePolicyHasNotExpired(NftId policyNftId, Timestamp expiredAt); error ErrorIPolicyServicePremiumMismatch(NftId policyNftId, uint256 premiumAmount, uint256 recalculatedPremiumAmount); + error ErrorIPolicyServicePolicyProductMismatch(NftId policyNftId, NftId policyProductNftId, NftId productNftId); + error ErrorIPolicyServicePolicyStateInvalid(NftId policyNftId, StateId expectedState, StateId foundState); /// @dev declines an application represented by {policyNftId} /// an application can only be declined in applied state diff --git a/contracts/instance/service/PolicyService.sol b/contracts/instance/service/PolicyService.sol index e087dc6f6..db42f7823 100644 --- a/contracts/instance/service/PolicyService.sol +++ b/contracts/instance/service/PolicyService.sol @@ -118,7 +118,7 @@ contract PolicyService is external override { - require(false, "ERROR:PRS-235:NOT_YET_IMPLEMENTED"); + revert(); } @@ -137,10 +137,20 @@ contract PolicyService is // check policy matches with calling product IPolicy.PolicyInfo memory applicationInfo = instanceReader.getPolicyInfo(applicationNftId); - require(applicationInfo.productNftId == productNftId, "POLICY_PRODUCT_MISMATCH"); + if(applicationInfo.productNftId != productNftId) { + revert ErrorIPolicyServicePolicyProductMismatch( + applicationNftId, + applicationInfo.productNftId, + productNftId); + } // check policy is in state applied - require(instanceReader.getPolicyState(applicationNftId) == APPLIED(), "ERROR:PRS-021:STATE_NOT_APPLIED"); + if(instanceReader.getPolicyState(applicationNftId) != APPLIED()) { + revert ErrorIPolicyServicePolicyStateInvalid( + applicationNftId, + APPLIED(), + instanceReader.getPolicyState(applicationNftId)); + } StateId newPolicyState = UNDERWRITTEN();