Created
May 14, 2018 15:52
-
-
Save lingqingmeng/40749d6a196d9895ca3612fb2d15df5d to your computer and use it in GitHub Desktop.
ShipmentContract HOT-11 Branch
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.18; | |
/** | |
3500,12000,6,18504502,49504020,400 | |
*/ | |
/** | |
* @title Ownable | |
* @dev The Ownable contract has an owner address, and provides basic authorization control | |
* functions, this simplifies the implementation of "user permissions". | |
*/ | |
contract Ownable { | |
address public owner; | |
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner); | |
/** | |
* @dev The Ownable constructor sets the original `owner` of the contract to the sender | |
* account. | |
*/ | |
function Ownable() public { | |
owner = msg.sender; | |
} | |
/** | |
* @dev Throws if called by any account other than the owner. | |
*/ | |
modifier onlyOwner() { | |
require(msg.sender == owner); | |
_; | |
} | |
/** | |
* @dev Allows the current owner to transfer control of the contract to a newOwner. | |
* @param newOwner The address to transfer ownership to. | |
*/ | |
function transferOwnership(address newOwner) public onlyOwner { | |
require(newOwner != address(0)); | |
OwnershipTransferred(owner, newOwner); | |
owner = newOwner; | |
} | |
} | |
/** | |
* @title SafeMath | |
* @dev Math operations with safety checks that throw on error | |
*/ | |
library SafeMath { | |
function mul(uint256 a, uint256 b) internal pure returns (uint256) { | |
if (a == 0) { | |
return 0; | |
} | |
uint256 c = a * b; | |
assert(c / a == b); | |
return c; | |
} | |
function div(uint256 a, uint256 b) internal pure returns (uint256) { | |
// assert(b > 0); // Solidity automatically throws when dividing by 0 | |
uint256 c = a / b; | |
// assert(a == b * c + a % b); // There is no case in which this doesn't hold | |
return c; | |
} | |
function sub(uint256 a, uint256 b) internal pure returns (uint256) { | |
assert(b <= a); | |
return a - b; | |
} | |
function add(uint256 a, uint256 b) internal pure returns (uint256) { | |
uint256 c = a + b; | |
assert(c >= a); | |
return c; | |
} | |
} | |
contract ShipmentContract is Ownable { | |
using SafeMath for uint; | |
mapping(uint256 => address) public queue; | |
uint256 first = 1; | |
uint256 last = 0; | |
uint256 curr; | |
uint256 shipmentValue; | |
uint256 weightLbs; | |
uint256 numPieces; | |
uint256 poNumber; | |
uint256 shipmentId; | |
uint256 totalCost; | |
event ChangeStatus(string prevString, string currString); | |
struct EntityStruct { | |
uint entityData; | |
bool isEntity; | |
string entityStatus; | |
} | |
function ShipmentContract(uint256 _shipmentValue, uint256 _weightLbs, uint256 _numPieces, uint256 _poNumber, uint256 _shipmentId, uint256 _totalCost) { | |
curr = 0; | |
shipmentValue = _shipmentValue; | |
weightLbs = _weightLbs; | |
numPieces = _numPieces; | |
poNumber = _poNumber; | |
shipmentId = _shipmentId; | |
totalCost = _totalCost; | |
} | |
function enqueue(address data) public { | |
last += 1; | |
queue[last] = data; | |
} | |
function dequeue() public returns (address data) { | |
require(last >= first); // non-empty queue | |
data = queue[first]; | |
delete queue[first]; | |
first += 1; | |
} | |
function increment() public { | |
uint256 prev = curr; | |
uint256 next = curr.add(1); | |
curr = next; | |
address previous = queue[prev]; | |
address current = queue[curr]; | |
string a = entityStructs[previous].entityStatus; | |
string b = entityStructs[current].entityStatus; | |
ChangeStatus(a,b); | |
} | |
// returns the thing ahead of curr | |
function optimisticNext() public constant returns (string entityStatus){ | |
uint256 next = curr.add(1); | |
address a = queue[next]; | |
return entityStructs[a].entityStatus; | |
} | |
mapping(address => EntityStruct) public entityStructs; | |
address[] public entityList; | |
function isEntity(address entityAddress) public constant returns(bool isIndeed) { | |
return entityStructs[entityAddress].isEntity; | |
} | |
function getAtIndex(uint ind) public constant returns(address entityAddress) { | |
return entityList[ind]; | |
} | |
function getEntityCount() public constant returns(uint entityCount) { | |
return entityList.length; | |
} | |
function newEntity(address entityAddress, uint entityData, string entityStatus) public returns(uint rowNumber) { | |
assert(!isEntity(entityAddress)); | |
entityStructs[entityAddress].entityData = entityData; | |
entityStructs[entityAddress].entityStatus = entityStatus; | |
entityStructs[entityAddress].isEntity = true; | |
enqueue(entityAddress); | |
return entityList.push(entityAddress).sub(1); | |
} | |
function updateEntity(address entityAddress, uint entityData, string entityStatus) public returns(bool success) { | |
assert(isEntity(entityAddress)); | |
entityStructs[entityAddress].entityData = entityData; | |
entityStructs[entityAddress].entityStatus = entityStatus; | |
return true; | |
} | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment