Skip to content

Instantly share code, notes, and snippets.

@slashbinslashnoname
Last active August 28, 2016 00:13
Show Gist options
  • Save slashbinslashnoname/ed7f09b92633e38472d5fde632930562 to your computer and use it in GitHub Desktop.
Save slashbinslashnoname/ed7f09b92633e38472d5fde632930562 to your computer and use it in GitHub Desktop.
Tracking item in solidity
/* Cette solution simple de traçabilité repose sur un contrat principal (Traceability)
et un contrat ad hoc par objet à tracer (Tracking).
Le contrat principal se comporte comme une liste blanche associant les identifiants
des objets et les contrats de traçabilité.
Une version plus avancée intègrera le déploiement du contrat Tracking à l'intérieur
de Traceability
An Idea of Jerôme De Tychey, implemented in solc by Romain Lafforgue
"numéro de série: 0001; date de fabrication: 09082016;Lieu de fabrication: 75017PARIS"
sha3(00010908201675017PARIS)=
15905d14d04be568d5e263a664721065a484ffe9c94b474947d601468b2ea744
*/
contract Traceabilityv1 {
// Used to log the transfert
struct Transfert{
address to;
uint transfertDate;
}
struct Entry { // ucfirst for struct and Enums
address owner; // Used to store the current owner wich is capable to act with its entry
string serial; // dunno the type, optionnal
uint date; // UNIX Time, optionnal
// ...add other storage like city or coutry or storeit wherever you like
uint32 nbTransferts; // Increments transferts (beware to increment this when creating)
mapping ( uint32 => Transfert) transferts;
}
//You can provide a search by bytes32 now :)
mapping ( bytes32 => Entry ) public entries;
address public admin; // no camelcase nor capital letters for global vars
modifier onlyAdmin() {
if (msg.sender != admin) throw;
_
}
event addedTrack(address _trackAddress, bytes32 _identifier);
event updatedTrack(address _trackAddress, bytes32 _identifier);
function Traceabilityv1() {
admin = msg.sender;
}
/* Add an entry - if don't already exists (ie in entries) - identified by bytes32 (sha3 checksum) */
function addEntry(string _serial, uint _date, bytes32 _checksum) onlyAdmin {
if (entries[_checksum].owner != 0) throw; // if the entry already exist, we throw; SFYL;
Entry e = entries[_checksum]; // maybe a bad thing to checksum a sha3 of a sha3
e.owner = msg.sender;
e.serial = _serial;
e.date = _date;
e.nbTransferts = 0;
e.transferts[0] = Transfert({to:msg.sender, transfertDate: now}); //First transfert
addedTrack(msg.sender, _checksum);
}
/* Change the ownership of a bytes32 */
function changeOwnership(bytes32 _checksum, address _to)
{
if (entries[_checksum].owner == 0) throw; // if the entry don't exists exist, we throw; SFYL;
if (entries[_checksum].owner != msg.sender) throw; // If the entry doesn't exists, we throw; SFYL;
uint32 _nb = entries[_checksum].nbTransferts++;
entries[_checksum].transferts[_nb] = Transfert({to:msg.sender, transfertDate: now}); //Following transfert
updatedTrack(_to, _checksum);
}
/* Admin can choose a new admin */
function changeAdmin(address _newAdmin) onlyAdmin()
{
admin = _newAdmin;
}
/* Killcontract */
function kill() onlyAdmin() {
selfdestruct(admin);
}
/* better than killcontract : you reimburse every eth sent, this can overhelm dictatorial behaviour */
function(){
if (msg.value >0) {
msg.sender.send(msg.value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment