---
# System prepended metadata

title: Implement a smart contract using Remix
tags: [blockchain, smart_contract]

---

Implement a smart contract using Remix
===
*By Kongphob Mangkuang* *ID:411021362*
## What we will do:
* Create a file 
* Design the smart contract
* Compile the smart contract
* Connect to the network
* Deploy the smart contract
* Verify the smart contract
## Create a new file
Go to [Remix](https://remix.ethereum.org/). Navigate to FILE EXPLORER > contract and locate 1_storage.sol. Rename the file to a descriptive name such as VendingMachine.

![](https://i.imgur.com/MCdTuMa.png)



Design the smart contract
---
Design a smart contract that can be used to buy colas from a vending machine.
```javascript=
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

contract ColaVendingMachine {

address public Owner;
mapping (address => uint) public ColaBalance;

// set the Owner as the address that deployed the contract
// set the initial vending machine balance to 20 colas
constructor() {
    Owner = msg.sender;
    ColaBalance[address(this)] = 20;
}

function VendingMachineBalance() public view returns (uint) {
    return ColaBalance[address(this)];
}

// Let the Owner restock the vending machine
function Restock(uint amount) public {
    require(msg.sender == Owner, "Only the owner can restock.");
    ColaBalance[address(this)] += amount;
}

// Purchase cola from the vending machine
function Purchase(uint amount) public payable {
    require(msg.value >= amount * 1 ether, "You must pay at least 1 ETH per cola");
    require(ColaBalance[address(this)] >= amount, "Not enough colas in stock to complete this purchase");
    ColaBalance[address(this)] -= amount;
    ColaBalance[msg.sender] += amount;
}

// Let the Owner withdraw from the vending machine
function Withdraw() public {
    require(msg.sender == Owner, "Only the owner can withdraw funds.");
    uint balance = address(this).balance;
    payable(Owner).transfer(balance);
}
}
```
> *This is a Solidity smart contract for a Cola vending machine. It allows users to purchase Cola by sending Ether, while the owner can restock and withdraw funds.*


Compile the smart contract
---
Go to SOLIDITY COMPILER and select version 0.8.11. Click on Compile to compile the smart contract.

![](https://i.imgur.com/0jS4UMa.png)


Connect to Remix VM (London)
---
To test the smart contract, select Remix VM (London) from the DEPLOY & RUN TRANSACTIONS menu.

![](https://i.imgur.com/Rgzn9O2.png)

Deploy the smart contract 
---
Click on the Deploy button. You should see a message on the bottom left indicating that this is a transaction on the Ethereum network.

![](https://i.imgur.com/gn2LN57.png)


Verify the smart contract
---
Once deployed, scroll down to the Deployed Contracts section and click to view the contents. The smart contract should have the following functionalities:
![](https://i.imgur.com/Rp3k5Jw.png)
>* ***Purchase**: This allows users to buy colas by entering the number of colas they want to purchase and the corresponding ether value.*
>* ***Restock**: This function can only be used by the Owner of the contract to restock the vending machine.*
>* ***Withdraw**: This function can only be used by the owner to withdraw ether from the vending machine.*
>* ***ColaBalance**: This function allows users to check the total number of colas available in the vending machine.*
>* ***Owner**: This function displays the address of the contract owner.*
>* ***VendingMachineBalance**: This function allows users to check the remaining balance of cola in the vending machine.*
---
###### tags: `blockchain` `smart_contract` 
