Ergo Examples
Hello World
Each Ergo files is a module. A module is identified by a namespace, and contains declarations (e.g., constants, functions, contracts). Here is the “Hello World!” contract in Ergo:
namespace org.accordproject.helloworld
contract HelloWorld over TemplateModel {
// Simple Clause
clause greet(request : Request) : Response {
return Response{ output: "Hello " ++ contract.name ++ " " ++ request.input }
}
}
This declares that this module belongs to the org.accordproject.helloworld
namespace and contains a single HelloWorld
contract declaration with one greet
clause.
It also declares that the contract HelloWorld
is parameterized over a given TemplateModel
.
The TemplateModel
as well as the Request
and Response
are types which are specified using Composer Concerto modeling language.
The greet
clause takes a Request
as input and returns a Response
.
The code for the greet
clause returns a new Response
with a property output
which is a string containing the property name
of from the contract (contract
) and the property input
from the request (request
). ++
stands for string concatenation in Ergo.
Eat Apples!
A more complete example is the healthy eating clause inspired by a not so serious terms of services contract.
For this example, let us look first at the template for that legal clause written in natural language:
Eating healthy clause between [{employee}] (the Employee) and [{company}] (the Company).
The canteen only sells apple products. Apples, apple juice, apple flapjacks, toffee
apples. Employee gets fired if caught eating anything without apples in it.
THE EMPLOYEE, IF ALLERGIC TO APPLES, SHALL ALWAYS BE HUNGRY.
Apple products at the canteen are subject to a [{tax}]% tax.
The text specify the terms for the legal clause, and includes a few
variables such as employee
, company
and tax
.
The second component of a smart legal template is the model, which is
expressed using the Composer Concerto modeling
language. The model
describe the variables of the contract, as well as additional
information required to execute the contract logic. In our example,
this includes the input request for the clause (Food
), the response
to that request (Outcome
) and possible events emitted during the
clause execution (Bill
).
namespace org.accordproject.canteen
@AccordTemplateModel("eat-apples")
concept CanteenContract {
o String employee
o String company
o Double tax
}
transaction Food {
o String produce
o Double price
}
transaction Outcome {
o String notice
}
event Bill {
o String billTo
o Double amount
}
The last component of a smart legal template is the Ergo logic. In our example it is a single clause eathealthy
which can be used to process a Food
request.
namespace org.accordproject.canteen
contract EatApples over CanteenContract {
clause eathealthy(request : Food) : Outcome {
enforce request.produce = "apple"
else return Outcome{ notice : "You're fired!" };
emit Bill{
billTo: contract.employee,
amount: request.price * (1.0 + contract.tax / 100.0)
};
return Outcome{ notice : "Very healthy!" }
}
}
As in the "Hello World!" example, this is a smart legal contract with a single clause, but it illustrate a few new ideas.
The enforce
expression is used to check conditions that must be true
for normal execution of the clause. In this example, the enforce
makes sure the food is an apple and if not returns a new outcome
indicating termination of employment.
If the condition is true, the contract proceeds by emitting a bill for
the purchase of the apple. The employee to be billed is obtained from
the contract (contract.employee
). The total amount is calculated by
adding the tax, which is obtained from the contract (contract.tax
),
to the purchase price, which is obtained from the request
(request.price
). The calculation is done using a simple arithmetic
expression (request.price * (1.0 + contract.tax / 100.0)
).