2 - Receivers

Last updated 2 months ago

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.

WeiReceiver will throw an exception, if you send money to it directly. Instead you should use processFunds() function.

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. If the scheme consists of absolute receivers only, the behavior of this two functions is the same.

WeiReceiver will throw an exception, if you send more than getTotalWeiNeeded() or less than getMinWeiNeeded().

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
relativeExpense.processFunds.value(totalNeeded)(100*eth);
WeiAbsoluteExpense absoluteExpense = new WeiAbsoluteExpense(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)
absoluteExpense.processFunds.value(totalNeeded)(10*eth);