Created
March 2, 2017 21:35
-
-
Save 3esmit/74df451d3d06e91307449b93337227f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.8; | |
// ECR20 standard token interface | |
contract Token { | |
uint public totalSupply; | |
function balanceOf(address who) constant returns (uint); | |
function allowance(address owner, address spender) constant returns (uint); | |
function transfer(address to, uint value) returns (bool ok); | |
function transferFrom(address from, address to, uint value) returns (bool ok); | |
function approve(address spender, uint value) returns (bool ok); | |
event Transfer(address indexed from, address indexed to, uint value); | |
event Approval(address indexed owner, address indexed spender, uint value); | |
} | |
/** | |
* AbstractCoin ECR20-compliant token contract | |
* Child should implement initial supply or minting and overwite base | |
* Based on BasicCoin by Parity Team (Ethcore), 2016. | |
* By Ricardo Guilherme Schmidt | |
* Released under the Apache Licence 2. | |
*/ | |
// AbstractCoin, ECR20 tokens that all belong to the owner for sending around | |
contract AbstractCoin is Token { | |
// the base, tokens denoted in micros | |
uint constant public base = 0; | |
// storage and mapping of all balances & allowances | |
mapping (address => Account) accounts; | |
// this is as basic as can be, only the associated balance & allowances | |
struct Account { | |
uint balance; | |
mapping (address => uint) allowanceOf; | |
} | |
// the balance should be available | |
modifier when_owns(address _owner, uint _amount) { | |
if (accounts[_owner].balance < _amount) throw; | |
_; | |
} | |
// an allowance should be available | |
modifier when_has_allowance(address _owner, address _spender, uint _amount) { | |
if (accounts[_owner].allowanceOf[_spender] < _amount) throw; | |
_; | |
} | |
// A helper to notify if overflow occurs | |
modifier safe_add(uint a, uint b) { | |
if (a + b < a && a + b < b) throw; | |
_; | |
} | |
// balance of a specific address | |
function balanceOf(address _who) | |
constant | |
returns (uint256) { | |
return accounts[_who].balance; | |
} | |
// transfer | |
function transfer(address _to, uint256 _value) | |
when_owns(msg.sender, _value) | |
safe_add(accounts[_to].balance, _value) | |
returns (bool) { | |
Transfer(msg.sender, _to, _value); | |
accounts[msg.sender].balance -= _value; | |
accounts[_to].balance += _value; | |
return true; | |
} | |
// transfer via allowance | |
function transferFrom(address _from, address _to, uint256 _value) | |
when_owns(_from, _value) | |
when_has_allowance(_from, msg.sender, _value) | |
safe_add(accounts[_to].balance, _value) | |
returns (bool) { | |
Transfer(_from, _to, _value); | |
accounts[_from].allowanceOf[msg.sender] -= _value; | |
accounts[_from].balance -= _value; | |
accounts[_to].balance += _value; | |
return true; | |
} | |
// approve allowances | |
function approve(address _spender, uint256 _value) | |
returns (bool) { | |
Approval(msg.sender, _spender, _value); | |
accounts[msg.sender].allowanceOf[_spender] += _value; | |
return true; | |
} | |
// available allowance | |
function allowance(address _owner, address _spender) | |
constant | |
returns (uint256) { | |
return accounts[_owner].allowanceOf[_spender]; | |
} | |
} | |
/** | |
* Abstract contract used for recieving donations or profits | |
* Withdraw is divided by total tokens each account owns | |
* Unlock period allows transfers | |
* Lock period allow withdraws | |
* Child contract that implement minting should use modifier not_locked in minting function | |
* Inspired by ProfitContainer and Lockable by vDice | |
* | |
* By Ricardo Guilherme Schmidt | |
* Released under GPLv3 License | |
*/ | |
contract LockableCoin is AbstractCoin { | |
//creation time, defined when contract is created | |
uint public creationTime = now; | |
//time constants, defines epoch size and periods | |
uint public constant UNLOCKED_TIME = 25 days; | |
uint public constant LOCKED_TIME = 5 days; | |
uint public constant EPOCH_LENGTH = UNLOCKED_TIME + LOCKED_TIME; | |
//current epoch constant formula, recalculated in any contract call | |
uint public constant CURRENT_EPOCH = (now - creationTime) / EPOCH_LENGTH + 1; | |
//next lock constant formula, recalculated in any contract call | |
uint public constant NEXT_LOCK = (creationTime + CURRENT_EPOCH * UNLOCKED_TIME) + (CURRENT_EPOCH - 1) * LOCKED_TIME; | |
//used for calculating balance and for checking if account withdrawn | |
uint public currentPayEpoch; | |
//stores the balance from the lock time | |
uint public epochBalance; | |
//stores lock state, used for events | |
bool public lock; | |
//used for hecking if account withdrawn | |
mapping (address => uint) lastPaidOutEpoch; | |
//events | |
event Withdrawn(address tokenHolder, uint amountPaidOut); | |
event Deposited(address donator,uint value); | |
event Locked(); | |
event Unlocked(); | |
//checks if not locked and call event on change | |
modifier not_locked { | |
if (NEXT_LOCK < now) { | |
if (lock) throw; | |
lock = true; | |
Locked(); | |
return; | |
} | |
else { | |
if (lock) { | |
lock = false; | |
Unlocked(); | |
} | |
} | |
_; | |
} | |
//checks if is locked and call event on change | |
modifier locked { | |
if (NEXT_LOCK < now) { | |
if (!lock){ | |
lock = true; | |
Locked(); | |
} | |
} | |
else { | |
if (!lock) throw; | |
lock = false; | |
Unlocked(); | |
return; | |
} | |
_; | |
} | |
//update the balance and payout epoch | |
modifier update_epoch { | |
if(currentPayEpoch < CURRENT_EPOCH) { | |
currentPayEpoch = CURRENT_EPOCH; | |
epochBalance = this.balance; | |
} | |
_; | |
} | |
//checks if user already withdrawn | |
modifier not_paid { | |
if (lastPaidOutEpoch[msg.sender] == currentPayEpoch) throw; | |
_; | |
} | |
//check overflow in multiply | |
modifier safe_multiply(uint _a, uint _b) { | |
if (!(_b == 0 || ((_a * _b) / _b) == _a)) throw; | |
_; | |
} | |
//allow deposit and call event | |
function () | |
payable { | |
Deposited(msg.sender, msg.value); | |
} | |
//withdraw if locked and not paid, updates epoch | |
function withdrawal() | |
external | |
locked | |
update_epoch | |
not_paid | |
safe_multiply(balanceOf(msg.sender), epochBalance) { | |
uint _currentEpoch = CURRENT_EPOCH; | |
uint _tokenBalance = balanceOf(msg.sender); | |
uint _totalSupply = totalSupply; | |
if (this.balance == 0 || _tokenBalance == 0) throw; | |
lastPaidOutEpoch[msg.sender] = currentPayEpoch; | |
uint amountToPayOut = (_tokenBalance * epochBalance) / _totalSupply; | |
if(!msg.sender.send(amountToPayOut)) { | |
throw; | |
} | |
Withdrawn(msg.sender, amountToPayOut); | |
} | |
//if this coin owns tokens of other lockablecoin, allow withdraw | |
function withdrawalFrom(LockableCoin _otherLockableCoin) { | |
_otherLockableCoin.withdrawal(); | |
} | |
//return expected payout in lock or estimated when not locked | |
function expectedPayout(address _tokenHolder) | |
external | |
constant | |
returns (uint payout) { | |
if (now < NEXT_LOCK) //unlocked, estimate | |
payout = (balanceOf(_tokenHolder) * this.balance) / totalSupply; | |
else | |
payout = (balanceOf(_tokenHolder) * epochBalance) / totalSupply; | |
} | |
//overwrite not allow transfer during lock | |
function transfer(address _to, uint256 _value) | |
not_locked | |
returns (bool ok) { | |
return super.transfer(_to,_value); | |
} | |
//overwrite not allow transfer during lock | |
function transferFrom(address _from, address _to, uint256 _value) | |
not_locked | |
returns (bool ok) { | |
return super.transferFrom(_from,_to,_value); | |
} | |
//overwrite not allow transfer during lock | |
function approve(address _spender, uint256 _value) | |
returns (bool ok) { | |
return super.approve(_spender,_value); | |
} | |
} | |
// <ORACLIZE_API> | |
/* | |
Copyright (c) 2015-2016 Oraclize SRL | |
Copyright (c) 2016 Oraclize LTD | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
contract OraclizeI { | |
address public cbAddress; | |
function query(uint _timestamp, string _datasource, string _arg) payable returns (bytes32 _id); | |
function query_withGasLimit(uint _timestamp, string _datasource, string _arg, uint _gaslimit) payable returns (bytes32 _id); | |
function query2(uint _timestamp, string _datasource, string _arg1, string _arg2) payable returns (bytes32 _id); | |
function query2_withGasLimit(uint _timestamp, string _datasource, string _arg1, string _arg2, uint _gaslimit) payable returns (bytes32 _id); | |
function getPrice(string _datasource) returns (uint _dsprice); | |
function getPrice(string _datasource, uint gaslimit) returns (uint _dsprice); | |
function useCoupon(string _coupon); | |
function setProofType(byte _proofType); | |
function setConfig(bytes32 _config); | |
function setCustomGasPrice(uint _gasPrice); | |
} | |
contract OraclizeAddrResolverI { | |
function getAddress() returns (address _addr); | |
} | |
contract usingOraclize { | |
uint constant day = 60*60*24; | |
uint constant week = 60*60*24*7; | |
uint constant month = 60*60*24*30; | |
byte constant proofType_NONE = 0x00; | |
byte constant proofType_TLSNotary = 0x10; | |
byte constant proofStorage_IPFS = 0x01; | |
uint8 constant networkID_auto = 0; | |
uint8 constant networkID_mainnet = 1; | |
uint8 constant networkID_testnet = 2; | |
uint8 constant networkID_morden = 2; | |
uint8 constant networkID_consensys = 161; | |
OraclizeAddrResolverI OAR; | |
OraclizeI oraclize; | |
modifier oraclizeAPI { | |
if((address(OAR)==0)||(getCodeSize(address(OAR))==0)) oraclize_setNetwork(networkID_auto); | |
oraclize = OraclizeI(OAR.getAddress()); | |
_; | |
} | |
modifier coupon(string code){ | |
oraclize = OraclizeI(OAR.getAddress()); | |
oraclize.useCoupon(code); | |
_; | |
} | |
function oraclize_setNetwork(uint8 networkID) internal returns(bool){ | |
if (getCodeSize(0x1d3B2638a7cC9f2CB3D298A3DA7a90B67E5506ed)>0){ //mainnet | |
OAR = OraclizeAddrResolverI(0x1d3B2638a7cC9f2CB3D298A3DA7a90B67E5506ed); | |
return true; | |
} | |
if (getCodeSize(0xc03A2615D5efaf5F49F60B7BB6583eaec212fdf1)>0){ //ropsten testnet | |
OAR = OraclizeAddrResolverI(0xc03A2615D5efaf5F49F60B7BB6583eaec212fdf1); | |
return true; | |
} | |
if (getCodeSize(0x6f485C8BF6fc43eA212E93BBF8ce046C7f1cb475)>0){ //ethereum-bridge | |
OAR = OraclizeAddrResolverI(0x6f485C8BF6fc43eA212E93BBF8ce046C7f1cb475); | |
return true; | |
} | |
if (getCodeSize(0x20e12A1F859B3FeaE5Fb2A0A32C18F5a65555bBF)>0){ //ether.camp ide | |
OAR = OraclizeAddrResolverI(0x20e12A1F859B3FeaE5Fb2A0A32C18F5a65555bBF); | |
return true; | |
} | |
if (getCodeSize(0x51efaF4c8B3C9AfBD5aB9F4bbC82784Ab6ef8fAA)>0){ //browser-solidity | |
OAR = OraclizeAddrResolverI(0x51efaF4c8B3C9AfBD5aB9F4bbC82784Ab6ef8fAA); | |
return true; | |
} | |
return false; | |
} | |
function __callback(bytes32 myid, string result) { | |
__callback(myid, result, new bytes(0)); | |
} | |
function __callback(bytes32 myid, string result, bytes proof) { | |
} | |
function oraclize_getPrice(string datasource) oraclizeAPI internal returns (uint){ | |
return oraclize.getPrice(datasource); | |
} | |
function oraclize_getPrice(string datasource, uint gaslimit) oraclizeAPI internal returns (uint){ | |
return oraclize.getPrice(datasource, gaslimit); | |
} | |
function oraclize_query(string datasource, string arg) oraclizeAPI internal returns (bytes32 id){ | |
uint price = oraclize.getPrice(datasource); | |
if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price | |
return oraclize.query.value(price)(0, datasource, arg); | |
} | |
function oraclize_query(uint timestamp, string datasource, string arg) oraclizeAPI internal returns (bytes32 id){ | |
uint price = oraclize.getPrice(datasource); | |
if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price | |
return oraclize.query.value(price)(timestamp, datasource, arg); | |
} | |
function oraclize_query(uint timestamp, string datasource, string arg, uint gaslimit) oraclizeAPI internal returns (bytes32 id){ | |
uint price = oraclize.getPrice(datasource, gaslimit); | |
if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price | |
return oraclize.query_withGasLimit.value(price)(timestamp, datasource, arg, gaslimit); | |
} | |
function oraclize_query(string datasource, string arg, uint gaslimit) oraclizeAPI internal returns (bytes32 id){ | |
uint price = oraclize.getPrice(datasource, gaslimit); | |
if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price | |
return oraclize.query_withGasLimit.value(price)(0, datasource, arg, gaslimit); | |
} | |
function oraclize_query(string datasource, string arg1, string arg2) oraclizeAPI internal returns (bytes32 id){ | |
uint price = oraclize.getPrice(datasource); | |
if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price | |
return oraclize.query2.value(price)(0, datasource, arg1, arg2); | |
} | |
function oraclize_query(uint timestamp, string datasource, string arg1, string arg2) oraclizeAPI internal returns (bytes32 id){ | |
uint price = oraclize.getPrice(datasource); | |
if (price > 1 ether + tx.gasprice*200000) return 0; // unexpectedly high price | |
return oraclize.query2.value(price)(timestamp, datasource, arg1, arg2); | |
} | |
function oraclize_query(uint timestamp, string datasource, string arg1, string arg2, uint gaslimit) oraclizeAPI internal returns (bytes32 id){ | |
uint price = oraclize.getPrice(datasource, gaslimit); | |
if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price | |
return oraclize.query2_withGasLimit.value(price)(timestamp, datasource, arg1, arg2, gaslimit); | |
} | |
function oraclize_query(string datasource, string arg1, string arg2, uint gaslimit) oraclizeAPI internal returns (bytes32 id){ | |
uint price = oraclize.getPrice(datasource, gaslimit); | |
if (price > 1 ether + tx.gasprice*gaslimit) return 0; // unexpectedly high price | |
return oraclize.query2_withGasLimit.value(price)(0, datasource, arg1, arg2, gaslimit); | |
} | |
function oraclize_cbAddress() oraclizeAPI internal returns (address){ | |
return oraclize.cbAddress(); | |
} | |
function oraclize_setProof(byte proofP) oraclizeAPI internal { | |
return oraclize.setProofType(proofP); | |
} | |
function oraclize_setCustomGasPrice(uint gasPrice) oraclizeAPI internal { | |
return oraclize.setCustomGasPrice(gasPrice); | |
} | |
function oraclize_setConfig(bytes32 config) oraclizeAPI internal { | |
return oraclize.setConfig(config); | |
} | |
function getCodeSize(address _addr) constant internal returns(uint _size) { | |
assembly { | |
_size := extcodesize(_addr) | |
} | |
} | |
function parseAddr(string _a) internal returns (address){ | |
bytes memory tmp = bytes(_a); | |
uint160 iaddr = 0; | |
uint160 b1; | |
uint160 b2; | |
for (uint i=2; i<2+2*20; i+=2){ | |
iaddr *= 256; | |
b1 = uint160(tmp[i]); | |
b2 = uint160(tmp[i+1]); | |
if ((b1 >= 97)&&(b1 <= 102)) b1 -= 87; | |
else if ((b1 >= 48)&&(b1 <= 57)) b1 -= 48; | |
if ((b2 >= 97)&&(b2 <= 102)) b2 -= 87; | |
else if ((b2 >= 48)&&(b2 <= 57)) b2 -= 48; | |
iaddr += (b1*16+b2); | |
} | |
return address(iaddr); | |
} | |
function strCompare(string _a, string _b) internal returns (int) { | |
bytes memory a = bytes(_a); | |
bytes memory b = bytes(_b); | |
uint minLength = a.length; | |
if (b.length < minLength) minLength = b.length; | |
for (uint i = 0; i < minLength; i ++) | |
if (a[i] < b[i]) | |
return -1; | |
else if (a[i] > b[i]) | |
return 1; | |
if (a.length < b.length) | |
return -1; | |
else if (a.length > b.length) | |
return 1; | |
else | |
return 0; | |
} | |
function indexOf(string _haystack, string _needle) internal returns (int) | |
{ | |
bytes memory h = bytes(_haystack); | |
bytes memory n = bytes(_needle); | |
if(h.length < 1 || n.length < 1 || (n.length > h.length)) | |
return -1; | |
else if(h.length > (2**128 -1)) | |
return -1; | |
else | |
{ | |
uint subindex = 0; | |
for (uint i = 0; i < h.length; i ++) | |
{ | |
if (h[i] == n[0]) | |
{ | |
subindex = 1; | |
while(subindex < n.length && (i + subindex) < h.length && h[i + subindex] == n[subindex]) | |
{ | |
subindex++; | |
} | |
if(subindex == n.length) | |
return int(i); | |
} | |
} | |
return -1; | |
} | |
} | |
function strConcat(string _a, string _b, string _c, string _d, string _e) internal returns (string){ | |
bytes memory _ba = bytes(_a); | |
bytes memory _bb = bytes(_b); | |
bytes memory _bc = bytes(_c); | |
bytes memory _bd = bytes(_d); | |
bytes memory _be = bytes(_e); | |
string memory abcde = new string(_ba.length + _bb.length + _bc.length + _bd.length + _be.length); | |
bytes memory babcde = bytes(abcde); | |
uint k = 0; | |
for (uint i = 0; i < _ba.length; i++) babcde[k++] = _ba[i]; | |
for (i = 0; i < _bb.length; i++) babcde[k++] = _bb[i]; | |
for (i = 0; i < _bc.length; i++) babcde[k++] = _bc[i]; | |
for (i = 0; i < _bd.length; i++) babcde[k++] = _bd[i]; | |
for (i = 0; i < _be.length; i++) babcde[k++] = _be[i]; | |
return string(babcde); | |
} | |
function strConcat(string _a, string _b, string _c, string _d) internal returns (string) { | |
return strConcat(_a, _b, _c, _d, ""); | |
} | |
function strConcat(string _a, string _b, string _c) internal returns (string) { | |
return strConcat(_a, _b, _c, "", ""); | |
} | |
function strConcat(string _a, string _b) internal returns (string) { | |
return strConcat(_a, _b, "", "", ""); | |
} | |
// parseInt | |
function parseInt(string _a) internal returns (uint) { | |
return parseInt(_a, 0); | |
} | |
// parseInt(parseFloat*10^_b) | |
function parseInt(string _a, uint _b) internal returns (uint) { | |
bytes memory bresult = bytes(_a); | |
uint mint = 0; | |
bool decimals = false; | |
for (uint i=0; i<bresult.length; i++){ | |
if ((bresult[i] >= 48)&&(bresult[i] <= 57)){ | |
if (decimals){ | |
if (_b == 0) break; | |
else _b--; | |
} | |
mint *= 10; | |
mint += uint(bresult[i]) - 48; | |
} else if (bresult[i] == 46) decimals = true; | |
} | |
if (_b > 0) mint *= 10**_b; | |
return mint; | |
} | |
function uint2str(uint i) internal returns (string){ | |
if (i == 0) return "0"; | |
uint j = i; | |
uint len; | |
while (j != 0){ | |
len++; | |
j /= 10; | |
} | |
bytes memory bstr = new bytes(len); | |
uint k = len - 1; | |
while (i != 0){ | |
bstr[k--] = byte(48 + i % 10); | |
i /= 10; | |
} | |
return string(bstr); | |
} | |
} | |
// </ORACLIZE_API> | |
/** | |
* Contract that mint tokens by github commit stats | |
* This file contain two contracts: GitHubOracle and GitHubToken | |
* | |
* GitHubOracle register users and create GitHubToken contracts | |
* Registration requires user create a gist with only their account address | |
* GitHubOracle will create one GitHubToken contract per repository | |
* GitHubToken mint tokens by commit only for registered users in GitHubOracle | |
* GitHubToken is a LockableCoin, that accept donatations and can be withdrawn by Token Holders | |
* The lookups are done by Oraclize that charge a small fee | |
* The contract itself will never charge any fee | |
* | |
* By Ricardo Guilherme Schmidt | |
* Released under GPLv3 License | |
*/ | |
contract GitHubToken is LockableCoin, usingOraclize { | |
//constant for oraclize commits callbacks | |
uint8 constant CALLBACK_CLAIMCOMMIT = 1; | |
//stores repository name, used for claim calls | |
string private repository; | |
//stores repository name in sha3, used by GitHubOracle | |
bytes32 public sha3repository; | |
//temporary storage enumerating oraclize calls | |
mapping (bytes32 => uint8) oraclize_type; | |
//temporary storage for oraclize commit token claim calls | |
mapping (bytes32 => string) oraclize_claim; | |
//permanent storage of recipts of all commits | |
mapping (bytes32 => CommitReciept) public commits; | |
//Address of the oracle, used for github login address lookup | |
GitHubOracle public oracle; | |
//claim event | |
event Claim(address claimer, string commitid, uint total); | |
//stores the total and user, and if claimed (used against double claiming) | |
struct CommitReciept { | |
uint256 total; | |
address user; | |
bool claimed; | |
} | |
//protect against double claiming | |
modifier not_claimed(string commitid) { | |
if(isClaimed(commitid)) throw; | |
_; | |
} | |
function GitHubToken(string _repository, GitHubOracle _oracle) | |
payable { | |
oracle = _oracle; | |
repository = _repository; | |
sha3repository = sha3(_repository); | |
} | |
//checks if a commit is already claimed | |
function isClaimed(string _commitid) | |
constant | |
returns (bool) { | |
return commits[sha3(_commitid)].claimed; | |
} | |
//oraclize response callback | |
function __callback(bytes32 _ocid, string _result) { | |
if (msg.sender != oraclize_cbAddress()) throw; | |
uint8 callback_type = oraclize_type[_ocid]; | |
if(callback_type==CALLBACK_CLAIMCOMMIT && !lock){ | |
_claim(_ocid,_result); | |
} | |
delete oraclize_type[_ocid]; | |
} | |
//oraclize callback claim request | |
function _claim(bytes32 _ocid, string _result) | |
internal { | |
var (login,total) = extract(_result); | |
address user = oracle.getUserAddress(login); | |
if(user != 0x0){ | |
commits[sha3(oraclize_claim[_ocid])].user = user; | |
if(total > 0){ | |
bytes32 shacommit = sha3(oraclize_claim[_ocid]); | |
commits[shacommit].total = total; | |
if(commits[shacommit].user != 0x0 && !commits[shacommit].claimed){ | |
commits[shacommit].claimed = true; | |
accounts[user].balance += total; | |
totalSupply += total; | |
Claim(user,oraclize_claim[_ocid],total); | |
} | |
} | |
} | |
delete oraclize_claim[_ocid]; | |
} | |
//claims a commitid | |
function claim(string _commitid) | |
payable | |
not_locked | |
not_claimed(_commitid) { | |
bytes32 ocid = oraclize_query("URL", strConcat("json(https://api.github.com/repos/", repository,"/commits/", _commitid,").[author,stats].[login,total]")); | |
oraclize_type[ocid] = CALLBACK_CLAIMCOMMIT; | |
oraclize_claim[ocid] = _commitid; | |
} | |
//extract login name and total of changes in commit | |
function extract(string _s) | |
internal | |
constant | |
returns (string login,uint total) { | |
bytes memory v = bytes(_s); | |
uint comma = 0; | |
uint quot = 0; | |
uint quot2 = 0; | |
for (uint i =0;v.length > i;i++) { | |
if (v[i] == '"'){ //Find first quotation mark | |
quot=i; | |
break; | |
} | |
} | |
for (;v.length > i;i++) { | |
if (v[i] == '"') { //find second quotation mark | |
quot2=i; | |
}else if (v[i] == ',') { //find comma | |
comma=i; | |
break; | |
} | |
} | |
if(comma>0 && quot>0 && quot2 >0) { | |
bytes memory user = new bytes(quot2-quot-1); | |
for(i=0; i<user.length; i++){ | |
user[i] = v[quot+i+1]; | |
} | |
login = string(user); //user | |
for(i=comma+1; i<v.length-1; i++){ | |
if ((v[i] >= 48)&&(v[i] <= 57)){ //only ASCII numbers | |
total *= 10; | |
total += uint(v[i]) - 48; | |
} | |
} | |
} | |
} | |
} | |
contract GitHubOracle is usingOraclize { | |
//constant for oraclize commits callbacks | |
uint8 constant CALLBACK_REGISTER = 0; | |
//temporary storage enumerating oraclize calls | |
mapping (bytes32 => uint8) oraclize_type; | |
//temporary storage for oraclize user register queries | |
mapping (bytes32 => VerifyRequest) oraclize_register; | |
//permanent storage of sha3(login) of github users | |
mapping (bytes32 => address) github_users; | |
//permanent storage of registered repositories | |
mapping (bytes32 => Repository) repositories; | |
//events | |
event UserSet(string githubLogin, address account); | |
event RepositoryAdd(string repository, address account); | |
//stores the address of githubtoken and registered is used for overwriting previous registered | |
struct Repository { | |
GitHubToken account; | |
bool registered; | |
} | |
//stores data for oraclize user register request | |
struct VerifyRequest { | |
address sender; | |
bytes32 githubid; | |
string login; | |
} | |
//return the address of a github login | |
function getUserAddress(string _login) | |
external | |
constant | |
returns (address) { | |
return github_users[sha3(_login)]; | |
} | |
//oraclize response callback | |
function __callback(bytes32 _ocid, string result) { | |
if (msg.sender != oraclize_cbAddress()) throw; | |
uint8 callback_type = oraclize_type[_ocid]; | |
if(callback_type==CALLBACK_REGISTER){ | |
if(strCompare(result,"404: Not Found") != 0){ | |
address githubowner = parseAddr(result); | |
if(oraclize_register[_ocid].sender == githubowner){ | |
github_users[oraclize_register[_ocid].githubid] = githubowner; | |
UserSet(oraclize_register[_ocid].login, githubowner); | |
} | |
} | |
delete oraclize_register[_ocid]; | |
} | |
delete oraclize_type[_ocid]; | |
} | |
//register or change a github user ethereum address | |
function register(string _github_user, string _gistid) | |
payable { | |
bytes32 ocid = oraclize_query("URL", strConcat("https://gist.githubusercontent.com/",_github_user,"/",_gistid,"/raw/")); | |
oraclize_type[ocid] = CALLBACK_REGISTER; | |
oraclize_register[ocid] = VerifyRequest({sender: msg.sender, githubid: sha3(_github_user), login: _github_user}); | |
} | |
//creates a new GitHubToken contract to _repository | |
function addRepository(string _repository) | |
returns (GitHubToken) { | |
bytes32 repo = sha3(_repository); | |
if(repositories[repo].registered) throw; | |
repositories[repo] = Repository({account: new GitHubToken(_repository, this), registered: true}); | |
RepositoryAdd(_repository, repositories[repo].account); | |
return repositories[repo].account; | |
} | |
//register a contract deployed outside Oracle | |
function addRepository(string _repository, GitHubToken _addr) | |
returns (GitHubToken) { | |
bytes32 repo = sha3(_repository); | |
if(repositories[repo].registered || _addr.sha3repository() != repo) throw; | |
repositories[repo] = Repository({account: _addr, registered: true}); | |
RepositoryAdd(_repository, repositories[repo].account); | |
return repositories[repo].account; | |
} | |
//return the contract address of the repository (or 0x0 if none registered) | |
function getRepository(string _repository) | |
constant | |
returns (GitHubToken) { | |
return repositories[sha3(_repository)].account; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment