Coder Social home page Coder Social logo

avalanche-subnet's Introduction

DeFi Empire Smart Contract

Overview

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 the Vault contract

  • The ERC20-Token contract, src/ERC20.sol, was deployed on the mySubnet network at the contract address 0x5aa01B3b5877255cE50cc55e8986a7a5fe29C70e and

  • The Vault contract, src/Vault.sol was deployed on the mySubnet network at the contract address0x4Ac1d98D9cEF99EC6546dEd4Bd550b0b287aaD6D.

The ERC20-TOken and Vault Contract

  • The ERC20-Token contract, src/ERC20.sol, was deployed on the mySubnet network at the contract address 0x5aa01B3b5877255cE50cc55e8986a7a5fe29C70e and
  • The Vault contract, src/Vault.sol was deployed on the mySubnet network at the contract address0x4Ac1d98D9cEF99EC6546dEd4Bd550b0b287aaD6D.
  • 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.

Vault Contract Functionalities

Deposit

  • 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);
    }

Withdraw

  • 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);
    }

ERC-20 Interface

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);
}

Usage Guidelines

  1. 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);
  1. Deposit: Call the deposit function with the desired amount of tokens to mint corresponding shares.

  2. Withdraw: Call the withdraw function with the number of shares to burn and receive the proportional amount of tokens.

  3. 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);

Author

Ayoade Abdulrahman @metacraftersio

Disclaimer

  • 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 mySubnet Network (my EVM subnet using the Avalanche CLI).

avalanche-subnet's People

Contributors

ayoade96 avatar

Watchers

 avatar

Forkers

dashbaord202401

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.