This smart contract implements a basic version of an ERC20 token, which includes essential functionalities like transferring tokens, approving allowances, minting new tokens, and burning tokens. The token is named "Solidity by Example" with the symbol "SOLBYEX" and follows the standard ERC20 structure.
- Transfer Tokens: Allows token holders to transfer tokens to another address.
- Approve Allowance: Token holders can approve another address to spend tokens on their behalf.
- Transfer From: Allows the approved address to transfer tokens from the holder's account to another account.
- Mint Tokens: Allows the creation of new tokens, increasing the total supply.
- Burn Tokens: Allows the destruction of tokens, decreasing the total supply.
uint public totalSupply: Tracks the total supply of the token.mapping(address => uint) public balanceOf: Maps each address to its token balance.mapping(address => mapping(address => uint)) public allowance: Maps each address to another address's allowed spending amount.string public name = "Solidity by Example": The name of the token.string public symbol = "SOLBYEX": The symbol of the token.uint8 public decimals = 18: The number of decimals the token uses (common for ERC20 tokens).
event Transfer(address indexed from, address indexed to, uint value): Emitted when a transfer of tokens occurs.event Approval(address indexed owner, address indexed spender, uint value): Emitted when an approval is granted for an allowance.
- Transfers a specified
amountof tokens from the caller's address to therecipientaddress. - Emits a
Transferevent. - Returns
trueif the transfer is successful.
- Approves
spenderto spend a specifiedamountof tokens on behalf of the caller. - Emits an
Approvalevent. - Returns
trueif the approval is successful.
- Transfers a specified
amountof tokens fromsendertorecipientusing an allowance. - Decreases the allowance of the
msg.senderby theamounttransferred. - Emits a
Transferevent. - Returns
trueif the transfer is successful.
- Mints a specified
amountof tokens to the caller's address. - Increases the
totalSupplyand the caller's balance. - Emits a
Transferevent withfromset to the zero address.
- Burns a specified
amountof tokens from the caller's address. - Decreases the
totalSupplyand the caller's balance. - Emits a
Transferevent withtoset to the zero address.
- Deploying the Contract: Deploy the contract on the Ethereum network. The token name is "Solidity by Example" and the symbol is "SOLBYEX".
- Transferring Tokens: Call the
transferfunction to send tokens to another address. - Approving Allowances: Use the
approvefunction to allow another address to spend tokens on your behalf. - Transferring Using Allowance: Use the
transferFromfunction to transfer tokens from one address to another using the approved allowance. - Minting Tokens: Call the
mintfunction to create new tokens and increase the total supply. - Burning Tokens: Call the
burnfunction to destroy tokens and decrease the total supply.
This project is licensed under the MIT License.
The Vault smart contract is designed to interact with an ERC20 token, allowing users to deposit tokens into the vault and receive shares representing their stake. Users can also withdraw their tokens by burning these shares. The contract maintains a mapping of each user's balance of shares and tracks the total supply of shares in the vault.
- Token Deposit: Users can deposit an ERC20 token into the vault. In return, they receive shares that represent their proportional ownership of the vault.
- Token Withdrawal: Users can withdraw their tokens from the vault by burning their shares. The amount of tokens withdrawn is proportional to the number of shares burned.
- Minting & Burning: The contract mints shares when tokens are deposited and burns shares when tokens are withdrawn.
IERC20 public immutable token: The ERC20 token that the vault interacts with.uint public totalSupply: The total supply of shares in the vault.mapping(address => uint) public balanceOf: A mapping that stores the balance of shares for each address.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import "./IERC20.sol";
import "./ERC20Token.sol";
contract Vault {
IERC20 public immutable token;
uint public totalSupply;
mapping(address => uint) public balanceOf;
constructor(address _token) {
token = IERC20(_token);
}
function _mint(address _to, uint _shares) private {
totalSupply += _shares;
balanceOf[_to] += _shares;
}
function _burn(address _from, uint _shares) private {
totalSupply -= _shares;
balanceOf[_from] -= _shares;
}
function deposit(uint _amount) external {
/*
a = amount
B = balance of token before deposit
T = total supply
s = shares to mint
(T + s) / T = (a + B) / B
s = aT / B
*/
uint shares;
if (totalSupply == 0) {
shares = _amount;
} else {
shares = (_amount * totalSupply) / token.balanceOf(address(this));
}
_mint(msg.sender, shares);
token.transferFrom(msg.sender, address(this), _amount);
}
function withdraw(uint _shares) external {
/*
a = amount
B = balance of token before withdraw
T = total supply
s = shares to burn
(T - s) / T = (B - a) / B
a = sB / T
*/
uint amount = (_shares * token.balanceOf(address(this))) / totalSupply;
_burn(msg.sender, _shares);
token.transfer(msg.sender, amount);
}
}