/* * Create smart contract for tracking cars repair history. * Fill up the code blocks in Solidity for: * - getCar() * - createCar() * - updateKilometers() * - transferOwnership() */ pragma solidity ^0.4.21; /** * Contract to store the mileage of a car */ contract Odometer { event Creation( address indexed from, string indexed vin ); event Transfer( address indexed from, address indexed to, string indexed vin ); struct Car { string vin; address owner; uint kilometers; } mapping (string => Car) cars; function Odometer() public {} /** * Returns the current data of the given car */ function getCar(string vin) public constant returns(address owner, uint kilometers) { /* -- Your code here -- */ Car storage car = cars[vin]; return (car.owner, car.kilometers); } /** * Creates a track record of a new car. * Transaction will fail (and burn gas!) if the car already exists. */ function createCar(string vin) public { /* -- Your code here -- */ assert(cars[vin].owner == address(0)); cars[vin] = Car({ vin: vin, owner: msg.sender, kilometers: 0 }); emit Creation(msg.sender, vin); } /** * Updates the current kilometers of the car. Transactions fails and burns gas if * the new kilometer value is lower than the old one. */ function updateKilometers(string vin, uint kilometers) public { /* -- Your code here -- */ assert(cars[vin].kilometers < kilometers); assert(msg.sender == cars[vin].owner); cars[vin].kilometers = kilometers; } /** * Transfers the ownership of a car to a different address. * Transaction fails and burns gas if the car is not owned by the caller or if the kilometers are * less than the last known state. */ function transferOwnership(string vin, address owner, uint kilometers) public { /* -- Your code here -- */ updateKilometers(vin, kilometers); cars[vin].owner = owner; emit Transfer(msg.sender, owner, vin); } }
0.4.21