Created
October 7, 2019 17:15
-
-
Save minhth1905/4032ce7cae327220c2d0899106d1318c to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.17+commit.bdeb9e52.js&optimize=false&gist=
This file contains hidden or 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.4.17; | |
import "./Session.sol"; | |
contract Main { | |
struct Participant { | |
uint participantId; | |
string fullname; | |
string email; | |
uint deviation; | |
uint numberPricing; | |
address _account; | |
bool accesslogin; | |
bool loginBefore; | |
} | |
uint participantCount = 0; | |
uint maxParticipant = 10; | |
address public admin; | |
Session public session; | |
mapping(address => Participant) public participants; | |
function Main() public { | |
admin = msg.sender; | |
session = new Session(); | |
} | |
function register() public { | |
if(participantCount > maxParticipant - 1) { | |
revert(); | |
} | |
Participant storage currentParticipant = participants[msg.sender]; | |
currentParticipant.participantId = participantCount; | |
currentParticipant._account = msg.sender; | |
currentParticipant.deviation = 0; | |
currentParticipant.numberPricing = 0; | |
currentParticipant.loginBefore = false; | |
currentParticipant.accesslogin = false; | |
} | |
function updateInfor(string fullname, string email) public { | |
Participant storage currentParticipant = participants[msg.sender]; | |
currentParticipant.fullname = fullname; | |
currentParticipant.email = email; | |
currentParticipant.loginBefore = true; | |
currentParticipant.accesslogin = true; | |
} | |
function startSession(uint _productId) public validRequire(true, false) { | |
session.startSession(_productId); | |
} | |
function addNewProduct(string name, string description) public validRequire(true, false) { | |
session.addNewProduct(name, description); | |
} | |
function updateProduct(uint productId, string name, string description) public validRequire(true, false) { | |
session.updateProduct(productId, name, description); | |
} | |
function priceProduct(uint _productId, uint _price) public validRequire(false, true) { | |
bool checkParticipantPicing = session.checkParticipantPicing(_productId, msg.sender); | |
if(!checkParticipantPicing) { | |
participants[msg.sender].numberPricing = participants[msg.sender].numberPricing + 1; | |
} | |
session.pricing(_productId, _price, msg.sender); | |
calculatorProposedPrice(_productId); | |
} | |
function closeSession(uint _productId, uint finalPrice) public { | |
uint dnew = session.closeSession(_productId, finalPrice); | |
uint numberProductTokens = session.numberProductToken(_productId); | |
for(uint productTokenId = 0; productTokenId < numberProductTokens; productTokenId ++) { | |
var (account, price) = session.viewPricingSession(_productId, productTokenId); | |
uint dcurrent = participants[account].deviation; | |
uint n = participants[account].numberPricing; | |
uint d = (dcurrent*n + dnew)/(n + 1); | |
participants[account].deviation = d; | |
} | |
} | |
function getNumberPricingSession() public view returns(uint){ | |
uint data; | |
data = session.numberPricingSession(); | |
return data; | |
} | |
function viewProduct(uint productId) public returns(uint, uint, uint) { | |
return (session.viewProduct(productId)); | |
} | |
function numberProductToken(uint productId) public returns(uint) { | |
return (session.numberProductToken(productId)); | |
} | |
function viewPricingSession(uint productId, uint productTokenNumber) public returns( address, uint) { | |
return (session.viewPricingSession(productId, productTokenNumber)); | |
} | |
function calculatorProposedPrice(uint productId) public { | |
uint numberProductTokens = session.numberProductToken(productId); | |
uint totalPrice = 0; | |
uint totalDeviation = 0; | |
for(uint productTokenId = 0; productTokenId < numberProductTokens; productTokenId ++) { | |
var (account, price) = session.viewPricingSession(productId, productTokenId); | |
totalPrice += price*(100 - participants[account].deviation); | |
totalDeviation += participants[account].deviation; | |
} | |
uint result = totalPrice/(100*numberProductTokens - totalDeviation); | |
session.setProposedPrice(productId, result); | |
} | |
modifier validRequire(bool requireAdmin, bool requireLogin) | |
{ | |
if(requireAdmin) { | |
require(msg.sender == admin); | |
} | |
if(requireLogin) { | |
Participant storage currentParticipant = participants[msg.sender]; | |
require(currentParticipant.accesslogin); | |
} | |
_; | |
} | |
} |
This file contains hidden or 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.4.17; | |
contract Session { | |
enum State { CREATE, START, CLOSE } | |
struct Token { | |
address _account; | |
uint price; | |
} | |
struct Product { | |
string name; | |
string description; | |
bytes32[] images; | |
uint numberPricing; | |
uint proposedPrice; | |
uint finalPrice; | |
uint deviation; | |
Token[10] productTokens; | |
State state; | |
} | |
uint public numberProduct = 0; | |
uint maxProduct = 10; | |
uint maxParticipant = 10; | |
Product [10] public products; | |
Product public currentProduct; | |
Token public currentToken; | |
address public admin; | |
function Session() public { | |
admin = msg.sender; | |
} | |
function startSession(uint _productId) public { | |
if(_productId > maxProduct) { | |
revert(); | |
} | |
if (products[_productId].state == State.CLOSE) { | |
revert(); | |
} | |
products[_productId].state = State.START; | |
} | |
function closeSession(uint _productId, uint finalPrice) public returns(uint) { | |
if(_productId > maxProduct) { | |
revert(); | |
} | |
if (products[_productId].state != State.START) { | |
revert(); | |
} | |
uint tuso = finalPrice - products[_productId].proposedPrice; | |
if(tuso < 0) { | |
tuso = -tuso; | |
} | |
uint dnew = tuso*100/finalPrice; | |
products[_productId].finalPrice = finalPrice; | |
products[_productId].deviation = dnew; | |
products[_productId].state = State.CLOSE; | |
return dnew; | |
} | |
function addNewProduct(string name, string description) public { | |
if(numberProduct > maxProduct - 1){ | |
revert(); | |
} | |
Token[10] emptyArray; | |
products[numberProduct].name = name; | |
products[numberProduct].description = description; | |
products[numberProduct].images.push("image"); | |
products[numberProduct].numberPricing = 0; | |
products[numberProduct].proposedPrice = 0; | |
products[numberProduct].finalPrice = 0; | |
products[numberProduct].deviation = 0; | |
products[numberProduct].productTokens = emptyArray; | |
products[numberProduct].state = State.CREATE; | |
numberProduct++; | |
} | |
function updateProduct(uint productId, string name, string description) public { | |
if(productId > maxProduct - 1){ | |
revert(); | |
} | |
products[productId].name = name; | |
products[productId].description = description; | |
} | |
function checkPricingSessionOnGoing(uint productId) public returns(bool) { | |
return (products[productId].state == State.START); | |
} | |
function numberPricingSession() public constant returns(uint){ | |
return numberProduct; | |
} | |
function viewProduct(uint productId) public constant returns(uint, uint, uint) { | |
currentProduct = products[productId]; | |
return (products[productId].numberPricing, products[productId].proposedPrice, products[productId].finalPrice); | |
} | |
function viewPricingSession(uint productId, uint productTokenNumber) public returns( address, uint) { | |
currentProduct = products[productId]; | |
currentToken = currentProduct.productTokens[productTokenNumber]; | |
return (currentProduct.productTokens[productTokenNumber]._account, currentProduct.productTokens[productTokenNumber].price); | |
} | |
function numberProductToken(uint productId) public returns(uint) { | |
return (products[productId].numberPricing); | |
} | |
function pricing(uint _productId, uint _price, address _account) public { | |
currentProduct = products[_productId]; | |
if (products[_productId].state != State.START) { | |
revert(); | |
} | |
uint numberPricing = products[_productId].numberPricing; | |
bool pricedBefore = false; | |
for(uint participantId = 0; participantId < products[_productId].numberPricing; participantId ++) { | |
Token tmp = products[_productId].productTokens[participantId]; | |
if(tmp._account == _account) { | |
pricedBefore = true; | |
products[_productId].productTokens[participantId].price = _price; | |
break; | |
} | |
} | |
if(!pricedBefore) { | |
products[_productId].productTokens[numberPricing]._account = _account; | |
products[_productId].productTokens[numberPricing].price = _price; | |
numberPricing ++; | |
products[_productId].numberPricing = numberPricing; | |
} | |
} | |
function checkParticipantPicing(uint _productId, address _account) public returns (bool) { | |
for(uint participantId = 0; participantId < products[_productId].numberPricing; participantId ++) { | |
Token tmp = products[_productId].productTokens[participantId]; | |
if(tmp._account == _account) { | |
return (true); | |
} | |
} | |
return (false); | |
} | |
function setProposedPrice(uint productId, uint proposedPrice) public { | |
products[productId].proposedPrice = proposedPrice; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment