Skip to main content

Restrict Crypto Spending

At this point, anyone can edit anyone else's wallet/balance to 0, and edit their own wallet/balance to infinity. This is clearly not a great situation to be in.

To deal with this, we'll add a smart function that ensures that if you are editing your own wallet/balance, you can subtract, and if you are editing someone else's wallet/balance, you can only add.

In previous lessons, we gave you the full transaction with the exception of the code of the smart function. In this lesson, you'll be writing the full transaction and the smart function. Don't worry,you're ready for it! In this transaction, you need to:

  1. Create a new function
  2. Add that function to the relevant predicate

Creating a New Function

Your _fn object should have the following key-value pairs:

  • _id - This should be the collection of the subject you are creating (_fn).
  • name - We'll use "subtractOwnAddOthers?"
  • code - See Writing the Smart Function below
  • doc - An explanation of this function in words

Adding that Function to the Relevant Predicate

In order to do this, you need to include:

  • _id - The value of your _id key should specify the predicate you are updating with a unique two-tuple (see the previous lesson for an example).
  • spec - A reference to the function you just created; Use a tempid. (See the previous lesson for an example). This is a multi predicate, so your value should be between [ and ].
  • specDoc- The error message that you want to be thrown if a transaction violates this function. Note that if a predicate has multiple smart functions, the specDoc is shared among smart functions

Writing the Smart Function

You will need one (or several) of these Universal SmartFunctions to write, subtractOwnAddOthers?.

FunctionArgumentsExample
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)


You will also need these two context-specific functions:

  • (?o) represents the object (value) for the predicate you are updating. (Technically, this is the proposed object)
  • (?pO) represents the previous object (value) for the predicate you are updating.

And finally, you will need the function, ownWallet?. If you remember, we add ownWallet? to our ledger in Lesson 5. In addOwnSubtractOthers? we also need to figure out whether the wallet being updated belongs to the user doing the updating. However, instead of re-writing the ownWallet?, we can just use it in our smart function. In other words, if you insert (ownWallet?) anywhere in your smart function, it will return true if the wallet is yours, false if it is not.

Write and Add addOwnSubtractOthers?


Follow the instructions above to create a new smart function and add it to the relevant predicate.