Mastering Web3 with Waves Module #4

Ventuary Lab
Mastering Web3 with Waves
8 min readJul 7, 2019

Module #4 Practicing with RIDE for dApps

Hey! It’s “Mastering Web3 with Waves” online course. Module 4.

In this module, we’ll be practicing with smart contracts and we’ll also learn how to work with Waves main libraries.

4.0 Smart Contracts and dApps;
4.1 Coupon Bazaar Web3.0 dApp;
4.2 Commit-Reveal Voting;
4.3 Practice

The smart contract is a computer source code or program which can be executed within a distributed environment like blockchain network. All operations and execution results are stored in the blockchain.

In this definition, a smart contract is not a contract between parties, but it’s a protocol of cooperation between users, and also between user and computer or even computer and computer according to rules described in the smart contract source code.

Waves Smart Contracts are represented by two different types: Smart Accounts and Smart Assets. Any Waves account can be converted to the smart account by applying special account-script. Account script is a program code on RIDE language which contains a set of conditions to allow and deny different types of outgoing transactions.

In order to make a dApp development process easier for developers, RIDE- Waves smart contract language has two types of functions: @Verifier and @Callable.

@Callable functions are able to be called by users from outside. As a result of the execution, new information can be inserted or updated in dApp key-value data storage or (and) funds can be transferred from dApp balance to the caller or other addresses depending on dApp logic. In our case, dApp and smart account are the same entity.

@Callable functions are working with two types of input arguments: args and payment. Waves API has a special function to work with @Callable functions — invokeScript. Look at the example below: “Here you can see WriteSet in the RIDE smart contract code. It means that the result of this invocation will be written into the dApp’s key-value storage.”

Look at the second example below: “Here you can see TransferSet and ScriptResult in the RIDE smart contract code. It means that the result of this invocation will be written into the dApp’s key-value storage and also some funds will be transferred to a certain address at the same time”.

We can use all combinations of WriteSet, TransferSet or both of them using ScriptResult methods.

All operations are atomic!

Remember: We’re building a decentralized Web3 coupon marketplace — “Coupon Bazaar”.

Users are searching for goods and services discounts and they can buy them for a small price in the marketplace.

Each coupon — is a digital asset which represents a special discount from the supplier.

“Coupon Bazaar” is a marketplace. It provides matching, payments operations and delivery service between suppliers and customers.

  1. Suppliers:

• Add an Item
• Withdraw funds

2. Customers:

  • Purchase

Let’s use @Callable functions to implement the dApp for all described functionality! But, first of all, look at the code below: “You can see that we are able to declare any custom functions in RIDE. We’re working mostly with dApp key-value storage so, let’s implement some functions for that.”

Now, we’re ready to implement the functionality for suppliers: “Add item”.

Please, open IDE and add this functionality.

Deploy the dApp script from the previous step and call “Add an item” from JavaScript test file.

Open dApp’s data storage in explorer and put it here:

Users are searching for goods and services discounts and they can buy them for a small price in the marketplace.

Let’s implement “Purchase” for customer.

As you can see our dApp has a special key for supplier’s balance accounting. Also, the customer is able to buy the same coupon many times. All purchases will be recorded to the dApp storage.

Implement the purchase functionality and put here the state change from the tx-explorer.

The “Coupon Bazaar” dApp has a special key for supplier’s balance accounting. To withdraw funds to the supplier’s personal account we should implement withdraw function. Let’s do that:

Implement the withdraw functionality and put here the state change from the tx-explorer.

Looking ahead, let’s imagine that the “Coupon Bazaar” community members may wish to vote for the promotion or removal of certain products. They can do that by using private voting procedure.

But…wait a moment…
Everything in the public blockchain network is transparent. So, any votes will be open for others during the election, and vote of one member will be able to affect the decisions of others.

“Commit-Reveal” scheme will help us with that.

We have two steps now:

Commit step — here we’re collecting encrypted votes by using hash function and some random string (salt).

Reveal step — here we’re collecting decrypted votes and comparing their hashes with recorded hashes.
As you can see there is no way to change the vote after commit step is finished. Nobody knows the votes before the reveal step.

Let’s implement that! But, first of all we have to declare variables and functions:

Commit step — here we’re collecting encrypted votes by using hash function and some random string (salt):

next

let commits = ["G8ZMEiXEGefpEdgEFN5mYr6oEEABJrtcBBLkZf6Ujmcq",

"Bf2yysmAoroXAzVidK1wxuVYpRGLy1nWe6cNAGXBf5Hi",

"ACHSFMGY7bp3aHryCLYc499XvojeGrgBp59zSvwgLnkQ"]

let reveals = ["delisted", "featured", "featured"]

let salts = ["random1", "random2", "random3"]

Implement the committed functionality (using hashes and votes from the code above) in our dApp and put the tx-state changes here:

Try to make three commits from different accounts (with data from the previous step) and put here key-value data from dApp storage:

Reveal step — here we’re collecting decrypted votes and comparing their hashes with recorded hashes.
As you can see there is no way to change the vote after commit step is finished.

Nobody knows the votes before the reveal step. After reveal step is done we’ll see the voting result as a status change.

Try to make three reveal iterations from different accounts (with data from previous steps) and put here the “State Changes” from the third “voteReveal”-invokation transaction:

Remember: We’re building a decentralized Web3 coupon marketplace — “Coupon Bazaar”.

Users are searching for goods and services discounts and they can buy them for a small price in the marketplace.

Each coupon — is a digital asset which represents a special discount from the supplier.

In this step, we’ll try to improve our boilerplate app from “Module 1”.

Open github and fork the mweb3waves repository (https://github.com/AlekseiPupyshev/mweb3waves) to your github account.

Clone it to your local computer and switch to the bchintegration branch.

git clone https://github.com/**your*account**/mweb3waves.git;git checkout bchintegration;npm install;npm run build;npm run server;

Run npm commands after that and open http://localhost:5000/. This is a new boilerplate project for “Coupon Bazaar”.

Let’s look at the source code (package.json and index.jsx):

Here you can see some changes:

1. New important libs were included to the project — waves-transactions and waves-crypto.

2. Most important functions were imported to the react application.

You are already familiar with: { invokeScript, broadcast, waitForTx } functions from IDE Java Script test cases.

nodeInteraction — will able us to get data from dApp key-value storage.

Let’s do some exercises!

Open developer console in http://localhost:5000/ and put the printed result of console.log(JSON.stringify(v))

from:

How to get hashes for commit step implementation?

let commits = ["G8ZMEiXEGefpEdgEFN5mYr6oEEABJrtcBBLkZf6Ujmcq", "Bf2yysmAoroXAzVidK1wxuVYpRGLy1nWe6cNAGXBf5Hi", "ACHSFMGY7bp3aHryCLYc499XvojeGrgBp59zSvwgLnkQ"] 
let reveals = ["delisted", "featured", "featured"]
let salts = ["random1", "random2", "random3"]

Open developer console in http://localhost:5000/ and try to call the “crypto” functions:

Put here the hash for:

"delisted" + "random4"

Using nodeInteraction object put the code to get the current blockchain block number (height):

Hint:

https://wavesplatform.github.io/waves-transactions/globals.html

--

--

Ventuary Lab
Mastering Web3 with Waves

DLT, Web3.0, Blockchain, Decentralization, Startups, Waves Platform