Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: mech manager to control marketplaces and factories #51

Merged
merged 9 commits into from
Dec 16, 2024
35 changes: 13 additions & 22 deletions contracts/AgentMech.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

Check warning on line 2 in contracts/AgentMech.sol

View workflow job for this annotation

GitHub Actions / build

Found more than One contract per file. 2 contracts found!

import {IErrorsMech} from "./interfaces/IErrorsMech.sol";
import {IMechMarketplace} from "./interfaces/IMechMarketplace.sol";
Expand Down Expand Up @@ -47,7 +47,7 @@
}

function setUp(bytes memory initParams) public override {
require(readImmutable().length == 0, "Already initialized");

Check warning on line 50 in contracts/AgentMech.sol

View workflow job for this annotation

GitHub Actions / build

GC: Use Custom Errors instead of require statements
writeImmutable(initParams);
}

Expand Down Expand Up @@ -75,7 +75,6 @@
/// @title AgentMech - Smart contract for extending OlasMech
/// @dev A Mech that is operated by the holder of an ERC721 non-fungible token.
contract AgentMech is OlasMech {
event MechMarketplaceUpdated(address indexed mechMarketplace);
event Deliver(address indexed sender, uint256 requestId, bytes data);
event Request(address indexed sender, uint256 requestId, bytes data);
event RevokeRequest(address indexed sender, uint256 requestId);
Expand All @@ -93,9 +92,11 @@
bytes32 public constant DOMAIN_SEPARATOR_TYPE_HASH =
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
// Original domain separator value
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 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 @@ -105,8 +106,6 @@
uint256 public numTotalRequests;
// Number of total deliveries by this mech
uint256 public numTotalDeliveries;
// Mech marketplace address
address public mechMarketplace;
// Reentrancy lock
uint256 internal _locked = 1;

Expand All @@ -124,25 +123,19 @@
mapping(address => uint256) public mapNonces;

/// @dev AgentMech constructor.
/// @param _mechMarketplace Mech marketplace address.
/// @param _serviceRegistry Address of the token contract.
/// @param _serviceId Service Id.
/// @param _price The minimum required price.
/// @param _mechMarketplace Mech marketplace address.
constructor(address _serviceRegistry, uint256 _serviceId, uint256 _price, address _mechMarketplace)
constructor(address _mechMarketplace, address _serviceRegistry, uint256 _serviceId, uint256 _price)
OlasMech(_serviceRegistry, _serviceId)
{
// Check for zero address
if(_mechMarketplace == address(0)) {
revert ZeroAddress();
}

// Check for the token to have the owner
address tokenOwner = IERC721(_serviceRegistry).ownerOf(_serviceId);
if (tokenOwner == address(0)) {
revert AgentNotFound(_serviceId);
}

// Record the mech marketplace
mechMarketplace = _mechMarketplace;
// Record the price
price = _price;
Expand Down Expand Up @@ -178,15 +171,17 @@

/// @dev Registers a request.
/// @param account Requester account address.
/// @param payment Supplied request payment.
/// @param data Self-descriptive opaque data-blob.
/// @param requestId Request Id.
function _request(
address account,
uint256 payment,
bytes memory data,
uint256 requestId
) internal {
// Check the request payment
_preRequest(msg.value, requestId, data);
_preRequest(payment, requestId, data);

// Increase the requests count supplied by the sender
mapRequestCounts[account]++;
Expand Down Expand Up @@ -291,26 +286,27 @@
/// @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.
/// @param payment Supplied request payment.
/// @param data Self-descriptive opaque data-blob.
/// @param requestId Request Id.
function requestFromMarketplace(address account, bytes memory data, uint256 requestId) external payable {
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 (msg.sender != mechMarketplace) {
revert MarketplaceOnly(msg.sender, mechMarketplace);
revert MarketplaceNotAuthorized(msg.sender);
}

// Perform a request
_request(account, data, requestId);
_request(account, payment, data, requestId);
}

/// @dev Revokes the request from the mech that does not deliver it.
/// @notice Only marketplace can call this function if the request is not delivered by the chosen priority mech.
/// @param requestId Request Id.
function revokeRequest(uint256 requestId) external {
// Check for marketplace access
// Note if mechMarketplace is zero, this function must never be called
if (msg.sender != mechMarketplace) {
revert MarketplaceOnly(msg.sender, mechMarketplace);
revert MarketplaceNotAuthorized(msg.sender);
}

address account = mapRequestAddresses[requestId];
Expand Down Expand Up @@ -343,11 +339,6 @@
}
_locked = 2;

// Check for zero address
if (mechMarketplace == address(0)) {
revert ZeroAddress();
}

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

Expand Down
2 changes: 2 additions & 0 deletions contracts/Karma.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
}

// 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 @@ -150,7 +151,8 @@
/// @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
2 changes: 1 addition & 1 deletion contracts/MechFactoryBasic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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}(serviceRegistry, serviceId, price, mechMarketplace));
mech = address((new AgentMech){salt: salt}(mechMarketplace, serviceRegistry, serviceId, price));

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