Skip to content

Instantly share code, notes, and snippets.

@jin10086
Created April 11, 2018 11:34
Show Gist options
  • Save jin10086/76fa5a054a1e643fc6843a38284cdfa8 to your computer and use it in GitHub Desktop.
Save jin10086/76fa5a054a1e643fc6843a38284cdfa8 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.21;
/// @author MinakoKojima (https://github.com/lychees)
contract SponsorToken{
address public owner;
mapping (address => bool) public admins;
struct Order {
address issuer;
uint256 tokenId;
uint256 price;
uint256 count;
mapping (address => uint256) paymans;
mapping (uint => address) sponsors;
}
Order[] private orderBook;
uint256 private orderBookSize;
uint256 private a = 100;
uint256 private b = 1000;
uint256 private c = 10000;
uint256 private prea = 50;
uint256 private preb = 60;
uint256 private prec = 70;
function SponsorToken() public {
owner = msg.sender;
admins[owner] = true;
}
/* Modifiers */
modifier onlyOwner() {
require(owner == msg.sender);
_;
}
modifier onlyAdmins() {
require(admins[msg.sender]);
_;
}
/* Owner */
function setOwner (address _owner) onlyOwner() public {
owner = _owner;
}
function addAdmin (address _admin) onlyOwner() public {
admins[_admin] = true;
}
function removeAdmin (address _admin) onlyOwner() public {
delete admins[_admin];
}
/* Withdraw */
function withdrawAll () onlyAdmins() public {
msg.sender.transfer(address(this).balance);
}
function withdrawAmount (uint256 _amount) onlyAdmins() public {
msg.sender.transfer(_amount);
}
/* ERC721 */
function name() public pure returns (string _name) {
return "Sponsor Token";
}
/* Read */
function isAdmin(address _admin) public view returns (bool _isAdmin) {
return admins[_admin];
}
function totalOrder() public view returns (uint256 _totalOrder) {
return orderBookSize;
}
function getOrder(uint256 _id) public view returns (uint256 _count, address _issuer, uint256 _tokenId, uint256 _price, uint256 _amount) {
return (orderBook[_id].count, orderBook[_id].issuer, orderBook[_id].tokenId, orderBook[_id].price, orderBook[_id].paymans[msg.sender]);
}
function getParams() public view returns (uint256 _a,uint256 _b, uint256 _c,uint256 _prea,uint256 _preb,uint256 _prec) {
return (a,b,c,prea,preb,prec);
}
/* Util */
function isContract(address addr) internal view returns (bool) {
uint size;
assembly { size := extcodesize(addr) } // solium-disable-line
return size > 0;
}
function setParams(string str, uint256 i) public onlyAdmins {
if (keccak256(str) == keccak256('a')){
a = i;
}else if (keccak256(str) == keccak256('b')){
b = i;
}else if (keccak256(str) == keccak256('c')){
c = i;
}else if (keccak256(str) == keccak256('prea')){
prea = i;
}else if (keccak256(str) == keccak256('preb')){
preb = i;
}else if (keccak256(str) == keccak256('prec')){
prec = i;
}
}
/* Sponsor */
function put(address _issuer, uint256 _tokenId, uint256 _price) public {
Issuer issuer = Issuer(_issuer);
require(issuer.ownerOf(_tokenId) == msg.sender);
if (orderBookSize == orderBook.length) {
orderBook.push(Order(_issuer, _tokenId, _price, 0));
} else {
orderBook[orderBookSize] = Order(_issuer, _tokenId, _price, 0);
}
orderBookSize += 1;
}
function sponsor(uint256 _id, uint256 _parent) public payable{
require(_id < orderBookSize);
require(msg.value >= orderBook[_id].price);
require(!isContract(msg.sender));
require(_parent <= orderBook[_id].count);
//
// require(orderBook[_id].paymans[_parent] != address(0));
// 给上级打钱
if (_parent != 0){
address parent = orderBook[_id].sponsors[_parent];
if (orderBook[_id].count < a){
orderBook[_id].paymans[parent]+= orderBook[_id].price * prea/100;
}else if (orderBook[_id].count < b){
orderBook[_id].paymans[parent]+= orderBook[_id].price * preb/100;
}else{
orderBook[_id].paymans[parent]+= orderBook[_id].price * prec/100;
}
}
//如果是第一次打赏
if (orderBook[_id].paymans[msg.sender] == 0){
orderBook[_id].paymans[msg.sender] = 1;
orderBook[_id].count++;
orderBook[_id].sponsors[orderBook[_id].count] = msg.sender;
}
// Issuer issuer = Issuer(orderBook[_id].issuer);
// issuer.ownerOf(orderBook[_id].tokenId).transfer(orderBook[_id].price*1/50); // 2% Cut-off
if (msg.value > orderBook[_id].price) {
msg.sender.transfer(msg.value - orderBook[_id].price);
}
}
}
interface Issuer {
function transferFrom(address _from, address _to, uint256 _tokenId) external;
function transfer(address _to, uint256 _tokenId) external;
function ownerOf (uint256 _tokenId) external view returns (address _owner);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment