Created
October 14, 2019 10:43
-
-
Save minhth1905/a424eb3b686df519f65b1d12e09c5992 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; | |
contract Adoption { | |
address[16] public adopters; | |
function adopt(uint petId) public returns (uint) { | |
require(petId >= 0 && petId <= 15); | |
adopters[petId] = msg.sender; | |
return petId; | |
} | |
function getAdopters() public view returns (address[16] memory) { | |
return adopters; | |
} | |
} |
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.5.0; | |
pragma experimental ABIEncoderV2; | |
contract Money { | |
struct People{ | |
uint id; | |
string name; | |
uint amount; | |
} | |
mapping (uint => People) public peoples; | |
event votedEvent(uint indexed _candidateId); | |
uint public candidateConut; | |
constructor() public { | |
candidateConut = 0; | |
addCandidate("Holder 1"); | |
addCandidate("Holder 2"); | |
} | |
function addCandidate(string memory _name) public { | |
peoples[candidateConut] = People(candidateConut,_name,0); | |
candidateConut++; | |
} | |
function addManyCandidate(string[] memory names) public { | |
for(uint i = 0; i < names.length; i++) { | |
addCandidate(names[i]); | |
} | |
} | |
//return Single structure | |
function get(uint _candidateId) public view returns(People memory) { | |
return peoples[_candidateId]; | |
} | |
//return Array of structure Value | |
function getPeople() public view returns (uint[] memory, string[] memory,uint[] memory){ | |
uint[] memory id = new uint[](candidateConut); | |
string[] memory name = new string[](candidateConut); | |
uint[] memory amount = new uint[](candidateConut); | |
for (uint i = 0; i < candidateConut; i++) { | |
People storage people = peoples[i]; | |
id[i] = people.id; | |
name[i] = people.name; | |
amount[i] = people.amount; | |
} | |
return (id, name,amount); | |
} | |
//return Array of structure | |
function getPeoples() public view returns (People[] memory){ | |
People[] memory id = new People[](candidateConut); | |
for (uint i = 0; i < candidateConut; i++) { | |
People storage people = peoples[i]; | |
id[i] = people; | |
} | |
return id; | |
} | |
} |
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 "./test.sol"; | |
contract Session { | |
// Structure to hold details of Bidder} | |
enum State {Init, Vote, Done} | |
address public creator; | |
address public mainContract; | |
Main MainContract; | |
string public name; | |
string public description; | |
string public image; | |
uint256 public proposedPrice; | |
uint256 public deviation; | |
uint256 public finalPrice; | |
address[] public iParticipants; | |
uint256 public nParticipants; | |
mapping(address => uint256) public pricings; | |
State public state = State.Init; | |
address public admin; | |
function Session(address _mainContract, string _name, string _description, string _image) public { | |
creator = msg.sender; | |
admin = msg.sender; | |
mainContract = _mainContract; | |
MainContract = Main(_mainContract); | |
name = _name; | |
description = _description; | |
image = _image; | |
state = State.Init; | |
proposedPrice = 0; | |
deviation = 0; | |
finalPrice = 0; | |
nParticipants = 0; | |
MainContract.addSession(address(this)); | |
} | |
function startSession() public { | |
if (state == State.Done) { | |
revert(); | |
} | |
state = State.Vote; | |
} | |
function closeSession(uint256 _finalPrice) public { | |
if (state != State.Vote) { | |
revert(); | |
} | |
uint256 tuso = _finalPrice - proposedPrice; | |
if(tuso < 0) { | |
tuso = -tuso; | |
} | |
uint256 dnew = tuso*100/_finalPrice; | |
finalPrice = _finalPrice; | |
deviation = dnew; | |
for(uint indexParticipant = 0; indexParticipant < nParticipants; indexParticipant ++) { | |
MainContract.updateDeviation(iParticipants[indexParticipant], deviation); | |
MainContract.updateNSessions(iParticipants[indexParticipant]); | |
} | |
state = State.Done; | |
} | |
function updateProduct(string _name, string _description, string _image) public { | |
name = _name; | |
description = _description; | |
image = _image; | |
} | |
function pricing(uint256 _price) public { | |
var (fullname, email, nSessions, currentParticipantDeviation, islogin) = MainContract.getInfoParticipant(msg.sender); | |
if(!islogin) { | |
revert(); | |
} | |
if(_price <= 0) { | |
revert(); | |
} | |
iParticipants.push(msg.sender); | |
if(pricings[msg.sender] == 0) { | |
nParticipants ++; | |
} | |
pricings[msg.sender] = _price; | |
// caculator proposedPrice | |
uint256 totalPrice = 0; | |
uint256 totalDeviation = 0; | |
for(uint indexParticipant = 0; indexParticipant < nParticipants; indexParticipant ++) { | |
totalPrice += _price*(10000 - currentParticipantDeviation); | |
totalDeviation += currentParticipantDeviation; | |
} | |
proposedPrice = totalPrice/(10000*nParticipants - totalDeviation); | |
} | |
} |
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 Main { | |
// Structure to hold details of Bidder | |
struct IParticipant { | |
string fullname; | |
string email; | |
uint8 nSessions; | |
uint256 deviation; | |
bool islogin; | |
} | |
address public admin; | |
address[] public iParticipants; | |
uint256 public nParticipants; | |
mapping(address => IParticipant) public participants; | |
address[] public sessions; | |
uint256 public nSessions; | |
function Main() public { | |
admin = msg.sender; | |
} | |
function register(string fullname, string email) public { | |
address _account = msg.sender; | |
IParticipant storage currentParticipant = participants[_account]; | |
if (!currentParticipant.islogin) { | |
nParticipants = iParticipants.push(_account); | |
} | |
currentParticipant.fullname = fullname; | |
currentParticipant.email = email; | |
currentParticipant.nSessions = 0; | |
currentParticipant.islogin = true; | |
currentParticipant.deviation = 50;// Default is 50% | |
} | |
function addSession(address session) public { | |
nSessions = sessions.push(session); | |
} | |
function getInfoParticipant(address _account) public returns(string, string, uint8, uint256, bool) { | |
IParticipant storage currentParticipant = participants[_account]; | |
return (currentParticipant.fullname, currentParticipant.email, currentParticipant.nSessions, currentParticipant.deviation, currentParticipant.islogin); | |
} | |
function updateDeviation(address _account, uint256 deviationNew) public { | |
IParticipant storage currentParticipant = participants[_account]; | |
uint256 deviation = ((currentParticipant.deviation*currentParticipant.nSessions) + deviationNew)/(currentParticipant.nSessions + 1); | |
currentParticipant.deviation = deviation; | |
} | |
function updateNSessions(address _account) public { | |
IParticipant storage currentParticipant = participants[_account]; | |
currentParticipant.nSessions = currentParticipant.nSessions + 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment