This smart contract allows players to collect, build, and earn rewards for their participation in the game's activities.
-
It has two main contracts,
ERC20
token contract and theVault
contract -
The ERC20-Token contract,
src/ERC20.sol
, was deployed on themarsSubnet
network at the contract address0x5DB9A7629912EBF95876228C24A848de0bfB43A9
and -
The Vault contract,
src/Vault.sol
was deployed on themarsSubnet
network at the contract address0xa4DfF80B4a1D748BF28BC4A271eD834689Ea3407
.
- DeFi Empire Smart Contract
- Overview
- Table of Contents
- Deploying my EVM subnet using the Avalanche CLI
- Added Subnet to Metamask and Connected to Injected Provider
- Deploying the Smart contract using Foundry
- Contract Details
- Vault Contract Functionalities
- Interacting with the deployed contracts address
- Usage Guidelines
- Demo Videos
- Author
- Disclaimer
ERC20.sol
Vault.sol
- The ERC20-Token contract,
src/ERC20.sol
, was deployed on themarsSubnet
network at the contract address0x5DB9A7629912EBF95876228C24A848de0bfB43A9
and - The Vault contract,
src/Vault.sol
was deployed on themarsSubnet
network at the contract address0xa4DfF80B4a1D748BF28BC4A271eD834689Ea3407
. - The vault contract is used for managing deposits and withdrawals of a specified ERC-20 token.
- It has functionalities that allows users to deposit tokens, minting shares in proportion to the deposited amount, and later withdraw a corresponding amount of tokens based on the shares they hold.
- Contract Address:
0xa4DfF80B4a1D748BF28BC4A271eD834689Ea3407
- Network: marsSubnet
- Chain ID 64643074
- Currency Symbol MARS
- License: MIT
-
The
deposit()
function allows users to deposit ERC-20 tokens into the vault. -
This function calculates the number of shares to mint based on the deposited amount and the current total supply of shares.
function deposit(uint _amount) external {
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);
}
- The
withdraw
function allows users to withdraw their tokens from the vault. - This function calculates the amount to withdraw based on the number of shares burned and the current total supply of shares.
function withdraw(uint _shares) external {
uint amount = (_shares * token.balanceOf(address(this))) / totalSupply;
_burn(msg.sender, _shares);
token.transfer(msg.sender, amount);
}
The ERC20 interface provides the functions signatures basic token operations like checking balances, transferring tokens, and approving token transfers.
interface IERC20 {
function totalSupply() external view returns (uint);
function balanceOf(address account) external view returns (uint);
function transfer(address recipient, uint amount) external returns (bool);
function allowance(
address owner,
address spender
) external view returns (uint);
function approve(address spender, uint amount) external returns (bool);
function transferFrom(
address sender,
address recipient,
uint amount
) external returns (bool);
event Transfer(address indexed from, address indexed to, uint value);
event Approval(address indexed owner, address indexed spender, uint value);
}
- Initial State
- Minting tokens
- Burning tokens
- Approving the Vault contract to spend 100000 units of the ERC20 token
- Depositing 10000 units of the ERC20 token on the Vault
- Checking Balance after deposit
- Withdrawing
- Checking Balance After Withdrawal
- Token Approval:
Before depositing, ensure that you have approved the contract to spend your ERC20 tokens. Use the ERC20
approve
function to grant the necessary permission.
function approve(address spender, uint amount) external returns (bool);
-
Deposit: Call the
deposit
function with the desired amount of tokens to mint corresponding shares. -
Withdraw: Call the
withdraw
function with the number of shares to burn and receive the proportional amount of tokens. -
Monitor Balances: Keep track of your token balances and shares to manage deposits and withdrawals effectively, by calling the
balanceOf()
function balanceOf(address account) external view returns (uint);
https://www.loom.com/share/c27c0fb724974177ad407bcead436d1e?sid=7c09cd67-4ef5-4547-b12a-7a4557b521a1
https://www.loom.com/share/6e7fa57107714ac2b544bf122a91351d?sid=3360f4f6-1079-4ac0-b064-386201829daa
Marcellus Ifeanyi @metacraftersio
- This smart contract is provided under the MIT license, and users are encouraged to review and understand the code before interacting with it, also this contract is deployed on
marsSubnet
Network (my EVM subnet using the Avalanche CLI).