Writing Your First Plutus Smart Contract

img

5) Depositing Funds To The Smart Contract

In this section we will at last build the ability to directly interact with our smart contract. To do so we will be using the payToScript_ function. This function takes 3 inputs.

  1. The address of the smart contract.
  2. The amount of ada we wish to deposit.
  3. A DataScript.

The first two inputs are self explanatory, however the DataScript is much less so. A DataScript is any piece of data which we wish to send with our ada, and which becomes tied to those coins. Each time we deposit ada to a smart contract we must include a DataScript (even if it is empty) as it is used as an input for our ValidatorScript.

Currently we will only be creating a deposit function which sends an empty DataScript to understand the basics. In a future section we will utilise a DataScript in order to lock our coins into the smart contract. Until then, let’s look at the type of our depositing function we are about to write:

1
depositADA :: Value -> MockWallet ()

As you can see we have defined the type signature of depositADA, however this time it has 2 values separated by a ->. What this means is that depositADA takes a Value as input and returns a MockWallet (). A Value is a datatype which represents an Integer value of ada. Therefore this function we are creating takes an Integer amount of ada and then does something with the wallet (which is what MockWallet () means as we went over previously). In our case this “something” is a call to payToScript_ which sends ada to a smart contract.

1
depositADA val = payToScript_ smartContractAddress val unitData

In Haskell, input variable names such as val are assigned on the left-hand side of the =. They follow the order of which they are defined in the type signature. We then used val as the 2nd input to payToScript_. Our 3rd input, unitData, is the default empty DataScript, thus we do not need to construct one ourselves.

Finally, we need to once again use mkFunction to make the Plutus Playground aware of our function.

1
$(mkFunction 'depositADA)

And there we have it, we have implemented the depositADA function and made it available to be used in the Plutus Playground UI.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
module MyFirstPlutusSmartContract where

import qualified Language.PlutusTx as PlutusTx
import Ledger
import Wallet
import Playground.Contract

myFirstValidator :: ValidatorScript
myFirstValidator = ValidatorScript (fromCompiledCode $$(PlutusTx.compile
[|| \(a :: ()) (b :: ()) (c :: ()) -> () ||]))

smartContractAddress :: Address'
smartContractAddress = scriptAddress myFirstValidator

watchSmartContract :: MockWallet ()
watchSmartContract = startWatching smartContractAddress

depositADA :: Value ->; MockWallet ()
depositADA val = payToScript_ smartContractAddress val unitData

$(mkFunction 'watchSmartContract)
$(mkFunction 'depositADA)

Try this out yourself and see just how user friendly it is to deposit ada to the smart contract with so few lines of code. In the next section we will be covering how to withdraw ada.