Skip to content

Instantly share code, notes, and snippets.

@Souptacular
Created January 3, 2017 06:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Souptacular/b5b46a41d19a366d834e3c8f2b0e50be to your computer and use it in GitHub Desktop.
Save Souptacular/b5b46a41d19a366d834e3c8f2b0e50be to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.6;
contract TollwayManager {
address public owner;
address public VD;
function TollwayManager() {
owner = msg.sender;
TollEvent(block.timestamp, msg.sender, this, "Welcome to your tollway system.");
}
function createVD() {
VD = new VehicleDirectory(this);
TollEvent(block.timestamp, msg.sender, VD, "Created VD.");
setVD(VD);
}
function setVD(address newVD) {
VD = newVD;
TollEvent(block.timestamp, msg.sender, VD, "Set VD.");
}
function createTollGate(bytes name, bytes location, uint inputCostPerAxel)
returns (address TD) {
TD = new TollGate(name, location, inputCostPerAxel);
addTollGate(TD);
TollEvent(block.timestamp, msg.sender, TD, "Created toll gate.");
return TD;
}
function addTollGate(address inputTollGate) {
VehicleDirectory(VD).addTollGateToList(inputTollGate);
TollEvent(block.timestamp, msg.sender, inputTollGate, "Added toll gate.");
}
function removeTollgate(address inputTollGate) {
VehicleDirectory(VD).removeTollGateFromList(inputTollGate);
TollEvent(block.timestamp, msg.sender, inputTollGate, "Removed toll gate.");
}
function createTollUserAccount(bytes inputLegalName) {
address TUA = new TollUserAccount (inputLegalName, VD, this);
TollEvent(block.timestamp, msg.sender, TUA, "Created Toll User Account.");
}
/* Events */
event TollEvent (uint eventTimeStamp,
address indexed callingAddress,
address indexed resourceAddress,
bytes indexed description);
}
contract VehicleDirectory {
/* VINLookup stores a bytes32 value with a bytes32 VIN associated */
mapping(bytes32 => Vehicle) public VINLookup;
mapping(address => bool) public isATollGate;
address public TM;
struct Vehicle {
address vehicleOwner;
address previousOwner;
uint year;
bytes make;
bytes model;
bytes color;
bytes licensePlate;
bytes8 state;
uint8 axels;
}
function VehicleDirectory(address inputTM) {
TM = inputTM;
}
function createVehicle (bytes32 VIN, address vehicleOwner, uint year,
bytes make, bytes model, bytes color,
bytes licensePlate, bytes8 state, uint8 axels) {
VINLookup[VIN] = Vehicle(vehicleOwner, 0x0, year, make, model, color,
licensePlate, state, axels);
}
function addTollGateToList(address inputTollGate) {
isATollGate[inputTollGate] = true;
TollEvent(block.timestamp, msg.sender, inputTollGate, "Added toll gate.");
}
function removeTollGateFromList(address inputTollGate) {
isATollGate[inputTollGate] = false;
TollEvent(block.timestamp, msg.sender, inputTollGate, "Removed toll gate.");
}
function checkTollGate(address inputTollGate) returns (bool){
return isATollGate[inputTollGate];
}
function vehicleOwnerLookup (bytes32 VIN) returns (address vehicleOwner) {
return (VINLookup[VIN].vehicleOwner);
}
function vehicleAxelsLookup (bytes32 VIN) returns (uint8 axels) {
return (VINLookup[VIN].axels);
}
function setVehicleOwner (bytes32 VIN, address vehicleOwner) {
VINLookup[VIN].vehicleOwner = vehicleOwner;
}
function setVehiclePrevOwner (bytes32 VIN, address prevOwner) {
VINLookup[VIN].previousOwner = prevOwner;
}
function sendVehicleDescription (bytes32 VIN) returns (uint year,
bytes make, bytes model, bytes color,
bytes licensePlate, bytes8 state) {
return (VINLookup[VIN].year, VINLookup[VIN].make, VINLookup[VIN].model,
VINLookup[VIN].color, VINLookup[VIN].licensePlate, VINLookup[VIN].state);
}
event TollEvent (uint eventTimeStamp,
address indexed callingAddress,
address indexed resourceAddress,
bytes indexed description);
}
contract TollGate {
address public owner;
bytes public name;
bytes public location;
uint public costPerAxel;
bool decommissioned = false;
function TollGate(bytes inputName, bytes inputLocation, uint inputCostPerAxel) {
owner = msg.sender;
name = inputName;
location = inputLocation;
costPerAxel = inputCostPerAxel;
}
function chargeToll(address VD, bytes32 VIN) notDecomissioned {
uint charge = VehicleDirectory(VD).vehicleAxelsLookup(VIN) * costPerAxel;
address vehicleOwner = VehicleDirectory(VD).vehicleOwnerLookup(VIN);
TollUserAccount(vehicleOwner).collectToll(charge);
}
function decomissionToll() notDecomissioned {
decommissioned = true;
}
/* Modifers */
modifier notDecomissioned {
if (decommissioned == false) {
} throw;
_;
}
}
contract TollUserAccount {
/* A toll user has: vehicles and amount owed */
bytes legalName;
uint public userCreatedTimestamp;
uint public credits = 0; /* in wei */
uint public owed = 0; /* in wei */
address public TM;
address public VD;
address owner;
mapping(bytes32 => bool) public accountVINs;
function TollUserAccount (bytes inputLegalName, address inputVD, address inputTM) {
owner = msg.sender;
legalName = inputLegalName;
VD = inputVD;
TM = inputTM;
userCreatedTimestamp = block.timestamp;
}
function addVIN(bytes32 VIN) {
// accountVINs[VIN] = true;
VehicleDirectory(VD).setVehicleOwner(VIN, this);
}
function removeVIN(bytes32 VIN) {
// accountVINs[VIN] = false;
VehicleDirectory(VD).setVehiclePrevOwner(VIN, this);
VehicleDirectory(VD).setVehicleOwner(VIN, 0x0);
}
function collectToll(uint tollCharge) {
owed += tollCharge;
}
/* Responses
* 0: Payment failed. Refunding your payment.
* 1: Payment success. Only a partial payment.
* 2: Payment success. Bill fully paid.
* 3: Payment success. Paid with too much ether. Adding extra to credits.
* 4: Payment failed. Already paid in full.
*/
function payToll() payable returns(uint response) {
uint amtSent = msg.value / 2;
if(owed == 0) {
return 4;
} else if ((amtSent * 2) <= owed) {
owed -= (amtSent * 2);
return 1;
} else if (amtSent * 2 > owed) {
credits += ((amtSent * 2) - owed);
owed -= (amtSent * 2);
return 3;
} else {
return 0;
}
}
function adjustOwed(bool add, uint adjustment) {
if (add)
owed += adjustment;
else
owed -= adjustment;
}
/* Modifers */
modifier onlyOwnerOrVD {
if (msg.sender != owner || msg.sender != VD) {
} throw;
_;
}
modifier onlyOwnerOrTollGate {
if (msg.sender != owner || VehicleDirectory(VD).checkTollGate(msg.sender)) {
} throw;
_;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment