Skip to content

Commit

Permalink
add CommunityVault contract
Browse files Browse the repository at this point in the history
  • Loading branch information
0xb337r007 committed Nov 20, 2023
1 parent 1085ee8 commit 62f97b8
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ AddEntryTest:test_RevertWhen_SenderIsNotTokenDeployer() (gas: 14827)
CollectibleV1Test:test_Deployment() (gas: 36386)
CommunityERC20Test:test_Deployment() (gas: 35198)
CommunityTokenDeployerTest:test_Deployment() (gas: 14805)
CommunityVaultTest:test_Deployment() (gas: 10436)
CreateTest:test_Create() (gas: 2269916)
CreateTest:test_Create() (gas: 2568994)
CreateTest:test_RevertWhen_InvalidOwnerTokenAddress() (gas: 15523)
Expand Down
83 changes: 83 additions & 0 deletions contracts/CommunityVault.sol
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]);
}
}
}
16 changes: 8 additions & 8 deletions test/CollectibleV1.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ contract CollectibleV1Test is Test {
deployer = deploymentConfig.deployer();

collectibleV1 = new CollectibleV1(
name,
symbol,
maxSupply,
remoteBurnable,
transferable,
baseURI,
address(ownerToken),
address(masterToken)
name,
symbol,
maxSupply,
remoteBurnable,
transferable,
baseURI,
address(ownerToken),
address(masterToken)
);

accounts[0] = makeAddr("one");
Expand Down
47 changes: 47 additions & 0 deletions test/CommunityVault.t.sol
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));
}
}

0 comments on commit 62f97b8

Please sign in to comment.