Skip to content

Instantly share code, notes, and snippets.

@swiftma
Created September 25, 2018 03:29
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 swiftma/0fd5fbbd5feea7cf574e6c43ed782bd8 to your computer and use it in GitHub Desktop.
Save swiftma/0fd5fbbd5feea7cf574e6c43ed782bd8 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.25+commit.59dbf8f1.js&optimize=true&gist=
pragma solidity ^0.4.25;
contract Bridge {
// message sent to other chains
// srcChain is implicit (this chain), destChain is map key
struct SendMsg {
address srcAddr;
address destAddr;
uint256 value;
// final response status from other chains, -1 unknown, 1 success, 0 fail
int8 status;
}
// message received from other chains
// srcChain is map key, destChain is implicit (this chain)
struct RecvMsg {
address srcAddr;
address destAddr;
uint256 value;
bool success; // result of the message
}
// msgs sent to other chains, key is destChain
mapping (uint256 => SendMsg[]) public sendMsgsByDestChain;
mapping (uint256 => uint256) public lastSentMsgIdByDestChain;
mapping (uint256 => uint256) public lastAckedMsgIdByDestChain;
// received messages from other chains, by srcChain
mapping (uint256 => RecvMsg[]) public recvMsgsBySrcChain;
mapping (uint256 => uint256) public lastMsgIdBySrcChain;
// request event, contains only ID information to save storage
event Request(uint256 destChain, uint256 id);
event Response(uint256 srcChain, uint256 id, bool success);
address owner;
constructor(address _owner) public {
owner = _owner;
}
// send message to destChain/destAddr from srcAddr
// srcAddr could be different from msg.sender, if different, usually called by
// other contract to delegate their user
// called by normal user or other contract in the same chain
function send(address srcAddr, uint256 destChain, address destAddr) public payable {
require(srcAddr != address(0) && destChain>0 && destAddr!=address(0));
require(srcAddr.balance >= msg.value, "no enough money");
sendMsgsByDestChain[destChain].push(SendMsg({srcAddr: srcAddr,
destAddr: destAddr, value: msg.value, status: -1}));
uint256 lastMsgId = lastSentMsgIdByDestChain[destChain];
lastSentMsgIdByDestChain[destChain] = lastMsgId + 1; // first message id is 1
emit Request(destChain, lastMsgId + 1);
}
// ack request message to destChain/id, result is success
// ack id must be expected
// called by relay server
function ack(uint256 destChain, uint256 id, bool success) public {
require(msg.sender == owner, "only owner is allowed to call ack");
require(destChain>0);
uint256 expectedId = lastAckedMsgIdByDestChain[destChain] + 1;
require(id == expectedId, "id is not expected");
int8 status = success ? int8(1) : int8(0);
sendMsgsByDestChain[destChain][id-1].status = status;
lastAckedMsgIdByDestChain[destChain] = id;
//TOOD: add general message handler
if(!success){
// TODO: if failed, rollback value transer
}
}
// forward message from relay server to dest chain
// called by relay server
function forward(uint256 srcChain, uint256 id, address srcAddr, address destAddr) public payable {
require(msg.sender == owner, "only owner is allowed to relay message");
// ensure id is expected, prevent duplicate message and double spent
uint256 expectedMsgId = lastMsgIdBySrcChain[srcChain] + 1;
require(id == expectedMsgId, "id is not expected");
bool success = true;
//TODO: add general message handler
if(msg.value>0){
//TODO: limit the gas value to prevent reentry attack
success = destAddr.send(msg.value);
}
lastMsgIdBySrcChain[srcChain] = id;
recvMsgsBySrcChain[srcChain].push(RecvMsg({srcAddr: srcAddr,
destAddr: destAddr, value: msg.value, success: success}));
emit Response(srcChain, id, success);
}
// called by relay server to withdraw money from contract to himself
function withdraw(uint256 value) public {
require(value>0);
require(msg.sender == owner, "only owner is allowed to withdraw");
require(address(this).balance>=value, "no enough money");
owner.transfer(address(this).balance);
}
}
pragma solidity ^0.4.4;
contract Demo {
string public _val;
function setval(string val) public {
_val = val;
}
}
pragma solidity ^0.4.11;
contract G2Token {
string public name = "Dual Genesis"; // token name
string public symbol = "G2"; // token symbol
uint256 public decimals = 6; // token digit
mapping (address => uint256) public balanceOf;
mapping (address => mapping (address => uint256)) public allowance;
uint256 public totalSupply = 0;
bool public stopped = false;
uint256 constant valueFounder = 10000000000000000;
address owner = 0x0;
modifier isOwner {
assert(owner == msg.sender);
_;
}
modifier isRunning {
assert (!stopped);
_;
}
modifier validAddress {
assert(0x0 != msg.sender);
_;
}
constructor(address _addressFounder) public {
owner = msg.sender;
totalSupply = valueFounder;
balanceOf[_addressFounder] = valueFounder;
emit Transfer(0x0, _addressFounder, valueFounder);
}
function transfer(address _to, uint256 _value) public isRunning validAddress returns (bool success) {
require(balanceOf[msg.sender] >= _value);
require(balanceOf[_to] + _value >= balanceOf[_to]);
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
emit Transfer(msg.sender, _to, _value);
return true;
}
function transferFrom(address _from, address _to, uint256 _value) public isRunning validAddress returns (bool success) {
require(balanceOf[_from] >= _value);
require(balanceOf[_to] + _value >= balanceOf[_to]);
require(allowance[_from][msg.sender] >= _value);
balanceOf[_to] += _value;
balanceOf[_from] -= _value;
allowance[_from][msg.sender] -= _value;
emit Transfer(_from, _to, _value);
return true;
}
function approve(address _spender, uint256 _value) public isRunning validAddress returns (bool success) {
require(_value == 0 || allowance[msg.sender][_spender] == 0);
allowance[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
function stop() public isOwner {
stopped = true;
}
function start() public isOwner {
stopped = false;
}
function setName(string _name) public isOwner {
name = _name;
}
function burn(uint256 _value) public {
require(balanceOf[msg.sender] >= _value);
balanceOf[msg.sender] -= _value;
balanceOf[0x0] += _value;
emit Transfer(msg.sender, 0x0, _value);
}
event Transfer(address indexed _from, address indexed _to, uint256 _value);
event Approval(address indexed _owner, address indexed _spender, uint256 _value);
}
pragma solidity ^0.4.24;
/**
* @title ERC20Basic
* @dev Simpler version of ERC20 interface
* See https://github.com/ethereum/EIPs/issues/179
*/
contract ERC20Basic {
function totalSupply() public view returns (uint256);
function balanceOf(address who) public view returns (uint256);
function transfer(address to, uint256 value) public returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
}
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
/**
* @dev Multiplies two numbers, throws on overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
// Gas optimization: this is cheaper than asserting 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
if (a == 0) {
return 0;
}
c = a * b;
assert(c / a == b);
return c;
}
/**
* @dev Integer division of two numbers, truncating the quotient.
*/
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 a / b;
}
/**
* @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}
/**
* @dev Adds two numbers, throws on overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
c = a + b;
assert(c >= a);
return c;
}
}
/**
* @title Basic token
* @dev Basic version of StandardToken, with no allowances.
*/
contract BasicToken is ERC20Basic {
using SafeMath for uint256;
mapping(address => uint256) internal balances;
uint256 internal totalSupply_ = 10000000000000000;
/**
* @dev Total number of tokens in existence
*/
function totalSupply() public view returns (uint256) {
return totalSupply_;
}
/**
* @dev Transfer token for a specified address
* @param _to The address to transfer to.
* @param _value The amount to be transferred.
*/
function transfer(address _to, uint256 _value) public returns (bool) {
require(_value <= balances[msg.sender]);
require(_to != address(0));
balances[msg.sender] = balances[msg.sender].sub(_value);
balances[_to] = balances[_to].add(_value);
emit Transfer(msg.sender, _to, _value);
return true;
}
/**
* @dev Gets the balance of the specified address.
* @param _owner The address to query the the balance of.
* @return An uint256 representing the amount owned by the passed address.
*/
function balanceOf(address _owner) public view returns (uint256) {
return balances[_owner];
}
}
/**
* @title ERC20 interface
* @dev see https://github.com/ethereum/EIPs/issues/20
*/
contract ERC20 is ERC20Basic {
function allowance(address owner, address spender)
public view returns (uint256);
function transferFrom(address from, address to, uint256 value)
public returns (bool);
function approve(address spender, uint256 value) public returns (bool);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
}
/**
* @title Standard ERC20 token
*
* @dev Implementation of the basic standard token.
* https://github.com/ethereum/EIPs/issues/20
* Based on code by FirstBlood: https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol
*/
contract StandardToken is ERC20, BasicToken {
mapping (address => mapping (address => uint256)) internal allowed;
/**
* @dev Transfer tokens from one address to another
* @param _from address The address which you want to send tokens from
* @param _to address The address which you want to transfer to
* @param _value uint256 the amount of tokens to be transferred
*/
function transferFrom(
address _from,
address _to,
uint256 _value
)
public
returns (bool)
{
require(_value <= balances[_from]);
require(_value <= allowed[_from][msg.sender]);
require(_to != address(0));
balances[_from] = balances[_from].sub(_value);
balances[_to] = balances[_to].add(_value);
allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
emit Transfer(_from, _to, _value);
return true;
}
/**
* @dev Approve the passed address to spend the specified amount of tokens on behalf of msg.sender.
* Beware that changing an allowance with this method brings the risk that someone may use both the old
* and the new allowance by unfortunate transaction ordering. One possible solution to mitigate this
* race condition is to first reduce the spender's allowance to 0 and set the desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
* @param _spender The address which will spend the funds.
* @param _value The amount of tokens to be spent.
*/
function approve(address _spender, uint256 _value) public returns (bool) {
allowed[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
/**
* @dev Function to check the amount of tokens that an owner allowed to a spender.
* @param _owner address The address which owns the funds.
* @param _spender address The address which will spend the funds.
* @return A uint256 specifying the amount of tokens still available for the spender.
*/
function allowance(
address _owner,
address _spender
)
public
view
returns (uint256)
{
return allowed[_owner][_spender];
}
/**
* @dev Increase the amount of tokens that an owner allowed to a spender.
* approve should be called when allowed[_spender] == 0. To increment
* allowed value is better to use this function to avoid 2 calls (and wait until
* the first transaction is mined)
* From MonolithDAO Token.sol
* @param _spender The address which will spend the funds.
* @param _addedValue The amount of tokens to increase the allowance by.
*/
function increaseApproval(
address _spender,
uint256 _addedValue
)
public
returns (bool)
{
allowed[msg.sender][_spender] = (
allowed[msg.sender][_spender].add(_addedValue));
emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
return true;
}
/**
* @dev Decrease the amount of tokens that an owner allowed to a spender.
* approve should be called when allowed[_spender] == 0. To decrement
* allowed value is better to use this function to avoid 2 calls (and wait until
* the first transaction is mined)
* From MonolithDAO Token.sol
* @param _spender The address which will spend the funds.
* @param _subtractedValue The amount of tokens to decrease the allowance by.
*/
function decreaseApproval(
address _spender,
uint256 _subtractedValue
)
public
returns (bool)
{
uint256 oldValue = allowed[msg.sender][_spender];
if (_subtractedValue >= oldValue) {
allowed[msg.sender][_spender] = 0;
} else {
allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
}
emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
return true;
}
}
/**
* @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 OwnershipRenounced(address indexed previousOwner);
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 relinquish control of the contract.
* @notice Renouncing to ownership will leave the contract without an owner.
* It will not be possible to call the functions with the `onlyOwner`
* modifier anymore.
*/
function renounceOwnership() public onlyOwner {
emit OwnershipRenounced(owner);
owner = address(0);
}
/**
* @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 {
_transferOwnership(_newOwner);
}
/**
* @dev Transfers control of the contract to a newOwner.
* @param _newOwner The address to transfer ownership to.
*/
function _transferOwnership(address _newOwner) internal {
require(_newOwner != address(0));
emit OwnershipTransferred(owner, _newOwner);
owner = _newOwner;
}
}
/**
* @title Pausable
* @dev Base contract which allows children to implement an emergency stop mechanism.
*/
contract Pausable is Ownable {
event Pause();
event Unpause();
bool public paused = false;
/**
* @dev Modifier to make a function callable only when the contract is not paused.
*/
modifier whenNotPaused() {
require(!paused);
_;
}
/**
* @dev Modifier to make a function callable only when the contract is paused.
*/
modifier whenPaused() {
require(paused);
_;
}
/**
* @dev called by the owner to pause, triggers stopped state
*/
function pause() onlyOwner whenNotPaused public {
paused = true;
emit Pause();
}
/**
* @dev called by the owner to unpause, returns to normal state
*/
function unpause() onlyOwner whenPaused public {
paused = false;
emit Unpause();
}
}
/**
* @title Pausable token
* @dev StandardToken modified with pausable transfers.
**/
contract PausableToken is StandardToken, Pausable {
function transfer(
address _to,
uint256 _value
)
public
whenNotPaused
returns (bool)
{
return super.transfer(_to, _value);
}
function transferFrom(
address _from,
address _to,
uint256 _value
)
public
whenNotPaused
returns (bool)
{
return super.transferFrom(_from, _to, _value);
}
function approve(
address _spender,
uint256 _value
)
public
whenNotPaused
returns (bool)
{
return super.approve(_spender, _value);
}
function increaseApproval(
address _spender,
uint _addedValue
)
public
whenNotPaused
returns (bool success)
{
return super.increaseApproval(_spender, _addedValue);
}
function decreaseApproval(
address _spender,
uint _subtractedValue
)
public
whenNotPaused
returns (bool success)
{
return super.decreaseApproval(_spender, _subtractedValue);
}
}
contract G2Token is PausableToken {
string public name = "Dual Genesis"; // token name
string public symbol = "G2"; // token symbol
uint256 public decimals = 6; // token digit
mapping(address => uint256) public invests;
mapping(address => uint256) public releaseTimes;
mapping(address => uint256) public released;
constructor() public {
balances[msg.sender] = totalSupply();
emit Transfer(0x0, msg.sender, totalSupply());
}
/**
* @dev Add a new invester to the contract.
* @param _invester The address of the invester to add.
* @param _invest The number of locked G2 Token owned by the invester.
* @param _releaseTime timestamp when token release is first enabled
*/
function addInvester(address _invester, uint256 _invest, uint256 _releaseTime) onlyOwner public {
require(_invester != address(0));
require(_invest > 0);
require(invests[_invester] == 0);
require(_invest <= balances[msg.sender]);
invests[_invester] = _invest;
releaseTimes[_invester] = _releaseTime;
// lock _invest G2 coin to "this" address
balances[msg.sender] = balances[msg.sender].sub(_invest);
balances[this] = balances[this].add(_invest);
emit Transfer(msg.sender, this, _invest);
}
/**
* @dev release called by invester, to release their locked G2 token
*/
function release() public {
address invester = msg.sender;
require(invests[invester] > 0);
require(releaseTimes[invester] > 0);
uint256 releaseTime = releaseTimes[invester];
require(block.timestamp >= releaseTime);
uint256 total = invests[invester];
uint256 unreleased = total.sub(released[invester]);
require(unreleased > 0);
uint256 oneMonthSeconds = 30*24*60*60; //30 days seconds
uint256 totalCanUnlockPercent = 55; //55%
if(block.timestamp >= releaseTime.add(2*oneMonthSeconds)){ //60 days after first release time
totalCanUnlockPercent += 15;
}
if(block.timestamp >= releaseTime.add(4*oneMonthSeconds)){ //120 days after first release time
totalCanUnlockPercent += 15;
}
if(block.timestamp >= releaseTime.add(6*oneMonthSeconds)){ //180 days after first release time
totalCanUnlockPercent += 15;
}
uint256 totalCanUnlock = total.mul(totalCanUnlockPercent).div(100);
uint256 canRelease = totalCanUnlock.sub(released[invester]);
require(canRelease > 0);
released[invester] = released[invester].add(canRelease);
// unlock G2 coin from "this" to invester address
balances[this] = balances[this].sub(canRelease);
balances[invester] = balances[invester].add(canRelease);
emit Transfer(this, invester, canRelease);
}
}
pragma solidity ^0.4.24;
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
/**
* @dev Multiplies two numbers, throws on overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
// Gas optimization: this is cheaper than asserting 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
if (a == 0) {
return 0;
}
c = a * b;
assert(c / a == b);
return c;
}
/**
* @dev Integer division of two numbers, truncating the quotient.
*/
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 a / b;
}
/**
* @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}
/**
* @dev Adds two numbers, throws on overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
c = a + b;
assert(c >= a);
return c;
}
}
/**
* @title ERC20Basic
* @dev Simpler version of ERC20 interface
* See https://github.com/ethereum/EIPs/issues/179
*/
contract ERC20Basic {
function totalSupply() public view returns (uint256);
function balanceOf(address who) public view returns (uint256);
function transfer(address to, uint256 value) public returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
}
/**
* @title ERC20 interface
* @dev see https://github.com/ethereum/EIPs/issues/20
*/
contract ERC20 is ERC20Basic {
function allowance(address owner, address spender)
public view returns (uint256);
function transferFrom(address from, address to, uint256 value)
public returns (bool);
function approve(address spender, uint256 value) public returns (bool);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);
}
library SafeERC20 {
function safeTransfer(ERC20Basic token, address to, uint256 value) internal {
require(token.transfer(to, value));
}
function safeTransferFrom(
ERC20 token,
address from,
address to,
uint256 value
)
internal
{
require(token.transferFrom(from, to, value));
}
function safeApprove(ERC20 token, address spender, uint256 value) internal {
require(token.approve(spender, value));
}
}
/**
* @title G2TokenTimelock
* @dev G2TokenTimelock is a token holder contract that will allow a
* beneficiary to extract the tokens gradually
*/
contract G2TokenTimelock {
using SafeMath for uint256;
using SafeERC20 for ERC20Basic;
// ERC20 basic token contract being held
ERC20Basic public token;
// beneficiary of tokens after they are released
address public beneficiary;
// timestamp when token release is first enabled
uint256 public releaseTime;
uint256 public released; //
constructor(
ERC20Basic _token,
address _beneficiary,
uint256 _releaseTime
)
public
{
// solium-disable-next-line security/no-block-members
require(_releaseTime > block.timestamp);
token = _token;
beneficiary = _beneficiary;
releaseTime = _releaseTime;
released = 0;
}
/**
* @notice Transfers tokens held by timelock to beneficiary.
*/
function release() public {
// solium-disable-next-line security/no-block-members
require(block.timestamp >= releaseTime);
uint256 unreleased = token.balanceOf(this);
require(unreleased > 0);
uint256 total = unreleased.add(released);
uint256 oneMonthSeconds = 30*24*60*60; //30 days seconds
uint256 totalCanUnlockPercent = 55; //55%
if(block.timestamp >= releaseTime.add(2*oneMonthSeconds)){ //60 days after first release time
totalCanUnlockPercent += 15;
}
if(block.timestamp >= releaseTime.add(4*oneMonthSeconds)){ //120 days after first release time
totalCanUnlockPercent += 15;
}
if(block.timestamp >= releaseTime.add(6*oneMonthSeconds)){ //180 days after first release time
totalCanUnlockPercent += 15;
}
uint256 totalCanUnlock = total.mul(totalCanUnlockPercent).div(100);
uint256 canRelease = totalCanUnlock.sub(released);
require(canRelease > 0);
released = released.add(canRelease);
token.safeTransfer(beneficiary, canRelease);
}
}
pragma solidity ^0.4.4;
contract Md5hash {
string public _md5hash;
function md5hash() private {
bytes16[1] memory md5;
bytes5[1] memory input;
input[0] = "hello";
bytes16 helloMd5 = hex"5d41402abc4b2a76b9719d911017c592";
assembly {
if iszero(call(not(0), 0x09, 0, input, 5, md5, 16)) {
revert(0, 0)
}
}
assert(helloMd5 == md5[0]);
}
function test() public {
md5hash();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment