Skip to content

Commit

Permalink
feat(curation): add deposit functions
Browse files Browse the repository at this point in the history
  • Loading branch information
robertu7 committed Jun 3, 2024
1 parent 26a3e12 commit 8e7cd3d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 39 deletions.
70 changes: 49 additions & 21 deletions src/Curation/IVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,51 @@ interface IVault {

error ZeroAddress();

error ClaimExpired();
error ZeroAmount();

error ZeroBalance();

error NotEnoughBalance();
error ClaimExpired();

error InvalidSignature();

error AlreadyClaimed();

error TransferFailed(address token_, address account_, uint256 amount_);

//////////////////////////////
/// Event types
//////////////////////////////

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

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

/**
* @notice ETH claimed.
* @param id_ Unique ID of the claim.
* @param vaultId_ Vault ID of the claim.
* @param target_ Address to receive the tokens.
* @param amount_ Amount of tokens that were claimed.
*/
event Claimed(bytes32 indexed id_, address indexed target_, uint256 amount_);
event Claimed(bytes32 indexed vaultId_, address indexed target_, uint256 amount_);

/**
* @notice Tokens claimed.
* @param id_ Unique ID of the claim.
* @param vaultId_ Vault ID of the claim.
* @param token_ Token address.
* @param target_ Address to receive the tokens.
* @param amount_ Amount of tokens that were claimed.
*/
event Claimed(bytes32 indexed id_, address indexed token_, address indexed target_, uint256 amount_);
event Claimed(bytes32 indexed vaultId_, address indexed token_, address indexed target_, uint256 amount_);

/**
* @notice Unclaimed ETH were swept.
Expand All @@ -64,10 +79,32 @@ interface IVault {
/// Claim
//////////////////////////////

/**
* @notice Deposit ETH.
*
* @dev Throws: `ZeroAmount` error
* @dev Emits: `Deposited` events.
*
* @param vaultId_ Vault ID of the claim.
*/
function deposit(bytes32 vaultId_) external payable;

/**
* @notice Deposit tokens.
*
* @dev Throws: `ZeroAmount` error
* @dev Emits: `Deposited` events.
*
* @param vaultId_ Vault ID of the claim.
* @param token_ Token address.
* @param amount_ Amount of tokens to deposit.
*/
function deposit(bytes32 vaultId_, address token_, uint256 amount_) external;

/**
* @notice Claim ETH.
*
* @dev Throws: `NotEnoughBalance`, `ClaimExpired`, `InvalidSignature`,
* @dev Throws: `ZeroBalance`, `ClaimExpired`, `InvalidSignature`,
* or `AlreadyClaimed` error.
* @dev Emits: `Claimed` events.
*
Expand All @@ -77,21 +114,13 @@ interface IVault {
* @param v_ Signature field.
* @param r_ Signature field.
* @param s_ Signature field.
* @return success Whether the claim was successful.
*/
function claim(
bytes32 vaultId_,
address target_,
uint256 expiredAt_,
uint8 v_,
bytes32 r_,
bytes32 s_
) external returns (bool success);
function claim(bytes32 vaultId_, address target_, uint256 expiredAt_, uint8 v_, bytes32 r_, bytes32 s_) external;

/**
* @notice Claim tokens.
*
* @dev Throws: `NotEnoughBalance`, `ClaimExpired`, `InvalidSignature`
* @dev Throws: `ZeroBalance`, `ClaimExpired`, `InvalidSignature`
* or `AlreadyClaimed` error.
* @dev Emits: `Claimed` events.
*
Expand All @@ -102,7 +131,6 @@ interface IVault {
* @param v_ Signature field.
* @param r_ Signature field.
* @param s_ Signature field.
* @return success Whether the claim was successful.
*/
function claim(
bytes32 vaultId_,
Expand All @@ -112,7 +140,7 @@ interface IVault {
uint8 v_,
bytes32 r_,
bytes32 s_
) external returns (bool success);
) external;

//////////////////////////////
/// Sweep
Expand Down
50 changes: 32 additions & 18 deletions src/Curation/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pragma solidity ^0.8.20;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import "./IVault.sol";
Expand Down Expand Up @@ -37,14 +38,31 @@ contract Vault is IVault, Ownable {
}

/// @inheritdoc IVault
function claim(
bytes32 vaultId_,
address target_,
uint256 expiredAt_,
uint8 v_,
bytes32 r_,
bytes32 s_
) external returns (bool success) {
function deposit(bytes32 vaultId_) public payable {
if (msg.value <= 0) {
revert ZeroAmount();
}

balances[vaultId_] += msg.value;

emit Deposited(vaultId_, msg.value);
}

/// @inheritdoc IVault
function deposit(bytes32 vaultId_, address token_, uint256 amount_) public {
if (amount_ <= 0) {
revert ZeroAmount();
}

SafeERC20.safeTransferFrom(IERC20(token_), msg.sender, address(this), amount_);

erc20Balances[vaultId_][token_] += amount_;

emit Deposited(vaultId_, token_, amount_);
}

/// @inheritdoc IVault
function claim(bytes32 vaultId_, address target_, uint256 expiredAt_, uint8 v_, bytes32 r_, bytes32 s_) public {
// Check if the claim is expired
if (expiredAt_ < block.timestamp) {
revert ClaimExpired();
Expand All @@ -54,7 +72,7 @@ contract Vault is IVault, Ownable {
uint256 _claimed = claimed[vaultId_];
uint256 _available = _balance - _claimed;
if (_available <= 0) {
revert NotEnoughBalance();
revert ZeroBalance();
}

// Verify the signature
Expand All @@ -74,8 +92,6 @@ contract Vault is IVault, Ownable {
emit Claimed(vaultId_, target_, _available);

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

return true;
}

/// @inheritdoc IVault
Expand All @@ -87,7 +103,7 @@ contract Vault is IVault, Ownable {
uint8 v_,
bytes32 r_,
bytes32 s_
) external returns (bool success) {
) public {
// Check if the claim is expired
if (expiredAt_ < block.timestamp) {
revert ClaimExpired();
Expand All @@ -98,7 +114,7 @@ contract Vault is IVault, Ownable {
uint256 _claimed = erc20Claimed[vaultId_][token_];
uint256 _available = _balance - _claimed;
if (_available <= 0) {
revert NotEnoughBalance();
revert ZeroBalance();
}

// Verify the signature
Expand All @@ -118,12 +134,10 @@ contract Vault is IVault, Ownable {
emit Claimed(vaultId_, token_, target_, _available);

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

return true;
}

/// @inheritdoc IVault
function sweep(address target_) external onlyOwner {
function sweep(address target_) public onlyOwner {
uint256 _balance = address(this).balance;

emit Swept(target_, _balance);
Expand All @@ -132,7 +146,7 @@ contract Vault is IVault, Ownable {
}

/// @inheritdoc IVault
function sweep(address token_, address target_) external onlyOwner {
function sweep(address token_, address target_) public onlyOwner {
uint256 _balance = IERC20(token_).balanceOf(address(this));

emit Swept(token_, target_, _balance);
Expand All @@ -141,7 +155,7 @@ contract Vault is IVault, Ownable {
}

/// @inheritdoc IVault
function setSigner(address signer_) external onlyOwner {
function setSigner(address signer_) public onlyOwner {
if (signer_ == address(0)) {
revert ZeroAddress();
}
Expand Down

0 comments on commit 8e7cd3d

Please sign in to comment.