Skip to content

Commit

Permalink
feat(CommunityVault): add erc20 deposit function
Browse files Browse the repository at this point in the history
  • Loading branch information
0xb337r007 committed Mar 1, 2024
1 parent d02d6f7 commit 926135c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 19 deletions.
32 changes: 32 additions & 0 deletions contracts/CommunityVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,39 @@ import { CommunityOwnable } from "./CommunityOwnable.sol";
contract CommunityVault is CommunityOwnable {
using SafeERC20 for IERC20;

event ERC20Deposited(address indexed depositor, address indexed token, uint256 amount);
event ERC721Deposited(address indexed depositor, address indexed token, uint256 tokenId);

error CommunityVault_LengthMismatch();
error CommunityVault_NoRecipients();
error CommunityVault_TransferAmountZero();
error CommunityVault_ERC20TransferAmountTooBig();
error CommunityVault_DepositAmountZero();

mapping(address => uint256) public erc20TokenBalances;

constructor(address _ownerToken, address _masterToken) CommunityOwnable(_ownerToken, _masterToken) { }

/**
* @dev Allows anyone to deposit ERC20 tokens into the vault.
* @param token The address of the ERC20 token to deposit.
* @param amount The amount of tokens to deposit.
*/
function depositERC20(address token, uint256 amount) external {
if (amount == 0) {
revert CommunityVault_DepositAmountZero();
}

// Transfer tokens from the sender to this contract
IERC20(token).safeTransferFrom(msg.sender, address(this), amount);

// Update the total balance of the token in the vault
erc20TokenBalances[token] += amount;

// Emit an event for the deposit (optional, but recommended for tracking)
emit ERC20Deposited(msg.sender, token, amount);
}

/**
* @dev Transfers ERC20 tokens to a list of addresses.
* @param token The ERC20 token address.
Expand Down Expand Up @@ -50,6 +77,11 @@ contract CommunityVault is CommunityOwnable {
revert CommunityVault_TransferAmountZero();
}

if (amounts[i] > erc20TokenBalances[token]) {
revert CommunityVault_ERC20TransferAmountTooBig();
}

erc20TokenBalances[token] -= amounts[i];
IERC20(token).safeTransfer(recipients[i], amounts[i]);
}
}
Expand Down
67 changes: 48 additions & 19 deletions test/CommunityVault.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,7 @@ contract CommunityVaultTest is Test {
contract CommunityVaultBaseERC20Test is CommunityVaultTest {
function setUp() public virtual override {
CommunityVaultTest.setUp();

// mint 10 tokens to user
address user = accounts[0];
erc20Token.mint(user, 10e18);

// user transfer 10 tokens to the vault
vm.prank(user);
erc20Token.transfer(address(vault), 10e18);
erc20Token.mint(accounts[0], 10e18);
}
}

Expand All @@ -69,8 +62,6 @@ contract TransferERC20ByNonAdminTest is CommunityVaultBaseERC20Test {
}

function test_revertIfCalledByNonAdmin() public {
assertEq(erc20Token.balanceOf(address(vault)), 10e18);

uint256[] memory amounts = new uint256[](2);
amounts[0] = 1;
amounts[1] = 1;
Expand All @@ -83,13 +74,18 @@ contract TransferERC20ByNonAdminTest is CommunityVaultBaseERC20Test {
}

contract TransferERC20ByAdminTest is CommunityVaultBaseERC20Test {
uint256 depositAmount = 10e18;

function setUp() public virtual override {
CommunityVaultBaseERC20Test.setUp();

vm.startPrank(accounts[0]);
erc20Token.approve(address(vault), depositAmount);
vault.depositERC20(address(erc20Token), depositAmount);
vm.stopPrank();
}

function test_LengthMismatch() public {
assertEq(erc20Token.balanceOf(address(vault)), 10e18);

uint256[] memory amounts = new uint256[](1);
amounts[0] = 5e18;

Expand All @@ -99,8 +95,6 @@ contract TransferERC20ByAdminTest is CommunityVaultBaseERC20Test {
}

function test_TransferAmountZero() public {
assertEq(erc20Token.balanceOf(address(vault)), 10e18);

uint256[] memory amounts = new uint256[](2);
amounts[0] = 5e18;
amounts[1] = 0;
Expand All @@ -111,8 +105,6 @@ contract TransferERC20ByAdminTest is CommunityVaultBaseERC20Test {
}

function test_NoRecipients() public {
assertEq(erc20Token.balanceOf(address(vault)), 10e18);

uint256[] memory amounts = new uint256[](0);
address[] memory tmpAccounts = new address[](0);

Expand All @@ -123,15 +115,52 @@ contract TransferERC20ByAdminTest is CommunityVaultBaseERC20Test {

function test_AdminCanTransferERC20() public {
assertEq(erc20Token.balanceOf(address(vault)), 10e18);
assertEq(vault.erc20TokenBalances(address(erc20Token)), 10e18);

uint256[] memory amounts = new uint256[](2);
amounts[0] = 5e18;
amounts[1] = 5e18;
amounts[0] = 3e18;
amounts[1] = 3e18;

vm.prank(deployer);
vault.transferERC20(address(erc20Token), accounts, amounts);

assertEq(erc20Token.balanceOf(address(vault)), 4e18);
assertEq(vault.erc20TokenBalances(address(erc20Token)), 4e18);
}

function test_TransferERC20AmountTooBig() public {
assertEq(erc20Token.balanceOf(address(vault)), 10e18);
assertEq(vault.erc20TokenBalances(address(erc20Token)), 10e18);

uint256[] memory amounts = new uint256[](2);
amounts[0] = 10e18;
amounts[1] = 10e18;

vm.prank(deployer);
vm.expectRevert(CommunityVault.CommunityVault_ERC20TransferAmountTooBig.selector);
vault.transferERC20(address(erc20Token), accounts, amounts);
}
}

contract DepositERC20Test is CommunityVaultBaseERC20Test {
function testSuccessfulDepositERC20() public {
uint256 depositAmount = 5;
uint256 initialVaultBalance = erc20Token.balanceOf(address(vault));
uint256 initialTokenBalanceValue = vault.erc20TokenBalances(address(erc20Token));

vm.startPrank(accounts[0]);
erc20Token.approve(address(vault), depositAmount);
vault.depositERC20(address(erc20Token), depositAmount);
vm.stopPrank();

assertEq(erc20Token.balanceOf(address(vault)), initialVaultBalance + depositAmount);
assertEq(vault.erc20TokenBalances(address(erc20Token)), initialTokenBalanceValue + depositAmount);
}

assertEq(erc20Token.balanceOf(address(vault)), 0);
function testDepositZeroTokens() public {
vm.prank(accounts[0]);
vm.expectRevert(CommunityVault.CommunityVault_DepositAmountZero.selector);
vault.depositERC20(address(erc20Token), 0);
}
}

Expand Down

0 comments on commit 926135c

Please sign in to comment.