Skip to content

Commit

Permalink
added RequestSet to instance (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
doerfli committed Dec 13, 2024
1 parent 32f46cb commit 0d7abc9
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 39 deletions.
3 changes: 3 additions & 0 deletions contracts/instance/IInstance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {InstanceReader} from "./InstanceReader.sol";
import {InstanceStore} from "./InstanceStore.sol";
import {NftId} from "../type/NftId.sol";
import {ProductStore} from "./ProductStore.sol";
import {RequestSet} from "./RequestSet.sol";
import {RoleId} from "../type/RoleId.sol";
import {Seconds} from "../type/Seconds.sol";
import {UFixed} from "../type/UFixed.sol";
Expand Down Expand Up @@ -58,6 +59,7 @@ interface IInstance is
ProductStore productStore;
BundleSet bundleSet;
RiskSet riskSet;
RequestSet requestSet;
InstanceReader instanceReader;
}

Expand Down Expand Up @@ -149,6 +151,7 @@ interface IInstance is
function getInstanceReader() external view returns (InstanceReader);
function getBundleSet() external view returns (BundleSet);
function getRiskSet() external view returns (RiskSet);
function getRequestSet() external view returns (RequestSet);
function getInstanceAdmin() external view returns (InstanceAdmin);
function getInstanceStore() external view returns (InstanceStore);
function getProductStore() external view returns (ProductStore);
Expand Down
4 changes: 3 additions & 1 deletion contracts/instance/IInstanceService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface IInstanceService is IService {
error ErrorInstanceServiceMasterInstanceAdminAlreadySet();
error ErrorInstanceServiceMasterBundleSetAlreadySet();
error ErrorInstanceServiceMasterRiskSetAlreadySet();
error ErrorInstanceServiceMasterRequestSetAlreadySet();
error ErrorInstanceServiceInstanceAddressZero();

error ErrorInstanceServiceMasterInstanceReaderNotSet();
Expand All @@ -38,6 +39,7 @@ interface IInstanceService is IService {
error ErrorInstanceServiceInstanceReaderZero();
error ErrorInstanceServiceBundleSetZero();
error ErrorInstanceServiceRiskSetZero();
error ErrorInstanceServiceRequestSetZero();
error ErrorInstanceServiceInstanceStoreZero();
error ErrorInstanceServiceProductStoreZero();

Expand All @@ -59,7 +61,7 @@ interface IInstanceService is IService {
event LogInstanceServiceInstanceLocked(NftId instanceNftId, bool locked);
event LogInstanceServiceInstanceCreated(NftId instanceNftId, address instance);
event LogInstanceServiceMasterInstanceRegistered(NftId masterInstanceNftId, address masterInstance, address masterInstanceAdmin, address masterAccessManager,
address masterInstanceReader, address masterInstanceBundleSet, address masterInstanceRiskSet, address masterInstanceStore, address masterProductStore);
address masterInstanceReader, address masterInstanceBundleSet, address masterInstanceRiskSet, address masterInstanceRequestSet, address masterInstanceStore, address masterProductStore);
event LogInstanceServiceMasterInstanceReaderUpgraded(NftId instanceNfId, address newInstanceReader);
event LogInstanceServiceInstanceReaderUpgraded(NftId instanceNfId, address newInstanceReader);

Expand Down
7 changes: 7 additions & 0 deletions contracts/instance/Instance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {InstanceStore} from "./InstanceStore.sol";
import {NftId} from "../type/NftId.sol";
import {ProductStore} from "./ProductStore.sol";
import {Registerable} from "../shared/Registerable.sol";
import {RequestSet} from "./RequestSet.sol";
import {RoleId} from "../type/RoleId.sol";
import {Seconds} from "../type/Seconds.sol";
import {UFixed} from "../type/UFixed.sol";
Expand All @@ -35,6 +36,7 @@ contract Instance is
InstanceReader internal _instanceReader;
BundleSet internal _bundleSet;
RiskSet internal _riskSet;
RequestSet internal _requestSet;
InstanceStore internal _instanceStore;
ProductStore internal _productStore;
NftId [] internal _products;
Expand Down Expand Up @@ -88,6 +90,7 @@ contract Instance is
_productStore = instanceContracts.productStore;
_bundleSet = instanceContracts.bundleSet;
_riskSet = instanceContracts.riskSet;
_requestSet = instanceContracts.requestSet;
_instanceReader = instanceContracts.instanceReader;

// initialize instance supporting contracts
Expand Down Expand Up @@ -333,6 +336,10 @@ contract Instance is
return _riskSet;
}

function getRequestSet() external view returns (RequestSet) {
return _requestSet;
}

function getInstanceAdmin() external view returns (InstanceAdmin) {
return _instanceAdmin;
}
Expand Down
96 changes: 59 additions & 37 deletions contracts/instance/InstanceService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {InstanceStore} from "./InstanceStore.sol";
import {NftId} from "../type/NftId.sol";
import {ObjectType, INSTANCE, COMPONENT, INSTANCE, REGISTRY, STAKING} from "../type/ObjectType.sol";
import {ProductStore} from "./ProductStore.sol";
import {RequestSet} from "./RequestSet.sol";
import {RiskSet} from "./RiskSet.sol";
import {RoleId} from "../type/RoleId.sol";
import {Seconds} from "../type/Seconds.sol";
Expand Down Expand Up @@ -49,6 +50,7 @@ contract InstanceService is
address internal _masterInstanceReader;
address internal _masterInstanceBundleSet;
address internal _masterInstanceRiskSet;
address internal _masterInstanceRequestSet;
address internal _masterInstanceStore;
address internal _masterProductStore;

Expand Down Expand Up @@ -323,49 +325,68 @@ contract InstanceService is
if(_masterInstanceAdmin != address(0)) { revert ErrorInstanceServiceMasterInstanceAdminAlreadySet(); }
if(_masterInstanceBundleSet != address(0)) { revert ErrorInstanceServiceMasterBundleSetAlreadySet(); }
if(_masterInstanceRiskSet != address(0)) { revert ErrorInstanceServiceMasterRiskSetAlreadySet(); }
if(_masterInstanceRequestSet != address(0)) { revert ErrorInstanceServiceMasterRequestSetAlreadySet(); }
if(instanceAddress == address(0)) { revert ErrorInstanceServiceInstanceAddressZero(); }

{
IInstance instance = IInstance(instanceAddress);
address accessManagerAddress = instance.authority();
InstanceAdmin instanceAdmin = instance.getInstanceAdmin();
address instanceAdminAddress = address(instanceAdmin);
InstanceReader instanceReader = instance.getInstanceReader();
address instanceReaderAddress = address(instanceReader);
BundleSet bundleSet = instance.getBundleSet();
address bundleSetAddress = address(bundleSet);
RiskSet riskSet = instance.getRiskSet();
address riskSetAddress = address(riskSet);
InstanceStore instanceStore = instance.getInstanceStore();
address instanceStoreAddress = address(instanceStore);
ProductStore productStore = instance.getProductStore();
address productStoreAddress = address(productStore);

if(accessManagerAddress == address(0)) { revert ErrorInstanceServiceAccessManagerZero(); }
if(instanceAdminAddress == address(0)) { revert ErrorInstanceServiceInstanceAdminZero(); }
if(instanceReaderAddress == address(0)) { revert ErrorInstanceServiceInstanceReaderZero(); }
if(bundleSetAddress == address(0)) { revert ErrorInstanceServiceBundleSetZero(); }
if(riskSetAddress == address(0)) { revert ErrorInstanceServiceRiskSetZero(); }
if(instanceStoreAddress == address(0)) { revert ErrorInstanceServiceInstanceStoreZero(); }
if(productStoreAddress == address(0)) { revert ErrorInstanceServiceProductStoreZero(); } // TODO: rename exception

{
address instanceAdminAddress = address(instanceAdmin);
InstanceReader instanceReader = instance.getInstanceReader();
address instanceReaderAddress = address(instanceReader);

if(accessManagerAddress == address(0)) { revert ErrorInstanceServiceAccessManagerZero(); }
if(instanceAdminAddress == address(0)) { revert ErrorInstanceServiceInstanceAdminZero(); }
if(instanceReaderAddress == address(0)) { revert ErrorInstanceServiceInstanceReaderZero(); }

if(instance.authority() != instanceAdmin.authority()) { revert ErrorInstanceServiceInstanceAuthorityMismatch(); }
if(instanceReader.getInstance() != instance) { revert ErrorInstanceServiceInstanceReaderInstanceMismatch2(); }

_masterAccessManager = accessManagerAddress;
_masterInstanceAdmin = instanceAdminAddress;
_masterInstance = instanceAddress;
_masterInstanceReader = instanceReaderAddress;
}

{
BundleSet bundleSet = instance.getBundleSet();
address bundleSetAddress = address(bundleSet);
RiskSet riskSet = instance.getRiskSet();
address riskSetAddress = address(riskSet);
RequestSet requestSet = instance.getRequestSet();
address requestSetAddress = address(requestSet);

if(instance.authority() != instanceAdmin.authority()) { revert ErrorInstanceServiceInstanceAuthorityMismatch(); }
if(bundleSet.authority() != instanceAdmin.authority()) { revert ErrorInstanceServiceBundleSetAuthorityMismatch(); }
if(riskSet.authority() != instanceAdmin.authority()) { revert ErrorInstanceServiceRiskSetAuthorityMismatch(); }
if(instanceStore.authority() != instanceAdmin.authority()) { revert ErrorInstanceServiceInstanceStoreAuthorityMismatch(); }
if(productStore.authority() != instanceAdmin.authority()) { revert ErrorInstanceServiceProductStoreAuthorityMismatch(); }
if(bundleSet.getInstanceAddress() != address(instance)) { revert ErrorInstanceServiceBundleSetInstanceMismatch(); }
if(riskSet.getInstanceAddress() != address(instance)) { revert ErrorInstanceServiceRiskSetInstanceMismatch(); }
if(instanceReader.getInstance() != instance) { revert ErrorInstanceServiceInstanceReaderInstanceMismatch2(); }

_masterAccessManager = accessManagerAddress;
_masterInstanceAdmin = instanceAdminAddress;
_masterInstance = instanceAddress;
_masterInstanceReader = instanceReaderAddress;
_masterInstanceBundleSet = bundleSetAddress;
_masterInstanceRiskSet = riskSetAddress;
_masterInstanceStore = instanceStoreAddress;
_masterProductStore = productStoreAddress;
if(bundleSetAddress == address(0)) { revert ErrorInstanceServiceBundleSetZero(); }
if(riskSetAddress == address(0)) { revert ErrorInstanceServiceRiskSetZero(); }
if(requestSetAddress == address(0)) { revert ErrorInstanceServiceRequestSetZero(); }
if(bundleSet.authority() != instanceAdmin.authority()) { revert ErrorInstanceServiceBundleSetAuthorityMismatch(); }
if(riskSet.authority() != instanceAdmin.authority()) { revert ErrorInstanceServiceRiskSetAuthorityMismatch(); }
if(bundleSet.getInstanceAddress() != address(instance)) { revert ErrorInstanceServiceBundleSetInstanceMismatch(); }
if(riskSet.getInstanceAddress() != address(instance)) { revert ErrorInstanceServiceRiskSetInstanceMismatch(); }

_masterInstanceBundleSet = bundleSetAddress;
_masterInstanceRiskSet = riskSetAddress;
_masterInstanceRequestSet = requestSetAddress;
}

{
InstanceStore instanceStore = instance.getInstanceStore();
address instanceStoreAddress = address(instanceStore);
ProductStore productStore = instance.getProductStore();
address productStoreAddress = address(productStore);

if(instanceStoreAddress == address(0)) { revert ErrorInstanceServiceInstanceStoreZero(); }
if(productStoreAddress == address(0)) { revert ErrorInstanceServiceProductStoreZero(); } // TODO: rename exception

if(instanceStore.authority() != instanceAdmin.authority()) { revert ErrorInstanceServiceInstanceStoreAuthorityMismatch(); }
if(productStore.authority() != instanceAdmin.authority()) { revert ErrorInstanceServiceProductStoreAuthorityMismatch(); }

_masterInstanceStore = instanceStoreAddress;
_masterProductStore = productStoreAddress;
}
}

{
Expand All @@ -375,7 +396,7 @@ contract InstanceService is

emit LogInstanceServiceMasterInstanceRegistered(
masterInstanceNftId, _masterInstance, _masterInstanceAdmin, _masterAccessManager,
_masterInstanceReader, _masterInstanceBundleSet, _masterInstanceRiskSet, _masterInstanceStore, _masterProductStore);
_masterInstanceReader, _masterInstanceBundleSet, _masterInstanceRiskSet, _masterInstanceRequestSet, _masterInstanceStore, _masterProductStore);
}
}

Expand Down Expand Up @@ -447,6 +468,7 @@ contract InstanceService is
productStore: ProductStore(Clones.clone(address(_masterProductStore))),
bundleSet: BundleSet(Clones.clone(_masterInstanceBundleSet)),
riskSet: RiskSet(Clones.clone(_masterInstanceRiskSet)),
requestSet: RequestSet(Clones.clone(address(_masterInstanceRequestSet))),
instanceReader: InstanceReader(Clones.clone(address(_masterInstanceReader)))
}),
getRegistry(),
Expand Down
47 changes: 47 additions & 0 deletions contracts/instance/RequestSet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

import {IComponents} from "./module/IComponents.sol";

import {Key32} from "../type/Key32.sol";
import {LibNftIdSet} from "../type/NftIdSet.sol";
import {LibRequestIdSet} from "../type/RequestIdSet.sol";
import {NftId} from "../type/NftId.sol";
import {ObjectSet} from "./base/ObjectSet.sol";
import {ObjectSetHelperLib} from "./base/ObjectSetHelperLib.sol";
import {RequestId} from "../type/RequestId.sol";

contract RequestSet is
ObjectSet
{
using LibNftIdSet for LibNftIdSet.Set;
using LibRequestIdSet for LibRequestIdSet.Set;

event LogRequestSetRequestAdded(NftId indexed oracleNftId, RequestId indexed requestId);
event LogRequestSetRequestRemoved(NftId indexed oracleNftId, RequestId indexed requestId);

error ErrorRequestSetOracleNotRegistered(NftId oracleNftId);

/// @dev add a new request to a oracle registerd with this instance
// the corresponding oracles existence is checked via instance reader
function add(NftId oracleNftId, RequestId requestId) external restricted() {
IComponents.ComponentInfo memory componentInfo = ObjectSetHelperLib.getComponentInfo(_instanceAddress, oracleNftId);

// ensure pool is registered with instance
if (bytes(componentInfo.name).length == 0) {
revert ErrorRequestSetOracleNotRegistered(oracleNftId);
}

_add(oracleNftId, _toRequestKey32(requestId));
emit LogRequestSetRequestAdded(oracleNftId, requestId);
}

function remove(NftId oracleNftId, RequestId requestId) external restricted() {
_remove(oracleNftId, _toRequestKey32(requestId));
emit LogRequestSetRequestRemoved(oracleNftId, requestId);
}

function _toRequestKey32(RequestId requestId) private pure returns (Key32) {
return requestId.toKey32();
}
}
8 changes: 8 additions & 0 deletions contracts/instance/base/ObjectSet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ contract ObjectSet is
activeSet.add(key);
}

function _remove(NftId componentNftId, Key32 key) internal {
LibKey32Set.Set storage allSet = _allObjects[componentNftId];
LibKey32Set.Set storage activeSet = _activeObjects[componentNftId];

allSet.remove(key);
activeSet.remove(key);
}

function _activate(NftId componentNftId, Key32 key) internal {
_activeObjects[componentNftId].add(key);
}
Expand Down
6 changes: 5 additions & 1 deletion contracts/instance/base/ObjectSetHelperLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity ^0.8.20;

import {IBundle} from "../module/IBundle.sol";
import {IComponents} from "../module/IComponents.sol";
import {IInstance} from "../IInstance.sol";
import {IRisk} from "../module/IRisk.sol";
import {NftId} from "../../type/NftId.sol";
Expand All @@ -11,7 +12,6 @@ import {RiskId} from "../../type/RiskId.sol";
library ObjectSetHelperLib {

function getRiskInfo(address instanceAddress, RiskId riskId) public view returns (IRisk.RiskInfo memory) {

return IInstance(instanceAddress).getInstanceReader().getRiskInfo(riskId);
}

Expand All @@ -27,4 +27,8 @@ library ObjectSetHelperLib {
return getBundleInfo(instanceAddress, bundleNftId).poolNftId;
}

function getComponentInfo(address instanceAddress, NftId componentNftId) public view returns (IComponents.ComponentInfo memory) {
return IInstance(instanceAddress).getInstanceReader().getComponentInfo(componentNftId);
}

}
8 changes: 8 additions & 0 deletions test/base/GifTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {Instance} from "../../contracts/instance/Instance.sol";
import {InstanceReader} from "../../contracts/instance/InstanceReader.sol";
import {BundleSet} from "../../contracts/instance/BundleSet.sol";
import {RiskSet} from "../../contracts/instance/RiskSet.sol";
import {RequestSet} from "../../contracts/instance/RequestSet.sol";
import {InstanceStore} from "../../contracts/instance/InstanceStore.sol";
import {ProductStore} from "../../contracts/instance/ProductStore.sol";

Expand Down Expand Up @@ -77,6 +78,7 @@ contract GifTest is GifDeployer {
InstanceAuthorizationV3 public instanceAuthorizationV3;
BundleSet public masterBundleSet;
RiskSet public masterRiskSet;
RequestSet public masterRequestSet;
InstanceStore public masterInstanceStore;
ProductStore public masterProductStore;
Instance public masterInstance;
Expand All @@ -86,6 +88,7 @@ contract GifTest is GifDeployer {
InstanceAdmin public instanceAdmin;
BundleSet public instanceBundleSet;
RiskSet public instanceRiskSet;
RequestSet public instanceRequestSet;
InstanceStore public instanceStore;
IInstance public instance;
NftId public instanceNftId;
Expand Down Expand Up @@ -261,6 +264,7 @@ contract GifTest is GifDeployer {
masterProductStore = new ProductStore();
masterBundleSet = new BundleSet();
masterRiskSet = new RiskSet();
masterRequestSet = new RequestSet();
masterInstanceReader = new InstanceReader();

// crate instance
Expand All @@ -272,6 +276,7 @@ contract GifTest is GifDeployer {
productStore: masterProductStore,
bundleSet: masterBundleSet,
riskSet: masterRiskSet,
requestSet: masterRequestSet,
instanceReader: masterInstanceReader
}),
registry,
Expand Down Expand Up @@ -306,6 +311,7 @@ contract GifTest is GifDeployer {
console.log("master instance reader deployed at", address(masterInstanceReader));
console.log("master bundle set deployed at", address(masterBundleSet));
console.log("master risk set deployed at", address(masterRiskSet));
console.log("master request set deployed at", address(masterRequestSet));
console.log("master instance store deployed at", address(masterInstanceStore));
// solhint-enable
}
Expand All @@ -322,6 +328,7 @@ contract GifTest is GifDeployer {
instanceStore = instance.getInstanceStore();
instanceBundleSet = instance.getBundleSet();
instanceRiskSet = instance.getRiskSet();
instanceRequestSet = instance.getRequestSet();
instanceStore = instance.getInstanceStore();

// solhint-disable
Expand All @@ -331,6 +338,7 @@ contract GifTest is GifDeployer {
console.log("cloned instance reader deployed at", address(instanceReader));
console.log("cloned bundle set deployed at", address(instanceBundleSet));
console.log("cloned risk set deployed at", address(instanceRiskSet));
console.log("cloned request set deployed at", address(instanceRequestSet));
console.log("cloned instance store deployed at", address(instanceStore));
// solhint-enable
}
Expand Down

0 comments on commit 0d7abc9

Please sign in to comment.