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");
    payable(msg.sender).transfer(_amount);
}
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 Envoy-VC / Smart-Contracts

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


Top comments (0)