4 - Splitters

Last updated 2 months ago

WeiSplitter is a node that has single ETH input and multiple ETH outputs. WeiSplitter consumes no ETH, so it polls its children in order to return the amount of ETH needed:

// TODO
splitter = ...

There are two types of splitters:

  • WeiTopDownSplitter - the order of children nodes matters;

  • WeiUnsortedSplitter - the order of children nodes does not matter.

Splitter can be open() or close(). In opened state splitter can receive ETH, but in closed state it ignores childrens' needs and does not accept money.

You can addChild() to splitter, getChild() or or getChildrenCount().

You SHOULD NOT send more ETH than needed to the splitter! It will throw an exception.

1. WeiTopDownSplitter

In a top-down splitter ETH is flowing from the top to bottom. That is why the order of children nodes is very important.

However, if splitter contains only absolute expenses, order does not matter.

WeiTopDownSplitter example.sol
WeiTopDownSplitter allOutpults = new WeiTopDownSplitter('AllOutpults');
WeiUnsortedSplitter spends = new WeiUnsortedSplitter('Spends');
WeiUnsortedSplitter salaries = new WeiUnsortedSplitter('Salaries');
WeiAbsoluteExpense employee1 = new WeiAbsoluteExpense(10*eth);
WeiAbsoluteExpense employee2 = new WeiAbsoluteExpense(15*eth);
WeiAbsoluteExpense employee3 = new WeiAbsoluteExpense(8*eth);
// CONNECTIONS
allOutpults.addChild(spends);
spends.addChild(salaries);
salaries.addChild(employee1);
salaries.addChild(employee2);
salaries.addChild(employee3);
// TODO - ask needed amounts, etc
// TODO - send money
// TODO - show employee1, employee2, etc balances

2. WeiUnsortedSplitter

In unsorted splitter there is no difference how children are ordered, i.e. you can swap any elements and the result will still stay the same.

WeiUnsortedSplitter example 1.sol
WeiUnsortedSplitter salaries = new WeiUnsortedSplitter('Salaries');
WeiAbsoluteExpense employee1 = new WeiAbsoluteExpense(1*eth);
WeiAbsoluteExpense employee2 = new WeiAbsoluteExpense(2*eth);
salaries.addChild(employee1);
salaries.addChild(employee2);
salaries.getMinWeiNeeded(); // 3 eth
salaries.getTotalWeiNeeded(); // 3 eth
salaries.isNeedsMoney(); // true
salaries.processFunds.value(3*eth)(3*eth);
salaries.getMinWeiNeeded(); // 0
salaries.getTotalWeiNeeded(); // 0
salaries.isNeedsMoney(); // false
address(employee1).balance() // 1 eth
address(employee2).balance() // 2 eth