Skip to content

Commit cc79783

Browse files
committed
feat: v1
1 parent 8ce64fa commit cc79783

File tree

6 files changed

+213
-67
lines changed

6 files changed

+213
-67
lines changed

LICENSE

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
Copyright (c)-2024 ADDMA (Mangrove Association). All rights reserved.
2+
3+
The files in this repo are covered by different licenses:
4+
5+
* All code is licensed under the MIT License, with the exception of 3rd party code.
6+
7+
Each file header contains the SPDX license identifier of the license for that file.
8+
For more information about SPDX, please refer to <https://spdx.dev/>.
9+
10+
License texts:
11+
12+
-------------------------------------------------------------------------------
13+
14+
MIT License
15+
16+
Copyright (c) 2024 ADDMA (Mangrove Association)
17+
18+
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:
19+
20+
The above copyright notice and this permission notice (including the next paragraph) shall be included in all copies or substantial portions of the Software.
21+
22+
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.
23+
24+
-------------------------------------------------------------------------------
25+
Zeppelin Group Ltd LICENSE
26+
-------------------------------------------------------------------------------
27+
28+
The MIT License (MIT)
29+
30+
Copyright (c) 2016-2024 Zeppelin Group Ltd
31+
32+
Permission is hereby granted, free of charge, to any person obtaining
33+
a copy of this software and associated documentation files (the
34+
"Software"), to deal in the Software without restriction, including
35+
without limitation the rights to use, copy, modify, merge, publish,
36+
distribute, sublicense, and/or sell copies of the Software, and to
37+
permit persons to whom the Software is furnished to do so, subject to
38+
the following conditions:
39+
40+
The above copyright notice and this permission notice shall be included
41+
in all copies or substantial portions of the Software.
42+
43+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
44+
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
45+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
46+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
47+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
48+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
49+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+64-53
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,77 @@
1-
## Foundry
1+
# MangrovePoints
22

3-
**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.**
3+
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.
44

5-
Foundry consists of:
5+
## How to Use
66

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

12-
## Documentation
9+
1. Include the IMangrovePoints interface in your contract:
1310

14-
https://book.getfoundry.sh/
15-
16-
## Usage
17-
18-
### Build
19-
20-
```shell
21-
$ forge build
11+
```solidity
12+
import {IMangrovePoints} from "./IMangrovePoints.sol";
2213
```
2314

24-
### Test
15+
2. Call the `setOperator` method directly from your contract:
2516

26-
```shell
27-
$ forge test
17+
```solidity
18+
IMangrovePoints mangrovePoints = IMangrovePoints(MANGROVE_POINTS_ADDRESS);
19+
mangrovePoints.setOperator(address(this), OPERATOR_ADDRESS);
2820
```
2921

30-
### Format
31-
32-
```shell
33-
$ forge fmt
22+
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.
23+
24+
## Mangrove's Role
25+
26+
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:
27+
28+
- There must be a proof linking the custom maker contract to the admin, deployer, or other authorized entity.
29+
- It will be at Mangrove's discretion to accept or refuse any requests for operator assignment.
30+
31+
This feature ensures that legitimate contracts are not left without operators due to oversight or technical issues, while maintaining the integrity of the system.
32+
33+
## How MangrovePoints Works
34+
35+
The MangrovePoints contract is built on OpenZeppelin's Ownable contract and implements the IMangrovePoints interface. Here's an overview of its functionality:
36+
37+
```sol
38+
contract MangrovePoints is Ownable(msg.sender), IMangrovePoints {
39+
/**
40+
* @inheritdoc IMangrovePoints
41+
*/
42+
mapping(address => address) public operators;
43+
44+
/**
45+
* @notice Checks if the caller is allowed to change the operator for an account
46+
* @param account The account for which the operator change is being checked
47+
* @return canSet True if the caller is allowed to change the operator, false otherwise
48+
* @dev The caller is allowed if they are the account itself (when no operator is set) or the current operator
49+
*/
50+
function _isAllowedToChangeOperatorFor(address account) internal view returns (bool canSet) {
51+
address currentOperator = operators[account];
52+
canSet = currentOperator == address(0) ? msg.sender == account : msg.sender == currentOperator;
53+
}
54+
55+
/**
56+
* @inheritdoc IMangrovePoints
57+
* @dev If the caller is not allowed to change the operator, it falls back to checking if the caller is the owner
58+
*/
59+
function setOperator(address account, address operator) public {
60+
if (!_isAllowedToChangeOperatorFor(account)) {
61+
_checkOwner();
62+
}
63+
operators[account] = operator;
64+
emit OperatorSet(account, operator);
65+
}
66+
}
3467
```
3568

36-
### Gas Snapshots
37-
38-
```shell
39-
$ forge snapshot
40-
```
41-
42-
### Anvil
43-
44-
```shell
45-
$ anvil
46-
```
69+
1. The contract maintains a mapping of accounts to their operators.
70+
2. The `setOperator` function allows for setting or changing the operator for an account.
71+
3. There are checks in place to ensure that only authorized entities can change operators:
72+
- The account itself can set its operator if no operator is currently set.
73+
- The current operator can change the operator.
74+
- The owner of the MangrovePoints contract can set operators as a fallback.
75+
4. When an operator is set or changed, an `OperatorSet` event is emitted.
4776

48-
### Deploy
49-
50-
```shell
51-
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key>
52-
```
53-
54-
### Cast
55-
56-
```shell
57-
$ cast <subcommand>
58-
```
59-
60-
### Help
61-
62-
```shell
63-
$ forge --help
64-
$ anvil --help
65-
$ cast --help
66-
```
77+
The contract uses an internal function `_isAllowedToChangeOperatorFor` to determine if the caller is allowed to change the operator for a given account.

remappings.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/

src/IMangrovePoints.sol

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.20;
3+
4+
/**
5+
* @title IMangrovePoints
6+
* @notice Interface for the MangrovePoints contract
7+
*/
8+
interface IMangrovePoints {
9+
/**
10+
* @notice Emitted when an operator is set for an account
11+
* @param account The account for which the operator is set
12+
* @param operator The address of the operator
13+
*/
14+
event OperatorSet(address indexed account, address indexed operator);
15+
16+
/**
17+
* @notice Sets the operator for an account
18+
* @param account The account for which to set the operator
19+
* @param operator The address to set as the operator
20+
*/
21+
function setOperator(address account, address operator) external;
22+
23+
/**
24+
* @notice Returns the operator for a given account
25+
* @param account The account to check
26+
* @return The address of the operator for the given account
27+
*/
28+
function operators(address account) external view returns (address);
29+
}

src/MangrovePoints.sol

+32-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.20;
33

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

7-
function setNumber(uint256 newNumber) public {
8-
number = newNumber;
7+
/**
8+
* @title MangrovePoints
9+
* @notice A contract for managing operators for accounts
10+
* @dev Inherits from OpenZeppelin's Ownable contract
11+
*/
12+
contract MangrovePoints is Ownable(msg.sender), IMangrovePoints {
13+
/**
14+
* @inheritdoc IMangrovePoints
15+
*/
16+
mapping(address => address) public operators;
17+
18+
/**
19+
* @notice Checks if the caller is allowed to change the operator for an account
20+
* @param account The account for which the operator change is being checked
21+
* @return canSet True if the caller is allowed to change the operator, false otherwise
22+
* @dev The caller is allowed if they are the account itself (when no operator is set) or the current operator
23+
*/
24+
function _isAllowedToChangeOperatorFor(address account) internal view returns (bool canSet) {
25+
address currentOperator = operators[account];
26+
canSet = currentOperator == address(0) ? msg.sender == account : msg.sender == currentOperator;
927
}
1028

11-
function increment() public {
12-
number++;
29+
/**
30+
* @inheritdoc IMangrovePoints
31+
* @dev If the caller is not allowed to change the operator, it falls back to checking if the caller is the owner
32+
*/
33+
function setOperator(address account, address operator) public {
34+
if (!_isAllowedToChangeOperatorFor(account)) {
35+
_checkOwner();
36+
}
37+
operators[account] = operator;
38+
emit OperatorSet(account, operator);
1339
}
1440
}

test/MangrovePoints.t.sol

+38-8
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,53 @@
11
// SPDX-License-Identifier: UNLICENSED
2-
pragma solidity ^0.8.13;
2+
pragma solidity ^0.8.20;
33

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

77
contract MangrovePointsTest is Test {
88
MangrovePoints public mangrovePoints;
9+
address public owner;
10+
address public user1;
11+
address public user2;
912

1013
function setUp() public {
14+
owner = address(this);
15+
user1 = address(0x1);
16+
user2 = address(0x2);
1117
mangrovePoints = new MangrovePoints();
1218
}
1319

14-
function test_Increment() public {
15-
mangrovePoints.increment();
16-
assertEq(mangrovePoints.number(), 1);
20+
function test_InitialOwnership() public view {
21+
assertEq(mangrovePoints.owner(), owner);
1722
}
1823

19-
function testFuzz_SetNumber(uint256 x) public {
20-
mangrovePoints.setNumber(x);
21-
assertEq(mangrovePoints.number(), x);
24+
function test_SetOperatorByOwner() public {
25+
mangrovePoints.setOperator(user1, user2);
26+
assertEq(mangrovePoints.operators(user1), user2);
27+
}
28+
29+
function test_SetOperatorByAccount() public {
30+
vm.prank(user1);
31+
mangrovePoints.setOperator(user1, user2);
32+
assertEq(mangrovePoints.operators(user1), user2);
33+
}
34+
35+
function test_SetOperatorByCurrentOperator() public {
36+
mangrovePoints.setOperator(user1, user2);
37+
vm.prank(user2);
38+
mangrovePoints.setOperator(user1, address(0x3));
39+
assertEq(mangrovePoints.operators(user1), address(0x3));
40+
}
41+
42+
function test_FailSetOperatorByUnauthorized() public {
43+
vm.prank(user2);
44+
vm.expectRevert(abi.encodeWithSelector(Ownable.OwnableUnauthorizedAccount.selector, user2));
45+
mangrovePoints.setOperator(user1, user2);
46+
}
47+
48+
function test_EmitOperatorSetEvent() public {
49+
vm.expectEmit(true, true, false, true);
50+
emit IMangrovePoints.OperatorSet(user1, user2);
51+
mangrovePoints.setOperator(user1, user2);
2252
}
2353
}

0 commit comments

Comments
 (0)