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:
- Create a new function
- Add that function to the relevant predicate
Creating a New Function
_fn object should have the following key-value pairs:
_id- This should be the collection of the subject you are creating (
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
_idkey 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
multipredicate, so your value should be between
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
specDocis shared among smart functions
Writing the Smart Function
You will need one (or several) of these Universal SmartFunctions to write,
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.