Skip to content

Commit

Permalink
Merge pull request #52 from valory-xyz/marketplace_proxy
Browse files Browse the repository at this point in the history
refactor: marketplace proxy
  • Loading branch information
DavidMinarsch authored Dec 16, 2024
2 parents 85c513a + ef4acbd commit 7c2c719
Show file tree
Hide file tree
Showing 10 changed files with 285 additions and 399 deletions.
34 changes: 9 additions & 25 deletions contracts/AgentMech.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ interface IERC721 {
function ownerOf(uint256 tokenId) external view returns (address tokenOwner);
}

// Mech manager interface
interface IMechManager {
/// @dev Gets the status of mech marketplace.
/// @param mechMarketplace Mech marketplace address.
/// @return status True, if the marketplace is whitelisted.
function mapMechMarketplaces(address mechMarketplace) external view returns (bool status);
}

/// @dev A Mech that is operated by the multisig of an Olas service
contract OlasMech is Mech, IErrorsMech, ImmutableStorage {
/// @param _serviceRegistry Address of the registry contract.
Expand Down Expand Up @@ -103,8 +95,8 @@ contract AgentMech is OlasMech {
bytes32 public immutable domainSeparator;

Check warning on line 95 in contracts/AgentMech.sol

View workflow job for this annotation

GitHub Actions / build

Immutable variables name are set to be in capitalized SNAKE_CASE
// Original chain Id
uint256 public immutable chainId;

Check warning on line 97 in contracts/AgentMech.sol

View workflow job for this annotation

GitHub Actions / build

Immutable variables name are set to be in capitalized SNAKE_CASE
// Mech Manager address
address public immutable mechManager;
// Mech marketplace address
address public immutable mechMarketplace;

Check warning on line 99 in contracts/AgentMech.sol

View workflow job for this annotation

GitHub Actions / build

Immutable variables name are set to be in capitalized SNAKE_CASE

// Minimum required price
uint256 public price;
Expand All @@ -131,11 +123,11 @@ contract AgentMech is OlasMech {
mapping(address => uint256) public mapNonces;

/// @dev AgentMech constructor.
/// @param _mechManager Mech manager address.
/// @param _mechMarketplace Mech marketplace address.
/// @param _serviceRegistry Address of the token contract.
/// @param _serviceId Service Id.
/// @param _price The minimum required price.
constructor(address _mechManager, address _serviceRegistry, uint256 _serviceId, uint256 _price)
constructor(address _mechMarketplace, address _serviceRegistry, uint256 _serviceId, uint256 _price)
OlasMech(_serviceRegistry, _serviceId)
{
// Check for the token to have the owner
Expand All @@ -144,7 +136,7 @@ contract AgentMech is OlasMech {
revert AgentNotFound(_serviceId);
}

mechManager = _mechManager;
mechMarketplace = _mechMarketplace;
// Record the price
price = _price;

Expand Down Expand Up @@ -252,10 +244,9 @@ contract AgentMech is OlasMech {

/// @dev Delivers a request.
/// @notice This function ultimately calls mech marketplace contract to finalize the delivery.
/// @param mechMarketplace Mech marketplace address.
/// @param requestId Request id.
/// @param data Self-descriptive opaque data-blob.
function _deliver(address mechMarketplace, uint256 requestId, bytes memory data) internal returns (bytes memory requestData) {
function _deliver(uint256 requestId, bytes memory data) internal returns (bytes memory requestData) {
// Get an account to deliver request to
address account = mapRequestAddresses[requestId];

Expand Down Expand Up @@ -301,7 +292,7 @@ contract AgentMech is OlasMech {
function requestFromMarketplace(address account, uint256 payment, bytes memory data, uint256 requestId) external {
// TODO Shall the mech marketplace be checked for being whitelisted by mechManager? Now it's enforced
// Check for marketplace access
if (!IMechManager(mechManager).mapMechMarketplaces(msg.sender)) {
if (msg.sender != mechMarketplace) {
revert MarketplaceNotAuthorized(msg.sender);
}

Expand All @@ -314,7 +305,7 @@ contract AgentMech is OlasMech {
/// @param requestId Request Id.
function revokeRequest(uint256 requestId) external {
// Check for marketplace access
if (!IMechManager(mechManager).mapMechMarketplaces(msg.sender)) {
if (msg.sender != mechMarketplace) {
revert MarketplaceNotAuthorized(msg.sender);
}

Expand All @@ -332,13 +323,11 @@ contract AgentMech is OlasMech {

/// @dev Delivers a request by a marketplace.
/// @notice This function ultimately calls mech marketplace contract to finalize the delivery.
/// @param mechMarketplace Mech marketplace address.
/// @param requestId Request id.
/// @param data Self-descriptive opaque data-blob.
/// @param mechStakingInstance Mech staking instance address (optional).
/// @param mechServiceId Mech operator service Id.
function deliverToMarketplace(
address mechMarketplace,
uint256 requestId,
bytes memory data,
address mechStakingInstance,
Expand All @@ -350,13 +339,8 @@ contract AgentMech is OlasMech {
}
_locked = 2;

// Check for marketplace access
if (!IMechManager(mechManager).mapMechMarketplaces(mechMarketplace)) {
revert MarketplaceNotAuthorized(mechMarketplace);
}

// Request delivery
bytes memory requestData = _deliver(mechMarketplace, requestId, data);
bytes memory requestData = _deliver(requestId, data);

// Mech marketplace delivery finalization if the request was not delivered already
if (requestData.length > 0) {
Expand Down
3 changes: 2 additions & 1 deletion contracts/Karma.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ contract Karma {
// Contract owner
address public owner;

// TODO This must be fetched from mech manager and be removed / inaccessible from here
// Mapping of whitelisted marketplaces
mapping(address => bool) public mapMechMarketplaces;
// Mapping of mech address => karma
Expand Down Expand Up @@ -68,6 +67,7 @@ contract Karma {
}

// Store the karma implementation address
// solhint-disable-next-line avoid-low-level-calls
assembly {

Check warning on line 71 in contracts/Karma.sol

View workflow job for this annotation

GitHub Actions / build

Avoid to use inline assembly. It is acceptable only in rare cases
sstore(KARMA_PROXY, newImplementation)
}
Expand Down Expand Up @@ -151,6 +151,7 @@ contract Karma {
/// @dev Gets the implementation address.
/// @return implementation Implementation address.
function getImplementation() external view returns (address implementation) {
// solhint-disable-next-line avoid-low-level-calls
assembly {

Check warning on line 155 in contracts/Karma.sol

View workflow job for this annotation

GitHub Actions / build

Avoid to use inline assembly. It is acceptable only in rare cases
implementation := sload(KARMA_PROXY)
}
Expand Down
6 changes: 3 additions & 3 deletions contracts/MechFactoryBasic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ contract MechFactoryBasic {
string public constant VERSION = "0.1.0";

/// @dev Registers service as a mech.
/// @param mechManager Mech manager address.
/// @param mechMarketplace Mech marketplace address.
/// @param serviceRegistry Service registry address.
/// @param serviceId Service id.
/// @param payload Mech creation payload.
/// @return mech The created mech instance address.
function createMech(
address mechManager,
address mechMarketplace,
address serviceRegistry,
uint256 serviceId,
bytes memory payload
Expand All @@ -34,7 +34,7 @@ contract MechFactoryBasic {
bytes32 salt = keccak256(abi.encode(block.timestamp, msg.sender, serviceId));

// Service multisig is isOperator() for the mech
mech = address((new AgentMech){salt: salt}(mechManager, serviceRegistry, serviceId, price));
mech = address((new AgentMech){salt: salt}(mechMarketplace, serviceRegistry, serviceId, price));

emit CreateBasicMech(mech, serviceId, price);
}
Expand Down
Loading

0 comments on commit 7c2c719

Please sign in to comment.