GithubHelp home page GithubHelp logo

0xhuzaifa / simple-web3-wallet Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 1.0 72 KB

This project includes a decentralized wallet smart contract, "Wallet," for managing Ether and ERC20 tokens, and a custom ERC20 token contract, "HuziToken." Users can send and receive Ether, deposit and withdraw ERC20 tokens, and execute simplified token swaps within the wallet. It follows secure development practices and is thoroughly tested.

Solidity 16.36% JavaScript 83.64%
blockchain-technology erc20-tokens javascript solidity-smart-contract token-swap web3-wallet

simple-web3-wallet's Introduction

Project Documentation: Decentralized Wallet with Token Swapping

Overview

This project involves a decentralized wallet smart contract named Wallet, which allows users to manage their Ether and ERC20 tokens on the Ethereum blockchain. The wallet provides functionalities for receiving and sending Ether, depositing and withdrawing ERC20 tokens, and executing a simplified token swap. Additionally, the project includes a custom ERC20 token contract called HuziToken.

This document outlines the project's purpose, key components, and functionality, as well as provides setup instructions and details of the test cases.

Prerequisites

Before setting up and using the Wallet and HuziToken contracts, ensure that you have the following prerequisites installed:

Node.js and npm

If you haven't already installed Node.js and npm, you can download and install them from the official Node.js website.

Hardhat and Dependencies

You need to set up a Node.js project and install the required dependencies. Here are the steps:

  1. Initialize a new Node.js project.
npm init -y
  1. Install the development dependencies.
npm install --save-dev hardhat @nomiclabs/hardhat-ethers ethers @openzeppelin/contracts chai

Initialize a Hardhat Project

Once you have installed the required dependencies, you can set up a new Hardhat project with the following commands:

  1. Create a new Hardhat project.
npx hardhat

This command will guide you through the process of setting up your Hardhat project. When prompted, select "Create an JavaScript Hardhat" to create a basic project structure.

Now you have your project initialized and are ready to proceed with the setup of the Wallet and HuziToken contracts and running tests.

Contract Descriptions

Wallet.sol The Wallet contract manages user balances of Ether and ERC20 tokens. It provides the following key functionalities:

Receive Ether: The contract can receive Ether sent to it. The received Ether is added to the sender's balance.

Send Ether: Users can send a specified amount of Ether to another address, provided they have enough Ether in their balance.

Deposit ERC20 Tokens: Users can deposit ERC20 tokens into the wallet by approving the contract to spend a certain amount. The deposited tokens are added to the user's balance.

Withdraw ERC20 Tokens: Users can withdraw ERC20 tokens from their balance, with proper validation to ensure they have enough tokens.

Token Swap: A simplified representation of token swapping where users can exchange one ERC20 token for another at a 1:1 ratio. In real scenarios, more complex logic and external data (e.g., price feed oracles) would be needed for actual token swaps.

Additional Utility Functions: The contract provides functions to query balances of Ether and ERC20 tokens for any account.

HuziToken.sol The HuziToken contract is a custom ERC20 token. It allows for the creation of tokens with a specific name, symbol, and initial supply. This contract is used to demonstrate ERC20 token interactions within the Wallet contract.

Testing

Tests are implemented using the Hardhat testing environment, which integrates with ethers.js, chai, and ethereum-waffle for assertions.

Test Cases The test cases ensure the functionality of the Wallet contract and demonstrate its capabilities. They are organized into the following categories:

Initialization: Tests whether the deployment of the Wallet and HuziToken contracts is successful and verifies their initial state.

Ether Transactions: Verifies the functionality of sending and receiving Ether in the wallet, ensuring proper balance management for different users.

Token Transactions: Tests the depositing and withdrawing of ERC20 tokens, validating that the balances are updated correctly.

Token Swap: Demonstrates the simplified token swapping functionality, including the transfer of tokens between users.

For each test case, various before-and-after balances are captured and compared to ensure that the contract operations are successful and accurate.

Conclusion

The Wallet contract provides a foundation for managing Ethereum and ERC20 tokens within a decentralized wallet. While this project serves as an educational tool, it is essential to understand that further development, security measures, testing, and auditing are necessary to deploy it in a production environment. Users are encouraged to build upon this project, implement additional features, and consider real-world scenarios, such as integrating DEXs and price feed oracles, to create a robust and secure decentralized wallet application.

simple-web3-wallet's People

Watchers

 avatar

Forkers

vanntrong

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.