Skip to content

Commit

Permalink
✨ Vault beginnings
Browse files Browse the repository at this point in the history
  • Loading branch information
transmissions11 committed Jun 26, 2021
1 parent 37db726 commit 0cf854f
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 149 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Dapp Build Config.
export DAPP_BUILD_OPTIMIZE=1
export DAPP_BUILD_OPTIMIZE_RUNS=1000000000
export DAPP_TEST_FUZZ_RUNS=5000

# Install, update, build and test everything.
all: solc install update build test
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

- First, install Nix:

```sh
```
# User must be in sudoers
curl -L https://nixos.org/nix/install | sh
Expand All @@ -29,7 +29,7 @@ curl -L https://nixos.org/nix/install | sh

- Then, install dapptools:

```sh
```
curl https://dapp.tools/install | sh
```

Expand All @@ -54,15 +54,15 @@ make

#### Compiling

```sh
```
make build # or 'dapp build'
```

Compiles the project.

#### Testing

```sh
```
make test # or 'dapp test --verbosity 1'
```

Expand All @@ -72,14 +72,14 @@ Test the project and only log verbose info for failed tests.

Test the project and log verbose info for everything.

```sh
```
make vtest # or 'dapp test --verbosity 3'
```

#### Debugging

```sh
mak debug # or 'dapp debug'
```
make debug # or 'dapp debug'
```

Run a test using the HEVM interactive debugger.
25 changes: 24 additions & 1 deletion src/Vault.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.6;

import "./libraries/ERC20.sol";
import "./external/ERC20.sol";
import "./libraries/StringConcat.sol";

/// @title Fuse Vault/fvToken
/// @author TransmissionsDev + JetJadeja
/// @notice Yield bearing token that enables users to swap their
/// underlying asset for fvTokens to instantly begin earning yield.
contract Vault is ERC20 {
/// @notice The underlying token for the vault.
ERC20 public immutable underlying;
Expand All @@ -20,4 +24,23 @@ contract Vault is ERC20 {
{
underlying = _underlying;
}

/// @notice Deposits an underlying token and mints fvTokens.
/// @param amount The amount of the underlying token to deposit.
function deposit(uint256 amount) external {
_mint(msg.sender, amount);

// Transfer in underlying tokens from the sender.
underlying.transferFrom(msg.sender, address(this), amount);
}

/// @notice Burns fvTokens and sends underlying tokens to the sender.
/// @param amount The amount of fvTokens to burn.
function withdraw(uint256 amount) external {
// This will revert if the user does not have enough fvTokens.
_burn(msg.sender, amount);

// Transfer underlying tokens to the sender.
underlying.transfer(msg.sender, amount);
}
}
142 changes: 10 additions & 132 deletions src/libraries/ERC20.sol → src/external/ERC20.sol
Original file line number Diff line number Diff line change
@@ -1,129 +1,7 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/*
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}

function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}

/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);

/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);

/**
* @dev Moves `amount` tokens from the caller's account to `recipient`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address recipient, uint256 amount) external returns (bool);

/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender)
external
view
returns (uint256);

/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);

/**
* @dev Moves `amount` tokens from `sender` to `recipient` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool);

/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);

/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
}

/**
* @dev Interface for the optional metadata functions from the ERC20 standard.
*
* _Available since v4.1._
*/
interface IERC20Metadata is IERC20 {
/**
* @dev Returns the name of the token.
*/
function name() external view returns (string memory);

/**
* @dev Returns the symbol of the token.
*/
function symbol() external view returns (string memory);

/**
* @dev Returns the decimals places of the token.
*/
function decimals() external view returns (uint8);
}
import "../interfaces/IERC20.sol";

/**
* @dev Implementation of the {IERC20} interface.
Expand All @@ -149,7 +27,7 @@ interface IERC20Metadata is IERC20 {
* functions have been added to mitigate the well-known issues around setting
* allowances. See {IERC20-approve}.
*/
contract ERC20 is Context, IERC20, IERC20Metadata {
contract ERC20 is IERC20, IERC20Metadata {
mapping(address => uint256) private _balances;

mapping(address => mapping(address => uint256)) private _allowances;
Expand Down Expand Up @@ -239,7 +117,7 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
override
returns (bool)
{
_transfer(_msgSender(), recipient, amount);
_transfer(msg.sender, recipient, amount);
return true;
}

Expand Down Expand Up @@ -269,7 +147,7 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
override
returns (bool)
{
_approve(_msgSender(), spender, amount);
_approve(msg.sender, spender, amount);
return true;
}

Expand All @@ -293,13 +171,13 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
) public virtual override returns (bool) {
_transfer(sender, recipient, amount);

uint256 currentAllowance = _allowances[sender][_msgSender()];
uint256 currentAllowance = _allowances[sender][msg.sender];
require(
currentAllowance >= amount,
"ERC20: transfer amount exceeds allowance"
);
unchecked {
_approve(sender, _msgSender(), currentAllowance - amount);
_approve(sender, msg.sender, currentAllowance - amount);
}

return true;
Expand All @@ -323,9 +201,9 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
returns (bool)
{
_approve(
_msgSender(),
msg.sender,
spender,
_allowances[_msgSender()][spender] + addedValue
_allowances[msg.sender][spender] + addedValue
);
return true;
}
Expand All @@ -349,13 +227,13 @@ contract ERC20 is Context, IERC20, IERC20Metadata {
virtual
returns (bool)
{
uint256 currentAllowance = _allowances[_msgSender()][spender];
uint256 currentAllowance = _allowances[msg.sender][spender];
require(
currentAllowance >= subtractedValue,
"ERC20: decreased allowance below zero"
);
unchecked {
_approve(_msgSender(), spender, currentAllowance - subtractedValue);
_approve(msg.sender, spender, currentAllowance - subtractedValue);
}

return true;
Expand Down
Loading

0 comments on commit 0cf854f

Please sign in to comment.