Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create byodit.sol #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions winogradsky_project/ByodIT/test_code/byodit.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
pragma solidity ^0.4.2;
// 'pragma' indique au compileur dans quelle version de Solidity ce code est écrit
// 'pragma' tells the compiler which version of Solidity this code is written in
contract Byodit {

// variables
//// Valeur des données en ByoditCoin - Data value in ByoditCoin
uint public rate;
bool private transactionOK;

//// Colonnes - Byodit columns
mapping (address => uint) public dataProduction;
mapping (address => uint) public totalDataConsumption;
///// dataConsumption[msg.sender][origin]
mapping (address => mapping (address => uint)) public dataConsumption;
///// allowance[seller][msg.sender]
mapping (address => mapping (address => uint)) public allowance;

//// Liste des vendeurs - list of sellers
struct Seller {
uint index;
}
mapping (address => Seller) private sellersList;
address[] public sellerIndex;

// constructeur - builder
///
function Byodit() {
rate = 1; // (=> 1Go = 1 ByoditCoin)
}

// définition des events (pour affichage à partir de la lecture des logs) - definition of events (for display from log reading)
event Produce(address from, uint data);
event Consume(address from, address origin, uint data);
event Buy(address from, address to, uint data);

// fonction permettant de payer en ByoditCoin - function to pay in ByoditCoin
function sendCoin(address coinContractAddress,
address dataBuyer,
address dataSeller,
uint amount)
returns (bool success){
token m = token(coinContractAddress);
success = m.transferFrom(dataBuyer, dataSeller, amount);
return success;
}


function nbSellers() public constant returns (uint) {
return sellerIndex.length;
}


function isSeller(address sellerAddress) public constant returns(bool isSeller) {
if(sellerIndex.length == 0) return false;
return (sellerIndex[sellersList[sellerAddress].index] == sellerAddress);
}


function addSeller(address sellerAddress) private returns (uint nbSellers) {
if(!isSeller(sellerAddress)) {
sellersList[sellerAddress].index = sellerIndex.push(sellerAddress)-1;
}
}


// fonction permettant de mettre à jour la donnée produite et donc dispo à la vente
// function to update the data produced and thus available for sale
// seul le propriétaire du compte peut mettre à jour sa production
// only the owner of the account can update his production
function setProduction(uint data) returns (uint dataProd) {
dataProduction[msg.sender] += data;

//event
Produce(msg.sender, data);

return dataProduction[msg.sender];
}

// fonction permettant de consommer de la donée
// function to consume the data
// seul le propriétaire du compte peut mettre à jour sa consommation
// only the owner of the account can update its consumption
function consumeData (address origin, uint data) returns (uint DataCons) {
// dans le cas où on achète de la donnée d'un autre noeud
// in the case where one buys data from another node
if ( origin != msg.sender &&
data > allowance[origin][msg.sender] ) throw;
else allowance[origin][msg.sender] -= data;

dataConsumption[msg.sender][origin] += data;
totalDataConsumption[msg.sender] += data;

// event
Consume(msg.sender, origin, data);

return totalDataConsumption[msg.sender];
}

// fonction permettant la vente de donées - function allowing the sale of data
function buyData(address coinContractAddress, address seller, uint data) returns (bool transactionOK) {

// on verifie d'abord que l'acheteur n'achète pas ses propres données
// we verify first that the buyer does not buy his own data
if (msg.sender == seller) throw;

// appel de la fonction de transfer de ByoditCoin - calling the BitCoin transfer function
// 1Go = 1ByoditCoin, pas de besoin de conversion - 1GB = 1ByoditCoin, no need for conversion
transactionOK = sendCoin(coinContractAddress, msg.sender, seller, data);
if (transactionOK != true) throw;

// on met à jour - update :
// - la liste des vendeurs - list of sellers
addSeller(seller);
// - la quantité de donnée pouvant être consommée - amount of data that can be consumed
allowance[seller][msg.sender] += data;

//event
Buy(msg.sender, seller, data);

return transactionOK;
}

}