Payment
The PREDA payment contract is functionally equivalent to the Solidity ERC20 payment contract, but its transfer()
function is divided into two steps. First, the transfer()
function checks whether the sender's balance
is equal to or larger than the parameter amount
set in the transaction. If this is the case, the function subtracts the amount
from the balance
of the sender. This is the withdrawal step. In the second step, a relay is sent to the recipient's address and the amount
is added to the recipient's balance
. This is the step of deposit.
contract Payment {
@address bigint balance;
@address function bool transfer(address to, bigint amount) export
{
if(balance >= amount)
{
balance -= amount;
relay@to (^amount)
{
__debug.print("deposit: ", amount);
balance += amount;
}
return true;
}
return false;
}
}
In PREDA, relay
is a keyword to specify a relay statement, and @
is another keyword to specify the destination address of a relay. In this example, the lambda expression is used, which defines an anonymous function in the relay statement. The ˆ
operator is used to capture variable by value. Thus, programmers can pass parameters to the anonymous function without renaming it.