Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
53 lines (36 sloc) 2.37 KB

2 - Receivers

WeiReceiver is an abstract smart contract. Every moneyflow node is a WeiReceiver.
It can send ETH further or be a terminal node. In the last case it is called "a destination".

Notable functions:

  • processFunds() – use this payable function to send ETH to the node;
  • isNeedsMoney() – returns true if node can receive ETH;
  • getMinWeiNeeded() – returns a minimum amount of ETH, which node will accept;
  • getTotalWeiNeeded() – returns a maximum amount of ETH, which node will accept.

The basic algorithm for moneyflow is to first request getTotalWeiNeed() from moneyflow entry point (root node), and then send ETH by calling processFunds() function with the correct amount.

{% hint style="info" %} WeiReceiver will throw an exception, if you send money to it directly. Instead you should use processFunds() function. {% endhint %}

Absolute vs Relative receivers

There are 2 types of WeiReceivers currently:

  • Absolute - consumes absolute amounts of ETH, e.g.: 1 ETH;
  • Relative - consumes relative amounts of ETH, e.g.: 20%.

The difference between getMinWeiNeeded() or getTotalWeiNeed() exists only in cases when the current scheme has at least one relative receiver, or when minimal amount is not equal to total needed amount (absolute expense). If the scheme consists of absolute receivers with equal minimal amount and total needed amount only, the behavior of this two functions is the same.

{% hint style="info" %} WeiReceiver will throw an exception, if you send more than getTotalWeiNeeded() or less than getMinWeiNeeded() {% endhint %}

Code example

// Consumes 10%
WeiRelativeExpense relativeExpense = new WeiRelativeExpense(100000);
bool isNeeds = relativeExpense.isNeedsMoney(); // true
uint minNeeded = relativeExpense.getMinWeiNeeded(); // 0 
uint totalNeeded = relativeExpense.getTotalWeiNeeded(100*eth); // 10*eth is 10% of 100 eth

// will consume 10 ETH

WeiAbsoluteExpense absoluteExpense = new WeiAbsoluteExpense(10*eth, 10*eth);
isNeeds = absoluteExpense.isNeedsMoney(); // true
minNeeded = absoluteExpense.getMinWeiNeeded(); // 10*eth 
totalNeeded = absoluteExpense.getTotalWeiNeeded(10*eth); // 10*eth

// will consume 10 ETH one time only (but not less/more)