Skip to main content

Function: Balance Spent = Balance Received

The last and final smart function that we need to add is to make sure that balance spent is equal to balance received.

In order to create this restriction, we need to add a smart function to the wallet/balance predicate. However, instead of adding the smart function to the _predicate/spec, we need to add it othe _predicate/txSpec. The _predicate/txSpec runs once per transaction where wallet/balance appears, regardless of how many times wallet/balance appears in the transaction.

When writing a _predicate/txSpec, we have access to two context-dependant functions, objT and objF. objT is the sum of all the flakes relating to a single predicate (in this case, wallet/balance) that are being added during this block, and objF is the sum of all flakes being retracted.

To illustrate how this works, we'll use different data than the data in our ledger from this lesson Let's say that the below table is everyone's balances prior to any transactions:


Bob pays Jane 20, pays June 10, and pays Jone 15. In total Bob paid 45. In our ledger, every person's previous balance is retracted, and their new balance is asserted.

NameRetractedAssertedTotal Change

We see that the sum of all the balance retracted should equal the balance of all the balance asserted. In other words, after money is done being traded hands, we expect the total sum of money belonging to all participants to be equal. Below is the transaction to create a new smart function, and add it to wallet/name.

"_id": "_fn$evenCryptoBalance",
"name": "evenCryptoBalance?",
"doc": "The values of added and retracted crypto/balance flakes need to be equal"
"_id": ["_predicate/name", "wallet/balance"],
"txSpec": ["_fn$evenCryptoBalance"],
"txSpecDoc": "The values of added and retracted crypto/balance flakes need to be equal"

As you can see above, there is no smart function code. That is up to you. You will need to write a relevant smart function using objT, objF, and one or more of the below Universal SmartFunctions to write, subtractOwnAddOthers?. objT and objF take no arguments, and are called as follows: (objT) or (objF).

incn optional(inc)
decn optional(dec)
==arg1 arg2 ...(== 1 1 1 1)
+arg1 arg2 ...(+ 1 2 3)
-arg1 arg2 ...(- 10 9 3)
*arg1 arg2 ...(* 90 10 2)
/arg1 arg2 ...(/ 36 3 4)
>arg1 arg2 ...(> 90 10 2)
<arg1 arg2 ...(< 90 10 2)
>=arg1 arg2 ...(>= 90 90 10 2)
<arg1 arg2 ...(< 2 10 90)
<=arg1 arg2 ...(<= 2 10 90 90)
maxarg1 arg2 ...(max 1 2 3)
minarg1 arg2 ...(min 1 2 3)

Write evenCryptoBalance?

Follow the instructions above to write the function, evenCryptoBalance?.