DEV Community

Vedant Chainani
Vedant Chainani

Posted on

Ether Wallet Smart Contract

Ether Wallet

Ethereum wallets are applications that let you interact with your Ethereum account. Think of it like an internet banking app – without the bank. Your wallet lets you read your balance, send transactions and connect to applications.

The owner's address is listed first in the contract.

address payable public owner;
Enter fullscreen mode Exit fullscreen mode

The address has the designation payable so that ether can be sent to it.

The owner variable in the constructor is set to msg.sender, or the deployer of the contract, upon initialization.

constructor() {
    owner = payable(msg.sender);
Enter fullscreen mode Exit fullscreen mode

We now have two unique functions called "receive" and "fallback" that allow the contract to accept plain ether with or without a calldata. For more information on these functions, read on.

receive() external payable {}
fallback() external payable {}
Enter fullscreen mode Exit fullscreen mode

Following this, we require a function to withdraw our ether to the address of the owner; the withdraw function aids us in doing this. It has a require statement that makes that the owner, whom we initialised in the constructor, is the one calling the method. We transfer ether from the smart contract to the msg.sender if that criterion is satisfied.

function withdraw(uint _amount) external {
    require(msg.sender == owner, "caller is not owner");
Enter fullscreen mode Exit fullscreen mode

Finally, we create a new function named getBalance, which is a view function, meaning that anyone can call it to retrieve the smart contract's balance without affecting the blockchain's current state.

function getBalance() external view returns (uint) {
    return address(this).balance;
Enter fullscreen mode Exit fullscreen mode

For Entire Code -

GitHub logo web3astronaut / Smart-Contracts

A Collection of smart contracts to aid you with your web 3 projects.

readme banner

GitHub issues GitHub contributors GitHub
Collection of deployable smart contracts for Ethereum-based chains to hasten development

πŸ“‘ About Smart Contracts

The sole venue for smart contract programmers to display their concepts and work. To get you started on your web3 adventure, this repository will offer a selection of smart contracts ranging in difficulty from beginner to expert.

πŸ—ΊοΈ How to Contribute?

  • Check out the current issues or start a new one!
  • Create a branch for each problem you are working on, fork the repository, and then commit your changes.
  • Create a Pull Request (PR), which will be promptly reviewed and given suggestions for improvements by the community.
  • Additionally, supply the solidity source files and a descriptive readme on how the smart contract works.

πŸ“ How to make a Pull Request?

  1. Make a fork of the Smart-Contracts repository first. In the top right corner, click the fork icon.

  2. Clone your new fork of the…

Top comments (0)