Skip to content

Instantly share code, notes, and snippets.

@stefanbemelmans
Created May 30, 2018 07:04
Show Gist options
  • Save stefanbemelmans/7b27cca31597c58056be7eb7346de90d to your computer and use it in GitHub Desktop.
Save stefanbemelmans/7b27cca31597c58056be7eb7346de90d 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) {
if (items[id].created == false) {
require;
}
_;
}
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) {
assert;
}
_;
}
modifier itemsOwnedBySender(uint[] ids) {
for (uint i = 0; i < ids.length; i++) {
if (items[ids[i]].owner != msg.sender) {
require;
}
}
_;
}
modifier itemIsCombined(uint id) {
if (items[id].components.length < 2) {
assert;
}
_;
}
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) {
require;
}
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) {
throw;
}
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 {
event StartTrans(uint);
}
pragma solidity ^0.4.19;
import "./AssetHelperFunctions.sol";
contract CreateAsset {
event AssetCreate(Asset _newAsset);
// event RejectCreate();
// 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
}
mapping (uint => Asset) assetOwnerMap; // links _hercId to asset, starting with a single but will be an array in the future
mapping (uint => address) private hercUsers; // links herc Id to eth address owner
mapping (uint => Asset[]) private userAssets; // hercId mapped to asset arrays
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;
bool resolved;
}
modifier assetNotCreated(uint hercId) {
require(assetOwnerMap[hercId].exists == false);
_;
}
function _createAsset(
string name,
string description,
string manufacturer,
uint id,
uint createdOn,
address owner,
bool exists
) assetNotCreated(_hercId) {
uint _assetNum = userAssets[_hercId].length;
_newAsset = Asset(name, description, manufacturer, id, now, msg.sender, true);
assetOwnerMap[_hercId] = _newAsset;
userAssets[_hercId][_assetNum].push(_newAsset);
emit AssetCreate(_newAsset);
return true;
}
}
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.17;
contract SimpleStorage {
uint storedData;
address userAddress;
string message;
event NewAsset(uint date, string name);
struct Asset {
string name;
uint date;
// uint id;
// uint32 readyTime;
// uint16 winCount;
// uint16 lossCount;
}
Asset[] public assets;
mapping (uint => address) public assetToOwner;
mapping (address => uint) ownerAssetCount;
function _createAsset(string _name, uint _date) internal {
uint _id = assets.push(Asset(_name, _date)); // returns the length of array
assetToOwner[_id] = msg.sender;
ownerAssetCount[msg.sender]++;
emit NewAsset(_date, _name);
}
function setData(uint x) public {
storedData = x;
}
function setAddy() public {
userAddress = msg.sender;
}
function setMessage(string _message) public {
message = _message;
}
function getData() public view returns (uint) {
return storedData;
}
function getAddy() public view returns (address) {
return msg.sender;
}
function getDate() public view returns(uint) {
return now;
}
function getMessage() public view returns (string) {
return message;
}
function newAsset() public {
_createAsset(message, now);
}
}
pragma solidity ^0.4.17;
import "./CreateAssets.sol";
contract Test is CreateAssets {
event LogTest(address sender, address toWhom, uint amount);
CreateAssets._createAsset("hello", msg.sender, now);
}
pragma solidity ^0.4.0;
contract TestContract {
address public account;
int public value;
string public description;
bool public enabled;
mapping(uint256 => Label[]) public labels;
bytes32 public name;
uint[] public numbers;
mapping(address => int[]) public register;
mapping(uint256 => Data) public requirements;
struct Data {
uint time;
string ref;
string info;
}
struct Label {
string name;
string company;
}
// function TestContract() {
// }
// In Remix IDE: enter address between double quotes and prefix with 0x
// Setter: "0x583031d1113ad414f02576bd6afabfb302140225"
function setAccount(address _account) public{
account = _account;
}
// In Remix IDE: enter value between double quotes.
// The string is converted into hex values
// String must be <= 32 bytes otherwise conversion will not be ok.
// Use http://string-functions.com/hex-string.aspx to convert to string
// Setter: "12345678901234567890123456789012" <== value is 32 bytes, OK
// Setter: "123456789012345678901234567890123" <== value is 33 bytes, NOT OK
function setName(bytes32 _name) public{
name = _name;
}
// In Remix IDE: enter value without double quotes
// Setter: 12345 or 2e5
function setValue(int _value) public{
value = _value;
}
// In Remix IDE: enter key without double quotes.
// _ref and _info must be entered with double quotes
// Setter: 456, "aaa", "bbb"
// Getter: 456
function setRequirements(uint256 _idx, string _ref, string _info) public {
requirements[_idx] = Data(now, _ref, _info);
}
// In Remix IDE: enter address between double quotes and prefix with 0x
// The array of numbers must be placed between []
// Setter: "0x583031d1113ad414f02576bd6afabfb302140225", [1,2,3]
// Getter: "0x583031d1113ad414f02576bd6afabfb302140225", 0 <= array index number
function setRegister(address _account, int[] _ids) public {
register[_account] = _ids;
}
// In Remix IDE: The array of numbers must be placed between []
// Setter: [1,2,3]
// Getter: 0 <= array index number
function setNumbers(uint[] _numbers) public {
numbers = _numbers;
}
// In Remix IDE: enter boolean without double quotes.
// Setter: true
function setEnabled(bool _enabled) public {
enabled = _enabled;
}
// In Remix IDE: enter string between double quotes
// Setter: "Hello World"
function setDescription(string _description) public {
description = _description;
}
/* THIS IS NOT ALLOWED
TypeError: Internal type is not allowed for public or external functions
function setLabels(uint256 _idx, Label[] _labelList) {
labels[_idx] = _labelList;
}
*/
}
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