How to: Waves dApps — DAO Example using RIDE-language for smart contracts
Couple of days ago, Waves Labs announced new grants for Waves dApp developers.
In this article we’ll learn how to create the simple DAO dApp for voting and collective investing in projects.
DAO dApp demo case
Participants:
Alice — DAO dApp creator
Bob — DAO investor
Cooper — DAO investor
Neli —founder of the startup. She needs 80 WAVES for the project launch
Xena —founder of another startup
Actions:
[DEPOSIT]
Bob, Cooper and other investors can deposit their funds to the DAO dApp account created by Alice.
P.S.: all DAO investors might actually withdraw their funds back by calling [withdrawal] function from DAO’s smart contract.
[VOTE]
Bob, Cooper and other investors can vote for startups and specify the certain amount from deposited funds to the particular startup.
[GET FUNDS]
Neli, Xena or founders of other startups, who was backed by at least 2 investors, are able to withdraw funds from DAO dApp.
All DAO actions:
Preparations
All waves test-net accounts can be easily created manually by using “accounts tool” in RIDE online-IDE
To work with accounts, transactions and smart contracts on the waves test-net we need some waves (i.e.: one transaction cost at least 0.01 waves) on created accounts. Let’s use faucet.
So, we’ll start from multi-user wallet dApp case from official RIDE4dApps documentation.
“In this example, multiple accounts can deposit their funds and safely take them back while no one can interfere with this.
A dApp wallet is implemented where you can send payment as WAVES and save them in the wallet via the callable deposit function or you can take them back via the callable withdraw function.”
DAO dApp solution
Let’s use key-value data storage and data transactions to implement DAO logic:
“The maximum size for a key is 100 characters, and a key can contain arbitrary Unicode code points including spaces and other non-printable symbols. String values have a limit of 32,768 bytes and the maximum number of possible entries in data transaction is 100. Overall, the maximum size of a data transaction is around 140kb — for reference, almost exactly the length of Shakespeare’s play ‘Romeo and Juliet’.”
Let’s create 4 types of data keys and add some balance accounting logic within 2 new functions “vote” and “getFunds”:
xx…xx — address (35 symbols)
xx…xx_ia — investors, available balance (updated by: vote, deposit, withdrawal)
xx…xx_sv — startups, number of votes (updated by: vote, getFunds)
xx…xx_sf — startups, amount of investments (updated by: vote, getFunds)
xx…investor_startup…xx — flag, to prevent repeated votes from one investor
The source code of “Vote” function:
The source code of “getFunds” function:
Important!
Be careful with @Verifier function. Always check (by sigVerify) who is sender of the transaction from dApp account.
Let’s keep only one action available for Alice (dApp owner) — “update script”. It’s important during development and testing of the DAO dApp implementation.
The source code of “Verifier” function:
Now our smart contract is ready for deploy. We can deploy it using RIDE IDE and run deploy().
We can test it using RIDE IDE as well!
dApp Web Application
To allow non-technical users to work with DAO dApp we need to integrate it this some simple html+js web page.
Our web application can work with Waves Blockchain Node using Waves-Transactions API.
We need only two functions in our case:
import { invokeScript, broadcast } from ‘@waves/waves-transactions’
So, let’s look at the “deposit” function implementation using ReactJS:
Open this page to test the demo.
The source code is open for everyone in GitHub.
Be careful with the seed phrase. Waves-Keeper will support invokeScript for dApps developers shortly.
Enjoy the dApp development with Waves!