Waves Blockly Chapter 2: Asset creation and asset transfer
This is the second article in a new weekly series of tutorials explaining simple programming tasks on the Waves blockchain with the help of Google Blockly. We would like to thank Silas and Marc Jansen for writing this series of articles and are happy to reward their effort with a small grant from the WavesGo Education Fund.
The second chapter of the Waves Blockly tutorial shows how to create an asset and send that asset after it has been successfully created. To check when the asset was successfully created, a loop is used that regularly checks whether the asset has been created in the blockchain. If you would like to try it yourself, you can find the tool here
Loops are important components of programming languages. They are used to perform tasks repeatedly. There are three different types of loops:
The pre-test loop initially checks whether a particular condition is true or false, and on that basis, decides whether to perform the tasks within the loop.
The post-test loop executes its tasks at least once, checking at the end whether the conditions necessary for a new pass are met, and then repeats the process.
Count-controlled loops perform their tasks a certain number of times. The number can either be fixed via a fixed number, or it can be specified as a variable and thus also changed in the respective loop pass. Loops are found under the point “Loops”, see Figure 1.
To create an asset you need the block shown in Figure 2.
First, you have to specify a name for the asset. The name must contain at least 4 and a maximum of 16 characters. Then you give a description, the description must contain at least one character and may be up to 1,000 characters long. This is followed by the number of tokens you want to create for the asset. In the next point you specify how many decimal places the asset should have. On the Waves platform assets may have a maximum of 8 decimal places. Finally, enter the seed of the account from which the asset is to be created. One finds the block under the field “Waves Transactions”, as shown in Figure 3.
With the creation of an asset and the loops, you can now write a program that first creates an asset, then periodically checks to see if the asset has been created in the blockchain and, if so, sends the asset to a specified address. Figure 4 shows the part of the creation of the asset.
Subsequently, a head-controlled loop, the “while” loop, checks whether the asset has been saved successfully in the blockchain. This is done by checking that the balance of the account that created the asset is greater than zero for the asset. The asset can be identified by the unique asset ID (variable: id) returned as the return value of the asset creation block. The loop for periodic review is shown in Figure 5.
Finally, the asset is sent as soon as it is registered in the blockchain. To do this, the asset ID is used again, the number of tokens to be sent is specified, the recipient address is defined and the seed is specified from which the asset should be send. The seed is the same seed that was used to create the asset. The part of the program for sending the asset can be seen in Figure 6.
Finally, the entire code of the program is shown in Figure 7.
Find us online
Connect with us on social media to stay up to date: