-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1085ee8
commit 62f97b8
Showing
4 changed files
with
139 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// SPDX-License-Identifier: Mozilla Public License 2.0 | ||
|
||
pragma solidity ^0.8.17; | ||
|
||
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; | ||
import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
import "@openzeppelin/contracts/token/ERC721/IERC721.sol"; | ||
import "./CommunityOwnable.sol"; | ||
|
||
/** | ||
* @title CommunityVault | ||
* @dev This contract acts as a Vault for storing ERC20 and ERC721 tokens. | ||
* It allows any user to deposit tokens into the vault. | ||
* Only community owners, as defined in the CommunityOwnable contract, have | ||
* permissions to transfer these tokens out of the vault. | ||
*/ | ||
contract CommunityVault is CommunityOwnable { | ||
using SafeERC20 for IERC20; | ||
|
||
error CommunityVault_LengthMismatch(); | ||
error CommunityVault_NoRecipients(); | ||
error CommunityVault_TransferAmountZero(); | ||
|
||
constructor(address _ownerToken, address _masterToken) CommunityOwnable(_ownerToken, _masterToken) { } | ||
|
||
/** | ||
* @dev Transfers ERC20 tokens to a list of addresses. | ||
* @param token The ERC20 token address. | ||
* @param recipients The list of recipient addresses. | ||
* @param amounts The list of amounts to transfer to each recipient. | ||
*/ | ||
function transferERC20( | ||
address token, | ||
address[] calldata recipients, | ||
uint256[] calldata amounts | ||
) | ||
external | ||
onlyCommunityOwnerOrTokenMaster | ||
{ | ||
if (recipients.length != amounts.length) { | ||
revert CommunityVault_LengthMismatch(); | ||
} | ||
|
||
if (recipients.length == 0) { | ||
revert CommunityVault_NoRecipients(); | ||
} | ||
|
||
for (uint256 i = 0; i < recipients.length; i++) { | ||
if (amounts[i] == 0) { | ||
revert CommunityVault_TransferAmountZero(); | ||
} | ||
|
||
IERC20(token).safeTransfer(recipients[i], amounts[i]); | ||
} | ||
} | ||
|
||
/** | ||
* @dev Transfers ERC721 tokens to a list of addresses. | ||
* @param token The ERC721 token address. | ||
* @param recipients The list of recipient addresses. | ||
* @param tokenIds The list of token IDs to transfer to each recipient. | ||
*/ | ||
function transferERC721( | ||
address token, | ||
address[] calldata recipients, | ||
uint256[] calldata tokenIds | ||
) | ||
external | ||
onlyCommunityOwnerOrTokenMaster | ||
{ | ||
if (recipients.length != tokenIds.length) { | ||
revert CommunityVault_LengthMismatch(); | ||
} | ||
|
||
if (recipients.length == 0) { | ||
revert CommunityVault_NoRecipients(); | ||
} | ||
|
||
for (uint256 i = 0; i < recipients.length; i++) { | ||
IERC721(token).safeTransferFrom(address(this), recipients[i], tokenIds[i]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
|
||
pragma solidity ^0.8.17; | ||
|
||
import { Test } from "forge-std/Test.sol"; | ||
import { CommunityVault } from "../contracts/CommunityVault.sol"; | ||
import { DeploymentConfig } from "../script/DeploymentConfig.s.sol"; | ||
import { DeployOwnerAndMasterToken } from "../script/DeployOwnerAndMasterToken.s.sol"; | ||
import { CommunityERC20 } from "../contracts/tokens/CommunityERC20.sol"; | ||
import { OwnerToken } from "../contracts/tokens/OwnerToken.sol"; | ||
import { MasterToken } from "../contracts/tokens/MasterToken.sol"; | ||
|
||
contract CommunityVaultTest is Test { | ||
CommunityVault internal vault; | ||
|
||
address[] internal accounts = new address[](4); | ||
address internal deployer; | ||
|
||
CommunityERC20 internal erc20Token; | ||
OwnerToken internal ownerToken; | ||
MasterToken internal masterToken; | ||
|
||
function setUp() public { | ||
DeploymentConfig deploymentConfig; | ||
DeployOwnerAndMasterToken deployment = new DeployOwnerAndMasterToken(); | ||
(ownerToken, masterToken, deploymentConfig) = deployment.run(); | ||
|
||
deployer = deploymentConfig.deployer(); | ||
|
||
erc20Token = new CommunityERC20( | ||
"Test", | ||
"TEST", | ||
18, | ||
100, | ||
"", | ||
address(ownerToken), | ||
address(masterToken) | ||
); | ||
|
||
vault = new CommunityVault(address(ownerToken), address(masterToken)); | ||
} | ||
|
||
function test_Deployment() public { | ||
assertEq(vault.ownerToken(), address(ownerToken)); | ||
assertEq(vault.masterToken(), address(masterToken)); | ||
} | ||
} |