Last active
June 5, 2021 01:07
-
-
Save gwenf/2a638b0d798987b11f22bb54c93f7a65 to your computer and use it in GitHub Desktop.
Ethereum Supple Chain Demo
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
pragma solidity ^0.5.1; | |
contract Calculator { | |
int public num; | |
constructor() public { | |
num = 1; | |
} | |
function getNum() view public returns(int) { | |
return num; | |
} | |
function add(int x, int y) public pure returns(int) { | |
return x + y; | |
} | |
function sub(int x, int y) public pure returns(int) { | |
return x - y; | |
} | |
function setNum(int x) public { | |
num = x; | |
} | |
} |
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
pragma solidity ^0.5.1; | |
import "github.com/OpenZeppelin/zeppelin-solidity/contracts/math/SafeMath.sol"; | |
contract SupplyChain { | |
using SafeMath for uint; | |
// Entities | |
enum EntityType {PRODUCER, PROCESSOR, DISTRIBUTER, RETAILER} | |
struct Entity { | |
string name; | |
EntityType entityType; | |
} | |
mapping (address => Entity) public entities; | |
mapping (address => uint) entityNumGoods; | |
// Goods | |
enum GoodStatus {PENDING, SOLD} | |
struct Good { | |
string name; | |
GoodStatus status; | |
} | |
Good[] public goods; | |
mapping (uint => address) public goodToOwner; | |
// Events | |
event NewEntity(address entityAddress, string name, EntityType entityType); | |
event NewGood(uint goodId, string name); | |
event GoodSold(uint goodId, string name); | |
// Modifiers | |
modifier isProducer(address _sender) { | |
require(entities[_sender].entityType == EntityType.PRODUCER); | |
_; | |
} | |
modifier isNotDuplicateEntity(address _sender) { | |
bytes memory _name = bytes(entities[_sender].name); | |
require(_name.length == 0); | |
_; | |
} | |
// function to initialize an entity | |
function initEntity(string memory _name, EntityType _entityType) public isNotDuplicateEntity(msg.sender) { | |
entities[msg.sender].name = _name; | |
entities[msg.sender].entityType = _entityType; | |
emit NewEntity(msg.sender, _name, _entityType); | |
} | |
// function to initialize a good | |
function initGood(string memory _goodName) public isProducer(msg.sender) { | |
uint id = goods.push(Good(_goodName, GoodStatus.PENDING)); | |
goodToOwner[id] = msg.sender; | |
emit NewGood(id, _goodName); | |
} | |
function transferGood(uint goodId, address _to) public { | |
goodToOwner[goodId] = _to; | |
entityNumGoods[msg.sender].sub(1); | |
entityNumGoods[_to].add(1); | |
} | |
function makePayment(address payable _to) public payable { | |
require(msg.value > 0); | |
_to.transfer(msg.value); | |
} | |
// get all goods that belong to an entity | |
function getGoodsByOwner(address _owner) public view returns(uint[] memory) { | |
uint[] memory result = new uint[](entityNumGoods[_owner]); | |
uint counter = 0; | |
for (uint i = 0; i < goods.length; i++) { | |
if (goodToOwner[i] == _owner) { | |
result[counter] = i; | |
counter++; | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
contract SupplyChain { using SafeMath for uint;
// Entities enum EntityType {PRODUCER, PROCESSOR, DISTRIBUTER, RETAILER} struct Entity { string name; EntityType entityType; } mapping (address => Entity) public entities; mapping (address => uint) entityNumGoods; // Goods enum GoodStatus {PENDING, SOLD} struct Good { string name; GoodStatus status; } Good[] public goods; mapping (uint => address) public goodToOwner; // Events event NewEntity(address entityAddress, string name, EntityType entityType); event NewGood(uint goodId, string name); event GoodSold(uint goodId, string name); // Modifiers modifier isProducer(address _sender) { require(entities[_sender].entityType == EntityType.PRODUCER); _; } modifier isNotDuplicateEntity(address _sender) { bytes memory _name = bytes(entities[_sender].name); require(_name.length == 0); _; }
// function to initialize an entity function initEntity(string memory _name, EntityType _entityType) public isNotDuplicateEntity(msg.sender) { entities[msg.sender].name = _name; entities[msg.sender].entityType = _entityType; emit NewEntity(msg.sender, _name, _entityType); } // function to initialize a good function initGood(string memory _goodName) public isProducer(msg.sender) { uint id = goods.push(Good(_goodName, GoodStatus.PENDING)); goodToOwner[id] = msg.sender; emit NewGood(id, _goodName); }
function transferGood(uint goodId, address _to) public { goodToOwner[goodId] = _to; entityNumGoods[msg.sender].sub(1); entityNumGoods[_to].add(1); } function makePayment(address payable _to) public payable { require(msg.value > 0); _to.transfer(msg.value); } // get all goods that belong to an entity function getGoodsByOwner(address _owner) public view returns(uint[] memory) { uint[] memory result = new uint; uint counter = 0; for (uint i = 0; i < goods.length; i++) { if (goodToOwner[i] == _owner) { result[counter] = i; counter++; } } return result; }}