DEV Community

Cover image for Vending Machine Smart Contract Development
Femi Omoshona
Femi Omoshona

Posted on

Vending Machine Smart Contract Development


Vending machine is an interesting thing to get you started with the basics of Solidity. So, we are going to write a code in Solidity that will help us build a smart contract for a Lizy Vending Machine!

In this title you will learn all the things needed to build a Lizy's Vending Machine using solidity programming language. You will gain a whole host of practical experience/technical skills with vending smart contract, as well as theoretical within this title.

• Introduction to Vending Machine
• Variable
• Function and Function Visibility types (Private) etc
• Get Balance Function
• Purchase Function
• Restock Function
• Compiling our Smart Contract
• Deploying our Smart Contract
• Conclusion

A vending machine is an automated machine that provides items such as snacks, beverages, cigarettes, and lottery tickets to consumers after cash, a credit card, or other forms of payment are inserted into the machine or otherwise made.

Let us start off by going to Remix which is an IDE for the language of Solidity. It is used to write Smart Contracts for the Blockchain. In terms of implementation coding for ERC-20 tokens, the six basic coding functions are: get Balance


Remix is a powerful, open source tool that helps you write Solidity contracts straight from the browser. Written in JavaScript, Remix supports both usage in the browser and locally. Remix also supports testing, debugging and deploying of smart contracts and much more. Enter the url below to access the remix overview environment:

Image description

License Identifier

Solidity 0.8.11 introduces SPDX license identifiers so developers can specify the license the contract uses. (e.g. OpenZeppelin Contracts use MIT license).

SPDX license identifiers should be added to the top of contract files. (see example in OpenZeppelin Contracts ERC20.sol)

The following identifier should be added to the top of your contract (example uses MIT license):

//SPDX-License-Identifier: MIT
Enter fullscreen mode Exit fullscreen mode

The license should be from one of the following:

⚠️ If the license identifier isn’t included in the contract file the compiler will now show a warning.

❗ If there are multiple license identifiers in the contract file the compiler will now show an error.

We now need to write the version of solidity that we are going to be using:

pragma solidity ^0.8.15;
Enter fullscreen mode Exit fullscreen mode

The next step is to create a contract. In Solidity, we write a smart contract by typing the keyword contract followed by the name of the contract. Let us proceed by naming our contract Vending Machine. So, write:

contract VendingMachine{

Enter fullscreen mode Exit fullscreen mode

We will write our further code in the curly brackets as all the functions and variables would be a part of it. Next, we define the variables that will be a part of the contract that is:

1: Address public owner;
2: mapping(address => uint) public lizyBalances;

Here, address refers to the wallet address of the owner and mapping refers to a keyword where we can map one type of variable to be associated with another.

So, lizyBalances is a mapping where number of lizy's will be associated with the address. Next, we go on to create a constructor that will be automatically executed when deployment will occur on the Ethereum Blockchain.

            owner = msg.sender;
            lizyBalances[address(this)] = 1000;
Enter fullscreen mode Exit fullscreen mode

The creation of first function is getBalance(). It is used to get the balance of lizy's that are left in the Vending Machine.

lizyBalances[address(this)] represents the balance of the lizy's that are associated with the current Ethereum account.

            function getBalance() public view returns (uint) {

                return lizyBalances[address(this)];

Enter fullscreen mode Exit fullscreen mode

Function that is next is called restock(). It is restricted only to the owner and thus, the keyword comes as require.
The lizyBalances gets updated when the amount is entered by the owner to be incremented.

        function restock(uint amount) public {

            require(msg.sender == owner, "Only owner can restock the lizy!");
            lizyBalances[address(this)]+= amount;


Enter fullscreen mode Exit fullscreen mode

The last function is the purchase function that actually helps the person or the customer to purchase a lizy's.

The price for one lizy's that we entered is 0.5 ETH. Thus, the msg.value should be equal to the amount multiplied by 0.5 ETH.

Also, the Vending machine should have the number of lizy's entered by the customer. When both of these requirements are fulfilled, then the lizyBalances of this address gets deducted by the amount.

Also, the msg.sender’s lizyBalances will get updated by the number of lizy's purchased.

            function purchase(uint amount) public payable{

                require(msg.value >= amount * 0.5 ether, "You must pay at least 0.5 ether per lizy");

                require(lizyBalances[address(this)] >= amount, "OOPS! Not enough lizy");

                lizyBalances[address(this)] -= amount;

                lizyBalances[address(msg.sender)] += amount;

Enter fullscreen mode Exit fullscreen mode

Finally, the code is done. Now we, proceed to compile it. The Remix automatically compiles the code and shows if there is some error or not. Then, we go ahead to Deploy the code.

To deploy our smart contract we are going to using the Injected Provider-Metamask environment.

Make sure that we are in Deploy and Run Transactions tab. Then the environment to be selected is Injected Provider-Metamask. The contract should be our Vending Machine contract.

Deploy it now, and you will get the deployed contract as follows. Also, now whenever the customer has purchased the lizy's, the lizyBalances gets updated when the function is called.

Image description

Estimated gas fee 0.00138873 RopstenETH
Enter fullscreen mode Exit fullscreen mode

Below is the contract is the address of the lizy vending machine

Enter fullscreen mode Exit fullscreen mode


Image description

Other information includes

Transaction Hash:

Block: 12621525 

Transaction Fee: 0.00138872500388843 Ether ($0.0
Enter fullscreen mode Exit fullscreen mode


You have just created your own vending machine Smart Contract contain three functions (getBalance, purchase and restock). I hope this was a quick guide to get you up to speed.

Hope you enjoyed the article! If you have any questions or comments, feel free to drop them below or reach out to me on

Top comments (0)