Skip to content

Instantly share code, notes, and snippets.

@gavinzheng
Created March 29, 2019 05:50
Show Gist options
  • Select an option

  • Save gavinzheng/ea139f988f4a0383f870d66c9b67dcca to your computer and use it in GitHub Desktop.

Select an option

Save gavinzheng/ea139f988f4a0383f870d66c9b67dcca 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.24;
contract arraytest {
uint256[] array1;
constructor () public {
array1.push(0xAA);
array1.push(0xBB);
array1.push(0xCC);
}
}
contract AutoShop {
address[] autoAssets;
function createChildContract(string brand, string model) public payable {
// insert check if the sent ether is enough to cover the car asset ...
address newAutoAsset = new AutoAsset(brand, model, msg.sender);
autoAssets.push(newAutoAsset);
}
function getDeployedChildContracts() public view returns (address[]) {
return autoAssets;
}
}
contract AutoAsset {
string public brand;
string public model;
address public owner;
function AutoAsset(string _brand, string _model, address _owner) public
{
brand = _brand;
model = _model;
owner = _owner;
}
}
pragma solidity 0.4.24;
contract BasicToken {
uint256 totalSupply_;
mapping(address => uint256) balances;
constructor(uint256 _initialSupply) public {
totalSupply_ = _initialSupply;
balances[msg.sender] = _initialSupply;
}
function totalSupply() public view returns (uint256) {
return totalSupply_;
}
function balanceOf(address _owner) public view returns (uint256) {
return balances[_owner];
}
function transfer(address _to, uint256 _value) public returns (bool)
{
require(_to != address(0));
require(_value <= balances[msg.sender]);
balances[msg.sender] = balances[msg.sender] - _value;
balances[_to] = balances[_to] + _value;
return true;
}
}
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);
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 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 Standard ERC20 token
*
* @dev Implementation of the basic standard token.
* @dev https://github.com/ethereum/EIPs/issues/20
* @dev Based on code by FirstBlood: https://github.com/Firstbloodio/token/blob/master/smart_contract/FirstBloodToken.sol
*/
contract StandardToken is ERC20Basic {
using SafeMath for uint256;
mapping(address => uint256) balances;
uint256 totalSupply_;
/**
* @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];
}
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 Burnable Token
* @dev Token that can be irreversibly burned (destroyed).
*/
contract BurnableToken is StandardToken {
event Burn(address indexed burner, uint256 value);
/**
* @dev Burns a specific amount of tokens.
* @param _value The amount of token to be burned.
*/
function burn(uint256 _value) public {
_burn(msg.sender, _value);
}
function _burn(address _who, uint256 _value) internal {
require(_value <= balances[_who]);
// no need to require value <= totalSupply, since that would imply the
// sender's balance is greater than the totalSupply, which *should* be an assertion failure
balances[_who] = balances[_who].sub(_value);
totalSupply_ = totalSupply_.sub(_value);
emit Burn(_who, _value);
emit Transfer(_who, address(0), _value);
}
}
/**
* @title Mintable token
* @dev Simple ERC20 Token example, with mintable token creation
* Based on code by TokenMarketNet: https://github.com/TokenMarketNet/ico/blob/master/contracts/MintableToken.sol
*/
contract MintableToken is StandardToken, Ownable {
event Mint(address indexed to, uint256 amount);
event MintFinished();
bool public mintingFinished = false;
modifier canMint() {
require(!mintingFinished);
_;
}
modifier hasMintPermission() {
require(msg.sender == owner);
_;
}
/**
* @dev Function to mint tokens
* @param _to The address that will receive the minted tokens.
* @param _amount The amount of tokens to mint.
* @return A boolean that indicates if the operation was successful.
*/
function mint(
address _to,
uint256 _amount
)
hasMintPermission
canMint
public
returns (bool)
{
totalSupply_ = totalSupply_.add(_amount);
balances[_to] = balances[_to].add(_amount);
emit Mint(_to, _amount);
emit Transfer(address(0), _to, _amount);
return true;
}
/**
* @dev Function to stop minting new tokens.
* @return True if the operation was successful.
*/
function finishMinting() onlyOwner canMint public returns (bool) {
mintingFinished = true;
emit MintFinished();
return true;
}
}
contract Controlled is Ownable{
constructor() public {
setExclude(msg.sender);
}
// Flag that determines if the token is transferable or not.
bool public transferEnabled = true;
// flag that makes locked address effect
bool public plockFlag=true;
mapping(address => bool) locked;
mapping(address => bool) exclude;
function enableTransfer(bool _enable) public onlyOwner{
transferEnabled = _enable;
}
function enableLockFlag(bool _enable) public onlyOwner returns (bool success){
plockFlag = _enable;
return true;
}
function addLock(address _addr) public onlyOwner returns (bool success){
require(_addr!=msg.sender);
locked[_addr] = true;
return true;
}
function setExclude(address _addr) public onlyOwner returns (bool success){
exclude[_addr] = true;
return true;
}
function removeLock(address _addr) public onlyOwner returns (bool success){
locked[_addr] = false;
return true;
}
modifier transferAllowed(address _addr) {
if (!exclude[_addr]) {
// flase抛异常,并扣除gas消耗
assert(transferEnabled);
if(plockFlag){
assert(!locked[_addr]);
}
}
_;
}
}
/**
* @title Pausable token
*
* @dev StandardToken modified with pausable transfers.
**/
contract PausableToken is StandardToken, Controlled {
function transfer(address _to, uint256 _value) public transferAllowed(msg.sender) returns (bool) {
return super.transfer(_to, _value);
}
function transferFrom(address _from, address _to, uint256 _value) public transferAllowed(msg.sender) returns (bool) {
return super.transferFrom(_from, _to, _value);
}
function approve(address _spender, uint256 _value) public transferAllowed(msg.sender) returns (bool) {
return super.approve(_spender, _value);
}
}
/*
* @title Fitcoin
*/
contract BNNB is BurnableToken, MintableToken, PausableToken {
// Public variables of the token
string public name;
string public symbol;
// decimals is the strongly suggested default, avoid changing it
uint8 public decimals;
constructor() public {
name = "BNNB";
symbol = "BNNB";
decimals = 18;
totalSupply_ = 1000000000 * 10 ** uint256(decimals);
// Allocate initial balance to the owner
balances[msg.sender] = totalSupply_;
}
// transfer balance to owner
function withdrawEther() onlyOwner public {
address addr = this;
owner.transfer(addr.balance);
}
// can accept ether
function() payable public { }
// Allocate tokens to the users
// @param _owners The owners list of the token
// @param _values The value list of the token
function allocateTokens(address[] _owners, uint256[] _values) public onlyOwner {
require(_owners.length == _values.length, "data length mismatch");
address from = msg.sender;
for(uint256 i = 0; i < _owners.length ; i++){
address to = _owners[i];
uint256 value = _values[i];
require(value <= balances[from]);
balances[to] = balances[to].add(value);
balances[from] = balances[from].sub(value);
emit Transfer(from, to, value);
}
}
}
pragma solidity ^0.4.24;
contract Callee {
uint[] public values;
function getValue(uint initial) returns(uint) {
return initial + 150;
}
function storeValue(uint value) {
values.push(value);
}
function getValues() returns(uint) {
return values.length;
}
}
pragma solidity ^0.4.24;
contract Caller {
address addr= 0x692a70d2e424a56d2c6c27aa97d1a86395877b3a;
function someAction(address addr) returns(uint) {
Callee c = Callee(addr);
return c.getValue(100);
}
function storeAction(address addr) returns(uint) {
Callee c = Callee(addr);
c.storeValue(100);
return c.getValues();
}
function someUnsafeAction(address addr) {
addr.call(bytes4(keccak256("storeValue(uint256)")), 100);
}
}
contract Callee {
function getValue(uint initialValue) returns(uint);
function storeValue(uint value);
function getValues() returns(uint);
}
contract TheContractFactory is LiveFactory {
function uploadCode(string identifier, bytes o_code)
onlyOrNone(deployer[identifierHash(identifier)])
returns (bytes32) {
bytes32 h = identifierHash(identifier);
code[h] = o_code;
deployer[h] = msg.sender;
NewCode(identifier);
return h;
}
function deploy(string identifier) {
bytes c = code[identifierHash(identifier)];
if (c.length == 0) throw;
NewContract(deployCode(c), msg.sender, identifier);
}
function identifierHash(string identifier) returns (bytes32) {
return sha3(identifier);
}
modifier onlyOrNone(address x) {
if (x != 0x0 && x != msg.sender) throw;
_;
}
mapping (bytes32 => address) public deployer;
mapping (bytes32 => bytes) public code;
event NewContract(address x, address indexed owner, string identifier);
event NewCode(string identifier);
}
contract Counterfact is LiveFactory {
function addressForNonce(uint8 nonce) constant returns (address) {
if (nonce > 127) throw;
return address(sha3(0xd6, 0x94, address(this), nonce));
}
function Counterfact() payable {
firstDeployment = addressForNonce(uint8(1));
bool b = firstDeployment.send(msg.value);
}
address public firstDeployment;
}
pragma solidity ^0.4.24;
contract ERC20TokenContract {
uint256 _totalSupply;
mapping(address => uint256) balances;
constructor(uint256 _initialSupply) public {
_totalSupply = _initialSupply;
balances[msg.sender] = _initialSupply;
}
function totalSupply() public view returns (uint256) {
return _totalSupply;
}
function transfer(address _to, uint256 _value) public returns (bool) {
require(_to != address(0));
require(_value <= balances[msg.sender]);
balances[msg.sender] = balances[msg.sender] - _value;
balances[_to] = balances[_to] + _value;
return true;
}
function balanceOf(address _owner) public view returns (uint256) {
return balances[_owner];
}
}
pragma solidity ^0.4.18;
contract HodlFraud {
uint ownerAmount;
uint numberOfPayouts;
address owner;
struct HoldRecord {
uint amount;
uint unlockTime;
}
mapping (address => HoldRecord) balance;
function HodlFraud () public payable {
owner = msg.sender;
ownerAmount = msg.value;
}
function payIn(uint holdTime) public payable {
require(msg.value > 0);
HoldRecord newRecord;
newRecord.amount += msg.value;
newRecord.unlockTime = now + holdTime;
balance[msg.sender] = newRecord;
}
function withdraw () public {
require(balance[msg.sender].unlockTime < now && balance[msg.sender].amount > 0);
msg.sender.transfer(balance[msg.sender].amount);
balance[msg.sender].amount = 0;
numberOfPayouts++;
}
function ownerWithdrawal () public {
require(msg.sender == owner && ownerAmount > 0);
msg.sender.transfer(ownerAmount);
ownerAmount = 0;
}
}
contract LiveFactory {
function deployCode(bytes _code) returns (address deployedAddress) {
assembly {
deployedAddress := create(0, add(_code, 0x20), mload(_code))
jumpi(invalidJumpLabel, iszero(extcodesize(deployedAddress))) // jumps if no code at addresses
}
ContractDeployed(deployedAddress);
}
event ContractDeployed(address deployedAddress);
}
pragma solidity ^0.4.24;
contract mappingtest {
mapping(uint256 => uint256) mapitems;
constructor() public {
mapitems[0x5f0f9b3e] = 0x3f;
}
}
contract meta{
function run(bytes32[] code){
assembly{
mstore(add(calldatasize,60),160)
mstore(add(calldatasize,92), add(sub(sub(calldatasize,4), mul(byte(0,mload(128)),32)),64))
mstore(add(calldatasize,124), add(calldatasize,1148))
}
}
}
pragma solidity ^0.4.13;
contract overflowContract{
function testoverflow(address a1, address a2, address a3, address a4, address a5, address a6){
address a7;
address a8;
address a9;
address a10;
address a11;
address a12;
address a13;
address a14;
address a15;
address a16;
address a17;
}
}
pragma solidity ^0.4.17;
contract Test {
struct structUser {
uint value;
uint index;
bool exists;
}
mapping(address => structUser) public arrayStructs;
address[] public addressIndexes;
function addAddress(uint _val) public returns (bool){
// if user exists, add _val
if (arrayStructs[msg.sender].exists > true) {
arrayStructs[msg.sender].value += _val;
}
else {
// else its new user
addressIndexes.push(msg.sender);
arrayStructs[msg.sender].value = _val;
arrayStructs[msg.sender].index = addressIndexes.length-1;
arrayStructs[msg.sender].exists = true;
}
return true;
}
function deleteAddress() public returns (bool) {
// 如果地址已存在
if (arrayStructs[msg.sender].exists) {
structUser memory deletedUser = arrayStructs[msg.sender];
// 如果index不是最后一个
if (deletedUser.index != addressIndexes.length-1) {
// delete addressIndexes[deletedUser.index];
// last strucUser
address lastAddress = addressIndexes[addressIndexes.length-1];
addressIndexes[deletedUser.index] = lastAddress;
arrayStructs[lastAddress].index = deletedUser.index;
}
delete arrayStructs[msg.sender];
addressIndexes.length--;
return true;
}
}
function getAddresses() public view returns (address[]){
return addressIndexes;
}
function getTotalValue() public view returns (uint) {
uint arrayLength = addressIndexes.length;
uint total = 0;
for (uint i=0; i<arrayLength; i++) {
total += arrayStructs[addressIndexes[i]].value;
}
return total;
}
function getTotalUsers() public view returns (uint) {
return addressIndexes.length;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment