Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
Add tests for AccountsDepositWithdrawEndowments facet (#206)
Browse files Browse the repository at this point in the history
* Create initial test

* Add DummyWMatic contract

* Add endow closed & InvalidSplit test cases

* Add comment

* Create dummyWMATIC deploy func

* Create consts for valid/invalid requests

* Add missing revert cases

* Remove unused deployDummyWMATIC

* Increase value to 10k

* Add test case for deposit with no lock amt

* Add test case for deposit with lock amt

* Remove redundant index fund deployment

* Make DonationMatch inherit IDonationMatching

* Add donation match test cases

* Remove redundant endowment update in processTokenDeposit

* Rename donation match tests

* Add skip donation match tests

* Add skip donation match tests

* Add missing wmatic deposit assertion

* Add bps cases

* Add missing expect no donation match

* Refactor how splitToLiquid is read from registrar

* Start work on new test cases when non-index-fund sends requests

* Wrap no-locked-amt deposit tests in describe

* Add splitToLiquid config to Registrar

* Add new 'describe' for charities tests

* Add remaining test cases

* Separate tests by endow type

* Add missing revert case for depositMatic (when WMATIC deposit fails)

* Add reversal cases for depositERC20

* Organize depositMatic tests by endow type

* Copy paste remaining test cases (commented out)

* Remove redundatn 'locked amount' describe

* Add index fund sent tests for depositERC20

* Fix+refactor depositMatic tests

* Add non-index-fund depositERC20 tests

* Fix withdraw to beneficiaryEndowId

* Add basic revert cases

* Remove dots from revert messages

* Add more revert cases (in tokens. loop)

* Add reverting test cases for sending tokens to beneficiary

* Add successful pass test cases

* Add comment

* Add test cases including earlyLockedWithdrawFee for both charity&normal.endow

* Fix tests

* Remove redundant comments/logs from contracts

* Revert contract-address.json changes

* Test fixes to accommodate SafeERC20

* Remove redundant new line change

* Make cur. bal check be >= instead of just >

* Remove duplicate VaultType enum
  • Loading branch information
Nenad Misic authored Jul 19, 2023
1 parent 31e9abc commit cb32d8c
Show file tree
Hide file tree
Showing 5 changed files with 2,129 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ contract AccountsDepositWithdrawEndowments is
AccountStorage.State storage state = LibAccounts.diamondStorage();
AccountStorage.Endowment storage tempEndowment = state.ENDOWMENTS[details.id];

require(tokenAddress != address(0), "Invalid ERC20 token");
require(details.lockedPercentage + details.liquidPercentage == 100, "InvalidSplit");

RegistrarStorage.Config memory registrar_config = IRegistrar(state.config.registrarContract)
Expand All @@ -109,22 +108,19 @@ contract AccountsDepositWithdrawEndowments is
);
amount = amount.sub(depositFeeAmount);


IERC20(tokenAddress).safeTransfer(tempEndowment.depositFee.payoutAddress, depositFeeAmount);
}

uint256 lockedSplitPercent = details.lockedPercentage;
uint256 liquidSplitPercent = details.liquidPercentage;

LibAccounts.SplitDetails memory registrar_split_configs = registrar_config.splitToLiquid;

require(registrar_config.indexFundContract != address(0), "No Index Fund");

if (msg.sender != registrar_config.indexFundContract) {
if (tempEndowment.endowType == LibAccounts.EndowmentType.Charity) {
// use the Registrar default split for Charities
(lockedSplitPercent, liquidSplitPercent) = Validator.checkSplits(
registrar_split_configs,
registrar_config.splitToLiquid,
lockedSplitPercent,
liquidSplitPercent,
tempEndowment.ignoreUserSplits
Expand Down Expand Up @@ -172,7 +168,6 @@ contract AccountsDepositWithdrawEndowments is
state.STATES[details.id].balances.locked[tokenAddress] += lockedAmount;
state.STATES[details.id].balances.liquid[tokenAddress] += liquidAmount;

state.ENDOWMENTS[details.id] = tempEndowment;
emit EndowmentDeposit(details.id, tokenAddress, lockedAmount, liquidAmount);
}

Expand Down Expand Up @@ -231,7 +226,7 @@ contract AccountsDepositWithdrawEndowments is
senderAllowed = true;
}
}
require(senderAllowed, "Sender address is not listed in maturityAllowlist.");
require(senderAllowed, "Sender address is not listed in maturityAllowlist");
}
} else {
if (tempEndowment.allowlistedBeneficiaries.length > 0) {
Expand All @@ -243,7 +238,7 @@ contract AccountsDepositWithdrawEndowments is
}
require(
senderAllowed || msg.sender == tempEndowment.owner,
"Sender address is not listed in allowlistedBeneficiaries nor is it the Endowment Owner."
"Sender address is not listed in allowlistedBeneficiaries nor is it the Endowment Owner"
);
}

Expand Down Expand Up @@ -289,7 +284,7 @@ contract AccountsDepositWithdrawEndowments is
}

// ensure balance of tokens can cover the requested withdraw amount
require(current_bal > tokens[t].amnt, "InsufficientFunds");
require(current_bal >= tokens[t].amnt, "Insufficient Funds");

// calculate AP Protocol fee owed on withdrawn token amount
uint256 withdrawFeeAp = (tokens[t].amnt.mul(withdrawFeeRateAp)).div(LibAccounts.FEE_BASIS);
Expand Down Expand Up @@ -320,7 +315,10 @@ contract AccountsDepositWithdrawEndowments is

// send all tokens (less fees) to the ultimate beneficiary address/endowment
if (beneficiaryAddress != address(0)) {
IERC20(tokens[t].addr).safeTransfer(beneficiaryAddress, (amountLeftover - withdrawFeeEndow));
IERC20(tokens[t].addr).safeTransfer(
beneficiaryAddress,
(amountLeftover - withdrawFeeEndow)
);
} else {
// check endowment specified is not closed
require(
Expand All @@ -329,7 +327,11 @@ contract AccountsDepositWithdrawEndowments is
);
// Send deposit message to 100% Liquid account of an endowment
processTokenDeposit(
AccountMessages.DepositRequest({id: id, lockedPercentage: 0, liquidPercentage: 100}),
AccountMessages.DepositRequest({
id: beneficiaryEndowId,
lockedPercentage: 0,
liquidPercentage: 100
}),
tokens[t].addr,
(amountLeftover - withdrawFeeEndow)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.8.16;

import "./storage.sol";
import {DonationMatchMessages} from "./message.sol";
import {IDonationMatching} from "./IDonationMatching.sol";
import {RegistrarStorage} from "../../core/registrar/storage.sol";
import {IRegistrar} from "../../core/registrar/interfaces/IRegistrar.sol";
import {AccountMessages} from "../../core/accounts/message.sol";
Expand All @@ -27,7 +28,7 @@ interface IERC20Burnable is IERC20 {
* @dev DonationMatch contract
* The `DonationMatch` contract implements a donation matching mechanism for charity endowments and normal endowments.
*/
contract DonationMatch is Storage, Initializable {
contract DonationMatch is IDonationMatching, Storage, Initializable {
address emitterAddress;

/**
Expand Down
70 changes: 70 additions & 0 deletions contracts/test/DummyWMatic.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// SPDX-License-Identifier: UNLICENSED
// author: @misicnenad
pragma solidity ^0.8.0;

/**
* Inspired by the official testnet WMATIC code https://mumbai.polygonscan.com/address/0x9c3c9283d3e44854697cd22d3faa240cfb032889#code
*/
contract DummyWMATIC {
string public name = "Dummy Wrapped Matic";
string public symbol = "DWMATIC";
uint8 public decimals = 18;

event Approval(address indexed src, address indexed guy, uint wad);
event Transfer(address indexed src, address indexed dst, uint wad);
event Deposit(address indexed dst, uint wad);
event Withdrawal(address indexed src, uint wad);

mapping(address => uint) public balanceOf;
mapping(address => mapping(address => uint)) public allowance;

fallback() external payable {
deposit();
}

receive() external payable {
deposit();
}

function deposit() public payable {
balanceOf[msg.sender] += msg.value;
emit Deposit(msg.sender, msg.value);
}

function withdraw(uint wad) public {
require(balanceOf[msg.sender] >= wad);
balanceOf[msg.sender] -= wad;
payable(msg.sender).transfer(wad);
emit Withdrawal(msg.sender, wad);
}

function totalSupply() public view returns (uint) {
return address(this).balance;
}

function approve(address guy, uint wad) public returns (bool) {
allowance[msg.sender][guy] = wad;
emit Approval(msg.sender, guy, wad);
return true;
}

function transfer(address dst, uint wad) public returns (bool) {
return transferFrom(msg.sender, dst, wad);
}

function transferFrom(address src, address dst, uint wad) public returns (bool) {
require(balanceOf[src] >= wad);

if (src != msg.sender && allowance[src][msg.sender] != 0) {
require(allowance[src][msg.sender] >= wad);
allowance[src][msg.sender] -= wad;
}

balanceOf[src] -= wad;
balanceOf[dst] += wad;

emit Transfer(src, dst, wad);

return true;
}
}
Loading

0 comments on commit cb32d8c

Please sign in to comment.