Skip to content

Instantly share code, notes, and snippets.

@stefanbemelmans
Created May 30, 2018 22:42
Show Gist options
  • Save stefanbemelmans/b2b12c2919eae81cc8a9f22f1f2ea283 to your computer and use it in GitHub Desktop.
Save stefanbemelmans/b2b12c2919eae81cc8a9f22f1f2ea283 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.24+commit.e67f0147.js&optimize=false&gist=
pragma solidity ^0.4.0;
/*
Copyright (c) 2018 HERC SEZC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
library Addresses {
function isContract(address _base) internal constant returns (bool) {
uint codeSize;
assembly {
codeSize := extcodesize(_base)
}
return codeSize > 0;
}
}
/*
Copyright (c) 2018 HERC SEZC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.4.17;
import "./HERCToken.sol";
contract AssetFees is HERCToken{
// herc contract ropsten address 0x0C97b0B42140D77dE45Fc669E826225E6bb6B3D2
address public _herc;
address public _user = msg.sender;
uint _chainFee;
uint _viewerFee;
uint _hiprWin;
function transfer(address _to, uint _value)
public
returns (bool) {
return transfer(_to, _value);
}
modifier validValue {
assert(msg.value <= balanceOf(msg.sender));
_;
}
function _createFee() payable isOwner validValue public {
transfer(_herc, 10000);
}
function supplyChain() payable isOwner validValue public {
transfer(_herc, _chainFee);
}
function digitalViewer() payable isOwner validValue public {
transfer(_herc, _viewerFee);
}
function hiprWin() payable isOwner validValue public {
transfer(_user, _hiprWin);
}
}
pragma solidity ^0.4.19;
contract ItemTracking {
uint _hercID;
struct Item {
uint id;
address owner;
uint[] components;
bool exists;
bool created;
}
mapping(uint => Item) items;
modifier itemCreated(uint id) {
require(items[id].created == false);
_;
}
modifier itemNotCreated(uint id) {
if (items[id].created) {
revert();
}
_;
}
modifier itemExists(uint id) {
if (items[id].exists == false) {
// assert;
}
_;
}
// modifier itemsExist(uint[] ids) {
// for (uint i = 0; i < ids.length; i++) {
// if (items[ids[i]].exists == false) {
// require;
// }
// }
// _;
// }
modifier itemOwnedBySender(uint id) {
if (items[id].owner != msg.sender) {
revert();
}
_;
}
modifier itemsOwnedBySender(uint[] ids) {
for (uint i = 0; i < ids.length; i++) {
require(items[ids[i]].owner != msg.sender);
}
_;
}
modifier itemIsCombined(uint id) {
if (items[id].components.length < 2) {
revert();
}
_;
}
modifier itemContainsComponents(uint id, uint[] componentIds) {
for (uint i = 0; i < componentIds.length; i++) {
bool componentFound = false;
for (uint j = 0; j < items[id].components.length; j++) {
if (items[id].components[j] == componentIds[i]) {
componentFound = true;
break;
}
}
if (!componentFound) {
revert();
}
}
_;
}
// Create a new item
function create(uint id)
itemNotCreated(id) {
items[id].id = id;
items[id].exists = true;
items[id].created = true;
items[id].owner = msg.sender;
}
// Combine items to create a single new one
function combine(uint[] srcIds, uint resultId)
// itemsExist(srcIds)
itemsOwnedBySender(srcIds) {
// Verify that at least 2 components are being combined
if (srcIds.length < 2) {
revert();
}
for (uint i = 0; i < srcIds.length; i++) {
items[srcIds[i]].exists = false;
}
create(resultId);
items[resultId].components = srcIds;
}
// Split a combined item into its components
function split(uint srcId)
itemExists(srcId)
itemOwnedBySender(srcId)
itemIsCombined(srcId) {
items[srcId].exists = false;
for (uint i = 0; i < items[srcId].components.length; i++) {
uint componentId = items[srcId].components[i];
items[componentId].exists = true;
items[componentId].owner = items[srcId].owner;
}
}
// A private helper function for removing a component from the components
// array of an item. Doesn't handle error cases (e.g. invalid IDs or
// componentId not a component of itemID). The caller should make sure that
// input is valid.
function removeComponent(uint itemId, uint componentId) private {
// Find out what the components index is in parent item's component
// listing.
uint componentIndex = 0;
for (uint j = 0; j < items[itemId].components.length; j++) {
if (componentId == items[itemId].components[j]) {
componentIndex = j;
break;
}
}
// Remove item from componentIndex. To not bloat the array, let's do
// this by copying value from the last index to componentIndex and
// then remove the last index.
uint lastIndex = items[itemId].components.length - 1;
items[itemId].components[componentIndex] = items[itemId].components[lastIndex];
delete items[itemId].components[lastIndex];
items[itemId].components.length--;
}
// Extract the sub-components listed in the parameter. Leave the rest of the
// components in the parent item. Parent item maintains its old ID.
// If less than 2 components would remain in the parent component, then
// extract behaves exactly like split.
function extract(uint srcId, uint[] toBeExtractedIds)
itemExists(srcId)
itemOwnedBySender(srcId)
itemIsCombined(srcId)
itemContainsComponents(srcId, toBeExtractedIds) {
// If less than 2 components would remain in the parent item after
// the extraction, perform split.
if (items[srcId].components.length - toBeExtractedIds.length < 2) {
split(srcId);
return;
}
// Make extracted components exist and grant ownership to owner of
// parent item. Remove extracted components from parent item's component
// listing
for (uint i = 0; i < toBeExtractedIds.length; i++) {
uint componentId = toBeExtractedIds[i];
items[componentId].exists = true;
items[componentId].owner = items[srcId].owner;
removeComponent(srcId, componentId);
}
}
// Handover ownership of the item
function handover(uint id, address receiver)
itemExists(id)
itemOwnedBySender(id) {
items[id].owner = receiver;
}
// Return number of components for a given item
function getComponentCount(uint id)
itemCreated(id)
constant returns (uint) {
return items[id].components.length;
}
// Return ID of a component of a given parent item at given index.
function getComponentId(uint parentId, uint componentIndex)
itemCreated(parentId)
itemIsCombined(parentId)
constant returns (uint) {
if (componentIndex >= items[parentId].components.length) {
revert();
}
return items[parentId].components[componentIndex];
}
// Return owner of a given item
function getOwner(uint id) itemCreated(id) constant returns (address) {
return items[id].owner;
}
// Return exists status of a given item
function getExistsStatus(uint id) itemCreated(id) constant returns (bool) {
return items[id].exists;
}
// Return created status of a given item
function getCreatedStatus(uint id) constant returns (bool) {
return items[id].created;
}
}
pragma solidity ^0.4.19;
import "./CreateAsset.sol";
contract AssetTransactions is CreateAsset {
uint _hercId;
uint _recipientId;
event TransferStarted(uint, uint, Transaction);
event RejectTransfer(uint, Transaction);
mapping(uint => Transaction) transArrayMap; //mapping _hercId to transaction, starting with a single but will become an array
mapping(uint => Transaction[]) Transactions;
mapping(uint => address) authRecipientsMap;
struct Transaction {
uint txID; // get this from the chain
// perhaps implement lat and long in addition or instead of strings
address from;
address to;
string origin;
string destination;
// uint hops // implement later
// string startDate;
// string estArrivalDate;
// uint quantity;
// bool intiated;
// bool error;
// bool resolved;
}
function addRecipient(address _newRecipient) {
authRecipientsMap[_recipientId] = _newRecipient;
}
function startTrans(
uint _txId,
address _from,
address _to,
string _origin,
string _dest
)
public {
transArrayMap[_hercId] = Transaction(_txId, _from, _to, _origin, _dest);
Transactions[_hercId].push(transArrayMap[_hercId]);
emit TransferStarted(_hercId, _recipientId, transArrayMap[_hercId]);
}
}
// uint _startDate,
// uint _estEndDate,
// uint _quant,
// bool _init,
// bool _error,
// bool _resolved
pragma solidity ^0.4.19;
import "./ownable.sol";
import "./SafeMath.sol";
contract CreateAsset {
using SafeMath for uint256;
event NewAssetCreate(uint, string, string);
// event AssetCreate(address account, string uuid, string manufacturer);
// event RejectCreate(address account, string uuid, string message);
// event AssetTransfer(address from, address to, string uuid);
// event RejectTransfer(address from, address to, string uuid, string message);
uint _hercId; // this will be the user id
Asset _newAsset;
struct Asset {
string name;
string description;
string manufacturer;
uint hercId;
uint createdOn;
address owner;
bool exists;
// uint[] components; // not needed currently
}
Asset[] public assets;
mapping (uint => Asset) assetOwnerMap; // links _hercId to asset, starting with a single but will be an array in the future
mapping (uint => address) assetOwners; // links herc Id to eth address owner
mapping (address => uint) ownerAssetCount; // hercId mapped to asset arrays
// mapping (uint => address) hercIdToAddress; need to figure this out
function _createAsset(
string name,
string description,
string manufacturer,
uint id,
uint createdOn,
address owner,
bool exists
){
uint _id = assets.push(Asset(name, description, manufacturer, id, now, msg.sender, true))-1;
assetOwners[_id] = msg.sender;
ownerAssetCount[msg.sender]++;
emit NewAssetCreate(_id, assets[_id].name, assets[_id].manufacturer);
}
}
// struct Transaction {
// uint txID; // get this from the chain
// // perhaps implement lat and long in addition or instead of strings
// string origin;
// string destination;
// // uint hops // implement later
// string startDate;
// string estArrivalDate;
// uint quantity;
// bool intiated;
// bool error;
// }
pragma solidity ^0.4.24;
/*
Copyright (c) 2018 HERC SEZC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
interface ERC20 {
function transferFrom(address _from, address _to, uint _value) external returns (bool);
function approve(address _spender, uint _value) external returns (bool);
function allowance(address _owner, address _spender) external view returns (uint);
event Approval(address indexed _owner, address indexed _spender, uint _value);
}
pragma solidity ^0.4.0;
/*
Copyright (c) 2018 HERC SEZC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
interface ERC223 {
function transfer(address _to, uint _value, bytes _data) public returns (bool);
event Transfer(address indexed from, address indexed to, uint value, bytes indexed data);
}
pragma solidity ^0.4.0;
/*
Copyright (c) 2018 HERC SEZC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
interface ERC223ReceivingContract {
function tokenFallback(address _from, uint _value, bytes _data) external;
}
pragma solidity ^0.4.0;
/*
Copyright (c) 2018 HERC SEZC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import "./Token.sol";
import "./ERC20.sol";
import "./ERC223.sol";
import "./ERC223ReceivingContract.sol";
import "./SafeMath.sol";
import "./Addresses.sol";
contract HERCToken is Token("HERC", "Hercules", 18, 234259085), ERC20, ERC223 {
using SafeMath for uint;
using Addresses for address;
function HERCToken()
public {
_balanceOf[msg.sender] = _totalSupply;
}
function totalSupply()
public
view
returns (uint) {
return _totalSupply;
}
function balanceOf(address _addr)
public
view
returns (uint) {
return _balanceOf[_addr];
}
function transfer(address _to, uint _value)
public
returns (bool) {
return transfer(_to, _value, "");
}
function transfer(address _to, uint _value, bytes _data)
public
returns (bool) {
if (_value > 0 &&
_value <= _balanceOf[msg.sender]) {
if (_to.isContract()) {
ERC223ReceivingContract _contract = ERC223ReceivingContract(_to);
_contract.tokenFallback(msg.sender, _value, _data);
}
_balanceOf[msg.sender] = _balanceOf[msg.sender].sub(_value);
_balanceOf[_to] = _balanceOf[_to].add(_value);
return true;
}
return false;
}
function transferFrom(address _from, address _to, uint _value)
public
returns (bool) {
return transferFrom(_from, _to, _value, "");
}
function transferFrom(address _from, address _to, uint _value, bytes _data)
public
returns (bool) {
if (_allowances[_from][msg.sender] > 0 &&
_value > 0 &&
_allowances[_from][msg.sender] >= _value &&
_balanceOf[_from] >= _value) {
_allowances[_from][msg.sender] -= _value;
if (_to.isContract()) {
ERC223ReceivingContract _contract = ERC223ReceivingContract(_to);
_contract.tokenFallback(msg.sender, _value, _data);
}
_balanceOf[_from] = _balanceOf[_from].sub(_value);
_balanceOf[_to] = _balanceOf[_to].add(_value);
Transfer(_from, _to, _value);
return true;
}
return false;
}
function approve(address _spender, uint _value)
public
returns (bool) {
if (_balanceOf[msg.sender] >= _value) {
_allowances[msg.sender][_spender] = _value;
Approval(msg.sender, _spender, _value);
return true;
}
return false;
}
function allowance(address _owner, address _spender)
public
view
returns (uint) {
if (_allowances[_owner][_spender] < _balanceOf[_owner]) {
return _allowances[_owner][_spender];
}
return _balanceOf[_owner];
}
}
/**
* @title Ownable
* @dev The Ownable contract has an owner address, and provides basic authorization control
* functions, this simplifies the implementation of "user permissions".
*/
pragma solidity ^0.4.24;
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.
*/
constructor() 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));
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
pragma solidity ^0.4.0;
/*
Copyright (c) 2018 HERC SEZC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
library SafeMath {
function sub(uint _base, uint _value)
internal
pure
returns (uint) {
assert(_value <= _base);
return _base - _value;
}
function add(uint _base, uint _value)
internal
pure
returns (uint _ret) {
_ret = _base + _value;
assert(_ret >= _base);
}
function div(uint _base, uint _value)
internal
pure
returns (uint) {
assert(_value > 0 && (_base % _value) == 0);
return _base / _value;
}
function mul(uint _base, uint _value)
internal
pure
returns (uint _ret) {
_ret = _base * _value;
assert(0 == _base || _ret / _base == _value);
}
}
pragma solidity ^0.4.0;
/*
Copyright (c) 2018 HERC SEZC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
contract Token {
string internal _symbol;
string internal _name;
uint8 internal _decimals;
uint internal _totalSupply;
mapping (address => uint) internal _balanceOf;
mapping (address => mapping (address => uint)) internal _allowances;
constructor (string symbol, string name, uint8 decimals, uint totalSupply) public {
_symbol = symbol;
_name = name;
_decimals = decimals;
_totalSupply = totalSupply;
}
function name()
public
view
returns (string) {
return _name;
}
function symbol()
public
view
returns (string) {
return _symbol;
}
function decimals()
public
view
returns (uint8) {
return _decimals;
}
function totalSupply()
public
view
returns (uint) {
return _totalSupply;
}
function balanceOf(address _addr) public view returns (uint);
function transfer(address _to, uint _value) public returns (bool);
event Transfer(address indexed _from, address indexed _to, uint _value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment