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

Aethir Tokenomics – Case Study

Kajetan Olas

22 Nov 2024
Aethir Tokenomics – Case Study

Authors of the contents are not affiliated to the reviewed project in any way and none of the information presented should be taken as financial advice.

In this article we analyze tokenomics of Aethir - a project providing on-demand cloud compute resources for the AI, Gaming, and virtualized compute sectors.
Aethir aims to aggregate enterprise-grade GPUs from multiple providers into a DePIN (Decentralized Physical Infrastructure Network). Its competitive edge comes from utlizing the GPUs for very specific use-cases, such as low-latency rendering for online games.
Due to decentralized nature of its infrastructure Aethir can meet the demands of online-gaming in any region. This is especially important for some gamer-abundant regions in Asia with underdeveloped cloud infrastructure that causes high latency ("lags").
We will analyze Aethir's tokenomics, give our opinion on what was done well, and provide specific recommendations on how to improve it.

Evaluation Summary

Aethir Tokenomics Structure

The total supply of ATH tokens is capped at 42 billion ATH. This fixed cap provides a predictable supply environment, and the complete emissions schedule is listed here. As of November 2024 there are approximately 5.2 Billion ATH in circulation. In a year from now (November 2025), the circulating supply will almost triple, and will amount to approximately 15 Billion ATH. By November 2028, today's circulating supply will be diluted by around 86%.

From an investor standpoint the rational decision would be to stake their tokens and hope for rewards that will balance the inflation. Currently the estimated APR for 3-year staking is 195% and for 4-year staking APR is 261%. The rewards are paid out weekly. Furthermore, stakers can expect to get additional rewards from partnered AI projects.

Staking Incentives

Rewards are calculated based on the staking duration and staked amount. These factors are equally important and they linearly influence weekly rewards. This means that someone who stakes 100 ATH for 2 weeks will have the same weekly rewards as someone who stakes 200 ATH for 1 week. This mechanism greatly emphasizes long-term holding. That's because holding a token makes sense only if you go for long-term staking. E.g. a whale staking $200k with 1 week lockup. will have the same weekly rewards as person staking $1k with 4 year lockup. Furthermore the ATH staking rewards are fixed and divided among stakers. Therefore Increase of user base is likely to come with decrease in rewards.
We believe the main weak-point of Aethirs staking is the lack of equivalency between rewards paid out to the users and value generated for the protocol as a result of staking.

Token Distribution

The token distribution of $ATH is well designed and comes with long vesting time-frames. 18-month cliff and 36-moths subsequent linear vesting is applied to team's allocation. This is higher than industry standard and is a sign of long-term commitment.

  • Checkers and Compute Providers: 50%
  • Ecosystem: 15%
  • Team: 12.5%
  • Investors: 11.5%
  • Airdrop: 6%
  • Advisors: 5%

Aethir's airdrop is divided into 3 phases to ensure that only loyal users get rewarded. This mechanism is very-well thought and we rate it highly. It fosters high community engagement within the first months of the project and sets the ground for potentially giving more-control to the DAO.

Governance and Community-Led Development

Aethir’s governance model promotes community-led decision-making in a very practical way. Instead of rushing with creation of a DAO for PR and marketing purposes Aethir is trying to make it the right way. They support projects building on their infrastructure and regularly share updates with their community in the most professional manner.

We believe Aethir would benefit from implementing reputation boosted voting. An example of such system is described here. The core assumption is to abandon the simplistic: 1 token = 1 vote and go towards: Votes = tokens * reputation_based_multiplication_factor.

In the attached example, reputation_based_multiplication_factor rises exponentially with the number of standard deviations above norm, with regard to user's rating. For compute compute providers at Aethir, user's rating could be replaced by provider's uptime.

Perspectives for the future

While it's important to analyze aspects such as supply-side tokenomics, or governance, we must keep in mind that 95% of project's success depends on demand-side. In this regard the outlook for Aethir may be very bright. The project declares $36M annual reccuring revenue. Revenue like this is very rare in the web3 space. Many projects are not able to generate any revenue after succesfull ICO event, due to lack fo product-market-fit.

If you're looking to create a robust tokenomics model and go through institutional-grade testing please reach out to contact@nextrope.com. Our team is ready to help you with the token engineering process and ensure your project’s resilience in the long term.

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!