How to create a simple “Wallet” smart contract using Solidity ?

Paulina Lewandowska

19 Oct 2022
How to create a simple “Wallet” smart contract using Solidity ?

In the previous tutorial you learned how to create your first smart contract, in this post you will learn how to create a "Wallet" smart contract to which you will be able to send and withdraw Ethereum, of course the contract will be written so that only the owner of the contract will be able to withdraw funds.

What are the keywords: public, external, internal and private ?

You have already learned the keyword public in the previous tutorial "Hello World", it means that the function is public, that is, it can be called both "outside the contract" and inside. Contracts that inherit our contract will have access to such a function.

For example:

uint counter = 0;
    function externalFunction() external  {
        counter++;
        publicFunction();
    }
 
    function publicFunction() public  {
        counter++;
    }

As you can see, the public function can not only be called "outside the smart contract", but also inside it, as in the externalFunction, and just what does this external mean ?

If the function is external, it simply means that it can only be called "outside the smart contract" , just as we called our sayHelloWorld function in the previous tutorial. Contracts that will inherit our contract will have access to the external function.

Two keywords remain : internal and private.

function returnTwoPlusTwo() internal pure returns (uint256)  {
        return 2 + 2;
    }
 
    function returnMsgSender() private view returns (address)  {
        return msg.sender;
    }

Internal is exactly the opposite of external, such a function will not be called "outside the smart contract", but only inside. As with external and public, contracts that inherit such a function will have access to it.

The last is private which means that the function will be visible only in the contract in which it was defined, contracts that inherit our contract will not be able to call it.

Pure vs View

When I showed the functions internal and private, you can see that the words pure and view are added next to these functions. What do they mean ?

When the function is pure, it means that it does not modify or read any data from the blockchain, just like the returnTwoPlusTwo function - it only returns the result of 2+2, it does not load or modify any variable in the smartcontract or blockchain.

Functions with the suffix view, means that the function does not modify data from the blockchain, but instead reads it. For example, the returnMsgSender function reads the value of the msg.sender variable, which represents the address that called the function.

Okay, enough with the theory, let's finally get to work !

How to create "Wallet" smart contract?

First we must, as always, define the version of solidity that we want to use , I will use version 0.8.12, so at the top of the file I type:

pragma solidity 0.8.12;

I am creating a contract called Wallet :

contract Wallet {
   
}

Each wallet has its own owner so will define address on ethreum, who will be the owner of the smart contract. The owner of the smart contract will be the person who will deploy our smart contract.  And we should have a variable thanks to which we can monitor the balance of the smart contract.

contract Wallet {
    address public owner;
    uint public balance;
    constructor() {
        owner = msg.sender;
    }

Let's create a function by which we can transfer funds to the smart contract.

function depositEther() external payable{
        balance += msg.value;
    }

There are two new words in this function : payable and msg.value.

If the function is payable it means that with the function call we can send ethereum to the smart contract, and msg.value is simply the amount of wei sent with the function call.

Now let's create a function by which we can withdraw the sent ethereum, of course, only the owner of the smart contract should be able to do so.

function withdrawEther(uint amount) external{
        require(msg.sender == owner, "Only owner can call this function");
        require(amount <= balance, "Amount exceeds balance");
        balance -= amount;
        payable(owner).transfer(amount);
    }

Require makes the function return an error with the message we specified when the condition given in it fails.

In the function, we had to convert the owner variable from address to address payable, because ethereum can only be sent to addresses with the keyword payable.

Summary

And that's it ! Smart contract is ready ! Go ahead and try to play with it, and if you succeed, even improve it. 

In the next tutorial in the Nextrope Academy series, we will show how to write a simple auction smart contract.

Most viewed


Never miss a story

Stay updated about Nextrope news as it happens.

You are subscribed

Nextrope Partners with Hacken to Enhance Blockchain Security

Miłosz

21 Nov 2024
Nextrope Partners with Hacken to Enhance Blockchain Security

Nextrope announces a strategic partnership with Hacken, a renowned blockchain security auditor. It marks a significant step in delivering reliable decentralized solutions. After several successful collaborations resulting in flawless smart contract audits, the alliance solidifies the synergy between Nextrope's innovative blockchain development and Hacken's top-tier security auditing services. Together, we aim to set new benchmarks, ensuring that security is an integral part of blockchain technology.

Strengthening Blockchain Security

The partnership aims to fortify the security protocols within blockchain ecosystems. By integrating Hacken's comprehensive security audits with Nextrope's cutting-edge blockchain solutions, we are poised to offer unparalleled security features in our projects.

"Blockchain security should never be an afterthought"

"Our partnership with Hacken underscores our dedication to embedding security at the core of our blockchain solutions. Together, we're building a safer future for the industry."

said Mateusz Mach, CEO of Nextrope

About Nextrope

Nextrope is a forward-thinking blockchain development house specializing in creating innovative solutions for businesses worldwide. With a team of experienced developers and blockchain experts, Nextrope delivers high-quality, scalable, and secure blockchain applications tailored to meet the unique needs of each client.

About Hacken

Hacken is a leading blockchain security auditor known for its rigorous smart contract audits and security assessments. With a mission to make the industry safer, Hacken provides complex security services that help companies identify and mitigate vulnerabilities in their applications.

Looking Ahead

As a joint mission, both Nextrope and Hacken are committed to continuous innovation. We look forward to the exciting opportunities this partnership will bring and are eager to implement a more secure blockchain environment for all.

For more information, please contact:

Nextrope

Hacken

Join us on our journey to deliver top-notch blockchain tech and a safer future for the industry!

Nextrope as Sponsor at ETH Warsaw 2024: Highlights

Miłosz

04 Oct 2024
Nextrope as Sponsor at ETH Warsaw 2024: Highlights

ETH Warsaw has established itself as a significant event in the Web3 space, gathering developers, entrepreneurs, and investors in the heart of Poland’s capital each year. The 2024 edition was filled with builders and leaders united in advancing decentralized technologies.

Leading Event of Warsaw Blockchain Week

As a blend of conference and hackathon, ETH Warsaw aims to push the boundaries of innovation. For companies and individuals eager to shape the future of tech, the premier summit during Warsaw Blockchain Week offers a unique platform to connect and collaborate.

Major Milestones in Previous Editions

  • Over 1,000 participants attended the forum
  • 222 hackers competed, showcasing groundbreaking technical skills
  • $119,920 in bounties was awarded to boost promising solution development

Key Themes at ETH Warsaw 2024

This year’s discussions were centered around shaping the adoption of blockchain. To emphasize that future implementation requires a wide range of voices, perspectives, and understanding, ETH Warsaw 2024 encouraged participation from individuals of all backgrounds. As the industry stands on the cusp of a potential bull market, building resilient products brings substantial impact. Participants mutually raised an inhibitor posed by poor architecture or suspicious practices.

Infrastructure and Scalability

  • Layer 2 (L2) solutions
  • Zero-Knowledge Proofs (ZKPs)
  • Future of Account Abstraction in Decentralized Applications (DApps)
  • Advancements in Blockchain Interoperability
  • Integration of Artificial Intelligence (AI) and Machine Learning Models (MLMs) with on-chain data

Responsibility

With the premise of robust blockchain systems, we delved into topics such as privacy, advanced security protocols, and white-hacking as essential tools for maintaining trust. Discussions also included consensus mechanisms and their role in the entire infrastructure, beginning with transparent Decentralized Autonomous Organizations (DAOs).

Legal Policies

The track on financial freedom led to the transformative potential of decentralized finance (DeFi). We tackled the challenges and opportunities of blockchain products within a rapidly evolving regulatory landscape.

Mass Adoption

Conversations surrounding accessible platforms underscored the need to simplify onboarding for new users, ultimately crafting solutions that appeal to mainstream audiences. Contributors explored ways to improve user experience (UX), enhance community management, and support Web3 startups.

ETH Legal, co-organized with PKO BP and several leading law firms, studied the implementation of the MiCA guidelines starting next year and affecting the market. It aimed to dissect the complex policies that govern digital assets.

Currently, founders navigate a patchwork of regulations that vary by jurisdiction. There is a clear need for structured protocols that ensure consumer protection and market integrity while attracting more users. Legal experts broke down the implications of existing and anticipated changes on decentralized finance (DeFi), non-fungible tokens (NFTs), business logic, and other emerging technologies.

The importance of ETH Legal extended beyond theoretical discussions. It served as a vital forum for stakeholders to connect and share insights. Thanks to input from renowned experts in the field, attendees left with a deeper understanding of the challenges ahead.

Warsaw Blockchain Week: Nextrope’s Engagement

The Warsaw Blockchain Week 2024 ensured a wide range of activities, with a packed schedule of conferences, hackathons, and networking opportunities. Nextrope actively engaged in several side events throughout the week and recognized the immense potential to foster connections.

Side Events Attended by Nextrope

  • Elympics on TON
  • Aleph Zero Opening Party
  • Cookie3 x NOKS x TON Syndicate
  • Solana House

Nextrope’s Contribution to ETH Warsaw 2024

At ETH Warsaw 2024, Nextrope proudly positioned itself as a Pond Sponsor of the conference and hackathon, reflecting the event's mission. Following a strong track record of partnerships with large financial institutions and startups, we seized the opportunity to share our reflections with the community.

Together, we continue to innovate toward a more decentralized and inclusive future. By actively participating in open conversations about regulatory and technological advancements, Nextrope solidifies its role as an exemplar of dedication, forward-thinking, and technological resources.