Skip to main content

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)

Conditional routing example

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

SyntaxDescriptionExample
+Add
-Subtract
&&Logical ANDIf two properties need to be true: First && Second
||Logical ORIf either properties need to be true: First \|\| Second
/DivisionThe divide some property named Property with two: Property / 2
==Equality comparisonTwo properties need to be equal: First == Second
>Larger thanWill result to true: 10 > 1
>=Larger or equal thanWill result to true: 10 >= 10
<Smaller thanWill result to true: 1 > 10
<=Smaller or equal thanWill result to true: 1 <= 1
*MultiplicationWill result to 100: 10 * 10
!=Does not equalWill result to true because 10 and 9 are not equal 10 != 9
%ModuloWill divide two numbers and output the remainder. For example this will output 2: 10 % 8
^PowerFor example 2 ^ 4 will be 16 (2 2 2 * 2): 2 ^ 4

Method calls

Method callDescriptionExample
+Add
-Subtract

Available variables

Some built in variable available in the calculations.

VariableDescriptionExample
contract ownerThe wallet address of the current workflow ownerChecks if caller is the contract owner: caller == contract owner
countTotal number of items (assets) created in the workflow.Checks if no assets created yet: count == 0
callerThe wallet address of the caller of the current transitionChecks if the Seller property of the item is the current caller: Seller == caller
nowGets the current timestamp (the current blockchain block time) - can be off by few seconds or even minutesChecks if Deadline has passed: Deadline <= now
workflow balanceGets 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 nameDescriptionExample
ToStringUseful for creating string representationsChanging the name to contain the id of the item: Name = Id.ToString()