Created
November 11, 2023 20:54
-
-
Save mupacif/182a61325532d824c8499c85af234641 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol"; | |
contract CommoditiesMarketMaker { | |
address public owner; | |
uint256 public nextCommodityId; | |
struct Commodity { | |
uint256 id; | |
string name; | |
uint256 price; | |
uint256 quantity; | |
string category; | |
mapping(string => string) attributes; | |
} | |
mapping(uint256 => Commodity) public commodities; | |
mapping(string => address) public priceFeeds; | |
event CommodityTrade(uint256 indexed commodityId, uint256 quantity, uint256 price); | |
event CommodityAdded(uint256 indexed commodityId, string name, uint256 price, uint256 quantity, string category); | |
event CommodityUpdated(uint256 indexed commodityId, string name, uint256 price, uint256 quantity, string category); | |
event CommodityDeleted(uint256 indexed commodityId); | |
event PriceFeedUpdated(string indexed commodityName, address feedAddress); | |
modifier onlyOwner() { | |
require(msg.sender == owner, "Only the owner can call this function"); | |
_; | |
} | |
constructor() { | |
owner = msg.sender; | |
nextCommodityId = 1; // Start commodity ID counter at 1 | |
} | |
function addCommodity(string memory name, uint256 price, uint256 quantity, string memory category) public onlyOwner { | |
Commodity storage commodity = commodities[nextCommodityId]; | |
commodity.id = nextCommodityId; | |
commodity.name = name; | |
commodity.price = price; | |
commodity.quantity = quantity; | |
commodity.category = category; | |
emit CommodityAdded(nextCommodityId, name, price, quantity, category); | |
nextCommodityId++; | |
} | |
function updateCommodity(uint256 id, string memory name, uint256 price, uint256 quantity, string memory category) public onlyOwner { | |
Commodity storage commodity = commodities[id]; | |
require(commodity.id != 0, "Commodity does not exist"); | |
commodity.name = name; | |
commodity.price = price; | |
commodity.quantity = quantity; | |
commodity.category = category; | |
emit CommodityUpdated(id, name, price, quantity, category); | |
} | |
function deleteCommodity(uint256 id) public onlyOwner { | |
require(commodities[id].id != 0, "Commodity does not exist"); | |
delete commodities[id]; | |
emit CommodityDeleted(id); | |
} | |
function updatePriceFeed(string memory commodityName, address feedAddress) public onlyOwner { | |
priceFeeds[commodityName] = feedAddress; | |
emit PriceFeedUpdated(commodityName, feedAddress); | |
} | |
function trade(uint256 commodityId, uint256 quantity, uint256 price) public { | |
Commodity storage commodity = commodities[commodityId]; | |
require(commodity.id != 0, "Commodity does not exist"); | |
require(commodity.quantity >= quantity, "Insufficient commodity quantity"); | |
require(commodity.price == price, "Trade price does not match the commodity price"); | |
// Implement the logic for matching orders and executing trades | |
// For the hackathon, assume the trade always succeeds | |
commodity.quantity -= quantity; | |
// Emit an event for the successful trade | |
emit CommodityTrade(commodityId, quantity, price); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment