Skip to content

Commit

Permalink
change require to error
Browse files Browse the repository at this point in the history
  • Loading branch information
rapidddenis committed Mar 27, 2024
1 parent 0b18675 commit 351b752
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 19 deletions.
44 changes: 36 additions & 8 deletions contracts/instance/Instance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

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

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

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

Expand Down
14 changes: 10 additions & 4 deletions contracts/instance/InstanceReader.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions contracts/instance/service/BundleService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
1 change: 1 addition & 0 deletions contracts/instance/service/IBundleService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions contracts/instance/service/IPolicyService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 13 additions & 3 deletions contracts/instance/service/PolicyService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ contract PolicyService is
external
override
{
require(false, "ERROR:PRS-235:NOT_YET_IMPLEMENTED");
revert();
}


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

Expand Down

0 comments on commit 351b752

Please sign in to comment.