Skip to content

Commit

Permalink
feat(curation): add tests for Vault
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed Jun 3, 2024
1 parent 8e7cd3d commit d192eb2
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 16 deletions.
11 changes: 10 additions & 1 deletion .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,13 @@ TheSpaceTest:testTokenShouldBeDefaulted() (gas: 323029)
TheSpaceTest:testTotalSupply() (gas: 7613)
TheSpaceTest:testUpgradeTo() (gas: 3215197)
TheSpaceTest:testWithdrawTreasury() (gas: 352672)
TheSpaceTest:testWithdrawUBI() (gas: 375819)
TheSpaceTest:testWithdrawUBI() (gas: 375819)
VaultTest:testCannotErc20TokenDepositZeroAmount() (gas: 15881)
VaultTest:testCannotNativeTokenClaimAlreadyClaimed() (gas: 154235)
VaultTest:testCannotNativeTokenClaimExpired() (gas: 49765)
VaultTest:testCannotNativeTokenClaimInvalidSignature() (gas: 60595)
VaultTest:testCannotNativeTokenClaimZeroBalance() (gas: 20155)
VaultTest:testCannotNativeTokenDepositZeroAmount() (gas: 13631)
VaultTest:testErc20TokenDeposit() (gas: 91953)
VaultTest:testNativeTokenClaim() (gas: 142834)
VaultTest:testNativeTokenDeposit() (gas: 59588)
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ deploy-snapper: clean
deploy-curation: clean
@forge create Curation --rpc-url ${ETH_RPC_URL} --private-key ${DEPLOYER_PRIVATE_KEY} --legacy --verify --etherscan-api-key ${ETHERSCAN_API_KEY}

deploy-curation-vault: clean
@forge create Vault --rpc-url ${ETH_RPC_URL} --private-key ${DEPLOYER_PRIVATE_KEY} --legacy --verify --etherscan-api-key ${ETHERSCAN_API_KEY}

## Billboard
deploy-billboard: clean
@forge create Billboard --rpc-url ${ETH_RPC_URL} --private-key ${DEPLOYER_PRIVATE_KEY} --constructor-args ${BILLBOARD_ERC20_TOKEN} ${BILLBOARD_REGISTRY_ADDRESS} ${BILLBOARD_ADMIN_ADDRESS} 504 ${BILLBOARD_LEASE_TERM} "Billboard" "BLBD" --legacy --verify --etherscan-api-key ${ETHERSCAN_API_KEY}
Expand Down
31 changes: 28 additions & 3 deletions src/Curation/IVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@ interface IVault {
* @notice ETH deposited.
* @param vaultId_ Vault ID to receive the ETH.
* @param amount_ Amount of tokens that were deposited.
* @param sender_ Address that deposited the ETH.
*/
event Deposited(bytes32 indexed vaultId_, uint256 amount_);
event Deposited(bytes32 indexed vaultId_, uint256 amount_, address indexed sender_);

/**
* @notice Tokens deposited.
* @param vaultId_ Vault ID to receive the tokens.
* @param token_ Token address.
* @param amount_ Amount of tokens that were deposited.
* @param sender_ Address that deposited the tokens.
*/
event Deposited(bytes32 indexed vaultId_, address indexed token_, uint256 amount_);
event Deposited(bytes32 indexed vaultId_, address indexed token_, uint256 amount_, address indexed sender_);

/**
* @notice ETH claimed.
Expand Down Expand Up @@ -76,7 +78,7 @@ interface IVault {
event SignerChanged(address indexed signer_);

//////////////////////////////
/// Claim
/// Deposit
//////////////////////////////

/**
Expand All @@ -101,6 +103,29 @@ interface IVault {
*/
function deposit(bytes32 vaultId_, address token_, uint256 amount_) external;

//////////////////////////////
/// Claim
//////////////////////////////

/**
* Get the available ETH to claim of a vault.
*
* @param vaultId_ Vault ID of the claim.
*
* @return Amount of ETH available for the claim.
*/
function available(bytes32 vaultId_) external view returns (uint256);

/**
* Get the available tokens to claim of a vault.
*
* @param vaultId_ Vault ID of the claim.
* @param token_ Token address.
*
* @return Amount of tokens available for the claim.
*/
function available(bytes32 vaultId_, address token_) external view returns (uint256);

/**
* @notice Claim ETH.
*
Expand Down
38 changes: 26 additions & 12 deletions src/Curation/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ contract Vault is IVault, Ownable {

balances[vaultId_] += msg.value;

emit Deposited(vaultId_, msg.value);
emit Deposited(vaultId_, msg.value, msg.sender);
}

/// @inheritdoc IVault
Expand All @@ -58,7 +58,17 @@ contract Vault is IVault, Ownable {

erc20Balances[vaultId_][token_] += amount_;

emit Deposited(vaultId_, token_, amount_);
emit Deposited(vaultId_, token_, amount_, msg.sender);
}

/// @inheritdoc IVault
function available(bytes32 vaultId_) public view returns (uint256) {
return balances[vaultId_] - claimed[vaultId_];
}

/// @inheritdoc IVault
function available(bytes32 vaultId_, address token_) public view returns (uint256) {
return erc20Balances[vaultId_][token_] - erc20Claimed[vaultId_][token_];
}

/// @inheritdoc IVault
Expand All @@ -70,8 +80,8 @@ contract Vault is IVault, Ownable {

uint256 _balance = balances[vaultId_];
uint256 _claimed = claimed[vaultId_];
uint256 _available = _balance - _claimed;
if (_available <= 0) {
uint256 _amount = _balance - _claimed;
if (_amount <= 0) {
revert ZeroBalance();
}

Expand All @@ -83,15 +93,17 @@ contract Vault is IVault, Ownable {
}
if (executed[_hash]) {
revert AlreadyClaimed();
} else {
executed[_hash] = true;
}

// Claim ETH
claimed[vaultId_] = 0;
claimed[vaultId_] = _balance;

// Transfer tokens
emit Claimed(vaultId_, target_, _available);
emit Claimed(vaultId_, target_, _amount);

require(payable(target_).send(_balance), "Failed ETH transfer");
require(payable(target_).send(_amount), "Failed ETH transfer");
}

/// @inheritdoc IVault
Expand All @@ -112,8 +124,8 @@ contract Vault is IVault, Ownable {
// Check available balance
uint256 _balance = erc20Balances[vaultId_][token_];
uint256 _claimed = erc20Claimed[vaultId_][token_];
uint256 _available = _balance - _claimed;
if (_available <= 0) {
uint256 _amount = _balance - _claimed;
if (_amount <= 0) {
revert ZeroBalance();
}

Expand All @@ -125,15 +137,17 @@ contract Vault is IVault, Ownable {
}
if (executed[_hash]) {
revert AlreadyClaimed();
} else {
executed[_hash] = true;
}

// Claim the given tokens
erc20Claimed[vaultId_][token_] = 0;
erc20Claimed[vaultId_][token_] = _balance;

// Transfer tokens
emit Claimed(vaultId_, token_, target_, _available);
emit Claimed(vaultId_, token_, target_, _amount);

require(IERC20(token_).transfer(target_, _balance), "Failed token transfer");
require(IERC20(token_).transfer(target_, _amount), "Failed token transfer");
}

/// @inheritdoc IVault
Expand Down
Loading

0 comments on commit d192eb2

Please sign in to comment.