Skip to content

Commit

Permalink
feature: role
Browse files Browse the repository at this point in the history
  • Loading branch information
longgu02 committed Apr 18, 2023
1 parent 4f34404 commit 6b7f40b
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 16 deletions.
2 changes: 0 additions & 2 deletions contracts/AccessControl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

pragma solidity >=0.7.0 <0.9.0;

import "hardhat/console.sol";

contract Role {
function addRole() public view {}

Expand Down
42 changes: 34 additions & 8 deletions contracts/Role.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,42 @@

pragma solidity >=0.7.0 <0.9.0;

import "hardhat/console.sol";
contract Roles {
enum RoleType {
NONE,
SUPPILER,
MANUFACTURER,
CUSTOMER
}
address[] members;
mapping(address => RoleType) internal bearer;

contract Role {
function addMember() public view {}
function addMember(address _address, RoleType role) public {
require(hasRole(_address) == RoleType.NONE, "You already have role");
bearer[_address] = role;
members.push(_address);
}

function removeMember() public view {}
function removeMember(address _address) public {
require(hasRole(_address) != RoleType.NONE, "Not a member");
bearer[_address] = RoleType.NONE;
}

function hasRole() public view {}
function hasRole(address _address) public view returns (RoleType) {
return bearer[_address];
}

function getMembers() public view {}

function getRoleCount() public view {}
function getMembersWithRole(
RoleType role
) public view returns (address[] memory) {
address[] memory result = new address[](members.length);
uint256 counter = 0;
for (uint256 i = 0; i < members.length; i++) {
if (bearer[members[i]] == role) {
result[counter] = members[i];
counter++;
}
}
return result;
}
}
83 changes: 77 additions & 6 deletions contracts/SupplyChain.sol
Original file line number Diff line number Diff line change
@@ -1,15 +1,86 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;
import "./Role.sol";
import "./AccessControl.sol";
import "./Product.sol";

import "hardhat/console.sol";
pragma solidity >=0.7.0 <0.9.0;

contract SupplyChain {
function createOrder() public view {}

function confirmOrder() public view {}
Role private roleContract;
AccessControl private accessControlContract;
Product private productContract;
address private admin;

enum OrderStatus {
PENDING,
SUCCESS,
FAILED
}

struct Order {
uint256 id;
uint256 productId;
address customer;
address supplier;
address manufacturer;
uint256 createdDate;
uint256 supplyPrice;
uint256 manufacturePrice;
OrderStatus status;
bool paidStatus;
bool isSignedCustomer;
bool isSignedSupplier;
bool isSignedManufacturer;

}

Order[] public orderList;

constructor(address _roleContract, address _accessControlContract, address _productContract){
accessControlContract = AccessControl(_accessControlContract);
roleContract = Role(_roleContract);
productContract = Product(_productContract);
admin = msg.sender;
}

function createOrder(uint256 _productId, address _customer, address _supplier, address _manufacturer, uint _supplyPrice, uint _manufacturePrice) public {
Order memory newOrder = Order({
id : orderList.length + 1,
productId: _productId,
customer : _customer,
supplier : _supplier,
manufacturer : _manufacturer,
createdDate : block.timestamp,
supplyPrice : _supplyPrice,
manufacturePrice : _manufacturePrice,
status : OrderStatus.PENDING,
paidStatus : false,
isSignedCustomer : false,
isSignedSupplier : false,
isSignedManufacturer : false
});
orderList.push(newOrder);
}


function confirmOrder(uint256 _orderId) public view {
Role.RoleType role = Role.hasRole(msg.sender);
// The signer must have role to call this function
require(role != Role.RoleType.NONE, "Permission denied");
// Check if the signer is stakeholder
require(msg.sender == orderList[orderId].manufacturer ||
msg.sender == orderList[orderId].supplier ||
msg.sender == orderList[orderId].customer, "You are not stakeholder of this order");
// Check if the signer has signed the order
// switch(role){
// case Role.RoleType.NONE:
// }
}

function shipOrder() public view {}

function shipOrder() public view {}
function viewOrder() public view {}

function viewOrder() public view {}
}

0 comments on commit 6b7f40b

Please sign in to comment.