Calculations
Calculations are a way to set attribute values automatically. Sometimes it is more convenient and/or neccessary to set some attributes for users instead of letting them set all of the attributes themselves.
Some examples:
- Default values for attributes.
- Convenient background calculations.
- Security reasons - so that users can not override what must be a contractual clause.
In this example Calculations are used to set the 'End time' attribute of an item to 7 days (now + 24 * 60 * 60
seconds)
Item properties
It is possible to reference/use any properties of the items. For example if the item is Package and it has a Courier property it can be used in the calculation directly to check if current caller is the Courier:
Courier == caller
Operands
Syntax | Description | Example |
---|---|---|
+ | Add | |
- | Subtract | |
&& | Logical AND | If two properties need to be true: First && Second |
|| | Logical OR | If either properties need to be true: First \|\| Second |
/ | Division | The divide some property named Property with two: Property / 2 |
== | Equality comparison | Two properties need to be equal: First == Second |
> | Larger than | Will result to true: 10 > 1 |
>= | Larger or equal than | Will result to true: 10 >= 10 |
< | Smaller than | Will result to true: 1 > 10 |
<= | Smaller or equal than | Will result to true: 1 <= 1 |
* | Multiplication | Will result to 100: 10 * 10 |
!= | Does not equal | Will result to true because 10 and 9 are not equal 10 != 9 |
% | Modulo | Will divide two numbers and output the remainder. For example this will output 2: 10 % 8 |
^ | Power | For example 2 ^ 4 will be 16 (2 2 2 * 2): 2 ^ 4 |
Method calls
Method call | Description | Example |
---|---|---|
+ | Add | |
- | Subtract |
Available variables
Some built in variable available in the calculations.
Variable | Description | Example |
---|---|---|
contract owner | The wallet address of the current workflow owner | Checks if caller is the contract owner: caller == contract owner |
count | Total number of items (assets) created in the workflow. | Checks if no assets created yet: count == 0 |
caller | The wallet address of the caller of the current transition | Checks if the Seller property of the item is the current caller: Seller == caller |
now | Gets the current timestamp (the current blockchain block time) - can be off by few seconds or even minutes | Checks if Deadline has passed: Deadline <= now |
workflow balance | Gets the balance of the current workflow in native currency (in Ethereum it would be ETH). Useful in escrow-like workflows where participants pay the money into the contract. | Checks if balance is over 100: workflow balance > 100 |
Method calls
Some built in method calls available in the calculations.
Method name | Description | Example |
---|---|---|
ToString | Useful for creating string representations | Changing the name to contain the id of the item: Name = Id.ToString() |