Created
October 3, 2022 22:29
-
-
Save ac12644/0ea4d8a4c85c5e3b5ebbfdba21d807c5 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; | |
... | |
//2nd step of suppply chain process | |
function sellItemByFarmer(uint256 _productCode, uint256 _price) | |
public | |
onlyFarmer // check _msgSender() belongs to farmerRole | |
producedByFarmer(_productCode) // check items state has been produced | |
verifyCaller(items[_productCode].ownerID) // check _msgSender() is owner | |
{ | |
items[_productCode].itemState = State.ForSaleByFarmer; | |
items[_productCode].productPrice = _price; | |
emit ForSaleByFarmer(_productCode); | |
} | |
// 3rd step of suppply chain process | |
function purchaseItemByDistributor(uint256 _productCode) | |
public | |
payable | |
onlyDistributor // check _msgSender() belongs to distributorRole | |
forSaleByFarmer(_productCode) // check items state is for ForSaleByFarmer | |
paidEnough(items[_productCode].productPrice) // check if distributor sent enough Ether for item | |
checkValue(_productCode, payable(_msgSender())) // check if overpayed return remaing funds back to _msgSender() | |
{ | |
address payable ownerAddressPayable = _make_payable( | |
items[_productCode].farmerID | |
); // make originFarmID payable | |
ownerAddressPayable.transfer(items[_productCode].productPrice); // transfer funds from distributor to farmer | |
items[_productCode].ownerID = _msgSender(); // update owner | |
items[_productCode].distributorID = _msgSender(); // update distributor | |
items[_productCode].itemState = State.PurchasedByDistributor; // update state | |
itemsHistory[_productCode].FTD = block.number; // add block number | |
emit PurchasedByDistributor(_productCode); | |
} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment