I'll help you create a simple vending machine smart contract using Solidity that can be deployed using Remix IDE. This contract will allow users to purchase items and the owner to manage the inventory.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract VendingMachine {
address public owner;
mapping(uint => uint) public items; // itemId => price
mapping(uint => uint) public inventory; // itemId => quantity
event ItemPurchased(address buyer, uint itemId, uint quantity);
event InventoryRefilled(uint itemId, uint quantity);
event PriceUpdated(uint itemId, uint newPrice);
constructor() {
owner = msg.sender;
// Initialize some items with prices (in wei)
items[1] = 1000000000000000; // 0.001 ETH for item 1
items[2] = 2000000000000000; // 0.002 ETH for item 2
items[3] = 3000000000000000; // 0.003 ETH for item 3
// Initialize inventory
inventory[1] = 10;
inventory[2] = 10;
inventory[3] = 10;
}
modifier onlyOwner() {
require(msg.sender == owner, "Only owner can perform this action");
_;
}
function purchaseItem(uint _itemId, uint _quantity) public payable {
require(items[_itemId] > 0, "Item does not exist");
require(inventory[_itemId] >= _quantity, "Insufficient inventory");
require(msg.value >= items[_itemId] * _quantity, "Insufficient payment");
inventory[_itemId] -= _quantity;
emit ItemPurchased(msg.sender, _itemId, _quantity);
// Return excess payment
uint excess = msg.value - (items[_itemId] * _quantity);
if (excess > 0) {
payable(msg.sender).transfer(excess);
}
}
function refillInventory(uint _itemId, uint _quantity) public onlyOwner {
require(items[_itemId] > 0, "Item does not exist");
inventory[_itemId] += _quantity;
emit InventoryRefilled(_itemId, _quantity);
}
function updatePrice(uint _itemId, uint _newPrice) public onlyOwner {
require(_newPrice > 0, "Price must be greater than 0");
items[_itemId] = _newPrice;
emit PriceUpdated(_itemId, _newPrice);
}
function getItemPrice(uint _itemId) public view returns (uint) {
return items[_itemId];
}
function getInventoryLevel(uint _itemId) public view returns (uint) {
return inventory[_itemId];
}
function withdrawFunds() public onlyOwner {
payable(owner).transfer(address(this).balance);
}
}
To use this contract in Remix IDE:
- Open Remix IDE (remix.ethereum.org)
- Create a new file named
VendingMachine.sol
- Copy and paste the code above
- Compile the contract using the Solidity compiler (0.8.0 or higher)
- Deploy the contract using the Deploy & Run Transactions module
This contract includes the following features:
- Purchase items with ETH
- Automatic refund of excess payment
- Inventory management
- Price updates
- Owner-only functions for refilling and withdrawing funds
- Event logging for important actions
The contract starts with 3 items initialized with different prices and 10 units each in inventory. The owner can:
- Refill inventory
- Update prices
- Withdraw accumulated funds
Users can:
- Check prices
- Check inventory levels
- Purchase items
Would you like me to explain any specific part of the contract in more detail?