Skip to content

Commit

Permalink
refactor: extract create into MM
Browse files Browse the repository at this point in the history
  • Loading branch information
kupermind committed Dec 12, 2024
1 parent d1bfb10 commit 4d9087c
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 146 deletions.
29 changes: 0 additions & 29 deletions contracts/AgentFactory.sol

This file was deleted.

39 changes: 0 additions & 39 deletions contracts/AgentMech.sol
Original file line number Diff line number Diff line change
Expand Up @@ -288,23 +288,6 @@ contract AgentMech is OlasMech {
emit Deliver(msg.sender, requestId, requestData);
}

/// @dev Registers a request without a marketplace.
/// @notice Interface provided for backwards compatibility only.
/// @param data Self-descriptive opaque data-blob.
/// @return requestId Request Id.
function request(bytes memory data) external payable returns (uint256 requestId) {
if (mechMarketplace != address(0)) {
revert MarketplaceExists(mechMarketplace);
}

// Get the local request Id
requestId = getRequestId(msg.sender, data, mapNonces[msg.sender]);
mapNonces[msg.sender]++;

// Perform a request
_request(msg.sender, data, requestId);
}

/// @dev Registers a request by a marketplace.
/// @notice This function is called by the marketplace contract since this mech was specified as a priority one.
/// @param account Requester account address.
Expand Down Expand Up @@ -342,28 +325,6 @@ contract AgentMech is OlasMech {
emit RevokeRequest(account, requestId);
}

/// @dev Delivers a request without a marketplace.
/// @notice Interface provided for backwards compatibility only.
/// @param requestId Request id.
/// @param data Self-descriptive opaque data-blob.
function deliver(uint256 requestId, bytes memory data) external onlyOperator {
// Reentrancy guard
if (_locked > 1) {
revert ReentrancyGuard();
}
_locked = 2;

// Check for the marketplace existence
if (mechMarketplace != address(0)) {
revert MarketplaceExists(mechMarketplace);
}

// Request delivery
_deliver(requestId, data);

_locked = 1;
}

/// @dev Delivers a request by a marketplace.
/// @notice This function ultimately calls mech marketplace contract to finalize the delivery.
/// @param requestId Request id.
Expand Down
41 changes: 41 additions & 0 deletions contracts/MechFactoryBasic.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import {AgentMech} from "./AgentMech.sol";

/// @title Mech Factory Basic - Periphery smart contract for managing basic mech creation
contract MechFactoryBasic {
event CreateBasicMech(address indexed mech, uint256 indexed serviceId, uint256 indexed price);

// Agent factory version number
string public constant VERSION = "0.1.0";

/// @dev Registers service as a mech.
/// @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 mechMarketplace,
address serviceRegistry,
uint256 serviceId,
bytes memory payload
) external returns (address mech) {
// Check payload length
if (payload.length != 32) {
revert();
}

// Decode price
uint256 price = abi.decode(payload, (uint256));

// Get salt
bytes32 salt = keccak256(abi.encode(block.timestamp, msg.sender, serviceId));

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

emit CreateBasicMech(mech, serviceId, price);
}
}
28 changes: 17 additions & 11 deletions contracts/MechMarketplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ import {IMech} from "./interfaces/IMech.sol";
import {IServiceRegistry} from "./interfaces/IServiceRegistry.sol";
import {IStaking, IStakingFactory} from "./interfaces/IStaking.sol";

interface IMechFactory {
/// @dev Registers service as a mech.
/// @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 mechMarketplace, address serviceRegistry, uint256 serviceId, bytes memory payload)
external returns (address mech);
}

// Mech delivery info struct
struct MechDelivery {
// Priority mech address
Expand All @@ -22,7 +33,7 @@ struct MechDelivery {

/// @title Mech Marketplace - Marketplace for posting and delivering requests served by agent mechs
contract MechMarketplace is IErrorsMarketplace {
event CreateMech(address indexed mech, uint256 indexed agentId, uint256 indexed price);
event CreateMech(address indexed mech, uint256 indexed serviceId);
event MarketplaceRequest(address indexed requester, address indexed requestedMech, uint256 requestId, bytes data);
event MarketplaceDeliver(address indexed priorityMech, address indexed actualMech, address indexed requester,
uint256 requestId, bytes data);
Expand All @@ -35,7 +46,7 @@ contract MechMarketplace is IErrorsMarketplace {
}

// Contract version number
string public constant VERSION = "1.0.0";
string public constant VERSION = "1.1.0";
// Domain separator type hash
bytes32 public constant DOMAIN_SEPARATOR_TYPE_HASH =
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
Expand Down Expand Up @@ -133,17 +144,12 @@ contract MechMarketplace is IErrorsMarketplace {

/// @dev Registers service as a mech.
/// @param serviceId Service id.
/// @param price Minimum required payment the agent accepts.
/// @param mechFactory Mech factory address.
/// @return mech The created mech instance address.
function create(uint256 serviceId, uint256 price) external returns (address mech) {
// Get service multisig address
(, address multisig, , , , , ) = IServiceRegistry(serviceRegistry).mapServices(serviceId);

bytes32 salt = keccak256(abi.encode(multisig, serviceId));
// multisig is isOperator() for the mech
mech = address((new AgentMech){salt: salt}(serviceRegistry, serviceId, price, address(this)));
function create(uint256 serviceId, address mechFactory, bytes memory payload) external returns (address mech) {
mech = IMechFactory(mechFactory).createMech(address(this), serviceRegistry, serviceId, payload);

emit CreateMech(mech, serviceId, price);
emit CreateMech(mech, serviceId);
}

/// @dev Registers a request.
Expand Down
67 changes: 0 additions & 67 deletions contracts/integrations/nevermined/AgentFactorySubscription.sol

This file was deleted.

44 changes: 44 additions & 0 deletions contracts/integrations/nevermined/MechFactorySubscription.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import {AgentMechSubscription} from "./AgentMechSubscription.sol";

/// @title Mech Factory Subscription - Periphery smart contract for managing subscription mech creation
contract MechFactorySubscription {
event CreateSubscriptionMech(address indexed mech, uint256 indexed serviceId, uint256 minCreditsPerRequest,
address indexed subscriptionNFT, uint256 subscriptionTokenId);

// Agent factory version number
string public constant VERSION = "0.1.0";

/// @dev Registers service as a mech.
/// @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 mechMarketplace,
address serviceRegistry,
uint256 serviceId,
bytes memory payload
) external returns (address mech) {
// Check payload length
if (payload.length != 32) {
revert();
}

// Decode subscription parameters
(uint256 minCreditsPerRequest, address subscriptionNFT, uint256 subscriptionTokenId) =
abi.decode(payload, (uint256, address, uint256));

// Get salt
bytes32 salt = keccak256(abi.encode(block.timestamp, msg.sender, serviceId));

// Service multisig is isOperator() for the mech
mech = address((new AgentMechSubscription){salt: salt}(serviceRegistry, serviceId, minCreditsPerRequest,
subscriptionNFT, subscriptionTokenId, mechMarketplace));

emit CreateSubscriptionMech(mech, serviceId, minCreditsPerRequest, subscriptionNFT, subscriptionTokenId);
}
}

0 comments on commit 4d9087c

Please sign in to comment.