Skip to content

Instantly share code, notes, and snippets.

@gatherheart
Created November 23, 2019 14:12
Show Gist options
  • Save gatherheart/c0be2e5761400ed6fd8141bea5d2d13a to your computer and use it in GitHub Desktop.
Save gatherheart/c0be2e5761400ed6fd8141bea5d2d13a 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.5.13+commit.5b0b510c.js&optimize=false&gist=
pragma solidity >=0.4.22 < 0.6.0;
// REFERENCE TO github.com/gatherheart
// support for only uint256 and address array
library ArrayUtils{
using ArrayUtils for uint256 [];
using ArrayUtils for address [];
function pop(uint256 [] storage array, uint256 idx) internal returns(uint256){
uint256 ret = array[idx];
array.removeItem(idx);
return ret;
}
function removeAll(uint256 [] storage array) internal {
for (uint256 i = 0; i < array.length - 1; i++){
delete array[i];
}
delete array[array.length - 1];
array.length = 0;
}
function removeAll(address [] storage array) internal {
for (uint256 i = 0; i < array.length - 1; i++){
delete array[i];
}
delete array[array.length - 1];
array.length = 0;
}
function remove(uint256 [] storage array, uint256 index) internal {
if(index >= array.length)
return;
for (uint256 i = index; i < array.length - 1; i++){
array[i] = array[i + 1];
}
delete array[array.length - 1];
array.length--;
}
function remove(address [] storage array, uint256 index) internal {
if(index >= array.length)
return;
for (uint256 i = index; i < array.length - 1; i++){
array[i] = array[i + 1];
}
delete array[array.length - 1];
array.length--;
}
function count(uint256 [] memory array, uint256 item) internal pure returns(uint256){
if(!array.exist(item))
return 0;
uint256 countNum = 0;
for(uint256 i = 0; i < array.length; i++){
if(array[i] == item)
countNum++;
}
return countNum;
}
function count(address [] storage array, address item) internal view returns(uint256){
if(!array.exist(item))
return 0;
uint256 countNum = 0;
for(uint256 i = 0; i < array.length; i++){
if(array[i] == item)
countNum++;
}
return countNum;
}
function removeItem(uint256 [] storage array, uint256 item) internal{
if(!array.exist(item))
return;
for (uint256 i = array.indexOf(item); i < array.length - 1; i++){
array[i] = array[i + 1];
}
delete array[array.length - 1];
array.length--;
}
function removeItem(address [] storage array, address item) internal{
if(!array.exist(item))
return;
for (uint256 i = array.indexOf(item); i < array.length - 1; i++){
array[i] = array[i + 1];
}
delete array[array.length - 1];
array.length--;
}
function indexOf(uint256 [] memory array, uint256 item) internal pure returns(uint256){
require(array.exist(item));
for (uint256 i = 0; i < array.length; i++){
if(array[i] == item)
return i;
}
}
function indexOf(address [] storage array, address item) internal view returns(uint256){
require(array.exist(item));
for (uint256 i = 0; i < array.length; i++){
if(array[i] == item)
return i;
}
}
function exist(uint256 [] memory array, uint256 item) internal pure returns(bool) {
for (uint256 i = 0; i < array.length; i++){
if(array[i] == item)
return true;
}
return false;
}
function exist(address [] storage array, address item) internal view returns(bool) {
for (uint256 i = 0; i < array.length; i++){
if(array[i] == item)
return true;
}
return false;
}
function sort(uint256 [] storage array) internal{
QuickSort(array, 0, array.length - 1);
}
function QuickSort(uint256 [] storage array, uint256 left, uint256 right) internal {
uint256 i = left;
uint256 j = right;
// base
if(i == j)
return;
uint256 pivot = array[ uint256(left + (right - left) / 2) ];
while (i <= j) {
while (array[i] < pivot)
i++;
while (pivot < array[j])
j--;
if (i <= j) {
(array[i], array[j]) = (array[j], array[i]);
i++;
j--;
}
}
// branch
if (left < j)
QuickSort(array, left, j);
if (i < right)
QuickSort(array, i, right);
}
}
pragma solidity >=0.4.22 <0.6.0;
import "./Ownerable.sol";
import "./SafeMath.sol";
import "./ArrayUtils.sol";
import "./Ownerable.sol";
contract Database {
constructor () public {}
mapping (uint256 => Transaction) public transactions;
uint256 [] public openTxids;
uint256 [] public closedTxids;
struct Transaction {
uint256 txid; // transaction id
uint256 txTime; // the time of transaction date
string txStatus;
// seller's information
address seller;
string sellerAddress;
string sellerEmail;
string sellerPhone;
address validator;
uint256 amount; // serial number of the CarCoin
string message;
Validation [] reports; // the list of reports about seller's history
}
struct Validation {
uint256 timeStamp; // report time
string nameOfPage; // reference page
string nameofSite;
uint256 accessTime;
string url; // reference url
}
}
pragma solidity >=0.4.22 <0.6.0;
import "./IERC20.sol";
import "./SafeMath.sol";
contract MyTokenERC20 is ERC20{
using SafeMath for uint256;
string public name = "Used2BlockCoin";
string public symbol = "UBC";
uint8 public decimals = 18;
mapping (address => uint256) private balances;
mapping (address => mapping (address => uint256)) private allowed;
uint256 private _totalSupply;
// Address Should not be Zero
modifier NotZeroAddr(address addr){
require(addr != address(0));
_;
}
constructor () public{
_totalSupply = 10**18;
balances[msg.sender] = _totalSupply;
}
function totalSupply() external view returns (uint256){
return _totalSupply;
}
function balanceOf(address who) external view NotZeroAddr(who) returns (uint256){
return balances[who];
}
function allowance(address owner, address spender) external view NotZeroAddr(spender) returns (uint256){
return allowed[owner][spender];
}
function transfer(address to, uint256 value) external NotZeroAddr(to) returns (bool){
// Underflow And overflow check
require(
balances[msg.sender] >= value
&& balances[to] + value > balances[to]
);
_transfer(msg.sender, to, value);
return true;
}
function _transfer(address _from, address to, uint256 value) private{
balances[_from].sub(value);
balances[to].add(value);
emit Transfer(_from, to, value);
}
function approve(address spender, uint256 value) external NotZeroAddr(spender) returns (bool) {
allowed[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
// fund holder: addresss _from,
// sender who is approved from fund holder : msg.sender
// receiver : address to
function transferFrom(address _from, address to, uint256 value) external NotZeroAddr(_from) NotZeroAddr(to) returns (bool){
require(allowed[_from][msg.sender] >= value);
_transfer(_from, to, value);
allowed[_from][msg.sender] -= value;
return true;
}
}
pragma solidity >=0.4.22 <0.6.0;
interface ERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address who) external view returns (uint256);
function allowance(address owner, address spender) external view returns (uint256);
function transfer(address to, uint256 value) external returns (bool);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(address _from, address to, uint256 value) external returns (bool);
event Transfer(address indexed _from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
pragma solidity >=0.4.22 < 0.6.0;
contract Ownerable {
// event
event OwnerLog(address);
address public owner;
constructor() public{
owner = msg.sender;
emit OwnerLog(msg.sender);
}
modifier onlyOwner{
require(msg.sender == owner);
_;
}
modifier NotZeroAddr(address addr){
require(addr != address(0));
_;
}
function transferOwnership(address newOwner) public onlyOwner NotZeroAddr(newOwner) {
if(owner != newOwner)
owner = newOwner;
}
}
pragma solidity >=0.4.22 <0.6.0;
// Reference to https://github.com/OpenZeppelin/openzeppelin-solidity
library SafeMath {
function incr(uint256 a) internal pure returns(uint256){
return add(a, 1);
}
function decr(uint256 a) internal pure returns(uint256){
return sub(a, 1);
}
function mul(uint256 a, uint256 b) internal pure returns(uint256){
if (a == 0 || b == 0)
return 0;
uint256 result = a * b;
require(result / b == a, "SafeMath: overflow");
return result;
}
function add(uint256 a, uint256 b) internal pure returns(uint256){
uint256 result = a + b;
require(result >= a && result >= b, "SafeMath: overflow");
return result;
}
function sub(uint256 a, uint256 b) internal pure returns(uint256){
uint256 result = a - b;
require(result <= a, "SafeMath: underflow");
return result;
}
function div(uint256 a, uint256 b) internal pure returns(uint256){
require(b != 0, "Exception: Division by zero");
uint256 result = a / b;
return result;
}
function mod(uint256 a, uint256 b) internal pure returns(uint256){
require(b != 0, "Exception: modulo by zero");
uint256 result = a % b;
return result;
}
}
pragma solidity >=0.4.22 <0.6.0;
import "./Database.sol";
import "./ERC20.sol";
import "./Ownerable.sol";
contract Used2Block is Ownerable, ERC20, Database {
using SafeMath for uint256;
using ArrayUtils for uint256 [];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment