Skip to content

Commit

Permalink
Updated Splitter contract
Browse files Browse the repository at this point in the history
  • Loading branch information
akru committed Nov 23, 2016
1 parent 6a4db20 commit 8d598c3
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 62 deletions.
48 changes: 48 additions & 0 deletions contracts/builder/BuilderSplitter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// AIRA Builder for Splitter contract
//
// Ethereum address:
// - Mainnet:
// - Testnet:
//

pragma solidity ^0.4.4;
import 'creator/CreatorSplitter.sol';
import './Builder.sol';

/**
* @title BuilderSplitter contract
*/
contract BuilderSplitter is Builder {
/**
* @dev Run script creation contract
* @param _client is a contract destination address (zero for sender)
* @return address new contract
*/
function create(address _client) payable returns (address) {
if (buildingCostWei > 0 && beneficiary != 0) {
// Too low value
if (msg.value < buildingCostWei) throw;
// Beneficiary send
if (!beneficiary.send(buildingCostWei)) throw;
// Refund
if (msg.value > buildingCostWei) {
if (!msg.sender.send(msg.value - buildingCostWei)) throw;
}
} else {
// Refund all
if (msg.value > 0) {
if (!msg.sender.send(msg.value)) throw;
}
}

if (_client == 0)
_client = msg.sender;

var inst = CreatorSplitter.create();
getContractsOf[_client].push(inst);
Builded(_client, inst);
inst.delegate(_client);
return inst;
}
}
14 changes: 14 additions & 0 deletions contracts/creator/CreatorSplitter.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pragma solidity ^0.4.4;

import 'dao/Splitter.sol';

library CreatorSplitter {
function create() returns (Splitter)
{ return new Splitter(); }

function version() constant returns (string)
{ return "v0.5.0 (89f18671)"; }

function abi() constant returns (string)
{ return '[{"constant":false,"inputs":[{"name":"_destination","type":"address"}],"name":"remove","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_destination","type":"address"},{"name":"_percent","type":"uint256"}],"name":"set","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"withdraw","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"first","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_owner","type":"address"}],"name":"delegate","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_current","type":"address"}],"name":"next","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"summary","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"percent","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"token","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"inputs":[{"name":"_token_ether","type":"address"}],"type":"constructor"},{"payable":false,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"sender","type":"address"},{"indexed":true,"name":"value","type":"uint256"}],"name":"Received","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"to","type":"address"},{"indexed":true,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}]'; }
}
78 changes: 16 additions & 62 deletions contracts/dao/Splitter.sol
Original file line number Diff line number Diff line change
@@ -1,94 +1,48 @@
pragma solidity ^0.4.4;
import 'common/Mortal.sol';
import 'lib/AddressList.sol';
import 'token/TokenEther.sol';

contract Splitter is Mortal {
TokenEther public token;

/**
* @dev Splitter constructor
* @param _token_ether is a ether token
*/
function Splitter(address _token_ether)
{ token = TokenEther(_token_ether); }

event Received(address indexed sender, uint indexed value);
event Transfer(address indexed to, uint indexed value);

AddressList.Data destination;
using AddressList for AddressList.Data;

address[] public destination;
mapping(address => uint) public percent;

/**
* @dev Get first destination address
* @return first address
*/
function first() constant returns (address)
{ return destination.first(); }

/**
* @dev Get next destination address
* @param _current is a current address
* @return next address
*/
function next(address _current) constant returns (address)
{ return destination.next(_current); }

/**
* @dev Count summary ratio of received values
* @return summary percent of all destination
* @dev Append new destination address
* @param _destination is a destination address
*/
function summary() constant returns (uint) {
uint sum = 0;
for (var d = destination.first(); d != 0; d = destination.next(d))
sum += percent[d];
return sum;
}
function append(address _destination) onlyOwner
{ destination.push(_destination); }

/**
* @dev Set destination address and ratio
* @param _destination is a destination address
* @param _percent is a ratio in percent
*/
function set(address _destination, uint _percent) onlyOwner {
if (_percent + summary() > 100) throw;

if (!destination.contains(_destination))
destination.append(_destination);

percent[_destination] = _percent;
}

/**
* @dev Remove destination from list
* @param _destination is a destination to remove
*/
function remove(address _destination) onlyOwner
{ destination.remove(_destination); }
function set(address _destination, uint _percent) onlyOwner
{ percent[_destination] = _percent; }

/**
* @dev Withdraw accumulated contract values, this method refill token balance
* and transfer to destinations according to ratio percent
*/
function withdraw() onlyOwner {
if (this.balance > 0) {
token.refill.value(this.balance)();
var balance = token.getBalance();

/* XXX: possible DoS by block gas limit */
for (var d = destination.first(); d != 0; d = destination.next(d)) {
var value = balance * 100 / percent[d];
if (!token.transfer(d, value)) throw;
Transfer(d, value);
for (uint i = 0; i < destination.length; ++i) {
var part = percent[destination[i]];
if (part > 0) {
var value = this.balance * 100 / part;
if (!destination[i].send(value)) throw;
}
}
}
}

/**
* @dev Received log
*/
function()
function () payable
{ Received(msg.sender, msg.value); }

event Received(address indexed sender, uint indexed value);
}

0 comments on commit 8d598c3

Please sign in to comment.