A DAO is a decentralized autonomous organization that operates on blockchain technology using smart contracts, enabling strangers to coordinate online and make collective decisions without trusting each other; the architecture typically includes a token contract for governance voting, a treasury contract for holding funds, a governance contract for proposal creation and voting, and a time lock contract to introduce delays between votes passing and actions executing, as demonstrated through a practical coding tutorial using Truffle, OpenZeppelin libraries, and Ethereum development tools.
How to Build a DAO on Ethereum with Solidity Smart Contracts
Added:all right so welcome back to dap udiversity so today i've got another coding tutorial for you i'm going to show you how to create a decentralized autonomous organization or a dow so this is one of the hottest use cases for web 3.0 technology that's getting a ton of traction dell's hold the potential to completely change how people coordinate online from corporations to investment funds anywhere where strangers need to you know coordinate online to make decisions and i first created this tutorial for my members inside of the dappy diversity community at blockchain mastery university but because so many people have asked me about this highly requested topic i've decided to make this video public for you all and you get the bonus of having a sneak preview so if you like what you see today and you want to see more videos just like this then head on over to dappyversion.com for slash boot camp to get started today so if you're new around here hey i'm gregory and on this channel i turn you into a blockchain master so if that's something interested in then definitely smash that like button down below and subscribe to this channel alright that being said let's jump in and get started let's start off with the question you know what is a dow okay so a dow is a decentralized autonoma autonomous organization that's what dao is dao that's what it stands for okay and really that is exactly what it sounds like it's an organization that runs on top of the blockchain it's decentralized it's powered by smart contracts and it lets people coordinate online in a way that they never could before okay so dows have definitely caught the attention of the mainstream this is an article here on forbes.com okay talking about what are dells and why you should pay attention so you know he just says can you imagine a way of organizing with other people around the world without knowing each other and establishing your own rules and making your own decisions autonomously all encoded on a blockchain while daos are making this real so essentially a dow is just an organization that's run by smart contracts on a blockchain that's what makes smart contracts so powerful as they are computer or their programs around a blockchain okay and a contract is just code that can't change okay that's the power of a dow is you can coordinate with people online across the world who don't have to know each other where code is law code runs your organization okay and you can vote on uh things that you want your organization to do like do you want to spend money okay do you want to hire somebody you want to fire somebody that's what dells enable you to do okay and that's what's caught the attention because you know if this has the potential to replace you know corporations or enhance what they can do um and actually create new kinds of entities that weren't possible before that's the promise of web 3.0 as you can you know coalesce people around some common vision where the code uh you know governs exactly what you decide to do so let me show you the most common use case for dowels as they're being used right now okay so um basically it the easiest thing to use a dow for right now is is to decide how money is spent where a bunch of people can get together and say hey there's a there's a amount of money sitting somewhere in a smart contract and we can all vote on how that money gets spent okay this has applications you know in people getting paid it has applications for investment funds uh or any any anytime a community owns money that's governed by smart contract where no individual owns it or controls it that's that's the easiest use case because dowels uh you know run blockchains the easiest thing to program in an airtight fashion is how money moves around because you have cryptocurrency in a blockchain and then you can vote on how that money moves so that you can make decisions in a decentralized way so that's exactly what we're going to create in this tutorial so i'm going to show you basically on my screen here that we're going to have a treasury like this okay so this treasury is going to hold uh some money let's just just for example purposely say it's like a million dollars so a million dollars uh in ether or ethereum cryptocurrency okay and then we're gonna have token holders who are a part of this dow that's what gives you membership is you hold a special token related to the dow you might have seen this with like other dow governance tokens like the uni swap token the uni token actually is a governance token for that protocol okay or the compound token comp token is a governance token for that protocol that you vote on what those protocols decide to do so in our case we have a fictitious dow for dap or dap university an adapt token and we're gonna have token holders like this okay and they are gonna vote on how money gets spent and this you know treasury is going to hold the money and then they're going to decide who to send it to and you know whether it should be released or not we'll let people vote on it now that previous slide was just a high level overview of how the dow is going to work but here's an exact breakdown of every essential part of what we're going to create today okay so we're going to have several smart contracts on the blockchain that are responsible for this we're going to have a token smart contract all right this is going to govern uh the actual cryptocurrency token that the token holders are going to possess because you need a token in order to vote okay this is going to be an erc20 token so we're going to create a token smart contract all right we're going to give the holders some of these tokens all right so they can actually vote now the the voting is actually going to take place with a different um a smart contract we'll go back in a second but the treasury all right is actually where the ether funds are going to go so we have this treasury here that holds ethereum cryptocurrency or ether all right and these voters here are going to vote on how they want this uh cryptocurrency to be spent all right we're going to propose some sort of recipient we're going to say hey you know do we want this person to receive x amount of money let's say for a fictitious use case it's you know to uh you know fund a new project or something like that well as a token holder you can talk to this governance contract here okay and basically say hey we want to create a proposal that says um you know we want x amount of dollars or ether to go to this recipient you know for some reason and um you will add this proposal to the governance contract and then once that you know uh proposal is accepted the token holders can then vote on this proposal and if enough votes pass you can specify how many votes you want to pass then the governance contract will actually take a special encoded function that is created in the proposal and it will call that function on this treasury contract to release the funds that's what the governor's contract does whenever you credit proposal you basically tell it the function that you wanted to call on a different contract in this case it's going to be released funds from this treasury here and then those eth funds will actually get released to the recipient okay so the last thing to note here in this architecture is that there's gonna be a time lock so basically there's gonna be a small time delay in between you know when the vote passes and when you know stuff can actually happen naturally common endows so like if somebody makes a decision um everybody has time to prepare for it and adjust okay um to get ready for it and so we're gonna have a separate contract that's gonna be a time lock that essentially you know uh is a time to delay in between the time that the vote passes and the action occurs in this case to release the funds from the treasury to the recipient all right so let's go ahead and start coding the project okay so you should have a zip file down below that's going to let you download this code all right to install it i'm just going to look at this github repository here um just for some simple instructions while we're coding the project out okay these are the official setup steps you probably won't have access to this github repository just make sure you check the link down below for the zip file it'll it will have everything you need it'll even have this readme inside of here so you can look at all these instructions okay so just go ahead and download that code open it in your terminal and we definitely want to do the first step which is npm install okay um so i've already done this step it's probably going to just tell me we've already done it but you need to do this step and make sure all your packages install okay um so that's the first step now there's also a prerequisite which is you need to make sure you have your system dependencies on your computer um you know for node.js truffle and ganache okay there should be some videos inside the blockchain mastery program on how to install these individually if you don't i just don't want to you know take the time to do it every single video so definitely go check that out if you need to understand how to install these things okay so um now now that you've got your system appendices installed and you have uh done npm installing your project let's go ahead and just take a look at what we have inside of here all right so we've got a i'm just going to open my uh code in my text editor here i'm using sublime text you can use whatever you'd like okay but we have um you know a contrast directory that has all the smart contracts for the project a migration directory that lets us put these smart contracts in the blockchain we have our truffle dash config file which connects our project to the blockchain and finally we have a script in place that's going to let us actually you know create a new proposal release funds and we're going to see the dow in action okay so to create this project we are going to use some code from open zeppelin library as a starting point so open zeppelin actually has a great framework for creating lots of things like erc20 tokens erc721 tokens so just a translation that means erc20 tokens are cryptocurrencies all right if you want to create a cryptocurrency on ethereum or some other evm compatible chain you can use an erc20 token many of these are solved problems so you can get an off-the-shelf solution with a library like open zeppelin and they have great documentation for this stuff same thing for nft erc721 and then also for a dow okay they actually have a pretty good suite of tools that lets you create a dow without having to figure out all the complicated stuff from scratch okay so you can see the documentation here uh on open zeppelin governance on how to create a token all right how to create the governance contract and also how to create a time lock and they go in detail the proposal life cycle how to create a proposal all right how to cast a vote how to execute a proposal all right so we're not going to just read through the documentation this video you can check it out um if you want more learnings on that we're going to show it to you in real time with our coding project okay and one of the resources i want to uh point you to is the open zeppelin um contracts wizard so you can see like different contracts like erc20 token erc721 1155 and the governor token which is what the governor contract which is we're going to use for the dow here okay and so you can you can kind of play with this in the ui um to try to change some settings like your proposal threshold the quorum um you know erc 20 votes time lock you know how you want your how you want your dow to work you can kind of play around with the settings it'll spit out um some some suggestions for you and of course uh you can edit all this yourself as a developer with code but this will help you get started and that's what we've done this video we've put everything together in our project and we've shown you how to uh actually do this okay so let's go ahead and open up our project here looks like i got two windows let me just kill this one um let's first look at the uh token contract okay all right so the first contract that we use is a token contract okay so this is an erc20 token all right we've just used the open zeplin library and we have just pulled in the erc20 uh library for open zeppelin but specifically this is a voting token all right erc 20 votes is what you want to understand okay this inherits from erc20 but it also has special voting um you know capabilities all right so it has a mint function and a burn function but it also has this special after token transfer uh you know logic inside of it okay so there's some additional uh stuff on here but mostly it works just like a regular erc20 token just with a little extra functionality under the hood so nothing too special here so the next thing that we want to look at is the treasury all right so the treasury contract is pretty simple all right this is where the ethereum cryptocurrency is going to go all right whenever we uh want to transfer it okay or that's where it's going to live until we transfer it all right and then the next contract is the time lock all right this is the time lock controller that just controls the delay all right for the um the delay for the releasing of the funds after the proposal has passed okay and then finally is the governance contract okay this is the main contract like we were saying here the contracts wizard the governor all right that actually uh controls um you know the project so we've got it pulls in all the open zeppelin contracts the governor governor counting simple counter votes quorum uh fraction and then time lock control okay and you can see all those here just like the contract generator so we'll go over how these work a little bit more in a second but um let's go ahead and look at how we put these contracts on the blockchain and everything you need to know when you want to deploy it all right then we're going to watch it in action all right we're going to demonstrate it and then we're going to explain it so we have the contracts coded here again you can see the token settings you can see the governance contract you've got the dap university dow as the title here but let's look at how you put these countries in the blockchain because that's going to give some clues into how how the relationships work so what we've done is uh we've got this deployment script here all right and this deployment script just pulls in our contracts we're going to deploy so we want to pull the token the timelock governance treasury and we want to pull uh some accounts from our wallet here now before i forget let's go ahead and start ganache all right let's make sure it's actually running the right port here this is going to be our development blockchain that lets us you know uh have 10 accounts for free all right with fake ether involved okay let's just make sure that we are on the right port here uh triple dash config 7545 um let's see here yeah 75 45 okay we're connected to ganache so we're going to take each of these accounts and assign them a role in this scenario so account 1 or count 0 i guess you want to say 0 1 2 3 4 5. all right and inside of our migration script we're going to assign those to a niche a variable so we use web3 git accounts to get all the accounts and we see the executor the proposer the voter all right five voters so we have an executor all right the proposer this person is going to propose a vote and these are going to be people who vote on the vote all right so this person executes the proposal this person proposes it and these people vote on what they wanted to pass all right so the first step is we deploy the token we give it some settings so dappy diversity is the name deputy is the symbol and then uh we want to give 1 000 tokens we just want to distribute those initially okay so we deployed the token and then once it is um deployed we want to take 50 tokens and give them to each of the voters all right so after we deploy the contract here um you know the executor is going to own the tokens they own it all right but then they're going to send 50 tokens to each of the voters that's what this block here does all right do token transfer pass in the voter so now we want to configure the time lock delay for the vote so how long we have to wait until we can execute after a proposal has passed so we're just going to in a setting of 1 here all right so in addition to passing this minimum delay which is what this is we also need to set up these two arrays so the first array contains the addresses of all those who are allowed to make a proposal all right so we're just going to use the proposer here this is the person that we just assigned in our scenario um and we also need to uh have an array of the addresses of people who are allowed to execute the proposals so that's what this executor is going to be all right so we just you could you could change this and have a bunch of people could make proposals and then you could change this to have a bunch of people be executors this could even be different dowels if you wanted to or multi-sigs if you really wanted to okay that could get complex but you could change that as a coder if you wanted to all right and then um that's for the time lock okay and now we deploy the time lock that's the next steps we have a token we have a time lock and now we want to deploy the governance okay so the government's going to take some settings like the quorum the percentage of the total supply of people or tokens i should say they need to approach approve the proposals so this is just a percentage so just five percent so five percent of the tokens need to uh you know uh make a vote and pass uh the voting delay so how many blocks actual proposal until the vote becomes active okay so we're just gonna say zero for here and then voting period is how many blocks do you want the vote to stay open so we should say five blocks in this case okay so now we're gonna deploy the governance all right with all these settings that's exactly what's accepting the smart contract and then um we get a copy that the final step is really to deploy the treasury so we've got the token we've got the time lock we've got the governance now we deployed the treasury we actually want to hold the ether funds okay so the time lock will be the owner of the treasury contract that's what's important to understand here so um once the proposal is successfully executed the timelock contract is going to be responsible for calling the function all right so um we're going to give the time lock some ether we're going to get 25 ether okay and we're going to deploy it and we're going to tell who the executor is and how much funds we want to send in that transaction like this okay um and then finally we want to transfer the ownership of the treasury to the time lock address because that's who's going to own it and then finally we can just assign the rules okay so we do the proposer roll time lock proposal rule okay execute a role execute a role and then we grant these roles which you can see here so inside the governance we're starting to have time lock um there's going to be some uh functions inside here that says grant role they're included in the library you can read about that in the documentation but that's what we do here in the deployment setup right all right so now let's go ahead and just deploy the contracts to the blockchain so that we can watch this thing in action so let's do truffle migrate dash reset and again make sure that you have ganache running okay all right so i'm gonna hit enter here wait for this to finish all right so you'll see all the contracts compile first that's the first step and then they are deployed okay all right so i actually had a little bit of a problem with my deployment here um so ganache threw an error telling me that i had an invalid opcode whenever i was deploying so if you encounter this problem i'm going to show you how to fix it okay so you might be able to continue on with the steps tutorial without doing this step but if you have a problem uh i'm going to show you how to fix it on screen so let's stop ganache all right and let's go to our terminal and open up ganache cli this is the command line version of ganache there should be a video in the video vault on how to install that if you haven't already so we're going to do ganache cli dash p which tells it what port to run on 75 45 because inside of our truffle dash config file we connect our project to 7545 so this lets you start ganache cli on the correct port to connect to our truffle project without any code changes all right because by default gennar cli starts on port 8545 so we want to start on port 75.45 so we're going to do that we're going to start the blockchain this way all right so you still see the 10 accounts here with 100 fake ether you see their private keys of course never use these and for real um and we're going to redeploy the contracts here so we'll do truffle migrate dash dash reset just to make sure everything deploys properly all right so now that works so i don't have any envelope code issues so now we have the contracts deployed we actually want to run the script to watch this governance process in action all right and so um in order to do that i actually want to redeploy the contracts i meant to just do this because we're changing ganache cli versus ganache gui i'm actually going to go to line 26 and this deploy contracts tab here all right all right let's deploy contracts file and change it to zero so that's not part of the original demonstration but if you're using genre cli we're just going to change the delay this is how long you have to wait until we can execute uh a past proposal okay so we're just going to adapt this for development purposes because we had a technical glitch sometimes this stuff happens when you're developing for blockchain but you should be able to create real uh delays on real blockchains uh by changing this value okay so let's redeploy the contracts again sorry truffle migrate dash dash reset uh we modify our migration script so we want to redeploy and actually kind of like leaving these problems inside the videos you know you might think you have a perfect video without any problems but this is this is what it's like to build software and me teaching you how i'm fixing this problem as i encounter it uh can impart some of that problem solving knowledge to you and add more tools to your tool belt all right so now let's um actually run the script so we can watch the governance process it take place then we'll explain it so we'll do truffle exec i'm going to put my screen up here so you can see it scripts and we'll say one underscore create proposal all right and this is going to actually do a governor's proposal and vote on it so you can see all of the um you can see all this logged at the console here so we start off are the funds released no so they're in the treasury so we have 25 each inside the treasury we created this proposal right here all right the currency the proposal is zero it's pending all right so it was created at block 36 the proposal deadline is block 41.
so the current block number is 36.
number of votes required to pass is 50.
all right so we're casting votes um current city proposal is active rights proposed state has changed votes four all right 150 votes against 50 votes neutral or abstained is 50. all right so current block is number 42 and we have enough votes to pass so the same proposal is succeeded and then once it's succeeded there's technically the delay before it can get executed so we stick it in the queue with the delay to pass and now it is executed and the funds have been released from the treasury that's true and now there is zero ether inside the treasury and it has been sent to the recipient all right so let's just go and take a look at that here this create proposal script all right so we pull in the token timelock governance treasury we set up the uh you know people in the scenario um just like we saw the migration script all right we set those as variables we track the state of are the funds released uh what how many funds what's the block number what's the proposal state what's the vote uh the amount swept three two yells sorry utils two way five ether all right and then the token is a weight token deployed okay and then we basically delegate uh tokens from each voter all right and then uh we get the treasury we try to see the state of the treasury are the funds released so the answer is no let me run this first time uh we get the balance of the funds all right inside the treasury so let's just get balance the treasury address to see how much ether it does that's what three get balance does gives you the ether balance all right um you get the governance contract all right and then we uh here's where here's where things get interesting um you can basically tell like whenever you're doing a proposal your proposal is to call a function on another smart contract so this is how we do that so we see a weight treasury contract methods release funds we like we return the function itself that we want to call and we say encode abi all right we basically we basically write it in a way that we can tell another smart contract to call this method uh and that's the function that we want to call all right we say you know release funds from treasury that's the description of the encoding function and so we're having a proposal to call this function under the smart contract and so we just pass in the function input function here the description from proposer the treasury address so this is this is the recipient um and then this is the other code and function in the description right so um we show the creative proposal here the id of it we say the state of the approval is now pending all right we take a snapshot of governance okay we get the deadline all right and then we get the block number current block number all right and then we look at the quorum here okay uh and then we cast the votes cast vote one two three four five and we say zero for against our z zero votes against one four two abstain all right and then um we get the proposal state we transfer the tokens for the execution and then um the next thing that we do here is actually uh get the proposed votes all right we see how many are against how many four how many abstained we log that out to the console so that we can verify that happen on the blockchain get the block number again to make sure that it can happen it can pass um we get the proposal state it's now succeeded all right and then now we queue it to release funds from the treasury um and then we execute it all right so the execute is just on the governor's contract we call execute has to come from the correct address which is the executor and then um yeah you basically allow it to do that so here's the proposed date is it released and then here are the funds okay and that's the final state all right and so that's an overview on how to create a decentralized autonomous organization or a dow again dallas have a huge promise for the future okay on how we can coordinate people online how we can manage money how can we manage organizations where people don't have to trust each other they don't have to understand each other's identities but they can still coalesce around a common goal uh with blockchain technology where code is law uh powered by smart contracts all right so that's all i got for today as always smash that like button down below for the youtube algorithm subscribe to this channel if you haven't already that will help these videos out so the more people learn about blockchain and if you like this video and you want to see more videos just like this then i can show you become a blockchain master step by step start to finish over at dap university dot com forward slash boot camp you'd have to be an expert to get started today i've helped people with zero coding experience become real world blockchain developers in a matter of months so that's all i've got hence next time thanks for watching dap university
Up Next

Designing a 5V Regulated Power Supply PCB: KiCad Tutorial
@DrSingaravelanA
7.6K views•2024-03-26

IFS Therapy Demonstration: Complete Session with Unburdening
@IFSCA
95.9K views•2021-01-13

Building a Web3 Music Player with Solidity and NFTs
@DappUniversity
51.5K views•2022-04-08

Game of Thrones Opening Credits: A Cinematic Analysis
@gameofthrones
46.3M views•2011-04-18
Related Study Plans & Knowledge Roadmaps
Structured learning paths in General & Interdisciplinary Studies


































![Fundamentals Lecture #12 [SP23]](https://i.ytimg.com/vi/5tCLOL2tuK4/maxresdefault.jpg)




