Skip to content

Commit

Permalink
feat: v1
Browse files Browse the repository at this point in the history
  • Loading branch information
maxencerb committed Oct 10, 2024
1 parent 8ce64fa commit cc79783
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 67 deletions.
49 changes: 49 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
Copyright (c)-2024 ADDMA (Mangrove Association). All rights reserved.

The files in this repo are covered by different licenses:

* All code is licensed under the MIT License, with the exception of 3rd party code.

Each file header contains the SPDX license identifier of the license for that file.
For more information about SPDX, please refer to <https://spdx.dev/>.

License texts:

-------------------------------------------------------------------------------

MIT License

Copyright (c) 2024 ADDMA (Mangrove Association)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-------------------------------------------------------------------------------
Zeppelin Group Ltd LICENSE
-------------------------------------------------------------------------------

The MIT License (MIT)

Copyright (c) 2016-2024 Zeppelin Group Ltd

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
117 changes: 64 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,66 +1,77 @@
## Foundry
# MangrovePoints

**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**
MangrovePoints is a smart contract designed to keep track of the custom maker contract links with their owners. This contract allows for the management of operators for accounts, providing a flexible and secure way to handle permissions.

Foundry consists of:
## How to Use

- **Forge**: Ethereum testing framework (like Truffle, Hardhat and DappTools).
- **Cast**: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data.
- **Anvil**: Local Ethereum node, akin to Ganache, Hardhat Network.
- **Chisel**: Fast, utilitarian, and verbose solidity REPL.
To use the MangrovePoints contract in your custom maker contract, follow these steps:

## Documentation
1. Include the IMangrovePoints interface in your contract:

https://book.getfoundry.sh/

## Usage

### Build

```shell
$ forge build
```solidity
import {IMangrovePoints} from "./IMangrovePoints.sol";
```

### Test
2. Call the `setOperator` method directly from your contract:

```shell
$ forge test
```solidity
IMangrovePoints mangrovePoints = IMangrovePoints(MANGROVE_POINTS_ADDRESS);
mangrovePoints.setOperator(address(this), OPERATOR_ADDRESS);
```

### Format

```shell
$ forge fmt
Replace `MANGROVE_POINTS_ADDRESS` with the deployed address of the MangrovePoints contract, and `OPERATOR_ADDRESS` with the address you want to set as the operator for your contract.

## Mangrove's Role

While the primary method for setting operators is through the custom maker contracts themselves, Mangrove also has the ability to add operators in cases where the contract may have missed calling this function. However, this is subject to certain conditions:

- There must be a proof linking the custom maker contract to the admin, deployer, or other authorized entity.
- It will be at Mangrove's discretion to accept or refuse any requests for operator assignment.

This feature ensures that legitimate contracts are not left without operators due to oversight or technical issues, while maintaining the integrity of the system.

## How MangrovePoints Works

The MangrovePoints contract is built on OpenZeppelin's Ownable contract and implements the IMangrovePoints interface. Here's an overview of its functionality:

```sol
contract MangrovePoints is Ownable(msg.sender), IMangrovePoints {
/**
* @inheritdoc IMangrovePoints
*/
mapping(address => address) public operators;
/**
* @notice Checks if the caller is allowed to change the operator for an account
* @param account The account for which the operator change is being checked
* @return canSet True if the caller is allowed to change the operator, false otherwise
* @dev The caller is allowed if they are the account itself (when no operator is set) or the current operator
*/
function _isAllowedToChangeOperatorFor(address account) internal view returns (bool canSet) {
address currentOperator = operators[account];
canSet = currentOperator == address(0) ? msg.sender == account : msg.sender == currentOperator;
}
/**
* @inheritdoc IMangrovePoints
* @dev If the caller is not allowed to change the operator, it falls back to checking if the caller is the owner
*/
function setOperator(address account, address operator) public {
if (!_isAllowedToChangeOperatorFor(account)) {
_checkOwner();
}
operators[account] = operator;
emit OperatorSet(account, operator);
}
}
```

### Gas Snapshots

```shell
$ forge snapshot
```

### Anvil

```shell
$ anvil
```
1. The contract maintains a mapping of accounts to their operators.
2. The `setOperator` function allows for setting or changing the operator for an account.
3. There are checks in place to ensure that only authorized entities can change operators:
- The account itself can set its operator if no operator is currently set.
- The current operator can change the operator.
- The owner of the MangrovePoints contract can set operators as a fallback.
4. When an operator is set or changed, an `OperatorSet` event is emitted.

### Deploy

```shell
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>
```

### Cast

```shell
$ cast <subcommand>
```

### Help

```shell
$ forge --help
$ anvil --help
$ cast --help
```
The contract uses an internal function `_isAllowedToChangeOperatorFor` to determine if the caller is allowed to change the operator for a given account.
1 change: 1 addition & 0 deletions remappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/
29 changes: 29 additions & 0 deletions src/IMangrovePoints.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

/**
* @title IMangrovePoints
* @notice Interface for the MangrovePoints contract
*/
interface IMangrovePoints {
/**
* @notice Emitted when an operator is set for an account
* @param account The account for which the operator is set
* @param operator The address of the operator
*/
event OperatorSet(address indexed account, address indexed operator);

/**
* @notice Sets the operator for an account
* @param account The account for which to set the operator
* @param operator The address to set as the operator
*/
function setOperator(address account, address operator) external;

/**
* @notice Returns the operator for a given account
* @param account The account to check
* @return The address of the operator for the given account
*/
function operators(address account) external view returns (address);
}
38 changes: 32 additions & 6 deletions src/MangrovePoints.sol
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract MangrovePoints {
uint256 public number;
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {IMangrovePoints} from "./IMangrovePoints.sol";

function setNumber(uint256 newNumber) public {
number = newNumber;
/**
* @title MangrovePoints
* @notice A contract for managing operators for accounts
* @dev Inherits from OpenZeppelin's Ownable contract
*/
contract MangrovePoints is Ownable(msg.sender), IMangrovePoints {
/**
* @inheritdoc IMangrovePoints
*/
mapping(address => address) public operators;

/**
* @notice Checks if the caller is allowed to change the operator for an account
* @param account The account for which the operator change is being checked
* @return canSet True if the caller is allowed to change the operator, false otherwise
* @dev The caller is allowed if they are the account itself (when no operator is set) or the current operator
*/
function _isAllowedToChangeOperatorFor(address account) internal view returns (bool canSet) {
address currentOperator = operators[account];
canSet = currentOperator == address(0) ? msg.sender == account : msg.sender == currentOperator;
}

function increment() public {
number++;
/**
* @inheritdoc IMangrovePoints
* @dev If the caller is not allowed to change the operator, it falls back to checking if the caller is the owner
*/
function setOperator(address account, address operator) public {
if (!_isAllowedToChangeOperatorFor(account)) {
_checkOwner();
}
operators[account] = operator;
emit OperatorSet(account, operator);
}
}
46 changes: 38 additions & 8 deletions test/MangrovePoints.t.sol
Original file line number Diff line number Diff line change
@@ -1,23 +1,53 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
pragma solidity ^0.8.20;

import {Test, console} from "forge-std/Test.sol";
import {MangrovePoints} from "../src/MangrovePoints.sol";
import {IMangrovePoints, MangrovePoints, Ownable} from "../src/MangrovePoints.sol";

contract MangrovePointsTest is Test {
MangrovePoints public mangrovePoints;
address public owner;
address public user1;
address public user2;

function setUp() public {
owner = address(this);
user1 = address(0x1);
user2 = address(0x2);
mangrovePoints = new MangrovePoints();
}

function test_Increment() public {
mangrovePoints.increment();
assertEq(mangrovePoints.number(), 1);
function test_InitialOwnership() public view {
assertEq(mangrovePoints.owner(), owner);
}

function testFuzz_SetNumber(uint256 x) public {
mangrovePoints.setNumber(x);
assertEq(mangrovePoints.number(), x);
function test_SetOperatorByOwner() public {
mangrovePoints.setOperator(user1, user2);
assertEq(mangrovePoints.operators(user1), user2);
}

function test_SetOperatorByAccount() public {
vm.prank(user1);
mangrovePoints.setOperator(user1, user2);
assertEq(mangrovePoints.operators(user1), user2);
}

function test_SetOperatorByCurrentOperator() public {
mangrovePoints.setOperator(user1, user2);
vm.prank(user2);
mangrovePoints.setOperator(user1, address(0x3));
assertEq(mangrovePoints.operators(user1), address(0x3));
}

function test_FailSetOperatorByUnauthorized() public {
vm.prank(user2);
vm.expectRevert(abi.encodeWithSelector(Ownable.OwnableUnauthorizedAccount.selector, user2));
mangrovePoints.setOperator(user1, user2);
}

function test_EmitOperatorSetEvent() public {
vm.expectEmit(true, true, false, true);
emit IMangrovePoints.OperatorSet(user1, user2);
mangrovePoints.setOperator(user1, user2);
}
}

0 comments on commit cc79783

Please sign in to comment.