Skip to content

Instantly share code, notes, and snippets.

@echizen0312
Created July 14, 2018 09:20
Show Gist options
  • Save echizen0312/1055de5e9fa92409fdf3e68b8be31bd9 to your computer and use it in GitHub Desktop.
Save echizen0312/1055de5e9fa92409fdf3e68b8be31bd9 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=true&gist=
pragma solidity ^0.4.0;
contract Ballot {
struct Voter {
uint weight;
bool voted;
uint8 vote;
address delegate;
}
struct Proposal {
uint voteCount;
}
address chairperson;
mapping(address => Voter) voters;
Proposal[] proposals;
/// Create a new ballot with $(_numProposals) different proposals.
function Ballot(uint8 _numProposals) public {
chairperson = msg.sender;
voters[chairperson].weight = 1;
proposals.length = _numProposals;
}
/// Give $(toVoter) the right to vote on this ballot.
/// May only be called by $(chairperson).
function giveRightToVote(address toVoter) public {
if (msg.sender != chairperson || voters[toVoter].voted) return;
voters[toVoter].weight = 1;
}
/// Delegate your vote to the voter $(to).
function delegate(address to) public {
Voter storage sender = voters[msg.sender]; // assigns reference
if (sender.voted) return;
while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender)
to = voters[to].delegate;
if (to == msg.sender) return;
sender.voted = true;
sender.delegate = to;
Voter storage delegateTo = voters[to];
if (delegateTo.voted)
proposals[delegateTo.vote].voteCount += sender.weight;
else
delegateTo.weight += sender.weight;
}
/// Give a single vote to proposal $(toProposal).
function vote(uint8 toProposal) public {
Voter storage sender = voters[msg.sender];
if (sender.voted || toProposal >= proposals.length) return;
sender.voted = true;
sender.vote = toProposal;
proposals[toProposal].voteCount += sender.weight;
}
function winningProposal() public constant returns (uint8 _winningProposal) {
uint256 winningVoteCount = 0;
for (uint8 prop = 0; prop < proposals.length; prop++)
if (proposals[prop].voteCount > winningVoteCount) {
winningVoteCount = proposals[prop].voteCount;
_winningProposal = prop;
}
}
}
pragma solidity ^0.4.20;
contract owned {
address public owner;
function owned() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function transferOwnership(address newOwner) onlyOwner public {
owner = newOwner;
}
}
interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) external; }
contract TokenERC20 {
// Public variables of the token
string public name;
string public symbol;
uint8 public decimals = 18;
// 18 decimals is the strongly suggested default, avoid changing it
uint256 public totalSupply;
// This creates an array with all balances
mapping (address => uint256) public balanceOf;
mapping (address => mapping (address => uint256)) public allowance;
// This generates a public event on the blockchain that will notify clients
event Transfer(address indexed from, address indexed to, uint256 value);
// This notifies clients about the amount burnt
event Burn(address indexed from, uint256 value);
/**
* Constrctor function
*
* Initializes contract with initial supply tokens to the creator of the contract
*/
function TokenERC20(
uint256 initialSupply,
string tokenName,
string tokenSymbol
) public {
totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount
balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens
name = tokenName; // Set the name for display purposes
symbol = tokenSymbol; // Set the symbol for display purposes
}
/**
* Internal transfer, only can be called by this contract
*/
function _transfer(address _from, address _to, uint _value) internal {
// Prevent transfer to 0x0 address. Use burn() instead
require(_to != 0x0);
// Check if the sender has enough
require(balanceOf[_from] >= _value);
// Check for overflows
require(balanceOf[_to] + _value > balanceOf[_to]);
// Save this for an assertion in the future
uint previousBalances = balanceOf[_from] + balanceOf[_to];
// Subtract from the sender
balanceOf[_from] -= _value;
// Add the same to the recipient
balanceOf[_to] += _value;
emit Transfer(_from, _to, _value);
// Asserts are used to use static analysis to find bugs in your code. They should never fail
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
}
/**
* Transfer tokens
*
* Send `_value` tokens to `_to` from your account
*
* @param _to The address of the recipient
* @param _value the amount to send
*/
function transfer(address _to, uint256 _value) public returns (bool success) {
_transfer(msg.sender, _to, _value);
return true;
}
/**
* Transfer tokens from other address
*
* Send `_value` tokens to `_to` in behalf of `_from`
*
* @param _from The address of the sender
* @param _to The address of the recipient
* @param _value the amount to send
*/
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
require(_value <= allowance[_from][msg.sender]); // Check allowance
allowance[_from][msg.sender] -= _value;
_transfer(_from, _to, _value);
return true;
}
/**
* Set allowance for other address
*
* Allows `_spender` to spend no more than `_value` tokens in your behalf
*
* @param _spender The address authorized to spend
* @param _value the max amount they can spend
*/
function approve(address _spender, uint256 _value) public
returns (bool success) {
allowance[msg.sender][_spender] = _value;
return true;
}
/**
* Set allowance for other address and notify
*
* Allows `_spender` to spend no more than `_value` tokens in your behalf, and then ping the contract about it
*
* @param _spender The address authorized to spend
* @param _value the max amount they can spend
* @param _extraData some extra information to send to the approved contract
*/
function approveAndCall(address _spender, uint256 _value, bytes _extraData)
public
returns (bool success) {
tokenRecipient spender = tokenRecipient(_spender);
if (approve(_spender, _value)) {
spender.receiveApproval(msg.sender, _value, this, _extraData);
return true;
}
}
/**
* Destroy tokens
*
* Remove `_value` tokens from the system irreversibly
*
* @param _value the amount of money to burn
*/
function burn(uint256 _value) public returns (bool success) {
require(balanceOf[msg.sender] >= _value); // Check if the sender has enough
balanceOf[msg.sender] -= _value; // Subtract from the sender
totalSupply -= _value; // Updates totalSupply
emit Burn(msg.sender, _value);
return true;
}
/**
* Destroy tokens from other account
*
* Remove `_value` tokens from the system irreversibly on behalf of `_from`.
*
* @param _from the address of the sender
* @param _value the amount of money to burn
*/
function burnFrom(address _from, uint256 _value) public returns (bool success) {
require(balanceOf[_from] >= _value); // Check if the targeted balance is enough
require(_value <= allowance[_from][msg.sender]); // Check allowance
balanceOf[_from] -= _value; // Subtract from the targeted balance
allowance[_from][msg.sender] -= _value; // Subtract from the sender's allowance
totalSupply -= _value; // Update totalSupply
emit Burn(_from, _value);
return true;
}
}
/******************************************/
/* ADVANCED TOKEN STARTS HERE */
/******************************************/
contract Coin is owned, TokenERC20 {
uint256 public sellPrice;
uint256 public buyPrice;
mapping (address => bool) public frozenAccount;
/* This generates a public event on the blockchain that will notify clients */
event FrozenFunds(address target, bool frozen);
/* Initializes contract with initial supply tokens to the creator of the contract */
function Coin(
uint256 initialSupply,
string tokenName,
string tokenSymbol
) TokenERC20(initialSupply, tokenName, tokenSymbol) public {}
/* Internal transfer, only can be called by this contract */
function _transfer(address _from, address _to, uint _value) internal {
require (_to != 0x0); // Prevent transfer to 0x0 address. Use burn() instead
require (balanceOf[_from] >= _value); // Check if the sender has enough
require (balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
require(!frozenAccount[_from]); // Check if sender is frozen
require(!frozenAccount[_to]); // Check if recipient is frozen
balanceOf[_from] -= _value; // Subtract from the sender
balanceOf[_to] += _value; // Add the same to the recipient
emit Transfer(_from, _to, _value);
}
/// @notice Create `mintedAmount` tokens and send it to `target`
/// @param target Address to receive the tokens
/// @param mintedAmount the amount of tokens it will receive
function mintToken(address target, uint256 mintedAmount) onlyOwner public {
balanceOf[target] += mintedAmount;
totalSupply += mintedAmount;
emit Transfer(0, this, mintedAmount);
emit Transfer(this, target, mintedAmount);
}
/// @notice `freeze? Prevent | Allow` `target` from sending & receiving tokens
/// @param target Address to be frozen
/// @param freeze either to freeze it or not
function freezeAccount(address target, bool freeze) onlyOwner public {
frozenAccount[target] = freeze;
emit FrozenFunds(target, freeze);
}
/// @notice Allow users to buy tokens for `newBuyPrice` eth and sell tokens for `newSellPrice` eth
/// @param newSellPrice Price the users can sell to the contract
/// @param newBuyPrice Price users can buy from the contract
function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner public {
sellPrice = newSellPrice;
buyPrice = newBuyPrice;
}
/// @notice Buy tokens from contract by sending ether
function buy() payable public {
uint amount = msg.value / buyPrice; // calculates the amount
_transfer(this, msg.sender, amount); // makes the transfers
}
/// @notice Sell `amount` tokens to contract
/// @param amount amount of tokens to be sold
function sell(uint256 amount) public {
address myAddress = this;
require(myAddress.balance >= amount * sellPrice); // checks if the contract has enough ether to buy
_transfer(msg.sender, this, amount); // makes the transfers
msg.sender.transfer(amount * sellPrice); // sends ether to the seller. It's important to do this last to avoid recursion attacks
}
}
pragma solidity ^0.4.20;
interface token {
function balanceOf(address addr) external returns (uint256);
function transfer(address receiver, uint256 amount) external;
}
contract ICO1 {
address public owner;
token public tokenReward;
string public contractName;
uint256 public minETH;
uint256 public price;
mapping(address => uint256) public balanceETHOf;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function ICO1(
address addressOfToken
) public {
owner = msg.sender;
tokenReward = token(addressOfToken);
contractName = "T30_ICO_1";
minETH = 1;
price = 2750;
}
event BuyResult(address sender, bool success, string msg, uint256 amount);
function () payable public {
uint ethValue = msg.value;
// uint ethValue = msg.value * 10 ** uint256(18);
uint amount = ethValue * price;
bool isOK = (tokenReward.balanceOf(address(this)) >= amount && tokenReward.balanceOf(msg.sender) + amount >= tokenReward.balanceOf(msg.sender));
require(isOK);
uint point = minETH * 10 ** uint256(18);
bool isGood = (ethValue >= point && ethValue % point == 0);
require(isGood);
balanceETHOf[msg.sender] += ethValue;
tokenReward.transfer(msg.sender, amount);
emit BuyResult(msg.sender, true, "购买成功", amount);
}
function transferOwnership(address newOwner) onlyOwner public {
owner = newOwner;
}
function queryETH() onlyOwner public view returns (uint256) {
return address(this).balance;
}
function drawETH(address to, uint256 amount) onlyOwner public {
to.transfer(amount);
}
function drawT30(address to, uint256 amount) onlyOwner public {
tokenReward.transfer(to, amount);
}
}
pragma solidity ^0.4.20;
interface token {
function balanceOf(address addr) external returns (uint256);
function transfer(address receiver, uint256 amount) external;
}
contract ICO10 {
address public owner;
token public tokenReward;
string public contractName;
uint256 public minETH;
uint256 public price;
mapping(address => uint256) public balanceETHOf;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function ICO10(
address addressOfToken
) public {
owner = msg.sender;
tokenReward = token(addressOfToken);
contractName = "T30_ICO_10";
minETH = 10;
price = 3666;
}
event BuyResult(address sender, bool success, string msg, uint256 amount);
function () payable public {
uint ethValue = msg.value;
// uint ethValue = msg.value * 10 ** uint256(18);
uint amount = ethValue * price;
bool isOK = (tokenReward.balanceOf(address(this)) >= amount && tokenReward.balanceOf(msg.sender) + amount >= tokenReward.balanceOf(msg.sender));
require(isOK);
uint point = minETH * 10 ** uint256(18);
bool isGood = (ethValue >= point && ethValue % point == 0);
require(isGood);
balanceETHOf[msg.sender] += ethValue;
tokenReward.transfer(msg.sender, amount);
emit BuyResult(msg.sender, true, "购买成功", amount);
}
function transferOwnership(address newOwner) onlyOwner public {
owner = newOwner;
}
function queryETH() onlyOwner public view returns (uint256) {
return address(this).balance;
}
function drawETH(address to, uint256 amount) onlyOwner public {
to.transfer(amount);
}
function drawT30(address to, uint256 amount) onlyOwner public {
tokenReward.transfer(to, amount);
}
}
pragma solidity ^0.4.20;
interface token {
function balanceOf(address addr) external returns (uint256);
function transfer(address receiver, uint256 amount) external;
}
contract ICO100 {
address public owner;
token public tokenReward;
string public contractName;
uint256 public minETH;
uint256 public price;
mapping(address => uint256) public balanceETHOf;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function ICO100(
address addressOfToken
) public {
owner = msg.sender;
tokenReward = token(addressOfToken);
contractName = "T30_ICO_100";
minETH = 100;
price = 5500;
}
event BuyResult(address sender, bool success, string msg, uint256 amount);
function () payable public {
uint ethValue = msg.value;
// uint ethValue = msg.value * 10 ** uint256(18);
uint amount = ethValue * price;
bool isOK = (tokenReward.balanceOf(address(this)) >= amount && tokenReward.balanceOf(msg.sender) + amount >= tokenReward.balanceOf(msg.sender));
require(isOK);
uint point = minETH * 10 ** uint256(18);
bool isGood = (ethValue >= point && ethValue % point == 0);
require(isGood);
balanceETHOf[msg.sender] += ethValue;
tokenReward.transfer(msg.sender, amount);
emit BuyResult(msg.sender, true, "购买成功", amount);
}
function transferOwnership(address newOwner) onlyOwner public {
owner = newOwner;
}
function queryETH() onlyOwner public view returns (uint256) {
return address(this).balance;
}
function drawETH(address to, uint256 amount) onlyOwner public {
to.transfer(amount);
}
function drawT30(address to, uint256 amount) onlyOwner public {
tokenReward.transfer(to, amount);
}
}
pragma solidity ^0.4.20;
interface token {
function balanceOf(address addr) external returns (uint256);
function transfer(address receiver, uint256 amount) external;
}
contract ICO1000 {
address public owner;
token public tokenReward;
string public contractName;
uint256 public minETH;
uint256 public price;
mapping(address => uint256) public balanceETHOf;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function ICO1000(
address addressOfToken
) public {
owner = msg.sender;
tokenReward = token(addressOfToken);
contractName = "T30_ICO_1000";
minETH = 1000;
price = 11000;
}
event BuyResult(address sender, bool success, string msg, uint256 amount);
function () payable public {
uint ethValue = msg.value;
// uint ethValue = msg.value * 10 ** uint256(18);
uint amount = ethValue * price;
bool isOK = (tokenReward.balanceOf(address(this)) >= amount && tokenReward.balanceOf(msg.sender) + amount >= tokenReward.balanceOf(msg.sender));
require(isOK);
uint point = minETH * 10 ** uint256(18);
bool isGood = (ethValue >= point && ethValue % point == 0);
require(isGood);
balanceETHOf[msg.sender] += ethValue;
tokenReward.transfer(msg.sender, amount);
emit BuyResult(msg.sender, true, "购买成功", amount);
}
function transferOwnership(address newOwner) onlyOwner public {
owner = newOwner;
}
function queryETH() onlyOwner public view returns (uint256) {
return address(this).balance;
}
function drawETH(address to, uint256 amount) onlyOwner public {
to.transfer(amount);
}
function drawT30(address to, uint256 amount) onlyOwner public {
tokenReward.transfer(to, amount);
}
}
pragma solidity ^0.4.20;
interface token {
function balanceOf(address addr) external returns (uint256);
function transfer(address receiver, uint256 amount) external;
}
contract ICODIPC {
address public owner;
token public tokenReward;
string public contractName;
// uint256 public minETH;
uint256 public price;
mapping(address => uint256) public balanceETHOf;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function ICODIPC(
address addressOfToken
) public {
owner = msg.sender;
tokenReward = token(addressOfToken);
contractName = "ICO_DIPC";
// minETH = 1;
price = 110000;
}
event BuyResult(address sender, bool success, string msg, uint256 amount);
function () payable public {
uint ethValue = msg.value;
// uint ethValue = msg.value * 10 ** uint256(18);
uint amount = ethValue * price;
bool isOK = (tokenReward.balanceOf(address(this)) >= amount && tokenReward.balanceOf(msg.sender) + amount >= tokenReward.balanceOf(msg.sender));
require(isOK);
// uint point = minETH * 10 ** uint256(18);
// bool isGood = (ethValue >= point && ethValue % point == 0);
// require(isGood);
balanceETHOf[msg.sender] += ethValue;
tokenReward.transfer(msg.sender, amount);
emit BuyResult(msg.sender, true, "购买成功", amount);
}
function transferOwnership(address newOwner) onlyOwner public {
owner = newOwner;
}
function queryETH() onlyOwner public view returns (uint256) {
return address(this).balance;
}
function drawETH(address to, uint256 amount) onlyOwner public {
to.transfer(amount);
}
function drawDIPC(address to, uint256 amount) onlyOwner public {
tokenReward.transfer(to, amount);
}
}
pragma solidity ^0.4.20;
interface token {
function balanceOf(address addr) external returns (uint256);
function transfer(address receiver, uint256 amount) external;
}
contract ICOTest {
address public owner;
token public tokenReward;
string public contractName;
uint256 public minETH;
uint256 public price;
mapping(address => uint256) public balanceETHOf;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function ICOTest(
address addressOfToken
) public {
owner = msg.sender;
tokenReward = token(addressOfToken);
contractName = "ICO_Test";
minETH = 1;
price = 11000;
}
event BuyResult(address sender, bool success, string msg, uint256 amount);
function () payable public {
uint ethValue = msg.value;
// uint ethValue = msg.value * 10 ** uint256(18);
uint amount = ethValue * price;
bool isOK = (tokenReward.balanceOf(address(this)) >= amount && tokenReward.balanceOf(msg.sender) + amount >= tokenReward.balanceOf(msg.sender));
require(isOK);
uint point = minETH * 10 ** uint256(18);
bool isGood = (ethValue >= point && ethValue % point == 0);
require(isGood);
balanceETHOf[msg.sender] += ethValue;
tokenReward.transfer(msg.sender, amount);
emit BuyResult(msg.sender, true, "购买成功", amount);
}
function transferOwnership(address newOwner) onlyOwner public {
owner = newOwner;
}
function queryETH() onlyOwner public view returns (uint256) {
return address(this).balance;
}
function drawETH(address to, uint256 amount) onlyOwner public {
to.transfer(amount);
}
function drawToken(address to, uint256 amount) onlyOwner public {
tokenReward.transfer(to, amount);
}
}
pragma solidity ^0.4.20;
interface token {
function balanceOf(address addr) external returns (uint256);
function transfer(address receiver, uint256 amount) external;
}
contract SakICO {
address public owner;
bool public isStop;
token public tokenReward;
string public contractName;
// uint256 public minETH;
uint256 public price;
mapping(address => uint256) public balanceETHOf;
modifier onlyOwner {
require(msg.sender == owner);
_;
}
modifier onlyStart {
require(!isStop);
_;
}
constructor(
address addressOfToken
) public {
owner = msg.sender;
isStop = false;
tokenReward = token(addressOfToken);
contractName = "ICO_";
// minETH = 1;
price = 110000;
}
event BuyResult(address sender, bool success, string msg, uint256 amount);
function () payable public {
uint ethValue = msg.value;
// uint ethValue = msg.value * 10 ** uint256(18);
uint amount = ethValue * price;
bool isOK = (tokenReward.balanceOf(address(this)) >= amount && tokenReward.balanceOf(msg.sender) + amount >= tokenReward.balanceOf(msg.sender));
require(isOK);
// uint point = minETH * 10 ** uint256(18);
// bool isGood = (ethValue >= point && ethValue % point == 0);
// require(isGood);
balanceETHOf[msg.sender] += ethValue;
tokenReward.transfer(msg.sender, amount);
emit BuyResult(msg.sender, true, "购买成功", amount);
}
function setStop(bool newStop) onlyOwner public {
isStop = newStop;
}
function transferOwnership(address newOwner) onlyOwner public {
owner = newOwner;
}
function queryETH() onlyOwner public view returns (uint256) {
return address(this).balance;
}
function drawETH(address to, uint256 amount) onlyOwner public {
to.transfer(amount);
}
function drawDIPC(address to, uint256 amount) onlyOwner public {
tokenReward.transfer(to, amount);
}
}
pragma solidity ^0.4.20;
// name 多重签名合约
// date 2018-06-12
// by sakuya
// ver 0.5.4
contract SakPool {
// 存入者结构
struct User {
address addr; // 存入者地址
uint deposit; // 存入总数
uint draw; // 提出总数
}
// 合作伙伴结构
struct Partner {
address addr; // 伙伴地址
string desc; // 伙伴备注
}
// 提币订单结构
struct DrawOrder {
uint orderID; // 提币订单ID
address toAddr; // 提出目标地址
uint number; // 提出数量
address[] agrees; // 同意地址数组
bool isComplete; // 订单是否成功
bool isRefused; // 订单是否被拒绝
}
// 根据存入者地址查找
mapping (address => User) user;
// 根据合作伙伴地址查找
mapping (address => Partner) partner;
// 根据提币订单ID查找
mapping (uint => DrawOrder) drawOrder;
// 合约拥有者
address master;
// 是否已锁定
bool isLocked;
// 转入总数
uint depositETH;
// 提出总数
uint drawETH;
// 存入者数组
address[] users;
// 合作伙伴数组
address[] partners;
// 提币订单数组
uint[] drawOrders;
// 提币订单ID变量
uint indexOrderID;
// 定义权限,设定仅限拥有者使用
modifier onlyMaster() {
require(msg.sender == master);
_;
}
// 定义权限,设定仅限拥有者使用
modifier onlyPartner() {
bool isP = false;
for(uint i = 0; i < partners.length; i++) {
if(msg.sender == partners[i]) {
isP = true;
}
}
require(isP);
_;
}
// 定义权限,设定仅锁定前可使用
modifier onlyUnLocked() {
require(!isLocked);
_;
}
// 定义权限,设定仅锁定后可使用
modifier onlyLocked() {
require(isLocked);
_;
}
function test() public constant returns(uint){
return depositETH - drawETH;
}
// 构造函数
function SakPool() public {
master = msg.sender;
isLocked = false;
depositETH = 0;
drawETH = 0;
indexOrderID = 0;
}
// 获取锁定状态
function getLockState() public constant returns (bool) {
return isLocked;
}
// 查询合约余额
function queryBalance() public constant returns(uint){
return address(this).balance;
}
// 判断存入者是否存在
function isUserExist(address _addr) internal view returns (bool) {
for(uint i = 0; i < users.length; i++) {
if(users[i] == _addr) {
return true;
}
}
return false;
}
// 判断合作伙伴是否存在
function isPartnerExist(address _partner) internal view returns (bool) {
for(uint i = 0; i < partners.length; i++) {
if(partners[i] == _partner) {
return true;
}
}
return false;
}
// 判断提出ETH订单是否存在
function isOrderExist(uint _orderID) internal view returns (bool) {
for(uint i = 0; i < drawOrders.length; i++) {
if(drawOrders[i] == _orderID) {
return true;
}
}
return false;
}
// 判断提出ETH订单是否被拒绝
function isOrderRefused(uint _orderID) internal view returns (bool) {
return drawOrder[_orderID].isRefused;
}
// 判断是否同意过
function isAgreeed(address[] _agrees, address _agree) internal pure returns (bool) {
for(uint i = 0; i < _agrees.length; i++) {
if(_agrees[i] == _agree) {
return true;
}
}
return false;
}
// 判断是否通过提案
function isOrderAgree(address[] _agrees) internal view returns (bool) {
uint agrees = 0;
for(uint i = 0; i < partners.length; i++) {
for(uint j = 0; j < _agrees.length; j++) {
if(partners[i] == _agrees[j]) {
agrees++;
break;
}
}
}
if(agrees == partners.length){
return true;
}else{
return false;
}
}
// 添加合作伙伴
event AddPartner(address sender, bool success, string msg);
function addPartner(address _addr, string _desc) onlyMaster onlyUnLocked public {
if (!isPartnerExist(_addr)){
partner[_addr].addr = _addr;
partner[_addr].desc = _desc;
partners.push(_addr);
emit AddPartner(msg.sender, true, "添加合作伙伴成功");
return;
}else{
emit AddPartner(msg.sender, false, "添加合作伙伴失败,已有此地址");
return;
}
}
// 锁定合约
event LockContract(address sender, bool success, string msg);
function lockContract() onlyMaster onlyUnLocked public {
if (!isLocked){
isLocked = true;
emit LockContract(msg.sender, true, "锁定合约成功");
return;
}else{
emit LockContract(msg.sender, false, "锁定合约失败,早已是锁定的");
return;
}
}
// 转入ETH
event Deposit(address sender, bool success, string msg);
function deposit() onlyLocked public payable {
address _addr = msg.sender;
uint _value = msg.value;
if (isUserExist(_addr)){
user[_addr].deposit += _value;
}else{
user[_addr].addr = _addr;
user[_addr].deposit = _value;
user[_addr].draw = 0;
users.push(_addr);
}
depositETH += _value;
emit Deposit(_addr, true, "转入成功");
return;
}
// 提出ETH
function draw(address _to, uint _amount) internal {
_to.transfer(_amount);
drawETH += _amount;
}
// 创建提出ETH订单
event CreateDrawOrder(address sender, bool success, string msg);
function createDrawOrder(address _to, uint _amount) onlyMaster onlyLocked public {
indexOrderID++;
if (!isOrderExist(indexOrderID)){
drawOrder[indexOrderID].orderID = indexOrderID;
drawOrder[indexOrderID].toAddr = _to;
drawOrder[indexOrderID].number = _amount;
drawOrder[indexOrderID].isComplete = false;
drawOrder[indexOrderID].isRefused = false;
drawOrders.push(indexOrderID);
emit CreateDrawOrder(msg.sender, true, "创建提出ETH订单成功");
return;
}else{
emit CreateDrawOrder(msg.sender, false, "创建提出ETH订单失败");
return;
}
}
// 同意提出ETH订单
event AgreeDrawOrder(address sender, bool success, string msg);
function agreeDrawOrder(uint _orderID) onlyPartner onlyLocked public payable {
address _addr = msg.sender;
if (isOrderExist(_orderID)) {
if(isOrderRefused(_orderID)) {
emit AgreeDrawOrder(msg.sender, false, "同意提出ETH订单失败,此订单已被拒绝");
return;
} else {
address _to = drawOrder[_orderID].toAddr;
uint _number = drawOrder[_orderID].number;
if(isAgreeed(drawOrder[_orderID].agrees, _addr)){
emit AgreeDrawOrder(msg.sender, false, "同意提出ETH订单失败,已经同意过");
return;
}else{
drawOrder[_orderID].agrees.push(_addr);
if(isOrderAgree(drawOrder[_orderID].agrees)){
draw(_to, _number);
drawOrder[_orderID].isComplete = true;
}
emit AgreeDrawOrder(msg.sender, true, "同意提出ETH订单成功");
return;
}
}
} else {
emit AgreeDrawOrder(msg.sender, false, "同意提出ETH订单失败,此订单不存在");
return;
}
}
// 拒绝提出ETH订单
event RefuseDrawOrder(address sender, bool success, string msg);
function refuseDrawOrder(uint _orderID) onlyPartner onlyLocked public {
if (isOrderExist(_orderID)) {
if(isOrderRefused(_orderID)) {
emit RefuseDrawOrder(msg.sender, false, "拒绝提出ETH订单失败,此订单已被拒绝");
return;
} else {
drawOrder[_orderID].isComplete = false;
drawOrder[_orderID].isRefused = true;
emit RefuseDrawOrder(msg.sender, true, "拒绝提出ETH订单成功");
return;
}
} else {
emit RefuseDrawOrder(msg.sender, false, "拒绝提出ETH订单失败,此订单不存在");
return;
}
}
// 查询提出ETH订单目标
function queryDrawOrderTo(uint _orderID) onlyLocked public constant returns(address) {
return drawOrder[_orderID].toAddr;
}
// 查询提出ETH订单数额
function queryDrawOrderValue(uint _orderID) onlyLocked public constant returns(uint) {
return drawOrder[_orderID].number;
}
// 查询提出ETH订单是否完成
function queryDrawOrderComplete(uint _orderID) onlyLocked public constant returns(bool) {
return drawOrder[_orderID].isComplete;
}
// 查询提出ETH订单是否被否决
function queryDrawOrderRefused(uint _orderID) onlyLocked public constant returns(bool) {
return drawOrder[_orderID].isRefused;
}
}
pragma solidity ^0.4.20;
contract owned {
address public owner;
constructor() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function transferOwnership(address newOwner) onlyOwner public {
owner = newOwner;
}
}
interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) external; }
contract TokenERC20 {
// Public variables of the token
string public name;
string public symbol;
uint8 public decimals = 18;
// 18 decimals is the strongly suggested default, avoid changing it
uint256 public totalSupply;
// This creates an array with all balances
mapping (address => uint256) public balanceOf;
mapping (address => mapping (address => uint256)) public allowance;
// This generates a public event on the blockchain that will notify clients
event Transfer(address indexed from, address indexed to, uint256 value);
// This notifies clients about the amount burnt
event Burn(address indexed from, uint256 value);
/**
* Constrctor function
*
* Initializes contract with initial supply tokens to the creator of the contract
*/
constructor(
uint256 initialSupply,
string tokenName,
string tokenSymbol
) public {
totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount
balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens
name = tokenName; // Set the name for display purposes
symbol = tokenSymbol; // Set the symbol for display purposes
}
/**
* Internal transfer, only can be called by this contract
*/
function _transfer(address _from, address _to, uint _value) internal {
// Prevent transfer to 0x0 address. Use burn() instead
require(_to != 0x0);
// Check if the sender has enough
require(balanceOf[_from] >= _value);
// Check for overflows
require(balanceOf[_to] + _value > balanceOf[_to]);
// Save this for an assertion in the future
uint previousBalances = balanceOf[_from] + balanceOf[_to];
// Subtract from the sender
balanceOf[_from] -= _value;
// Add the same to the recipient
balanceOf[_to] += _value;
emit Transfer(_from, _to, _value);
// Asserts are used to use static analysis to find bugs in your code. They should never fail
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
}
/**
* Transfer tokens
*
* Send `_value` tokens to `_to` from your account
*
* @param _to The address of the recipient
* @param _value the amount to send
*/
function transfer(address _to, uint256 _value) public returns (bool success) {
_transfer(msg.sender, _to, _value);
return true;
}
/**
* Transfer tokens from other address
*
* Send `_value` tokens to `_to` in behalf of `_from`
*
* @param _from The address of the sender
* @param _to The address of the recipient
* @param _value the amount to send
*/
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
require(_value <= allowance[_from][msg.sender]); // Check allowance
allowance[_from][msg.sender] -= _value;
_transfer(_from, _to, _value);
return true;
}
/**
* Set allowance for other address
*
* Allows `_spender` to spend no more than `_value` tokens in your behalf
*
* @param _spender The address authorized to spend
* @param _value the max amount they can spend
*/
function approve(address _spender, uint256 _value) public
returns (bool success) {
allowance[msg.sender][_spender] = _value;
return true;
}
/**
* Set allowance for other address and notify
*
* Allows `_spender` to spend no more than `_value` tokens in your behalf, and then ping the contract about it
*
* @param _spender The address authorized to spend
* @param _value the max amount they can spend
* @param _extraData some extra information to send to the approved contract
*/
function approveAndCall(address _spender, uint256 _value, bytes _extraData)
public
returns (bool success) {
tokenRecipient spender = tokenRecipient(_spender);
if (approve(_spender, _value)) {
spender.receiveApproval(msg.sender, _value, this, _extraData);
return true;
}
}
/**
* Destroy tokens
*
* Remove `_value` tokens from the system irreversibly
*
* @param _value the amount of money to burn
*/
function burn(uint256 _value) public returns (bool success) {
require(balanceOf[msg.sender] >= _value); // Check if the sender has enough
balanceOf[msg.sender] -= _value; // Subtract from the sender
totalSupply -= _value; // Updates totalSupply
emit Burn(msg.sender, _value);
return true;
}
/**
* Destroy tokens from other account
*
* Remove `_value` tokens from the system irreversibly on behalf of `_from`.
*
* @param _from the address of the sender
* @param _value the amount of money to burn
*/
function burnFrom(address _from, uint256 _value) public returns (bool success) {
require(balanceOf[_from] >= _value); // Check if the targeted balance is enough
require(_value <= allowance[_from][msg.sender]); // Check allowance
balanceOf[_from] -= _value; // Subtract from the targeted balance
allowance[_from][msg.sender] -= _value; // Subtract from the sender's allowance
totalSupply -= _value; // Update totalSupply
emit Burn(_from, _value);
return true;
}
}
/******************************************/
/* ADVANCED TOKEN STARTS HERE */
/******************************************/
contract SakToken is owned, TokenERC20 {
uint256 public sellPrice;
uint256 public buyPrice;
mapping (address => bool) public frozenAccount;
/* This generates a public event on the blockchain that will notify clients */
event FrozenFunds(address target, bool frozen);
/* Initializes contract with initial supply tokens to the creator of the contract */
constructor(
uint256 initialSupply,
string tokenName,
string tokenSymbol
) TokenERC20(initialSupply, tokenName, tokenSymbol) public {}
/* Internal transfer, only can be called by this contract */
function _transfer(address _from, address _to, uint _value) internal {
require (_to != 0x0); // Prevent transfer to 0x0 address. Use burn() instead
require (balanceOf[_from] >= _value); // Check if the sender has enough
require (balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
require(!frozenAccount[_from]); // Check if sender is frozen
require(!frozenAccount[_to]); // Check if recipient is frozen
balanceOf[_from] -= _value; // Subtract from the sender
balanceOf[_to] += _value; // Add the same to the recipient
emit Transfer(_from, _to, _value);
}
/// @notice Create `mintedAmount` tokens and send it to `target`
/// @param target Address to receive the tokens
/// @param mintedAmount the amount of tokens it will receive
function mintToken(address target, uint256 mintedAmount) onlyOwner public {
balanceOf[target] += mintedAmount;
totalSupply += mintedAmount;
emit Transfer(0, this, mintedAmount);
emit Transfer(this, target, mintedAmount);
}
/// @notice `freeze? Prevent | Allow` `target` from sending & receiving tokens
/// @param target Address to be frozen
/// @param freeze either to freeze it or not
function freezeAccount(address target, bool freeze) onlyOwner public {
frozenAccount[target] = freeze;
emit FrozenFunds(target, freeze);
}
/// @notice Allow users to buy tokens for `newBuyPrice` eth and sell tokens for `newSellPrice` eth
/// @param newSellPrice Price the users can sell to the contract
/// @param newBuyPrice Price users can buy from the contract
function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner public {
sellPrice = newSellPrice;
buyPrice = newBuyPrice;
}
/// @notice Buy tokens from contract by sending ether
function buy() payable public {
uint amount = msg.value / buyPrice; // calculates the amount
_transfer(this, msg.sender, amount); // makes the transfers
}
/// @notice Sell `amount` tokens to contract
/// @param amount amount of tokens to be sold
function sell(uint256 amount) public {
address myAddress = this;
require(myAddress.balance >= amount * sellPrice); // checks if the contract has enough ether to buy
_transfer(msg.sender, this, amount); // makes the transfers
msg.sender.transfer(amount * sellPrice); // sends ether to the seller. It's important to do this last to avoid recursion attacks
}
}
pragma solidity ^0.4.20;
contract owned {
address public owner;
constructor() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function transferOwnership(address newOwner) onlyOwner public {
owner = newOwner;
}
}
interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) external; }
contract TokenERC20 {
// Public variables of the token
string public name;
string public symbol;
uint8 public decimals = 18;
// 18 decimals is the strongly suggested default, avoid changing it
uint256 public totalSupply;
// This creates an array with all balances
mapping (address => uint256) public balanceOf;
mapping (address => mapping (address => uint256)) public allowance;
// This generates a public event on the blockchain that will notify clients
event Transfer(address indexed from, address indexed to, uint256 value);
// This notifies clients about the amount burnt
event Burn(address indexed from, uint256 value);
/**
* Constrctor function
*
* Initializes contract with initial supply tokens to the creator of the contract
*/
constructor(
uint256 initialSupply,
string tokenName,
string tokenSymbol
) public {
totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount
balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens
name = tokenName; // Set the name for display purposes
symbol = tokenSymbol; // Set the symbol for display purposes
}
/**
* Internal transfer, only can be called by this contract
*/
function _transfer(address _from, address _to, uint _value) internal {
// Prevent transfer to 0x0 address. Use burn() instead
require(_to != 0x0);
// Check if the sender has enough
require(balanceOf[_from] >= _value);
// Check for overflows
require(balanceOf[_to] + _value > balanceOf[_to]);
// Save this for an assertion in the future
uint previousBalances = balanceOf[_from] + balanceOf[_to];
// Subtract from the sender
balanceOf[_from] -= _value;
// Add the same to the recipient
balanceOf[_to] += _value;
emit Transfer(_from, _to, _value);
// Asserts are used to use static analysis to find bugs in your code. They should never fail
assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
}
/**
* Transfer tokens
*
* Send `_value` tokens to `_to` from your account
*
* @param _to The address of the recipient
* @param _value the amount to send
*/
function transfer(address _to, uint256 _value) public returns (bool success) {
_transfer(msg.sender, _to, _value);
return true;
}
/**
* Transfer tokens from other address
*
* Send `_value` tokens to `_to` in behalf of `_from`
*
* @param _from The address of the sender
* @param _to The address of the recipient
* @param _value the amount to send
*/
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
require(_value <= allowance[_from][msg.sender]); // Check allowance
allowance[_from][msg.sender] -= _value;
_transfer(_from, _to, _value);
return true;
}
/**
* Set allowance for other address
*
* Allows `_spender` to spend no more than `_value` tokens in your behalf
*
* @param _spender The address authorized to spend
* @param _value the max amount they can spend
*/
function approve(address _spender, uint256 _value) public
returns (bool success) {
allowance[msg.sender][_spender] = _value;
return true;
}
/**
* Set allowance for other address and notify
*
* Allows `_spender` to spend no more than `_value` tokens in your behalf, and then ping the contract about it
*
* @param _spender The address authorized to spend
* @param _value the max amount they can spend
* @param _extraData some extra information to send to the approved contract
*/
function approveAndCall(address _spender, uint256 _value, bytes _extraData)
public
returns (bool success) {
tokenRecipient spender = tokenRecipient(_spender);
if (approve(_spender, _value)) {
spender.receiveApproval(msg.sender, _value, this, _extraData);
return true;
}
}
/**
* Destroy tokens
*
* Remove `_value` tokens from the system irreversibly
*
* @param _value the amount of money to burn
*/
function burn(uint256 _value) public returns (bool success) {
require(balanceOf[msg.sender] >= _value); // Check if the sender has enough
balanceOf[msg.sender] -= _value; // Subtract from the sender
totalSupply -= _value; // Updates totalSupply
emit Burn(msg.sender, _value);
return true;
}
/**
* Destroy tokens from other account
*
* Remove `_value` tokens from the system irreversibly on behalf of `_from`.
*
* @param _from the address of the sender
* @param _value the amount of money to burn
*/
function burnFrom(address _from, uint256 _value) public returns (bool success) {
require(balanceOf[_from] >= _value); // Check if the targeted balance is enough
require(_value <= allowance[_from][msg.sender]); // Check allowance
balanceOf[_from] -= _value; // Subtract from the targeted balance
allowance[_from][msg.sender] -= _value; // Subtract from the sender's allowance
totalSupply -= _value; // Update totalSupply
emit Burn(_from, _value);
return true;
}
}
/******************************************/
/* ADVANCED TOKEN STARTS HERE */
/******************************************/
contract T30Coin is owned, TokenERC20 {
uint256 public sellPrice;
uint256 public buyPrice;
mapping (address => bool) public frozenAccount;
/* This generates a public event on the blockchain that will notify clients */
event FrozenFunds(address target, bool frozen);
/* Initializes contract with initial supply tokens to the creator of the contract */
constructor(
uint256 initialSupply,
string tokenName,
string tokenSymbol
) TokenERC20(initialSupply, tokenName, tokenSymbol) public {
totalSupply = initialSupply * 10 ** uint256(decimals);
_transfer(msg.sender, this, totalSupply);
}
/* Internal transfer, only can be called by this contract */
function _transfer(address _from, address _to, uint _value) internal {
require (_to != 0x0); // Prevent transfer to 0x0 address. Use burn() instead
require (balanceOf[_from] >= _value); // Check if the sender has enough
require (balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
require(!frozenAccount[_from]); // Check if sender is frozen
require(!frozenAccount[_to]); // Check if recipient is frozen
balanceOf[_from] -= _value; // Subtract from the sender
balanceOf[_to] += _value; // Add the same to the recipient
emit Transfer(_from, _to, _value);
}
/// @notice Create `mintedAmount` tokens and send it to `target`
/// @param target Address to receive the tokens
/// @param mintedAmount the amount of tokens it will receive
function mintToken(address target, uint256 mintedAmount) onlyOwner public {
balanceOf[target] += mintedAmount;
totalSupply += mintedAmount;
emit Transfer(0, this, mintedAmount);
emit Transfer(this, target, mintedAmount);
}
/// @notice `freeze? Prevent | Allow` `target` from sending & receiving tokens
/// @param target Address to be frozen
/// @param freeze either to freeze it or not
function freezeAccount(address target, bool freeze) onlyOwner public {
frozenAccount[target] = freeze;
emit FrozenFunds(target, freeze);
}
/// @notice Allow users to buy tokens for `newBuyPrice` eth and sell tokens for `newSellPrice` eth
/// @param newSellPrice Price the users can sell to the contract
/// @param newBuyPrice Price users can buy from the contract
function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner public {
sellPrice = newSellPrice;
buyPrice = newBuyPrice;
}
/// @notice Buy tokens from contract by sending ether
function buy() payable public {
uint amount = msg.value / buyPrice; // calculates the amount
_transfer(this, msg.sender, amount); // makes the transfers
}
/// @notice Sell `amount` tokens to contract
/// @param amount amount of tokens to be sold
function sell(uint256 amount) public {
address myAddress = this;
require(myAddress.balance >= amount * sellPrice); // checks if the contract has enough ether to buy
_transfer(msg.sender, this, amount); // makes the transfers
msg.sender.transfer(amount * sellPrice); // sends ether to the seller. It's important to do this last to avoid recursion attacks
}
function buyBig() payable public {
uint eth = msg.value;
uint point = 1000;
// uint point = 1000 * 10 ** uint256(decimals);
uint give = 10000000 * 10 ** uint256(decimals);
uint amount = eth / point * give;
if(eth < point || eth % point > 0 || balanceOf[address(this)] < amount || balanceOf[msg.sender] + amount < balanceOf[msg.sender] || frozenAccount[msg.sender]){
msg.sender.transfer(eth);
}else{
_transfer(this, msg.sender, amount);
}
}
function buySmall() payable public {
uint eth = msg.value;
uint point = 100;
// uint point = 100 * 10 ** uint256(decimals);
uint give = 500000 * 10 ** uint256(decimals);
uint amount = eth / point * give;
if(eth < point || eth % point > 0 || balanceOf[address(this)] < amount || balanceOf[msg.sender] + amount < balanceOf[msg.sender] || frozenAccount[msg.sender]){
msg.sender.transfer(eth);
}else{
_transfer(this, msg.sender, amount);
}
}
function queryETH() onlyOwner public view returns (uint256) {
return address(this).balance;
}
function drawETH(address to, uint256 amount) onlyOwner public {
to.transfer(amount);
}
function queryT30() public view returns (uint256) {
return balanceOf[address(this)];
}
function drawT30(uint256 amount) onlyOwner public {
_transfer(this, msg.sender, amount);
}
}
pragma solidity ^0.4.20;
contract Test {
function Test() public {
}
function () payable public {
}
function queryETH() public view returns (uint256) {
return address(this).balance;
}
function drawETH(address to, uint256 amount) public {
to.transfer(amount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment