All language subtitles for [English (auto-generated)] Learn Solidity, Blockchain Development, & Smart Contracts Powered By AI (1)

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian Download
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:27,080 --> 00:00:30,080 all right welcome back welcome to the 2 00:00:30,080 --> 00:00:33,640 defi section of this course we scroll 3 00:00:33,640 --> 00:00:36,600 all the way down here Foundry defi we 4 00:00:36,600 --> 00:00:38,200 can see the code that we're going to 5 00:00:38,200 --> 00:00:41,239 work with for this course now I got to 6 00:00:41,239 --> 00:00:44,280 say ahead of time defi is an absolutely 7 00:00:44,280 --> 00:00:46,640 massive topic quite frankly that 8 00:00:46,640 --> 00:00:49,239 deserves an entire video of itself so 9 00:00:49,239 --> 00:00:50,800 we're just going to give you a brief 10 00:00:50,800 --> 00:00:53,239 rundown of what defi is what you can do 11 00:00:53,239 --> 00:00:54,680 with it and some of the most popular 12 00:00:54,680 --> 00:00:57,719 apps out there so first to get started 13 00:00:57,719 --> 00:01:00,280 we can use this site called defi Lama to 14 00:01:00,280 --> 00:01:02,000 get a snapshot of what's going on in the 15 00:01:02,000 --> 00:01:04,119 defi world right now the total value 16 00:01:04,119 --> 00:01:07,320 locked in defi some of the dominant apps 17 00:01:07,320 --> 00:01:09,400 and what the dominant Protocols are 18 00:01:09,400 --> 00:01:11,000 doing for me we can look at some of 19 00:01:11,000 --> 00:01:13,640 these top five for now too L is a liquid 20 00:01:13,640 --> 00:01:15,520 staking platform which I'm not going to 21 00:01:15,520 --> 00:01:18,920 go into right now makeer da is a CDP or 22 00:01:18,920 --> 00:01:21,159 a collateralized debt position protocol 23 00:01:21,159 --> 00:01:23,560 for making stable coins which is what 24 00:01:23,560 --> 00:01:25,799 we're going to be building a is a 25 00:01:25,799 --> 00:01:27,720 borrowing lending protocol like an open- 26 00:01:27,720 --> 00:01:30,680 Source decentralized Bank curve Finance 27 00:01:30,680 --> 00:01:32,840 is a decentralized exchange more 28 00:01:32,840 --> 00:01:34,439 specifically for working with these 29 00:01:34,439 --> 00:01:36,240 stable coins and then Unis swap is a 30 00:01:36,240 --> 00:01:38,560 general purpose exchange for swapping 31 00:01:38,560 --> 00:01:40,759 tokens and different types of assets to 32 00:01:40,759 --> 00:01:42,280 me if you understand some of these top 33 00:01:42,280 --> 00:01:44,200 ones as you go down the list you can get 34 00:01:44,200 --> 00:01:46,280 more and more intricate or more and more 35 00:01:46,280 --> 00:01:47,560 specific different types of defi 36 00:01:47,560 --> 00:01:49,600 protocols the beauty of Defi and 37 00:01:49,600 --> 00:01:51,680 decentralized finance is that you get 38 00:01:51,680 --> 00:01:53,399 access to sophisticated Financial 39 00:01:53,399 --> 00:01:54,960 products and instruments in a 40 00:01:54,960 --> 00:01:57,320 decentralized context if you're not 41 00:01:57,320 --> 00:01:59,240 super familiar with financial 42 00:01:59,240 --> 00:02:00,479 terminology 43 00:02:00,479 --> 00:02:02,039 some of this might be a little bit 44 00:02:02,039 --> 00:02:03,600 difficult to grasp but it is a 45 00:02:03,600 --> 00:02:05,640 phenomenal Rabbit Hole to go down and it 46 00:02:05,640 --> 00:02:08,879 is a absolute ton of fun there's so many 47 00:02:08,879 --> 00:02:10,720 cool things you can do in Defi and in my 48 00:02:10,720 --> 00:02:14,160 mind defi is one of if not the most 49 00:02:14,160 --> 00:02:16,400 interesting and most important 50 00:02:16,400 --> 00:02:18,920 industries and applications that smart 51 00:02:18,920 --> 00:02:21,280 contracts enable so I highly recommend 52 00:02:21,280 --> 00:02:23,879 you get used to some def some great 53 00:02:23,879 --> 00:02:25,640 places to learn about defi are going to 54 00:02:25,640 --> 00:02:27,480 be places like bankless which has a 55 00:02:27,480 --> 00:02:29,680 podcast that I listen to again like I 56 00:02:29,680 --> 00:02:31,800 said metamask learn to learn more 57 00:02:31,800 --> 00:02:34,239 specifically about wallets and safety 58 00:02:34,239 --> 00:02:37,560 about wallets and honestly so many more 59 00:02:37,560 --> 00:02:39,680 most Concepts that you've learned in web 60 00:02:39,680 --> 00:02:41,720 2 or traditional Finance translate over 61 00:02:41,720 --> 00:02:44,159 to defi really well one of my favorite 62 00:02:44,159 --> 00:02:45,640 things to tell people to do is to try 63 00:02:45,640 --> 00:02:49,200 out a and Unis swap to really see how 64 00:02:49,200 --> 00:02:50,800 some of these protocols work so if we 65 00:02:50,800 --> 00:02:51,920 scroll down here we can go to the 66 00:02:51,920 --> 00:02:54,560 website same thing with this one scroll 67 00:02:54,560 --> 00:02:58,000 down we can go to the website here in a 68 00:02:58,000 --> 00:02:59,519 we can even launch the app and we can 69 00:02:59,519 --> 00:03:00,440 see 70 00:03:00,440 --> 00:03:01,959 what this app even looks like let's go 71 00:03:01,959 --> 00:03:05,000 ahead connect our wallet here I am going 72 00:03:05,000 --> 00:03:07,799 to switch to I'm going to check my chain 73 00:03:07,799 --> 00:03:10,120 that I'm on we're on too testnet if you 74 00:03:10,120 --> 00:03:11,480 look up at the top one thing that's 75 00:03:11,480 --> 00:03:12,879 really cool is you can see this little 76 00:03:12,879 --> 00:03:15,480 ipfs thing here this little ipfs thing 77 00:03:15,480 --> 00:03:17,640 only happens with brave but it means 78 00:03:17,640 --> 00:03:20,560 that this site is also hosted on ipfs 79 00:03:20,560 --> 00:03:22,959 right web 3 all about decentral but if 80 00:03:22,959 --> 00:03:25,480 we scroll down we can see we can supply 81 00:03:25,480 --> 00:03:28,000 different assets and get an interest 82 00:03:28,000 --> 00:03:30,720 rate on those assets we Supply them for 83 00:03:30,720 --> 00:03:33,519 example if we were to supply usdc which 84 00:03:33,519 --> 00:03:35,560 is a United States dollar Peg stable 85 00:03:35,560 --> 00:03:39,159 coin we'd get 2.45% interest similar 86 00:03:39,159 --> 00:03:41,599 exactly to how a bank account works the 87 00:03:41,599 --> 00:03:43,239 reason that we get this interest is 88 00:03:43,239 --> 00:03:45,519 because there are other people who are 89 00:03:45,519 --> 00:03:47,959 borrowing these assets again similar to 90 00:03:47,959 --> 00:03:50,360 a bank and they get charged in interest 91 00:03:50,360 --> 00:03:52,879 rate for borrowing these assets so if 92 00:03:52,879 --> 00:03:55,040 you supply assets you get given an 93 00:03:55,040 --> 00:03:57,799 interest rate and if you borrow assets 94 00:03:57,799 --> 00:03:59,480 you get you have to pay some interest 95 00:03:59,480 --> 00:04:01,239 rate this is why this protocol is so 96 00:04:01,239 --> 00:04:02,799 cool it's permissionless banking 97 00:04:02,799 --> 00:04:05,079 permissionless borrowing and lending of 98 00:04:05,079 --> 00:04:07,640 assets Unis swap is another really cool 99 00:04:07,640 --> 00:04:09,480 application where you can very easily 100 00:04:09,480 --> 00:04:11,640 swap tokens between each other for 101 00:04:11,640 --> 00:04:15,079 example let's if I were to switch to 102 00:04:15,079 --> 00:04:17,000 ethereum mainnet I could choose 103 00:04:17,000 --> 00:04:19,680 something like eth and swap to and asset 104 00:04:19,680 --> 00:04:21,880 the a governance token and it's a 105 00:04:21,880 --> 00:04:24,479 permissionless decentralized exchange 106 00:04:24,479 --> 00:04:27,360 and really enables access to financial 107 00:04:27,360 --> 00:04:29,960 markets in a much more transparent more 108 00:04:29,960 --> 00:04:32,039 accountable and fair way and I just get 109 00:04:32,039 --> 00:04:33,600 absolutely excited about working with 110 00:04:33,600 --> 00:04:35,320 these for those of you looking to get 111 00:04:35,320 --> 00:04:37,120 started here I definitely recommend 112 00:04:37,120 --> 00:04:39,000 trying to use some of these on test Nets 113 00:04:39,000 --> 00:04:40,840 unfortunately not all of these work on 114 00:04:40,840 --> 00:04:43,479 test Nets but I have a couple videos 115 00:04:43,479 --> 00:04:44,919 that shows actually working with some of 116 00:04:44,919 --> 00:04:46,520 these that we can that we can go ahead 117 00:04:46,520 --> 00:04:48,560 and actually watch such as leverage 118 00:04:48,560 --> 00:04:51,440 trading in defi become a defi Quant 119 00:04:51,440 --> 00:04:53,680 flash loans with a which is an advanced 120 00:04:53,680 --> 00:04:56,639 defi tool and a couple of more and when 121 00:04:56,639 --> 00:04:58,919 working with these I don't recommend 122 00:04:58,919 --> 00:05:01,360 doing this on the eum main chain I would 123 00:05:01,360 --> 00:05:03,320 recommend doing something like polygon 124 00:05:03,320 --> 00:05:05,160 optimism or arbitrum where it's going to 125 00:05:05,160 --> 00:05:08,280 be a lot cheaper to make transactions 126 00:05:08,280 --> 00:05:10,280 ethereum fees right now are too high and 127 00:05:10,280 --> 00:05:12,800 that's why we have scaling with layer 128 00:05:12,800 --> 00:05:15,479 twos like arbitrum and optimism one 129 00:05:15,479 --> 00:05:17,000 other thing that I should absolutely 130 00:05:17,000 --> 00:05:19,000 mention now that we're getting into defi 131 00:05:19,000 --> 00:05:20,440 is that there's this topic that we're 132 00:05:20,440 --> 00:05:22,560 not going to have time to go over called 133 00:05:22,560 --> 00:05:26,120 meev M or minor extractable value or 134 00:05:26,120 --> 00:05:28,120 maximal extractable value depending on 135 00:05:28,120 --> 00:05:30,000 who you ask is something something that 136 00:05:30,000 --> 00:05:32,400 plagues the defi industry and something 137 00:05:32,400 --> 00:05:33,720 that a lot of eth cord devs and 138 00:05:33,720 --> 00:05:36,319 protocols like flash Bots are working on 139 00:05:36,319 --> 00:05:38,639 right now basically the concept is if 140 00:05:38,639 --> 00:05:41,160 you are the validator who gets to order 141 00:05:41,160 --> 00:05:43,280 the transactions in a block you can 142 00:05:43,280 --> 00:05:45,199 order those transactions in such a way 143 00:05:45,199 --> 00:05:47,199 that benefit you there are a lot of 144 00:05:47,199 --> 00:05:48,680 different protocols working on 145 00:05:48,680 --> 00:05:51,000 mitigating me and preventing me in the 146 00:05:51,000 --> 00:05:53,039 future or making it more fair and 147 00:05:53,039 --> 00:05:55,759 there's all types of opinions on me I 148 00:05:55,759 --> 00:05:57,680 100% recommend that if you're looking to 149 00:05:57,680 --> 00:06:00,680 go more into really deep meev more into 150 00:06:00,680 --> 00:06:02,800 really deep defi stuff you check out 151 00:06:02,800 --> 00:06:07,199 this flash.net new to me repo and check 152 00:06:07,199 --> 00:06:09,120 out some of these videos and blogs they 153 00:06:09,120 --> 00:06:12,080 are absolutely astonishing to read they 154 00:06:12,080 --> 00:06:15,039 are a ton of fun to read and also show a 155 00:06:15,039 --> 00:06:17,240 terrifying side to the ethereum world as 156 00:06:17,240 --> 00:06:19,680 well but in any case the project that 157 00:06:19,680 --> 00:06:20,919 we're going to be building in this 158 00:06:20,919 --> 00:06:23,639 course is going to be a stable coin now 159 00:06:23,639 --> 00:06:25,680 the concept of stable coins is a bit of 160 00:06:25,680 --> 00:06:28,120 an advanced topic believe it or not 161 00:06:28,120 --> 00:06:29,240 maybe you've heard some stuff about it 162 00:06:29,240 --> 00:06:31,319 maybe haven't in any case we're going to 163 00:06:31,319 --> 00:06:32,840 watch a video that I made previously 164 00:06:32,840 --> 00:06:34,919 about stable coins and what they really 165 00:06:34,919 --> 00:06:36,840 are and what a lot of mainstream media 166 00:06:36,840 --> 00:06:38,720 actually gets wrong about them and by 167 00:06:38,720 --> 00:06:40,319 doing this we as developers are going to 168 00:06:40,319 --> 00:06:42,360 have a much better understanding of what 169 00:06:42,360 --> 00:06:44,080 these stable coins really are and how 170 00:06:44,080 --> 00:06:45,759 they actually work under the hood this 171 00:06:45,759 --> 00:06:47,919 is going to be a tough lesson and I'm 172 00:06:47,919 --> 00:06:49,360 going to say this straight up it's going 173 00:06:49,360 --> 00:06:51,360 to be very difficult there's a lot going 174 00:06:51,360 --> 00:06:54,560 on here I want you to be absolutely sure 175 00:06:54,560 --> 00:06:57,720 to go to chat gbt and ask chat gbt 176 00:06:57,720 --> 00:07:02,000 questions absolutely in the GitHub repo 177 00:07:02,000 --> 00:07:03,759 associated with this course be sure to 178 00:07:03,759 --> 00:07:06,560 use this discussion Tab and ask a lot of 179 00:07:06,560 --> 00:07:09,759 questions in fact if you want you can 180 00:07:09,759 --> 00:07:12,199 even browse around the makerd Forum 181 00:07:12,199 --> 00:07:14,800 which is the protocol behind the die 182 00:07:14,800 --> 00:07:16,639 token which is what our project that 183 00:07:16,639 --> 00:07:18,440 we're going to build is very roughly 184 00:07:18,440 --> 00:07:21,240 based off of and kind of read what 185 00:07:21,240 --> 00:07:23,000 people in the industry are actually 186 00:07:23,000 --> 00:07:24,680 working with and actually doing like I 187 00:07:24,680 --> 00:07:27,639 said there is a lot to defy and this 188 00:07:27,639 --> 00:07:29,680 project that we're making I'm actually 189 00:07:29,680 --> 00:07:31,800 planning on getting audited after we 190 00:07:31,800 --> 00:07:33,720 release this course and this is easily 191 00:07:33,720 --> 00:07:36,080 going to be our most advanced project in 192 00:07:36,080 --> 00:07:38,520 the course hands down even though we 193 00:07:38,520 --> 00:07:40,280 have a couple lessons after this 194 00:07:40,280 --> 00:07:42,639 upgrades governance and introduction to 195 00:07:42,639 --> 00:07:45,000 security this Foundry stable coin 196 00:07:45,000 --> 00:07:47,479 project is the most advanced product 197 00:07:47,479 --> 00:07:49,479 we're working with hands down if you're 198 00:07:49,479 --> 00:07:50,919 able to go through this and understand 199 00:07:50,919 --> 00:07:52,639 everything in this lesson you should be 200 00:07:52,639 --> 00:07:54,280 incredibly proud of yourself and 201 00:07:54,280 --> 00:07:55,919 Incredibly excited to move forward 202 00:07:55,919 --> 00:07:58,159 because this is a hard project and 203 00:07:58,159 --> 00:08:00,680 that's okay take your time don't 204 00:08:00,680 --> 00:08:03,639 overburden yourself there's a lot to go 205 00:08:03,639 --> 00:08:05,639 through here if you've never understood 206 00:08:05,639 --> 00:08:08,800 defi take some time and look into defi 207 00:08:08,800 --> 00:08:11,720 if we go to Google we look up like learn 208 00:08:11,720 --> 00:08:15,319 defi we get a to coin Telegraph article 209 00:08:15,319 --> 00:08:16,919 get some medium 210 00:08:16,919 --> 00:08:20,520 articles maybe even Udi coinbase 211 00:08:20,520 --> 00:08:22,319 coinbase actually has a great article on 212 00:08:22,319 --> 00:08:25,520 this there's a lot of places to learn 213 00:08:25,520 --> 00:08:28,240 about D5 so yes I do recommend if you're 214 00:08:28,240 --> 00:08:30,120 completely unfamiliar with D5 215 00:08:30,120 --> 00:08:32,399 pause the video now take at least 15 20 216 00:08:32,399 --> 00:08:34,080 minutes just to do some Google searching 217 00:08:34,080 --> 00:08:36,399 around right any knowledge that primes 218 00:08:36,399 --> 00:08:38,000 you for this next session is great and 219 00:08:38,000 --> 00:08:40,479 there's really just so much to defi the 220 00:08:40,479 --> 00:08:42,440 main thing is that defi is 221 00:08:42,440 --> 00:08:45,160 permissionless open-source finance and 222 00:08:45,160 --> 00:08:48,959 to me it is the absolute best thing 223 00:08:48,959 --> 00:08:52,680 about smart contracts with defi we can 224 00:08:52,680 --> 00:08:54,959 actually move away from financial 225 00:08:54,959 --> 00:08:57,279 institutions and products that have no 226 00:08:57,279 --> 00:08:59,519 transparency that don't help you that 227 00:08:59,519 --> 00:09:01,959 have centralization risk in the last 20 228 00:09:01,959 --> 00:09:04,600 years have been a history lesson on why 229 00:09:04,600 --> 00:09:08,480 we need defi to take over the world so 230 00:09:08,480 --> 00:09:09,800 definitely want you to do a little bit 231 00:09:09,800 --> 00:09:11,920 of research here so let's do a walk 232 00:09:11,920 --> 00:09:13,519 through the code and then let's walk 233 00:09:13,519 --> 00:09:15,680 through this video on what stable coins 234 00:09:15,680 --> 00:09:17,920 actually 235 00:09:19,399 --> 00:09:22,480 are so here's our codebase if we go into 236 00:09:22,480 --> 00:09:26,839 SRC we see two main files we have 237 00:09:26,839 --> 00:09:28,959 decentralized stable coin. soul and if 238 00:09:28,959 --> 00:09:31,320 we look at into here it's a real 239 00:09:31,320 --> 00:09:35,760 minimalistic very basic is erc20 we've 240 00:09:35,760 --> 00:09:38,560 got some more interesting more advanced 241 00:09:38,560 --> 00:09:41,360 Imports such as erc20 burnable and 242 00:09:41,360 --> 00:09:43,920 ownable but here it looks real similar 243 00:09:43,920 --> 00:09:45,360 to an erc20 right we've got a 244 00:09:45,360 --> 00:09:48,040 Constructor with erc20 we have a burn 245 00:09:48,040 --> 00:09:49,440 function where we can actually burn 246 00:09:49,440 --> 00:09:51,640 tokens remove tokens and we have a mint 247 00:09:51,640 --> 00:09:53,920 function where we can mint and print new 248 00:09:53,920 --> 00:09:58,000 tokens but the main contract is this DSC 249 00:09:58,000 --> 00:10:01,600 engine. soul so this decentralized 250 00:10:01,600 --> 00:10:05,320 stable coin is controlled by this DSC 251 00:10:05,320 --> 00:10:08,240 engine and this has a ton of stuff in 252 00:10:08,240 --> 00:10:10,760 here so of the main functionality in 253 00:10:10,760 --> 00:10:13,440 here is going to be where we deposit 254 00:10:13,440 --> 00:10:16,480 collateral and mint DSC so this stable 255 00:10:16,480 --> 00:10:17,800 coin is going to work because it's going 256 00:10:17,800 --> 00:10:20,040 to be backed by collaterals that have 257 00:10:20,040 --> 00:10:22,000 monetary value we'll talk about more 258 00:10:22,000 --> 00:10:23,839 about that in a minute you can redeem 259 00:10:23,839 --> 00:10:25,680 your collateral remove your collateral 260 00:10:25,680 --> 00:10:27,160 look at all this natat spec by the way 261 00:10:27,160 --> 00:10:29,399 look at all these these comments we we 262 00:10:29,399 --> 00:10:31,360 can burn DSC we can burn our 263 00:10:31,360 --> 00:10:33,480 decentralized stable coin people can get 264 00:10:33,480 --> 00:10:35,160 liquidated what the heck is liquidation 265 00:10:35,160 --> 00:10:37,360 we'll talk about that later they can 266 00:10:37,360 --> 00:10:39,800 mint DSC if they have enough collateral 267 00:10:39,800 --> 00:10:41,240 and we'll talk about collateral soon 268 00:10:41,240 --> 00:10:44,120 deposit collateral redeem collateral Etc 269 00:10:44,120 --> 00:10:45,880 but there's a whole bunch of collateral 270 00:10:45,880 --> 00:10:47,399 stuff and we'll explain that in a little 271 00:10:47,399 --> 00:10:49,959 bit we have this test folder where we 272 00:10:49,959 --> 00:10:52,360 have our unit tests where we go over 273 00:10:52,360 --> 00:10:54,600 engine where we test the engine the 274 00:10:54,600 --> 00:10:56,560 stable coin and also this Oracle lib 275 00:10:56,560 --> 00:10:59,040 thing we have mocks which we know mocks 276 00:10:59,040 --> 00:11:01,760 are and then we have this thing that we 277 00:11:01,760 --> 00:11:04,560 kind of briefly went over which is fuzz 278 00:11:04,560 --> 00:11:07,120 test and we have this invariant thing 279 00:11:07,120 --> 00:11:09,320 that what is this invariant thing and we 280 00:11:09,320 --> 00:11:11,079 have this new type of invariant 281 00:11:11,079 --> 00:11:13,079 underscore test which we're going to go 282 00:11:13,079 --> 00:11:14,959 over and we're going to explain these 283 00:11:14,959 --> 00:11:17,399 invariant tests separate these invariant 284 00:11:17,399 --> 00:11:19,240 tests in my mind separate mediocre 285 00:11:19,240 --> 00:11:21,399 solidity developers from incredibly 286 00:11:21,399 --> 00:11:23,120 powerful incredibly Advanced solidity 287 00:11:23,120 --> 00:11:25,680 developers we've got our scripts as well 288 00:11:25,680 --> 00:11:27,839 real minimal here we mainly just deploy 289 00:11:27,839 --> 00:11:29,560 the stable coin we're going to be using 290 00:11:29,560 --> 00:11:32,240 chain link price feeds to measure the 291 00:11:32,240 --> 00:11:34,000 price of the underlying collateral again 292 00:11:34,000 --> 00:11:36,120 we'll talk about that soon but this is 293 00:11:36,120 --> 00:11:38,360 really the the highle walkthrough and 294 00:11:38,360 --> 00:11:40,040 you can find all the code and all the 295 00:11:40,040 --> 00:11:43,160 information you need on this repo like I 296 00:11:43,160 --> 00:11:44,680 said this is going to be an advanced 297 00:11:44,680 --> 00:11:47,760 section take your time with it be sure 298 00:11:47,760 --> 00:11:50,000 to ask questions use all the tools you 299 00:11:50,000 --> 00:11:52,160 have at your disposal to code along and 300 00:11:52,160 --> 00:11:54,680 understand what we're doing but the 301 00:11:54,680 --> 00:11:55,920 other reason why I wanted to make a 302 00:11:55,920 --> 00:11:57,959 stable coin here is because I strongly 303 00:11:57,959 --> 00:11:59,920 believe stable coin coins are going to 304 00:11:59,920 --> 00:12:01,600 be one of the most important D5 305 00:12:01,600 --> 00:12:03,959 Primitives or D5 products that web 3 has 306 00:12:03,959 --> 00:12:06,519 to offer and in my opinion right now the 307 00:12:06,519 --> 00:12:09,920 current Solutions aren't good enough so 308 00:12:09,920 --> 00:12:11,399 hopefully this inspires some of you to 309 00:12:11,399 --> 00:12:13,839 go out and build a better one but let's 310 00:12:13,839 --> 00:12:16,079 go ahead let's go into this video let's 311 00:12:16,079 --> 00:12:18,600 learn about stable coins and take your 312 00:12:18,600 --> 00:12:21,639 time learning this all right let's watch 313 00:12:21,639 --> 00:12:23,839 this 314 00:12:24,839 --> 00:12:27,040 video when you research stable coins 315 00:12:27,040 --> 00:12:29,920 today you get a lot of misleading 316 00:12:29,920 --> 00:12:31,279 information and that's what we're here 317 00:12:31,279 --> 00:12:32,920 to clarify today in this video we're 318 00:12:32,920 --> 00:12:34,240 going to teach you everything you need 319 00:12:34,240 --> 00:12:36,760 to know about stable coins but actually 320 00:12:36,760 --> 00:12:39,240 what are stable coins why we care 321 00:12:39,240 --> 00:12:41,480 categories and properties designs of top 322 00:12:41,480 --> 00:12:43,720 stable coins and then the real reason 323 00:12:43,720 --> 00:12:46,160 behind what they do what stable coins 324 00:12:46,160 --> 00:12:47,959 really do this video is for both 325 00:12:47,959 --> 00:12:49,920 everybody Technical and non-technical 326 00:12:49,920 --> 00:12:50,920 and we're going to correct a lot of the 327 00:12:50,920 --> 00:12:52,399 misleading information out there there's 328 00:12:52,399 --> 00:12:54,240 going to be a lot of information here 329 00:12:54,240 --> 00:12:56,279 for people less familiar with defo so as 330 00:12:56,279 --> 00:12:57,920 always don't be discouraged if something 331 00:12:57,920 --> 00:12:59,519 doesn't make sense the first time you 332 00:12:59,519 --> 00:13:01,839 buckle up buckle up dum dum we're in for 333 00:13:01,839 --> 00:13:04,800 a volatile ride about stability a stable 334 00:13:04,800 --> 00:13:08,120 coin is a nonvolatile crypto asset 335 00:13:08,120 --> 00:13:10,040 that's it fleshed out we could rephrase 336 00:13:10,040 --> 00:13:12,720 it to a stable coin is a crypto asset 337 00:13:12,720 --> 00:13:15,800 whose buying power fluctuates very 338 00:13:15,800 --> 00:13:17,360 little relative to the rest of the 339 00:13:17,360 --> 00:13:19,320 market and this is the first place where 340 00:13:19,320 --> 00:13:21,800 we disagree with traditional media if 341 00:13:21,800 --> 00:13:23,839 you Google what is a stablecoin you'll 342 00:13:23,839 --> 00:13:25,120 see something like this response 343 00:13:25,120 --> 00:13:26,240 everywhere stable coins are 344 00:13:26,240 --> 00:13:27,920 cryptocurrencies the value which is 345 00:13:27,920 --> 00:13:29,519 pegged or tied to that of of another 346 00:13:29,519 --> 00:13:30,880 currency commodity or financial 347 00:13:30,880 --> 00:13:32,839 instrument and to that I disagree I 348 00:13:32,839 --> 00:13:34,639 think that's an easy initial way to 349 00:13:34,639 --> 00:13:36,000 understand them but not the whole story 350 00:13:36,000 --> 00:13:38,320 a stable coin is a crypto asset whose 351 00:13:38,320 --> 00:13:41,240 buying power stays relatively stable a 352 00:13:41,240 --> 00:13:43,560 good example of buying power would be an 353 00:13:43,560 --> 00:13:45,760 Apple Market if you want to a market to 354 00:13:45,760 --> 00:13:48,120 buy apples with Bitcoin 6 months ago the 355 00:13:48,120 --> 00:13:49,880 number of apples you could buy would be 356 00:13:49,880 --> 00:13:51,839 drastically different than the number of 357 00:13:51,839 --> 00:13:53,480 apples you could buy today that's an 358 00:13:53,480 --> 00:13:56,279 example of buying power changing and not 359 00:13:56,279 --> 00:13:58,279 being very stable however someone buying 360 00:13:58,279 --> 00:13:59,680 apples with do 361 00:13:59,680 --> 00:14:00,880 would probably be able to buy the same 362 00:14:00,880 --> 00:14:02,959 amount of apples 6 months ago to now 363 00:14:02,959 --> 00:14:04,360 that's an example of buying power 364 00:14:04,360 --> 00:14:06,360 staying relatively the same since we 365 00:14:06,360 --> 00:14:07,839 could buy the same amount of apples 366 00:14:07,839 --> 00:14:10,160 today than 6 months ago a dollar would 367 00:14:10,160 --> 00:14:12,440 be considered a more stable asset 368 00:14:12,440 --> 00:14:14,079 whereas Bitcoin would be much less 369 00:14:14,079 --> 00:14:16,399 stable this is what we mean by buying 370 00:14:16,399 --> 00:14:18,959 power an asset whose price fluctuates 371 00:14:18,959 --> 00:14:21,199 rapidly all the time is a poor example 372 00:14:21,199 --> 00:14:24,079 of stable buying power oh it went to 373 00:14:24,079 --> 00:14:26,399 zero this for example would not be a 374 00:14:26,399 --> 00:14:28,639 stable asset now a stable crypto asset 375 00:14:28,639 --> 00:14:30,519 is just a stable asset that's a 376 00:14:30,519 --> 00:14:33,000 cryptocurrency most cryptocurrencies by 377 00:14:33,000 --> 00:14:35,480 Nature aren't stable but we will give 378 00:14:35,480 --> 00:14:37,759 examples of stable coins so summary here 379 00:14:37,759 --> 00:14:39,680 a stable coin is a crypto asset whose 380 00:14:39,680 --> 00:14:42,079 buying power stays relatively the same 381 00:14:42,079 --> 00:14:43,199 and if that's all you take from this 382 00:14:43,199 --> 00:14:45,959 video Perfect great let's keep going now 383 00:14:45,959 --> 00:14:47,680 why do we care about stable coins 384 00:14:47,680 --> 00:14:50,519 because money is 385 00:14:50,519 --> 00:14:52,959 important but not like I Love Money type 386 00:14:52,959 --> 00:14:54,920 of important in everyday Society we need 387 00:14:54,920 --> 00:14:57,560 some type of low volatility AKA stable 388 00:14:57,560 --> 00:15:00,120 currency to fulfill the three functions 389 00:15:00,120 --> 00:15:02,440 of money and for web 3 we need a crypto 390 00:15:02,440 --> 00:15:04,079 version of this the three functions of 391 00:15:04,079 --> 00:15:07,120 money are storage of value unit of 392 00:15:07,120 --> 00:15:10,040 account and medium of exchange storage 393 00:15:10,040 --> 00:15:12,040 of value is a way for us to keep the 394 00:15:12,040 --> 00:15:14,079 value and wealth we've generated putting 395 00:15:14,079 --> 00:15:15,560 dollars in your bank account or buying 396 00:15:15,560 --> 00:15:17,720 stocks cryptocurrencies is a good 397 00:15:17,720 --> 00:15:20,199 example of storing your value apples 398 00:15:20,199 --> 00:15:22,320 would make for a poor storage value 399 00:15:22,320 --> 00:15:24,160 since they would rot over time and lose 400 00:15:24,160 --> 00:15:26,720 their value unit of account is a way to 401 00:15:26,720 --> 00:15:28,759 measure how valuable something is when 402 00:15:28,759 --> 00:15:30,800 you go shopping you see prices being 403 00:15:30,800 --> 00:15:33,240 listed in terms of dollars this is an 404 00:15:33,240 --> 00:15:35,600 example of the dollar being used as a 405 00:15:35,600 --> 00:15:37,600 unit of account pricing something in 406 00:15:37,600 --> 00:15:39,959 Bitcoin would be a poor unit of account 407 00:15:39,959 --> 00:15:42,040 since the prices would change all the 408 00:15:42,040 --> 00:15:44,120 time medium of exchange is an agreed 409 00:15:44,120 --> 00:15:46,440 upon method to transact with each other 410 00:15:46,440 --> 00:15:48,279 buying groceries with dollars is a good 411 00:15:48,279 --> 00:15:50,839 example of using dollars as a medium of 412 00:15:50,839 --> 00:15:53,279 exchange buying groceries with car tires 413 00:15:53,279 --> 00:15:55,600 would make for a poor medium of exchange 414 00:15:55,600 --> 00:15:57,720 since car tires are hard to transact 415 00:15:57,720 --> 00:15:59,399 with in order for our everyday lives to 416 00:15:59,399 --> 00:16:01,600 be efficient we need money to do all 417 00:16:01,600 --> 00:16:03,399 these three things and like I said in 418 00:16:03,399 --> 00:16:06,480 web 3 we need a web 3 money we need to 419 00:16:06,480 --> 00:16:07,959 be able to know the value of stuff in 420 00:16:07,959 --> 00:16:10,120 web 3 be able to pay for things without 421 00:16:10,120 --> 00:16:11,920 prices going crazy and we need to be 422 00:16:11,920 --> 00:16:14,040 able to store our wealth in web 3 in a 423 00:16:14,040 --> 00:16:15,680 decentralized world we need a 424 00:16:15,680 --> 00:16:17,880 decentralized money assets like ethereum 425 00:16:17,880 --> 00:16:20,040 work great as a storage of value and 426 00:16:20,040 --> 00:16:22,399 medium of exchange but fall a little bit 427 00:16:22,399 --> 00:16:24,120 on their unit of account due to their 428 00:16:24,120 --> 00:16:26,000 buying power of volatility however maybe 429 00:16:26,000 --> 00:16:27,519 in the future as ethereum BEC more 430 00:16:27,519 --> 00:16:29,160 adapted it'll become stable and and we 431 00:16:29,160 --> 00:16:30,920 won't even need stable coins but right 432 00:16:30,920 --> 00:16:32,839 now it's a little too volatile and yet 433 00:16:32,839 --> 00:16:34,040 I'll still tell you it's ultrasound 434 00:16:34,040 --> 00:16:35,440 money since I do think it's an amazing 435 00:16:35,440 --> 00:16:36,959 store value but that's a conversation 436 00:16:36,959 --> 00:16:38,639 for another time banket don't kill me 437 00:16:38,639 --> 00:16:40,079 I'd love your content also if you're 438 00:16:40,079 --> 00:16:41,240 here be sure to like And subscribe 439 00:16:41,240 --> 00:16:42,839 because we're just cooking the fire up 440 00:16:42,839 --> 00:16:45,759 and we are about to pop off anyways okay 441 00:16:45,759 --> 00:16:47,880 so now we know what a stable coin is why 442 00:16:47,880 --> 00:16:49,759 we care let's talk about the different 443 00:16:49,759 --> 00:16:52,040 categories of stable coins and here is 444 00:16:52,040 --> 00:16:54,800 the second place I strongly disagree 445 00:16:54,800 --> 00:16:56,839 with traditional media if you search for 446 00:16:56,839 --> 00:16:59,040 types of stable coins you'll get some 447 00:16:59,040 --> 00:17:00,360 that pulls them into categories like 448 00:17:00,360 --> 00:17:02,920 this Fiat collateralized crypto 449 00:17:02,920 --> 00:17:05,160 collateralized commodity collateralized 450 00:17:05,160 --> 00:17:08,000 and then algorithmic now this isn't too 451 00:17:08,000 --> 00:17:10,240 bad of a categorization it does make it 452 00:17:10,240 --> 00:17:12,160 easier for new people to understand but 453 00:17:12,160 --> 00:17:15,199 I think it paints a in acccurate picture 454 00:17:15,199 --> 00:17:17,520 so let's categorize stable coins but 455 00:17:17,520 --> 00:17:20,039 actually the categorizations that I like 456 00:17:20,039 --> 00:17:23,280 are relative stability stability method 457 00:17:23,280 --> 00:17:25,400 and collateral type so what are these 458 00:17:25,400 --> 00:17:27,000 categorizations let's start with 459 00:17:27,000 --> 00:17:28,960 relative stability when we talk about 460 00:17:28,960 --> 00:17:31,480 stability something is stable only 461 00:17:31,480 --> 00:17:33,880 relative to something else the most 462 00:17:33,880 --> 00:17:36,400 popular type of stable coins is pegging 463 00:17:36,400 --> 00:17:38,720 or anchored stable coins and these are 464 00:17:38,720 --> 00:17:40,880 stable coins that are pegged or anchored 465 00:17:40,880 --> 00:17:43,240 to another asset like the US dollar 466 00:17:43,240 --> 00:17:47,640 tether D and usdc are all examples of US 467 00:17:47,640 --> 00:17:50,039 dollar pegged stable coins they Follow 468 00:17:50,039 --> 00:17:52,200 The Narrative of one of these coins 469 00:17:52,200 --> 00:17:54,679 equals $1 and that's how they stay 470 00:17:54,679 --> 00:17:56,320 stable it's stable because they track 471 00:17:56,320 --> 00:17:58,320 the price of another asset that we think 472 00:17:58,320 --> 00:17:59,360 is stable 473 00:17:59,360 --> 00:18:00,960 and most of these stable coins have some 474 00:18:00,960 --> 00:18:03,799 type of mechanism to make them almost 475 00:18:03,799 --> 00:18:06,400 interchangeable with their pegged asset 476 00:18:06,400 --> 00:18:09,080 for example usdc says that for every 477 00:18:09,080 --> 00:18:12,799 usdc token printed or minted there is a 478 00:18:12,799 --> 00:18:14,880 dollar or a bunch of assets that equal a 479 00:18:14,880 --> 00:18:17,000 dollar in some bank account somewhere so 480 00:18:17,000 --> 00:18:19,320 the way it keeps its value is that at 481 00:18:19,320 --> 00:18:21,440 any time you should be able to swap your 482 00:18:21,440 --> 00:18:24,559 usdc for the dollar or at least 483 00:18:24,559 --> 00:18:26,400 hypothetically so something like d on 484 00:18:26,400 --> 00:18:28,120 the other hand uses a permissionless 485 00:18:28,120 --> 00:18:30,440 over collateral ization to maintain its 486 00:18:30,440 --> 00:18:31,919 Peg but we'll get to understanding that 487 00:18:31,919 --> 00:18:33,760 a little later however a stable coin 488 00:18:33,760 --> 00:18:35,679 doesn't have to be pegged to another 489 00:18:35,679 --> 00:18:38,840 asset it can be floating remember to be 490 00:18:38,840 --> 00:18:41,159 considered a stable coin its buying 491 00:18:41,159 --> 00:18:43,480 power just has to stay relatively the 492 00:18:43,480 --> 00:18:46,039 same over time so a floating stable coin 493 00:18:46,039 --> 00:18:48,080 is floating because its buying power 494 00:18:48,080 --> 00:18:50,880 stays the same and it's not tied down to 495 00:18:50,880 --> 00:18:53,000 any other asset with this mechanism you 496 00:18:53,000 --> 00:18:55,039 could hypothetically have a stable coin 497 00:18:55,039 --> 00:18:57,520 that's even more stable than an anchored 498 00:18:57,520 --> 00:18:59,520 or pegged stable coin let's look at an 499 00:18:59,520 --> 00:19:02,120 example let's say I can buy 10 apples 500 00:19:02,120 --> 00:19:04,960 for $10 today but in five years I can 501 00:19:04,960 --> 00:19:07,480 only buy five apples with $10 so it 502 00:19:07,480 --> 00:19:10,640 would cost me $20 to buy 10 apples this 503 00:19:10,640 --> 00:19:13,080 isn't an unheard of phenomenon to happen 504 00:19:13,080 --> 00:19:14,880 and it's commonly just referred to as 505 00:19:14,880 --> 00:19:16,880 inflation now let's introduce a stable 506 00:19:16,880 --> 00:19:19,039 coin whose buying power floats up and 507 00:19:19,039 --> 00:19:20,919 down with the market for now let's just 508 00:19:20,919 --> 00:19:22,559 pretend it does so magically and let's 509 00:19:22,559 --> 00:19:25,520 call it Apple coin today you can buy 10 510 00:19:25,520 --> 00:19:28,280 apples with 10 Apple coins and in 10 511 00:19:28,280 --> 00:19:31,440 years you can also buy 10 apples with 10 512 00:19:31,440 --> 00:19:33,760 Apple coins which one of these assets 513 00:19:33,760 --> 00:19:36,159 would you say is more stable yes the 514 00:19:36,159 --> 00:19:38,480 Apple coin's buying power stayed the 515 00:19:38,480 --> 00:19:40,679 same over the 5-year period so we'd say 516 00:19:40,679 --> 00:19:42,760 it's a more stable asset even though 517 00:19:42,760 --> 00:19:44,440 it's not pegged to something like a 518 00:19:44,440 --> 00:19:47,520 dollar and Yes stable coins like this do 519 00:19:47,520 --> 00:19:49,240 exist and they use a lot of clever 520 00:19:49,240 --> 00:19:51,840 algorithms to do this one such example 521 00:19:51,840 --> 00:19:54,520 is the Ry stable coin by reflexor Labs 522 00:19:54,520 --> 00:19:56,559 how Ry Works might be its own video but 523 00:19:56,559 --> 00:19:57,840 we've left some links in the description 524 00:19:57,840 --> 00:19:59,559 for you if you want to learn more about 525 00:19:59,559 --> 00:20:01,400 how it works because this concept can be 526 00:20:01,400 --> 00:20:03,320 a little hard to grasp to further 527 00:20:03,320 --> 00:20:04,880 explain it let's go through another 528 00:20:04,880 --> 00:20:06,760 analogy let's look at the anchor and the 529 00:20:06,760 --> 00:20:08,679 buoy in this image which of these do you 530 00:20:08,679 --> 00:20:10,520 think is more stable the anchor or the 531 00:20:10,520 --> 00:20:12,159 buoy well it depends on what you're 532 00:20:12,159 --> 00:20:14,440 comparing them to compared to sea level 533 00:20:14,440 --> 00:20:16,880 the buoy is more stable since it'll 534 00:20:16,880 --> 00:20:19,320 always be at sea level as the water 535 00:20:19,320 --> 00:20:21,360 level rises and Falls the distance 536 00:20:21,360 --> 00:20:23,200 between the anchor and the sea level is 537 00:20:23,200 --> 00:20:25,200 constantly changing the buoy is an 538 00:20:25,200 --> 00:20:27,799 example of a floating stable coin now if 539 00:20:27,799 --> 00:20:29,600 we compare these two to the ocean floor 540 00:20:29,600 --> 00:20:31,679 though the anchor is more stable of the 541 00:20:31,679 --> 00:20:34,120 two since it'll always be right on the 542 00:20:34,120 --> 00:20:36,080 ocean floor this is analogous to a 543 00:20:36,080 --> 00:20:38,120 pegged SL anchored stable coin now if 544 00:20:38,120 --> 00:20:39,360 we're going to be really serious with 545 00:20:39,360 --> 00:20:40,919 this analogy then the question might be 546 00:20:40,919 --> 00:20:42,760 well what happens when a storm comes or 547 00:20:42,760 --> 00:20:44,720 what happens when the water's crazy or 548 00:20:44,720 --> 00:20:46,280 the Tide's crazy or something and to 549 00:20:46,280 --> 00:20:47,840 that I'd say you're right and a stable 550 00:20:47,840 --> 00:20:49,840 coin needs to take extra precautions to 551 00:20:49,840 --> 00:20:51,720 take these into account and maybe get 552 00:20:51,720 --> 00:20:54,400 the average sea level over the course of 553 00:20:54,400 --> 00:20:56,559 time and most popular floating stable 554 00:20:56,559 --> 00:20:58,760 coins have some mechanism to account for 555 00:20:58,760 --> 00:21:00,799 turbulence like that summary Peg stable 556 00:21:00,799 --> 00:21:02,640 coins are tied to that of another asset 557 00:21:02,640 --> 00:21:03,919 while floating stable coins use 558 00:21:03,919 --> 00:21:06,000 different mechanisms to keep the same 559 00:21:06,000 --> 00:21:07,880 buying power over time number two 560 00:21:07,880 --> 00:21:10,039 stability method the stability method is 561 00:21:10,039 --> 00:21:12,159 this mechanism that keeps the coin 562 00:21:12,159 --> 00:21:14,240 stable if it's a peg stable coin what is 563 00:21:14,240 --> 00:21:15,919 the pegging mechanism if it's a floating 564 00:21:15,919 --> 00:21:17,600 stable coin well what is the floating 565 00:21:17,600 --> 00:21:19,480 mechanism and it typically revolves 566 00:21:19,480 --> 00:21:21,679 around minting and burning the stable 567 00:21:21,679 --> 00:21:24,320 coins in very specific ways and usually 568 00:21:24,320 --> 00:21:27,159 refers to who or what is doing the 569 00:21:27,159 --> 00:21:28,960 minting and burning these are on a 570 00:21:28,960 --> 00:21:32,039 spectrum of governed to algorithmic in a 571 00:21:32,039 --> 00:21:33,799 governed stable coin there is a 572 00:21:33,799 --> 00:21:36,039 governing body or a centralized body 573 00:21:36,039 --> 00:21:37,880 that is minting and burning the stable 574 00:21:37,880 --> 00:21:40,000 coins you can imagine the maximumly 575 00:21:40,000 --> 00:21:42,000 governed and least algorithmic coin 576 00:21:42,000 --> 00:21:44,720 would be a single person/ entity minting 577 00:21:44,720 --> 00:21:46,279 new stable coins promising that the 578 00:21:46,279 --> 00:21:48,320 coins are not volatile but it could also 579 00:21:48,320 --> 00:21:50,200 be an organization like a government or 580 00:21:50,200 --> 00:21:52,159 even a dow choosing to Mint and burn new 581 00:21:52,159 --> 00:21:54,720 coins these govern coins are typically 582 00:21:54,720 --> 00:21:56,679 considered centralized since there is a 583 00:21:56,679 --> 00:21:58,679 singular body that is controlling the 584 00:21:58,679 --> 00:22:00,039 minting and burning you can make them 585 00:22:00,039 --> 00:22:02,000 more decentralized by introducing a dow 586 00:22:02,000 --> 00:22:03,320 and that kind of makes them more 587 00:22:03,320 --> 00:22:04,799 algorithmic but we'll get more into that 588 00:22:04,799 --> 00:22:08,720 later coins like usdc usdt and TUSD are 589 00:22:08,720 --> 00:22:11,440 examples of governed stable coins on the 590 00:22:11,440 --> 00:22:13,760 other hand an algorithmic stable coin is 591 00:22:13,760 --> 00:22:15,679 a stable coin whose stability is 592 00:22:15,679 --> 00:22:19,080 maintained by a permissionless algorithm 593 00:22:19,080 --> 00:22:21,679 with no human intervention and this is 594 00:22:21,679 --> 00:22:23,559 the third place where I disagree with 595 00:22:23,559 --> 00:22:25,840 traditional media a coin like dy I would 596 00:22:25,840 --> 00:22:27,679 consider much more algorithmic than 597 00:22:27,679 --> 00:22:30,120 govern because uses a permissionless 598 00:22:30,120 --> 00:22:32,360 algorithm to Mint and burn tokens 599 00:22:32,360 --> 00:22:34,039 whereas traditional media might say an 600 00:22:34,039 --> 00:22:36,159 algorithmic stable coin is always under 601 00:22:36,159 --> 00:22:38,039 collateralized or follow senior in 602 00:22:38,039 --> 00:22:39,480 shares or something like that but an 603 00:22:39,480 --> 00:22:41,760 algorithmic stable coin is just when a 604 00:22:41,760 --> 00:22:44,120 set of autonomous code or algorithm 605 00:22:44,120 --> 00:22:46,480 dictates the minting and burning there 606 00:22:46,480 --> 00:22:49,480 are zero meddling humans would have been 607 00:22:49,480 --> 00:22:51,440 mine if it hadn't been to those meddling 608 00:22:51,440 --> 00:22:53,520 kids examples of algorithmic stable 609 00:22:53,520 --> 00:22:56,919 coins are going to be die frax Ry and 610 00:22:56,919 --> 00:22:59,120 yes the $40 billion 611 00:22:59,120 --> 00:23:02,039 disaster us yes we're going to talk a 612 00:23:02,039 --> 00:23:03,960 little bit more about classic us and 613 00:23:03,960 --> 00:23:06,200 Luna now a token can have algorithmic 614 00:23:06,200 --> 00:23:07,919 and governed properties in the same way 615 00:23:07,919 --> 00:23:09,159 that it can be somewhere in the middle 616 00:23:09,159 --> 00:23:11,960 of being floating and pegged D for 617 00:23:11,960 --> 00:23:13,760 example does have an autonomous set of 618 00:23:13,760 --> 00:23:15,039 code that dictates the minting and 619 00:23:15,039 --> 00:23:16,720 burning but it does also have a dow 620 00:23:16,720 --> 00:23:18,039 where they can vote on different 621 00:23:18,039 --> 00:23:19,600 interest rates and what can be 622 00:23:19,600 --> 00:23:21,159 collateral types and different things 623 00:23:21,159 --> 00:23:23,760 like that so technically it is a hybrid 624 00:23:23,760 --> 00:23:25,880 system it has some governance mechanisms 625 00:23:25,880 --> 00:23:29,159 and also some algorithmic ones usdc C 626 00:23:29,159 --> 00:23:31,039 would fall purely in the governed 627 00:23:31,039 --> 00:23:33,440 category because it's controlled by a 628 00:23:33,440 --> 00:23:36,480 centralized body us and Luna would fall 629 00:23:36,480 --> 00:23:38,720 almost purely in algorithmic the dirt 630 00:23:38,720 --> 00:23:41,360 roads Blog has some amazing takes on 631 00:23:41,360 --> 00:23:42,919 these pieces and a wonderful 632 00:23:42,919 --> 00:23:44,880 visualization of where on a spectrum of 633 00:23:44,880 --> 00:23:46,400 coins that are more algorithmic or 634 00:23:46,400 --> 00:23:48,360 govern they use dumb as the opposite of 635 00:23:48,360 --> 00:23:50,600 algorithmic instead of govern which 636 00:23:50,600 --> 00:23:52,799 probably isn't wrong most classically 637 00:23:52,799 --> 00:23:54,720 categorized Fiat collateralized stable 638 00:23:54,720 --> 00:23:58,039 coin almost all fall into the Govern or 639 00:23:58,039 --> 00:24:00,120 dumb section since they are dealing with 640 00:24:00,120 --> 00:24:02,279 fiat currency and you need a centralized 641 00:24:02,279 --> 00:24:04,159 entity to onboard that Fiat to the 642 00:24:04,159 --> 00:24:06,000 blockchain you'll also notice on this 643 00:24:06,000 --> 00:24:07,760 chart they have anchored versus 644 00:24:07,760 --> 00:24:10,240 reflexive on the x-axis that's referring 645 00:24:10,240 --> 00:24:11,919 to how the collateral type affects the 646 00:24:11,919 --> 00:24:13,760 stable coin and collateral type is what 647 00:24:13,760 --> 00:24:15,720 we're going to cover next so the summary 648 00:24:15,720 --> 00:24:17,320 here though is algorithmic stable coins 649 00:24:17,320 --> 00:24:19,080 use some sort of autonomous 650 00:24:19,080 --> 00:24:21,039 permissionless code to Mint and burn 651 00:24:21,039 --> 00:24:23,360 tokens whereas a govern stable coin have 652 00:24:23,360 --> 00:24:25,159 some human interaction that mints and 653 00:24:25,159 --> 00:24:27,640 burns the coins and keeps them stable 654 00:24:27,640 --> 00:24:28,840 now before we go into to our final 655 00:24:28,840 --> 00:24:30,399 category let's look at this chart again 656 00:24:30,399 --> 00:24:33,240 we could replace the word anchored with 657 00:24:33,240 --> 00:24:36,480 exogenous and reflexive with endogenous 658 00:24:36,480 --> 00:24:38,200 and we'd have a chart that shows 659 00:24:38,200 --> 00:24:39,960 collateral type versus stability 660 00:24:39,960 --> 00:24:41,760 mechanism which brings us to number 661 00:24:41,760 --> 00:24:44,080 three collateral type now when we say 662 00:24:44,080 --> 00:24:46,520 collateral we mean the stuff backing our 663 00:24:46,520 --> 00:24:48,880 stable coins and giving it value for 664 00:24:48,880 --> 00:24:50,960 example usdc has the dollar as its 665 00:24:50,960 --> 00:24:52,559 collateral and it's the dollar that 666 00:24:52,559 --> 00:24:54,919 gives the usdc token its value because 667 00:24:54,919 --> 00:24:57,200 you hypothetically can swap one usdc for 668 00:24:57,200 --> 00:25:00,720 $1 die is collateralized by many assets 669 00:25:00,720 --> 00:25:02,480 for example you could deposit eth and 670 00:25:02,480 --> 00:25:06,760 get minted dye in return and US was in a 671 00:25:06,760 --> 00:25:09,480 roundabout way collateralized by Luna 672 00:25:09,480 --> 00:25:11,640 exogenous collateral is collateral that 673 00:25:11,640 --> 00:25:14,360 originates from outside the protocol and 674 00:25:14,360 --> 00:25:16,679 endogenous collateral originates from 675 00:25:16,679 --> 00:25:18,919 inside the protocol so one of the easier 676 00:25:18,919 --> 00:25:21,240 ways to Define what type of collateral 677 00:25:21,240 --> 00:25:22,960 protocol is using is to ask this 678 00:25:22,960 --> 00:25:25,799 question if the stable coin fails does 679 00:25:25,799 --> 00:25:28,840 the underlying collateral also fail if 680 00:25:28,840 --> 00:25:32,799 yes it's endogenous if no it's exogenous 681 00:25:32,799 --> 00:25:35,679 if usdc fails the protocol does the 682 00:25:35,679 --> 00:25:38,080 underlying collateral the dollar failed 683 00:25:38,080 --> 00:25:40,360 no so the protocol has exogenous 684 00:25:40,360 --> 00:25:42,720 collateral if the usdc stable coin fails 685 00:25:42,720 --> 00:25:44,440 the dollar is going to keep being the 686 00:25:44,440 --> 00:25:47,720 dollar if die the stable coin fails does 687 00:25:47,720 --> 00:25:50,720 the underlying collateral eth also fail 688 00:25:50,720 --> 00:25:53,440 no so the die system is exogenous the 689 00:25:53,440 --> 00:25:55,720 value of eth isn't dependent on the 690 00:25:55,720 --> 00:25:58,960 value of Dy if us fails does the 691 00:25:58,960 --> 00:26:02,720 underlying collateral Luna Tera fail yes 692 00:26:02,720 --> 00:26:04,240 absolutely and this is exactly what 693 00:26:04,240 --> 00:26:05,840 happened that caused the system to lose 694 00:26:05,840 --> 00:26:08,080 40 billion dollars in what seemed like a 695 00:26:08,080 --> 00:26:10,559 day exogenous collateral originates from 696 00:26:10,559 --> 00:26:12,320 outside the protocol endogenous 697 00:26:12,320 --> 00:26:14,559 collateral originates from inside the 698 00:26:14,559 --> 00:26:16,520 protocol two other good tests that you 699 00:26:16,520 --> 00:26:18,760 can ask are was the collateral created 700 00:26:18,760 --> 00:26:20,399 with the sole purpose of being 701 00:26:20,399 --> 00:26:23,320 collateral or does the protocol own the 702 00:26:23,320 --> 00:26:25,640 issuance of the underlying collateral if 703 00:26:25,640 --> 00:26:27,159 the answer is yes to either one of those 704 00:26:27,159 --> 00:26:29,480 then it's endogenous collateral now the 705 00:26:29,480 --> 00:26:31,480 traditional media usually says that 706 00:26:31,480 --> 00:26:33,799 algorithmic stable coins are to blame 707 00:26:33,799 --> 00:26:34,960 but I think what they're really 708 00:26:34,960 --> 00:26:37,240 referring to is endogenously 709 00:26:37,240 --> 00:26:39,080 collateralized table coins it makes 710 00:26:39,080 --> 00:26:40,120 sense that they can be scary and 711 00:26:40,120 --> 00:26:41,559 potentially dangerous because their 712 00:26:41,559 --> 00:26:43,799 value kind of comes from nothing 713 00:26:43,799 --> 00:26:45,640 exogenously collateralized stable coins 714 00:26:45,640 --> 00:26:48,120 are typically over collateralized 715 00:26:48,120 --> 00:26:50,240 meaning there's more value of collateral 716 00:26:50,240 --> 00:26:52,159 than there is of the stable coins here 717 00:26:52,159 --> 00:26:53,760 we have another image from dirt roads 718 00:26:53,760 --> 00:26:55,520 comparing different stable coins the 719 00:26:55,520 --> 00:26:57,640 exogenous versus endogenous collateral 720 00:26:57,640 --> 00:26:59,320 of the protocol calls and how much they 721 00:26:59,320 --> 00:27:02,039 have maker d/ diey has almost all 722 00:27:02,039 --> 00:27:04,279 exogenous collateral frax which is 723 00:27:04,279 --> 00:27:05,559 another stable coin we haven't really 724 00:27:05,559 --> 00:27:07,360 spoken about too much has a mix of 725 00:27:07,360 --> 00:27:09,679 exogenous and endogenous collateral and 726 00:27:09,679 --> 00:27:12,840 the old teral Luna and US system had 727 00:27:12,840 --> 00:27:15,000 mainly endogenous collateral which is 728 00:27:15,000 --> 00:27:16,880 how the system was able to crumble so 729 00:27:16,880 --> 00:27:19,600 quickly so yeah endogenously 730 00:27:19,600 --> 00:27:21,679 collateralized stable coins don't have a 731 00:27:21,679 --> 00:27:23,640 great track record so why would you want 732 00:27:23,640 --> 00:27:27,000 to make one well the answer is scale and 733 00:27:27,000 --> 00:27:28,919 often times people also say Capital 734 00:27:28,919 --> 00:27:30,799 efficiency with exogenously 735 00:27:30,799 --> 00:27:32,880 collateralized stable coins the only way 736 00:27:32,880 --> 00:27:34,679 you can mint more stable coins is by 737 00:27:34,679 --> 00:27:36,520 onboarding more collateral you can only 738 00:27:36,520 --> 00:27:38,600 have a stable coin market cap that is 739 00:27:38,600 --> 00:27:41,279 high or as high as that is the value of 740 00:27:41,279 --> 00:27:43,000 all your collateral so if you want to 741 00:27:43,000 --> 00:27:46,000 have $68 billion in stable coins that 742 00:27:46,000 --> 00:27:49,000 that means you need to have $68 billion 743 00:27:49,000 --> 00:27:51,039 worth of collateral and that's a lot of 744 00:27:51,039 --> 00:27:52,600 money that you would need to onboard to 745 00:27:52,600 --> 00:27:55,080 your system if you have an endogenously 746 00:27:55,080 --> 00:27:57,360 collateralized stable coin you can have 747 00:27:57,360 --> 00:28:00,559 $0 worth of collateral meaning it's much 748 00:28:00,559 --> 00:28:03,440 easier to become massive faster now I 749 00:28:03,440 --> 00:28:05,039 agree with the dirt roads publication 750 00:28:05,039 --> 00:28:06,480 when they say that exha usly 751 00:28:06,480 --> 00:28:08,440 collateralized stable coins can't scale 752 00:28:08,440 --> 00:28:10,039 and I talk more about that in the blog 753 00:28:10,039 --> 00:28:11,480 associated with this video so if you're 754 00:28:11,480 --> 00:28:13,120 interested be sure to check that out 755 00:28:13,120 --> 00:28:14,640 after the rest of this video but watch 756 00:28:14,640 --> 00:28:16,320 the rest of this video cuz we're just 757 00:28:16,320 --> 00:28:18,320 getting started in the blog we also talk 758 00:28:18,320 --> 00:28:20,159 more about seniorage shares and shelling 759 00:28:20,159 --> 00:28:21,960 coin logic which if you're interested in 760 00:28:21,960 --> 00:28:23,840 that stuff definitely check it out most 761 00:28:23,840 --> 00:28:25,760 of these endogenous coins can be traced 762 00:28:25,760 --> 00:28:27,480 back to a paper written by a man named 763 00:28:27,480 --> 00:28:28,679 Robert Sam 764 00:28:28,679 --> 00:28:30,039 where he talks about how to build an 765 00:28:30,039 --> 00:28:32,320 endogenously collateralized stable coin 766 00:28:32,320 --> 00:28:34,440 using a seniorage shares model which 767 00:28:34,440 --> 00:28:35,799 again I'm not going to go into but I 768 00:28:35,799 --> 00:28:37,120 wanted to mention it because it's 769 00:28:37,120 --> 00:28:38,720 probably one of the most influential 770 00:28:38,720 --> 00:28:39,880 papers when it comes to these 771 00:28:39,880 --> 00:28:42,039 endogenously collateralized stable coins 772 00:28:42,039 --> 00:28:44,200 endogenously collateralized stable coins 773 00:28:44,200 --> 00:28:45,760 there a lot of words now there's more 774 00:28:45,760 --> 00:28:47,880 information on the endogenous collateral 775 00:28:47,880 --> 00:28:50,200 debate in the blog but let's do a 776 00:28:50,200 --> 00:28:51,799 thought experiment that I do think is 777 00:28:51,799 --> 00:28:54,840 compelling for endogenous collateralized 778 00:28:54,840 --> 00:28:56,960 stable coins endogenously collateralized 779 00:28:56,960 --> 00:28:59,360 stable coins imagine you have a currency 780 00:28:59,360 --> 00:29:02,120 and it's collateralized 100% by gold and 781 00:29:02,120 --> 00:29:04,640 you run a bank and it's open 24/7 to 782 00:29:04,640 --> 00:29:06,399 allow people to exchange your generic 783 00:29:06,399 --> 00:29:08,919 coin for gold in your vaults people love 784 00:29:08,919 --> 00:29:10,679 the convenience of our stable coin 785 00:29:10,679 --> 00:29:12,200 instead of having to Lug around their 786 00:29:12,200 --> 00:29:14,760 gold so they treat our stable coin as if 787 00:29:14,760 --> 00:29:17,279 it was gold because they know that at 788 00:29:17,279 --> 00:29:19,000 any time they can exchange it at the 789 00:29:19,000 --> 00:29:21,399 bank this is an example of our generic 790 00:29:21,399 --> 00:29:24,559 coin being exogenously collateralized by 791 00:29:24,559 --> 00:29:28,159 gold pegged to its price and governed by 792 00:29:28,159 --> 00:29:31,159 By Us by Me by our entity since we own 793 00:29:31,159 --> 00:29:33,200 the issuance and burning of the coin you 794 00:29:33,200 --> 00:29:35,279 come to our bank we'll issue this coin 795 00:29:35,279 --> 00:29:37,440 or we'll burn it once you redeem your 796 00:29:37,440 --> 00:29:39,000 gold and since you can always exchange 797 00:29:39,000 --> 00:29:41,399 our coin for gold our coin keeps its 798 00:29:41,399 --> 00:29:44,159 value now let's say the bank is only 799 00:29:44,159 --> 00:29:46,559 open 5 days a week does our coin lose 800 00:29:46,559 --> 00:29:48,720 its value now that you can't always 801 00:29:48,720 --> 00:29:51,200 exchange it for gold well probably not 802 00:29:51,200 --> 00:29:53,320 the market probably won't even care now 803 00:29:53,320 --> 00:29:55,000 let's say you need to close the bank for 804 00:29:55,000 --> 00:29:58,039 a week for renovations does our coin 805 00:29:58,039 --> 00:30:00,679 lose its value now well let's say we 806 00:30:00,679 --> 00:30:03,480 close the bank for a month or a year or 807 00:30:03,480 --> 00:30:06,080 a decade forever if we get to the point 808 00:30:06,080 --> 00:30:07,919 where you could never actually exchange 809 00:30:07,919 --> 00:30:10,000 our coin for gold or the underlying 810 00:30:10,000 --> 00:30:12,440 collateral again maybe do people just 811 00:30:12,440 --> 00:30:15,760 use the coin as its currency backed by 812 00:30:15,760 --> 00:30:18,720 nothing in a way now it's moved from 813 00:30:18,720 --> 00:30:21,120 being exogenous to now being endogenous 814 00:30:21,120 --> 00:30:24,159 since it's now backed by well itself 815 00:30:24,159 --> 00:30:26,880 this is why dirt roads has reflexive on 816 00:30:26,880 --> 00:30:29,120 their chart one of our coins is no 817 00:30:29,120 --> 00:30:31,159 longer equal to some amount of gold it 818 00:30:31,159 --> 00:30:34,320 now equals one of 819 00:30:34,320 --> 00:30:36,960 itself okay I know there's a lot here 820 00:30:36,960 --> 00:30:39,240 and there's even more in the blog so if 821 00:30:39,240 --> 00:30:40,840 some of this confused you don't be 822 00:30:40,840 --> 00:30:42,399 discouraged you can always come back 823 00:30:42,399 --> 00:30:44,000 there's a ton of supporting documents 824 00:30:44,000 --> 00:30:45,559 and blogs and links that I personally 825 00:30:45,559 --> 00:30:47,399 watch to learn a lot of this as well so 826 00:30:47,399 --> 00:30:49,559 be sure to use that if you get confused 827 00:30:49,559 --> 00:30:50,880 but now let's look at some of the top 828 00:30:50,880 --> 00:30:53,440 stable coins what their properties are 829 00:30:53,440 --> 00:30:55,519 and then we're going to get to some 830 00:30:55,519 --> 00:30:57,080 really interesting stuff we're not going 831 00:30:57,080 --> 00:30:59,080 to go too deep into the architectures of 832 00:30:59,080 --> 00:31:01,919 the stable coins we are going to go over 833 00:31:01,919 --> 00:31:03,840 d a little bit more though because the 834 00:31:03,840 --> 00:31:06,480 die/ maker di system is a pretty 835 00:31:06,480 --> 00:31:08,720 standard model for what a stable coin 836 00:31:08,720 --> 00:31:10,039 could look like and it was one of the 837 00:31:10,039 --> 00:31:11,519 most influential stable coins ever 838 00:31:11,519 --> 00:31:13,519 created so there's that and it's 839 00:31:13,519 --> 00:31:15,200 important that we go through these so we 840 00:31:15,200 --> 00:31:17,080 can understand what's currently out 841 00:31:17,080 --> 00:31:19,360 there and why they're currently out 842 00:31:19,360 --> 00:31:21,200 there and then we can 843 00:31:21,200 --> 00:31:24,440 reveal why they're really out there why 844 00:31:24,440 --> 00:31:26,159 there's so many stable coins what are 845 00:31:26,159 --> 00:31:28,120 the incentives for people to Mint them 846 00:31:28,120 --> 00:31:30,000 and it may just blow your mind so let's 847 00:31:30,000 --> 00:31:32,000 start with Dy as we've mentioned before 848 00:31:32,000 --> 00:31:34,519 Dy is a pegged algorithmic and 849 00:31:34,519 --> 00:31:36,600 exogenously collateralized stable coin 850 00:31:36,600 --> 00:31:38,080 it's one of the most influential di 851 00:31:38,080 --> 00:31:40,240 projects ever created and was a huge 852 00:31:40,240 --> 00:31:42,880 factor in supercharging the dii space 853 00:31:42,880 --> 00:31:45,080 roughly the way it works is you deposit 854 00:31:45,080 --> 00:31:47,120 eth or some other crypto collateral into 855 00:31:47,120 --> 00:31:49,600 the smart contract that has this die 856 00:31:49,600 --> 00:31:51,880 algorithm code and based off the current 857 00:31:51,880 --> 00:31:54,120 collateral to US dollar or eth to US 858 00:31:54,120 --> 00:31:57,320 dollar price it'll me you some amount of 859 00:31:57,320 --> 00:32:01,000 die you can only mint less dye than the 860 00:32:01,000 --> 00:32:03,159 total value of collateral or eat that 861 00:32:03,159 --> 00:32:05,320 you have this way the system always has 862 00:32:05,320 --> 00:32:07,600 more collateral then they'd have minted 863 00:32:07,600 --> 00:32:10,679 dye additionally every year or so you'll 864 00:32:10,679 --> 00:32:12,639 get charged something called a stability 865 00:32:12,639 --> 00:32:15,679 fee usually around like 2% and now 866 00:32:15,679 --> 00:32:17,080 you're free to do whatever you want with 867 00:32:17,080 --> 00:32:19,039 your die if you want to get your eth 868 00:32:19,039 --> 00:32:20,639 back you have to give your die back to 869 00:32:20,639 --> 00:32:22,919 the smart contract which will then burn 870 00:32:22,919 --> 00:32:25,200 your diey it'll use the current price of 871 00:32:25,200 --> 00:32:27,279 e to figure out how much money it should 872 00:32:27,279 --> 00:32:29,159 give back to you it's because of this 873 00:32:29,159 --> 00:32:31,799 stability fee and collateralized E that 874 00:32:31,799 --> 00:32:34,600 people often refer to this system as a 875 00:32:34,600 --> 00:32:37,039 collateralized debt position since we 876 00:32:37,039 --> 00:32:40,600 technically owe D back to the protocol 877 00:32:40,600 --> 00:32:43,000 at some point so yes all the Dy that's 878 00:32:43,000 --> 00:32:45,880 in existence somebody minted from the 879 00:32:45,880 --> 00:32:48,679 maker protocol and needs to pay it back 880 00:32:48,679 --> 00:32:50,679 at some point if you can't pay your 881 00:32:50,679 --> 00:32:52,880 stability fees or the price of e tanks 882 00:32:52,880 --> 00:32:54,840 and now the value of our collateral is 883 00:32:54,840 --> 00:32:56,320 less than the value of the die that we 884 00:32:56,320 --> 00:32:58,960 minted people can liquidate us which 885 00:32:58,960 --> 00:33:00,960 means they can take our collateral 886 00:33:00,960 --> 00:33:02,639 protocol always needs to have more 887 00:33:02,639 --> 00:33:04,480 collateral than men to die so this is 888 00:33:04,480 --> 00:33:06,120 sort of your punishment for not keeping 889 00:33:06,120 --> 00:33:07,919 the collateral up and a way to save the 890 00:33:07,919 --> 00:33:09,639 system from becoming under 891 00:33:09,639 --> 00:33:11,279 collateralized and then there's also a 892 00:33:11,279 --> 00:33:12,720 maker token that's used to vote for 893 00:33:12,720 --> 00:33:14,320 stuff now the reason I give this 894 00:33:14,320 --> 00:33:16,240 overview is I want your brain to be 895 00:33:16,240 --> 00:33:18,840 asking the question hey uh I get charged 896 00:33:18,840 --> 00:33:21,320 to Mint a stable coin all the die in the 897 00:33:21,320 --> 00:33:23,960 world somebody's being charged to have 898 00:33:23,960 --> 00:33:26,559 it out there someone could take my 899 00:33:26,559 --> 00:33:28,279 collateral if I don't monitor the 900 00:33:28,279 --> 00:33:30,679 balance and most importantly why would I 901 00:33:30,679 --> 00:33:33,200 spend money to Mint this stable coin why 902 00:33:33,200 --> 00:33:34,679 would I be the one to do that great 903 00:33:34,679 --> 00:33:36,399 question that is the fundamental 904 00:33:36,399 --> 00:33:39,440 question and we'll get to that usdc usdc 905 00:33:39,440 --> 00:33:41,840 is a classic pegged governed exogenously 906 00:33:41,840 --> 00:33:43,440 collateralized stable coin it's backed 907 00:33:43,440 --> 00:33:45,240 by real world dollars in a bank account 908 00:33:45,240 --> 00:33:47,279 not much else to say here USD and Luna 909 00:33:47,279 --> 00:33:50,320 the classic old USD and Luna we know it 910 00:33:50,320 --> 00:33:52,200 collapsed but we can learn from what 911 00:33:52,200 --> 00:33:54,159 happened to hopefully prevent it in the 912 00:33:54,159 --> 00:33:56,480 future us was a stable coin pegged to 913 00:33:56,480 --> 00:33:59,399 the dollar algorithmic and endogenous 914 00:33:59,399 --> 00:34:01,880 and it imploded on itself using what we 915 00:34:01,880 --> 00:34:03,799 know about stable coins can you guess 916 00:34:03,799 --> 00:34:06,200 why well since the system was endogenous 917 00:34:06,200 --> 00:34:08,599 once Us lost its Peg Luna became less 918 00:34:08,599 --> 00:34:10,240 attractive to hold since people weren't 919 00:34:10,240 --> 00:34:12,359 holding Luna the price fell and it made 920 00:34:12,359 --> 00:34:14,440 it harder to keep the peg of us which 921 00:34:14,440 --> 00:34:16,720 made Luna's price fall which makes us 922 00:34:16,720 --> 00:34:18,320 harder to hold its bag which makes you 923 00:34:18,320 --> 00:34:20,159 see the pattern people still want to try 924 00:34:20,159 --> 00:34:21,839 using these endogenously collateralized 925 00:34:21,839 --> 00:34:23,639 stable coins because they do scale so 926 00:34:23,639 --> 00:34:25,960 quickly so protocols like frax have come 927 00:34:25,960 --> 00:34:28,079 a long way to do some type of hybrid 928 00:34:28,079 --> 00:34:29,960 between endogenous and exogenously 929 00:34:29,960 --> 00:34:32,599 collateralized stable coin Rye Ry is one 930 00:34:32,599 --> 00:34:34,639 of the few floating stable coins where 931 00:34:34,639 --> 00:34:36,720 it's not pegged to any other asset it's 932 00:34:36,720 --> 00:34:38,679 focused on minimal governance being 933 00:34:38,679 --> 00:34:41,280 floating and using only ethereum as 934 00:34:41,280 --> 00:34:43,399 their type of collateral with a nearly 935 00:34:43,399 --> 00:34:46,399 purely algorithmic stability mechanism 936 00:34:46,399 --> 00:34:49,159 in a way one could argue that because 937 00:34:49,159 --> 00:34:51,520 their collateral is only e the price of 938 00:34:51,520 --> 00:34:54,480 Ry will always be somewhat pegged to the 939 00:34:54,480 --> 00:34:56,679 price of eth but that might be a longer 940 00:34:56,679 --> 00:34:58,760 argument due to the it's floating 941 00:34:58,760 --> 00:35:00,520 algorithmic and exogenously 942 00:35:00,520 --> 00:35:02,040 collateralized now they use some really 943 00:35:02,040 --> 00:35:03,800 cool supply and demand mechanisms to 944 00:35:03,800 --> 00:35:06,280 keep the price stable and nonvolatile 945 00:35:06,280 --> 00:35:07,839 but it's not really important how it 946 00:35:07,839 --> 00:35:10,000 works for the purpose of this video so 947 00:35:10,000 --> 00:35:11,320 once again I'm going to leave some links 948 00:35:11,320 --> 00:35:13,119 in the description if you want to learn 949 00:35:13,119 --> 00:35:15,359 more about Ry the video on screen right 950 00:35:15,359 --> 00:35:17,560 now I think is particularly good at 951 00:35:17,560 --> 00:35:19,920 explaining exactly how it works and 952 00:35:19,920 --> 00:35:22,320 explaining why it's such a good stable 953 00:35:22,320 --> 00:35:24,520 coin for normal average people to have 954 00:35:24,520 --> 00:35:25,800 so now that we've gone over some stable 955 00:35:25,800 --> 00:35:27,720 coins we've gone over the categories why 956 00:35:27,720 --> 00:35:29,440 why we care what is a stable coin let's 957 00:35:29,440 --> 00:35:32,280 talk about what they really do we can 958 00:35:32,280 --> 00:35:34,440 start by asking the question okay which 959 00:35:34,440 --> 00:35:37,119 one of these is the best stable coin and 960 00:35:37,119 --> 00:35:39,720 to that I need to ask the best stable 961 00:35:39,720 --> 00:35:42,960 coin for who centralized govern coins 962 00:35:42,960 --> 00:35:45,119 obviously have the issue of centrality 963 00:35:45,119 --> 00:35:46,560 which sort of defeats the purpose of 964 00:35:46,560 --> 00:35:48,680 being in web 3 so maybe we want some 965 00:35:48,680 --> 00:35:50,560 flavors of algorithmic stable coins 966 00:35:50,560 --> 00:35:52,040 maybe that's probably what we want for 967 00:35:52,040 --> 00:35:54,520 web 3 but these algorithmic coins might 968 00:35:54,520 --> 00:35:56,880 feel untested to non- crypto people and 969 00:35:56,880 --> 00:35:58,400 the fees ass associated with them might 970 00:35:58,400 --> 00:36:00,680 be a little bit scary for me personally 971 00:36:00,680 --> 00:36:03,760 like I said I really love the idea of Ry 972 00:36:03,760 --> 00:36:05,800 the idea is to have stable buying power 973 00:36:05,800 --> 00:36:07,599 as opposed to being pegged to some other 974 00:36:07,599 --> 00:36:09,599 asset and it's algorithmic nature as 975 00:36:09,599 --> 00:36:11,440 opposed to being centralized so it's a 976 00:36:11,440 --> 00:36:13,040 decentralized stable coin that's what we 977 00:36:13,040 --> 00:36:14,839 want but every coin has their trade-offs 978 00:36:14,839 --> 00:36:16,480 and I'd argue there is definitely no 979 00:36:16,480 --> 00:36:18,800 best coin right now the stable coin 980 00:36:18,800 --> 00:36:21,000 that's best for the average person might 981 00:36:21,000 --> 00:36:23,240 matter much less it's the stable coin 982 00:36:23,240 --> 00:36:25,640 that's best for Rich whales might be 983 00:36:25,640 --> 00:36:27,800 what's more important here yes the 984 00:36:27,800 --> 00:36:30,520 stable coins the whales like might be 985 00:36:30,520 --> 00:36:32,359 what's more important now for most 986 00:36:32,359 --> 00:36:34,800 algorithmic stable coins you'll see this 987 00:36:34,800 --> 00:36:36,680 some sort of fee associated with minting 988 00:36:36,680 --> 00:36:39,680 the coins protocols do make money off of 989 00:36:39,680 --> 00:36:42,040 the stable coin systems which I think is 990 00:36:42,040 --> 00:36:43,680 good sometimes they need money for 991 00:36:43,680 --> 00:36:45,640 maintenance incentives for the stability 992 00:36:45,640 --> 00:36:48,040 of the coin or money for improvements so 993 00:36:48,040 --> 00:36:50,079 I do think these fees are good and we 994 00:36:50,079 --> 00:36:51,680 need stable coins for the three 995 00:36:51,680 --> 00:36:54,319 functions of money storage of value unit 996 00:36:54,319 --> 00:36:56,800 of account and medium of exchange but 997 00:36:56,800 --> 00:37:00,200 are you going to be the one to pay these 998 00:37:00,200 --> 00:37:03,200 fees to Mint them and keep them in 999 00:37:03,200 --> 00:37:07,280 circulation someone has to pay to Mint 1000 00:37:07,280 --> 00:37:10,440 these coins and often keep paying the 1001 00:37:10,440 --> 00:37:12,040 market cap for some of these stable 1002 00:37:12,040 --> 00:37:14,599 coins is in the billions if there is a 1003 00:37:14,599 --> 00:37:17,359 1% fee on these and the market cap is 1 1004 00:37:17,359 --> 00:37:21,079 billion we're talking about $10 million 1005 00:37:21,079 --> 00:37:22,839 our average people going to collectively 1006 00:37:22,839 --> 00:37:25,720 pay $10 million a year to keep these in 1007 00:37:25,720 --> 00:37:28,160 circulation no so average people aren't 1008 00:37:28,160 --> 00:37:29,520 printing these for the three functions 1009 00:37:29,520 --> 00:37:32,400 of money well then who is minting these 1010 00:37:32,400 --> 00:37:33,839 so let's play a little thought 1011 00:37:33,839 --> 00:37:35,760 experiment let's say I have eth as an 1012 00:37:35,760 --> 00:37:38,280 investment and I've bought up all the e 1013 00:37:38,280 --> 00:37:40,359 I've sold my house I've sold everything 1014 00:37:40,359 --> 00:37:42,680 I own and I've used everything I have to 1015 00:37:42,680 --> 00:37:45,280 buy ethereum but I want more what can I 1016 00:37:45,280 --> 00:37:47,880 do I can put my eth into one of these 1017 00:37:47,880 --> 00:37:50,400 stable coin protocols get the minted 1018 00:37:50,400 --> 00:37:53,040 stable coin and then sell the stable 1019 00:37:53,040 --> 00:37:55,359 coin for more eth you might have heard 1020 00:37:55,359 --> 00:37:57,520 Concepts like Leverage investing or 1021 00:37:57,520 --> 00:37:59,640 margin trading and this is essentially 1022 00:37:59,640 --> 00:38:01,800 the web 3 equivalent it's kind of funny 1023 00:38:01,800 --> 00:38:03,560 why are stable coins good well because 1024 00:38:03,560 --> 00:38:05,280 we need those three functions of money 1025 00:38:05,280 --> 00:38:07,480 but why are stable coins minted well 1026 00:38:07,480 --> 00:38:09,520 because investors want to make leveraged 1027 00:38:09,520 --> 00:38:12,480 bets in fact most stable coin platforms 1028 00:38:12,480 --> 00:38:14,200 have this as one of the biggest reasons 1029 00:38:14,200 --> 00:38:16,680 to minth their stable coin to multiply 1030 00:38:16,680 --> 00:38:19,560 exposure or to maximize your position on 1031 00:38:19,560 --> 00:38:21,800 some crypto asset now for sophisticated 1032 00:38:21,800 --> 00:38:23,839 investors this isn't new information at 1033 00:38:23,839 --> 00:38:26,920 all this is like investing 101 however 1034 00:38:26,920 --> 00:38:28,920 it does feel weird that we need stable 1035 00:38:28,920 --> 00:38:31,560 coins for the three functions money but 1036 00:38:31,560 --> 00:38:33,680 that's not why they're minted so even 1037 00:38:33,680 --> 00:38:35,480 though I said I really like Ry as a 1038 00:38:35,480 --> 00:38:37,720 stable coin for the people a reason D 1039 00:38:37,720 --> 00:38:39,319 might be higher market cap is that 1040 00:38:39,319 --> 00:38:41,800 investors like The Leverage opportunity 1041 00:38:41,800 --> 00:38:43,640 much more than they like the leverage 1042 00:38:43,640 --> 00:38:45,880 opportunity that Ry offers it could be 1043 00:38:45,880 --> 00:38:47,800 something else but that might be a big 1044 00:38:47,800 --> 00:38:50,040 reason how much stable coins are minted 1045 00:38:50,040 --> 00:38:53,040 are based off of how much investors 1046 00:38:53,040 --> 00:38:55,720 think they can use that stable coin to 1047 00:38:55,720 --> 00:38:57,680 get more exposure to assets that they 1048 00:38:57,680 --> 00:39:00,079 really want so I know we've gone over a 1049 00:39:00,079 --> 00:39:02,040 lot here and the rabbit hole really 1050 00:39:02,040 --> 00:39:04,079 doesn't end there curve. Finance being 1051 00:39:04,079 --> 00:39:05,720 one of the most important protocols for 1052 00:39:05,720 --> 00:39:07,480 stable coins is a really interesting 1053 00:39:07,480 --> 00:39:09,240 story as well my friend Garrett who 1054 00:39:09,240 --> 00:39:11,280 teaches about technology and finance has 1055 00:39:11,280 --> 00:39:13,440 a fantastic example as to why a stable 1056 00:39:13,440 --> 00:39:15,040 coin exchange might be so important you 1057 00:39:15,040 --> 00:39:17,920 might wonder how a stable coin exchange 1058 00:39:17,920 --> 00:39:19,599 ever got off the ground is there really 1059 00:39:19,599 --> 00:39:22,319 any demand out there to trade $1 for $1 1060 00:39:22,319 --> 00:39:24,280 then I think back to my university days 1061 00:39:24,280 --> 00:39:26,440 one weekend my laundry pile grew so 1062 00:39:26,440 --> 00:39:28,440 disgusting I was desperate to use the 1063 00:39:28,440 --> 00:39:30,119 washing machine immediately but the 1064 00:39:30,119 --> 00:39:32,319 laundry machine took quarters and I only 1065 00:39:32,319 --> 00:39:34,160 had dollar bills quarters were in such 1066 00:39:34,160 --> 00:39:35,720 short supply around the dorm room that I 1067 00:39:35,720 --> 00:39:37,839 was willing to pay more than $1 for four 1068 00:39:37,839 --> 00:39:40,520 quarters in this urgent moment I had 1069 00:39:40,520 --> 00:39:42,160 specific utility in mind and that 1070 00:39:42,160 --> 00:39:44,400 changed my personal equation for this 1071 00:39:44,400 --> 00:39:46,400 and a and curve both looking to launch 1072 00:39:46,400 --> 00:39:48,119 their own stable coins soon is going to 1073 00:39:48,119 --> 00:39:50,319 be really interesting as well so we'll 1074 00:39:50,319 --> 00:39:52,079 just have to see how these unfold I do 1075 00:39:52,079 --> 00:39:53,920 think though that we are going to get 1076 00:39:53,920 --> 00:39:55,480 better and better at creating these 1077 00:39:55,480 --> 00:39:58,119 stable coins because we do need they are 1078 00:39:58,119 --> 00:39:59,800 important and for you developers 1079 00:39:59,800 --> 00:40:01,720 watching who want to build one of these 1080 00:40:01,720 --> 00:40:03,319 we have some minimal stable coin 1081 00:40:03,319 --> 00:40:05,400 contract examples in our def minimal 1082 00:40:05,400 --> 00:40:07,400 repo Link in the description so if 1083 00:40:07,400 --> 00:40:09,040 you're looking to tackle this problem 1084 00:40:09,040 --> 00:40:10,760 definitely be sure to get started there 1085 00:40:10,760 --> 00:40:12,359 I'm really excited for the future of 1086 00:40:12,359 --> 00:40:14,800 Defi and for the future of stable coins 1087 00:40:14,800 --> 00:40:16,359 as I think they are a wildly important 1088 00:40:16,359 --> 00:40:19,119 primitive for Defi and I know that this 1089 00:40:19,119 --> 00:40:21,400 was a long video but I hope you all 1090 00:40:21,400 --> 00:40:23,119 learned something and I hope this gives 1091 00:40:23,119 --> 00:40:26,359 you a better idea of what stable coins 1092 00:40:26,359 --> 00:40:29,200 really are and how they're created if 1093 00:40:29,200 --> 00:40:30,800 you learned something leave a comment in 1094 00:40:30,800 --> 00:40:32,680 the comment section if you didn't learn 1095 00:40:32,680 --> 00:40:34,079 something leave a comment in the comment 1096 00:40:34,079 --> 00:40:35,680 section or if I got something wrong 1097 00:40:35,680 --> 00:40:37,480 leave a comment there thank you all for 1098 00:40:37,480 --> 00:40:41,560 watching and I'll see you next 1099 00:40:43,040 --> 00:40:46,200 time all right we have now learned a ton 1100 00:40:46,200 --> 00:40:48,560 about Defi and hopefully I piqu your 1101 00:40:48,560 --> 00:40:50,720 Curiosity on how much more there is 1102 00:40:50,720 --> 00:40:52,640 still to learn but now we're going to 1103 00:40:52,640 --> 00:40:55,880 move on to creating our own stable coin 1104 00:40:55,880 --> 00:40:58,119 here so again you can follow along with 1105 00:40:58,119 --> 00:40:59,720 all the code that we're going we're 1106 00:40:59,720 --> 00:41:01,800 going to be going over in this Foundry 1107 00:41:01,800 --> 00:41:03,960 defi stablecoin f23 section of this 1108 00:41:03,960 --> 00:41:06,040 course and like I said I'm planning on 1109 00:41:06,040 --> 00:41:08,640 getting this code actually audited so be 1110 00:41:08,640 --> 00:41:10,359 sure if you have your GitHub repo be 1111 00:41:10,359 --> 00:41:12,200 sure to watch this repo and look for 1112 00:41:12,200 --> 00:41:14,079 updates I will be posting the audit 1113 00:41:14,079 --> 00:41:15,920 reports in this as well I've got a 1114 00:41:15,920 --> 00:41:17,480 couple videos on what smart contract 1115 00:41:17,480 --> 00:41:19,520 audits are and why they're so important 1116 00:41:19,520 --> 00:41:21,040 and I'll leave a couple links in the get 1117 00:41:21,040 --> 00:41:23,040 AO associated with this course for those 1118 00:41:23,040 --> 00:41:24,640 of you looking to go down the security 1119 00:41:24,640 --> 00:41:26,200 track definitely be sure to watch this 1120 00:41:26,200 --> 00:41:29,400 video but our right we are finally in 1121 00:41:29,400 --> 00:41:33,280 our GitHub repo let's build this stable 1122 00:41:33,280 --> 00:41:38,560 coin mkdir foundary defi stablecoin 1123 00:41:38,560 --> 00:41:41,640 f23 let's open this up with code or you 1124 00:41:41,640 --> 00:41:44,000 know the drill file open folder we're 1125 00:41:44,000 --> 00:41:45,720 going to be going a little faster this 1126 00:41:45,720 --> 00:41:48,280 lesson because a lot of what we're doing 1127 00:41:48,280 --> 00:41:49,640 is just going to be drilling in 1128 00:41:49,640 --> 00:41:51,560 information that you already know we 1129 00:41:51,560 --> 00:41:53,400 have a couple of new things to go over 1130 00:41:53,400 --> 00:41:55,240 such as stateless fuzzing which we'll 1131 00:41:55,240 --> 00:41:57,240 talk about in a bit but a lot of the 1132 00:41:57,240 --> 00:42:00,400 coding aspect of this you already know 1133 00:42:00,400 --> 00:42:04,200 all right we're in our folder Forge nit 1134 00:42:04,200 --> 00:42:06,839 clear let's make a little read me over 1135 00:42:06,839 --> 00:42:09,800 here and talk about the design of our 1136 00:42:09,800 --> 00:42:12,440 protocol so we're going to make a stable 1137 00:42:12,440 --> 00:42:14,880 coin but if you watch that stable coin 1138 00:42:14,880 --> 00:42:16,599 video we're going to make a stable coin 1139 00:42:16,599 --> 00:42:19,480 that is one anchored so when we're 1140 00:42:19,480 --> 00:42:22,480 talking about the relative stability 1141 00:42:22,480 --> 00:42:25,720 it's going be anchored or pegged to the 1142 00:42:25,720 --> 00:42:27,880 US dollar so this means we're going to 1143 00:42:27,880 --> 00:42:29,880 have to put some code in here to make 1144 00:42:29,880 --> 00:42:32,680 sure that our stable coin is always 1145 00:42:32,680 --> 00:42:37,040 worth $1 number two the stability 1146 00:42:37,040 --> 00:42:39,640 stability mechanism or the way we do 1147 00:42:39,640 --> 00:42:42,599 minting is going to be 1148 00:42:42,599 --> 00:42:44,599 algorithmic this means that this is 1149 00:42:44,599 --> 00:42:46,559 going to be a decentralized stable coin 1150 00:42:46,559 --> 00:42:48,599 there's not going to be any centralized 1151 00:42:48,599 --> 00:42:51,720 entity that's going to Mint or burn or 1152 00:42:51,720 --> 00:42:53,880 maintain the price this is going to be 1153 00:42:53,880 --> 00:42:57,920 100% onchain And algorithmic which is 1154 00:42:57,920 --> 00:43:00,559 ideally what we have for the future of 1155 00:43:00,559 --> 00:43:02,440 stable coins there's no controlling 1156 00:43:02,440 --> 00:43:04,200 entity that controls our stable coins so 1157 00:43:04,200 --> 00:43:06,440 this is great A Better stable coin for 1158 00:43:06,440 --> 00:43:08,640 web 3 probably isn't anchored or pegged 1159 00:43:08,640 --> 00:43:11,599 it's probably floating but that's a much 1160 00:43:11,599 --> 00:43:13,720 harder mechanism to do so we're going to 1161 00:43:13,720 --> 00:43:15,200 go ahead with the anchor to Peg for the 1162 00:43:15,200 --> 00:43:18,400 moment and then finally the collateral 1163 00:43:18,400 --> 00:43:21,839 type is going to be you guessed it 1164 00:43:21,839 --> 00:43:23,920 exogenous and it's going to be crypto 1165 00:43:23,920 --> 00:43:25,160 collateral we're going to use 1166 00:43:25,160 --> 00:43:27,240 cryptocurrencies as collateral for for 1167 00:43:27,240 --> 00:43:29,880 this currency we're going to use 1168 00:43:29,880 --> 00:43:32,559 ethereum and Bitcoin as the collateral 1169 00:43:32,559 --> 00:43:35,680 for our system here so with this being 1170 00:43:35,680 --> 00:43:37,760 our architecture we're going to keep 1171 00:43:37,760 --> 00:43:40,119 this in mind okay how can we actually 1172 00:43:40,119 --> 00:43:43,680 make sure that this is always Peg to a 1173 00:43:43,680 --> 00:43:45,240 dollar and one way we can do this is 1174 00:43:45,240 --> 00:43:47,920 with a chain link price feed so with a 1175 00:43:47,920 --> 00:43:50,000 chain link price feed we get the price 1176 00:43:50,000 --> 00:43:54,880 feed and we set a function to exchange 1177 00:43:54,880 --> 00:43:58,960 eth and Bitcoin or whatever their dollar 1178 00:43:58,960 --> 00:44:01,880 equivalent is and this way if right in 1179 00:44:01,880 --> 00:44:04,200 our contract we have this exchange set 1180 00:44:04,200 --> 00:44:06,960 up price of this stable coin should 1181 00:44:06,960 --> 00:44:09,720 hypothetically always be around a dollar 1182 00:44:09,720 --> 00:44:11,440 to make the stability mechanism 1183 00:44:11,440 --> 00:44:14,040 algorithmic we're going to say people 1184 00:44:14,040 --> 00:44:18,720 can only mint stable coin with enough 1185 00:44:18,720 --> 00:44:21,839 collateral and that's going to be CED 1186 00:44:21,839 --> 00:44:23,880 directly into our protocol and the 1187 00:44:23,880 --> 00:44:25,800 collateral type being exogenous of 1188 00:44:25,800 --> 00:44:27,440 course we're only going to allow these 1189 00:44:27,440 --> 00:44:29,599 two types of cryptocurrencies to be 1190 00:44:29,599 --> 00:44:31,400 deposited specifically we're going to 1191 00:44:31,400 --> 00:44:33,839 use wrapped eth and wrapped Bitcoin so 1192 00:44:33,839 --> 00:44:36,160 the erc20 version of eth and the erc20 1193 00:44:36,160 --> 00:44:39,480 version of Bitcoin some might argue that 1194 00:44:39,480 --> 00:44:41,480 this wrapped Bitcoin is a little bit 1195 00:44:41,480 --> 00:44:43,440 centralized depending on who is 1196 00:44:43,440 --> 00:44:45,880 onboarding the Bitcoin into ethereum but 1197 00:44:45,880 --> 00:44:48,280 that's another conversation so over 1198 00:44:48,280 --> 00:44:51,240 collateralized stablecoin with we and 1199 00:44:51,240 --> 00:44:54,520 Bitcoin as the collat for this let's do 1200 00:44:54,520 --> 00:44:56,559 this so the first thing we're going to 1201 00:44:56,559 --> 00:44:58,000 do is is we're going to well we're going 1202 00:44:58,000 --> 00:45:00,599 to get rid of all these goodbye goodbye 1203 00:45:00,599 --> 00:45:03,440 goodbye delete and now we're going to 1204 00:45:03,440 --> 00:45:06,599 create our two main contracts new file 1205 00:45:06,599 --> 00:45:08,800 D 1206 00:45:08,800 --> 00:45:13,160 centralized table coin do a capital c. 1207 00:45:13,160 --> 00:45:15,480 soul and this is just going to be the 1208 00:45:15,480 --> 00:45:18,480 actual token so I'm actually going to 1209 00:45:18,480 --> 00:45:21,359 copy paste my layout of functions 1210 00:45:21,359 --> 00:45:24,079 because I like them I like to have this 1211 00:45:24,079 --> 00:45:27,520 and at the top of course spdx 1212 00:45:27,520 --> 00:45:30,720 license identifier MIT if you want you 1213 00:45:30,720 --> 00:45:33,240 can just copy this directly from GitHub 1214 00:45:33,240 --> 00:45:35,720 repo associated with this course 1215 00:45:35,720 --> 00:45:38,680 SRC right here copy this layout here I 1216 00:45:38,680 --> 00:45:40,720 like to have it as a reference then you 1217 00:45:40,720 --> 00:45:45,200 already know pragma solidity 1218 00:45:45,200 --> 00:45:48,880 0.818 and we'll do contract 1219 00:45:48,880 --> 00:45:50,480 decentralized 1220 00:45:50,480 --> 00:45:54,240 stable coin boom like this and now this 1221 00:45:54,240 --> 00:45:55,400 is going to be one of the main 1222 00:45:55,400 --> 00:45:57,440 differences between this code writing 1223 00:45:57,440 --> 00:45:58,760 here and all the other products that 1224 00:45:58,760 --> 00:46:01,119 we've done we're going to be very 1225 00:46:01,119 --> 00:46:04,359 verbose with our code documentation and 1226 00:46:04,359 --> 00:46:06,880 the reason for this is is when it comes 1227 00:46:06,880 --> 00:46:08,480 to Security Professionals who are going 1228 00:46:08,480 --> 00:46:10,559 to review this code we're going to make 1229 00:46:10,559 --> 00:46:13,160 their lives so much easier if we have a 1230 00:46:13,160 --> 00:46:15,760 ton of text explaining what our code is 1231 00:46:15,760 --> 00:46:18,599 doing additionally if you work with AIS 1232 00:46:18,599 --> 00:46:21,119 AIS are fantastic at reading and 1233 00:46:21,119 --> 00:46:22,960 understanding language so the more 1234 00:46:22,960 --> 00:46:24,680 language that we have to explain what 1235 00:46:24,680 --> 00:46:27,599 our code should do the better off our 1236 00:46:27,599 --> 00:46:30,960 code can be sent through some AI model 1237 00:46:30,960 --> 00:46:33,440 to make sure that it doesn't have issues 1238 00:46:33,440 --> 00:46:35,800 so we're going to go ahead add a little 1239 00:46:35,800 --> 00:46:38,760 bit of npec here right from the get-go 1240 00:46:38,760 --> 00:46:40,680 at Title 1241 00:46:40,680 --> 00:46:45,000 dentra by stable coin all one word at 1242 00:46:45,000 --> 00:46:47,720 author Patrick Collins or put your name 1243 00:46:47,720 --> 00:46:50,200 here Yep this is open source MIT license 1244 00:46:50,200 --> 00:46:51,599 you can do whatever the heck you want 1245 00:46:51,599 --> 00:46:54,640 with it I'm even going to say collateral 1246 00:46:54,640 --> 00:46:57,680 exogenous eth and BTC 1247 00:46:57,680 --> 00:46:59,839 minting or the stability mechanism is 1248 00:46:59,839 --> 00:47:02,480 going to be 1249 00:47:02,480 --> 00:47:05,359 aloric meaning it's decentralized 1250 00:47:05,359 --> 00:47:08,640 relative stability is going to be pegged 1251 00:47:08,640 --> 00:47:12,319 to USD then I'm going to say under here 1252 00:47:12,319 --> 00:47:16,599 this is the contract meant to be 1253 00:47:16,599 --> 00:47:18,720 governed by 1254 00:47:18,720 --> 00:47:24,520 DSC engine this contract is just the I'm 1255 00:47:24,520 --> 00:47:28,559 going to toggle word rep here ear T20 1256 00:47:28,559 --> 00:47:33,400 implementation of our stable coin system 1257 00:47:33,400 --> 00:47:34,960 so that's what this is going to be this 1258 00:47:34,960 --> 00:47:37,000 decentralized stable coin is purely 1259 00:47:37,000 --> 00:47:39,960 going to be an erc20 with minting and 1260 00:47:39,960 --> 00:47:41,720 burning and stuff right it's not going 1261 00:47:41,720 --> 00:47:44,119 to be it's not going to have any of the 1262 00:47:44,119 --> 00:47:45,720 logic the logic is going to be in a 1263 00:47:45,720 --> 00:47:48,280 separate contract so let's go ahead and 1264 00:47:48,280 --> 00:47:51,760 let's make this so first Constructor 1265 00:47:51,760 --> 00:47:53,880 boom and we're going to use open 1266 00:47:53,880 --> 00:47:56,520 Zeppelin to get this kick started so 1267 00:47:56,520 --> 00:47:59,200 right at the top import well actually 1268 00:47:59,200 --> 00:48:02,400 before we even do that Forge install 1269 00:48:02,400 --> 00:48:06,440 open Zeppelin slopen Zeppelin D 1270 00:48:06,440 --> 00:48:10,240 contracts D- no- commit and we go ahead 1271 00:48:10,240 --> 00:48:12,160 we went ahead and installed that now 1272 00:48:12,160 --> 00:48:14,880 we're going to open up our Foundry dotl 1273 00:48:14,880 --> 00:48:16,319 we're going to add some remappings in 1274 00:48:16,319 --> 00:48:20,960 here so REM mappings equals at open 1275 00:48:20,960 --> 00:48:25,760 Zeppelin SLC contracts equals lib slopen 1276 00:48:25,760 --> 00:48:30,280 Zeppelin slash contracts slash contracts 1277 00:48:30,280 --> 00:48:32,520 that looks pretty good right up at at 1278 00:48:32,520 --> 00:48:35,760 the top of this now import little named 1279 00:48:35,760 --> 00:48:36,839 import 1280 00:48:36,839 --> 00:48:39,319 erc20 we're going to import this erc20 1281 00:48:39,319 --> 00:48:41,400 burnable contract I'll explain this in a 1282 00:48:41,400 --> 00:48:44,960 second and then also erc20 from atop 1283 00:48:44,960 --> 00:48:48,280 Zeppelin contracts toen 1284 00:48:48,280 --> 00:48:50,520 erc20 1285 00:48:50,520 --> 00:48:54,760 extensions erc20 burnable dossou did I 1286 00:48:54,760 --> 00:48:57,160 spell this right looks like that that's 1287 00:48:57,160 --> 00:48:59,200 right so what did I oh oops looks like I 1288 00:48:59,200 --> 00:49:00,799 spelled contracts wrong let's spell it 1289 00:49:00,799 --> 00:49:03,079 right okay cool that looks good now what 1290 00:49:03,079 --> 00:49:05,200 we're going to say is that our 1291 00:49:05,200 --> 00:49:06,839 decentralized stable coin let me even 1292 00:49:06,839 --> 00:49:08,680 zoom out a little bit Yep this is going 1293 00:49:08,680 --> 00:49:10,200 to be a big one so we're going to zoom 1294 00:49:10,200 --> 00:49:11,440 out a little bit here hopefully you all 1295 00:49:11,440 --> 00:49:16,240 can see this is ec20 burnable okay and 1296 00:49:16,240 --> 00:49:17,520 the reason that we're going to use this 1297 00:49:17,520 --> 00:49:19,280 contract if we command clicking it or 1298 00:49:19,280 --> 00:49:21,680 just look it up is it has this burn 1299 00:49:21,680 --> 00:49:24,000 function and we want this burn function 1300 00:49:24,000 --> 00:49:25,319 because this is going to help us 1301 00:49:25,319 --> 00:49:27,119 maintain the peg price we're going to be 1302 00:49:27,119 --> 00:49:29,359 burning a lot of these tokens you'll 1303 00:49:29,359 --> 00:49:31,400 understand that in a bit now same as an 1304 00:49:31,400 --> 00:49:35,680 erc20 so the erc20 burnable contract is 1305 00:49:35,680 --> 00:49:38,720 an erc20 which is why we can import the 1306 00:49:38,720 --> 00:49:41,000 erc20 contract from it as well and the 1307 00:49:41,000 --> 00:49:44,040 erc20 burnable Constructor is an erc20 1308 00:49:44,040 --> 00:49:47,319 which means we have to use the erc20 1309 00:49:47,319 --> 00:49:49,960 Constructor as well which takes a name 1310 00:49:49,960 --> 00:49:51,960 where we're going to call ours 1311 00:49:51,960 --> 00:49:55,960 decentralized stable coin and DSC like 1312 00:49:55,960 --> 00:49:57,880 this and that's it this is going to be 1313 00:49:57,880 --> 00:49:59,359 our whole Constructor we're not going to 1314 00:49:59,359 --> 00:50:01,160 touch it that's all we're going to do 1315 00:50:01,160 --> 00:50:02,880 let me zoom in a little bit I'm going to 1316 00:50:02,880 --> 00:50:04,359 keep zooming in and out hopefully it's 1317 00:50:04,359 --> 00:50:08,480 not too crazy now we want this coin to 1318 00:50:08,480 --> 00:50:12,599 be 100% governed by our engine and our 1319 00:50:12,599 --> 00:50:15,400 engine is going to have all this stuff 1320 00:50:15,400 --> 00:50:18,480 about what collaterals to use how to use 1321 00:50:18,480 --> 00:50:21,160 it what to Peg it to Etc this is purely 1322 00:50:21,160 --> 00:50:23,440 just going to be the token so since we 1323 00:50:23,440 --> 00:50:26,760 want this token to be 100% controlled by 1324 00:50:26,760 --> 00:50:28,400 our logic we're going to make this 1325 00:50:28,400 --> 00:50:31,240 ownable as well which means we're going 1326 00:50:31,240 --> 00:50:33,079 to have only owner modifiers where the 1327 00:50:33,079 --> 00:50:34,920 owner is going to be that immutable 1328 00:50:34,920 --> 00:50:36,960 logic we're going to create so open 1329 00:50:36,960 --> 00:50:38,559 Zeppelin has a package for that too so 1330 00:50:38,559 --> 00:50:41,319 we're going to do import ownable from 1331 00:50:41,319 --> 00:50:44,599 atop Zeppelin contracts 1332 00:50:44,599 --> 00:50:47,880 access ownable doso I'm going to copy 1333 00:50:47,880 --> 00:50:49,880 this so our contract is going to be 1334 00:50:49,880 --> 00:50:52,240 erc20 burnable and it's going to be 1335 00:50:52,240 --> 00:50:55,119 ownable and there's going to be two 1336 00:50:55,119 --> 00:50:57,839 major functions that we want our engine 1337 00:50:57,839 --> 00:51:00,400 to own those functions are going to be 1338 00:51:00,400 --> 00:51:04,480 function burn where it takes in a un 1339 00:51:04,480 --> 00:51:08,319 256 uncore amount public we're going to 1340 00:51:08,319 --> 00:51:11,240 override the burn function in burnable 1341 00:51:11,240 --> 00:51:13,680 this is going to be only owner so that 1342 00:51:13,680 --> 00:51:16,359 only the engine only the logic that we 1343 00:51:16,359 --> 00:51:18,520 give it can mint and burn and we're 1344 00:51:18,520 --> 00:51:22,480 going to say U 256 balance equals 1345 00:51:22,480 --> 00:51:26,160 balance of message. sender and we're 1346 00:51:26,160 --> 00:51:28,680 going to make sure that somebody when 1347 00:51:28,680 --> 00:51:30,319 somebody tries to burn some token they 1348 00:51:30,319 --> 00:51:32,760 at least have that much token so we're 1349 00:51:32,760 --> 00:51:35,720 first all going to say if the amount is 1350 00:51:35,720 --> 00:51:38,520 less than or equal to zero then they 1351 00:51:38,520 --> 00:51:40,480 can't burn right we don't want people to 1352 00:51:40,480 --> 00:51:42,760 try burning zero that's silly so we're 1353 00:51:42,760 --> 00:51:45,920 going to revert with an error put all of 1354 00:51:45,920 --> 00:51:48,040 our errors right up at the top here 1355 00:51:48,040 --> 00:51:50,240 error de centralized stable coin 1356 00:51:50,240 --> 00:51:54,960 underscore underscore must be more than 1357 00:51:54,960 --> 00:51:57,599 zero like this so we're going to revert 1358 00:51:57,599 --> 00:51:59,480 with this error and then we're also 1359 00:51:59,480 --> 00:52:02,520 going to say if the user's balance is 1360 00:52:02,520 --> 00:52:04,040 less than the amount that they're trying 1361 00:52:04,040 --> 00:52:07,440 to burn then we're going to revert with 1362 00:52:07,440 --> 00:52:09,040 another custom error I'm just going to 1363 00:52:09,040 --> 00:52:12,000 copy paste and I'm just going to say 1364 00:52:12,000 --> 00:52:16,720 burn amount exceeds balance boom paste 1365 00:52:16,720 --> 00:52:19,319 that in like this and then finally we're 1366 00:52:19,319 --> 00:52:21,280 going to do this thing called super. bur 1367 00:52:21,280 --> 00:52:23,400 which we haven't talked about yet so 1368 00:52:23,400 --> 00:52:26,920 this super keyword basically says hey 1369 00:52:26,920 --> 00:52:30,040 use the burn function from the parent 1370 00:52:30,040 --> 00:52:32,480 class which in this case is the erc20 1371 00:52:32,480 --> 00:52:34,280 burnable so all this code is going to 1372 00:52:34,280 --> 00:52:36,960 run it's going to hit this line it's 1373 00:52:36,960 --> 00:52:38,720 going to say hey go to the Super class 1374 00:52:38,720 --> 00:52:41,200 and use the burn function there so our 1375 00:52:41,200 --> 00:52:43,000 codee's going to go oh okay well ec20 1376 00:52:43,000 --> 00:52:44,720 burnable that's the super class or the 1377 00:52:44,720 --> 00:52:46,799 parent class ah just use the burn 1378 00:52:46,799 --> 00:52:48,880 function in here which calls the burn 1379 00:52:48,880 --> 00:52:51,680 function here in the ec20 so which does 1380 00:52:51,680 --> 00:52:54,599 all this stuff in here so that's what 1381 00:52:54,599 --> 00:52:57,240 the super keyword does only owner only 1382 00:52:57,240 --> 00:52:59,119 engine's going to add this now we're 1383 00:52:59,119 --> 00:53:01,160 going to do this function mint this is 1384 00:53:01,160 --> 00:53:05,640 going to be an address 2 and a uent 1385 00:53:05,640 --> 00:53:09,640 256 amount this is going to be public 1386 00:53:09,640 --> 00:53:10,920 excuse me this is actually going to be 1387 00:53:10,920 --> 00:53:13,880 external also only owner this one 1388 00:53:13,880 --> 00:53:17,079 probably could be external as well but 1389 00:53:17,079 --> 00:53:19,200 that'll come out in the audit external 1390 00:53:19,200 --> 00:53:22,000 only owner and this is actually going to 1391 00:53:22,000 --> 00:53:25,119 return a Boolean when you do mint you 1392 00:53:25,119 --> 00:53:27,280 want to have a return a Boolean here 1393 00:53:27,280 --> 00:53:28,799 when you do a mint we're going to return 1394 00:53:28,799 --> 00:53:30,920 true if it actually works but we're 1395 00:53:30,920 --> 00:53:34,559 going to say if 2 equals equals address 1396 00:53:34,559 --> 00:53:36,359 zero we're going to do a little 1397 00:53:36,359 --> 00:53:40,079 sanitization of the inputs here then 1398 00:53:40,079 --> 00:53:42,680 we're going to revert revert with a new 1399 00:53:42,680 --> 00:53:46,559 error error to centralized stable coin 1400 00:53:46,559 --> 00:53:49,240 not zero address we're not going to let 1401 00:53:49,240 --> 00:53:51,119 people accidentally mint to the zero 1402 00:53:51,119 --> 00:53:53,200 address because that happens kind of a 1403 00:53:53,200 --> 00:53:56,599 lot and we're going to say if the amount 1404 00:53:56,599 --> 00:53:58,720 is less than or equal to zero then we're 1405 00:53:58,720 --> 00:54:02,559 also going to revert with this more than 1406 00:54:02,559 --> 00:54:06,319 zero error here boom and then finally 1407 00:54:06,319 --> 00:54:08,599 we're going to return true oh and then 1408 00:54:08,599 --> 00:54:10,920 obviously we should run 1409 00:54:10,920 --> 00:54:14,760 mintor 2 Comm amount so we're not 1410 00:54:14,760 --> 00:54:17,079 overriding any functions in here right 1411 00:54:17,079 --> 00:54:19,119 we're just calling the mint function 1412 00:54:19,119 --> 00:54:21,319 over here we had to do super because 1413 00:54:21,319 --> 00:54:23,280 we're overriding the burn function and 1414 00:54:23,280 --> 00:54:25,200 we're saying hey do all this stuff and 1415 00:54:25,200 --> 00:54:27,240 then do the regular burn there is no 1416 00:54:27,240 --> 00:54:29,160 mint function there's an underscore mint 1417 00:54:29,160 --> 00:54:30,960 function that we're going to be calling 1418 00:54:30,960 --> 00:54:34,160 and guess what that's it this contract's 1419 00:54:34,160 --> 00:54:37,040 done we're not doing any more here Forge 1420 00:54:37,040 --> 00:54:39,680 build cool and then we probably want to 1421 00:54:39,680 --> 00:54:41,280 write some tests for this write a deploy 1422 00:54:41,280 --> 00:54:42,520 script but we're going to do all that in 1423 00:54:42,520 --> 00:54:44,880 a little 1424 00:54:46,359 --> 00:54:49,400 bit now what we're going to do is we're 1425 00:54:49,400 --> 00:54:51,319 going to build the engine the engine to 1426 00:54:51,319 --> 00:54:53,920 the car the main components of this 1427 00:54:53,920 --> 00:54:56,000 contract you want to take a break and be 1428 00:54:56,000 --> 00:54:57,960 proud of you yourself right here go for 1429 00:54:57,960 --> 00:54:59,839 it if you want to even pause the video 1430 00:54:59,839 --> 00:55:01,160 start writing some of your own tests 1431 00:55:01,160 --> 00:55:04,000 write your own deploy scripts go for it 1432 00:55:04,000 --> 00:55:06,799 but let's go ahead and start building 1433 00:55:06,799 --> 00:55:09,480 this engine we're going to be building 1434 00:55:09,480 --> 00:55:10,839 this a little bit different than some of 1435 00:55:10,839 --> 00:55:12,680 the other projects we might even be 1436 00:55:12,680 --> 00:55:14,720 testing some of this along the way to 1437 00:55:14,720 --> 00:55:17,040 make sure we get things right so I'm 1438 00:55:17,040 --> 00:55:20,480 going to go ahead create a new file d c 1439 00:55:20,480 --> 00:55:25,280 n g. Soul centralize stable coin engine 1440 00:55:25,280 --> 00:55:27,920 and let's build this in engine to this 1441 00:55:27,920 --> 00:55:31,760 car all right let's go back to let me 1442 00:55:31,760 --> 00:55:34,400 grab I'm just going to copy paste this 1443 00:55:34,400 --> 00:55:37,000 beginning part let's come to the engine 1444 00:55:37,000 --> 00:55:39,920 paste that in we have spdx layout of 1445 00:55:39,920 --> 00:55:41,039 contracts 1446 00:55:41,039 --> 00:55:44,079 prag contract 1447 00:55:44,079 --> 00:55:45,640 DSC 1448 00:55:45,640 --> 00:55:49,359 engine this let's give this a lot of 1449 00:55:49,359 --> 00:55:53,079 natat speec all right title DSC engine I 1450 00:55:53,079 --> 00:55:57,079 don't want that at author a 1451 00:55:57,079 --> 00:56:01,559 or metrick Collins the system is 1452 00:56:01,559 --> 00:56:06,000 designed to be as minimal as possible 1453 00:56:06,000 --> 00:56:10,720 and have the tokens maintain a 1454 00:56:10,720 --> 00:56:16,799 $1 maintain a one token equals equals $1 1455 00:56:16,799 --> 00:56:20,720 Peg toggle the word WP this stable coin 1456 00:56:20,720 --> 00:56:24,599 has the properties Jus collateral dollar 1457 00:56:24,599 --> 00:56:27,520 pegged Alor 1458 00:56:27,520 --> 00:56:33,079 algorithm stable it is similar to D if D 1459 00:56:33,079 --> 00:56:37,200 had no governance no fees and was only 1460 00:56:37,200 --> 00:56:41,160 backed by wrapped eth and wrapped 1461 00:56:41,160 --> 00:56:44,760 Bitcoin at notice this contract is the 1462 00:56:44,760 --> 00:56:47,200 core of the 1463 00:56:47,200 --> 00:56:52,720 DSC system it handles all the logic for 1464 00:56:52,720 --> 00:56:58,880 minting and redeeming DC as well as 1465 00:56:58,880 --> 00:57:02,960 depositing and drawing collateral at 1466 00:57:02,960 --> 00:57:07,680 notice this contract is very Loosely 1467 00:57:07,680 --> 00:57:15,240 based on die on the maker Dow DSS die 1468 00:57:15,240 --> 00:57:17,440 system you might be asking Patrick 1469 00:57:17,440 --> 00:57:20,680 that's a lot of text here yes we want a 1470 00:57:20,680 --> 00:57:23,240 lot of text when people read our code 1471 00:57:23,240 --> 00:57:26,520 our code should be readable remember 1472 00:57:26,520 --> 00:57:29,440 your code is going to be written once 1473 00:57:29,440 --> 00:57:32,680 read hundreds of thousands of times I 1474 00:57:32,680 --> 00:57:36,920 have read the A and maker code so many 1475 00:57:36,920 --> 00:57:39,079 times and so many other people have as 1476 00:57:39,079 --> 00:57:41,160 well you want your code to be very 1477 00:57:41,160 --> 00:57:43,160 verbose so that other people can come 1478 00:57:43,160 --> 00:57:47,119 and understand what's going on so let's 1479 00:57:47,119 --> 00:57:49,480 begin so let's think about what are the 1480 00:57:49,480 --> 00:57:52,400 main functions that our project should 1481 00:57:52,400 --> 00:57:53,880 have what are the main things that we 1482 00:57:53,880 --> 00:57:55,480 should do right before we start even 1483 00:57:55,480 --> 00:57:58,119 start coding anything and often times a 1484 00:57:58,119 --> 00:57:59,880 lot of people will take this step and 1485 00:57:59,880 --> 00:58:02,599 will actually create an interface for 1486 00:58:02,599 --> 00:58:04,480 their code they'll create an interface 1487 00:58:04,480 --> 00:58:05,960 and say hey here's all the functions 1488 00:58:05,960 --> 00:58:07,760 that I want this to do and then they'll 1489 00:58:07,760 --> 00:58:09,960 say hey our contract is that interface 1490 00:58:09,960 --> 00:58:11,559 so that they don't forget any of those 1491 00:58:11,559 --> 00:58:13,720 functions for us I'm just going to go 1492 00:58:13,720 --> 00:58:16,079 ahead and write them out here so I want 1493 00:58:16,079 --> 00:58:19,839 one function to be function deposit 1494 00:58:19,839 --> 00:58:23,520 collateral and mint DSC I want people to 1495 00:58:23,520 --> 00:58:25,599 be able to deposit their die or their 1496 00:58:25,599 --> 00:58:29,200 Bitcoin and mint are DSC token I want 1497 00:58:29,200 --> 00:58:33,880 people to redeem their collateral or DSC 1498 00:58:33,880 --> 00:58:36,160 right when people are done with doing 1499 00:58:36,160 --> 00:58:37,799 whatever they want with the stable coin 1500 00:58:37,799 --> 00:58:40,039 they can turn the stable coin the the 1501 00:58:40,039 --> 00:58:42,280 DSC decentralized stable coin back in 1502 00:58:42,280 --> 00:58:44,559 for whatever collateral they originally 1503 00:58:44,559 --> 00:58:47,520 used I want people to be able to 1504 00:58:47,520 --> 00:58:50,960 function burn their DSC and the reason 1505 00:58:50,960 --> 00:58:53,319 for this is if they're nervous that they 1506 00:58:53,319 --> 00:58:55,079 have too much stable coin and not enough 1507 00:58:55,079 --> 00:58:56,440 collateral and they want just a quick 1508 00:58:56,440 --> 00:58:58,920 way to have more collateral than DSC 1509 00:58:58,920 --> 00:59:01,760 they can quickly burn stuff which is 1510 00:59:01,760 --> 00:59:03,920 another part of the system we should 1511 00:59:03,920 --> 00:59:08,680 even put a point in here our DSC should 1512 00:59:08,680 --> 00:59:14,799 our DSC system should always be over 1513 00:59:14,799 --> 00:59:18,079 collateralized at no point should the 1514 00:59:18,079 --> 00:59:22,520 value of all collateral be less than or 1515 00:59:22,520 --> 00:59:26,880 equal to the value of all the DSC 1516 00:59:26,880 --> 00:59:30,160 or the dollar backed value of all the 1517 00:59:30,160 --> 00:59:33,200 DSC we should always have more 1518 00:59:33,200 --> 00:59:35,440 collateral than DSE in the system at all 1519 00:59:35,440 --> 00:59:38,160 times and we need to code in such a way 1520 00:59:38,160 --> 00:59:41,160 so burn DSC is a function that will make 1521 00:59:41,160 --> 00:59:43,200 more sense in a little bit we should 1522 00:59:43,200 --> 00:59:46,440 have a function called liquidate and 1523 00:59:46,440 --> 00:59:48,200 this is going to be a really important 1524 00:59:48,200 --> 00:59:51,319 function the reason that we're always 1525 00:59:51,319 --> 00:59:53,160 going to have more collateral if the 1526 00:59:53,160 --> 00:59:55,640 value of their collateral drops too much 1527 00:59:55,640 --> 00:59:58,440 let's say let's say I put in $100 worth 1528 00:59:58,440 --> 01:00:03,480 of eth and I minted $50 worth of DSC I 1529 01:00:03,480 --> 01:00:05,799 have more collateral than DSC that's 1530 01:00:05,799 --> 01:00:11,559 good what if the eth price tanks to $40 1531 01:00:11,559 --> 01:00:14,960 40 eth now we are under collateralized 1532 01:00:14,960 --> 01:00:17,520 right now we have less eth than we have 1533 01:00:17,520 --> 01:00:21,559 DSC and keep and this user should get 1534 01:00:21,559 --> 01:00:23,119 what's called liquidated they shouldn't 1535 01:00:23,119 --> 01:00:25,720 be allowed to hold a position in our 1536 01:00:25,720 --> 01:00:28,119 system system anymore so ideally we set 1537 01:00:28,119 --> 01:00:32,000 some threshold that's too low maybe it's 1538 01:00:32,000 --> 01:00:34,920 maybe it's 20% and if you hold $60 worth 1539 01:00:34,920 --> 01:00:37,720 of eth and $50 worth of D DSC you should 1540 01:00:37,720 --> 01:00:39,079 get kicked out of the system because 1541 01:00:39,079 --> 01:00:41,200 you're way too close to being under 1542 01:00:41,200 --> 01:00:43,599 collateralized this liquidate function 1543 01:00:43,599 --> 01:00:45,440 is going to be the function that other 1544 01:00:45,440 --> 01:00:47,599 users can call to remove people's 1545 01:00:47,599 --> 01:00:50,000 positions to save the protocol and we'll 1546 01:00:50,000 --> 01:00:51,520 talk about that a little bit more very 1547 01:00:51,520 --> 01:00:53,760 soon and we're going to want a function 1548 01:00:53,760 --> 01:00:56,280 Health Factor this should be an external 1549 01:00:56,280 --> 01:00:59,200 view function or excuse me get health 1550 01:00:59,200 --> 01:01:01,480 factor and this will allow to see how 1551 01:01:01,480 --> 01:01:04,440 healthy people are so let's go back up 1552 01:01:04,440 --> 01:01:07,599 to this example here so if if the price 1553 01:01:07,599 --> 01:01:11,520 of eth dumps to $40 we're now $10 under 1554 01:01:11,520 --> 01:01:14,400 collateralized right and that's not good 1555 01:01:14,400 --> 01:01:16,160 that's really bad we never want this to 1556 01:01:16,160 --> 01:01:20,119 happen so what we can do is we can set a 1557 01:01:20,119 --> 01:01:24,920 threshold to let's say for this example 1558 01:01:24,920 --> 01:01:27,000 50% or 1559 01:01:27,000 --> 01:01:31,079 150% so if you have $50 in the system 1560 01:01:31,079 --> 01:01:33,680 you need to have at least 75 eth at all 1561 01:01:33,680 --> 01:01:35,799 times this way there's a little bit of a 1562 01:01:35,799 --> 01:01:37,720 buffer that way we can never be under 1563 01:01:37,720 --> 01:01:40,039 collateralized if the price tanks here 1564 01:01:40,039 --> 01:01:41,119 if you go to 1565 01:01:41,119 --> 01:01:43,640 $74 now what we can do is we can 1566 01:01:43,640 --> 01:01:47,960 liquidate and we can say hey if someone 1567 01:01:47,960 --> 01:01:52,000 liquid if someone pays back your borrow 1568 01:01:52,000 --> 01:01:53,359 your minted 1569 01:01:53,359 --> 01:01:58,200 DSC they can have all your collateral or 1570 01:01:58,200 --> 01:02:01,839 a discount so maybe we say hey somebody 1571 01:02:01,839 --> 01:02:06,520 pay back this 50 DSC and you can have 1572 01:02:06,520 --> 01:02:09,319 this $74 worth of eth somebody's going 1573 01:02:09,319 --> 01:02:10,640 to be very incentivized to do this 1574 01:02:10,640 --> 01:02:12,279 because they're going to make 20 1575 01:02:12,279 --> 01:02:15,880 $24 so we'll set some threshold maybe 1576 01:02:15,880 --> 01:02:19,079 150% and we'll we'll say hey anybody who 1577 01:02:19,079 --> 01:02:20,920 liquidates your position if you're under 1578 01:02:20,920 --> 01:02:23,960 the threshold they can have as a reward 1579 01:02:23,960 --> 01:02:26,119 some of your extra collateral this will 1580 01:02:26,119 --> 01:02:28,359 incentivize people to always have extra 1581 01:02:28,359 --> 01:02:29,599 collateral otherwise they're going to 1582 01:02:29,599 --> 01:02:31,760 lose way more money than they borrowed 1583 01:02:31,760 --> 01:02:33,920 that didn't make sense so one more time 1584 01:02:33,920 --> 01:02:37,640 let's do that example so if I mint so if 1585 01:02:37,640 --> 01:02:39,640 I put down $100 worth of eth as 1586 01:02:39,640 --> 01:02:44,160 collateral and I mint $50 worth of DSC 1587 01:02:44,160 --> 01:02:45,400 now I'm going to go off and do whatever 1588 01:02:45,400 --> 01:02:51,559 I want with DSC price of my eth tanks to 1589 01:02:51,559 --> 01:02:55,279 $75 or better yet let's say $74 so other 1590 01:02:55,279 --> 01:02:57,559 user is going to going to see oh my God 1591 01:02:57,559 --> 01:02:59,880 under collateralized and we're going to 1592 01:02:59,880 --> 01:03:01,880 let people liquidate their positions if 1593 01:03:01,880 --> 01:03:03,480 they become under collateralized based 1594 01:03:03,480 --> 01:03:05,599 off the threshold some other users going 1595 01:03:05,599 --> 01:03:06,920 to see that and they're going to say 1596 01:03:06,920 --> 01:03:12,160 okay I'll pay back the $50 of DSC I'll 1597 01:03:12,160 --> 01:03:13,799 pay back the $50 of DC so now this 1598 01:03:13,799 --> 01:03:17,319 person has zero debt and in return get 1599 01:03:17,319 --> 01:03:19,960 all your collateral so now this person 1600 01:03:19,960 --> 01:03:23,559 has $0 worth of eth and this user got 1601 01:03:23,559 --> 01:03:24,240 the 1602 01:03:24,240 --> 01:03:28,319 $74 and all they to do was pay $50 of 1603 01:03:28,319 --> 01:03:32,279 DSC to get $74 of eth so this person is 1604 01:03:32,279 --> 01:03:35,279 now up just made $25 or 1605 01:03:35,279 --> 01:03:38,640 $24 by liquidating you they're 1606 01:03:38,640 --> 01:03:40,880 incentivized to make money and this is 1607 01:03:40,880 --> 01:03:42,720 your punishment for letting your 1608 01:03:42,720 --> 01:03:45,200 collateral get too low so hopefully that 1609 01:03:45,200 --> 01:03:47,279 makes sense if this system of 1610 01:03:47,279 --> 01:03:49,359 liquidations doesn't make sense to you 1611 01:03:49,359 --> 01:03:51,279 you know where to go come to the GitHub 1612 01:03:51,279 --> 01:03:53,520 repos with this course and start joining 1613 01:03:53,520 --> 01:03:55,359 the discussion all right so hopefully 1614 01:03:55,359 --> 01:03:56,960 this made sense if it doesn't use the 1615 01:03:56,960 --> 01:03:59,079 discussions tab of course now these are 1616 01:03:59,079 --> 01:04:01,520 kind of this combination function we're 1617 01:04:01,520 --> 01:04:03,039 probably going to want a function just 1618 01:04:03,039 --> 01:04:06,640 called deposit this deposit collateral 1619 01:04:06,640 --> 01:04:09,000 external we're probably going to want a 1620 01:04:09,000 --> 01:04:10,799 function 1621 01:04:10,799 --> 01:04:14,240 redeem collateral external and then 1622 01:04:14,240 --> 01:04:16,480 we're probably going to want along with 1623 01:04:16,480 --> 01:04:17,839 a burn DSC we're probably going to want 1624 01:04:17,839 --> 01:04:22,000 a function mint DSC external and these 1625 01:04:22,000 --> 01:04:23,799 look like these are probably going to be 1626 01:04:23,799 --> 01:04:26,079 the majority of what a protocol does and 1627 01:04:26,079 --> 01:04:27,480 what a lot of people even do is 1628 01:04:27,480 --> 01:04:29,359 sometimes they'll even write tests right 1629 01:04:29,359 --> 01:04:31,640 now describing what each one of these 1630 01:04:31,640 --> 01:04:33,760 should actually do to the system right 1631 01:04:33,760 --> 01:04:35,119 we're not going to do that but we may 1632 01:04:35,119 --> 01:04:37,640 actually write some tests as we go along 1633 01:04:37,640 --> 01:04:40,599 here and I like to write my deploy 1634 01:04:40,599 --> 01:04:42,319 script kind of early and you'll see me 1635 01:04:42,319 --> 01:04:44,839 do that here that way I can write tests 1636 01:04:44,839 --> 01:04:49,599 using my deploy script so let's go ahead 1637 01:04:51,039 --> 01:04:53,559 though where is the best place to start 1638 01:04:53,559 --> 01:04:56,079 tackling this well to me the the easiest 1639 01:04:56,079 --> 01:04:57,119 place to start is actually with the 1640 01:04:57,119 --> 01:04:59,079 depositing right because that's the 1641 01:04:59,079 --> 01:05:00,480 first thing people are realistically 1642 01:05:00,480 --> 01:05:01,680 going to do with this protocol is 1643 01:05:01,680 --> 01:05:04,000 actually deposit the collateral so I'm 1644 01:05:04,000 --> 01:05:06,480 going to start there so for this deposit 1645 01:05:06,480 --> 01:05:08,640 collateral function what are they going 1646 01:05:08,640 --> 01:05:10,720 to want to do here well we're going to 1647 01:05:10,720 --> 01:05:12,279 need to let them pick what collateral 1648 01:05:12,279 --> 01:05:13,720 they want to deposit so we'll say 1649 01:05:13,720 --> 01:05:17,599 address token collateral address and 1650 01:05:17,599 --> 01:05:21,279 then also the unit 256 amount collateral 1651 01:05:21,279 --> 01:05:22,960 we're going to do a little toggle word 1652 01:05:22,960 --> 01:05:26,079 WP all right cool so deposit coll the 1653 01:05:26,079 --> 01:05:27,559 token collateral address and then the 1654 01:05:27,559 --> 01:05:29,400 amount that they're going to want to do 1655 01:05:29,400 --> 01:05:31,079 so already we can see that there's going 1656 01:05:31,079 --> 01:05:32,400 to be a whole bunch of stuff that we're 1657 01:05:32,400 --> 01:05:35,160 going to want to do here so let's even 1658 01:05:35,160 --> 01:05:37,480 doce a little bit of natspec bit of 1659 01:05:37,480 --> 01:05:40,000 natspec here we'll say at pram we'll 1660 01:05:40,000 --> 01:05:42,520 just explain what the prams are and this 1661 01:05:42,520 --> 01:05:45,400 is where get up co-pilot is really 1662 01:05:45,400 --> 01:05:47,640 helpful because often times it's really 1663 01:05:47,640 --> 01:05:52,480 good with docs so we're going to say Pam 1664 01:05:52,480 --> 01:05:55,079 RM so both of those look good token clat 1665 01:05:55,079 --> 01:05:56,640 all address the amount of token to 1666 01:05:56,640 --> 01:05:58,599 deposit as collateral amount collateral 1667 01:05:58,599 --> 01:05:59,960 the amount of collateral to deposit 1668 01:05:59,960 --> 01:06:02,279 right real simple so we're going to want 1669 01:06:02,279 --> 01:06:03,839 a couple of things here we're going to 1670 01:06:03,839 --> 01:06:05,520 want to sanitize this a little bit so 1671 01:06:05,520 --> 01:06:06,920 the amount collateral we're definitely 1672 01:06:06,920 --> 01:06:09,160 going to want this to be more than zero 1673 01:06:09,160 --> 01:06:10,160 so we're probably going to want a 1674 01:06:10,160 --> 01:06:12,119 modifier called more than zero that we 1675 01:06:12,119 --> 01:06:14,559 can use throughout these functions 1676 01:06:14,559 --> 01:06:15,799 sometimes people might accidentally send 1677 01:06:15,799 --> 01:06:17,240 a zero transaction we want to 1678 01:06:17,240 --> 01:06:20,279 automatically revert those so we scroll 1679 01:06:20,279 --> 01:06:23,119 up to the top here we see our modifiers 1680 01:06:23,119 --> 01:06:24,559 come before our 1681 01:06:24,559 --> 01:06:26,799 functions so we're going to create our 1682 01:06:26,799 --> 01:06:28,799 modifiers here and we're even going to 1683 01:06:28,799 --> 01:06:31,760 add like a little little section A whole 1684 01:06:31,760 --> 01:06:33,240 bunch of these 1685 01:06:33,240 --> 01:06:35,760 here boom 1686 01:06:35,760 --> 01:06:39,880 modifiers like this cool and we'll say 1687 01:06:39,880 --> 01:06:43,279 modifier more than zero and this 1688 01:06:43,279 --> 01:06:45,960 modifier will take a un 1689 01:06:45,960 --> 01:06:51,079 256 amount and we'll just say if mount 1690 01:06:51,079 --> 01:06:53,279 mount equals equals z then we're going 1691 01:06:53,279 --> 01:06:56,760 to go ahead and revert with a new error 1692 01:06:56,760 --> 01:06:58,559 where do errors go let's scroll to the 1693 01:06:58,559 --> 01:07:00,799 top errors 1694 01:07:00,799 --> 01:07:04,319 go right underneath Imports actually 1695 01:07:04,319 --> 01:07:06,200 it's not quite right they're going to go 1696 01:07:06,200 --> 01:07:08,359 right underneath the contract so we're 1697 01:07:08,359 --> 01:07:10,279 going to do we're going to copy this 1698 01:07:10,279 --> 01:07:12,520 whole 1699 01:07:12,599 --> 01:07:15,599 section say errors we're going to go 1700 01:07:15,599 --> 01:07:19,279 here we're going to say error DSC engine 1701 01:07:19,279 --> 01:07:21,920 uncore uncore what do we want to call 1702 01:07:21,920 --> 01:07:25,160 that just needs more than zero needs 1703 01:07:25,160 --> 01:07:28,680 more than zero this and we're going to 1704 01:07:28,680 --> 01:07:31,359 revert with needs more than zero and of 1705 01:07:31,359 --> 01:07:33,200 course add the little underscore here 1706 01:07:33,200 --> 01:07:34,960 cool so now we have a more than zero 1707 01:07:34,960 --> 01:07:38,279 modifier so we can make this external 1708 01:07:38,279 --> 01:07:40,760 more than zero and we'll pass the amount 1709 01:07:40,760 --> 01:07:42,920 collateral and boom okay cool we're 1710 01:07:42,920 --> 01:07:44,559 doing a little sanitization here what 1711 01:07:44,559 --> 01:07:48,240 else should we do you know let's even 1712 01:07:48,240 --> 01:07:51,240 copy this we'll paste this here we'll 1713 01:07:51,240 --> 01:07:52,880 say 1714 01:07:52,880 --> 01:07:54,920 functions and we're going to have a 1715 01:07:54,920 --> 01:07:56,279 Constructor 1716 01:07:56,279 --> 01:07:57,480 so I'm just going to put this here for 1717 01:07:57,480 --> 01:08:00,359 now Constructor should spell Constructor 1718 01:08:00,359 --> 01:08:03,839 right and then we're going to have a 1719 01:08:03,839 --> 01:08:05,920 section after functions like a 1720 01:08:05,920 --> 01:08:08,200 subsection we're going to call it 1721 01:08:08,200 --> 01:08:09,760 external 1722 01:08:09,760 --> 01:08:13,240 functions this external functions cool 1723 01:08:13,240 --> 01:08:16,199 CU we want to go receive and fallback 1724 01:08:16,199 --> 01:08:17,520 we're not going to have those but then 1725 01:08:17,520 --> 01:08:20,560 external and public so external function 1726 01:08:20,560 --> 01:08:24,480 first anyways okay more than zero got 1727 01:08:24,480 --> 01:08:27,880 that and we don't want people to use any 1728 01:08:27,880 --> 01:08:29,560 collateral right we probably only want 1729 01:08:29,560 --> 01:08:31,960 them to use certain collateral that we 1730 01:08:31,960 --> 01:08:33,640 allow so we're going to have to create a 1731 01:08:33,640 --> 01:08:38,679 new modifier called is allowed token so 1732 01:08:38,679 --> 01:08:42,520 we're going to do modifier is allowed 1733 01:08:42,520 --> 01:08:46,199 token this is going to take a address 1734 01:08:46,199 --> 01:08:49,120 token basically we're going to say if 1735 01:08:49,120 --> 01:08:51,759 the token not allowed the token isn't 1736 01:08:51,759 --> 01:08:55,159 allowed then revert right however at the 1737 01:08:55,159 --> 01:08:56,960 moment we don't have like a token allow 1738 01:08:56,960 --> 01:08:59,159 list so let's create that this is 1739 01:08:59,159 --> 01:09:02,520 probably going to be a state mapping so 1740 01:09:02,520 --> 01:09:05,440 let's scroll to the top where do state 1741 01:09:05,440 --> 01:09:07,920 variables go so errors type Declaration 1742 01:09:07,920 --> 01:09:11,239 state variables okay errors great let's 1743 01:09:11,239 --> 01:09:14,040 put it here State 1744 01:09:14,040 --> 01:09:17,640 variables okay and let's do our let's 1745 01:09:17,640 --> 01:09:20,520 create an allowed list of mapping so 1746 01:09:20,520 --> 01:09:21,880 we'll do 1747 01:09:21,880 --> 01:09:27,400 mapping address to BU you know private 1748 01:09:27,400 --> 01:09:32,120 sore token to allow and we could do this 1749 01:09:32,120 --> 01:09:34,319 however I already know that we're going 1750 01:09:34,319 --> 01:09:37,159 to need price feeds so instead what I'm 1751 01:09:37,159 --> 01:09:39,319 going to do is I'm not going to do an 1752 01:09:39,319 --> 01:09:41,640 address to Bull I'm going to do an 1753 01:09:41,640 --> 01:09:44,199 address to address and this is going to 1754 01:09:44,199 --> 01:09:45,799 be our price feed mapping so this is 1755 01:09:45,799 --> 01:09:48,880 going to be sore price feeds and 1756 01:09:48,880 --> 01:09:53,239 normally I do the syntax token to price 1757 01:09:53,239 --> 01:09:55,679 feed right well for this we're just 1758 01:09:55,679 --> 01:09:56,960 going to call this price feeds and we're 1759 01:09:56,960 --> 01:10:00,199 going to use the newer solidity named 1760 01:10:00,199 --> 01:10:01,440 mappings to make this a little bit 1761 01:10:01,440 --> 01:10:03,480 clearer so I'm going to say address 1762 01:10:03,480 --> 01:10:08,400 token maps to address price feed now 1763 01:10:08,400 --> 01:10:09,920 this is S price feed and anybody can 1764 01:10:09,920 --> 01:10:11,640 look up and go ah okay so this is token 1765 01:10:11,640 --> 01:10:14,120 to price feed cool so we're going to 1766 01:10:14,120 --> 01:10:16,760 have this list of price feeds and where 1767 01:10:16,760 --> 01:10:19,520 should we probably set this well we're 1768 01:10:19,520 --> 01:10:21,760 probably going to want to set this up 1769 01:10:21,760 --> 01:10:23,239 right in the Constructor right right 1770 01:10:23,239 --> 01:10:24,800 when we deploy this contract that's when 1771 01:10:24,800 --> 01:10:25,760 we're going to say okay okay these are 1772 01:10:25,760 --> 01:10:27,360 going to be the allowed tokens these are 1773 01:10:27,360 --> 01:10:28,719 the price feeds and that way it's going 1774 01:10:28,719 --> 01:10:30,199 to be like that forever right we're 1775 01:10:30,199 --> 01:10:32,679 never going to be able to change this so 1776 01:10:32,679 --> 01:10:35,239 what we'll do is in our Constructor now 1777 01:10:35,239 --> 01:10:38,840 we'll take in the the allowed tokens and 1778 01:10:38,840 --> 01:10:41,760 their price feeds right because in order 1779 01:10:41,760 --> 01:10:44,679 for this system to work go to like data. 1780 01:10:44,679 --> 01:10:47,199 chain. Link in order for this entire 1781 01:10:47,199 --> 01:10:50,080 system to work if we want to know how 1782 01:10:50,080 --> 01:10:52,600 much value our ethereum that people 1783 01:10:52,600 --> 01:10:54,360 deposit in here is worth we need to have 1784 01:10:54,360 --> 01:10:56,320 the pricing right the only way for us to 1785 01:10:56,320 --> 01:10:58,480 know if we're over collateralized if we 1786 01:10:58,480 --> 01:11:01,199 know the value of our eth in our Bitcoin 1787 01:11:01,199 --> 01:11:02,679 so we're going to use these two price 1788 01:11:02,679 --> 01:11:05,560 feeds to to maintain that because these 1789 01:11:05,560 --> 01:11:06,760 price feeds are going to be on different 1790 01:11:06,760 --> 01:11:08,640 addresses on different chains you 1791 01:11:08,640 --> 01:11:10,320 already know that we got to parameter 1792 01:11:10,320 --> 01:11:13,080 tize it so we'll do an address 1793 01:11:13,080 --> 01:11:14,840 array 1794 01:11:14,840 --> 01:11:18,719 memory token addresses comma address 1795 01:11:18,719 --> 01:11:24,600 array memory price feed addresses as 1796 01:11:24,600 --> 01:11:26,080 input parameters 1797 01:11:26,080 --> 01:11:28,159 and we're going to say token address 1798 01:11:28,159 --> 01:11:29,880 zero maps to price feed zero token 1799 01:11:29,880 --> 01:11:32,640 address one maps to price feed one Etc 1800 01:11:32,640 --> 01:11:35,120 and while we're in here I already know 1801 01:11:35,120 --> 01:11:37,480 that our DSC engine is going to need to 1802 01:11:37,480 --> 01:11:40,520 know about our decentralized table coin 1803 01:11:40,520 --> 01:11:42,040 why because our DC engine is going to 1804 01:11:42,040 --> 01:11:45,120 need to know to call burn and mint so in 1805 01:11:45,120 --> 01:11:47,280 here in the Constructor this is also 1806 01:11:47,280 --> 01:11:49,280 where we're going to pass the address 1807 01:11:49,280 --> 01:11:51,520 DSC address decentralized stable coin 1808 01:11:51,520 --> 01:11:55,239 address and so in here let's do some 1809 01:11:55,239 --> 01:11:58,159 vanity checks on this we'll say if the 1810 01:11:58,159 --> 01:12:02,080 token addresses. length does not equal 1811 01:12:02,080 --> 01:12:04,800 the price feed addresses price feed 1812 01:12:04,800 --> 01:12:09,000 address is price feed address is length 1813 01:12:09,000 --> 01:12:10,480 we have an issue right because if 1814 01:12:10,480 --> 01:12:12,239 there's more tokens or more price feeds 1815 01:12:12,239 --> 01:12:14,560 that means we mess something up so we're 1816 01:12:14,560 --> 01:12:16,560 going to go ahead and revert with a new 1817 01:12:16,560 --> 01:12:19,280 error go to our errors here we'll say 1818 01:12:19,280 --> 01:12:24,320 error DC engine uncore uncore token 1819 01:12:24,320 --> 01:12:30,719 address is and price feed addresses must 1820 01:12:30,719 --> 01:12:34,199 be same length a massive error I know 1821 01:12:34,199 --> 01:12:35,719 but I like being verose like I've told 1822 01:12:35,719 --> 01:12:38,639 you before so if those don't match we're 1823 01:12:38,639 --> 01:12:40,560 going to go ahead and revert then we're 1824 01:12:40,560 --> 01:12:41,960 going to Loop through the token 1825 01:12:41,960 --> 01:12:44,719 addresses and update our mapping that we 1826 01:12:44,719 --> 01:12:48,679 just created here to say okay the token 1827 01:12:48,679 --> 01:12:51,320 address is map to the price feed address 1828 01:12:51,320 --> 01:12:52,719 now in order for us to get a pricing 1829 01:12:52,719 --> 01:12:56,159 we're going to have to use the US D 1830 01:12:56,159 --> 01:12:58,760 price feeds and everything every price 1831 01:12:58,760 --> 01:13:00,000 feed that we're going to have to use is 1832 01:13:00,000 --> 01:13:02,480 going to be the USD back price feed so 1833 01:13:02,480 --> 01:13:06,880 for example it's going to be eth USD BTC 1834 01:13:06,880 --> 01:13:12,280 USD mkr USD Etc okay so we're going to 1835 01:13:12,280 --> 01:13:14,199 Loop through we're going to say four U 1836 01:13:14,199 --> 01:13:19,760 and 256 I equals z i is less than hen 1837 01:13:19,760 --> 01:13:22,239 addresses. length 1838 01:13:22,239 --> 01:13:24,199 i++ so we're going to Loop through this 1839 01:13:24,199 --> 01:13:25,800 token addresses array 1840 01:13:25,800 --> 01:13:29,159 and we're going to say sore price 1841 01:13:29,159 --> 01:13:31,560 feeds of token 1842 01:13:31,560 --> 01:13:35,199 addresses I is going to equal to price 1843 01:13:35,199 --> 01:13:38,560 feed addresses of I so we're going to 1844 01:13:38,560 --> 01:13:40,679 set up this price feed so whatever the 1845 01:13:40,679 --> 01:13:43,320 token so the token of I is going to 1846 01:13:43,320 --> 01:13:45,239 equal the price feed of I and that's how 1847 01:13:45,239 --> 01:13:48,560 we're going to set up what tokens are 1848 01:13:48,560 --> 01:13:50,400 allowed on our platform if they have a 1849 01:13:50,400 --> 01:13:53,159 price feed they're allowed if they don't 1850 01:13:53,159 --> 01:13:54,840 they're not allowed and then I know 1851 01:13:54,840 --> 01:13:55,840 we're we're going to do a lot of stuff 1852 01:13:55,840 --> 01:13:59,120 with our DSC so this is where this is 1853 01:13:59,120 --> 01:14:00,320 definitely going to be an immutable 1854 01:14:00,320 --> 01:14:02,840 variable so we can scroll up make a a 1855 01:14:02,840 --> 01:14:06,159 mutable variable we'll say so we'll say 1856 01:14:06,159 --> 01:14:10,760 decentralized stable coin private iore 1857 01:14:10,760 --> 01:14:12,360 DSC because we're going to make this 1858 01:14:12,360 --> 01:14:14,000 immutable and since we're using 1859 01:14:14,000 --> 01:14:15,360 decentralized stable coin we're going to 1860 01:14:15,360 --> 01:14:17,040 have to go ahead and import 1861 01:14:17,040 --> 01:14:20,600 this so let's go to the top here do 1862 01:14:20,600 --> 01:14:24,000 import decentralized stable coin from 1863 01:14:24,000 --> 01:14:27,199 decentralized stablecoin Soul so now 1864 01:14:27,199 --> 01:14:30,120 decentralized stable coin private idsc 1865 01:14:30,120 --> 01:14:32,000 you can now do oh excuse me this is 1866 01:14:32,000 --> 01:14:35,080 going to be private 1867 01:14:35,520 --> 01:14:37,880 mutable go bit down to the Constructor 1868 01:14:37,880 --> 01:14:39,960 we're just going to say idsc equals 1869 01:14:39,960 --> 01:14:42,800 decentralized stable coin DSC address 1870 01:14:42,800 --> 01:14:45,320 like that again using GitHub copilot if 1871 01:14:45,320 --> 01:14:47,440 you don't have GitHub co-pilot that's 1872 01:14:47,440 --> 01:14:48,960 okay there's a lot of other free AIS 1873 01:14:48,960 --> 01:14:52,320 that you can use as well Okay cool so we 1874 01:14:52,320 --> 01:14:54,800 set up our Constructor we're going back 1875 01:14:54,800 --> 01:14:57,239 down down to deposit collateral whole 1876 01:14:57,239 --> 01:14:58,400 reason we were doing this is we're 1877 01:14:58,400 --> 01:15:00,320 saying okay we should only allow certain 1878 01:15:00,320 --> 01:15:02,880 kinds of collateral on our platform so 1879 01:15:02,880 --> 01:15:05,520 now we can create a new modifier called 1880 01:15:05,520 --> 01:15:08,920 is allowed token where we can just say 1881 01:15:08,920 --> 01:15:13,920 if sore price feeds of token equals 1882 01:15:13,920 --> 01:15:16,800 equals address Zer then we can go ahead 1883 01:15:16,800 --> 01:15:19,800 and revert with a new oh that looks like 1884 01:15:19,800 --> 01:15:22,000 a good one DSC engine token not allowed 1885 01:15:22,000 --> 01:15:23,560 I'm just going to copy that go to the 1886 01:15:23,560 --> 01:15:26,280 top and thanks get up copilot just 1887 01:15:26,280 --> 01:15:28,719 autofilled it in for me okay great and 1888 01:15:28,719 --> 01:15:30,080 then of course we need to do this down 1889 01:15:30,080 --> 01:15:34,199 here Co so is token allowed more than 1890 01:15:34,199 --> 01:15:37,159 zero is allowed excuse me is allowed 1891 01:15:37,159 --> 01:15:39,000 token 1892 01:15:39,000 --> 01:15:40,880 token 1893 01:15:40,880 --> 01:15:44,320 collateral address like this all right 1894 01:15:44,320 --> 01:15:46,800 cool and then additionally I'm going to 1895 01:15:46,800 --> 01:15:51,120 add a non reentrant modifier here as 1896 01:15:51,120 --> 01:15:53,320 well we're going to grab this from open 1897 01:15:53,320 --> 01:15:55,239 Zeppelin whenever we're working with 1898 01:15:55,239 --> 01:15:58,159 external contracts it might be a good 1899 01:15:58,159 --> 01:15:59,920 idea consider making your function 1900 01:15:59,920 --> 01:16:02,040 non-reentrant 1901 01:16:02,040 --> 01:16:05,040 re-entrancy 1902 01:16:05,120 --> 01:16:08,560 and to be honest sometimes I'll just rip 1903 01:16:08,560 --> 01:16:10,639 a non-reentrant modifier even if I'm 1904 01:16:10,639 --> 01:16:12,480 pretty certain it's not vulnerable to 1905 01:16:12,480 --> 01:16:15,080 re-entrancy attack I feel like to be 1906 01:16:15,080 --> 01:16:16,639 honest most functions should be non- 1907 01:16:16,639 --> 01:16:19,600 reentered by default but especially when 1908 01:16:19,600 --> 01:16:21,520 working with external contracts it's a 1909 01:16:21,520 --> 01:16:23,360 good idea to maybe put this modifier 1910 01:16:23,360 --> 01:16:25,760 here now this might go to audit and we 1911 01:16:25,760 --> 01:16:27,719 might say hey well we don't need this 1912 01:16:27,719 --> 01:16:29,639 non-reentrant modifier and maybe we get 1913 01:16:29,639 --> 01:16:31,800 rid of it but maybe we don't the 1914 01:16:31,800 --> 01:16:34,040 trade-off is it's a little bit more gas 1915 01:16:34,040 --> 01:16:36,080 intensive to have this here but it's 1916 01:16:36,080 --> 01:16:38,520 also safer so I'm just going to stick it 1917 01:16:38,520 --> 01:16:40,880 in here even if I'm pretty certain I 1918 01:16:40,880 --> 01:16:42,480 don't need it we're going to get this 1919 01:16:42,480 --> 01:16:46,080 from open Zeppelin has a non- reentrance 1920 01:16:46,080 --> 01:16:47,639 a nonreentrant 1921 01:16:47,639 --> 01:16:50,199 modifier from their re-entrancy guard so 1922 01:16:50,199 --> 01:16:53,920 we'll import actually my GI co-pilot 1923 01:16:53,920 --> 01:16:55,960 automatically had it reeny Guard from 1924 01:16:55,960 --> 01:16:59,360 open Zeppelin contracts security 1925 01:16:59,360 --> 01:17:01,239 reentrancy guard. Soul yep that's 1926 01:17:01,239 --> 01:17:03,639 actually exactly correct and then what 1927 01:17:03,639 --> 01:17:06,360 we can do is scroll down and we see our 1928 01:17:06,360 --> 01:17:09,840 DSC engine is re-entrancy guard by doing 1929 01:17:09,840 --> 01:17:12,320 this we now have access to this 1930 01:17:12,320 --> 01:17:15,600 non-reentrant modifier and now this 1931 01:17:15,600 --> 01:17:18,080 function is non-reentrant which is what 1932 01:17:18,080 --> 01:17:20,400 we want okay cool now we can finally 1933 01:17:20,400 --> 01:17:22,440 start doing some collateral stuff so 1934 01:17:22,440 --> 01:17:23,600 we're going to go ahead and deposit this 1935 01:17:23,600 --> 01:17:25,280 collateral first first thing we're going 1936 01:17:25,280 --> 01:17:27,440 to need to do is a way to track how much 1937 01:17:27,440 --> 01:17:28,760 collateral somebody has actually 1938 01:17:28,760 --> 01:17:31,639 deposited so what's that look like well 1939 01:17:31,639 --> 01:17:33,239 that probably looks like a mapping to me 1940 01:17:33,239 --> 01:17:37,000 so let's go to the top we'll create a 1941 01:17:37,000 --> 01:17:41,840 mapping of address user 1942 01:17:41,840 --> 01:17:48,480 to mapping of an address token to un 256 1943 01:17:48,480 --> 01:17:51,320 amount private 1944 01:17:51,320 --> 01:17:53,520 sore 1945 01:17:53,520 --> 01:17:55,960 colateral the 1946 01:17:55,960 --> 01:17:58,400 posited so this is a mapping to a 1947 01:17:58,400 --> 01:18:00,239 mapping crazy right so we're going to 1948 01:18:00,239 --> 01:18:03,679 map the user's balances to a mapping of 1949 01:18:03,679 --> 01:18:06,280 tokens which is going to get mapped to 1950 01:18:06,280 --> 01:18:08,560 the amount of each token that they have 1951 01:18:08,560 --> 01:18:11,120 all right so let's scroll back down to 1952 01:18:11,120 --> 01:18:13,639 our deposit collateral function so now 1953 01:18:13,639 --> 01:18:16,679 we have a collateral deposited mapping 1954 01:18:16,679 --> 01:18:18,960 so we can do s collateral deposited of 1955 01:18:18,960 --> 01:18:22,880 message. sender of this token collateral 1956 01:18:22,880 --> 01:18:24,360 address now that we know it is an 1957 01:18:24,360 --> 01:18:26,719 allowed collateral address it's going to 1958 01:18:26,719 --> 01:18:30,080 be plus equal to the amount collateral 1959 01:18:30,080 --> 01:18:33,199 right and and I'm actually run into an 1960 01:18:33,199 --> 01:18:36,600 issue here where when I do Forge format 1961 01:18:36,600 --> 01:18:39,440 formats the code look like this but 1962 01:18:39,440 --> 01:18:40,880 right now when I'm saving it's 1963 01:18:40,880 --> 01:18:43,360 reformatting it a different way so what 1964 01:18:43,360 --> 01:18:44,760 I'm going to do is it looks like it's 1965 01:18:44,760 --> 01:18:46,639 using a different formatter that I don't 1966 01:18:46,639 --> 01:18:47,800 like so I'm going to go to the 1967 01:18:47,800 --> 01:18:50,520 extensions solity hard hat hit the 1968 01:18:50,520 --> 01:18:53,320 settings here extension settings and 1969 01:18:53,320 --> 01:18:54,840 we're going to change this from prettier 1970 01:18:54,840 --> 01:18:56,600 to forge because I want to use the forge 1971 01:18:56,600 --> 01:18:59,600 format settings now if I hit save okay 1972 01:18:59,600 --> 01:19:01,960 great it saves and formats the way I 1973 01:19:01,960 --> 01:19:04,199 wanted to okay great so as you can see 1974 01:19:04,199 --> 01:19:05,960 here we're updating State and what 1975 01:19:05,960 --> 01:19:07,920 should we do when we update State we 1976 01:19:07,920 --> 01:19:10,679 should emit an event so we're going to 1977 01:19:10,679 --> 01:19:12,080 emit 1978 01:19:12,080 --> 01:19:15,040 collateral deposited we're going to have 1979 01:19:15,040 --> 01:19:16,880 it be the message. sender who is 1980 01:19:16,880 --> 01:19:20,679 depositing the token collateral address 1981 01:19:20,679 --> 01:19:24,120 and the amount collateral as well which 1982 01:19:24,120 --> 01:19:26,719 means that that we have our first event 1983 01:19:26,719 --> 01:19:29,760 all right so let's go on up here Zoom 1984 01:19:29,760 --> 01:19:31,880 back out scroll all the way up to the 1985 01:19:31,880 --> 01:19:34,280 layout where do events go events go 1986 01:19:34,280 --> 01:19:35,920 after the state variables okay so I'm 1987 01:19:35,920 --> 01:19:38,360 going to copy this it go right after the 1988 01:19:38,360 --> 01:19:40,239 state variables before the modifier so 1989 01:19:40,239 --> 01:19:43,159 we're going to say events we're going to 1990 01:19:43,159 --> 01:19:46,520 go here we're going to say 1991 01:19:46,520 --> 01:19:49,199 event and get up co-pilot automatically 1992 01:19:49,199 --> 01:19:50,760 filled it in for me awesome address 1993 01:19:50,760 --> 01:19:54,280 index user address index token un 256 1994 01:19:54,280 --> 01:19:56,520 amount we don't really need to index 1995 01:19:56,520 --> 01:19:58,400 that maybe we do why not let's just 1996 01:19:58,400 --> 01:20:01,360 index it all right cool and just to know 1997 01:20:01,360 --> 01:20:03,400 a keyboard shortcut that I use a lot is 1998 01:20:03,400 --> 01:20:06,199 control back or control minus which 1999 01:20:06,199 --> 01:20:08,360 allows you to go back to the last spot 2000 01:20:08,360 --> 01:20:10,679 you were in your code if you do control 2001 01:20:10,679 --> 01:20:12,639 shift minus it'll go forward and I use 2002 01:20:12,639 --> 01:20:14,920 this all the time so for example if I'm 2003 01:20:14,920 --> 01:20:17,600 way down here in my code and I hit 2004 01:20:17,600 --> 01:20:19,760 control back I'll just go right back to 2005 01:20:19,760 --> 01:20:21,920 the last place my cruiser was and I use 2006 01:20:21,920 --> 01:20:24,280 this all the time I'm not sure what the 2007 01:20:24,280 --> 01:20:27,239 keyboard shortcut on Windows or linuxes 2008 01:20:27,239 --> 01:20:29,080 but on a Mac it is control back and 2009 01:20:29,080 --> 01:20:31,480 control shift back and I use it all the 2010 01:20:31,480 --> 01:20:34,760 time anyways so we are in deposit 2011 01:20:34,760 --> 01:20:37,159 collateral we have this emit here we 2012 01:20:37,159 --> 01:20:39,800 finally have the event and Okay cool so 2013 01:20:39,800 --> 01:20:43,000 we're updating the collateral internal 2014 01:20:43,000 --> 01:20:45,679 recordkeeping we're admitting a vent now 2015 01:20:45,679 --> 01:20:47,040 of course we should actually get the 2016 01:20:47,040 --> 01:20:49,400 tokens right and you can see here we're 2017 01:20:49,400 --> 01:20:53,719 following CI right so you do a little 2018 01:20:53,719 --> 01:20:58,480 notice follow follows CI checks effects 2019 01:20:58,480 --> 01:21:00,440 interactions so all the checks are 2020 01:21:00,440 --> 01:21:02,040 happening in our modifiers here these 2021 01:21:02,040 --> 01:21:05,000 are all the checks our effects are right 2022 01:21:05,000 --> 01:21:07,480 here and then finally our external 2023 01:21:07,480 --> 01:21:09,040 interactions so this is where we're 2024 01:21:09,040 --> 01:21:11,560 going to do that transfer from and we're 2025 01:21:11,560 --> 01:21:13,600 going to need to wrap our collateral as 2026 01:21:13,600 --> 01:21:16,840 an erc20 so we're going to need to call 2027 01:21:16,840 --> 01:21:18,320 transfer from on it so I'm going to go 2028 01:21:18,320 --> 01:21:20,000 ahead and import 2029 01:21:20,000 --> 01:21:26,000 ic20 from at open zeppin contracts SL 2030 01:21:26,000 --> 01:21:27,600 token 2031 01:21:27,600 --> 01:21:32,159 erc20 erc20 doou contrl minus to go 2032 01:21:32,159 --> 01:21:34,400 right back down to here isn't that nice 2033 01:21:34,400 --> 01:21:37,920 and we can finally do ic20 of the token 2034 01:21:37,920 --> 01:21:42,120 collateral address do transfer from 2035 01:21:42,120 --> 01:21:46,080 message. sender to address 2036 01:21:46,080 --> 01:21:47,760 this 2037 01:21:47,760 --> 01:21:52,080 amount all like this and we'll say so 2038 01:21:52,080 --> 01:21:54,679 this function actually returns a Boolean 2039 01:21:54,679 --> 01:21:57,199 full success and we want to make sure 2040 01:21:57,199 --> 01:21:59,360 that this is actually being true so we 2041 01:21:59,360 --> 01:22:02,840 want to say if not success we'll just 2042 01:22:02,840 --> 01:22:05,679 revert DC 2043 01:22:05,679 --> 01:22:10,600 engine uncore transfer failed like this 2044 01:22:10,600 --> 01:22:13,639 what's this an error you bet it is so 2045 01:22:13,639 --> 01:22:15,560 we're going to put this at the top erir 2046 01:22:15,560 --> 01:22:18,239 DSC engine transfer failed and I'm going 2047 01:22:18,239 --> 01:22:20,080 to hit control minus to go right back 2048 01:22:20,080 --> 01:22:22,120 down to the code that I was working with 2049 01:22:22,120 --> 01:22:24,960 Okay cool so this function look looks 2050 01:22:24,960 --> 01:22:26,880 pretty good to me I'm able to deposit 2051 01:22:26,880 --> 01:22:29,080 collateral in here and update our 2052 01:22:29,080 --> 01:22:31,679 mappings now might be a good time for us 2053 01:22:31,679 --> 01:22:34,080 to start writing some tests for this 2054 01:22:34,080 --> 01:22:35,679 right and we could do something similar 2055 01:22:35,679 --> 01:22:37,840 to what we did before where we just kind 2056 01:22:37,840 --> 01:22:40,199 of threw together a real quick setup in 2057 01:22:40,199 --> 01:22:41,719 our unit tests and then had our 2058 01:22:41,719 --> 01:22:44,320 integration tests be what our deploy 2059 01:22:44,320 --> 01:22:46,800 script actually uses well let's write a 2060 01:22:46,800 --> 01:22:48,360 couple more functions first and then we 2061 01:22:48,360 --> 01:22:51,679 can go ahead and and do all that 2062 01:22:53,000 --> 01:22:55,960 work so we have a way to deposit the 2063 01:22:55,960 --> 01:22:58,920 collateral okay awesome what should we 2064 01:22:58,920 --> 01:23:00,280 do next well the next thing that we 2065 01:23:00,280 --> 01:23:03,679 should do is have a way to Mint our DSC 2066 01:23:03,679 --> 01:23:05,040 token right once they deposit the 2067 01:23:05,040 --> 01:23:06,199 collateral they should be able to Mint 2068 01:23:06,199 --> 01:23:08,920 the DSC token and then the combination 2069 01:23:08,920 --> 01:23:10,719 of those two will be this function 2070 01:23:10,719 --> 01:23:13,480 deposit collateral and mint DSC so let's 2071 01:23:13,480 --> 01:23:14,960 go ahead and actually create this mint 2072 01:23:14,960 --> 01:23:17,120 DSC function because now that they have 2073 01:23:17,120 --> 01:23:18,480 some collateral we should be able to 2074 01:23:18,480 --> 01:23:20,280 Mint some DSC and now this is actually 2075 01:23:20,280 --> 01:23:22,400 going to be a surprisingly involved 2076 01:23:22,400 --> 01:23:24,840 method right cuz in order to Mint DS C 2077 01:23:24,840 --> 01:23:29,360 we need to check if the collateral value 2078 01:23:29,360 --> 01:23:33,440 is greater than the DSC amount and this 2079 01:23:33,440 --> 01:23:34,960 is obviously going to involve a number 2080 01:23:34,960 --> 01:23:36,920 of things right it's going to involve 2081 01:23:36,920 --> 01:23:39,199 price feeds we're going to be checking 2082 01:23:39,199 --> 01:23:41,280 values we're be checking a whole lot of 2083 01:23:41,280 --> 01:23:42,760 stuff okay so this is actually going to 2084 01:23:42,760 --> 01:23:44,920 be a little bit more of an in-depth 2085 01:23:44,920 --> 01:23:47,159 function here so let's create this mint 2086 01:23:47,159 --> 01:23:49,920 DSC function and we'll have this get 2087 01:23:49,920 --> 01:23:54,760 passed in a un 256 amount DSC to Mint 2088 01:23:54,760 --> 01:23:56,280 people can pick how much DSA they want 2089 01:23:56,280 --> 01:23:59,000 to Mint so let's say for example someone 2090 01:23:59,000 --> 01:24:01,840 deposits $200 worth of eth maybe they 2091 01:24:01,840 --> 01:24:05,040 only want to Mint $20 worth of DSC right 2092 01:24:05,040 --> 01:24:06,320 so they can pick how much they want to 2093 01:24:06,320 --> 01:24:09,880 Mint here so amount to Mint here and 2094 01:24:09,880 --> 01:24:11,800 let's add some checks here we should 2095 01:24:11,800 --> 01:24:15,239 have the mint more than zero Mount DSC 2096 01:24:15,239 --> 01:24:18,639 to Mint this what else we probably want 2097 01:24:18,639 --> 01:24:21,000 this to be non-reentrant even though we 2098 01:24:21,000 --> 01:24:22,320 probably don't need this to be non 2099 01:24:22,320 --> 01:24:25,960 reentrant because it's our C token but 2100 01:24:25,960 --> 01:24:28,000 let's put it in here anyways it's 2101 01:24:28,000 --> 01:24:29,960 probably all we need for now we probably 2102 01:24:29,960 --> 01:24:32,320 want to do 2103 01:24:32,320 --> 01:24:35,080 some looks like get up co-pilot even 2104 01:24:35,080 --> 01:24:38,120 gave me some some follows CI yes oh 2105 01:24:38,120 --> 01:24:42,400 that's wrong amount DEC to Mint amount D 2106 01:24:42,400 --> 01:24:45,880 to Mint the amount of 2107 01:24:45,880 --> 01:24:50,119 decentralized stable coin to Mint at 2108 01:24:50,119 --> 01:24:55,760 notice they must have more collateral 2109 01:24:55,760 --> 01:25:01,159 value than the minimum threshold we're 2110 01:25:01,159 --> 01:25:02,239 going to figure out what that means in a 2111 01:25:02,239 --> 01:25:04,960 second so mint DSC we'll need to keep 2112 01:25:04,960 --> 01:25:07,320 track of how much everybody has minted 2113 01:25:07,320 --> 01:25:09,000 right so whenever they're minting DSC 2114 01:25:09,000 --> 01:25:10,719 they're in a way they're minting debt 2115 01:25:10,719 --> 01:25:11,560 right so we're going to need to keep 2116 01:25:11,560 --> 01:25:14,040 track of that where can we do that well 2117 01:25:14,040 --> 01:25:16,760 we can do that as a state variable so 2118 01:25:16,760 --> 01:25:22,920 we'll do mapping of an address user 2119 01:25:22,920 --> 01:25:26,400 mapped to their U 256 which is going to 2120 01:25:26,400 --> 01:25:29,719 be an address user map to the u u 2121 01:25:29,719 --> 01:25:33,960 256 Mount DSC minted it's going to be 2122 01:25:33,960 --> 01:25:35,320 private 2123 01:25:35,320 --> 01:25:40,320 sore DSC minted this now I can go back 2124 01:25:40,320 --> 01:25:44,840 down here this new mapping SDC minted 2125 01:25:44,840 --> 01:25:49,199 message. sender plus equals amount DSC 2126 01:25:49,199 --> 01:25:51,400 to Mint so we're going to keep track of 2127 01:25:51,400 --> 01:25:53,159 all that they minted again this is going 2128 01:25:53,159 --> 01:25:55,159 to follow CI 2129 01:25:55,159 --> 01:25:56,880 so now we want to do a little check here 2130 01:25:56,880 --> 01:26:01,360 if they minted too much for example like 2131 01:26:01,360 --> 01:26:05,520 they minted $150 or $150 worth of DSC 2132 01:26:05,520 --> 01:26:08,480 but they only have $100 worth of eth 2133 01:26:08,480 --> 01:26:09,880 that's going to be way too much we 2134 01:26:09,880 --> 01:26:13,520 should 100% revert if that happened so 2135 01:26:13,520 --> 01:26:15,440 I'm actually going to make a function 2136 01:26:15,440 --> 01:26:17,199 going to make an internal function 2137 01:26:17,199 --> 01:26:23,960 called revert if Health factor is broken 2138 01:26:23,960 --> 01:26:27,760 with the message. sender being sent so 2139 01:26:27,760 --> 01:26:29,320 we're going to create this new internal 2140 01:26:29,320 --> 01:26:31,320 function so up here we have external 2141 01:26:31,320 --> 01:26:33,800 functions we're going scroll down here 2142 01:26:33,800 --> 01:26:35,080 we're going to make 2143 01:26:35,080 --> 01:26:38,560 this private and internal 2144 01:26:38,560 --> 01:26:42,520 functions this this and we're only going 2145 01:26:42,520 --> 01:26:44,480 to be able to call this if we're only 2146 01:26:44,480 --> 01:26:46,239 going to be able to call this internally 2147 01:26:46,239 --> 01:26:47,639 right so we're going to create this 2148 01:26:47,639 --> 01:26:49,480 function function revert if Health 2149 01:26:49,480 --> 01:26:52,639 factor is broken address User it's going 2150 01:26:52,639 --> 01:26:55,280 to be an internal View 2151 01:26:55,280 --> 01:26:58,199 function and we're going to basically 2152 01:26:58,199 --> 01:27:02,040 one check Health factor which is 2153 01:27:02,040 --> 01:27:05,760 basically do they have enough collateral 2154 01:27:05,760 --> 01:27:09,480 right and then revert if they don't have 2155 01:27:09,480 --> 01:27:12,440 a good health Factor so this health 2156 01:27:12,440 --> 01:27:13,679 factor is actually a term that I 2157 01:27:13,679 --> 01:27:16,600 borrowed from the a documentation reach 2158 01:27:16,600 --> 01:27:18,239 wallet these risk parameters enable the 2159 01:27:18,239 --> 01:27:20,320 calculation of Health factor and you can 2160 01:27:20,320 --> 01:27:22,000 see a little bit of an image here that 2161 01:27:22,000 --> 01:27:24,480 shows it so additionally we're going to 2162 01:27:24,480 --> 01:27:26,719 need to make a function that checks the 2163 01:27:26,719 --> 01:27:28,440 health Factor actually these are going 2164 01:27:28,440 --> 01:27:30,880 to be private and internal view 2165 01:27:30,880 --> 01:27:33,159 functions so first we need to make a 2166 01:27:33,159 --> 01:27:35,159 function that can get the health Factor 2167 01:27:35,159 --> 01:27:36,360 so we're going to make another function 2168 01:27:36,360 --> 01:27:40,199 here called function underscore Health 2169 01:27:40,199 --> 01:27:42,600 Factor we're going to take an address 2170 01:27:42,600 --> 01:27:44,960 user and you see we have this leading 2171 01:27:44,960 --> 01:27:47,239 underscore to tell us as developers that 2172 01:27:47,239 --> 01:27:49,840 this is an internal function so let's 2173 01:27:49,840 --> 01:27:52,800 actually give this in underscore as well 2174 01:27:52,800 --> 01:27:54,719 function 2175 01:27:54,719 --> 01:27:59,199 oops Factor this will be a private view 2176 01:27:59,199 --> 01:28:02,320 going to return a u into 256 so what 2177 01:28:02,320 --> 01:28:04,560 this health Factor thing is going to do 2178 01:28:04,560 --> 01:28:07,239 give it a little bit of Nat speec yeah 2179 01:28:07,239 --> 01:28:11,760 we can say this is going to returns how 2180 01:28:11,760 --> 01:28:17,159 close to liquidation a user is say if a 2181 01:28:17,159 --> 01:28:23,440 user goes below one then they can get 2182 01:28:23,440 --> 01:28:25,560 liquidated so we're going to figure out 2183 01:28:25,560 --> 01:28:28,280 what the ratio of collateral to DSC 2184 01:28:28,280 --> 01:28:30,280 minted that users can have with this 2185 01:28:30,280 --> 01:28:32,119 health Factor function we're going to be 2186 01:28:32,119 --> 01:28:33,840 building a lot of stuff here so in order 2187 01:28:33,840 --> 01:28:35,719 to figure this out what do we need well 2188 01:28:35,719 --> 01:28:38,480 we're going to need to get both their 2189 01:28:38,480 --> 01:28:41,920 total DSC minted right and we're going 2190 01:28:41,920 --> 01:28:44,960 to need to get their total 2191 01:28:44,960 --> 01:28:47,520 collateral value right not just the 2192 01:28:47,520 --> 01:28:48,719 total collateral we're going to need to 2193 01:28:48,719 --> 01:28:51,040 get the total collateral value make sure 2194 01:28:51,040 --> 01:28:53,360 the value is greater than the total DSC 2195 01:28:53,360 --> 01:28:54,560 minted 2196 01:28:54,560 --> 01:28:56,080 so we're going to have to make another 2197 01:28:56,080 --> 01:28:57,719 function we're going to need to get the 2198 01:28:57,719 --> 01:29:04,159 UN 256 total DSC minted and the UN 256 2199 01:29:04,159 --> 01:29:07,520 collateral value in USD we're going to 2200 01:29:07,520 --> 01:29:11,080 create a new function called get count 2201 01:29:11,080 --> 01:29:14,440 information from that user so we're 2202 01:29:14,440 --> 01:29:17,480 going to do another one boom function 2203 01:29:17,480 --> 01:29:21,760 uncore get account information like this 2204 01:29:21,760 --> 01:29:24,320 we're going take an address user this 2205 01:29:24,320 --> 01:29:27,159 will be a private View and we're going 2206 01:29:27,159 --> 01:29:28,560 to 2207 01:29:28,560 --> 01:29:32,920 return two things U and 256 total DSC 2208 01:29:32,920 --> 01:29:34,920 minted and U into 2209 01:29:34,920 --> 01:29:39,520 256 collateral value in USD two 2210 01:29:39,520 --> 01:29:41,800 functions here to get the total USD 2211 01:29:41,800 --> 01:29:45,360 that's easy right we just do total DSC 2212 01:29:45,360 --> 01:29:49,480 minted equals this array this excuse me 2213 01:29:49,480 --> 01:29:51,000 this mapping that we just created right 2214 01:29:51,000 --> 01:29:52,719 we're keeping track of this exactly so 2215 01:29:52,719 --> 01:29:54,639 all we got to do is this to get the 2216 01:29:54,639 --> 01:29:56,719 total collateral value in USD we're 2217 01:29:56,719 --> 01:29:59,040 going to need to do some more math so 2218 01:29:59,040 --> 01:30:02,920 I'm going to say collateral value in USD 2219 01:30:02,920 --> 01:30:05,960 equals get account 2220 01:30:05,960 --> 01:30:08,719 collateral value and we're going to pass 2221 01:30:08,719 --> 01:30:11,440 some user so this is going to be a 2222 01:30:11,440 --> 01:30:13,159 different function and we're going to 2223 01:30:13,159 --> 01:30:15,040 make this a public function so that 2224 01:30:15,040 --> 01:30:16,920 anybody can call this function 2225 01:30:16,920 --> 01:30:18,840 themselves so we're actually going to 2226 01:30:18,840 --> 01:30:21,600 copy this private and view internal 2227 01:30:21,600 --> 01:30:23,800 functions public internal View and then 2228 01:30:23,800 --> 01:30:25,920 we're we're going to do View and pure 2229 01:30:25,920 --> 01:30:27,280 public and external functions all the 2230 01:30:27,280 --> 01:30:30,159 way at the bottom we'll say private 2231 01:30:30,159 --> 01:30:34,040 instead of we'll say public and external 2232 01:30:34,040 --> 01:30:37,639 view functions we'll do function get 2233 01:30:37,639 --> 01:30:41,840 account bilateral value address of the 2234 01:30:41,840 --> 01:30:44,239 user this will be a public view so that 2235 01:30:44,239 --> 01:30:48,920 anybody can call it returns a uint 256 2236 01:30:48,920 --> 01:30:50,239 stay with me I know we're kind of going 2237 01:30:50,239 --> 01:30:52,320 down this tree stay with me here we're 2238 01:30:52,320 --> 01:30:54,760 about to go even further now to get the 2239 01:30:54,760 --> 01:30:56,560 actual value what do we need to do well 2240 01:30:56,560 --> 01:31:00,560 we need to Loop through each collateral 2241 01:31:00,560 --> 01:31:03,760 token get the amount they have 2242 01:31:03,760 --> 01:31:09,679 deposited and map it to price to get the 2243 01:31:09,679 --> 01:31:12,880 USD value so we're going to need to Loop 2244 01:31:12,880 --> 01:31:15,800 through all the collateral tokens uhoh 2245 01:31:15,800 --> 01:31:17,719 do we have a way we can do that let's 2246 01:31:17,719 --> 01:31:20,320 scroll to the top well we have a mapping 2247 01:31:20,320 --> 01:31:22,280 of price feeds but we don't have a way 2248 01:31:22,280 --> 01:31:25,600 to Loop through them so what we can do 2249 01:31:25,600 --> 01:31:27,639 is we could just have like you know 2250 01:31:27,639 --> 01:31:32,320 address we and then address address 2251 01:31:32,320 --> 01:31:34,679 wrapped BTC we could just have two 2252 01:31:34,679 --> 01:31:36,080 tokens like this we would have to Loop 2253 01:31:36,080 --> 01:31:37,760 through anything we're going to make 2254 01:31:37,760 --> 01:31:40,080 this system a little bit agnostic so 2255 01:31:40,080 --> 01:31:41,480 that you could deploy this with any 2256 01:31:41,480 --> 01:31:44,119 amount of stable coins any amount of 2257 01:31:44,119 --> 01:31:46,719 collaterals so we're going to make a new 2258 01:31:46,719 --> 01:31:48,560 state variable it's going to be an 2259 01:31:48,560 --> 01:31:52,960 address array private sore collateral 2260 01:31:52,960 --> 01:31:55,440 tokens and what we're going to do is 2261 01:31:55,440 --> 01:31:58,000 right in our Constructor when we update 2262 01:31:58,000 --> 01:32:00,840 our price feeds with the token and the 2263 01:32:00,840 --> 01:32:03,400 price feed we're also going to add the 2264 01:32:03,400 --> 01:32:08,080 tokens addresses of I to this array now 2265 01:32:08,080 --> 01:32:09,639 we have this array of collateral tokens 2266 01:32:09,639 --> 01:32:11,320 that we can Loop through and that way we 2267 01:32:11,320 --> 01:32:13,440 can calculate how much value people have 2268 01:32:13,440 --> 01:32:17,600 based off of all of the tokens so get 2269 01:32:17,600 --> 01:32:19,639 account collateral value we're going to 2270 01:32:19,639 --> 01:32:24,800 say four U and 256 I equals z I is less 2271 01:32:24,800 --> 01:32:29,960 than S collateral tokens. length i++ 2272 01:32:29,960 --> 01:32:34,480 address token equals s collateral tokens 2273 01:32:34,480 --> 01:32:38,360 of I and we'll get the amount so this is 2274 01:32:38,360 --> 01:32:39,920 the token that we're working with we'll 2275 01:32:39,920 --> 01:32:44,040 get the amount by un 256 amount that is 2276 01:32:44,040 --> 01:32:46,719 deposited by this user by sore 2277 01:32:46,719 --> 01:32:48,360 collateral 2278 01:32:48,360 --> 01:32:53,360 deposited user token like this then 2279 01:32:53,360 --> 01:32:54,440 total 2280 01:32:54,440 --> 01:32:59,560 collateral value in USD plus equals 2281 01:32:59,560 --> 01:33:01,520 uh-oh now that we have the amount we're 2282 01:33:01,520 --> 01:33:03,600 going to need to get the USD value of 2283 01:33:03,600 --> 01:33:05,520 this and this is probably a function 2284 01:33:05,520 --> 01:33:07,679 that we're going to want to have public 2285 01:33:07,679 --> 01:33:09,560 so that other people can use this as 2286 01:33:09,560 --> 01:33:14,800 well so create a function get USD value 2287 01:33:14,800 --> 01:33:16,960 where somebody passes in an address 2288 01:33:16,960 --> 01:33:20,800 token U and 256 amount this will be a 2289 01:33:20,800 --> 01:33:25,840 public view returns U 56 and this is 2290 01:33:25,840 --> 01:33:27,800 where we're going to do some price feed 2291 01:33:27,800 --> 01:33:30,960 stuff some stuff that's very familiar so 2292 01:33:30,960 --> 01:33:33,000 we're going to need to get the price 2293 01:33:33,000 --> 01:33:35,480 feed for the token and then times the 2294 01:33:35,480 --> 01:33:38,239 amount by the price so we're going have 2295 01:33:38,239 --> 01:33:40,199 to work with aggregator V3 interface 2296 01:33:40,199 --> 01:33:42,520 again that chain link data feeds so 2297 01:33:42,520 --> 01:33:44,360 import and I know we've worked with this 2298 01:33:44,360 --> 01:33:45,960 before 2299 01:33:45,960 --> 01:33:48,040 aggregator V3 2300 01:33:48,040 --> 01:33:50,199 interface 2301 01:33:50,199 --> 01:33:52,679 from it looks like get up co-pilots got 2302 01:33:52,679 --> 01:33:54,719 my back at chain link contracts blah 2303 01:33:54,719 --> 01:33:56,960 blah blah this means we're going to have 2304 01:33:56,960 --> 01:33:59,480 to install that remember we can install 2305 01:33:59,480 --> 01:34:03,719 the smaller one with Forge install Smart 2306 01:34:03,719 --> 01:34:07,400 contract kit SL chainlink brownie 2307 01:34:07,400 --> 01:34:10,400 contracts at 2308 01:34:10,400 --> 01:34:13,320 0.6.1 D- no 2309 01:34:13,320 --> 01:34:18,360 dashit this chain Le brownie 2310 01:34:19,840 --> 01:34:21,639 contracts awesome then we're going to 2311 01:34:21,639 --> 01:34:24,239 have to go to our founder. 2312 01:34:24,239 --> 01:34:26,520 add some remappings in here so we're 2313 01:34:26,520 --> 01:34:28,920 going to put a little comma we're going 2314 01:34:28,920 --> 01:34:34,360 to say at chain link SL contracts equals 2315 01:34:34,360 --> 01:34:38,880 lib slash chain link brownie contracts 2316 01:34:38,880 --> 01:34:42,000 SL contracts like that so now we have 2317 01:34:42,000 --> 01:34:43,960 the aggregator V3 interface scroll down 2318 01:34:43,960 --> 01:34:46,880 to the bottom get USD value I know we've 2319 01:34:46,880 --> 01:34:49,639 done this before I know this is familiar 2320 01:34:49,639 --> 01:34:51,440 to a lot of you but we're going to say 2321 01:34:51,440 --> 01:34:54,600 aggregator V3 interface price Fe 2322 01:34:54,600 --> 01:34:57,800 equals aggregator V3 interface of sore 2323 01:34:57,800 --> 01:35:00,760 price feeds of the token so we're going 2324 01:35:00,760 --> 01:35:03,040 to get the price feed of the token we're 2325 01:35:03,040 --> 01:35:05,080 looking to get the value of now we have 2326 01:35:05,080 --> 01:35:07,239 the price feed here we're going to do 2327 01:35:07,239 --> 01:35:10,880 this WOW get a co-pilot spot on we're 2328 01:35:10,880 --> 01:35:12,440 going to get the price by calling Price 2329 01:35:12,440 --> 01:35:15,400 feed. latest round data and this is 2330 01:35:15,400 --> 01:35:16,360 where we're going to do a little bit of 2331 01:35:16,360 --> 01:35:18,880 math right let's say 1 eth equals 2332 01:35:18,880 --> 01:35:23,840 $1,000 The Returned value from CL will 2333 01:35:23,840 --> 01:35:24,840 be 2334 01:35:24,840 --> 01:35:29,600 1000 Time 1 E8 how do I know this well 2335 01:35:29,600 --> 01:35:32,400 if I go to docs. chain. link go to data 2336 01:35:32,400 --> 01:35:36,760 feeds I can scroll down to price feed 2337 01:35:36,760 --> 01:35:39,760 addresses and I can see if I do show 2338 01:35:39,760 --> 01:35:42,239 more details for 2339 01:35:42,239 --> 01:35:46,199 ethusd eth-usd it has eight decimal 2340 01:35:46,199 --> 01:35:49,080 places and I know Bitcoin USD also has 2341 01:35:49,080 --> 01:35:50,960 eight decimal places we could add some 2342 01:35:50,960 --> 01:35:52,719 code in here so that we make sure we're 2343 01:35:52,719 --> 01:35:54,320 getting the right decimals but I'm just 2344 01:35:54,320 --> 01:35:56,480 going to go with eight for now and now 2345 01:35:56,480 --> 01:35:59,480 we can do a little bit of math here so 2346 01:35:59,480 --> 01:36:00,880 we're going to need to do the price 2347 01:36:00,880 --> 01:36:03,960 times the amount right but is this all 2348 01:36:03,960 --> 01:36:07,239 we need to do turn price times amount no 2349 01:36:07,239 --> 01:36:08,639 because the number is going to be way 2350 01:36:08,639 --> 01:36:10,400 too big right let's say the price is a 2351 01:36:10,400 --> 01:36:14,440 th000 times 1 E8 and now we're 2352 01:36:14,440 --> 01:36:16,719 multiplying that by let's say the the 2353 01:36:16,719 --> 01:36:21,320 amount is 1,000 times 1 E18 because it's 2354 01:36:21,320 --> 01:36:23,119 going to be in way this number is going 2355 01:36:23,119 --> 01:36:26,040 to be absolutely massive right so first 2356 01:36:26,040 --> 01:36:28,679 we need to multiply this a th by a 2357 01:36:28,679 --> 01:36:31,080 number to get this number to match this 2358 01:36:31,080 --> 01:36:33,040 one so these need to be the same units 2359 01:36:33,040 --> 01:36:35,719 of precision so that's going to be we're 2360 01:36:35,719 --> 01:36:37,880 going to have to multiply this first by 2361 01:36:37,880 --> 01:36:40,920 1 E10 and because I don't like mic 2362 01:36:40,920 --> 01:36:43,280 numbers we're going to scroll to the top 2363 01:36:43,280 --> 01:36:45,560 create a new state variable right on 2364 01:36:45,560 --> 01:36:50,760 here we're going to do a u 256 private 2365 01:36:50,760 --> 01:36:54,400 constant additional feed 2366 01:36:54,400 --> 01:36:58,400 Precision equals 1 E10 like this and now 2367 01:36:58,400 --> 01:37:00,800 it's not a magic number so instead of 2368 01:37:00,800 --> 01:37:03,400 doing this now we're going to say okay 2369 01:37:03,400 --> 01:37:05,159 price and we're going to wrap it as a u 2370 01:37:05,159 --> 01:37:08,880 into 256 so that everything's down 256 2371 01:37:08,880 --> 01:37:10,840 we say the price first needs to be 2372 01:37:10,840 --> 01:37:12,639 multiplied by the additional feed 2373 01:37:12,639 --> 01:37:15,159 Precision so that now both of these are 2374 01:37:15,159 --> 01:37:17,920 U and 256s and they both have 1 2375 01:37:17,920 --> 01:37:20,320 E18 but then we're going to have to 2376 01:37:20,320 --> 01:37:23,800 divide all of these by 1 E18 as well so 2377 01:37:23,800 --> 01:37:25,719 this number doesn't look super wonky so 2378 01:37:25,719 --> 01:37:27,119 we're going to have to wrap this whole 2379 01:37:27,119 --> 01:37:32,440 thing by 1 E18 and as I hate floating 2380 01:37:32,440 --> 01:37:33,800 magic numbers like this so we're going 2381 01:37:33,800 --> 01:37:36,080 to scroll back to the top copy paste 2382 01:37:36,080 --> 01:37:38,560 this this is now just going to be called 2383 01:37:38,560 --> 01:37:43,239 Precision 1 E18 grab Precision divide by 2384 01:37:43,239 --> 01:37:46,800 Precision so u56 price times additional 2385 01:37:46,800 --> 01:37:49,119 feed Precision Time amount divided by 2386 01:37:49,119 --> 01:37:50,679 precision and we should be good to go 2387 01:37:50,679 --> 01:37:53,560 here this is where my brain immediately 2388 01:37:53,560 --> 01:37:55,800 goes okay definitely need to write some 2389 01:37:55,800 --> 01:37:57,960 tests for this so once we finished going 2390 01:37:57,960 --> 01:37:59,320 through this we're definitely going to 2391 01:37:59,320 --> 01:38:00,599 read we're definitely going to write 2392 01:38:00,599 --> 01:38:03,320 some tests at least for get USD value 2393 01:38:03,320 --> 01:38:07,320 here so let's go back up through our 2394 01:38:07,320 --> 01:38:09,040 massive tree of functions that we just 2395 01:38:09,040 --> 01:38:11,360 created that are not complete so let's 2396 01:38:11,360 --> 01:38:14,480 go back to get account collateral value 2397 01:38:14,480 --> 01:38:16,520 and this is going to be the total us the 2398 01:38:16,520 --> 01:38:18,280 total collateral value in USD it's going 2399 01:38:18,280 --> 01:38:22,119 to be get the USD value of the current 2400 01:38:22,119 --> 01:38:24,040 token we're on 2401 01:38:24,040 --> 01:38:26,080 times the amount that we're working with 2402 01:38:26,080 --> 01:38:29,560 toal Cal value in USD actually let's put 2403 01:38:29,560 --> 01:38:32,360 that there boom and that's it right so 2404 01:38:32,360 --> 01:38:34,599 we just Loop through all the tokens in 2405 01:38:34,599 --> 01:38:37,520 the token array and we just add up the 2406 01:38:37,520 --> 01:38:39,000 value and USD of each one of these 2407 01:38:39,000 --> 01:38:41,000 tokens and I know we don't need a return 2408 01:38:41,000 --> 01:38:42,239 here but I'm going to add a return here 2409 01:38:42,239 --> 01:38:45,920 anyways return this so cool so now we 2410 01:38:45,920 --> 01:38:47,280 have a way to get the collateral value 2411 01:38:47,280 --> 01:38:48,119 in 2412 01:38:48,119 --> 01:38:51,560 USD we needed that way up here oops 2413 01:38:51,560 --> 01:38:55,880 let's do collateral value in 2414 01:38:55,920 --> 01:39:00,119 USD get clonal value oops let's do this 2415 01:39:00,119 --> 01:39:01,679 and awesome so now our get account 2416 01:39:01,679 --> 01:39:04,599 information is going to return the total 2417 01:39:04,599 --> 01:39:07,560 USD minted the total DSC minted and then 2418 01:39:07,560 --> 01:39:09,560 the total value of all the collateral 2419 01:39:09,560 --> 01:39:12,239 here okay 2420 01:39:13,599 --> 01:39:16,560 great and we can scroll up again we have 2421 01:39:16,560 --> 01:39:18,360 this revert if hell factor is broken 2422 01:39:18,360 --> 01:39:21,080 which is still busted because this 2423 01:39:21,080 --> 01:39:23,400 function doesn't do anything but now we 2424 01:39:23,400 --> 01:39:25,119 can actually have it do something 2425 01:39:25,119 --> 01:39:27,480 because we have this health Factor here 2426 01:39:27,480 --> 01:39:29,360 we're going to update our health Factor 2427 01:39:29,360 --> 01:39:31,599 function because now we have the two of 2428 01:39:31,599 --> 01:39:35,400 these what we can do is we can just get 2429 01:39:35,400 --> 01:39:39,159 the ratio of these two so we could say 2430 01:39:39,159 --> 01:39:42,679 flatal value in USD divid by total USD 2431 01:39:42,679 --> 01:39:44,760 minted this is what we're going to 2432 01:39:44,760 --> 01:39:48,159 return for a health Factor right well 2433 01:39:48,159 --> 01:39:51,800 not quite so let's say we minted we have 2434 01:39:51,800 --> 01:39:56,360 100 collateral divided 100 DSC right 2435 01:39:56,360 --> 01:39:58,800 this is one: one if we go down a we go 2436 01:39:58,800 --> 01:40:00,679 down a penny we're going to be under 2437 01:40:00,679 --> 01:40:03,080 collateralized right and we don't want 2438 01:40:03,080 --> 01:40:04,800 to go under collateralized we always 2439 01:40:04,800 --> 01:40:06,320 want to be over 2440 01:40:06,320 --> 01:40:08,760 collateralized because if this ever goes 2441 01:40:08,760 --> 01:40:12,639 below 100 our system is is bunked up 2442 01:40:12,639 --> 01:40:14,679 right so we want to set the threshold to 2443 01:40:14,679 --> 01:40:16,719 be like hey if you go under 2444 01:40:16,719 --> 01:40:19,880 150 collateral you can get liquidated 2445 01:40:19,880 --> 01:40:22,119 right if we go under 100 it's already 2446 01:40:22,119 --> 01:40:24,320 too late so we want to say hey we want 2447 01:40:24,320 --> 01:40:27,360 you to go at least 150 so we're going to 2448 01:40:27,360 --> 01:40:30,520 create a liquidation threshold and we're 2449 01:40:30,520 --> 01:40:32,840 going to do this at the top so we're 2450 01:40:32,840 --> 01:40:39,239 going to say unit 256 private constant 2451 01:40:39,239 --> 01:40:43,159 liquidation threshold equals 50 and this 2452 01:40:43,159 --> 01:40:45,880 means you need to be 150 or no this 2453 01:40:45,880 --> 01:40:49,639 means you need to be 200% over 2454 01:40:49,639 --> 01:40:52,719 collateralized I think might be 150 but 2455 01:40:52,719 --> 01:40:55,199 we'll find out in the test so now if we 2456 01:40:55,199 --> 01:40:57,320 go back down to where we were to get our 2457 01:40:57,320 --> 01:40:58,639 health Factor we're not just going to 2458 01:40:58,639 --> 01:41:01,000 divide these two and even this doesn't 2459 01:41:01,000 --> 01:41:02,800 really work because if we have 150 over 2460 01:41:02,800 --> 01:41:07,040 100 150 divid 100 is equal to 1.5 2461 01:41:07,040 --> 01:41:08,280 decimals don't work in solidity so it 2462 01:41:08,280 --> 01:41:10,400 would just be one I guess that would 2463 01:41:10,400 --> 01:41:12,840 work but we want we want to know exactly 2464 01:41:12,840 --> 01:41:14,560 what the health factor is right with 2465 01:41:14,560 --> 01:41:17,679 Precision so first off let's instead of 2466 01:41:17,679 --> 01:41:21,080 just doing this let's say U into 2467 01:41:21,080 --> 01:41:26,239 256 collateral adjusted or threshold 2468 01:41:26,239 --> 01:41:31,040 equals the collateral value in USD times 2469 01:41:31,040 --> 01:41:33,320 liquidation threshold and then we should 2470 01:41:33,320 --> 01:41:35,000 divide by 100 right because the 2471 01:41:35,000 --> 01:41:37,400 liquidation threshold has is multiplying 2472 01:41:37,400 --> 01:41:39,800 it's making our number much bigger so we 2473 01:41:39,800 --> 01:41:42,480 should divide by 100 as I don't like 2474 01:41:42,480 --> 01:41:45,440 floating numbers so we'll do U into 256 2475 01:41:45,440 --> 01:41:48,800 private constant 2476 01:41:48,800 --> 01:41:52,920 liquidation Precision equals 100 so 2477 01:41:52,920 --> 01:41:55,000 we'll go back back down divided by the 2478 01:41:55,000 --> 01:41:57,119 liquidation Precision so now we have 2479 01:41:57,119 --> 01:42:00,040 this collateral amount adjusted for this 2480 01:42:00,040 --> 01:42:02,320 this threshold right so now you can kind 2481 01:42:02,320 --> 01:42:04,760 of think of it as instead of say we have 2482 01:42:04,760 --> 01:42:10,239 $150 of eth divided by $100 of DSC right 2483 01:42:10,239 --> 01:42:13,520 this would be 1.5 but now they need to 2484 01:42:13,520 --> 01:42:17,440 multiply by 50 as well but now this 2485 01:42:17,440 --> 01:42:19,280 collateral value is multiplied by 2486 01:42:19,280 --> 01:42:22,199 essentially 1 over5 right let's let's do 2487 01:42:22,199 --> 01:42:25,960 the math here right if we had say $1,000 2488 01:42:25,960 --> 01:42:29,280 of eth we just times that by 50 which 2489 01:42:29,280 --> 01:42:33,119 gets us to ,000 but then we divided by 2490 01:42:33,119 --> 01:42:36,599 that by about 100 which equals 500 so if 2491 01:42:36,599 --> 01:42:40,560 we had $1,000 of eth * 50 was this 2492 01:42:40,560 --> 01:42:43,199 divided 100 is 500 same thing with this 2493 01:42:43,199 --> 01:42:45,679 this example down here if we had $150 2494 01:42:45,679 --> 01:42:51,520 worth of each eth we say 150 * 50 equal 2495 01:42:51,520 --> 01:42:57,520 75 / 100 equal 75 and then if we do 75 2496 01:42:57,520 --> 01:43:00,080 ID 100 that is going to be definitely 2497 01:43:00,080 --> 01:43:02,920 less than one right so we we're 2498 01:43:02,920 --> 01:43:05,239 basically saying with this 50 threshold 2499 01:43:05,239 --> 01:43:08,719 50 over 100 is essentially 1 over two 2500 01:43:08,719 --> 01:43:10,280 we're saying you need to have double the 2501 01:43:10,280 --> 01:43:12,520 collateral in here so yeah so now that 2502 01:43:12,520 --> 01:43:15,440 I'm talking it out loud this 50% 2503 01:43:15,440 --> 01:43:17,000 liquidation threshold means we need to 2504 01:43:17,000 --> 01:43:19,639 be 200% over collateralized right we 2505 01:43:19,639 --> 01:43:21,199 need to have double the collateral that 2506 01:43:21,199 --> 01:43:23,679 we have the mint 2507 01:43:23,679 --> 01:43:27,000 DSC anyways so whole bunch of math 2508 01:43:27,000 --> 01:43:29,679 hopefully this makes sense if not 2509 01:43:29,679 --> 01:43:31,360 definitely work with your AI to make 2510 01:43:31,360 --> 01:43:33,639 sure this makes sense or ask questions 2511 01:43:33,639 --> 01:43:35,360 in the discussion right I know some of 2512 01:43:35,360 --> 01:43:36,599 this math can get a little bit tricky 2513 01:43:36,599 --> 01:43:38,719 here so collateral adjusted for 2514 01:43:38,719 --> 01:43:42,119 threshold and now we can return the 2515 01:43:42,119 --> 01:43:44,920 collateral adjusted oops this collateral 2516 01:43:44,920 --> 01:43:49,480 adjusted for threshold times Precision 2517 01:43:49,480 --> 01:43:52,880 divided by that total DSC minted now 2518 01:43:52,880 --> 01:43:55,639 this will give us our true Health factor 2519 01:43:55,639 --> 01:43:57,719 and if this is less than one you can get 2520 01:43:57,719 --> 01:44:01,840 liquidated now this is one example right 2521 01:44:01,840 --> 01:44:04,040 let's look at another example let's say 2522 01:44:04,040 --> 01:44:05,920 I guess this is two examples so let's 2523 01:44:05,920 --> 01:44:10,280 say they have $1,000 worth of eth and 2524 01:44:10,280 --> 01:44:14,360 100 DSC right so let's do the math here 2525 01:44:14,360 --> 01:44:20,719 1,00 * 50 equal 50 1 2 3 ID 100 is equal 2526 01:44:20,719 --> 01:44:26,000 to 500 500 divid 100 which is definitely 2527 01:44:26,000 --> 01:44:28,400 greater than one right 500 divid 100 is 2528 01:44:28,400 --> 01:44:31,840 5 so this person with $1,000 of e 2529 01:44:31,840 --> 01:44:34,000 deposited and 100 DSC minted would have 2530 01:44:34,000 --> 01:44:38,159 a health factor of 500 nice so now that 2531 01:44:38,159 --> 01:44:40,199 we have a health Factor we can actually 2532 01:44:40,199 --> 01:44:42,080 finally do this revert if Health factor 2533 01:44:42,080 --> 01:44:45,239 is broken function where we say we can 2534 01:44:45,239 --> 01:44:48,040 even put this above this as kind of like 2535 01:44:48,040 --> 01:44:52,159 a pseudo natspec we could say U into 256 2536 01:44:52,159 --> 01:44:53,599 Health Factor 2537 01:44:53,599 --> 01:44:57,119 equals underscore Health factor of the 2538 01:44:57,119 --> 01:45:01,800 user and we say if the user excuse me 2539 01:45:01,800 --> 01:45:04,560 actually let's do user Health factor and 2540 01:45:04,560 --> 01:45:07,560 if the user Health factor is less than 2541 01:45:07,560 --> 01:45:10,159 the some Min Health factor which is 2542 01:45:10,159 --> 01:45:12,000 going to be one Health Factor so let's 2543 01:45:12,000 --> 01:45:13,400 go ahead and create this let's go to the 2544 01:45:13,400 --> 01:45:16,480 top because we hate floating numbers you 2545 01:45:16,480 --> 01:45:19,800 into 256 private constant Min Health 2546 01:45:19,800 --> 01:45:22,320 Factor equals 1 if the user Health 2547 01:45:22,320 --> 01:45:23,840 factor is like than the Min Health 2548 01:45:23,840 --> 01:45:25,880 Factor then we're going to go ahead and 2549 01:45:25,880 --> 01:45:28,360 revert I don't love this we're going to 2550 01:45:28,360 --> 01:45:29,679 do DSC 2551 01:45:29,679 --> 01:45:33,679 engine underscore underscore breaks 2552 01:45:33,679 --> 01:45:36,400 Health Factor we're going to pass in 2553 01:45:36,400 --> 01:45:38,639 this health factor that we break with 2554 01:45:38,639 --> 01:45:42,880 boom new air scroll the top Air engine 2555 01:45:42,880 --> 01:45:47,920 breaks Health Factor un 256 Health 2556 01:45:48,400 --> 01:45:50,360 Factor looks like we still have some red 2557 01:45:50,360 --> 01:45:51,800 here what did I forget okay revert of 2558 01:45:51,800 --> 01:45:53,599 Health factor is broken underscore like 2559 01:45:53,599 --> 01:45:55,880 this boom now it looks like nothing's 2560 01:45:55,880 --> 01:45:59,159 red let's just make sure Forge 2561 01:45:59,159 --> 01:46:03,199 build successful 2562 01:46:04,800 --> 01:46:09,199 nice okay where were we mint DSC okay 2563 01:46:09,199 --> 01:46:12,560 mint DSC so we added some more DSC 2564 01:46:12,560 --> 01:46:16,599 minted and if adding this DSC breaks the 2565 01:46:16,599 --> 01:46:19,239 health Factor because breaks the health 2566 01:46:19,239 --> 01:46:20,920 Factor we should revert we should not 2567 01:46:20,920 --> 01:46:24,239 let anybody mint DSC if they're going to 2568 01:46:24,239 --> 01:46:25,719 cause themselves to get liquidated I 2569 01:46:25,719 --> 01:46:27,880 mean we could go ahead and let them do 2570 01:46:27,880 --> 01:46:29,960 it but like let's not because that's not 2571 01:46:29,960 --> 01:46:33,040 a very good user experience now what we 2572 01:46:33,040 --> 01:46:35,400 want to do is we want to actually mint 2573 01:46:35,400 --> 01:46:39,560 the DSC so this is where the DSC has 2574 01:46:39,560 --> 01:46:42,119 this mint function that's only owner and 2575 01:46:42,119 --> 01:46:44,480 the owner of this is going to be the DSC 2576 01:46:44,480 --> 01:46:47,360 engine now we could say and if we look 2577 01:46:47,360 --> 01:46:48,880 at this mint function it returns a 2578 01:46:48,880 --> 01:46:53,679 Boolean so we'll say bull minted 2579 01:46:53,679 --> 01:46:57,960 equals iore DSC mint and what does it 2580 01:46:57,960 --> 01:47:01,280 take for parameters address 2 and amount 2581 01:47:01,280 --> 01:47:03,159 so address two is going to be message. 2582 01:47:03,159 --> 01:47:05,400 sender and amount is going to be amount 2583 01:47:05,400 --> 01:47:09,920 DSC to Mint and then we'll say if not 2584 01:47:09,920 --> 01:47:14,159 minted we'll just say revert DSC engine 2585 01:47:14,159 --> 01:47:17,719 uncore uncore mint failed which is a new 2586 01:47:17,719 --> 01:47:22,080 error scroll up to the top error DC mint 2587 01:47:22,080 --> 01:47:25,760 failed yeah 2588 01:47:30,800 --> 01:47:34,320 Co so now we have a mint function and we 2589 01:47:34,320 --> 01:47:35,960 have a deposit function so we can 2590 01:47:35,960 --> 01:47:39,159 deposit collateral we can mint DSC but 2591 01:47:39,159 --> 01:47:40,840 additionally we can get account 2592 01:47:40,840 --> 01:47:42,560 information we can calculate someone's 2593 01:47:42,560 --> 01:47:45,040 Health Factor we can calculate the USD 2594 01:47:45,040 --> 01:47:47,599 value of these different tokens so at 2595 01:47:47,599 --> 01:47:50,280 this point I'm like o I have no idea if 2596 01:47:50,280 --> 01:47:52,440 what I'm doing makes any sort of sense I 2597 01:47:52,440 --> 01:47:53,480 want to make make sure I write some 2598 01:47:53,480 --> 01:47:56,679 tests here so this is where we could go 2599 01:47:56,679 --> 01:48:00,000 ahead and create a new folder unit tests 2600 01:48:00,000 --> 01:48:02,000 and if you wanted to you could skip 2601 01:48:02,000 --> 01:48:03,960 writing the scripts and just kind of 2602 01:48:03,960 --> 01:48:06,080 deploy in your unit tests and then do 2603 01:48:06,080 --> 01:48:07,360 some integration tests but I'm just 2604 01:48:07,360 --> 01:48:09,080 going to have my unit tests also be my 2605 01:48:09,080 --> 01:48:11,360 integration test for this one so let's 2606 01:48:11,360 --> 01:48:15,119 go ahead let's write a script deploy 2607 01:48:15,119 --> 01:48:19,760 DSC s.o you already know the drill for 2608 01:48:19,760 --> 01:48:21,520 this 2609 01:48:21,520 --> 01:48:25,719 spdx like since I even zoom in a little 2610 01:48:25,719 --> 01:48:31,280 bit identifier MIT contract deploy DSC 2611 01:48:31,280 --> 01:48:37,199 is script import script from Forge 2612 01:48:37,199 --> 01:48:42,560 STD script. Soul like that pragma 2613 01:48:42,560 --> 01:48:44,480 solidity 2614 01:48:44,480 --> 01:48:47,280 0.818 it's good in here we're going to 2615 01:48:47,280 --> 01:48:50,480 have our function run external we've 2616 01:48:50,480 --> 01:48:53,280 done this a 100 times exter 2617 01:48:53,280 --> 01:48:56,719 n and this is going to returns a couple 2618 01:48:56,719 --> 01:48:58,360 of things it's going to both return the 2619 01:48:58,360 --> 01:49:00,080 DS 2620 01:49:00,080 --> 01:49:04,000 decentralized stable coin and the DSC 2621 01:49:04,000 --> 01:49:05,679 engine and it's going to return 2622 01:49:05,679 --> 01:49:06,719 something else but I'm not going to put 2623 01:49:06,719 --> 01:49:08,760 it in quite yet so to do that we're 2624 01:49:08,760 --> 01:49:09,760 going to have to 2625 01:49:09,760 --> 01:49:12,320 import centralized 2626 01:49:12,320 --> 01:49:15,760 stablecoin from 2627 01:49:15,760 --> 01:49:18,960 slsrc centralized stable coin. we're 2628 01:49:18,960 --> 01:49:21,560 going to close and reopen my vs code 2629 01:49:21,560 --> 01:49:23,320 it's being really weird right now 2630 01:49:23,320 --> 01:49:26,040 there we go all fixed so the two of 2631 01:49:26,040 --> 01:49:28,560 these what are we going to do well we're 2632 01:49:28,560 --> 01:49:33,119 going to do vm. start broadcast this. 2633 01:49:33,119 --> 01:49:34,800 stop 2634 01:49:34,800 --> 01:49:38,000 broadcast this oh and we need the DSC 2635 01:49:38,000 --> 01:49:44,199 engine import DSC engine from srcds 2636 01:49:44,199 --> 01:49:47,440 engine. Soul and in here we're going to 2637 01:49:47,440 --> 01:49:50,599 deploy both of these so we'll say 2638 01:49:50,599 --> 01:49:53,480 centralized stable coin DSC equals 2639 01:49:53,480 --> 01:49:56,440 new decentralized stable coin does our 2640 01:49:56,440 --> 01:49:58,280 decentralized stable coin have any 2641 01:49:58,280 --> 01:50:01,280 parameters it does not we're also going 2642 01:50:01,280 --> 01:50:03,440 to deploy our 2643 01:50:03,440 --> 01:50:09,719 dsce our DSC engine engine equals new BC 2644 01:50:09,719 --> 01:50:12,960 engine this takes a whole bunch of stuff 2645 01:50:12,960 --> 01:50:16,199 right this is going to take go to the 2646 01:50:16,199 --> 01:50:18,880 Constructor token addresses price feed 2647 01:50:18,880 --> 01:50:21,960 addresses if we toggle oops tle the word 2648 01:50:21,960 --> 01:50:24,679 wrap addresses price feed addresses and 2649 01:50:24,679 --> 01:50:27,040 the DSC address so we have the DSC 2650 01:50:27,040 --> 01:50:29,239 address boom it's going to be this one 2651 01:50:29,239 --> 01:50:31,239 where do we get the price feed addresses 2652 01:50:31,239 --> 01:50:32,800 and you guessed it we're going to make a 2653 01:50:32,800 --> 01:50:35,040 helper config so what does this DSC 2654 01:50:35,040 --> 01:50:38,840 engine need Constructor an array of 2655 01:50:38,840 --> 01:50:41,199 token addresses an array of price feeds 2656 01:50:41,199 --> 01:50:44,639 and then the DSC address so where are we 2657 01:50:44,639 --> 01:50:46,119 going to get those addresses from you 2658 01:50:46,119 --> 01:50:50,800 guessed it a helper config so new file 2659 01:50:50,800 --> 01:50:54,639 helper configs do Soul 2660 01:50:54,639 --> 01:50:59,719 spdx license identifier MIT pragma 2661 01:50:59,719 --> 01:51:01,199 solidity 2662 01:51:01,199 --> 01:51:07,040 0.818 contract helper config is script 2663 01:51:07,040 --> 01:51:08,679 import 2664 01:51:08,679 --> 01:51:11,880 script from Forge 2665 01:51:11,880 --> 01:51:15,159 script. soul for now let's do this on 2666 01:51:15,159 --> 01:51:19,239 the sepolia Chain so we'll do 2667 01:51:19,239 --> 01:51:23,800 struct Network config do we need in here 2668 01:51:23,800 --> 01:51:25,560 we're going to need an well we're going 2669 01:51:25,560 --> 01:51:29,800 to need we and wrapped Bitcoin those 2670 01:51:29,800 --> 01:51:31,400 price feed addresses and those DSC 2671 01:51:31,400 --> 01:51:36,199 addresses address we USD price 2672 01:51:36,199 --> 01:51:40,679 feed address wrapped Bitcoin USD price 2673 01:51:40,679 --> 01:51:45,880 feed again we is the erc20 version of 2674 01:51:45,880 --> 01:51:48,840 ethereum I've got a example we token 2675 01:51:48,840 --> 01:51:51,239 contract on zolia and if you look at it 2676 01:51:51,239 --> 01:51:54,119 right here go write contract it's got 2677 01:51:54,119 --> 01:51:56,119 this function deposit where you deposit 2678 01:51:56,119 --> 01:51:59,440 eth and it'll return to you an erc20 2679 01:51:59,440 --> 01:52:02,840 version of eth to your metams called we 2680 01:52:02,840 --> 01:52:04,280 then whenever you're done with it you 2681 01:52:04,280 --> 01:52:07,639 just withdraw your eth and burn your we 2682 01:52:07,639 --> 01:52:09,800 WRA Bitcoin is something similar but 2683 01:52:09,800 --> 01:52:12,320 with Bitcoin the difference is since 2684 01:52:12,320 --> 01:52:13,800 Bitcoin doesn't originate on the eth 2685 01:52:13,800 --> 01:52:16,560 blockchain there is some risk in 2686 01:52:16,560 --> 01:52:18,480 bridging it over but I'm not going to go 2687 01:52:18,480 --> 01:52:19,840 into that that's something for you to 2688 01:52:19,840 --> 01:52:21,920 look up so we're also going to need the 2689 01:52:21,920 --> 01:52:24,159 address of the we token itself we're 2690 01:52:24,159 --> 01:52:26,280 going to need the address of the wrapped 2691 01:52:26,280 --> 01:52:29,199 Bitcoin itself and we're going to need a 2692 01:52:29,199 --> 01:52:32,880 un 256 deployer deployer key kind of 2693 01:52:32,880 --> 01:52:35,119 like what we did in one of our previous 2694 01:52:35,119 --> 01:52:37,480 lessons we're going to have Network 2695 01:52:37,480 --> 01:52:43,159 config public Active network config and 2696 01:52:43,159 --> 01:52:46,199 then we're going to have 2697 01:52:46,199 --> 01:52:48,719 Constructor like here we're going to 2698 01:52:48,719 --> 01:52:53,280 have a function get sepolia e 2699 01:52:53,280 --> 01:52:57,880 config public view returns Network 2700 01:52:57,880 --> 01:53:01,639 config memory this and then we're going 2701 01:53:01,639 --> 01:53:06,440 to return Network config we're going to 2702 01:53:06,440 --> 01:53:10,719 have we USD price feed B let's go to 2703 01:53:10,719 --> 01:53:15,000 docs. chain. link let's go to theia 2704 01:53:15,000 --> 01:53:17,400 zolia where's 2705 01:53:17,400 --> 01:53:20,679 ebtc eth excuse me e USD right here copy 2706 01:53:20,679 --> 01:53:23,199 that paste 2707 01:53:23,199 --> 01:53:26,400 we're going to need W Bitcoin USD price 2708 01:53:26,400 --> 01:53:30,159 feed on seoa bitcoin USD right here grab 2709 01:53:30,159 --> 01:53:34,360 that need the we contract address which 2710 01:53:34,360 --> 01:53:37,719 I have here this is one that I deployed 2711 01:53:37,719 --> 01:53:40,599 we here we're going to need wrapped 2712 01:53:40,599 --> 01:53:42,639 Bitcoin and if you're looking for all of 2713 01:53:42,639 --> 01:53:44,560 these and you want to just copy them out 2714 01:53:44,560 --> 01:53:46,880 of the GitHub repo associated with this 2715 01:53:46,880 --> 01:53:47,880 you can go to 2716 01:53:47,880 --> 01:53:51,480 SRC you can go to script upper config 2717 01:53:51,480 --> 01:53:52,480 and they're all in here if you if you 2718 01:53:52,480 --> 01:53:55,480 want just copy paste by the way but also 2719 01:53:55,480 --> 01:53:58,800 wrapped Bitcoin which I guess is this 2720 01:53:58,800 --> 01:54:00,840 address thought I had a different one 2721 01:54:00,840 --> 01:54:01,880 well whatever we're going to use this 2722 01:54:01,880 --> 01:54:04,239 one and if it doesn't work that's fine 2723 01:54:04,239 --> 01:54:06,040 we're going to use this one then of 2724 01:54:06,040 --> 01:54:11,320 course player key bm. EMV unit private 2725 01:54:11,320 --> 01:54:15,040 key like this okay and then we're going 2726 01:54:15,040 --> 01:54:21,000 to do function get or create envil eth 2727 01:54:21,000 --> 01:54:23,320 config public 2728 01:54:23,320 --> 01:54:27,480 turns Network config 2729 01:54:27,480 --> 01:54:29,360 memory and we're going to do a little 2730 01:54:29,360 --> 01:54:32,560 bit of mock deployments here but we're 2731 01:54:32,560 --> 01:54:37,480 going to say if Active network config do 2732 01:54:37,480 --> 01:54:40,239 wusd price feed does not equal to the 2733 01:54:40,239 --> 01:54:42,840 zero address then we've already set it 2734 01:54:42,840 --> 01:54:46,320 turn Active network e config we're going 2735 01:54:46,320 --> 01:54:48,520 to do some broadcasting so we're going 2736 01:54:48,520 --> 01:54:51,280 to need a couple of couple of mocks in 2737 01:54:51,280 --> 01:54:53,960 here we're need some mock price feeds 2738 01:54:53,960 --> 01:54:58,119 and some mock ec20 tokens so we're going 2739 01:54:58,119 --> 01:55:01,719 to need a mock B3 aggregator which we're 2740 01:55:01,719 --> 01:55:06,199 going to go to test folder mocks and I'm 2741 01:55:06,199 --> 01:55:08,480 going to copy paste a mock from this 2742 01:55:08,480 --> 01:55:10,360 repo if you want to copy paste as well 2743 01:55:10,360 --> 01:55:14,199 go for it test 2744 01:55:14,199 --> 01:55:17,320 mock mock V3 2745 01:55:17,320 --> 01:55:20,320 aggregator 2746 01:55:21,320 --> 01:55:25,800 this new file mock V3 2747 01:55:25,800 --> 01:55:31,360 agregator doou cool import that import 2748 01:55:31,360 --> 01:55:34,480 mock B3 2749 01:55:34,480 --> 01:55:39,480 agregator fromt dot slash test 2750 01:55:39,480 --> 01:55:42,960 slmm V3 aggregator Doo so we have that 2751 01:55:42,960 --> 01:55:44,440 we're also going to need some moach Er 2752 01:55:44,440 --> 01:55:47,320 c20s we can get those actually directly 2753 01:55:47,320 --> 01:55:50,760 from open Zeppelin so if we do import 2754 01:55:50,760 --> 01:55:52,920 irc2 mock 2755 01:55:52,920 --> 01:55:58,360 from atop Zeppelin contract slmx 2756 01:55:58,360 --> 01:56:00,159 erc20 2757 01:56:00,159 --> 01:56:02,719 mocko and if we command click into this 2758 01:56:02,719 --> 01:56:04,599 or you open this up you can see there's 2759 01:56:04,599 --> 01:56:05,920 a whole bunch of stuff in here like we 2760 01:56:05,920 --> 01:56:07,639 can mint as much as we want burn as much 2761 01:56:07,639 --> 01:56:09,599 as we want do transfers and stuff we can 2762 01:56:09,599 --> 01:56:11,360 do pretty much whatever we want and 2763 01:56:11,360 --> 01:56:13,000 that's why it's a mock token good for 2764 01:56:13,000 --> 01:56:16,840 testing with so down here vm. start 2765 01:56:16,840 --> 01:56:19,480 broadcast we're going to create a mock 2766 01:56:19,480 --> 01:56:24,320 B3 aggregator ethusd price feed equals 2767 01:56:24,320 --> 01:56:27,760 new Mach V3 aggregator and what does 2768 01:56:27,760 --> 01:56:30,440 this take for the Constructor takes 2769 01:56:30,440 --> 01:56:35,400 decimals and an and an initial answer so 2770 01:56:35,400 --> 01:56:37,320 we're going to scroll up here we're 2771 01:56:37,320 --> 01:56:41,520 going to say UN 2 or excuse me U 8 2772 01:56:41,520 --> 01:56:44,239 public constant 2773 01:56:44,239 --> 01:56:48,400 decimals decimals equals 8 and we'll say 2774 01:56:48,400 --> 01:56:55,280 U or not U me int 250 D6 public constant 2775 01:56:55,280 --> 01:57:00,159 eth USD price equals 2776 01:57:00,159 --> 01:57:03,280 2000 E8 and then we're going to do the 2777 01:57:03,280 --> 01:57:05,960 same thing but instead of eth it's going 2778 01:57:05,960 --> 01:57:09,880 to be BTC and we'll make this 1,000 to 2779 01:57:09,880 --> 01:57:14,000 E8 eth USD scroll down all right new 2780 01:57:14,000 --> 01:57:18,400 mock what does it take control click U 8 2781 01:57:18,400 --> 01:57:20,599 decimals initial answer control minus to 2782 01:57:20,599 --> 01:57:22,679 go back 2783 01:57:22,679 --> 01:57:25,880 decimals and then the initial answer 2784 01:57:25,880 --> 01:57:29,599 those okay oh and let's do vm. stop 2785 01:57:29,599 --> 01:57:32,520 broadcast now we're going to do erc20 2786 01:57:32,520 --> 01:57:38,960 mock we mock equals new erc20 mock what 2787 01:57:38,960 --> 01:57:42,000 does this one take name symbol initial 2788 01:57:42,000 --> 01:57:47,239 account initial balance okay say we we 2789 01:57:47,239 --> 01:57:50,400 message. sender 2790 01:57:50,400 --> 01:57:53,719 1008 we probably want to do more than we 2791 01:57:53,719 --> 01:57:54,520 probably don't want to have these 2792 01:57:54,520 --> 01:57:56,920 floating numbers in here but it's just a 2793 01:57:56,920 --> 01:57:58,639 mock it's not a big deal I guess now 2794 01:57:58,639 --> 01:58:00,840 we're going to copy paste all of this 2795 01:58:00,840 --> 01:58:02,800 for 2796 01:58:02,800 --> 01:58:04,800 BTC 2797 01:58:04,800 --> 01:58:07,480 BTC we're going to say 2798 01:58:07,480 --> 01:58:11,560 BTC it's going to be the wrapped 2799 01:58:11,560 --> 01:58:13,639 BTC wrapped 2800 01:58:13,639 --> 01:58:17,760 BTC wrapped BTC like that stop the 2801 01:58:17,760 --> 01:58:22,639 broadcast and then return Network config 2802 01:58:22,639 --> 01:58:26,440 we USD price feed is going to be address 2803 01:58:26,440 --> 01:58:28,360 oh thanks get up 2804 01:58:28,360 --> 01:58:31,360 co-pilot thanks get up copilot thanksg 2805 01:58:31,360 --> 01:58:33,520 up co-pilot thanks get up 2806 01:58:33,520 --> 01:58:36,000 co-pilot and this is actually going to 2807 01:58:36,000 --> 01:58:38,960 be the default Anvil key which if you 2808 01:58:38,960 --> 01:58:42,400 want you can just go back to here again 2809 01:58:42,400 --> 01:58:43,840 and copy paste it out of here or you 2810 01:58:43,840 --> 01:58:47,560 know what we just run Anvil scroll up 2811 01:58:47,560 --> 01:58:50,960 boom private key right here cancel that 2812 01:58:50,960 --> 01:58:57,239 say U 256 public excuse me int 26 public 2813 01:58:57,239 --> 01:59:02,840 default Anvil key equals B that in and 2814 01:59:02,840 --> 01:59:05,560 we'll say we just use the default Anvil 2815 01:59:05,560 --> 01:59:08,679 key if you're working with Anvil okay 2816 01:59:08,679 --> 01:59:12,560 nice so now we have get or create Anvil 2817 01:59:12,560 --> 01:59:16,239 get zolia let's update our Constructor 2818 01:59:16,239 --> 01:59:20,520 so we'll say if block. chain ID equals 2819 01:59:20,520 --> 01:59:23,440 equals 111 2820 01:59:23,440 --> 01:59:28,840 55111 then active Active network config 2821 01:59:28,840 --> 01:59:34,599 equals get Suppa eth config else to 2822 01:59:34,599 --> 01:59:38,159 Active network config equals get or 2823 01:59:38,159 --> 01:59:41,560 create eth Anvil config nice so we've 2824 01:59:41,560 --> 01:59:43,800 got a little bit of a helper config here 2825 01:59:43,800 --> 01:59:46,280 a little semicolon down here this looks 2826 01:59:46,280 --> 01:59:48,440 pretty good what's wrong here sorry this 2827 01:59:48,440 --> 01:59:49,760 is a 2828 01:59:49,760 --> 01:59:53,040 ent6 okay cool so now that we have a 2829 01:59:53,040 --> 01:59:54,880 helper config we can go finally back to 2830 01:59:54,880 --> 01:59:57,800 our deploy DSC let's import that in here 2831 01:59:57,800 --> 02:00:02,480 import helper config from helper config 2832 02:00:02,480 --> 02:00:05,760 right at the top we'll say helper config 2833 02:00:05,760 --> 02:00:10,199 config equals new upper config and out 2834 02:00:10,199 --> 02:00:13,400 of this config we're going to get all 2835 02:00:13,400 --> 02:00:17,159 this we Bitcoin we repid coin deploy key 2836 02:00:17,159 --> 02:00:21,639 Etc so I'm going to say address with USD 2837 02:00:21,639 --> 02:00:23,639 price feed oh I can even just hit Tab 2838 02:00:23,639 --> 02:00:27,320 and looks like it has most of it yep W 2839 02:00:27,320 --> 02:00:30,440 USD rep Bitcoin we rep Bitcoin deploy 2840 02:00:30,440 --> 02:00:33,360 key equals config do Active network 2841 02:00:33,360 --> 02:00:35,840 config cool looks good to me I'm going 2842 02:00:35,840 --> 02:00:38,360 to toggle word wrap so that it wraps 2843 02:00:38,360 --> 02:00:41,159 around okay cool we have all of those 2844 02:00:41,159 --> 02:00:43,639 now our DSC engine takes an array of 2845 02:00:43,639 --> 02:00:46,040 token addresses an array of price feeds 2846 02:00:46,040 --> 02:00:48,440 so we can say right at the top let's 2847 02:00:48,440 --> 02:00:50,880 make those arrays we'll say address 2848 02:00:50,880 --> 02:00:54,719 array public token 2849 02:00:54,719 --> 02:00:59,239 addresses address array public price 2850 02:00:59,239 --> 02:01:03,320 feed addresses and we'll say token 2851 02:01:03,320 --> 02:01:07,040 addresses equals we and rep 2852 02:01:07,040 --> 02:01:12,639 BTC price feed addresses equals we USD 2853 02:01:12,639 --> 02:01:15,360 price feed address W Bitcoin USD price 2854 02:01:15,360 --> 02:01:18,679 feed okay cool I think that's everything 2855 02:01:18,679 --> 02:01:20,840 right yeah so now we can go back to this 2856 02:01:20,840 --> 02:01:25,040 line now DC engine engine new DC engine 2857 02:01:25,040 --> 02:01:27,960 and it takes the token 2858 02:01:27,960 --> 02:01:33,239 addresses price feed addresses and DSC 2859 02:01:33,239 --> 02:01:35,719 okay cool and then finally something we 2860 02:01:35,719 --> 02:01:37,480 haven't really talked about too much but 2861 02:01:37,480 --> 02:01:39,920 this decentralized stable coin like I 2862 02:01:39,920 --> 02:01:41,960 said it's ownable but it needs to be 2863 02:01:41,960 --> 02:01:44,960 owned by the engine so this ownable 2864 02:01:44,960 --> 02:01:47,599 actually has a transfer ownership 2865 02:01:47,599 --> 02:01:49,639 function and we're going to call that to 2866 02:01:49,639 --> 02:01:52,320 transfer ownership to the engine so 2867 02:01:52,320 --> 02:01:54,119 we'll go back to our deploy here oh 2868 02:01:54,119 --> 02:01:58,920 sorry this isn't DSC me address DSC so 2869 02:01:58,920 --> 02:02:01,599 then we're going to call 2870 02:02:01,599 --> 02:02:06,400 dc. transfer ownership to the address 2871 02:02:06,400 --> 02:02:10,920 DSC engine now only the engine oops 2872 02:02:10,920 --> 02:02:13,440 engine excuse me only the engine can do 2873 02:02:13,440 --> 02:02:15,480 anything with it and then we're going to 2874 02:02:15,480 --> 02:02:20,119 return all these return DSC and engine 2875 02:02:20,119 --> 02:02:24,639 nice oh and the deployer is going to go 2876 02:02:26,199 --> 02:02:28,960 here okay cool so the reason we did all 2877 02:02:28,960 --> 02:02:31,000 this was because I wanted to write my 2878 02:02:31,000 --> 02:02:33,599 unit tests using actual deploy scripts 2879 02:02:33,599 --> 02:02:35,239 because I prefer to do that but like I 2880 02:02:35,239 --> 02:02:36,880 said it might be a good idea for you to 2881 02:02:36,880 --> 02:02:38,199 write unit tests before you write your 2882 02:02:38,199 --> 02:02:40,920 deploy scripts and then integration test 2883 02:02:40,920 --> 02:02:43,560 with your deploy scripts but in any case 2884 02:02:43,560 --> 02:02:45,320 let's go ahead and finally create a test 2885 02:02:45,320 --> 02:02:49,800 in here DSC engine test. 2886 02:02:49,800 --> 02:02:51,520 T.O remember the whole reason we're 2887 02:02:51,520 --> 02:02:53,360 doing all this I know we've been coding 2888 02:02:53,360 --> 02:02:57,040 a lot is in the DSC engine we added a 2889 02:02:57,040 --> 02:02:58,840 ton of functions in here some of them 2890 02:02:58,840 --> 02:03:00,599 like get USD value which we definitely 2891 02:03:00,599 --> 02:03:02,719 want to check get collateral account 2892 02:03:02,719 --> 02:03:05,159 value we want to make sure minting Works 2893 02:03:05,159 --> 02:03:07,000 our Constructor Works depositing Works 2894 02:03:07,000 --> 02:03:09,400 Etc so we're just kind of testing as we 2895 02:03:09,400 --> 02:03:11,560 go along which like I said when I'm 2896 02:03:11,560 --> 02:03:13,639 actually coding this I did write tests 2897 02:03:13,639 --> 02:03:17,000 and I did write deploy scripts because I 2898 02:03:17,000 --> 02:03:18,880 did want to test as I was going right I 2899 02:03:18,880 --> 02:03:20,920 didn't want to have to go back and 2900 02:03:20,920 --> 02:03:23,639 refactor and rewrite my code if I made 2901 02:03:23,639 --> 02:03:25,360 some glaring mistake right it's really 2902 02:03:25,360 --> 02:03:27,400 good to test while you're building as 2903 02:03:27,400 --> 02:03:29,239 well and to be honest I I feel like it 2904 02:03:29,239 --> 02:03:30,800 makes me go faster because I have more 2905 02:03:30,800 --> 02:03:33,360 confidence that what I did was correct 2906 02:03:33,360 --> 02:03:35,639 while I'm coding so 2907 02:03:35,639 --> 02:03:37,440 spdx 2908 02:03:37,440 --> 02:03:42,920 license identifier MIT pragma solidity 2909 02:03:42,920 --> 02:03:44,360 0. 2910 02:03:44,360 --> 02:03:50,440 8.18 little car here contract DSC engine 2911 02:03:50,440 --> 02:03:57,599 test is test import test from Forge STD 2912 02:03:57,599 --> 02:04:02,400 test that's all like this all right cool 2913 02:04:02,400 --> 02:04:03,960 function 2914 02:04:03,960 --> 02:04:06,880 setup public or external we're going to 2915 02:04:06,880 --> 02:04:11,320 need to deploy to deploy our contract so 2916 02:04:11,320 --> 02:04:17,480 we're going to import deoy DSC from do 2917 02:04:17,480 --> 02:04:23,040 script SL deploy dc. s.o deploy DC 2918 02:04:23,040 --> 02:04:26,079 deployer we're going to say deployer 2919 02:04:26,079 --> 02:04:29,360 equals new deploy DSC like that again 2920 02:04:29,360 --> 02:04:31,920 I'm using a lot of tabs here and we're 2921 02:04:31,920 --> 02:04:35,040 going to say we're going to need the 2922 02:04:35,040 --> 02:04:39,520 import de centralized stablecoin 2923 02:04:39,520 --> 02:04:44,159 from SRC decentralized stablecoin doou 2924 02:04:44,159 --> 02:04:45,760 we're also going to need to import the 2925 02:04:45,760 --> 02:04:50,360 engine the scce from again this is where 2926 02:04:50,360 --> 02:04:52,000 get up co-pilot could really make your 2927 02:04:52,000 --> 02:04:53,760 life a lot easier just being able to hit 2928 02:04:53,760 --> 02:04:56,599 tab here or just whatever AI that you're 2929 02:04:56,599 --> 02:05:00,159 working with now we're going to say DSC 2930 02:05:00,159 --> 02:05:02,559 let's actually make these we'll say 2931 02:05:02,559 --> 02:05:04,480 decentralized table coin 2932 02:05:04,480 --> 02:05:06,679 DSC and DSC 2933 02:05:06,679 --> 02:05:09,679 engine we'll call 2934 02:05:09,679 --> 02:05:12,159 dsce that's confusing you call this like 2935 02:05:12,159 --> 02:05:14,119 engine or something I'm going to call M 2936 02:05:14,119 --> 02:05:19,440 dsce so now our deploy returns DSC and 2937 02:05:19,440 --> 02:05:21,719 the engine so we're going to say return 2938 02:05:21,719 --> 02:05:23,559 DSC 2939 02:05:23,559 --> 02:05:27,400 dsce equals deployer run okay that looks 2940 02:05:27,400 --> 02:05:29,480 pretty good there's a bunch of stuff 2941 02:05:29,480 --> 02:05:31,119 more for us to do but at least we have 2942 02:05:31,119 --> 02:05:33,400 our tests set up here so one of the 2943 02:05:33,400 --> 02:05:35,760 first tests that we want to do is this 2944 02:05:35,760 --> 02:05:38,520 price feed test right we want to make 2945 02:05:38,520 --> 02:05:41,360 sure this get USD value this math that 2946 02:05:41,360 --> 02:05:42,480 we're doing here because we're doing 2947 02:05:42,480 --> 02:05:43,960 some weird math stuff we want to make 2948 02:05:43,960 --> 02:05:46,320 sure this is actually working correctly 2949 02:05:46,320 --> 02:05:48,199 so I'm going to do I'm going to set up a 2950 02:05:48,199 --> 02:05:51,040 little price feeds Test Section price 2951 02:05:51,040 --> 02:05:54,360 tests like this we're going to say 2952 02:05:54,360 --> 02:06:00,119 function do test get USD 2953 02:06:00,119 --> 02:06:02,960 value and here we're going to test our 2954 02:06:02,960 --> 02:06:05,119 get USD value function so it gets past a 2955 02:06:05,119 --> 02:06:07,280 token address and an amount so we're 2956 02:06:07,280 --> 02:06:09,960 going to need to get those tokens that 2957 02:06:09,960 --> 02:06:12,800 we use to deploy this we can get that 2958 02:06:12,800 --> 02:06:14,719 pretty easily from our helper config so 2959 02:06:14,719 --> 02:06:16,119 what we can do actually back in our 2960 02:06:16,119 --> 02:06:19,040 deploy we can also have this return the 2961 02:06:19,040 --> 02:06:22,040 config 2962 02:06:23,199 --> 02:06:24,840 and just at the bottom we'll also have a 2963 02:06:24,840 --> 02:06:26,880 return 2964 02:06:26,880 --> 02:06:30,639 config I'm a config and config will be 2965 02:06:30,639 --> 02:06:34,440 the helper config helper config config 2966 02:06:34,440 --> 02:06:37,559 import that import helper config from 2967 02:06:37,559 --> 02:06:39,320 dot dot 2968 02:06:39,320 --> 02:06:41,840 script fig. 2969 02:06:41,840 --> 02:06:47,760 s.o cool and now we can get the 2970 02:06:47,760 --> 02:06:52,920 we address and we can also get the e USD 2971 02:06:52,920 --> 02:06:55,280 so we'll put those up at the top two 2972 02:06:55,280 --> 02:07:00,440 we'll say address ethusd price feed and 2973 02:07:00,440 --> 02:07:04,000 we'll say address we get this from the 2974 02:07:04,000 --> 02:07:05,920 he helper config so those are the first 2975 02:07:05,920 --> 02:07:10,040 two so it's eth USD price feed bitcoin 2976 02:07:10,040 --> 02:07:12,880 price feed goes here so comma we comma 2977 02:07:12,880 --> 02:07:14,719 this is the Bitcoin token comma this is 2978 02:07:14,719 --> 02:07:18,280 deployer key equals config 2979 02:07:18,280 --> 02:07:22,079 do Active network config 2980 02:07:22,079 --> 02:07:23,559 cool we have the price feed and we have 2981 02:07:23,559 --> 02:07:26,320 we so now we can finally go down here we 2982 02:07:26,320 --> 02:07:28,880 set this function up so we'll say un 256 2983 02:07:28,880 --> 02:07:32,520 eth amount equals let's say there's 15 2984 02:07:32,520 --> 02:07:38,280 eth right 15 eth if we have 15 eth times 2985 02:07:38,280 --> 02:07:39,520 it by 2986 02:07:39,520 --> 02:07:43,559 $2,000 per eth equals what 2987 02:07:43,559 --> 02:07:48,679 30,000 30,000 e 18 right real simple 2988 02:07:48,679 --> 02:07:52,480 simple math so let's do that you 6 2989 02:07:52,480 --> 02:07:57,920 expected USD equals 3 301 2 3 2990 02:07:57,920 --> 02:08:01,800 $30,000 and we'll say U into 256 actual 2991 02:08:01,800 --> 02:08:08,760 USD equals dsce doget USD value we and 2992 02:08:08,760 --> 02:08:11,520 eth amount and the reason this should 2993 02:08:11,520 --> 02:08:13,440 work is because in our engine we pass 2994 02:08:13,440 --> 02:08:16,400 the token and the amount and internally 2995 02:08:16,400 --> 02:08:18,719 it uses the price feed associated with 2996 02:08:18,719 --> 02:08:21,679 that token calls the price to get the 2997 02:08:21,679 --> 02:08:23,760 amount and now we should be able to just 2998 02:08:23,760 --> 02:08:27,599 do assert equals expected 2999 02:08:27,599 --> 02:08:31,360 USD and actual USD all right I know 3000 02:08:31,360 --> 02:08:32,599 there's a lot of setup just to write 3001 02:08:32,599 --> 02:08:34,360 this one test but like I said I like 3002 02:08:34,360 --> 02:08:36,440 making sure my deploy scripts are part 3003 02:08:36,440 --> 02:08:38,520 of my test Suite right from beginning 3004 02:08:38,520 --> 02:08:40,199 but it might be a good idea to just do 3005 02:08:40,199 --> 02:08:43,840 them as integration test so Forge test- 3006 02:08:43,840 --> 02:08:46,920 m test get USD 3007 02:08:46,920 --> 02:08:50,239 value and it works now I will point out 3008 02:08:50,239 --> 02:08:51,920 the first couple of times that I ran 3009 02:08:51,920 --> 02:08:55,159 this test I actually failed miserably I 3010 02:08:55,159 --> 02:08:56,840 got a number of things wrong and that's 3011 02:08:56,840 --> 02:08:58,360 okay because you will and that's why you 3012 02:08:58,360 --> 02:09:00,679 write tests so I also while I'm here 3013 02:09:00,679 --> 02:09:03,280 let's also write at least one deposit 3014 02:09:03,280 --> 02:09:06,320 collateral test so let me copy this 3015 02:09:06,320 --> 02:09:07,679 paste it here because we're going to 3016 02:09:07,679 --> 02:09:10,559 write a lot of deposit collateral D 3017 02:09:10,559 --> 02:09:12,360 deposit 3018 02:09:12,360 --> 02:09:15,239 collateral test make it look a little 3019 02:09:15,239 --> 02:09:17,960 bit pretty at least 3020 02:09:17,960 --> 02:09:20,840 that we can do some more simple tests 3021 02:09:20,840 --> 02:09:25,800 like function test revert reverts if 3022 02:09:25,800 --> 02:09:28,760 collateral zero 3023 02:09:28,760 --> 02:09:32,040 public uh we'll prank a user so up at 3024 02:09:32,040 --> 02:09:35,280 the top we'll do an address public user 3025 02:09:35,280 --> 02:09:38,639 equals make ADR 3026 02:09:38,639 --> 02:09:42,920 user like this user 3027 02:09:42,920 --> 02:09:44,840 Capital 3028 02:09:44,840 --> 02:09:51,480 user let say vm. start prank or user now 3029 02:09:51,480 --> 02:09:54,719 we'll at least approve the token can go 3030 02:09:54,719 --> 02:10:00,119 to the protocol so we'll do erc20 mock W 3031 02:10:00,119 --> 02:10:01,960 do we have that imported nope we're 3032 02:10:01,960 --> 02:10:03,679 going to have to import that import 3033 02:10:03,679 --> 02:10:09,320 erc20 mock from atop Zeppelin SLC 3034 02:10:09,320 --> 02:10:14,199 contracts slash what is it MOX slash or 3035 02:10:14,199 --> 02:10:18,520 contracts MOX erc20 mock okay ec20 mack. 3036 02:10:18,520 --> 02:10:24,079 Soul ec20 mock we do 3037 02:10:24,079 --> 02:10:27,320 approve uh address 3038 02:10:27,320 --> 02:10:31,159 dce some amount let's do at the top 3039 02:10:31,159 --> 02:10:36,639 let's make another U 256 public constant 3040 02:10:36,639 --> 02:10:38,520 amount 3041 02:10:38,520 --> 02:10:42,800 collateral equals let say 10 ether worth 3042 02:10:42,800 --> 02:10:43,639 of 3043 02:10:43,639 --> 02:10:46,639 collateral down here we'll approve that 3044 02:10:46,639 --> 02:10:50,599 10 collateral and then we'll do vm. 3045 02:10:50,599 --> 02:10:52,360 expect 3046 02:10:52,360 --> 02:10:56,840 revert with DSC engine dot we're going 3047 02:10:56,840 --> 02:10:58,920 to need to use that needs more than zero 3048 02:10:58,920 --> 02:11:01,199 in here needs more than 3049 02:11:01,199 --> 02:11:03,960 zero do selector and now you guys know 3050 02:11:03,960 --> 02:11:07,920 what the selector bit means dsce do 3051 02:11:07,920 --> 02:11:10,760 deposit colat 3052 02:11:10,760 --> 02:11:15,880 collateral say we zero this and then vm. 3053 02:11:15,880 --> 02:11:18,440 stop Rank and actually this might fail 3054 02:11:18,440 --> 02:11:20,280 for a different reason but let's go 3055 02:11:20,280 --> 02:11:24,280 ahead and try it Forge test DM 3056 02:11:25,520 --> 02:11:27,840 oops okay cool and this actually did 3057 02:11:27,840 --> 02:11:29,599 pass now if we want to make this a 3058 02:11:29,599 --> 02:11:31,440 little bit better of a test we should 3059 02:11:31,440 --> 02:11:35,880 also mint our user some weth and we 3060 02:11:35,880 --> 02:11:36,880 probably should do that right in the 3061 02:11:36,880 --> 02:11:38,159 setup so we don't have to do that for 3062 02:11:38,159 --> 02:11:41,760 every single test what I'm going to do 3063 02:11:41,760 --> 02:11:44,960 is I'm going to do erc20 mock 3064 02:11:44,960 --> 02:11:49,920 we mint user we're going to do a 2 into 3065 02:11:49,920 --> 02:11:55,520 256 a constant starting here see 3066 02:11:55,520 --> 02:11:57,199 20 3067 02:11:57,199 --> 02:11:58,719 balance 3068 02:11:58,719 --> 02:12:00,480 balance 3069 02:12:00,480 --> 02:12:02,960 equals and let's say this is 10 ether as 3070 02:12:02,960 --> 02:12:06,920 well 10 ether starting year20 balance 3071 02:12:06,920 --> 02:12:11,239 boom all right cool so now Forge test 3072 02:12:11,239 --> 02:12:13,239 all of our tests are passing cool and 3073 02:12:13,239 --> 02:12:14,679 like I said I'll do this kind of as a 3074 02:12:14,679 --> 02:12:16,679 sanity check to make sure that my 3075 02:12:16,679 --> 02:12:19,639 architecture is even making sense right 3076 02:12:19,639 --> 02:12:22,159 so we what we probably want to do next 3077 02:12:22,159 --> 02:12:24,960 then is have a test for collateral is 3078 02:12:24,960 --> 02:12:27,639 being deposited in these data structures 3079 02:12:27,639 --> 02:12:30,000 but for now I'm content with these tests 3080 02:12:30,000 --> 02:12:31,599 so I'm just going to go back to writing 3081 02:12:31,599 --> 02:12:34,679 my contracts like I said there's no one 3082 02:12:34,679 --> 02:12:36,920 single process and I don't think I've 3083 02:12:36,920 --> 02:12:40,239 ever written a smart contract completely 3084 02:12:40,239 --> 02:12:42,320 in one go I'm pretty much always writing 3085 02:12:42,320 --> 02:12:44,880 tests as I'm writing the code so it is a 3086 02:12:44,880 --> 02:12:47,760 really good idea to to do this yes you 3087 02:12:47,760 --> 02:12:50,079 do not have to write the deploy script 3088 02:12:50,079 --> 02:12:51,239 as you're writing your code but 3089 02:12:51,239 --> 02:12:53,079 something that I like to do and then you 3090 02:12:53,079 --> 02:12:54,239 know what while we're writing these 3091 02:12:54,239 --> 02:12:59,719 tests let's also do D- Fork URL sepolia 3092 02:12:59,719 --> 02:13:03,840 RPC URL let's also do this because this 3093 02:13:03,840 --> 02:13:07,199 probably will fail actually because we 3094 02:13:07,199 --> 02:13:10,280 can't just mint we at a thin air and we 3095 02:13:10,280 --> 02:13:13,040 do indeed fail test get USD value oh 3096 02:13:13,040 --> 02:13:14,679 interesting that's the one that fails 3097 02:13:14,679 --> 02:13:17,360 test get USD value ah this one fails 3098 02:13:17,360 --> 02:13:20,040 because we're hardcoding the expected 3099 02:13:20,040 --> 02:13:23,119 USD right here here and of course the 3100 02:13:23,119 --> 02:13:25,440 price on sapoia is the actual price as 3101 02:13:25,440 --> 02:13:26,800 opposed to kind of this fake price that 3102 02:13:26,800 --> 02:13:28,880 we're making up so we should probably 3103 02:13:28,880 --> 02:13:31,920 update this test to make it more 3104 02:13:31,920 --> 02:13:33,880 agnostic right we probably should update 3105 02:13:33,880 --> 02:13:34,880 this test so that instead of just 3106 02:13:34,880 --> 02:13:37,599 hardcoding 3,000 in here update this 3107 02:13:37,599 --> 02:13:39,159 test to use the price of the actual 3108 02:13:39,159 --> 02:13:41,040 price feed for now I'm going to leave it 3109 02:13:41,040 --> 02:13:42,960 as is and then I can fix it later for 3110 02:13:42,960 --> 02:13:45,679 running those Fork 3111 02:13:46,960 --> 02:13:49,599 tests so where are we now okay so we 3112 02:13:49,599 --> 02:13:52,360 have a way to deposit collateral we have 3113 02:13:52,360 --> 02:13:54,599 a way to Mint we don't really have too 3114 02:13:54,599 --> 02:13:56,679 many tests here we're just assuming that 3115 02:13:56,679 --> 02:13:59,119 this kind of works for now which is okay 3116 02:13:59,119 --> 02:14:00,840 but this is good right we're getting 3117 02:14:00,840 --> 02:14:05,000 somewhere so we can mint our debt or our 3118 02:14:05,000 --> 02:14:07,679 DSC we can actually now we can get a 3119 02:14:07,679 --> 02:14:09,280 whole lot of information as well which 3120 02:14:09,280 --> 02:14:12,719 is awesome let's now combine these two 3121 02:14:12,719 --> 02:14:14,280 into kind of this main function that 3122 02:14:14,280 --> 02:14:15,520 we're thinking a lot of people are going 3123 02:14:15,520 --> 02:14:19,320 to call this deposit collateral in mint 3124 02:14:19,320 --> 02:14:21,760 USD right the purpose of this protocol 3125 02:14:21,760 --> 02:14:24,360 is to Mint this stable coin right 3126 02:14:24,360 --> 02:14:26,599 deposit collateral and mint DSC which is 3127 02:14:26,599 --> 02:14:28,400 just going to be the combination of 3128 02:14:28,400 --> 02:14:31,079 deposit collateral and mint DSC so in 3129 02:14:31,079 --> 02:14:33,280 here what this this is going to take 3130 02:14:33,280 --> 02:14:36,040 it's going to take similar stuff to 3131 02:14:36,040 --> 02:14:40,239 deposit collateral and address token 3132 02:14:40,239 --> 02:14:45,119 collateral address a u into 256 amount 3133 02:14:45,119 --> 02:14:48,880 collateral and then also a un 256 amount 3134 02:14:48,880 --> 02:14:51,760 DSC to Mint 3135 02:14:51,760 --> 02:14:53,800 right that and here we're just going to 3136 02:14:53,800 --> 02:14:56,559 call so deposit collateral is external 3137 02:14:56,559 --> 02:14:58,880 right now we'll make this a public 3138 02:14:58,880 --> 02:15:03,800 function so we'll change this to deposit 3139 02:15:03,800 --> 02:15:06,760 collateral or we give it the token 3140 02:15:06,760 --> 02:15:09,119 collateral address and the amount 3141 02:15:09,119 --> 02:15:13,119 collateral and then we'll call Mint DSC 3142 02:15:13,119 --> 02:15:15,920 amount DSC to Mint boom so it's all this 3143 02:15:15,920 --> 02:15:17,520 function does is just combines the two 3144 02:15:17,520 --> 02:15:19,079 of them oh and mint 3145 02:15:19,079 --> 02:15:23,360 DSC mint DSC is not defined because it's 3146 02:15:23,360 --> 02:15:25,639 external we'll make this public as well 3147 02:15:25,639 --> 02:15:28,119 so that our contract can also call it 3148 02:15:28,119 --> 02:15:30,199 toggle word rep put that back on okay 3149 02:15:30,199 --> 02:15:31,440 and cool so this is going to be one of 3150 02:15:31,440 --> 02:15:33,520 our main functions we're thinking so 3151 02:15:33,520 --> 02:15:36,400 let's add some Nat spec to it we'll say 3152 02:15:36,400 --> 02:15:40,360 at param this is where get co-pilot is 3153 02:15:40,360 --> 02:15:42,040 really helpful token collateral address 3154 02:15:42,040 --> 02:15:43,679 the address of the token to deposit as 3155 02:15:43,679 --> 02:15:47,320 collateral at Pam amount collateral yep 3156 02:15:47,320 --> 02:15:49,960 at Pam that looks good too and then 3157 02:15:49,960 --> 02:15:51,559 we'll at noce 3158 02:15:51,559 --> 02:15:55,639 this function will deposit your 3159 02:15:55,639 --> 02:16:00,040 collateral and and mint DSC in one 3160 02:16:00,040 --> 02:16:03,280 trans action right because otherwise 3161 02:16:03,280 --> 02:16:04,639 we're going to have to have people call 3162 02:16:04,639 --> 02:16:06,719 deposit collateral and then mint but 3163 02:16:06,719 --> 02:16:07,760 some people they're probably just going 3164 02:16:07,760 --> 02:16:09,719 to want to do both at the same time 3165 02:16:09,719 --> 02:16:11,000 because that's kind of the purpose of 3166 02:16:11,000 --> 02:16:13,159 this 3167 02:16:14,520 --> 02:16:16,800 protocol okay great so we have a way to 3168 02:16:16,800 --> 02:16:19,159 for people to get money in how do they 3169 02:16:19,159 --> 02:16:21,119 get their money out so we're going to 3170 02:16:21,119 --> 02:16:23,119 need to write this redeem collateral 3171 02:16:23,119 --> 02:16:25,320 right so in order for them to redeem 3172 02:16:25,320 --> 02:16:27,880 collateral let's talk about this in 3173 02:16:27,880 --> 02:16:31,760 order to redeem collateral they need 3174 02:16:31,760 --> 02:16:35,040 what one their health 3175 02:16:35,040 --> 02:16:40,160 Factor must be over one after collateral 3176 02:16:40,160 --> 02:16:41,960 pulled so we're going to want to put 3177 02:16:41,960 --> 02:16:43,319 some checks in here to make sure that 3178 02:16:43,319 --> 02:16:45,200 they have enough money in here and 3179 02:16:45,200 --> 02:16:47,000 that's kind of the main thing right 3180 02:16:47,000 --> 02:16:49,120 that's all we really need to worry about 3181 02:16:49,120 --> 02:16:52,200 so let's go ahead start right this so 3182 02:16:52,200 --> 02:16:54,040 first we should let them choose which 3183 02:16:54,040 --> 02:16:57,479 collateral they want so address token 3184 02:16:57,479 --> 02:17:00,479 collateral address and then obviously 3185 02:17:00,479 --> 02:17:02,920 the amount amount 3186 02:17:02,920 --> 02:17:04,840 collateral and we're going to want to 3187 02:17:04,840 --> 02:17:07,760 add this more than zero modifier in here 3188 02:17:07,760 --> 02:17:09,160 for the amount collateral we don't want 3189 02:17:09,160 --> 02:17:11,679 them to be sending accidental zero 3190 02:17:11,679 --> 02:17:14,120 transactions and because we're going to 3191 02:17:14,120 --> 02:17:16,359 be moving tokens around we'll just do 3192 02:17:16,359 --> 02:17:18,719 non- reentrance better safe than sorry 3193 02:17:18,719 --> 02:17:20,160 we can figure out later in kind of like 3194 02:17:20,160 --> 02:17:22,719 a gas off it if this is even needed now 3195 02:17:22,719 --> 02:17:25,120 I'm going to write this function as if 3196 02:17:25,120 --> 02:17:26,599 somebody redeeming collateral is the 3197 02:17:26,599 --> 02:17:28,000 only time they actually redeem 3198 02:17:28,000 --> 02:17:29,639 collateral however we're going to 3199 02:17:29,639 --> 02:17:32,000 refactor this in the future to make our 3200 02:17:32,000 --> 02:17:34,960 code much more modular there's this 3201 02:17:34,960 --> 02:17:37,519 concept in computer science called dry 3202 02:17:37,519 --> 02:17:40,559 don't repeat yourself if you find 3203 02:17:40,559 --> 02:17:42,200 yourself coding the same thing that that 3204 02:17:42,200 --> 02:17:43,280 should send off a light bulb in your 3205 02:17:43,280 --> 02:17:45,559 head going oh maybe what I'm doing isn't 3206 02:17:45,559 --> 02:17:47,760 the best practice so we're going to code 3207 02:17:47,760 --> 02:17:50,719 this one way and then I'm telling you 3208 02:17:50,719 --> 02:17:51,760 right right now we're going to go back 3209 02:17:51,760 --> 02:17:53,240 and we're going to refactor this in the 3210 02:17:53,240 --> 02:17:55,319 future but I want to code it this one 3211 02:17:55,319 --> 02:17:58,359 way first just to show you the process 3212 02:17:58,359 --> 02:18:00,120 that you'll probably go through and how 3213 02:18:00,120 --> 02:18:01,519 you'll probably refactor it when you 3214 02:18:01,519 --> 02:18:03,760 come across this yourself so we're code 3215 02:18:03,760 --> 02:18:05,280 this one way now let's do it we're going 3216 02:18:05,280 --> 02:18:07,120 to need to pull the collateral out and 3217 02:18:07,120 --> 02:18:08,399 we're going to update our internal 3218 02:18:08,399 --> 02:18:10,240 accounting so we have this 3219 02:18:10,240 --> 02:18:13,200 sore collateral deposited of message. 3220 02:18:13,200 --> 02:18:17,000 sender of the token collateral address 3221 02:18:17,000 --> 02:18:18,359 right this is the our internal 3222 02:18:18,359 --> 02:18:20,319 accounting how much collateral they've 3223 02:18:20,319 --> 02:18:23,120 added we're going to do minus equals 3224 02:18:23,120 --> 02:18:24,960 amount collateral so this is assuming 3225 02:18:24,960 --> 02:18:26,880 we're going to pull it out if they try 3226 02:18:26,880 --> 02:18:29,200 to pull out more than what they have 3227 02:18:29,200 --> 02:18:31,359 we're relying on the solidity compiler a 3228 02:18:31,359 --> 02:18:34,000 little bit to throw an error right if in 3229 02:18:34,000 --> 02:18:35,439 their balance they have 100 and they 3230 02:18:35,439 --> 02:18:38,160 subtract try to pull out 1,000 right 3231 02:18:38,160 --> 02:18:41,760 it'll revert because as of newer 3232 02:18:41,760 --> 02:18:43,280 versions of solidity they don't let you 3233 02:18:43,280 --> 02:18:45,960 do this unsafe math stuff which is 3234 02:18:45,960 --> 02:18:48,639 awesome it saved us a lot of hassle so 3235 02:18:48,639 --> 02:18:50,479 and then since we're updating State 3236 02:18:50,479 --> 02:18:52,920 we're we're going to emit an event let's 3237 02:18:52,920 --> 02:18:53,920 call it 3238 02:18:53,920 --> 02:18:55,559 collateral 3239 02:18:55,559 --> 02:18:59,120 redeemed we'll say it's message. sender 3240 02:18:59,120 --> 02:19:05,040 so from m. sender the amount collateral 3241 02:19:05,040 --> 02:19:06,880 uh the token 3242 02:19:06,880 --> 02:19:10,719 collateral address like this so we're 3243 02:19:10,719 --> 02:19:12,920 going to go to the top we're also going 3244 02:19:12,920 --> 02:19:15,120 to refactor this event but you'll 3245 02:19:15,120 --> 02:19:16,880 understand why later event collateral 3246 02:19:16,880 --> 02:19:21,880 redeemed address indexed user address 3247 02:19:21,880 --> 02:19:27,960 indexed token uint 256 indexed amount 3248 02:19:27,960 --> 02:19:30,800 like this okay control minus go right 3249 02:19:30,800 --> 02:19:33,000 back down to where we were okay amount 3250 02:19:33,000 --> 02:19:35,559 collateral oh what's wrong with this oh 3251 02:19:35,559 --> 02:19:37,880 these are 3252 02:19:40,120 --> 02:19:44,000 backwards okay cool now all we have to 3253 02:19:44,000 --> 02:19:48,359 do is return the money well how do we do 3254 02:19:48,359 --> 02:19:52,280 this so we want to follow CI right 3255 02:19:52,280 --> 02:19:54,960 checks effects interactions checks 3256 02:19:54,960 --> 02:19:58,280 effects interactions but we also want to 3257 02:19:58,280 --> 02:19:59,920 make sure the health factor is good 3258 02:19:59,920 --> 02:20:03,720 after collateral collateral is pulled 3259 02:20:03,720 --> 02:20:05,600 and this is where sometimes you'll see 3260 02:20:05,600 --> 02:20:08,000 CI be violated when I need to check 3261 02:20:08,000 --> 02:20:10,040 something after a token transfer has 3262 02:20:10,040 --> 02:20:13,040 happened sometimes you'll see this CI be 3263 02:20:13,040 --> 02:20:15,840 violated a little bit and what you could 3264 02:20:15,840 --> 02:20:19,680 do is you could do like calculate health 3265 02:20:19,680 --> 02:20:23,319 Factor after and then like simulate it 3266 02:20:23,319 --> 02:20:24,840 but a lot of people choose to not do 3267 02:20:24,840 --> 02:20:26,160 this because this is really gas 3268 02:20:26,160 --> 02:20:28,280 inefficient so what a lot of people do 3269 02:20:28,280 --> 02:20:30,800 is they just go ahead with the doing the 3270 02:20:30,800 --> 02:20:34,359 token transfer first and then checking 3271 02:20:34,359 --> 02:20:37,000 this and reverting if this happens and 3272 02:20:37,000 --> 02:20:40,000 that's usually fine though because we're 3273 02:20:40,000 --> 02:20:41,760 going to revert the transaction if it's 3274 02:20:41,760 --> 02:20:44,439 bad right so what we'll do is we'll do 3275 02:20:44,439 --> 02:20:46,000 this token transfer and then we'll make 3276 02:20:46,000 --> 02:20:48,280 sure the health factor is okay so you 3277 02:20:48,280 --> 02:20:49,880 know how to move tokens around so we'll 3278 02:20:49,880 --> 02:20:51,640 say bull success 3279 02:20:51,640 --> 02:20:55,640 equals ier 20 token collateral 3280 02:20:55,640 --> 02:20:58,319 collateral address dot we can just do 3281 02:20:58,319 --> 02:21:01,240 transfer instead of transfer from since 3282 02:21:01,240 --> 02:21:03,120 transfer is when you transfer from 3283 02:21:03,120 --> 02:21:04,920 yourself transfer from is when you 3284 02:21:04,920 --> 02:21:08,439 transfer from somebody else so transfer 3285 02:21:08,439 --> 02:21:10,319 and we're going to be sending it to 3286 02:21:10,319 --> 02:21:12,560 message. sender and we're going to send 3287 02:21:12,560 --> 02:21:15,760 amount collateral and then if not 3288 02:21:15,760 --> 02:21:18,160 success if if not success we're just 3289 02:21:18,160 --> 02:21:21,399 going to go ahead and revert DSC engine 3290 02:21:21,399 --> 02:21:26,240 we'll just do transfer failed like this 3291 02:21:26,240 --> 02:21:28,680 and then we want to make sure that the 3292 02:21:28,680 --> 02:21:30,920 health Factor isn't broken and we have 3293 02:21:30,920 --> 02:21:32,840 written a function that does that 3294 02:21:32,840 --> 02:21:35,520 already called revert if Health factor 3295 02:21:35,520 --> 02:21:38,520 is broken so we can just grab this go 3296 02:21:38,520 --> 02:21:40,680 back up to our redeem and just do revert 3297 02:21:40,680 --> 02:21:42,120 of Health factors broken for the 3298 02:21:42,120 --> 02:21:43,560 message. 3299 02:21:43,560 --> 02:21:45,920 sender okay cool like I said we're going 3300 02:21:45,920 --> 02:21:48,880 to refactor this very soon okay but it 3301 02:21:48,880 --> 02:21:51,040 looks like this is actually pretty good 3302 02:21:51,040 --> 02:21:53,960 for redeeming collateral now this 3303 02:21:53,960 --> 02:21:55,960 revertive health factor is broken is a 3304 02:21:55,960 --> 02:21:58,080 little bit Troublesome with just this 3305 02:21:58,080 --> 02:21:59,800 raw redeem collateral let's say I put 3306 02:21:59,800 --> 02:22:03,520 $100 in and then I mint let's say $20 3307 02:22:03,520 --> 02:22:06,560 worth of DSC put $100 worth of Ethan and 3308 02:22:06,560 --> 02:22:09,479 I mint $20 worth of DSC let's say I'm 3309 02:22:09,479 --> 02:22:12,080 done like I want to burn all my DSC and 3310 02:22:12,080 --> 02:22:15,520 I want to withdraw all of my eth well if 3311 02:22:15,520 --> 02:22:18,080 I try to redeem all my eth it'll break 3312 02:22:18,080 --> 02:22:20,760 right it'll break my health Factor so 3313 02:22:20,760 --> 02:22:22,760 what I need to do first is I need to 3314 02:22:22,760 --> 02:22:26,800 First burn back my DSC and then I need 3315 02:22:26,800 --> 02:22:29,720 to redeem eth so it's a kind of this two 3316 02:22:29,720 --> 02:22:32,520 transaction process here this kind of 3317 02:22:32,520 --> 02:22:34,399 stinks so let's turn it into a one 3318 02:22:34,399 --> 02:22:35,960 transaction process so we're going to 3319 02:22:35,960 --> 02:22:38,760 combine redeeming your collateral with 3320 02:22:38,760 --> 02:22:41,680 also burning your DSC which means we're 3321 02:22:41,680 --> 02:22:43,319 going to need to create a burn DSC 3322 02:22:43,319 --> 02:22:44,960 function and we're also going to 3323 02:22:44,960 --> 02:22:47,800 refactor this in a little bit but I'm 3324 02:22:47,800 --> 02:22:49,319 just going to write it as if this is the 3325 02:22:49,319 --> 02:22:53,479 only burn D function for now so let's 3326 02:22:53,479 --> 02:22:55,359 have people burn their DSC right this is 3327 02:22:55,359 --> 02:22:57,800 when they they say hey I'm done with 3328 02:22:57,800 --> 02:23:01,319 these tokens and this will reduce that 3329 02:23:01,319 --> 02:23:03,840 if we scroll up to the top we have this 3330 02:23:03,840 --> 02:23:06,840 mapping here SD cemented it'll reduce 3331 02:23:06,840 --> 02:23:09,880 this sdsc minted so essentially it'll 3332 02:23:09,880 --> 02:23:13,560 reduce their debt in the system so burn 3333 02:23:13,560 --> 02:23:16,200 DSC we're going to add some modifiers 3334 02:23:16,200 --> 02:23:20,560 here this should be more than zero 3335 02:23:20,560 --> 02:23:23,240 amount so we want to do a unit 256 3336 02:23:23,240 --> 02:23:25,640 amount so they can burn as much as they 3337 02:23:25,640 --> 02:23:27,600 want and then what we're going to want 3338 02:23:27,600 --> 02:23:31,600 to do is we're going to say sore DSC 3339 02:23:31,600 --> 02:23:35,240 minted of the message. sender is going 3340 02:23:35,240 --> 02:23:38,040 to minus equal amount so we're going to 3341 02:23:38,040 --> 02:23:41,439 remove that debt remove that DSC minted 3342 02:23:41,439 --> 02:23:45,120 then we're going to do a little bu 3343 02:23:45,120 --> 02:23:47,840 success equals iore 3344 02:23:47,840 --> 02:23:51,399 DSC transfer from 3345 02:23:51,399 --> 02:23:57,120 D message. sender to address this amount 3346 02:23:57,120 --> 02:23:58,920 and we could also send this to the Zer 3347 02:23:58,920 --> 02:24:00,399 address but we're going to just send it 3348 02:24:00,399 --> 02:24:03,760 to our address for now because the 3349 02:24:03,760 --> 02:24:07,600 decentralized stable coin erc20 burnable 3350 02:24:07,600 --> 02:24:09,880 has its own burn function and we're just 3351 02:24:09,880 --> 02:24:11,479 going to call the burn function directly 3352 02:24:11,479 --> 02:24:13,600 on the token itself but first we're 3353 02:24:13,600 --> 02:24:15,800 going to take it from them bring it into 3354 02:24:15,800 --> 02:24:16,880 our contract and then we're going to 3355 02:24:16,880 --> 02:24:21,920 burn it so if not success then we'll 3356 02:24:21,920 --> 02:24:24,040 revert 3357 02:24:24,040 --> 02:24:28,399 dsce transfer failed this and this 3358 02:24:28,399 --> 02:24:30,040 conditional is kind of hypothetically 3359 02:24:30,040 --> 02:24:32,000 unreachable because if the transfer 3360 02:24:32,000 --> 02:24:34,160 fails up here we're going to throw the 3361 02:24:34,160 --> 02:24:36,800 transfer from fail error but let's say 3362 02:24:36,800 --> 02:24:39,080 this DSC token is implemented wrong 3363 02:24:39,080 --> 02:24:41,399 great we kind of have this backup but so 3364 02:24:41,399 --> 02:24:43,600 they're going to send their DSC token 3365 02:24:43,600 --> 02:24:45,920 here then we're going to call IOD DSC do 3366 02:24:45,920 --> 02:24:49,760 burn amount now since we're burning DSC 3367 02:24:49,760 --> 02:24:54,200 question is do we need to check if this 3368 02:24:54,200 --> 02:24:58,080 breaks Health Factor well probably not 3369 02:24:58,080 --> 02:24:59,479 right because we're burning DSC we're 3370 02:24:59,479 --> 02:25:02,000 burning debt it's highly unlikely that 3371 02:25:02,000 --> 02:25:04,080 burning your debt removing your debt is 3372 02:25:04,080 --> 02:25:06,800 going to break the health Factor right 3373 02:25:06,800 --> 02:25:09,680 we probably don't ever need this but I'm 3374 02:25:09,680 --> 02:25:11,760 just going to add this in here for 3375 02:25:11,760 --> 02:25:16,120 now just as a backup in a gas audit we 3376 02:25:16,120 --> 02:25:18,279 can figure out if we actually need it I 3377 02:25:18,279 --> 02:25:22,960 don't think this will would ever hit and 3378 02:25:22,960 --> 02:25:25,040 this is where when I do go to an audit 3379 02:25:25,040 --> 02:25:28,359 when I do go to a security professional 3380 02:25:28,359 --> 02:25:29,880 I can make sure to point this line out 3381 02:25:29,880 --> 02:25:31,200 say hey I don't think this line will 3382 02:25:31,200 --> 02:25:33,200 ever hit and I'm thinking of pulling it 3383 02:25:33,200 --> 02:25:35,880 out what do you think right it's good to 3384 02:25:35,880 --> 02:25:37,200 call these out in your comments that 3385 02:25:37,200 --> 02:25:38,880 when you do go to a professional who 3386 02:25:38,880 --> 02:25:41,520 knows they can help you out figure this 3387 02:25:41,520 --> 02:25:42,840 out so for now we're going to put it in 3388 02:25:42,840 --> 02:25:44,399 here although it's highly likely we 3389 02:25:44,399 --> 02:25:46,600 don't even need this and we're going to 3390 02:25:46,600 --> 02:25:47,960 refactor this function pretty soon 3391 02:25:47,960 --> 02:25:50,520 anyways so we have this burn DSC fun 3392 02:25:50,520 --> 02:25:52,920 function we're going to make it public 3393 02:25:52,920 --> 02:25:55,479 because we're going to be burning DSC 3394 02:25:55,479 --> 02:25:56,880 and redeeming collateral at the same 3395 02:25:56,880 --> 02:25:59,720 time so now we have redeem collateral we 3396 02:25:59,720 --> 02:26:02,040 have burn DSC now we can write this 3397 02:26:02,040 --> 02:26:05,120 redeem collateral for DSC where we send 3398 02:26:05,120 --> 02:26:07,520 DSC and redeem collateral at the same 3399 02:26:07,520 --> 02:26:09,600 time and so in here we're going to say 3400 02:26:09,600 --> 02:26:12,000 address token 3401 02:26:12,000 --> 02:26:13,840 colateral 3402 02:26:13,840 --> 02:26:16,000 address U 3403 02:26:16,000 --> 02:26:18,680 256 amount 3404 02:26:18,680 --> 02:26:24,120 collateral U 56 amount DSC to burn we'll 3405 02:26:24,120 --> 02:26:26,439 have this be external so then we're 3406 02:26:26,439 --> 02:26:28,840 going to call burn DSC with the amount 3407 02:26:28,840 --> 02:26:31,600 of DSC to burn we're also going to call 3408 02:26:31,600 --> 02:26:34,160 redeem collateral so we're going to burn 3409 02:26:34,160 --> 02:26:36,359 the DSC first then we're going to redeem 3410 02:26:36,359 --> 02:26:37,960 their collateral with the 3411 02:26:37,960 --> 02:26:41,520 token collateral address and the amount 3412 02:26:41,520 --> 02:26:44,640 collateral redeem 3413 02:26:44,640 --> 02:26:46,800 collateral oh this is external let's 3414 02:26:46,800 --> 02:26:49,960 make this public let's go back and then 3415 02:26:49,960 --> 02:26:52,240 of course of course we should revert if 3416 02:26:52,240 --> 02:26:54,040 Health factor is broken but if we look 3417 02:26:54,040 --> 02:26:56,200 our redeemed collateral function 3418 02:26:56,200 --> 02:26:57,840 currently does this already so we don't 3419 02:26:57,840 --> 02:26:59,800 need to do that here so I'm just going 3420 02:26:59,800 --> 02:27:03,040 to put this comment redeem 3421 02:27:03,040 --> 02:27:07,479 collateral already checks Health Factor 3422 02:27:07,479 --> 02:27:09,040 right here then we'll add a little bit 3423 02:27:09,040 --> 02:27:13,160 of natspec here so at Pam boom that's 3424 02:27:13,160 --> 02:27:15,600 not even the right Pam collateral 3425 02:27:15,600 --> 02:27:17,880 address the 3426 02:27:17,880 --> 02:27:20,960 collateral address to red 3427 02:27:20,960 --> 02:27:23,439 RAM amount collateral the the amount 3428 02:27:23,439 --> 02:27:25,840 collateral to redeem RAM amount DC to 3429 02:27:25,840 --> 02:27:28,279 burn the amount of DSC to burn this 3430 02:27:28,279 --> 02:27:35,279 function Burns DC and redeems underlying 3431 02:27:35,279 --> 02:27:37,920 collateral in one 3432 02:27:37,920 --> 02:27:40,240 transaction okay cool are we going to 3433 02:27:40,240 --> 02:27:42,520 refactor these two functions soon yes 3434 02:27:42,520 --> 02:27:44,880 absolutely but I want you to understand 3435 02:27:44,880 --> 02:27:46,720 why we're going to reactor them so we're 3436 02:27:46,720 --> 02:27:48,680 going to leave them in as they are for 3437 02:27:48,680 --> 02:27:51,680 now 3438 02:27:52,960 --> 02:27:56,399 Okay cool so this is looking pretty good 3439 02:27:56,399 --> 02:27:59,479 so we have a lot of stuff in here uh we 3440 02:27:59,479 --> 02:28:02,479 have deposit collateral and mint DSC so 3441 02:28:02,479 --> 02:28:04,160 people can mint our stable coin by 3442 02:28:04,160 --> 02:28:06,200 depositing collateral people can just 3443 02:28:06,200 --> 02:28:08,240 straight up deposit collateral people 3444 02:28:08,240 --> 02:28:10,000 can then redeem their collateral for the 3445 02:28:10,000 --> 02:28:12,880 US for the DSC that they minted they can 3446 02:28:12,880 --> 02:28:14,439 just straight up redeem collateral they 3447 02:28:14,439 --> 02:28:17,000 can just straight up mint DSC so long as 3448 02:28:17,000 --> 02:28:19,080 they didn't break the health Factor they 3449 02:28:19,080 --> 02:28:20,880 can burn DSC to to go help their health 3450 02:28:20,880 --> 02:28:22,520 factor and I don't think this line will 3451 02:28:22,520 --> 02:28:24,600 ever hit we've got to do a couple of 3452 02:28:24,600 --> 02:28:26,600 more things here most importantly we got 3453 02:28:26,600 --> 02:28:29,560 to do this liquidate function so this 3454 02:28:29,560 --> 02:28:31,479 liquidate function is kind of the the 3455 02:28:31,479 --> 02:28:34,040 key thing that holds this whole system 3456 02:28:34,040 --> 02:28:36,640 together if we do start nearing under 3457 02:28:36,640 --> 02:28:38,439 collateralization we need someone to 3458 02:28:38,439 --> 02:28:40,160 start liquidating positions removing 3459 02:28:40,160 --> 02:28:41,600 those positions we need somebody to 3460 02:28:41,600 --> 02:28:45,560 basically call redeem and burn for you 3461 02:28:45,560 --> 02:28:47,560 if your health Factor becomes too poor 3462 02:28:47,560 --> 02:28:49,160 right because the worst thing that would 3463 02:28:49,160 --> 02:28:53,600 happen is let's say there's $10000 worth 3464 02:28:53,600 --> 02:28:57,840 of eth backing $50 worth of DSC and then 3465 02:28:57,840 --> 02:29:02,000 the price of eth Tanks to $20 right $20 3466 02:29:02,000 --> 02:29:06,720 of eth backing $50 of DC well now the 3467 02:29:06,720 --> 02:29:11,600 DSC isn't worth $1 right the DSC then is 3468 02:29:11,600 --> 02:29:13,120 going to be worth you know whatever 20 3469 02:29:13,120 --> 02:29:16,240 over 50 is so we can't let this happen 3470 02:29:16,240 --> 02:29:18,840 right we need to make sure we liquidate 3471 02:29:18,840 --> 02:29:20,680 people's positions we remov people's 3472 02:29:20,680 --> 02:29:23,680 positions in the system if the price of 3473 02:29:23,680 --> 02:29:26,880 the collateral tanks okay and this is 3474 02:29:26,880 --> 02:29:30,279 where liquidation comes in so we say if 3475 02:29:30,279 --> 02:29:33,680 someone is almost under 3476 02:29:33,680 --> 02:29:36,600 collateralized we will pay you to 3477 02:29:36,600 --> 02:29:38,720 liquidate them we have kind of this 3478 02:29:38,720 --> 02:29:40,760 gamified incentive system here where 3479 02:29:40,760 --> 02:29:42,760 people can get basically free money for 3480 02:29:42,760 --> 02:29:44,479 removing other people's positions in the 3481 02:29:44,479 --> 02:29:47,760 protocol so in this situation up here as 3482 02:29:47,760 --> 02:29:49,479 the price is going down let's say the 3483 02:29:49,479 --> 02:29:53,880 price goes down to $75 backing $50 of 3484 02:29:53,880 --> 02:29:57,160 DSC this is way lower than our 50% 3485 02:29:57,160 --> 02:29:59,560 threshold so what we're going to do is 3486 02:29:59,560 --> 02:30:03,560 we're going to let Liquidator Liquidator 3487 02:30:03,560 --> 02:30:07,720 take this $75 backing takes the $75 3488 02:30:07,720 --> 02:30:13,560 backing and pays off the $50 DSC and 3489 02:30:13,560 --> 02:30:16,720 Burns Burns off the 50 DSC so we're 3490 02:30:16,720 --> 02:30:18,600 going to have somebody able to take 3491 02:30:18,600 --> 02:30:22,040 their money in exchange for them making 3492 02:30:22,040 --> 02:30:25,520 sure our protocol stays collateralized 3493 02:30:25,520 --> 02:30:26,920 so that's what this liquida function is 3494 02:30:26,920 --> 02:30:29,439 going to do so first off they're going 3495 02:30:29,439 --> 02:30:32,359 to be able to choose the 3496 02:30:32,359 --> 02:30:34,640 collateral the user that they want to 3497 02:30:34,640 --> 02:30:38,920 liquidate and the UN 256 debt to cover 3498 02:30:38,920 --> 02:30:40,840 and they'll be able to track the users 3499 02:30:40,840 --> 02:30:42,880 and their positions by listening to 3500 02:30:42,880 --> 02:30:45,200 these events that we've been emitting 3501 02:30:45,200 --> 02:30:46,800 which is exciting we're going to 3502 02:30:46,800 --> 02:30:48,080 definitely going to want a lot of n spec 3503 02:30:48,080 --> 02:30:50,680 for this app Pam 3504 02:30:50,680 --> 02:30:52,200 collateral is going to be the collateral 3505 02:30:52,200 --> 02:30:56,439 to liquidate or better yet the 3506 02:30:56,439 --> 02:31:01,120 rc20 address collateral address to 3507 02:31:01,120 --> 02:31:06,319 liquidate from the user at Pam user the 3508 02:31:06,319 --> 02:31:10,600 user who has broken the health Factor 3509 02:31:10,600 --> 02:31:16,479 their health Factor should be below Min 3510 02:31:16,479 --> 02:31:20,279 Health Factor at Pam 3511 02:31:20,279 --> 02:31:24,840 debt to cover is going to be the amount 3512 02:31:24,840 --> 02:31:32,000 of DSC you want to burn to improve the 3513 02:31:32,000 --> 02:31:34,920 users Health Factor okay we're going to 3514 02:31:34,920 --> 02:31:37,120 add a lot of at notices here at 3515 02:31:37,120 --> 02:31:40,040 notice you can 3516 02:31:40,040 --> 02:31:44,600 partially or liquidate a user just so as 3517 02:31:44,600 --> 02:31:46,680 so long as you improve their health 3518 02:31:46,680 --> 02:31:48,439 Factor that's all we care about at 3519 02:31:48,439 --> 02:31:53,720 notice you will get a liquidation bonus 3520 02:31:53,720 --> 02:31:57,240 for taking the users funds right we want 3521 02:31:57,240 --> 02:32:00,160 to incentivize them to to actually do 3522 02:32:00,160 --> 02:32:02,279 this right if we say okay cool you'll 3523 02:32:02,279 --> 02:32:05,760 get the $50 back for paying off the $50 3524 02:32:05,760 --> 02:32:07,359 debt it's going to be hard to 3525 02:32:07,359 --> 02:32:09,040 incentivize people to do that but if we 3526 02:32:09,040 --> 02:32:11,840 say you get all $75 and all you have to 3527 02:32:11,840 --> 02:32:14,160 do is pay back 50 then that's going to 3528 02:32:14,160 --> 02:32:15,760 be a bonus that they should be able to 3529 02:32:15,760 --> 02:32:18,160 take to incentivize them to do this at 3530 02:32:18,160 --> 02:32:21,160 notice this function 3531 02:32:21,160 --> 02:32:25,520 working assumes the protocol protocol 3532 02:32:25,520 --> 02:32:30,520 will be roughly 200 100% over 3533 02:32:30,520 --> 02:32:34,200 collateralized in order for this to work 3534 02:32:34,200 --> 02:32:36,840 why well because in this scenario if 3535 02:32:36,840 --> 02:32:39,640 this drops down to $20 backing $50 of 3536 02:32:39,640 --> 02:32:43,080 DSC and if I pay back the 50 DSC and I 3537 02:32:43,080 --> 02:32:45,600 get $20 well then I I'm not going to do 3538 02:32:45,600 --> 02:32:47,840 that I'm not going to pay $50 to get 3539 02:32:47,840 --> 02:32:51,160 back 20 so this whole system only works 3540 02:32:51,160 --> 02:32:53,680 if the system is always over 3541 02:32:53,680 --> 02:32:55,680 collateralized the only way we can give 3542 02:32:55,680 --> 02:32:58,080 liquidation bonuses is if we're over 3543 02:32:58,080 --> 02:32:59,359 collateralized so the only way we can 3544 02:32:59,359 --> 02:33:01,920 incentivize people to liquidate poor 3545 02:33:01,920 --> 02:33:04,760 users is if we are over collateralized 3546 02:33:04,760 --> 02:33:09,240 so we could say at notice a known bug 3547 02:33:09,240 --> 02:33:13,560 would be if the protocol were 3548 02:33:13,560 --> 02:33:16,359 100% or less 3549 02:33:16,359 --> 02:33:20,640 collateralized then we wouldn't be able 3550 02:33:20,640 --> 02:33:21,840 to 3551 02:33:21,840 --> 02:33:24,439 incentivize the 3552 02:33:24,439 --> 02:33:26,120 Liquidators and then we would just say 3553 02:33:26,120 --> 02:33:30,520 like for example if the price of the 3554 02:33:30,520 --> 02:33:35,920 collateral plummeted before anyone could 3555 02:33:35,920 --> 02:33:39,000 be liquidated so hopefully this makes a 3556 02:33:39,000 --> 02:33:40,920 lot of sense if this doesn't make sense 3557 02:33:40,920 --> 02:33:44,240 you know what to do ask chat chbt ask in 3558 02:33:44,240 --> 02:33:47,479 the discussions Forum Google it use the 3559 02:33:47,479 --> 02:33:49,920 resources that you have to your advance 3560 02:33:49,920 --> 02:33:54,319 AG yes yes okay good so debt to cover we 3561 02:33:54,319 --> 02:33:56,439 probably want to do more than zero very 3562 02:33:56,439 --> 02:33:59,000 lame if they did just zero so we're 3563 02:33:59,000 --> 02:34:01,520 going to do more than zero here we're 3564 02:34:01,520 --> 02:34:03,319 going to be moving tokens around so 3565 02:34:03,319 --> 02:34:06,160 we're going to do non reenen as well 3566 02:34:06,160 --> 02:34:07,359 maybe we'll do some more modifiers but 3567 02:34:07,359 --> 02:34:09,279 that looks pretty good to me right now 3568 02:34:09,279 --> 02:34:11,120 so now what do we need to do well we 3569 02:34:11,120 --> 02:34:13,160 need to do a bunch of stuff here well we 3570 02:34:13,160 --> 02:34:16,720 first need to check Health factor of the 3571 02:34:16,720 --> 02:34:19,800 user right is this user even liquid 3572 02:34:19,800 --> 02:34:22,880 datable right remember we want to do 3573 02:34:22,880 --> 02:34:24,359 follows 3574 02:34:24,359 --> 02:34:28,760 CI checks effects 3575 02:34:28,760 --> 02:34:30,359 interactions right we always want to 3576 02:34:30,359 --> 02:34:31,640 follow CI so we should do some more 3577 02:34:31,640 --> 02:34:33,359 checks here because we should only 3578 02:34:33,359 --> 02:34:36,240 liquidate people who are liquidable so 3579 02:34:36,240 --> 02:34:38,800 first thing we should do U into 256 3580 02:34:38,800 --> 02:34:43,279 starting user Health Factor equals 3581 02:34:43,279 --> 02:34:46,439 underscore Health factor of the user 3582 02:34:46,439 --> 02:34:48,160 right because we have this health Factor 3583 02:34:48,160 --> 02:34:49,760 function which gets that Health Factor 3584 02:34:49,760 --> 02:34:53,000 factor and what we can say and let's put 3585 02:34:53,000 --> 02:34:57,160 this above now we'll say if the starting 3586 02:34:57,160 --> 02:34:59,040 user Health 3587 02:34:59,040 --> 02:35:02,479 factor is greater than or equal to the 3588 02:35:02,479 --> 02:35:04,479 Min Health factor which what's our Min 3589 02:35:04,479 --> 02:35:07,479 Health Factor again one actually it 3590 02:35:07,479 --> 02:35:10,880 should be 1 E18 and we're definitely 3591 02:35:10,880 --> 02:35:12,319 going to write some tests to make sure 3592 02:35:12,319 --> 02:35:15,479 this is correct so 1 E18 because we're 3593 02:35:15,479 --> 02:35:17,920 using our Precision here what else are 3594 02:35:17,920 --> 02:35:19,240 we doing Health Factor why didn't we 3595 02:35:19,240 --> 02:35:21,600 write test elsewhere revert if Health 3596 02:35:21,600 --> 02:35:23,120 factor is broken oh I guess we haven't 3597 02:35:23,120 --> 02:35:25,040 tested this yet well I guess we're going 3598 02:35:25,040 --> 02:35:26,640 to be testing it very soon make sure 3599 02:35:26,640 --> 02:35:28,720 that that Health Factor bid is right so 3600 02:35:28,720 --> 02:35:30,160 the starting user Health factor is 3601 02:35:30,160 --> 02:35:32,279 greater than or equal to the health 3602 02:35:32,279 --> 02:35:36,359 Factor then we revert right revert DSC 3603 02:35:36,359 --> 02:35:39,800 engine uncore underscore Health Factor 3604 02:35:39,800 --> 02:35:43,200 okay right Health factor is fine got a 3605 02:35:43,200 --> 02:35:46,200 new error let's go to the top scroll 3606 02:35:46,200 --> 02:35:51,040 down error Health Factor o back back 3607 02:35:51,040 --> 02:35:53,080 cool back down here remember I'm doing 3608 02:35:53,080 --> 02:35:55,040 control minus to go back might be 3609 02:35:55,040 --> 02:35:58,120 something else depending on your setup 3610 02:35:58,120 --> 02:36:00,479 so let's do some thinking so now we have 3611 02:36:00,479 --> 02:36:01,920 their starting Health Factor what do we 3612 02:36:01,920 --> 02:36:08,520 want to do we want to burn their DSC 3613 02:36:08,520 --> 02:36:10,680 debt right we want to reduce the amount 3614 02:36:10,680 --> 02:36:14,040 of DSC they have and take their 3615 02:36:14,040 --> 02:36:15,760 collateral we want to remove them from 3616 02:36:15,760 --> 02:36:17,160 the system basically right so how do we 3617 02:36:17,160 --> 02:36:21,560 do that let's say they have 140 3618 02:36:22,080 --> 02:36:27,080 $140 of eth deposited and $100 of DSC 3619 02:36:27,080 --> 02:36:28,319 with a setup like this their health 3620 02:36:28,319 --> 02:36:30,560 Factor should be below what it currently 3621 02:36:30,560 --> 02:36:33,600 is what we could do is we can say okay 3622 02:36:33,600 --> 02:36:36,319 we're going to cover so this is what bad 3623 02:36:36,319 --> 02:36:40,520 user this dis bad user has that means we 3624 02:36:40,520 --> 02:36:41,720 could cover we could say okay we're 3625 02:36:41,720 --> 02:36:45,200 going to cover debt to cover is going to 3626 02:36:45,200 --> 02:36:47,840 equal that $100 and we need to pay back 3627 02:36:47,840 --> 02:36:51,040 $100 so we're going to have to get a u 3628 02:36:51,040 --> 02:36:55,520 256 collateral or token amount from debt 3629 02:36:55,520 --> 02:36:58,720 covered covered cover covered so we need 3630 02:36:58,720 --> 02:36:59,720 to figure out okay if we're covering 3631 02:36:59,720 --> 02:37:04,840 $100 of debt $100 of DSC equals how much 3632 02:37:04,840 --> 02:37:07,319 eth so we're going to pay back $100 of 3633 02:37:07,319 --> 02:37:10,439 debt and how much eth is that okay how 3634 02:37:10,439 --> 02:37:12,760 much e eth is that so we're going to get 3635 02:37:12,760 --> 02:37:14,720 the token amount of De color the eth 3636 02:37:14,720 --> 02:37:17,560 basically equals and we're going to have 3637 02:37:17,560 --> 02:37:20,399 to do some pricing stuff we're have to 3638 02:37:20,399 --> 02:37:26,000 say it equals get token amount from USD 3639 02:37:26,000 --> 02:37:28,080 so we're going to add the collateral and 3640 02:37:28,080 --> 02:37:30,439 the debt to cover so we're going to 3641 02:37:30,439 --> 02:37:32,880 figure out okay how much of this token 3642 02:37:32,880 --> 02:37:34,120 are we going to get right we're going to 3643 02:37:34,120 --> 02:37:37,120 cover $100 worth of debt how much in eth 3644 02:37:37,120 --> 02:37:39,680 or whatever collateral token is $100 3645 02:37:39,680 --> 02:37:40,840 worth of debt so we're going to create a 3646 02:37:40,840 --> 02:37:43,600 new function get token amount from USD 3647 02:37:43,600 --> 02:37:45,520 this is going to be a public view 3648 02:37:45,520 --> 02:37:46,720 function so we're going to scroll all 3649 02:37:46,720 --> 02:37:48,760 the way down here public and external 3650 02:37:48,760 --> 02:37:50,279 view functions 3651 02:37:50,279 --> 02:37:53,760 function get token amount from USD and 3652 02:37:53,760 --> 02:37:56,840 we're going to pass an address token or 3653 02:37:56,840 --> 02:38:02,160 collateral unit 256 USD amount in way 3654 02:38:02,160 --> 02:38:07,439 this will be a public view returns 3655 02:38:07,439 --> 02:38:10,120 256 and we're going to have to do some 3656 02:38:10,120 --> 02:38:12,080 price feed stuff so we're going to need 3657 02:38:12,080 --> 02:38:12,920 what are we going to need to do we're 3658 02:38:12,920 --> 02:38:15,800 going to need to get the price of eth or 3659 02:38:15,800 --> 02:38:17,560 the token and then we're going to have 3660 02:38:17,560 --> 02:38:21,040 to say Okay so if the pricing is dollar 3661 02:38:21,040 --> 02:38:24,240 per eth and we have eth how do we get 3662 02:38:24,240 --> 02:38:27,479 the dollar well if we do some math here 3663 02:38:27,479 --> 02:38:32,000 let's say it's $2,000 of eth and we have 3664 02:38:32,000 --> 02:38:34,720 $1,000 right how much eth is that we're 3665 02:38:34,720 --> 02:38:37,720 going to do the 1,000 divided 2,000 3666 02:38:37,720 --> 02:38:40,800 which is going to equal 0.5 eth right so 3667 02:38:40,800 --> 02:38:43,399 we're going to do this number divided by 3668 02:38:43,399 --> 02:38:45,920 this number the amount the USD amount in 3669 02:38:45,920 --> 02:38:49,040 way divided by the price and that's how 3670 02:38:49,040 --> 02:38:51,120 we're going to get get this token or 3671 02:38:51,120 --> 02:38:54,399 collateral amount from the USD amount so 3672 02:38:54,399 --> 02:38:58,040 we'll say agregator V3 interface price 3673 02:38:58,040 --> 02:39:03,000 feed equals aggregator V3 interface of S 3674 02:39:03,000 --> 02:39:05,920 price feed of token oh wow it 3675 02:39:05,920 --> 02:39:07,960 even build in a lot of this for me 3676 02:39:07,960 --> 02:39:09,800 that's great yes this looks right so 3677 02:39:09,800 --> 02:39:11,359 we're going to get the price feed of the 3678 02:39:11,359 --> 02:39:13,840 token and get called latest round data 3679 02:39:13,840 --> 02:39:16,600 on it to get the price here and then 3680 02:39:16,600 --> 02:39:19,960 we're just going to do this right here 3681 02:39:19,960 --> 02:39:23,520 so we're going to say the amount USD in 3682 02:39:23,520 --> 02:39:27,840 way divided by the price or unit 56 3683 02:39:27,840 --> 02:39:30,359 price is this the full story no 3684 02:39:30,359 --> 02:39:31,960 absolutely not because we should always 3685 02:39:31,960 --> 02:39:33,840 multiply first so we're going to do this 3686 02:39:33,840 --> 02:39:37,359 times our pre cision then we can divide 3687 02:39:37,359 --> 02:39:39,439 by the price but is that the whole story 3688 02:39:39,439 --> 02:39:41,319 no because the price has eight decimal 3689 02:39:41,319 --> 02:39:44,640 places and we needed to have 18 so we're 3690 02:39:44,640 --> 02:39:47,279 going to do additional feed Precision 3691 02:39:47,279 --> 02:39:49,479 like this 3692 02:39:49,479 --> 02:39:52,680 is Alec right so if we have let's say we 3693 02:39:52,680 --> 02:39:54,279 have 10 3694 02:39:54,279 --> 02:39:57,439 E18 for Mountain way we're timesing that 3695 02:39:57,439 --> 02:39:59,200 by 1 3696 02:39:59,200 --> 02:40:02,319 E18 like this we're saying divide that 3697 02:40:02,319 --> 02:40:03,920 by the price let's say the price is 3698 02:40:03,920 --> 02:40:06,960 $2,000 $2,000 3699 02:40:06,960 --> 02:40:12,000 E8 times the 1 E10 the additional piece 3700 02:40:12,000 --> 02:40:14,240 here this looks pretty correct do we 3701 02:40:14,240 --> 02:40:15,359 need anything else let's see just pull 3702 02:40:15,359 --> 02:40:18,920 out the calculator right so 10 1 2 3 4 5 3703 02:40:18,920 --> 02:40:26,439 6 7 8 1 2 3 4 5 6 7 8 9 10 * 1 1 2 3 4 5 3704 02:40:26,439 --> 02:40:32,200 6 7 8 1 2 3 4 5 6 7 8 9 10 divided by 2 3705 02:40:32,200 --> 02:40:36,800 1 2 3 2000 1 2 3 4 5 6 7 8 9 10 1 2 3 4 3706 02:40:36,800 --> 02:40:40,560 5 6 7 8 equals this number which is 3707 02:40:40,560 --> 02:40:44,279 probably a half right 1 2 1 2 3 4 5 6 7 3708 02:40:44,279 --> 02:40:49,720 8 9 10 11 12 13 14 15 that right 1 2 2 3 3709 02:40:49,720 --> 02:40:57,160 4 5 6 7 8 9 1 2 3 4 5 6 7 8 3710 02:40:57,160 --> 02:40:58,800 9 3711 02:40:58,800 --> 02:41:02,680 89 0 point is that right oh because it's 3712 02:41:02,680 --> 02:41:05,399 $10 thoughh yes this is right I was like 3713 02:41:05,399 --> 02:41:07,960 I thought we did $1,000 but this is $10 3714 02:41:07,960 --> 02:41:10,080 yes okay cool that looks good to me 3715 02:41:10,080 --> 02:41:11,319 great and then obviously we're going to 3716 02:41:11,319 --> 02:41:13,000 test this soon to make sure that is 3717 02:41:13,000 --> 02:41:15,439 right so we have this function where we 3718 02:41:15,439 --> 02:41:17,560 get the token amount from the USD so 3719 02:41:17,560 --> 02:41:19,399 we're saying hey we're going to cover 3720 02:41:19,399 --> 02:41:22,000 $100 of your debt or something like that 3721 02:41:22,000 --> 02:41:24,319 how much eth is $100 worth of your debt 3722 02:41:24,319 --> 02:41:28,240 maybe it's if the price is $2,000 we 3723 02:41:28,240 --> 02:41:31,399 obviously we would do 100 divid by 2,000 3724 02:41:31,399 --> 02:41:34,120 0.05 right there right this is going to 3725 02:41:34,120 --> 02:41:37,680 be something like 0.05 eth or whatever 3726 02:41:37,680 --> 02:41:41,520 it is right cool so we have how much eth 3727 02:41:41,520 --> 02:41:43,399 we need to take away from their 3728 02:41:43,399 --> 02:41:46,160 collateral as a reward for paying back 3729 02:41:46,160 --> 02:41:48,880 this DSC but additionally we also want 3730 02:41:48,880 --> 02:41:50,000 to 3731 02:41:50,000 --> 02:41:53,439 and give them a 10% bonus right because 3732 02:41:53,439 --> 02:41:54,960 we want to incentivize them if they're 3733 02:41:54,960 --> 02:41:56,960 just if it's just a one for one they're 3734 02:41:56,960 --> 02:41:58,160 not going to want to do this so let's 3735 02:41:58,160 --> 02:42:00,359 say we're going to give them a 10% bonus 3736 02:42:00,359 --> 02:42:05,439 so we are giving the liquid dator 3737 02:42:05,439 --> 02:42:10,640 $110 of weth for 100 dsse whatever $110 3738 02:42:10,640 --> 02:42:14,200 of we is going to be we should Implement 3739 02:42:14,200 --> 02:42:19,520 a feature to liquid dat in the event the 3740 02:42:19,520 --> 02:42:22,520 protocol protocol is 3741 02:42:22,520 --> 02:42:24,479 insolvent we're not going to add that in 3742 02:42:24,479 --> 02:42:26,279 yet though but we should probably add 3743 02:42:26,279 --> 02:42:29,840 something like that and then and sweep 3744 02:42:29,840 --> 02:42:33,640 extra amounts into a treasury but we're 3745 02:42:33,640 --> 02:42:35,200 not going to add either one of those so 3746 02:42:35,200 --> 02:42:36,560 don't worry if that's confusing don't 3747 02:42:36,560 --> 02:42:37,520 worry about that we're not even going to 3748 02:42:37,520 --> 02:42:39,200 implement that anyways so we'll say you 3749 02:42:39,200 --> 02:42:44,319 256 bonus colateral equals this token 3750 02:42:44,319 --> 02:42:49,479 amount debt covered times some liquid 3751 02:42:49,479 --> 02:42:51,720 ation bonus that we haven't defined yet 3752 02:42:51,720 --> 02:42:53,880 although we're going to do 10% divided 3753 02:42:53,880 --> 02:42:56,760 by 100 but I hate these floating numbers 3754 02:42:56,760 --> 02:42:57,960 so we're going to do something better 3755 02:42:57,960 --> 02:43:00,560 than 100 we're going to do Liquidator 3756 02:43:00,560 --> 02:43:03,120 Precision which is 100 we'll do unit to 3757 02:43:03,120 --> 02:43:05,240 256 private 3758 02:43:05,240 --> 02:43:11,479 constant liquid dator bonus equals Shan 3759 02:43:11,479 --> 02:43:14,720 bonus it's going to be 10 be like this 3760 02:43:14,720 --> 02:43:18,120 means a 10% bonus and since it's going 3761 02:43:18,120 --> 02:43:20,160 to be 10/ div by 100 that's going to be 3762 02:43:20,160 --> 02:43:23,319 10% let's go back down liquidation bonus 3763 02:43:23,319 --> 02:43:26,200 divided by 3764 02:43:26,200 --> 02:43:29,000 liquidation Precision so the bonus 3765 02:43:29,000 --> 02:43:31,720 collateral is going to be 10% so let's 3766 02:43:31,720 --> 02:43:35,800 say it was 0.5 0.05 eth we're going to 3767 02:43:35,800 --> 02:43:39,720 multiply that by 0.1 right because we're 3768 02:43:39,720 --> 02:43:41,600 going to multiply first and then divide 3769 02:43:41,600 --> 02:43:42,920 to get 3770 02:43:42,920 --> 02:43:46,439 0.05 eth so as a total or that's not 3771 02:43:46,439 --> 02:43:49,479 quite right it's going to be 0.5 3772 02:43:49,479 --> 02:43:52,240 0.05 time 3773 02:43:52,240 --> 02:43:54,880 0.1 equals yes so that means they're 3774 02:43:54,880 --> 02:43:58,040 going to get 3775 02:43:58,240 --> 02:44:01,160 0.05 five is that right I think that's 3776 02:44:01,160 --> 02:44:05,279 right why why am I being bad at math I'm 3777 02:44:05,279 --> 02:44:08,240 one yes okay cool great so that's what 3778 02:44:08,240 --> 02:44:09,120 they're going to get this bonus 3779 02:44:09,120 --> 02:44:10,720 collateral we got to add the bonus 3780 02:44:10,720 --> 02:44:14,080 collateral to the actual collateral 3781 02:44:14,080 --> 02:44:16,319 we're going to say like you went to 256 3782 02:44:16,319 --> 02:44:19,840 total collateral collateral 3783 02:44:19,840 --> 02:44:23,880 to redeem equals token amount from 3784 02:44:23,880 --> 02:44:27,760 covered plus bonus collateral like that 3785 02:44:27,760 --> 02:44:29,880 and so and now we need to redeem this 3786 02:44:29,880 --> 02:44:32,439 amount of collateral for whatever's 3787 02:44:32,439 --> 02:44:34,080 calling the liquidate function right we 3788 02:44:34,080 --> 02:44:35,479 need to redeem that collateral and then 3789 02:44:35,479 --> 02:44:38,399 we also need to burn the DSC from this 3790 02:44:38,399 --> 02:44:40,319 user as well so we need to give them the 3791 02:44:40,319 --> 02:44:42,840 collateral and burn the DSC that they're 3792 02:44:42,840 --> 02:44:46,000 covering with their debt to 3793 02:44:47,520 --> 02:44:49,800 cover now though if if we look at our 3794 02:44:49,800 --> 02:44:52,920 redeem collateral redeem 3795 02:44:52,920 --> 02:44:55,200 collateral function which is public 3796 02:44:55,200 --> 02:44:58,560 right now we can see as inputs it takes 3797 02:44:58,560 --> 02:45:00,240 token collateral address and amount 3798 02:45:00,240 --> 02:45:02,840 collateral right and it is hardcoded to 3799 02:45:02,840 --> 02:45:05,600 message. Sender our third party user 3800 02:45:05,600 --> 02:45:07,800 isn't the one with the bad debt right we 3801 02:45:07,800 --> 02:45:09,880 need to redeem a random person's 3802 02:45:09,880 --> 02:45:12,399 collateral so what we can do is we can 3803 02:45:12,399 --> 02:45:15,040 refactor this code so that there's an 3804 02:45:15,040 --> 02:45:17,600 internal redeem collateral function that 3805 02:45:17,600 --> 02:45:20,279 can redeem collateral from anybody right 3806 02:45:20,279 --> 02:45:23,080 and only very permissioned functions can 3807 02:45:23,080 --> 02:45:25,479 call redeem collateral so what we're 3808 02:45:25,479 --> 02:45:26,600 going to do is actually we're going to 3809 02:45:26,600 --> 02:45:29,200 take all of this code and we're going to 3810 02:45:29,200 --> 02:45:30,760 change it and we're going to delete it 3811 02:45:30,760 --> 02:45:31,960 and we're going to put it into a 3812 02:45:31,960 --> 02:45:34,680 different function all the way down the 3813 02:45:34,680 --> 02:45:36,920 in private and internal functions here 3814 02:45:36,920 --> 02:45:38,520 we're going to make a function 3815 02:45:38,520 --> 02:45:41,760 underscore redeem 3816 02:45:41,760 --> 02:45:43,479 collateral and this is going to be an 3817 02:45:43,479 --> 02:45:45,040 internal function where we can actually 3818 02:45:45,040 --> 02:45:47,640 redeem collateral from anybody and as 3819 02:45:47,640 --> 02:45:50,200 input this is going to take address 3820 02:45:50,200 --> 02:45:52,960 token collateral address same as the 3821 02:45:52,960 --> 02:45:54,520 regular redeem collateral also the unit 3822 02:45:54,520 --> 02:45:57,359 256 amount collateral but we're going to 3823 02:45:57,359 --> 02:46:01,760 add address from and an address to so 3824 02:46:01,760 --> 02:46:03,680 this way and this is going to be private 3825 02:46:03,680 --> 02:46:07,399 so this way somebody can liquidate an 3826 02:46:07,399 --> 02:46:10,040 address from and then get the rewards to 3827 02:46:10,040 --> 02:46:11,399 and this is where I'm going to paste all 3828 02:46:11,399 --> 02:46:14,720 that code we got from above and I think 3829 02:46:14,720 --> 02:46:16,479 this looks good yeah we're just going to 3830 02:46:16,479 --> 02:46:18,399 paste that code exactly like this 3831 02:46:18,399 --> 02:46:20,840 instead of doing doing message. sender 3832 02:46:20,840 --> 02:46:23,520 this is just going to be from and emit 3833 02:46:23,520 --> 02:46:25,479 collateral redeemed we're going to 3834 02:46:25,479 --> 02:46:28,359 update our collateral 3835 02:46:28,920 --> 02:46:33,640 redeemed to redeemed from we'll do 3836 02:46:33,640 --> 02:46:37,640 address redeemed from address redeemed 3837 02:46:37,640 --> 02:46:41,040 to we'll do token and then we'll have 3838 02:46:41,040 --> 02:46:45,680 amount not be indexed so from to token 3839 02:46:45,680 --> 02:46:47,880 and amount so now we got to refactor 3840 02:46:47,880 --> 02:46:50,920 some of these collaterals so from when 3841 02:46:50,920 --> 02:46:53,160 we do just the regular redeem collateral 3842 02:46:53,160 --> 02:46:54,680 we'll update this in a minute actually 3843 02:46:54,680 --> 02:46:56,479 yeah we'll update that in a minute so in 3844 02:46:56,479 --> 02:46:58,200 this internal function it's going to be 3845 02:46:58,200 --> 02:47:03,800 from from to address collateral and then 3846 02:47:03,800 --> 02:47:05,840 we're going to transfer we're going to 3847 02:47:05,840 --> 02:47:08,960 transfer the tokens to whoever the two 3848 02:47:08,960 --> 02:47:10,399 is so this is going to be like the 3849 02:47:10,399 --> 02:47:13,680 Liquidator Mount collateral and do that 3850 02:47:13,680 --> 02:47:14,920 and now we're going to use this internal 3851 02:47:14,920 --> 02:47:17,319 function up in our regular redeem 3852 02:47:17,319 --> 02:47:19,479 collateral function that we created 3853 02:47:19,479 --> 02:47:23,720 right uh up here oh no up this one up 3854 02:47:23,720 --> 02:47:25,640 here we're going to dump all this and 3855 02:47:25,640 --> 02:47:27,240 just use this underscore redeem 3856 02:47:27,240 --> 02:47:29,439 collateral and for here we're just going 3857 02:47:29,439 --> 02:47:32,399 to say message. sender so from message. 3858 02:47:32,399 --> 02:47:36,319 sender to message. Sender token 3859 02:47:36,319 --> 02:47:38,120 collateral address amount collateral and 3860 02:47:38,120 --> 02:47:39,960 now that we have this internal function 3861 02:47:39,960 --> 02:47:42,439 we can use this redeem collateral bit 3862 02:47:42,439 --> 02:47:45,279 down in our liquidate which is down here 3863 02:47:45,279 --> 02:47:48,520 we can now just say redeem collateral 3864 02:47:48,520 --> 02:47:50,920 from from the user that's being 3865 02:47:50,920 --> 02:47:55,040 liquidated so from user to whoever's 3866 02:47:55,040 --> 02:47:57,439 calling liquidate here so it's going to 3867 02:47:57,439 --> 02:48:01,920 be message. sender the collateral token 3868 02:48:01,920 --> 02:48:04,600 that we're liquidating and then finally 3869 02:48:04,600 --> 02:48:07,120 the total collateral to redeem so this 3870 02:48:07,120 --> 02:48:08,240 is why I said we were going to do a 3871 02:48:08,240 --> 02:48:11,479 little bit of refactoring soon right we 3872 02:48:11,479 --> 02:48:13,359 generally only want to redeem collateral 3873 02:48:13,359 --> 02:48:15,760 from into the same person however when 3874 02:48:15,760 --> 02:48:17,279 we're doing a liquidate we're going to 3875 02:48:17,279 --> 02:48:19,640 redeem to whoever's calling the liid dat 3876 02:48:19,640 --> 02:48:21,640 so they're going to get that reward the 3877 02:48:21,640 --> 02:48:23,760 total collateral to redeem is that total 3878 02:48:23,760 --> 02:48:26,040 amount debt to cover plus some bonus 3879 02:48:26,040 --> 02:48:28,399 here and now we actually need to yeah we 3880 02:48:28,399 --> 02:48:30,560 need to burn the DSC now so right now if 3881 02:48:30,560 --> 02:48:33,319 we look up burn burn DSC we have this 3882 02:48:33,319 --> 02:48:35,880 public function which just does the same 3883 02:48:35,880 --> 02:48:37,760 thing it just does it just burns from 3884 02:48:37,760 --> 02:48:39,640 message. sender we're going to have to 3885 02:48:39,640 --> 02:48:41,040 do the same here we're going to want to 3886 02:48:41,040 --> 02:48:43,479 make an internal burn DSC function that 3887 02:48:43,479 --> 02:48:46,399 allows us to burn from anybody right so 3888 02:48:46,399 --> 02:48:48,880 if we scroll down to where we're doing 3889 02:48:48,880 --> 02:48:52,319 those private functions or scroll up so 3890 02:48:52,319 --> 02:48:54,680 we have this internal redeem collateral 3891 02:48:54,680 --> 02:48:56,479 we also are going to need an internal 3892 02:48:56,479 --> 02:49:01,800 burn DSC function uncore burn DSC unit 3893 02:49:01,800 --> 02:49:08,160 256 amount DSC to burn address on be 3894 02:49:08,160 --> 02:49:11,640 half of like whose DSC are we burning 3895 02:49:11,640 --> 02:49:14,240 for whose debt are we paying down and 3896 02:49:14,240 --> 02:49:17,040 then address DSC from where are we 3897 02:49:17,040 --> 02:49:20,399 getting the DSC from we a private and we 3898 02:49:20,399 --> 02:49:22,920 can go back to the burn DSC this burn 3899 02:49:22,920 --> 02:49:26,000 DSC function right and same thing we can 3900 02:49:26,000 --> 02:49:28,279 just copy all of this go back to our 3901 02:49:28,279 --> 02:49:30,800 internal burn DSC function paste this in 3902 02:49:30,800 --> 02:49:32,960 here we're going to want to update this 3903 02:49:32,960 --> 02:49:35,399 right instead of message. sender this is 3904 02:49:35,399 --> 02:49:39,000 going to be on be half of we're going to 3905 02:49:39,000 --> 02:49:42,399 B we're going to take away their debt 3906 02:49:42,399 --> 02:49:43,960 we're still going to do this but instead 3907 02:49:43,960 --> 02:49:47,160 of message. sender it's going to be ESC 3908 02:49:47,160 --> 02:49:50,279 from and it's going to be amount DSC to 3909 02:49:50,279 --> 02:49:52,960 burn this is still fine this just needs 3910 02:49:52,960 --> 02:49:55,080 to be a amount DSC to burn and we don't 3911 02:49:55,080 --> 02:49:57,680 need to check Health Factor yet because 3912 02:49:57,680 --> 02:49:58,760 this is going to be our internal 3913 02:49:58,760 --> 02:50:01,399 function so we might even say like in 3914 02:50:01,399 --> 02:50:05,040 the comments here low level internal 3915 02:50:05,040 --> 02:50:07,880 function or at Dev low-level internal 3916 02:50:07,880 --> 02:50:12,399 function do not call unless the function 3917 02:50:12,399 --> 02:50:13,960 calling 3918 02:50:13,960 --> 02:50:19,840 it is checking for health factors being 3919 02:50:19,840 --> 02:50:22,520 broken so now we have this burn DSC we 3920 02:50:22,520 --> 02:50:25,520 can go back up to our burn DSC function 3921 02:50:25,520 --> 02:50:28,520 we can just swap delete all of this call 3922 02:50:28,520 --> 02:50:31,319 burn DSC and this going to be message. 3923 02:50:31,319 --> 02:50:34,760 sender message. sender amount right 3924 02:50:34,760 --> 02:50:37,040 somebody calls burn DSC thems great 3925 02:50:37,040 --> 02:50:39,200 we'll just have them call this burn DC 3926 02:50:39,200 --> 02:50:41,399 amount to burn on behalf of themselves 3927 02:50:41,399 --> 02:50:43,120 from themselves oh looks like got this 3928 02:50:43,120 --> 02:50:45,560 backwards amount should be the first one 3929 02:50:45,560 --> 02:50:47,600 okay and now we have this burn DSC 3930 02:50:47,600 --> 02:50:48,640 function we can go down to look 3931 02:50:48,640 --> 02:50:51,560 liquidate and we can call burn DSC what 3932 02:50:51,560 --> 02:50:56,319 are the uh amount on behalf of from so 3933 02:50:56,319 --> 02:50:59,080 the amount is going to be this debt to 3934 02:50:59,080 --> 02:51:03,120 cover amount on behalf of is going to be 3935 02:51:03,120 --> 02:51:05,279 the user and the one who's going to be 3936 02:51:05,279 --> 02:51:06,960 paying this is going to be message. 3937 02:51:06,960 --> 02:51:09,600 sender right because it's the whoever's 3938 02:51:09,600 --> 02:51:10,960 the Liquidator whoever's calling 3939 02:51:10,960 --> 02:51:12,560 liquidate is going to be the one who's 3940 02:51:12,560 --> 02:51:14,920 paying down the debt to cover right 3941 02:51:14,920 --> 02:51:17,479 paying back that minted DSC and now 3942 02:51:17,479 --> 02:51:19,000 since we're doing these internal calls 3943 02:51:19,000 --> 02:51:20,680 that don't have checks we absolutely 3944 02:51:20,680 --> 02:51:22,840 need to make sure we're checking this 3945 02:51:22,840 --> 02:51:24,640 health factor is okay right so we're 3946 02:51:24,640 --> 02:51:28,040 going to do un 256 ending user Health 3947 02:51:28,040 --> 02:51:31,160 Factor equals underscore Health factor 3948 02:51:31,160 --> 02:51:34,920 of the user and if the ending Health 3949 02:51:34,920 --> 02:51:38,200 factor is less than or equal to the 3950 02:51:38,200 --> 02:51:39,760 starting Health Factor right if we 3951 02:51:39,760 --> 02:51:41,560 didn't improve the health Factor we 3952 02:51:41,560 --> 02:51:45,800 should revert DSC engine underscore 3953 02:51:45,800 --> 02:51:51,000 Health Factor not improved copy this go 3954 02:51:51,000 --> 02:51:54,720 to the top we'll do error DSC engine 3955 02:51:54,720 --> 02:51:57,319 Health Factor not improved go back down 3956 02:51:57,319 --> 02:51:59,960 so if we don't improve the health Factor 3957 02:51:59,960 --> 02:52:04,279 we should 100% revert and then also if 3958 02:52:04,279 --> 02:52:06,520 calling this liquidate function if 3959 02:52:06,520 --> 02:52:08,239 paying down some debt and doing all this 3960 02:52:08,239 --> 02:52:11,000 stuff actually hurts the liquidator's 3961 02:52:11,000 --> 02:52:13,040 health Factor we should also revert 3962 02:52:13,040 --> 02:52:14,840 right so we should also call revert if 3963 02:52:14,840 --> 02:52:17,520 Health factor is broken for the message. 3964 02:52:17,520 --> 02:52:19,359 sender right if this process ruined 3965 02:52:19,359 --> 02:52:21,040 their health Factor we shouldn't let 3966 02:52:21,040 --> 02:52:23,880 them do this okay so we're following 3967 02:52:23,880 --> 02:52:26,479 checks effects interactions here the 3968 02:52:26,479 --> 02:52:31,279 most part these two functions are making 3969 02:52:31,279 --> 02:52:33,239 external calls to external contracts 3970 02:52:33,239 --> 02:52:35,000 right and then we're doing kind of like 3971 02:52:35,000 --> 02:52:37,000 a check afterwards again this is a bit 3972 02:52:37,000 --> 02:52:39,479 of a trade-off we could calculate before 3973 02:52:39,479 --> 02:52:42,760 and then run this but that's kind of gas 3974 02:52:42,760 --> 02:52:44,680 inefficient we're just going to check 3975 02:52:44,680 --> 02:52:46,960 after we do all this hey just make sure 3976 02:52:46,960 --> 02:52:49,359 that the health factor is okay make sure 3977 02:52:49,359 --> 02:52:51,239 that we didn't break anyone's Health 3978 02:52:51,239 --> 02:52:53,840 Factor so now we have this liquidation 3979 02:52:53,840 --> 02:52:55,920 function which is incredibly powerful 3980 02:52:55,920 --> 02:52:57,479 and kind of what ties this whole thing 3981 02:52:57,479 --> 02:52:59,840 together right there's an incentive here 3982 02:52:59,840 --> 02:53:02,840 for people to call liquidate so that our 3983 02:53:02,840 --> 02:53:05,120 protocol is never insolvent right and I 3984 02:53:05,120 --> 02:53:06,279 know I've been kind of throwing around a 3985 02:53:06,279 --> 02:53:08,399 lot of these financial terms but our 3986 02:53:08,399 --> 02:53:11,600 protocol always has more collateral than 3987 02:53:11,600 --> 02:53:14,000 it has minted DSC the value of the 3988 02:53:14,000 --> 02:53:16,640 collateral should always be more than 3989 02:53:16,640 --> 02:53:19,200 the minted DSC always incredibly 3990 02:53:19,200 --> 02:53:20,560 powerful function we're obviously going 3991 02:53:20,560 --> 02:53:21,920 to be testing the living hell out of 3992 02:53:21,920 --> 02:53:23,200 this right because we want to make sure 3993 02:53:23,200 --> 02:53:24,479 it actually 3994 02:53:24,479 --> 02:53:28,200 workso okay what do we need now oh looks 3995 02:53:28,200 --> 02:53:31,040 like we broke some stuff oh this needs a 3996 02:53:31,040 --> 02:53:32,880 semicolon what else do we break there's 3997 02:53:32,880 --> 02:53:34,520 a little red thing down here let's go 3998 02:53:34,520 --> 02:53:37,319 fix it amount this should be amount DC 3999 02:53:37,319 --> 02:53:40,640 to burn got a couple more red things 4000 02:53:40,640 --> 02:53:43,120 redeem collateral user message as sender 4001 02:53:43,120 --> 02:53:46,239 redeem collateral uh oops we did these 4002 02:53:46,239 --> 02:53:49,200 backwards I'm going to copy these 4003 02:53:49,200 --> 02:53:52,200 put these at the front okay oh looks 4004 02:53:52,200 --> 02:53:54,680 like that fixed pretty much most of this 4005 02:53:54,680 --> 02:53:56,800 I don't see any more red stuff on the 4006 02:53:56,800 --> 02:53:58,439 side here so we're going to go ahead and 4007 02:53:58,439 --> 02:54:00,680 run Forge build make sure everything's 4008 02:54:00,680 --> 02:54:03,040 at least 4009 02:54:03,120 --> 02:54:05,239 compiling awesome maybe we'll even run 4010 02:54:05,239 --> 02:54:07,000 Forge test I don't think this would have 4011 02:54:07,000 --> 02:54:08,520 broken any tests looks like those are 4012 02:54:08,520 --> 02:54:10,880 working fine but all right we're doing 4013 02:54:10,880 --> 02:54:14,479 some fantastic work and like I 4014 02:54:15,760 --> 02:54:18,439 said at this point I probably would be 4015 02:54:18,439 --> 02:54:20,800 any test with this but we're almost done 4016 02:54:20,800 --> 02:54:22,080 with all of our code here so we're just 4017 02:54:22,080 --> 02:54:23,840 going to keep going right and this code 4018 02:54:23,840 --> 02:54:25,359 is starting to look pretty darn good 4019 02:54:25,359 --> 02:54:27,680 right we've got these amazing natspec 4020 02:54:27,680 --> 02:54:29,160 comments in here we've got comments all 4021 02:54:29,160 --> 02:54:30,960 over the place maybe these probably 4022 02:54:30,960 --> 02:54:32,279 should be cleaned up a little bit but 4023 02:54:32,279 --> 02:54:34,439 that's fine we've got a little this 4024 02:54:34,439 --> 02:54:36,520 little Dev thing a shout out saying hey 4025 02:54:36,520 --> 02:54:39,239 don't let anybody call burn DSC without 4026 02:54:39,239 --> 02:54:40,920 checking the health Factor right this is 4027 02:54:40,920 --> 02:54:42,680 really good to tell Auditors and 4028 02:54:42,680 --> 02:54:44,600 Security Professionals about this right 4029 02:54:44,600 --> 02:54:46,600 it's really good to call this out we've 4030 02:54:46,600 --> 02:54:47,920 now have this internal redeemed 4031 02:54:47,920 --> 02:54:49,200 collateral function so it can be used 4032 02:54:49,200 --> 02:54:50,720 for the liquidate or the redeem 4033 02:54:50,720 --> 02:54:53,040 collateral Health Factor it looks like 4034 02:54:53,040 --> 02:54:55,000 pretty much all the functions that we 4035 02:54:55,000 --> 02:54:57,760 originally wrote are working in here 4036 02:54:57,760 --> 02:55:00,279 right and the reason again that this all 4037 02:55:00,279 --> 02:55:03,640 works is because when we mint DSC we can 4038 02:55:03,640 --> 02:55:06,479 only mint as much as we have collateral 4039 02:55:06,479 --> 02:55:08,439 in the system we're setting this 4040 02:55:08,439 --> 02:55:10,640 exchange rate essentially for our 4041 02:55:10,640 --> 02:55:12,479 protocol hey cool you have a $100 you 4042 02:55:12,479 --> 02:55:15,560 have $150 worth of eth deposited great 4043 02:55:15,560 --> 02:55:19,120 you can mint $100 worth of DSC 4044 02:55:19,120 --> 02:55:20,600 so it's this exchange rate that we're 4045 02:55:20,600 --> 02:55:23,120 setting which is maintaining the price 4046 02:55:23,120 --> 02:55:24,359 let's just double check that we have 4047 02:55:24,359 --> 02:55:25,520 kind of most of the functions that we 4048 02:55:25,520 --> 02:55:27,600 want here and let's also check that 4049 02:55:27,600 --> 02:55:30,399 they're in the right place so we go to 4050 02:55:30,399 --> 02:55:32,760 the top here got a whole bunch of Errors 4051 02:55:32,760 --> 02:55:34,640 that's great got a whole bunch of 4052 02:55:34,640 --> 02:55:36,680 constants because we hate magic numbers 4053 02:55:36,680 --> 02:55:38,840 we have a mapping of price feeds a 4054 02:55:38,840 --> 02:55:40,960 mapping of collateral deposited from 4055 02:55:40,960 --> 02:55:42,800 user to token to the amount they have 4056 02:55:42,800 --> 02:55:45,359 deposited we have their debt or their 4057 02:55:45,359 --> 02:55:49,319 DSC minted collateral tokens the idsc 4058 02:55:49,319 --> 02:55:52,279 token we've got some events in here 4059 02:55:52,279 --> 02:55:54,760 couple modifiers some 4060 02:55:54,760 --> 02:55:57,600 functions we've got a way to deposit 4061 02:55:57,600 --> 02:56:00,200 collateral and mint DSC in one 4062 02:56:00,200 --> 02:56:02,359 transaction we've got a way to calculate 4063 02:56:02,359 --> 02:56:03,560 health Factor I'm going to tell you 4064 02:56:03,560 --> 02:56:06,200 right now there's actually a bug in here 4065 02:56:06,200 --> 02:56:08,160 ho not going to tell you what that bug 4066 02:56:08,160 --> 02:56:10,479 is yet though maybe you can figure it 4067 02:56:10,479 --> 02:56:12,760 out we've got some view functions down 4068 02:56:12,760 --> 02:56:14,359 below we're probably going to add more 4069 02:56:14,359 --> 02:56:17,239 as we write tests but this is looking 4070 02:56:17,239 --> 02:56:18,800 pretty darn 4071 02:56:18,800 --> 02:56:21,239 good now like I said there's at least 4072 02:56:21,239 --> 02:56:23,080 one big bug and there might even be and 4073 02:56:23,080 --> 02:56:24,600 there's likely more but there's at least 4074 02:56:24,600 --> 02:56:27,319 one big bug in here so that would be a 4075 02:56:27,319 --> 02:56:29,439 good time to take a break because after 4076 02:56:29,439 --> 02:56:32,239 this we're going to go deep into writing 4077 02:56:32,239 --> 02:56:35,120 tests for this protocol we're going to 4078 02:56:35,120 --> 02:56:36,520 write some new tests and we're going to 4079 02:56:36,520 --> 02:56:39,120 show you some really Advanced testing 4080 02:56:39,120 --> 02:56:41,880 methodologies so take that break go for 4081 02:56:41,880 --> 02:56:46,560 a walk and I'll see you in a few 4082 02:56:58,560 --> 02:56:59,800 all right so let's pull up our terminal 4083 02:56:59,800 --> 02:57:02,120 here 4084 02:57:02,120 --> 02:57:05,120 Forge 4085 02:57:10,640 --> 02:57:13,880 coverage oh we got some work to do all 4086 02:57:13,880 --> 02:57:16,560 right well no time like the present 4087 02:57:16,560 --> 02:57:18,720 let's get into it so we have some price 4088 02:57:18,720 --> 02:57:21,200 feed tests over here we probably also 4089 02:57:21,200 --> 02:57:22,960 want to set up some Constructor tests 4090 02:57:22,960 --> 02:57:24,640 right we want to make sure that stuff is 4091 02:57:24,640 --> 02:57:26,720 being initialized correctly so let's 4092 02:57:26,720 --> 02:57:29,319 copy that we'll do 4093 02:57:29,319 --> 02:57:33,359 constuctor structor test like that we'll 4094 02:57:33,359 --> 02:57:36,200 fix this so our ADHD doesn't go crazy 4095 02:57:36,200 --> 02:57:41,399 and we'll do function test something 4096 02:57:41,399 --> 02:57:43,520 what are we testing uh let's go down to 4097 02:57:43,520 --> 02:57:45,640 the instructor here and what should we 4098 02:57:45,640 --> 02:57:47,239 be testing okay well we're doing this 4099 02:57:47,239 --> 02:57:49,439 revert here so we should make sure we 4100 02:57:49,439 --> 02:57:51,720 actually are reverting correctly when 4101 02:57:51,720 --> 02:57:53,680 the lengths aren't the same so function 4102 02:57:53,680 --> 02:57:58,760 test reverts if token length doesn't 4103 02:57:58,760 --> 02:58:02,720 match price feeds 4104 02:58:02,720 --> 02:58:07,200 feeds public zoom in zoom in we're going 4105 02:58:07,200 --> 02:58:11,040 to be in here a while so function this 4106 02:58:11,040 --> 02:58:12,880 now we're going to do we're going to 4107 02:58:12,880 --> 02:58:16,880 create some address arrays address array 4108 02:58:16,880 --> 02:58:21,720 public token addresses address 4109 02:58:21,720 --> 02:58:27,600 array public price price feed 4110 02:58:27,600 --> 02:58:30,040 addresses we're going to say token 4111 02:58:30,040 --> 02:58:32,359 addresses. push we're going to push we 4112 02:58:32,359 --> 02:58:36,200 into here feed addresses 4113 02:58:36,200 --> 02:58:38,800 add. push 4114 02:58:38,800 --> 02:58:41,120 FUSD price feed and we're going to push 4115 02:58:41,120 --> 02:58:45,359 two in here oh cool BTC do we have I 4116 02:58:45,359 --> 02:58:49,600 guess we should pull BTC USD price feed 4117 02:58:49,600 --> 02:58:55,279 get this too so FUSD BTC USD price feed 4118 02:58:55,279 --> 02:58:57,920 okay oops price feed addresses price 4119 02:58:57,920 --> 02:58:59,399 feed addresses so we'll push the two of 4120 02:58:59,399 --> 02:59:02,680 those in there now we'll do 4121 02:59:02,680 --> 02:59:08,720 vm. expect revert DSC engine dot what's 4122 02:59:08,720 --> 02:59:11,439 the name of the error copy 4123 02:59:11,439 --> 02:59:15,960 this boom do selector selector like that 4124 02:59:15,960 --> 02:59:18,680 doing some toggle word wrap now X spect 4125 02:59:18,680 --> 02:59:24,080 reverts now we call a new DSC Engine 4126 02:59:24,080 --> 02:59:26,319 with the token addresses and the price 4127 02:59:26,319 --> 02:59:29,000 feed addresses oh what else goes in the 4128 02:59:29,000 --> 02:59:32,200 DSC oh we also need the address DSC 4129 02:59:32,200 --> 02:59:35,960 right address DSC so this should revert 4130 02:59:35,960 --> 02:59:39,880 let's see for test 4131 02:59:41,479 --> 02:59:44,160 DM nice okay that's passing all right 4132 02:59:44,160 --> 02:59:46,920 cool price feed tests we're testing 4133 02:59:46,920 --> 02:59:48,600 getting the USD 4134 02:59:48,600 --> 02:59:51,080 value I think we had another one down 4135 02:59:51,080 --> 02:59:53,920 here right we had something like get 4136 02:59:53,920 --> 02:59:56,880 token amount from USD we sure did this 4137 02:59:56,880 --> 02:59:59,479 is a public view so let's test this one 4138 02:59:59,479 --> 03:00:03,319 as well do function test and I usually 4139 03:00:03,319 --> 03:00:05,160 just like to paste the function names 4140 03:00:05,160 --> 03:00:08,560 especially when they're like this 4141 03:00:08,560 --> 03:00:10,680 public so we're going to do basically 4142 03:00:10,680 --> 03:00:13,080 the opposite of this this got the USD 4143 03:00:13,080 --> 03:00:14,880 value of some eth amount we're going to 4144 03:00:14,880 --> 03:00:18,160 do un 256 4145 03:00:18,160 --> 03:00:22,840 USD amount right equals we'll say 100 e 4146 03:00:22,840 --> 03:00:25,239 100 ether and then 4147 03:00:25,239 --> 03:00:26,920 256 4148 03:00:26,920 --> 03:00:29,800 expected we do a little bit of math here 4149 03:00:29,800 --> 03:00:32,160 if we have if it's 4150 03:00:32,160 --> 03:00:37,160 $2,000 per eth and we have $100 we're 4151 03:00:37,160 --> 03:00:40,560 going to do 100 divid 2,000 so 100 divid 4152 03:00:40,560 --> 03:00:44,239 2,000 0.05 so we can say expect a we is 4153 03:00:44,239 --> 03:00:48,040 0.05 ether now we'll do U into 256 4154 03:00:48,040 --> 03:00:53,439 actual we equals dsce do this function 4155 03:00:53,439 --> 03:00:56,239 with the lowercase pass in we and then 4156 03:00:56,239 --> 03:01:01,600 the USD amount assert equal 4157 03:01:01,600 --> 03:01:05,479 expected X expected weth and the actual 4158 03:01:05,479 --> 03:01:07,319 we the two of them should be the same 4159 03:01:07,319 --> 03:01:11,239 Let's test it out Forge test 4160 03:01:13,399 --> 03:01:16,279 DM nice that passed all right let's keep 4161 03:01:16,279 --> 03:01:19,120 going POS a collateral test yes test 4162 03:01:19,120 --> 03:01:22,200 revert of collateral zero that's good 4163 03:01:22,200 --> 03:01:23,800 what else let's go to this deposit 4164 03:01:23,800 --> 03:01:27,040 collateral deposit 4165 03:01:27,040 --> 03:01:30,120 collateral function okay we should also 4166 03:01:30,120 --> 03:01:31,880 revert here we're basically just going 4167 03:01:31,880 --> 03:01:34,040 to go through this whole function and 4168 03:01:34,040 --> 03:01:35,760 kind of test each line we just tested 4169 03:01:35,760 --> 03:01:37,960 this one let's tested this one so this 4170 03:01:37,960 --> 03:01:41,279 will be something like function test 4171 03:01:41,279 --> 03:01:43,680 reverts with 4172 03:01:43,680 --> 03:01:47,680 unapproved collateral 4173 03:01:48,000 --> 03:01:51,560 public we'll make an erc2 mock some Rand 4174 03:01:51,560 --> 03:01:54,080 token right we'll just make some silly 4175 03:01:54,080 --> 03:01:59,200 token new erc2 mock ran will be the name 4176 03:01:59,200 --> 03:02:02,640 ran will be the token do we have a user 4177 03:02:02,640 --> 03:02:05,040 in here think we do have a user right 4178 03:02:05,040 --> 03:02:07,439 okay yeah we do have a user let's give 4179 03:02:07,439 --> 03:02:10,600 this money to a user and then we'll give 4180 03:02:10,600 --> 03:02:13,840 him some starting amount I think we have 4181 03:02:13,840 --> 03:02:16,920 starting erc20 balance or amount 4182 03:02:16,920 --> 03:02:18,200 collateral yeah we'll do amount 4183 03:02:18,200 --> 03:02:19,800 collateral give them the amount 4184 03:02:19,800 --> 03:02:21,720 collateral we'll do 4185 03:02:21,720 --> 03:02:26,040 vm. start prank this user who has this 4186 03:02:26,040 --> 03:02:28,239 token and they're going to try to 4187 03:02:28,239 --> 03:02:29,880 deposit this collateral and we're going 4188 03:02:29,880 --> 03:02:34,520 to expect it to revert so vm. expect 4189 03:02:34,520 --> 03:02:38,960 revert and this error here is allowed 4190 03:02:38,960 --> 03:02:42,680 token right now doesn't take any 4191 03:02:42,680 --> 03:02:45,520 parameters so that's easy enough for us 4192 03:02:45,520 --> 03:02:50,359 let's just copy this we'll do DSC 4193 03:02:50,359 --> 03:02:54,920 engine. this. selector and then we'll do 4194 03:02:54,920 --> 03:02:58,239 we'll call the deposit collateral so dc. 4195 03:02:58,239 --> 03:03:01,319 deposit collateral address of that Rand 4196 03:03:01,319 --> 03:03:04,239 token right Rand token or random token 4197 03:03:04,239 --> 03:03:06,040 we'll do amount collateral as well so 4198 03:03:06,040 --> 03:03:08,479 it'll just be that whole amount and then 4199 03:03:08,479 --> 03:03:12,279 we can do vm. stop prank and that should 4200 03:03:12,279 --> 03:03:15,600 work Forge test- 4201 03:03:15,600 --> 03:03:19,760 M see if that's correct 4202 03:03:19,920 --> 03:03:21,359 yep looking good all right let's keep 4203 03:03:21,359 --> 03:03:24,120 going what else what's next deposit 4204 03:03:24,120 --> 03:03:26,439 collateral not that one okay did this 4205 03:03:26,439 --> 03:03:28,960 did this non- reentrant we could be go 4206 03:03:28,960 --> 03:03:30,479 ahead and test re-entrant I'm going to 4207 03:03:30,479 --> 03:03:32,200 skip doing that for now but we probably 4208 03:03:32,200 --> 03:03:33,920 do want to do some re-entrance the test 4209 03:03:33,920 --> 03:03:36,279 at some point but yeah I'm going to skip 4210 03:03:36,279 --> 03:03:38,720 them for now and all right cool so then 4211 03:03:38,720 --> 03:03:41,200 we can start testing some of this so if 4212 03:03:41,200 --> 03:03:44,120 they deposit collateral we should see 4213 03:03:44,120 --> 03:03:46,200 that they actually do this we should see 4214 03:03:46,200 --> 03:03:48,680 that they emit an event let's go ahead 4215 03:03:48,680 --> 03:03:54,239 and do that so let's do function test 4216 03:03:54,239 --> 03:03:56,319 canit 4217 03:03:56,319 --> 03:04:02,200 collateral leral and get account info 4218 03:04:02,200 --> 03:04:04,960 because once they deposit we have this 4219 03:04:04,960 --> 03:04:06,399 get account 4220 03:04:06,399 --> 03:04:09,720 info oh it's private right now so let's 4221 03:04:09,720 --> 03:04:11,239 actually go to the bottom let's create a 4222 03:04:11,239 --> 03:04:13,800 public version of that so we'll do 4223 03:04:13,800 --> 03:04:19,080 function get account in for 4224 03:04:19,080 --> 03:04:24,399 this will be uh external external view 4225 03:04:24,399 --> 03:04:29,200 returns return this can't address user 4226 03:04:29,200 --> 03:04:31,239 returns these two I'm going to copy this 4227 03:04:31,239 --> 03:04:32,399 go back 4228 03:04:32,399 --> 03:04:36,760 down uh external view 4229 03:04:37,120 --> 03:04:39,800 returns these two things and we're going 4230 03:04:39,800 --> 03:04:44,560 to say total DSC minted comma B value in 4231 03:04:44,560 --> 03:04:48,160 usdc equals this internal function and 4232 03:04:48,160 --> 03:04:50,439 that should be good right oh we're going 4233 03:04:50,439 --> 03:04:52,680 to do instead of message. sender we'll 4234 03:04:52,680 --> 03:04:56,960 do address user this paste the user in 4235 03:04:56,960 --> 03:04:58,880 here so now we can get the total DS 4236 03:04:58,880 --> 03:05:00,920 minted and their collateral value in USD 4237 03:05:00,920 --> 03:05:03,439 from an external view function we should 4238 03:05:03,439 --> 03:05:06,640 be able to get that information so let's 4239 03:05:06,640 --> 03:05:08,760 have them actually deposit collateral in 4240 03:05:08,760 --> 03:05:10,880 here and since we're going to be doing a 4241 03:05:10,880 --> 03:05:12,640 lot of deposited collaterals we know we 4242 03:05:12,640 --> 03:05:15,960 can actually make a modifier I'll 4243 03:05:15,960 --> 03:05:19,600 deposited collateral 4244 03:05:19,600 --> 03:05:24,720 this bm. start prank 4245 03:05:24,720 --> 03:05:28,760 userc 20 mock weth in order to deposit 4246 03:05:28,760 --> 03:05:32,120 weth we need to approve address 4247 03:05:32,120 --> 03:05:35,840 esce comma amount 4248 03:05:35,840 --> 03:05:38,680 collateral we're going to do dsce 4249 03:05:38,680 --> 03:05:41,680 deposit collateral we amount 4250 03:05:41,680 --> 03:05:45,920 collateral. stop prank. stop prank 4251 03:05:45,920 --> 03:05:48,920 underscore like this 4252 03:05:48,920 --> 03:05:52,160 modifier right and we'll have this can 4253 03:05:52,160 --> 03:05:53,920 deposit collateral and get account info 4254 03:05:53,920 --> 03:05:56,200 we'll have them deposit collateral and 4255 03:05:56,200 --> 03:05:58,359 we'll now get that account information 4256 03:05:58,359 --> 03:06:00,239 so we'll say U into 4257 03:06:00,239 --> 03:06:04,319 256 total DSC minted 4258 03:06:04,319 --> 03:06:06,120 256 4259 03:06:06,120 --> 03:06:11,800 later lateral value in USD equals dsce 4260 03:06:11,800 --> 03:06:15,120 do get account 4261 03:06:15,120 --> 03:06:19,680 information the user oh from the user 4262 03:06:19,680 --> 03:06:21,560 and now what we're going to say we're 4263 03:06:21,560 --> 03:06:23,239 going to make sure these two numbers are 4264 03:06:23,239 --> 03:06:25,720 correct the total DSC minted and the 4265 03:06:25,720 --> 03:06:27,680 collateral value in USD they should have 4266 03:06:27,680 --> 03:06:32,359 minted no DSC so un 256 expected 4267 03:06:32,359 --> 03:06:37,279 expected total DC minted yep equals zero 4268 03:06:37,279 --> 03:06:39,239 un 256 4269 03:06:39,239 --> 03:06:44,560 expected colateral value in USD equals 4270 03:06:44,560 --> 03:06:47,960 this is going to be that dsce doget 4271 03:06:47,960 --> 03:06:53,680 token amount from USD with collateral 4272 03:06:53,680 --> 03:06:57,120 value in USD so expected collateral 4273 03:06:57,120 --> 03:06:59,920 value should just be this function then 4274 03:06:59,920 --> 03:07:01,840 we can say assert 4275 03:07:01,840 --> 03:07:05,120 equal total DSC minted should be this 4276 03:07:05,120 --> 03:07:07,359 expected total DC minted and then we can 4277 03:07:07,359 --> 03:07:11,000 do assert equal feral value in USD it's 4278 03:07:11,000 --> 03:07:12,840 going to be this expected collateral 4279 03:07:12,840 --> 03:07:19,520 value in USD all right clear Forge test 4280 03:07:21,840 --> 03:07:23,880 DM all right we have a fail you know 4281 03:07:23,880 --> 03:07:25,200 what to do we're going to run it again 4282 03:07:25,200 --> 03:07:30,120 Dash VV VV see what we messed up 4283 03:07:32,279 --> 03:07:37,120 here says the assertion failed left is 4284 03:07:37,120 --> 03:07:39,640 this number and right is a much smaller 4285 03:07:39,640 --> 03:07:41,720 number this is why it's not cool to do 4286 03:07:41,720 --> 03:07:43,520 two types of asserts but I know it's the 4287 03:07:43,520 --> 03:07:44,960 second one so we're saying the clal 4288 03:07:44,960 --> 03:07:47,880 value in USD is this but the right side 4289 03:07:47,880 --> 03:07:50,680 side is expected collateral value is 4290 03:07:50,680 --> 03:07:52,319 going to be this so let's see what's 4291 03:07:52,319 --> 03:07:56,239 wrong here 1 2 3 4 5 6 7 8 9 10 1 2 3 4 4292 03:07:56,239 --> 03:07:59,080 5 6 7 8 so it's 4293 03:07:59,080 --> 03:08:01,520 saying collateral value in USD is 20 4294 03:08:01,520 --> 03:08:05,399 grand does that make sense well it's 10 4295 03:08:05,399 --> 03:08:08,200 ether time 4296 03:08:08,200 --> 03:08:12,560 $2,000 equals 20 grand right so that is 4297 03:08:12,560 --> 03:08:14,200 so 4298 03:08:14,200 --> 03:08:16,720 the collateral value in USD is right it 4299 03:08:16,720 --> 03:08:19,319 looks like my right is wrong 4300 03:08:19,319 --> 03:08:21,520 collateral expected collateral value in 4301 03:08:21,520 --> 03:08:25,399 USD dc. from token amount we oh what the 4302 03:08:25,399 --> 03:08:28,479 heck why am I doing that 1 2 3 4 5 6 7 8 4303 03:08:28,479 --> 03:08:34,720 9 10 1 2 3 4 5 6 7 8 so it's saying that 4304 03:08:34,720 --> 03:08:38,239 $20,000 is equal to 10 e so I don't know 4305 03:08:38,239 --> 03:08:41,479 why I'm calling this oh this is actually 4306 03:08:41,479 --> 03:08:46,200 expected expected deposit amount and we 4307 03:08:46,200 --> 03:08:48,720 shouldn't be comparing these two to this 4308 03:08:48,720 --> 03:08:50,279 should be compared to the 4309 03:08:50,279 --> 03:08:53,439 amount collateral right so 10 is how 4310 03:08:53,439 --> 03:08:55,680 much collateral we're putting in here 4311 03:08:55,680 --> 03:08:59,319 and then collateral value in USD yeah 4312 03:08:59,319 --> 03:09:00,840 that's correct so we're getting we're 4313 03:09:00,840 --> 03:09:02,479 using this collateral value in USD to 4314 03:09:02,479 --> 03:09:04,880 get the expected deposit amount that 4315 03:09:04,880 --> 03:09:09,600 looks more correct okay cool Forge test- 4316 03:09:09,600 --> 03:09:12,520 M and this is one of the kind of weird 4317 03:09:12,520 --> 03:09:14,600 Parts about writing test right okay cool 4318 03:09:14,600 --> 03:09:17,600 we fixed it sometimes your test is wrong 4319 03:09:17,600 --> 03:09:18,880 like what we just showed here I wrote my 4320 03:09:18,880 --> 03:09:21,680 test wrong but sometimes your code is 4321 03:09:21,680 --> 03:09:23,600 wrong and that's what these tests really 4322 03:09:23,600 --> 03:09:25,359 should be testing hey when is the code 4323 03:09:25,359 --> 03:09:27,439 actually wrong making some progress 4324 03:09:27,439 --> 03:09:30,439 Forge 4325 03:09:34,359 --> 03:09:36,080 coverage not a whole lot of great 4326 03:09:36,080 --> 03:09:38,800 coverage here let's keep going so 4327 03:09:38,800 --> 03:09:40,520 instead of me just kind of walking you 4328 03:09:40,520 --> 03:09:43,960 through the rest of these tests you know 4329 03:09:43,960 --> 03:09:46,520 how to write these tests in this file 4330 03:09:46,520 --> 03:09:48,520 I'm not really going to show you any 4331 03:09:48,520 --> 03:09:51,279 more unique tests right but like I said 4332 03:09:51,279 --> 03:09:53,880 there is at least one glaring issue in 4333 03:09:53,880 --> 03:09:57,479 our DSC engine. Soul there's a big issue 4334 03:09:57,479 --> 03:09:59,120 in here right we definitely need some 4335 03:09:59,120 --> 03:10:00,800 more getter functions as well so write 4336 03:10:00,800 --> 03:10:02,920 getter functions as you test but there's 4337 03:10:02,920 --> 03:10:05,479 at least one giant issue so what I'm 4338 03:10:05,479 --> 03:10:07,399 going to do now is instead of me 4339 03:10:07,399 --> 03:10:09,080 literally walking you through the rest 4340 03:10:09,080 --> 03:10:11,800 of these tests I mean it's you can see 4341 03:10:11,800 --> 03:10:13,399 all the tests in here is I'm going to 4342 03:10:13,399 --> 03:10:15,359 challenge you to write these tests 4343 03:10:15,359 --> 03:10:19,279 yourself to get this for for coverage up 4344 03:10:19,279 --> 03:10:22,000 remember you can do Forge coverage to 4345 03:10:22,000 --> 03:10:24,160 obviously see what's going on we're 4346 03:10:24,160 --> 03:10:26,880 focusing on this one right now you can 4347 03:10:26,880 --> 03:10:30,600 also do Forge coverage D- report to see 4348 03:10:30,600 --> 03:10:34,520 oh D- report debug to actually see the 4349 03:10:34,520 --> 03:10:37,319 exact lines that are missing right and 4350 03:10:37,319 --> 03:10:38,760 all these things are just kind of line 4351 03:10:38,760 --> 03:10:41,279 items line items whatever IC doesn't 4352 03:10:41,279 --> 03:10:42,840 matter scroll scroll scroll scroll 4353 03:10:42,840 --> 03:10:45,080 scroll okay these are the ones that are 4354 03:10:45,080 --> 03:10:47,680 actually issues right what which one's 4355 03:10:47,680 --> 03:10:50,359 this oh that's that is the decentralized 4356 03:10:50,359 --> 03:10:51,520 stable coin we want to write test for 4357 03:10:51,520 --> 03:10:54,399 that as well but let's just focus on 4358 03:10:54,399 --> 03:10:57,319 this massive list up here DSC engine 4359 03:10:57,319 --> 03:10:59,040 focus on this list and write some tests 4360 03:10:59,040 --> 03:11:01,160 for this because yeah the rest of these 4361 03:11:01,160 --> 03:11:04,120 unit tests or staging tests we're not 4362 03:11:04,120 --> 03:11:06,200 going to learn anything new this is just 4363 03:11:06,200 --> 03:11:08,319 one of these things that you got to do 4364 03:11:08,319 --> 03:11:09,520 that you got to get good at that you got 4365 03:11:09,520 --> 03:11:11,760 to write now you don't have to get to 4366 03:11:11,760 --> 03:11:15,040 100% if you get it to like 85 90 that's 4367 03:11:15,040 --> 03:11:17,399 pretty good some of these tests are 4368 03:11:17,399 --> 03:11:20,479 actually very difficult to write but you 4369 03:11:20,479 --> 03:11:22,800 should writing these tests should find a 4370 03:11:22,800 --> 03:11:26,160 glaring bug in at least one of these 4371 03:11:26,160 --> 03:11:28,239 functions and maybe if you find more 4372 03:11:28,239 --> 03:11:30,640 than one glaring bug that's great make a 4373 03:11:30,640 --> 03:11:33,399 PR make an issue to the repo but I want 4374 03:11:33,399 --> 03:11:36,040 you to pause the video and take some 4375 03:11:36,040 --> 03:11:37,720 time right it might be an hour it might 4376 03:11:37,720 --> 03:11:39,279 be two hours it might be 30 minutes 4377 03:11:39,279 --> 03:11:40,920 right depends on how quick your AI buddy 4378 03:11:40,920 --> 03:11:43,520 is how quick you are and write some more 4379 03:11:43,520 --> 03:11:45,840 tests and sometimes actually you can 4380 03:11:45,840 --> 03:11:48,000 even come to your contracts 4381 03:11:48,000 --> 03:11:50,200 and you can do something like grab this 4382 03:11:50,200 --> 03:11:54,560 go over to your chat GTP say hey this is 4383 03:11:54,560 --> 03:11:57,600 one of my solidity functions can you 4384 03:11:57,600 --> 03:12:01,880 write some tests for it for it in 4385 03:12:01,880 --> 03:12:04,479 Foundry and because it doesn't know what 4386 03:12:04,479 --> 03:12:06,840 Foundry is it's going to totally bunk it 4387 03:12:06,840 --> 03:12:09,160 up but you could use chat PT to help you 4388 03:12:09,160 --> 03:12:10,520 write some tests as well help you get 4389 03:12:10,520 --> 03:12:13,000 your coverage up but there's definitely 4390 03:12:13,000 --> 03:12:14,880 one major issue in here you might even 4391 03:12:14,880 --> 03:12:17,359 need to go back and refactor some code 4392 03:12:17,359 --> 03:12:19,080 right I'm going to say keep that to a 4393 03:12:19,080 --> 03:12:20,760 minimum though but there are some spots 4394 03:12:20,760 --> 03:12:22,279 where maybe you need to refactor your 4395 03:12:22,279 --> 03:12:25,160 code to make writing tests easier maybe 4396 03:12:25,160 --> 03:12:26,160 you need to write some more helper 4397 03:12:26,160 --> 03:12:28,399 functions right take this time to 4398 03:12:28,399 --> 03:12:30,720 experiment and tinker and think what 4399 03:12:30,720 --> 03:12:33,840 should I do how can I make sure my code 4400 03:12:33,840 --> 03:12:37,160 is safe and importantly how can I write 4401 03:12:37,160 --> 03:12:38,880 enough tests to find this bug that 4402 03:12:38,880 --> 03:12:41,439 Patrick is talking about right maybe you 4403 03:12:41,439 --> 03:12:43,600 do some refactoring now don't write any 4404 03:12:43,600 --> 03:12:45,319 fuzz tests yet we are going to go over 4405 03:12:45,319 --> 03:12:47,200 fuzz tests in a little bit but yeah just 4406 03:12:47,200 --> 03:12:48,399 try to write the rest of these unit 4407 03:12:48,399 --> 03:12:50,239 tests rest of these pseudo integration 4408 03:12:50,239 --> 03:12:52,120 tests and I'll see you when you come out 4409 03:12:52,120 --> 03:12:53,520 the other side but when you finish 4410 03:12:53,520 --> 03:12:56,520 writing the tests take a break take a 4411 03:12:56,520 --> 03:12:59,600 minute I want this to sink in this is 4412 03:12:59,600 --> 03:13:02,760 one this is the most advanced lesson 4413 03:13:02,760 --> 03:13:05,680 that you're going to take in this course 4414 03:13:05,680 --> 03:13:07,000 and to be honest probably it's going to 4415 03:13:07,000 --> 03:13:08,319 be one of the most advanced courses 4416 03:13:08,319 --> 03:13:10,760 you'll ever take in solidity okay so I 4417 03:13:10,760 --> 03:13:12,600 want you to take your time with this I 4418 03:13:12,600 --> 03:13:14,760 want you to understand what's going on 4419 03:13:14,760 --> 03:13:16,120 sometimes it might even make sense for 4420 03:13:16,120 --> 03:13:18,560 you to go oh well what is maker Dow do 4421 03:13:18,560 --> 03:13:20,520 how can I learn more about stable coins 4422 03:13:20,520 --> 03:13:23,200 where else can I go maybe I can ask chat 4423 03:13:23,200 --> 03:13:25,640 gbt for questions right those of you who 4424 03:13:25,640 --> 03:13:27,560 are going ah I really don't want to do 4425 03:13:27,560 --> 03:13:30,520 that you can 100% just copy paste mine 4426 03:13:30,520 --> 03:13:34,120 but I recommend you go through this 4427 03:13:34,120 --> 03:13:37,880 exercise okay lock off some time and 4428 03:13:37,880 --> 03:13:41,319 I'll see you soon 4429 03:13:53,640 --> 03:13:55,800 all right welcome back did you find the 4430 03:13:55,800 --> 03:13:58,439 bug how your test looking if you run 4431 03:13:58,439 --> 03:14:01,200 Forge coverage did it look something at 4432 03:14:01,200 --> 03:14:03,880 least like this do they look better do 4433 03:14:03,880 --> 03:14:06,239 they look worse so I copy pasted a whole 4434 03:14:06,239 --> 03:14:08,520 bunch of my tests and we're looking 4435 03:14:08,520 --> 03:14:10,760 pretty good here we definitely should be 4436 03:14:10,760 --> 03:14:12,160 increasing the test coverage of our 4437 03:14:12,160 --> 03:14:13,960 branches and we can definitely get these 4438 03:14:13,960 --> 03:14:16,120 a little bit higher but for at least the 4439 03:14:16,120 --> 03:14:17,720 DSC engine 4440 03:14:17,720 --> 03:14:19,800 we have a much better code coverage 4441 03:14:19,800 --> 03:14:21,720 going on here and I hope your code 4442 03:14:21,720 --> 03:14:24,080 coverage looks looks like this too now 4443 03:14:24,080 --> 03:14:25,359 additionally I did a little bit of 4444 03:14:25,359 --> 03:14:27,920 refactoring writing these codes one of 4445 03:14:27,920 --> 03:14:30,479 the main things that I added was I added 4446 03:14:30,479 --> 03:14:33,160 this calculate health Factor function 4447 03:14:33,160 --> 03:14:34,560 reason I added this calculate health 4448 03:14:34,560 --> 03:14:37,319 Factor internal function was so that I 4449 03:14:37,319 --> 03:14:39,560 could have this public calculate health 4450 03:14:39,560 --> 03:14:41,840 Factor function and then in my test what 4451 03:14:41,840 --> 03:14:44,239 I could do is have an expected Health 4452 03:14:44,239 --> 03:14:48,120 factor and that way when function breaks 4453 03:14:48,120 --> 03:14:50,880 Health factor I can pass it into the 4454 03:14:50,880 --> 03:14:53,640 expected Health Factor error revert here 4455 03:14:53,640 --> 03:14:55,399 in one of my tests right when we're 4456 03:14:55,399 --> 03:14:58,080 testing to expect an event I added this 4457 03:14:58,080 --> 03:15:00,720 new function to do that and having a 4458 03:15:00,720 --> 03:15:02,080 public function like calculate health 4459 03:15:02,080 --> 03:15:04,040 Factor might make it easier for people 4460 03:15:04,040 --> 03:15:06,920 to see what their health Factor might be 4461 03:15:06,920 --> 03:15:08,600 if they make some change right so that 4462 03:15:08,600 --> 03:15:10,560 was one of the big ones I made and the 4463 03:15:10,560 --> 03:15:14,120 bug was in the health Factor as well or 4464 03:15:14,120 --> 03:15:16,319 at least the bug that I planned to be in 4465 03:15:16,319 --> 03:15:19,199 there so in my calculate health factor 4466 03:15:19,199 --> 03:15:22,319 which instead of in our underscore 4467 03:15:22,319 --> 03:15:25,880 Health Factor function I'm just calling 4468 03:15:25,880 --> 03:15:27,600 this calculate health Factor function 4469 03:15:27,600 --> 03:15:28,479 right I'm getting the account 4470 03:15:28,479 --> 03:15:29,960 information and then just passing it to 4471 03:15:29,960 --> 03:15:32,439 this calculate health factor and in this 4472 03:15:32,439 --> 03:15:35,439 we needed to add a checker for if the 4473 03:15:35,439 --> 03:15:38,199 total DS cemented was zero and if it's 4474 03:15:38,199 --> 03:15:40,520 zero then we said okay cool your health 4475 03:15:40,520 --> 03:15:42,439 factor is going to be the max U and 256 4476 03:15:42,439 --> 03:15:44,160 or something like that right and the 4477 03:15:44,160 --> 03:15:47,000 reason that we need this is if someone 4478 03:15:47,000 --> 03:15:50,000 deposits a ton of collateral but has no 4479 03:15:50,000 --> 03:15:52,960 dsed well their health factor is going 4480 03:15:52,960 --> 03:15:56,160 to divide by zero which we can't have so 4481 03:15:56,160 --> 03:15:57,479 calculating someone's Health Factor 4482 03:15:57,479 --> 03:15:58,920 after they deposit collateral would 4483 03:15:58,920 --> 03:16:01,439 result in an issue we'd Break Stuff we 4484 03:16:01,439 --> 03:16:03,199 don't want to break stuff and then the 4485 03:16:03,199 --> 03:16:06,479 final piece that I did was I added a ton 4486 03:16:06,479 --> 03:16:09,040 of external view functions just to make 4487 03:16:09,040 --> 03:16:10,680 it easier to read and interact with this 4488 03:16:10,680 --> 03:16:13,199 protocol so those are some of the 4489 03:16:13,199 --> 03:16:14,920 refactors that I did and then obviously 4490 03:16:14,920 --> 03:16:16,960 we added a ton of tests there's nothing 4491 03:16:16,960 --> 03:16:19,920 nothing really new in here it's just you 4492 03:16:19,920 --> 03:16:21,960 got to write the test right everything 4493 03:16:21,960 --> 03:16:24,199 in here you've learned you can do and if 4494 03:16:24,199 --> 03:16:28,040 you wrote some tests and you got this to 4495 03:16:28,040 --> 03:16:31,000 a high level of coverage around 90% you 4496 03:16:31,000 --> 03:16:33,800 should be incredibly proud of yourself 4497 03:16:33,800 --> 03:16:35,399 this is hard to write test for this is 4498 03:16:35,399 --> 03:16:37,640 very difficult project so you should be 4499 03:16:37,640 --> 03:16:40,080 incredibly proud of yourself for just 4500 03:16:40,080 --> 03:16:43,000 getting this far but guess what we're 4501 03:16:43,000 --> 03:16:46,199 not even done because we want to make 4502 03:16:46,199 --> 03:16:49,960 this code so freaking amazing we need to 4503 03:16:49,960 --> 03:16:51,840 think a little bit about security and 4504 03:16:51,840 --> 03:16:54,479 we're not going to go too deep into 4505 03:16:54,479 --> 03:16:57,960 security however we should ask ourselves 4506 03:16:57,960 --> 03:16:59,560 some questions right when we're working 4507 03:16:59,560 --> 03:17:02,279 with a code base we want to say hm we 4508 03:17:02,279 --> 03:17:05,640 should always always ask what are our 4509 03:17:05,640 --> 03:17:08,960 invariant /properties what are the 4510 03:17:08,960 --> 03:17:12,520 invariance slopies of the system and 4511 03:17:12,520 --> 03:17:15,040 that way we can write some stateful and 4512 03:17:15,040 --> 03:17:17,960 stateless fuz tests now I know we 4513 03:17:17,960 --> 03:17:20,520 briefly went over one form of fuzz 4514 03:17:20,520 --> 03:17:22,239 testing but now we're going to go a 4515 03:17:22,239 --> 03:17:24,880 little bit deeper and I made a video on 4516 03:17:24,880 --> 03:17:26,680 state less and stateful fuzz testing 4517 03:17:26,680 --> 03:17:28,439 recently and we're going to go ahead and 4518 03:17:28,439 --> 03:17:29,960 watch that so you can have a better 4519 03:17:29,960 --> 03:17:31,720 understanding of what stat less and 4520 03:17:31,720 --> 03:17:34,520 stateful fuzz testing is and why it's so 4521 03:17:34,520 --> 03:17:36,560 important especially for a project like 4522 03:17:36,560 --> 03:17:39,800 this that has potentially a lot of money 4523 03:17:39,800 --> 03:17:42,479 moving around so let's understand what 4524 03:17:42,479 --> 03:17:47,680 fuzz testing is let's watch this video 4525 03:17:50,000 --> 03:17:51,279 all right contracts are written and 4526 03:17:51,279 --> 03:17:53,080 tested can I ship my code no I can 4527 03:17:53,080 --> 03:17:54,439 easily break this with a flash loan 4528 03:17:54,439 --> 03:17:55,880 attack a crap I didn't think about that 4529 03:17:55,880 --> 03:17:57,720 let me fix all right how about 4530 03:17:57,720 --> 03:18:00,399 now if I make a flash loan on a I can 4531 03:18:00,399 --> 03:18:02,840 use that loan to lock up a cdb on Faker 4532 03:18:02,840 --> 03:18:04,439 Dow and I can exploit the Oracle by 4533 03:18:04,439 --> 03:18:06,199 re-entering your dinner reservation at 4534 03:18:06,199 --> 03:18:08,319 Chili's causing a bridge malfunction on 4535 03:18:08,319 --> 03:18:10,040 the flux capacitor bypassing the 4536 03:18:10,040 --> 03:18:12,840 possibility media I can exploit your 4537 03:18:12,840 --> 03:18:17,040 contract I exploit your contract 4538 03:18:17,040 --> 03:18:18,600 most of the time hacks will come from a 4539 03:18:18,600 --> 03:18:20,160 scenario that you didn't think about or 4540 03:18:20,160 --> 03:18:22,160 write a test for but what if I told you 4541 03:18:22,160 --> 03:18:23,680 that you could write a test that cannot 4542 03:18:23,680 --> 03:18:26,160 check for just one scenario but every 4543 03:18:26,160 --> 03:18:28,439 scenario let's get froggy fuzz testing 4544 03:18:28,439 --> 03:18:30,160 or fuzzing is when you supply random 4545 03:18:30,160 --> 03:18:31,920 data to your system in an attempt to 4546 03:18:31,920 --> 03:18:33,439 break it so if this balloon is our 4547 03:18:33,439 --> 03:18:35,680 system/ code it's us doing random stuff 4548 03:18:35,680 --> 03:18:40,399 and attempt to break it this is chain 4549 03:18:41,439 --> 03:18:43,479 link now why would we want to do all 4550 03:18:43,479 --> 03:18:44,600 that let's say we have this function 4551 03:18:44,600 --> 03:18:46,359 called do stuff it takes an integer as 4552 03:18:46,359 --> 03:18:48,120 an input parameter and we know that no 4553 03:18:48,120 --> 03:18:49,880 matter what we give it as an input our 4554 03:18:49,880 --> 03:18:51,760 variable should always be zero should 4555 03:18:51,760 --> 03:18:53,399 always be zero the fact that this 4556 03:18:53,399 --> 03:18:55,120 variable should always be zero is known 4557 03:18:55,120 --> 03:18:57,239 as our invariant or our property of the 4558 03:18:57,239 --> 03:18:59,040 system that should always hold in our 4559 03:18:59,040 --> 03:19:00,800 balloon example if we Market our balloon 4560 03:19:00,800 --> 03:19:02,840 as indestructible or unbreakable or 4561 03:19:02,840 --> 03:19:04,560 unpoppable the invariant that would hold 4562 03:19:04,560 --> 03:19:07,000 would this balloon cannot be broken and 4563 03:19:07,000 --> 03:19:08,960 unlike this balloon in real life we can 4564 03:19:08,960 --> 03:19:10,800 write a test that will call the do stuff 4565 03:19:10,800 --> 03:19:13,560 function many times with random data and 4566 03:19:13,560 --> 03:19:15,160 check to see that our should always be 4567 03:19:15,160 --> 03:19:17,439 zero variable is always zero now a 4568 03:19:17,439 --> 03:19:19,319 normal unit test for our code might look 4569 03:19:19,319 --> 03:19:21,479 like this we pass a single data point we 4570 03:19:21,479 --> 03:19:23,040 call the function and then we do our 4571 03:19:23,040 --> 03:19:24,920 assertion to make sure that should 4572 03:19:24,920 --> 03:19:27,199 always be zero is in fact zero and with 4573 03:19:27,199 --> 03:19:28,640 this we might think our code is covered 4574 03:19:28,640 --> 03:19:30,160 but if we look back at our do stuff 4575 03:19:30,160 --> 03:19:31,960 function a little bit closer we can 4576 03:19:31,960 --> 03:19:34,279 clearly see that if our data input is 4577 03:19:34,279 --> 03:19:36,600 two should always be zero will end up 4578 03:19:36,600 --> 03:19:39,040 being one this would break our invariant 4579 03:19:39,040 --> 03:19:41,359 should always be zero will not be zero 4580 03:19:41,359 --> 03:19:42,560 now this may seem obvious for this 4581 03:19:42,560 --> 03:19:43,680 function but sometimes you'll have a 4582 03:19:43,680 --> 03:19:46,580 function that looks like this 4583 03:19:46,580 --> 03:19:51,319 [Music] 4584 03:19:51,319 --> 03:19:52,760 it would be insane to write a test case 4585 03:19:52,760 --> 03:19:54,239 for every single possible integer or 4586 03:19:54,239 --> 03:19:56,080 scenario so we need a programmatic way 4587 03:19:56,080 --> 03:19:57,920 to find this scenario now in our code we 4588 03:19:57,920 --> 03:19:59,680 also see a second exploit but we'll get 4589 03:19:59,680 --> 03:20:01,000 to that in a minute now there are two 4590 03:20:01,000 --> 03:20:02,640 popular methodologies to find these Ed 4591 03:20:02,640 --> 03:20:05,120 cases fuzz test/ invariant tests and 4592 03:20:05,120 --> 03:20:06,720 symbolic execution SL formal 4593 03:20:06,720 --> 03:20:08,359 verification we'll save the ladder for 4594 03:20:08,359 --> 03:20:09,840 another video if we were writing our 4595 03:20:09,840 --> 03:20:11,600 code in Foundry this would be our unit 4596 03:20:11,600 --> 03:20:13,600 test writing a fuzz test in Foundry 4597 03:20:13,600 --> 03:20:15,279 where we do all this random inputting is 4598 03:20:15,279 --> 03:20:17,160 going to be really similar instead of us 4599 03:20:17,160 --> 03:20:18,960 manually selecting our data right in our 4600 03:20:18,960 --> 03:20:20,640 test parameter we'll add our variable 4601 03:20:20,640 --> 03:20:22,680 comment out this line and that's it now 4602 03:20:22,680 --> 03:20:24,479 when we run a Foundry test here Foundry 4603 03:20:24,479 --> 03:20:26,680 will automatically randomized data run 4604 03:20:26,680 --> 03:20:28,239 through a code with a ton of different 4605 03:20:28,239 --> 03:20:30,239 examples this is as if they run with 4606 03:20:30,239 --> 03:20:33,000 data equals 0 data equals 1 data equals 4607 03:20:33,000 --> 03:20:34,760 this number that's a t but whatever you 4608 03:20:34,760 --> 03:20:36,359 get the picture now if I run my unit 4609 03:20:36,359 --> 03:20:38,319 test you'll see that the unit test 4610 03:20:38,319 --> 03:20:40,040 actually passes however if we run this 4611 03:20:40,040 --> 03:20:41,760 fuzz test you'll see it actually gives 4612 03:20:41,760 --> 03:20:43,920 us an output where it says assertion 4613 03:20:43,920 --> 03:20:45,960 violated counter example gives us the 4614 03:20:45,960 --> 03:20:48,319 call data and the arguments it was able 4615 03:20:48,319 --> 03:20:50,600 to find out by randomly throwing data at 4616 03:20:50,600 --> 03:20:52,800 our function call that two breaks our 4617 03:20:52,800 --> 03:20:55,160 invariant AKA it makes a such that 4618 03:20:55,160 --> 03:20:57,439 should always be zero is not zero now 4619 03:20:57,439 --> 03:20:59,000 it's really doing semi- random data 4620 03:20:59,000 --> 03:21:00,479 instead of purely random data and the 4621 03:21:00,479 --> 03:21:02,199 way your fuzzer picks the random data 4622 03:21:02,199 --> 03:21:04,399 matters it won't be able to go over 4623 03:21:04,399 --> 03:21:07,040 every single possible un 256 so 4624 03:21:07,040 --> 03:21:08,359 understanding how your fuzzers pick the 4625 03:21:08,359 --> 03:21:09,800 random data is an advanced thing that 4626 03:21:09,800 --> 03:21:11,399 you should learn later on at the moment 4627 03:21:11,399 --> 03:21:13,199 I think the trailer bits a kidna SL 4628 03:21:13,199 --> 03:21:14,560 optic integration is probably the best 4629 03:21:14,560 --> 03:21:16,359 fuzzer out there and it easily has the 4630 03:21:16,359 --> 03:21:18,399 best logo of all time but ripped Jesus 4631 03:21:18,399 --> 03:21:20,199 is a solid second so now that we have 4632 03:21:20,199 --> 03:21:22,120 our counter example here we can use this 4633 03:21:22,120 --> 03:21:24,359 to go back into our contract find out ah 4634 03:21:24,359 --> 03:21:26,239 okay so we are doing this wrong delete 4635 03:21:26,239 --> 03:21:28,319 this line and then run our test again 4636 03:21:28,319 --> 03:21:30,319 and see that it does indeed pass what's 4637 03:21:30,319 --> 03:21:32,040 important is this number down here the 4638 03:21:32,040 --> 03:21:35,239 number of runs so this did 256 different 4639 03:21:35,239 --> 03:21:37,279 random inputs to make our test run in 4640 03:21:37,279 --> 03:21:38,680 Foundry you can change the number of 4641 03:21:38,680 --> 03:21:41,359 runs in your Foundry Tomo file by just 4642 03:21:41,359 --> 03:21:43,479 adding a section like this rerunning 4643 03:21:43,479 --> 03:21:45,199 your test and now you'll see it did a 4644 03:21:45,199 --> 03:21:46,560 thousand different examples the number 4645 03:21:46,560 --> 03:21:47,640 number of runs is really important 4646 03:21:47,640 --> 03:21:49,080 obviously because more runs is more 4647 03:21:49,080 --> 03:21:51,239 random inputs more use cases more chance 4648 03:21:51,239 --> 03:21:52,840 that you'll actually catch the issue and 4649 03:21:52,840 --> 03:21:55,080 now congrats that's the basic of fuss 4650 03:21:55,080 --> 03:21:56,520 testing let's just do a little recap 4651 03:21:56,520 --> 03:21:57,960 here before going further the first 4652 03:21:57,960 --> 03:21:59,880 thing you need to do is understand our 4653 03:21:59,880 --> 03:22:02,080 invariant or property of the system that 4654 03:22:02,080 --> 03:22:04,399 must always hold and our example should 4655 03:22:04,399 --> 03:22:06,399 always be zero was our invariant 4656 03:22:06,399 --> 03:22:07,640 understand your invariant and then write 4657 03:22:07,640 --> 03:22:09,760 a test that would input random data to 4658 03:22:09,760 --> 03:22:11,840 try to break that invariant now if we go 4659 03:22:11,840 --> 03:22:13,359 back to our example contract though 4660 03:22:13,359 --> 03:22:14,920 you'll see with our fuzz test we were 4661 03:22:14,920 --> 03:22:16,920 able to find this first use case case 4662 03:22:16,920 --> 03:22:18,880 however it didn't find this second 4663 03:22:18,880 --> 03:22:20,479 scenario where should always be zero was 4664 03:22:20,479 --> 03:22:22,600 set to one if hidden value was seven in 4665 03:22:22,600 --> 03:22:24,479 order for this to revert hidden value 4666 03:22:24,479 --> 03:22:26,080 would need to be seven and the only way 4667 03:22:26,080 --> 03:22:28,800 to set hidden value to seven would be to 4668 03:22:28,800 --> 03:22:30,880 First Call do stuff with seven which 4669 03:22:30,880 --> 03:22:32,359 would set hidden value down here and 4670 03:22:32,359 --> 03:22:34,279 then call do stuff again with anything 4671 03:22:34,279 --> 03:22:36,120 our fuzz test as written would never be 4672 03:22:36,120 --> 03:22:37,560 able to find this that's because this 4673 03:22:37,560 --> 03:22:40,199 fuzz test is known as a stateless fuzz 4674 03:22:40,199 --> 03:22:41,800 test which is where the state of the 4675 03:22:41,800 --> 03:22:43,880 previous run is discarded for the next 4676 03:22:43,880 --> 03:22:45,479 run if we go back to our balloon example 4677 03:22:45,479 --> 03:22:46,920 stateless fuzzy would be doing something 4678 03:22:46,920 --> 03:22:48,760 to the balloon for one fuzz run then 4679 03:22:48,760 --> 03:22:50,520 discarding that balloon and blowing up a 4680 03:22:50,520 --> 03:22:52,880 new balloon for each fuzz run however 4681 03:22:52,880 --> 03:22:54,800 instead of doing State less fuzzing we 4682 03:22:54,800 --> 03:22:56,760 could do state full fuzzing stateful 4683 03:22:56,760 --> 03:22:58,640 fuzzing is where the ending state of our 4684 03:22:58,640 --> 03:23:01,040 previous fuzz run is the starting state 4685 03:23:01,040 --> 03:23:03,040 of the next fuzz run or example instead 4686 03:23:03,040 --> 03:23:04,960 of blowing up a new balloon for each one 4687 03:23:04,960 --> 03:23:06,560 of these runs we just use the same 4688 03:23:06,560 --> 03:23:08,319 balloon to do multiple random things to 4689 03:23:08,319 --> 03:23:11,000 it combined is considered one fuzz run 4690 03:23:11,000 --> 03:23:13,199 so a single fuzz run on a stateless fuzz 4691 03:23:13,199 --> 03:23:15,640 run would be having data be7 calling do 4692 03:23:15,640 --> 03:23:17,720 stuff just using the same contract that 4693 03:23:17,720 --> 03:23:20,080 we just called do stuff on and then call 4694 03:23:20,080 --> 03:23:21,439 another function on it if this was a 4695 03:23:21,439 --> 03:23:23,239 unit test we had we would of course see 4696 03:23:23,239 --> 03:23:25,520 this get violated but as you can see 4697 03:23:25,520 --> 03:23:27,199 with sufficiently complicated code 4698 03:23:27,199 --> 03:23:28,439 coming with these very specific 4699 03:23:28,439 --> 03:23:29,840 scenarios are going to be missed to 4700 03:23:29,840 --> 03:23:31,720 write a stateful fuzz test in Foundry 4701 03:23:31,720 --> 03:23:33,760 you need to use the invariant keyword 4702 03:23:33,760 --> 03:23:35,239 and it requires a little bit of setup 4703 03:23:35,239 --> 03:23:36,520 and don't get too confused by the 4704 03:23:36,520 --> 03:23:38,319 invariant keyword here yes it's being a 4705 03:23:38,319 --> 03:23:40,199 little overloaded right invariant test 4706 03:23:40,199 --> 03:23:42,359 in Foundry we first need to import this 4707 03:23:42,359 --> 03:23:45,040 STD invariant contract and inherit it in 4708 03:23:45,040 --> 03:23:47,720 our test contract then we need to tell 4709 03:23:47,720 --> 03:23:50,000 Foundry which contract to call random 4710 03:23:50,000 --> 03:23:51,640 functions on since we only have one 4711 03:23:51,640 --> 03:23:53,120 contract with one function we're going 4712 03:23:53,120 --> 03:23:54,920 to tell Foundry that my contract should 4713 03:23:54,920 --> 03:23:56,720 be called and it's allowed to call any 4714 03:23:56,720 --> 03:23:58,640 of the functions in my contract so we'd 4715 03:23:58,640 --> 03:24:00,720 say hey the Target contract for you is 4716 03:24:00,720 --> 03:24:02,600 going to be the address of example 4717 03:24:02,600 --> 03:24:04,479 contract Foundry is smart enough to know 4718 03:24:04,479 --> 03:24:06,319 okay it's going to grab any and all of 4719 03:24:06,319 --> 03:24:08,080 the functions from my contract and call 4720 03:24:08,080 --> 03:24:09,840 them in random orders with random data 4721 03:24:09,840 --> 03:24:11,120 so it's going to call do stuff with 4722 03:24:11,120 --> 03:24:12,359 random data and then it's going to call 4723 03:24:12,359 --> 03:24:13,920 do stuff with random data and then it's 4724 03:24:13,920 --> 03:24:15,279 going to call do stuff with random data 4725 03:24:15,279 --> 03:24:16,720 since do stuff is the only function fun 4726 03:24:16,720 --> 03:24:18,520 now we can write our invariant by saying 4727 03:24:18,520 --> 03:24:21,840 function invariant test always is zero 4728 03:24:21,840 --> 03:24:24,080 public and we can just add our resert 4729 03:24:24,080 --> 03:24:25,720 cert our example contract that should 4730 03:24:25,720 --> 03:24:28,080 always be zero is zero so it'll run do 4731 03:24:28,080 --> 03:24:29,760 stuff with some random data if it 4732 03:24:29,760 --> 03:24:32,000 happens across seven it'll set hidden 4733 03:24:32,000 --> 03:24:34,600 value to7 and then it'll call do stuff 4734 03:24:34,600 --> 03:24:37,439 again with hidden values starting at 4735 03:24:37,439 --> 03:24:39,600 seven which will trigger this 4736 03:24:39,600 --> 03:24:41,560 conditional so now if we run this test 4737 03:24:41,560 --> 03:24:43,120 we can see it does indeed find a 4738 03:24:43,120 --> 03:24:45,319 sequence where our invariant or our 4739 03:24:45,319 --> 03:24:47,439 assertion or our property is broken we 4740 03:24:47,439 --> 03:24:49,640 can see first on my contract it's going 4741 03:24:49,640 --> 03:24:51,760 to call do stuff with an argument of 4742 03:24:51,760 --> 03:24:54,000 seven and then it's going to call my 4743 03:24:54,000 --> 03:24:56,359 contract with an argument of some random 4744 03:24:56,359 --> 03:24:57,600 number because it doesn't matter what 4745 03:24:57,600 --> 03:24:59,960 the input is after it sets it to seven 4746 03:24:59,960 --> 03:25:01,840 so now that we have that we can go back 4747 03:25:01,840 --> 03:25:05,399 to our code remove this come back to our 4748 03:25:05,399 --> 03:25:08,720 test rerun our test and we'll find that 4749 03:25:08,720 --> 03:25:10,720 our code is now safe and sound because 4750 03:25:10,720 --> 03:25:13,800 our invariants hold up now an important 4751 03:25:13,800 --> 03:25:15,720 aside on the term invariant founder uses 4752 03:25:15,720 --> 03:25:17,560 the term invari variant to describe this 4753 03:25:17,560 --> 03:25:19,560 stateful fuzzing stat less fuzzing is 4754 03:25:19,560 --> 03:25:21,560 when you give random data to an input to 4755 03:25:21,560 --> 03:25:22,800 a function to see if it breaks some 4756 03:25:22,800 --> 03:25:24,960 invariant State full fuzzing is when you 4757 03:25:24,960 --> 03:25:26,960 give random data and random function 4758 03:25:26,960 --> 03:25:28,640 calls to a system to see if it breaks 4759 03:25:28,640 --> 03:25:30,439 some invariant in Foundry fuzzing is 4760 03:25:30,439 --> 03:25:32,520 stateless fuzzing and invariance are 4761 03:25:32,520 --> 03:25:33,800 stateful fuzzing so when people are 4762 03:25:33,800 --> 03:25:35,359 talking about invariance in Foundry 4763 03:25:35,359 --> 03:25:36,760 they're usually talking about stateful 4764 03:25:36,760 --> 03:25:39,000 fuzzing if they talk about fuzzing in 4765 03:25:39,000 --> 03:25:40,520 Foundry they're talking about State less 4766 03:25:40,520 --> 03:25:41,720 fuzzing even though they're both 4767 03:25:41,720 --> 03:25:43,239 technically fuzzing there's an issue on 4768 03:25:43,239 --> 03:25:44,720 the repo to potentially change the name 4769 03:25:44,720 --> 03:25:46,319 but I digress so in a real smart 4770 03:25:46,319 --> 03:25:47,880 contract your invariant won't be that a 4771 03:25:47,880 --> 03:25:49,560 balloon shouldn't pop or some function 4772 03:25:49,560 --> 03:25:51,080 should always be zero it might be 4773 03:25:51,080 --> 03:25:53,479 something like new tokens minted is less 4774 03:25:53,479 --> 03:25:54,920 than the inflation rate there should 4775 03:25:54,920 --> 03:25:56,800 only be one winner in a random Lottery 4776 03:25:56,800 --> 03:25:58,199 someone shouldn't be able to take more 4777 03:25:58,199 --> 03:25:59,640 money out of the protocol than they' put 4778 03:25:59,640 --> 03:26:00,920 in and let me tell you what at this 4779 03:26:00,920 --> 03:26:02,920 point congratulations you've learn the 4780 03:26:02,920 --> 03:26:04,920 basics of fuzzing this is something that 4781 03:26:04,920 --> 03:26:06,439 even some of the top protocols in this 4782 03:26:06,439 --> 03:26:07,960 space don't use and this is something 4783 03:26:07,960 --> 03:26:09,640 that we in cphon use to find High 4784 03:26:09,640 --> 03:26:11,399 severity vulnerabilities in smart 4785 03:26:11,399 --> 03:26:14,120 contracts hey I'm Alex ran co-founder at 4786 03:26:14,120 --> 03:26:16,399 cyphon we use invariant tests during our 4787 03:26:16,399 --> 03:26:19,239 audits to identify vulnerabilities that 4788 03:26:19,239 --> 03:26:22,399 are often difficult to catch purely with 4789 03:26:22,399 --> 03:26:24,439 manual reviews that's not to say they're 4790 03:26:24,439 --> 03:26:26,720 a silver bullet they are in no way a 4791 03:26:26,720 --> 03:26:29,640 replacement for experts manual review 4792 03:26:29,640 --> 03:26:32,520 but they certainly can Aid in the audit 4793 03:26:32,520 --> 03:26:35,359 process this needs to be the new floor 4794 03:26:35,359 --> 03:26:37,160 for security in web 3 if you're working 4795 03:26:37,160 --> 03:26:38,880 with a protocol that isn't doing 4796 03:26:38,880 --> 03:26:41,080 stateful fuzzing or invariant or fuzz 4797 03:26:41,080 --> 03:26:43,680 test red flag get them to use it make a 4798 03:26:43,680 --> 03:26:46,479 PR number one understand what the and 4799 03:26:46,479 --> 03:26:49,640 variants are number two write functions 4800 03:26:49,640 --> 03:26:52,760 that can execute them do not go to audit 4801 03:26:52,760 --> 03:26:54,840 without these don't let your Auditors 4802 03:26:54,840 --> 03:26:57,040 let you get away with not having them so 4803 03:26:57,040 --> 03:26:58,080 this video was just to give you the 4804 03:26:58,080 --> 03:26:59,640 basics and if you want to learn the 4805 03:26:59,640 --> 03:27:01,080 advanced fuzzing strategies on how to 4806 03:27:01,080 --> 03:27:03,080 fuzz look Pro be sure to watch our next 4807 03:27:03,080 --> 03:27:04,720 video on the topic is that'll give you 4808 03:27:04,720 --> 03:27:06,399 the keys to write professional fuzz and 4809 03:27:06,399 --> 03:27:08,520 professional invariant tests come on 4810 03:27:08,520 --> 03:27:10,279 gang let's make web three better and 4811 03:27:10,279 --> 03:27:13,070 I'll see you next time 4812 03:27:13,070 --> 03:27:17,830 [Music] 4813 03:27:21,199 --> 03:27:23,080 all right so now we've learned a little 4814 03:27:23,080 --> 03:27:25,239 bit about invariant tests or fuzzing 4815 03:27:25,239 --> 03:27:27,920 tests and why they are so absolutely 4816 03:27:27,920 --> 03:27:30,279 crucial especially for a project like 4817 03:27:30,279 --> 03:27:31,760 this so we're going to write some 4818 03:27:31,760 --> 03:27:35,120 stateful fuzz tests or invariant tests 4819 03:27:35,120 --> 03:27:36,600 in foundaries so we can have some more 4820 03:27:36,600 --> 03:27:38,359 confidence that our code actually does 4821 03:27:38,359 --> 03:27:40,640 what we want it to do and the method 4822 03:27:40,640 --> 03:27:43,199 that we saw in that video that we just 4823 03:27:43,199 --> 03:27:45,600 watched is kind of the most basic 4824 03:27:45,600 --> 03:27:47,319 methodology out there and if we go to 4825 03:27:47,319 --> 03:27:49,600 The Foundry docs we can go on the left 4826 03:27:49,600 --> 03:27:52,359 side go all the way down to fuzz testing 4827 03:27:52,359 --> 03:27:54,720 or excuse me go all the way down to this 4828 03:27:54,720 --> 03:27:56,439 invariant testing which invariant 4829 03:27:56,439 --> 03:27:57,920 testing like I said is stateful fuzz 4830 03:27:57,920 --> 03:27:59,960 testing and we can read more about some 4831 03:27:59,960 --> 03:28:01,840 of the more advanced ways to do these 4832 03:28:01,840 --> 03:28:04,359 fuzz tests or these invariant tests 4833 03:28:04,359 --> 03:28:05,479 we're still going to do the target 4834 03:28:05,479 --> 03:28:08,120 contracts but what we saw in the video 4835 03:28:08,120 --> 03:28:10,840 was a type of open testing right where 4836 03:28:10,840 --> 03:28:13,080 we just have function in variant a and 4837 03:28:13,080 --> 03:28:15,720 then the assert and what this does is it 4838 03:28:15,720 --> 03:28:17,680 just calls calls all the functions on 4839 03:28:17,680 --> 03:28:19,479 this contract to try to break that 4840 03:28:19,479 --> 03:28:21,840 invariant now this is good this is great 4841 03:28:21,840 --> 03:28:25,479 for an initial run of the code however 4842 03:28:25,479 --> 03:28:27,600 we want to do for more advanced systems 4843 03:28:27,600 --> 03:28:30,279 like ours Handler based testing 4844 03:28:30,279 --> 03:28:31,800 sufficiently complex Protocols are going 4845 03:28:31,800 --> 03:28:34,239 to have so many different random 4846 03:28:34,239 --> 03:28:37,000 intricacies that we want to narrow down 4847 03:28:37,000 --> 03:28:38,960 the random call so that we can have a 4848 03:28:38,960 --> 03:28:40,760 higher likelihood of getting and 4849 03:28:40,760 --> 03:28:42,479 catching actual errors so we're going to 4850 03:28:42,479 --> 03:28:45,120 do this Handler based type of testing in 4851 03:28:45,120 --> 03:28:47,000 this example here in this open testing 4852 03:28:47,000 --> 03:28:48,479 it just calls any of the functions in 4853 03:28:48,479 --> 03:28:51,000 the contract in any order and Handler B 4854 03:28:51,000 --> 03:28:52,680 based testing if we scroll down we can 4855 03:28:52,680 --> 03:28:54,920 kind of see this example here where we 4856 03:28:54,920 --> 03:28:56,800 create a contract called Handler where 4857 03:28:56,800 --> 03:29:00,239 we only call functions in specific ways 4858 03:29:00,239 --> 03:29:02,840 for example when depositing tokens we 4859 03:29:02,840 --> 03:29:04,720 need to make sure an approve happens 4860 03:29:04,720 --> 03:29:06,720 beforehand if you just call deposit 4861 03:29:06,720 --> 03:29:08,239 without approving that token that's kind 4862 03:29:08,239 --> 03:29:10,720 of a wasted fuzz run and if we only have 4863 03:29:10,720 --> 03:29:12,600 200 fuzz runs and we're wasting them on 4864 03:29:12,600 --> 03:29:15,279 failed fuzz runs well we're the chance 4865 03:29:15,279 --> 03:29:17,279 of us actually finding a bug becomes 4866 03:29:17,279 --> 03:29:21,000 smaller so if you think of the open fuzz 4867 03:29:21,000 --> 03:29:23,120 testing like this where you have Foundry 4868 03:29:23,120 --> 03:29:24,520 you call a whole bunch of functions on 4869 03:29:24,520 --> 03:29:27,680 the protocol with the asserts Handler is 4870 03:29:27,680 --> 03:29:30,239 going to call functions in specific ways 4871 03:29:30,239 --> 03:29:32,359 to the functions so that we have a 4872 03:29:32,359 --> 03:29:34,479 higher likelihood of calling functions 4873 03:29:34,479 --> 03:29:36,760 in orders that we want so we're going to 4874 03:29:36,760 --> 03:29:38,439 learn about this Handler based 4875 03:29:38,439 --> 03:29:40,880 methodology and we're going to build an 4876 03:29:40,880 --> 03:29:43,479 incredibly verbose Buzz testing or 4877 03:29:43,479 --> 03:29:46,640 invariant testing setup now 4878 03:29:46,640 --> 03:29:49,000 in our founder. toal to work with these 4879 03:29:49,000 --> 03:29:50,840 fuzz or these invariant tests we can do 4880 03:29:50,840 --> 03:29:53,560 this in variant section here we can say 4881 03:29:53,560 --> 03:29:56,120 the number of runs we'll say is 128 we 4882 03:29:56,120 --> 03:29:58,680 can also say the depth which is the 4883 03:29:58,680 --> 03:30:01,199 number of calls in a single run which we 4884 03:30:01,199 --> 03:30:03,439 might do 128 and then one of the most 4885 03:30:03,439 --> 03:30:04,760 important keywords you're going to run 4886 03:30:04,760 --> 03:30:08,439 into is this fail on revert so let's 4887 03:30:08,439 --> 03:30:10,160 talk about this fail on revert keyword 4888 03:30:10,160 --> 03:30:12,479 and setting so to create some invariant 4889 03:30:12,479 --> 03:30:15,239 tests let's create a new folder or Buzz 4890 03:30:15,239 --> 03:30:17,160 test called or invariant or whatever you 4891 03:30:17,160 --> 03:30:18,920 want to call it and in here we're going 4892 03:30:18,920 --> 03:30:21,160 to need to actually create two different 4893 03:30:21,160 --> 03:30:24,439 files we're going to do invariance 4894 03:30:24,439 --> 03:30:30,359 invariance test. T.O and then handler. 4895 03:30:30,359 --> 03:30:32,960 T.O this invariance file is going to 4896 03:30:32,960 --> 03:30:36,640 have have our invariance AKA our 4897 03:30:36,640 --> 03:30:38,160 properties of the system that should 4898 03:30:38,160 --> 03:30:40,000 always hold right that we just learned 4899 03:30:40,000 --> 03:30:42,800 for that video and this Handler this 4900 03:30:42,800 --> 03:30:44,479 Handler is going to narrow down the way 4901 03:30:44,479 --> 03:30:47,160 that we call functions this way we don't 4902 03:30:47,160 --> 03:30:50,000 waste runs like I was saying again if we 4903 03:30:50,000 --> 03:30:51,760 call deposit collateral in our stable 4904 03:30:51,760 --> 03:30:54,199 coin without approving that stable coin 4905 03:30:54,199 --> 03:30:56,399 that's kind of a wasted run and we don't 4906 03:30:56,399 --> 03:30:59,000 want to waste runs so this Handler is 4907 03:30:59,000 --> 03:31:00,800 going to set our code up set our 4908 03:31:00,800 --> 03:31:03,160 contracts up so that we don't waste 4909 03:31:03,160 --> 03:31:05,120 these runs and we're going to come back 4910 03:31:05,120 --> 03:31:06,840 to this fail and revert in a second for 4911 03:31:06,840 --> 03:31:09,199 now let's actually set this to false and 4912 03:31:09,199 --> 03:31:11,600 we'll set it to True soon but so the 4913 03:31:11,600 --> 03:31:13,319 first thing we always want to do when 4914 03:31:13,319 --> 03:31:14,880 writing in variant test when working 4915 03:31:14,880 --> 03:31:18,120 with this just want to ask the question 4916 03:31:18,120 --> 03:31:21,960 what are our invariant what are the 4917 03:31:21,960 --> 03:31:23,920 properties of the system that should 4918 03:31:23,920 --> 03:31:26,680 always hold well we can think of some 4919 03:31:26,680 --> 03:31:31,600 right well one the total supply of 4920 03:31:31,600 --> 03:31:36,359 DSC should be less than the total value 4921 03:31:36,359 --> 03:31:40,160 of Catal follow the word wrap which the 4922 03:31:40,160 --> 03:31:42,160 total supply of DC which is essentially 4923 03:31:42,160 --> 03:31:43,800 the debt should always be less than the 4924 03:31:43,800 --> 03:31:45,920 total value of collateral great we have 4925 03:31:45,920 --> 03:31:47,840 an invariant that we can test and we 4926 03:31:47,840 --> 03:31:50,359 should throw a ton of random function 4927 03:31:50,359 --> 03:31:52,640 calls to try to break this one okay what 4928 03:31:52,640 --> 03:31:54,239 else what other invariants should we 4929 03:31:54,239 --> 03:31:57,040 have maybe our getter functions our 4930 03:31:57,040 --> 03:31:58,120 getter view 4931 03:31:58,120 --> 03:32:02,199 functions should never revert and this 4932 03:32:02,199 --> 03:32:03,880 is actually sort of an 4933 03:32:03,880 --> 03:32:07,760 evergreen invariant most protocols can 4934 03:32:07,760 --> 03:32:09,120 and should probably just have an 4935 03:32:09,120 --> 03:32:10,600 invariant that looks like this getter 4936 03:32:10,600 --> 03:32:13,640 view function should never revert now we 4937 03:32:13,640 --> 03:32:15,960 can probably think of more but because 4938 03:32:15,960 --> 03:32:17,640 because doing these invariant tests can 4939 03:32:17,640 --> 03:32:19,279 be a little bit time intensive we're 4940 03:32:19,279 --> 03:32:20,800 just going to focus on these two for now 4941 03:32:20,800 --> 03:32:22,160 these are going to be the two invariants 4942 03:32:22,160 --> 03:32:25,840 that we focus on we try to work with so 4943 03:32:25,840 --> 03:32:28,640 let's begin working and writing our 4944 03:32:28,640 --> 03:32:30,160 invariant tests and then we're also 4945 03:32:30,160 --> 03:32:32,640 going to write our Handler to help make 4946 03:32:32,640 --> 03:32:33,960 sure all the function calls that we're 4947 03:32:33,960 --> 03:32:35,760 working with actually do what we want 4948 03:32:35,760 --> 03:32:39,319 them to do okay let's do it so this is 4949 03:32:39,319 --> 03:32:43,239 going to be another test file so spdx 4950 03:32:43,239 --> 03:32:48,640 license identifier MIT pragma so oh oh 4951 03:32:48,640 --> 03:32:49,720 that's nice that kind of just 4952 03:32:49,720 --> 03:32:51,880 automatically added it for me let's put 4953 03:32:51,880 --> 03:32:54,239 this to the top 4954 03:32:54,239 --> 03:32:55,840 0. 4955 03:32:55,840 --> 03:32:59,880 8.18 a little carrot here contract 4956 03:32:59,880 --> 03:33:04,120 invariance test like this and to do this 4957 03:33:04,120 --> 03:33:05,880 we're going to say we're going have to 4958 03:33:05,880 --> 03:33:07,399 import some stuff we're going to import 4959 03:33:07,399 --> 03:33:14,080 test from Forge TD test. soul and import 4960 03:33:14,080 --> 03:33:22,399 STD in variant from Forge STD STD 4961 03:33:22,399 --> 03:33:25,640 invariant Soul contract invariant test 4962 03:33:25,640 --> 03:33:28,520 and then we're going to say this is STD 4963 03:33:28,520 --> 03:33:31,920 invariant and it's test so this STD 4964 03:33:31,920 --> 03:33:34,520 invariant contract if we click into it 4965 03:33:34,520 --> 03:33:35,960 it has all this stuff that we're going 4966 03:33:35,960 --> 03:33:38,279 to need to work with the invariant one 4967 03:33:38,279 --> 03:33:39,880 of the most important functions that it 4968 03:33:39,880 --> 03:33:42,760 gives us is this Target contract where 4969 03:33:42,760 --> 03:33:44,920 it says hey this is the contract I want 4970 03:33:44,920 --> 03:33:47,160 you to call all these random functions 4971 03:33:47,160 --> 03:33:50,800 on okay great now just like our normal 4972 03:33:50,800 --> 03:33:52,600 tests we're going to have a function 4973 03:33:52,600 --> 03:33:55,920 setup external like this and we're going 4974 03:33:55,920 --> 03:33:58,560 to set up some stuff right a lot of this 4975 03:33:58,560 --> 03:33:59,800 is going to look similar to our unit 4976 03:33:59,800 --> 03:34:02,760 test so we're going to have to import 4977 03:34:02,760 --> 03:34:03,880 Ploy 4978 03:34:03,880 --> 03:34:05,439 DSC 4979 03:34:05,439 --> 03:34:12,840 from do do do do script deoy DSC 4980 03:34:12,840 --> 03:34:20,720 dos. import DS C engine from do 4981 03:34:20,720 --> 03:34:23,040 slsrc SL 4982 03:34:23,040 --> 03:34:29,560 DC engine. Soul like this and we'll say 4983 03:34:29,560 --> 03:34:33,359 deoy DSC deployer 4984 03:34:33,359 --> 03:34:37,880 deployer deployer equals new deoy DSC 4985 03:34:37,880 --> 03:34:43,319 and then of course DC engine dsce dce 4986 03:34:43,319 --> 03:34:47,399 equals deployer do run and this actually 4987 03:34:47,399 --> 03:34:50,120 returns a whole bunch of stuff Central 4988 03:34:50,120 --> 03:34:52,920 stable coin dce and helper config so 4989 03:34:52,920 --> 03:34:55,960 this is going to be we also need a DSC 4990 03:34:55,960 --> 03:34:58,880 so we're going to import that import the 4991 03:34:58,880 --> 03:35:01,600 centralized stable coin from dot do do 4992 03:35:01,600 --> 03:35:05,399 do SRC decentralized stable coin. Soul 4993 03:35:05,399 --> 03:35:06,680 so we're going to do decentralized 4994 03:35:06,680 --> 03:35:07,760 stablecoin 4995 03:35:07,760 --> 03:35:10,399 DSC and let's get the helper config as 4996 03:35:10,399 --> 03:35:17,199 well import helper config config from 4997 03:35:17,199 --> 03:35:23,359 do do script slh helper config dos. Soul 4998 03:35:23,359 --> 03:35:26,520 helper config config right and so this 4999 03:35:26,520 --> 03:35:31,120 turns the DSC dsce and config equals 5000 03:35:31,120 --> 03:35:34,600 deployer run great and now if we were 5001 03:35:34,600 --> 03:35:36,680 doing this open testing methodology 5002 03:35:36,680 --> 03:35:38,720 right if we go back to the docs here 5003 03:35:38,720 --> 03:35:40,120 we're doing this open testing 5004 03:35:40,120 --> 03:35:43,199 methodology we could kind of finish this 5005 03:35:43,199 --> 03:35:46,160 right now what we would do we we would 5006 03:35:46,160 --> 03:35:51,800 say Target contract address dce like 5007 03:35:51,800 --> 03:35:55,520 this paste this in here and then just by 5008 03:35:55,520 --> 03:35:57,199 adding this we're telling Foundry hey go 5009 03:35:57,199 --> 03:35:59,880 ahead go wild on this right go 5010 03:35:59,880 --> 03:36:01,399 absolutely Wild on this and actually 5011 03:36:01,399 --> 03:36:05,040 let's even rename this call this open 5012 03:36:05,040 --> 03:36:09,040 invariant test. T.O open invariant test. 5013 03:36:09,040 --> 03:36:11,960 T.O we'll say absolutely Go Buck Wild on 5014 03:36:11,960 --> 03:36:14,160 this and now we can add our invariant 5015 03:36:14,160 --> 03:36:17,239 right we'll say function invariant 5016 03:36:17,239 --> 03:36:22,319 variant underscore protocol must must 5017 03:36:22,319 --> 03:36:27,319 have more value than total Supply and 5018 03:36:27,319 --> 03:36:29,560 this will be a view function and what we 5019 03:36:29,560 --> 03:36:33,479 can just say is we want to get the value 5020 03:36:33,479 --> 03:36:38,319 of the of all the collateral in the 5021 03:36:38,319 --> 03:36:42,560 protocol compare it to all the debt or 5022 03:36:42,560 --> 03:36:45,640 the DSC so we can do that pretty easily 5023 03:36:45,640 --> 03:36:47,960 by using the collaterals itself right 5024 03:36:47,960 --> 03:36:50,920 using our helper config we'll say helper 5025 03:36:50,920 --> 03:36:55,560 config do activ Network config we can 5026 03:36:55,560 --> 03:36:58,560 get the what does this one do again 5027 03:36:58,560 --> 03:37:00,680 let's open up the helber config we can 5028 03:37:00,680 --> 03:37:02,520 get these two tokens and just say okay 5029 03:37:02,520 --> 03:37:04,120 well what's the balance of these two 5030 03:37:04,120 --> 03:37:06,760 tokens in our DSC engine and then what's 5031 03:37:06,760 --> 03:37:08,880 their value right so we'll just get 5032 03:37:08,880 --> 03:37:10,080 those two tokens we're going to do 5033 03:37:10,080 --> 03:37:12,439 nothing nothing we Bitcoin nothing so 5034 03:37:12,439 --> 03:37:17,520 we'll do blank blank we BTC nothing and 5035 03:37:17,520 --> 03:37:21,960 then those are ERC 20s so we'll import 5036 03:37:21,960 --> 03:37:23,960 import I 5037 03:37:23,960 --> 03:37:26,000 erc20 5038 03:37:26,000 --> 03:37:28,760 from all right cool that looks 5039 03:37:28,760 --> 03:37:34,160 good so we'll say erc20 we ic20 BTC this 5040 03:37:34,160 --> 03:37:38,000 is wrapped BTC wrapped BTC oops sorry 5041 03:37:38,000 --> 03:37:41,720 this is config Active network config we 5042 03:37:41,720 --> 03:37:45,720 need to wrap this erc20 5043 03:37:45,720 --> 03:37:49,840 20 like this what am I messing up oh 5044 03:37:49,840 --> 03:37:53,000 let's do do address we then address we 5045 03:37:53,000 --> 03:37:56,640 address BTC like this all right cool so 5046 03:37:56,640 --> 03:37:59,000 we're getting we and Bitcoin EXC me 5047 03:37:59,000 --> 03:38:01,080 wrapped eth and wrapped Bitcoin and now 5048 03:38:01,080 --> 03:38:03,960 what we can do is first we'll say unit 5049 03:38:03,960 --> 03:38:09,800 256 total Supply equals DSC do total 5050 03:38:09,800 --> 03:38:11,960 Supply so this is the total supply of 5051 03:38:11,960 --> 03:38:14,520 all DSC in the entire world right and we 5052 03:38:14,520 --> 03:38:16,600 know the only way to mint DSC is through 5053 03:38:16,600 --> 03:38:18,840 the DSC engine so through people 5054 03:38:18,840 --> 03:38:20,920 depositing and withdrawing collateral 5055 03:38:20,920 --> 03:38:22,720 now what we can do is we can say okay 5056 03:38:22,720 --> 03:38:24,120 let's get the uint 5057 03:38:24,120 --> 03:38:27,720 256 total total we 5058 03:38:27,720 --> 03:38:30,680 deposited equals and this is where we 5059 03:38:30,680 --> 03:38:31,920 can do 5060 03:38:31,920 --> 03:38:37,800 ic20 with balance of address DSC so this 5061 03:38:37,800 --> 03:38:40,439 is going to be the total amount of we 5062 03:38:40,439 --> 03:38:43,640 deposited into that contract or just the 5063 03:38:43,640 --> 03:38:46,680 total amount of we sent to that contract 5064 03:38:46,680 --> 03:38:50,720 then we're going to say un 256 total BTC 5065 03:38:50,720 --> 03:38:56,960 deposited equals ic20 wrapped BTC dot 5066 03:38:56,960 --> 03:39:00,399 balance of address DSC so we have the 5067 03:39:00,399 --> 03:39:03,399 total we total Bitcoin now we can get 5068 03:39:03,399 --> 03:39:08,080 those values we can say un 256 we value 5069 03:39:08,080 --> 03:39:10,199 equals we have a function here called 5070 03:39:10,199 --> 03:39:12,560 get USD value where we can get the USD 5071 03:39:12,560 --> 03:39:14,479 value of any token of any amount so 5072 03:39:14,479 --> 03:39:16,399 we're just going to use that 5073 03:39:16,399 --> 03:39:19,600 we value is going to be dsce doget USD 5074 03:39:19,600 --> 03:39:24,199 value we and the total we deposited un 5075 03:39:24,199 --> 03:39:29,080 256 wrapped BTC value same thing get USD 5076 03:39:29,080 --> 03:39:32,560 value WRA BTC total BTC deposited and 5077 03:39:32,560 --> 03:39:35,760 now what we can do is we can 5078 03:39:35,760 --> 03:39:41,479 do assert the we Value Plus the wrapped 5079 03:39:41,479 --> 03:39:43,680 Bitcoin value is greater than the total 5080 03:39:43,680 --> 03:39:45,880 Supply and this 5081 03:39:45,880 --> 03:39:49,479 is all we would need to do for this open 5082 03:39:49,479 --> 03:39:52,640 testing this open invariance that's it 5083 03:39:52,640 --> 03:39:54,840 we're done boom and this is why this is 5084 03:39:54,840 --> 03:39:56,800 the easiest type of invariant test but 5085 03:39:56,800 --> 03:39:59,840 you'll see running this we won't get 5086 03:39:59,840 --> 03:40:02,840 great results so let's do Forge testm 5087 03:40:02,840 --> 03:40:04,520 and now what this is going to do is it's 5088 03:40:04,520 --> 03:40:07,319 going to call all types of functions on 5089 03:40:07,319 --> 03:40:12,399 our dsce and try to break this invariant 5090 03:40:12,399 --> 03:40:14,720 so it looks like it was able to break 5091 03:40:14,720 --> 03:40:17,720 this really easily so let's clear this 5092 03:40:17,720 --> 03:40:20,720 and we'll add our Dash BV 5093 03:40:20,720 --> 03:40:25,399 BV we'll see what's 5094 03:40:27,120 --> 03:40:30,520 up yep yep assertion violated we're 5095 03:40:30,520 --> 03:40:32,760 having a hard time actually seeing the 5096 03:40:32,760 --> 03:40:34,720 numbers so we're going to import console 5097 03:40:34,720 --> 03:40:37,560 in here test 5098 03:40:37,560 --> 03:40:41,040 console and we're just going to say 5099 03:40:41,040 --> 03:40:46,479 console. log we value so me call 5100 03:40:46,479 --> 03:40:52,399 W value or that's w w BTC we we W BTC 5101 03:40:52,399 --> 03:40:54,120 value 5102 03:40:54,120 --> 03:40:57,040 console.log total Supply total Supply 5103 03:40:57,040 --> 03:41:00,600 let's run this again now what do we 5104 03:41:00,600 --> 03:41:03,800 get well we got our first issue is that 5105 03:41:03,800 --> 03:41:06,439 these are all zeros right and if they're 5106 03:41:06,439 --> 03:41:09,880 all zeros then this doesn't hold so we 5107 03:41:09,880 --> 03:41:12,279 can do greater than or equal to this is 5108 03:41:12,279 --> 03:41:14,520 kind of a bit of a copout right because 5109 03:41:14,520 --> 03:41:16,920 if they're equal that makes us nervous 5110 03:41:16,920 --> 03:41:18,760 but it's fine right we should always at 5111 03:41:18,760 --> 03:41:20,600 least have more collateral in the system 5112 03:41:20,600 --> 03:41:22,920 than total Supply so now let's try again 5113 03:41:22,920 --> 03:41:26,479 run this test again run successful and 5114 03:41:26,479 --> 03:41:29,720 we get hey you pass there's no way for 5115 03:41:29,720 --> 03:41:32,120 us to make it such that the total Supply 5116 03:41:32,120 --> 03:41:36,840 is lower so this is awesome right well 5117 03:41:36,840 --> 03:41:38,960 it's not that awesome we didn't find any 5118 03:41:38,960 --> 03:41:41,600 issues we're looking to find issues so 5119 03:41:41,600 --> 03:41:43,680 maybe we need to bump up the number of 5120 03:41:43,680 --> 03:41:46,040 runs may we need to bump it up to 5121 03:41:46,040 --> 03:41:49,239 2000 and let's run this again now you're 5122 03:41:49,239 --> 03:41:51,199 going to see this is going to take a lot 5123 03:41:51,199 --> 03:41:54,279 longer CU before I was doing 100 runs at 5124 03:41:54,279 --> 03:41:56,840 128 depth now it's was doing a th000 5125 03:41:56,840 --> 03:42:00,199 runs and each one of these runs has 128 5126 03:42:00,199 --> 03:42:02,920 so it took a lot longer and you can see 5127 03:42:02,920 --> 03:42:06,159 that it took 14 seconds as opposed to 5128 03:42:06,159 --> 03:42:10,760 what it do before with 128 we run 5129 03:42:11,319 --> 03:42:14,800 that it did it in 1 and half seconds but 5130 03:42:14,800 --> 03:42:16,720 if we look up here on this line this 5131 03:42:16,720 --> 03:42:18,560 line is incredibly important we have 5132 03:42:18,560 --> 03:42:22,439 calls and we have reverts so this made 5133 03:42:22,439 --> 03:42:25,840 16,384 calls and reverted 5134 03:42:25,840 --> 03:42:29,279 16,384 times so basically it wasn't even 5135 03:42:29,279 --> 03:42:32,479 able to do anything right so what if we 5136 03:42:32,479 --> 03:42:36,159 bump this up to a th000 clear run this 5137 03:42:36,159 --> 03:42:37,600 we're going to have to wait a few more 5138 03:42:37,600 --> 03:42:40,000 seconds 5139 03:42:41,880 --> 03:42:43,439 again 5140 03:42:43,439 --> 03:42:46,520 oh so it did this many calls and it also 5141 03:42:46,520 --> 03:42:50,040 reverted this many times and the reason 5142 03:42:50,040 --> 03:42:52,120 that this is still saying pass though is 5143 03:42:52,120 --> 03:42:54,000 because we have this fail on revert 5144 03:42:54,000 --> 03:42:56,760 equals false and this fail on revert 5145 03:42:56,760 --> 03:42:59,080 equals false has some pros and it has 5146 03:42:59,080 --> 03:43:02,319 some cons the pro a fail on reverse 5147 03:43:02,319 --> 03:43:06,040 equals false is that we can very quickly 5148 03:43:06,040 --> 03:43:08,640 write open testing functions like this 5149 03:43:08,640 --> 03:43:11,560 and we can very quickly write minimal 5150 03:43:11,560 --> 03:43:13,760 Handler functions that aren't perfect 5151 03:43:13,760 --> 03:43:15,399 but the downside is it's hard for for us 5152 03:43:15,399 --> 03:43:17,439 to make sure that all the calls we're 5153 03:43:17,439 --> 03:43:19,800 making actually make sense right because 5154 03:43:19,800 --> 03:43:22,720 this could be calling on our on our 5155 03:43:22,720 --> 03:43:24,880 engine maybe it's just trying to to 5156 03:43:24,880 --> 03:43:27,040 deposit collateral but it keeps using 5157 03:43:27,040 --> 03:43:28,680 random collateral out addresses that 5158 03:43:28,680 --> 03:43:30,080 don't make any sense so maybe it's 5159 03:43:30,080 --> 03:43:32,080 calling this 5160 03:43:32,080 --> 03:43:34,840 128,000 times with 128 different 5161 03:43:34,840 --> 03:43:37,560 collateral addresses but only two work 5162 03:43:37,560 --> 03:43:41,279 right so doing like this is cool for 5163 03:43:41,279 --> 03:43:42,640 kind of some sanity check and maybe 5164 03:43:42,640 --> 03:43:44,399 it'll catch something but seems like 5165 03:43:44,399 --> 03:43:46,560 it's not actually catch anything right 5166 03:43:46,560 --> 03:43:48,920 and that's not a very good use of this 5167 03:43:48,920 --> 03:43:51,720 so revert on false is fantastic for 5168 03:43:51,720 --> 03:43:54,439 quick tests and often if I'm doing a 5169 03:43:54,439 --> 03:43:56,680 competitive audit which you can learn 5170 03:43:56,680 --> 03:43:59,319 more about in the security course coming 5171 03:43:59,319 --> 03:44:01,840 out soon hopefully if I'm doing 5172 03:44:01,840 --> 03:44:03,600 competitive audit a lot of times I will 5173 03:44:03,600 --> 03:44:06,520 have revert on false be false just so I 5174 03:44:06,520 --> 03:44:09,199 can write up inv variant test quickly I 5175 03:44:09,199 --> 03:44:11,760 will also write handlers as well like 5176 03:44:11,760 --> 03:44:13,760 mini handlers just so I don't have to 5177 03:44:13,760 --> 03:44:15,279 get every nook and cranny but I still 5178 03:44:15,279 --> 03:44:17,840 will write a Handler so I can narrow 5179 03:44:17,840 --> 03:44:21,359 down some of the functions but this open 5180 03:44:21,359 --> 03:44:22,840 inv variance seems to have this major 5181 03:44:22,840 --> 03:44:24,840 flaw where it's probably making a bunch 5182 03:44:24,840 --> 03:44:28,960 of silly calls so this is great for very 5183 03:44:28,960 --> 03:44:31,080 small contracts but the more complex you 5184 03:44:31,080 --> 03:44:33,560 get like our system here this open inv 5185 03:44:33,560 --> 03:44:35,800 variant system it probably doesn't make 5186 03:44:35,800 --> 03:44:36,920 sense for us to do because it's not 5187 03:44:36,920 --> 03:44:38,040 going to catch anything it's just going 5188 03:44:38,040 --> 03:44:40,239 to keep breaking now if we set this to 5189 03:44:40,239 --> 03:44:43,080 true we open this back up we run this 5190 03:44:43,080 --> 03:44:45,800 again you'll see 5191 03:44:45,800 --> 03:44:47,880 exactly one of the calls it makes that 5192 03:44:47,880 --> 03:44:50,520 breaks so it looks like it called redeem 5193 03:44:50,520 --> 03:44:53,239 collateral for you for DSC this is the 5194 03:44:53,239 --> 03:44:54,840 first function call it made which 5195 03:44:54,840 --> 03:44:56,159 obviously doesn't make any sense because 5196 03:44:56,159 --> 03:44:57,600 you can't redeem any collateral unless 5197 03:44:57,600 --> 03:44:59,960 you have deposited collateral and and 5198 03:44:59,960 --> 03:45:01,920 you can see the args it put in it put in 5199 03:45:01,920 --> 03:45:05,199 some random address some random amount a 5200 03:45:05,199 --> 03:45:08,640 random amount and then another random 5201 03:45:08,640 --> 03:45:11,880 number right and you'll see we can call 5202 03:45:11,880 --> 03:45:14,439 this many times and it'll keep giving us 5203 03:45:14,439 --> 03:45:16,920 different that it ran into issues when 5204 03:45:16,920 --> 03:45:19,520 we say revert on false is true this can 5205 03:45:19,520 --> 03:45:22,720 give us some peace of mind knowing that 5206 03:45:22,720 --> 03:45:25,080 if this test passes that means all of 5207 03:45:25,080 --> 03:45:26,680 the transactions that went through 5208 03:45:26,680 --> 03:45:28,520 actually went through and it didn't make 5209 03:45:28,520 --> 03:45:30,159 a bunch of really dumb calls here right 5210 03:45:30,159 --> 03:45:32,880 so here fail on revert was false we 5211 03:45:32,880 --> 03:45:35,399 called liquidate first which obviously 5212 03:45:35,399 --> 03:45:38,120 doesn't make any sense with this with 5213 03:45:38,120 --> 03:45:40,120 some horrible random address some 5214 03:45:40,120 --> 03:45:41,800 horrible random address some random 5215 03:45:41,800 --> 03:45:43,279 amount right none of these make any 5216 03:45:43,279 --> 03:45:46,439 sense we call it again we failed again 5217 03:45:46,439 --> 03:45:48,479 it called deposit collateral mint DSC 5218 03:45:48,479 --> 03:45:49,760 which is good all right so we're trying 5219 03:45:49,760 --> 03:45:51,520 to deposit collateral but it used some 5220 03:45:51,520 --> 03:45:54,159 random address that isn't approved some 5221 03:45:54,159 --> 03:45:56,640 horrible amounts here and it just keeps 5222 03:45:56,640 --> 03:45:59,199 failing right so we want to try to 5223 03:45:59,199 --> 03:46:00,720 prevent and you can see here in the call 5224 03:46:00,720 --> 03:46:03,640 summary it made one run one call and 5225 03:46:03,640 --> 03:46:06,840 that call reverted right so we want to 5226 03:46:06,840 --> 03:46:09,520 narrow this down to say to try to point 5227 03:46:09,520 --> 03:46:12,359 our fuzz our random runs in a direction 5228 03:46:12,359 --> 03:46:15,399 that makes a lot more sense right so 5229 03:46:15,399 --> 03:46:18,680 this is cool not great opening variance 5230 03:46:18,680 --> 03:46:20,359 I'm just now going to comment out this 5231 03:46:20,359 --> 03:46:22,520 whole page because we're not going to 5232 03:46:22,520 --> 03:46:24,479 use this anymore okay we're I'm going to 5233 03:46:24,479 --> 03:46:25,880 leave the file in here though we're 5234 03:46:25,880 --> 03:46:27,120 going to create a new file though we're 5235 03:46:27,120 --> 03:46:28,640 going to call this 5236 03:46:28,640 --> 03:46:31,760 invariance t. soul I am going to copy 5237 03:46:31,760 --> 03:46:33,920 this whole invariance thing paste it in 5238 03:46:33,920 --> 03:46:35,359 here and uncommon it and we're going to 5239 03:46:35,359 --> 03:46:37,680 level this up so that this invariance 5240 03:46:37,680 --> 03:46:40,199 file actually let's just call this 5241 03:46:40,199 --> 03:46:42,880 invariance so that this new one is using 5242 03:46:42,880 --> 03:46:45,120 this Handler method it's using this 5243 03:46:45,120 --> 03:46:46,399 Handler method to narrow down the 5244 03:46:46,399 --> 03:46:47,600 function 5245 03:46:47,600 --> 03:46:50,319 calls and we'll do a mix of setting this 5246 03:46:50,319 --> 03:46:52,279 to true and false and you'll see where 5247 03:46:52,279 --> 03:46:56,080 some of the advantages and disadvantages 5248 03:46:57,600 --> 03:47:00,359 are so we have our invariance file we 5249 03:47:00,359 --> 03:47:02,479 have our Target contract here but we 5250 03:47:02,479 --> 03:47:05,239 want to make sure we call this in a 5251 03:47:05,239 --> 03:47:08,680 sensical order for example hey don't 5252 03:47:08,680 --> 03:47:13,319 call redeem collateral unless there is 5253 03:47:13,319 --> 03:47:15,600 collateral to redeem 5254 03:47:15,600 --> 03:47:17,720 right maybe we want to set this up so 5255 03:47:17,720 --> 03:47:20,040 we're going to create a Handler which is 5256 03:47:20,040 --> 03:47:22,159 going to handle the way we actually make 5257 03:47:22,159 --> 03:47:24,680 calls to the dce so it's basically going 5258 03:47:24,680 --> 03:47:26,319 to instead of us just randomly calling 5259 03:47:26,319 --> 03:47:28,080 redeem collateral we're only going to be 5260 03:47:28,080 --> 03:47:29,720 able to call redeem collateral if there 5261 03:47:29,720 --> 03:47:31,279 is collateral to redeem right because 5262 03:47:31,279 --> 03:47:32,439 otherwise the transaction is just going 5263 03:47:32,439 --> 03:47:34,439 to revert and that's a waste of function 5264 03:47:34,439 --> 03:47:36,199 call so now we're going to create this 5265 03:47:36,199 --> 03:47:39,239 Handler and instead of our Target 5266 03:47:39,239 --> 03:47:41,439 contract being the dsce our Target 5267 03:47:41,439 --> 03:47:43,600 contract is going to be this Handler 5268 03:47:43,600 --> 03:47:45,279 which handles the way we make those 5269 03:47:45,279 --> 03:47:49,000 calls okay so we're going to do spdx 5270 03:47:49,000 --> 03:47:51,040 license identifier MIT as you already 5271 03:47:51,040 --> 03:47:56,080 know fragma Z solidity 5272 03:47:56,080 --> 03:48:00,840 0.88 a little carrot contract Handler 5273 03:48:00,840 --> 03:48:03,319 like this and then we're going to say 5274 03:48:03,319 --> 03:48:04,920 this contract Handler is going to be 5275 03:48:04,920 --> 03:48:07,880 test as well and this is definitely some 5276 03:48:07,880 --> 03:48:10,479 Advanced code here so don't get too 5277 03:48:10,479 --> 03:48:12,159 discouraged if it doesn't make sense or 5278 03:48:12,159 --> 03:48:14,279 if it's hard the first time okay so 5279 03:48:14,279 --> 03:48:17,720 we're going to inut report test from 5280 03:48:17,720 --> 03:48:21,760 forg STD test. Soul remember to ask 5281 03:48:21,760 --> 03:48:24,960 questions and use the forms so what's 5282 03:48:24,960 --> 03:48:26,080 one of the first things you want to do 5283 03:48:26,080 --> 03:48:28,080 hey don't call redeem collateral unless 5284 03:48:28,080 --> 03:48:29,920 there is even collateral to redeem right 5285 03:48:29,920 --> 03:48:31,279 we want to make sure that that's this is 5286 03:48:31,279 --> 03:48:33,199 a valid run he only call redeem 5287 03:48:33,199 --> 03:48:35,359 collateral when there is collateral in 5288 03:48:35,359 --> 03:48:39,560 there so this function this contract is 5289 03:48:39,560 --> 03:48:41,920 going to do that for us we do need to 5290 03:48:41,920 --> 03:48:45,399 make a Constructor though so that this 5291 03:48:45,399 --> 03:48:48,760 Handler contract knows what the DSC 5292 03:48:48,760 --> 03:48:51,120 engine even is right because it's going 5293 03:48:51,120 --> 03:48:52,439 to be the one making the calls to it so 5294 03:48:52,439 --> 03:48:56,680 we do need to import the DSC engine from 5295 03:48:56,680 --> 03:48:58,319 do do 5296 03:48:58,319 --> 03:49:02,040 slsrc DSC engine. we also need to import 5297 03:49:02,040 --> 03:49:04,600 the decentralized stable coin from the 5298 03:49:04,600 --> 03:49:06,960 decentralized stable coin. Soul 5299 03:49:06,960 --> 03:49:08,600 Constructor and these are going to be 5300 03:49:08,600 --> 03:49:10,840 the main functions these are going to be 5301 03:49:10,840 --> 03:49:12,199 the main contracts that our Handler is 5302 03:49:12,199 --> 03:49:14,040 going to call so we're going to say DSN 5303 03:49:14,040 --> 03:49:15,359 and dce 5304 03:49:15,359 --> 03:49:16,120 and then we're going to say 5305 03:49:16,120 --> 03:49:19,000 decentralized stable coin DSC and in the 5306 03:49:19,000 --> 03:49:21,880 Constructor here we're going to say DSC 5307 03:49:21,880 --> 03:49:24,720 engine uncore DSC 5308 03:49:24,720 --> 03:49:29,439 engine decentralized stable coin _ DSC 5309 03:49:29,439 --> 03:49:31,680 and then we're just going to say oh cool 5310 03:49:31,680 --> 03:49:34,520 I already added it dsce DSC engine and 5311 03:49:34,520 --> 03:49:37,800 DSC isore DSC so because these are the 5312 03:49:37,800 --> 03:49:39,560 contracts that we want the Handler to 5313 03:49:39,560 --> 03:49:43,439 handle making the calls to Great makes 5314 03:49:43,439 --> 03:49:45,720 sense so let's talk about this redeemed 5315 03:49:45,720 --> 03:49:48,680 collateral right let's just focus on on 5316 03:49:48,680 --> 03:49:51,239 making this not revert so we're going to 5317 03:49:51,239 --> 03:49:53,120 say okay call this when you have 5318 03:49:53,120 --> 03:49:54,479 collateral so the first thing we 5319 03:49:54,479 --> 03:49:56,880 probably need to do is what probably 5320 03:49:56,880 --> 03:49:59,319 deposit collateral right so we'll create 5321 03:49:59,319 --> 03:50:01,439 a function called 5322 03:50:01,439 --> 03:50:03,199 deposit 5323 03:50:03,199 --> 03:50:05,080 collateral and this function is going to 5324 03:50:05,080 --> 03:50:08,319 look a little different than the deposit 5325 03:50:08,319 --> 03:50:10,040 collateral in the DSC engine right if 5326 03:50:10,040 --> 03:50:12,800 we're look in here this is what it does 5327 03:50:12,800 --> 03:50:14,279 we're going to set this deposit 5328 03:50:14,279 --> 03:50:16,640 collateral function up in our Handler so 5329 03:50:16,640 --> 03:50:18,560 that this transaction always goes 5330 03:50:18,560 --> 03:50:20,600 through right it doesn't revert but we 5331 03:50:20,600 --> 03:50:22,680 do want to keep the randomization right 5332 03:50:22,680 --> 03:50:25,319 we wanted to deposit random collaterals 5333 03:50:25,319 --> 03:50:27,600 that are valid collaterals so what we 5334 03:50:27,600 --> 03:50:30,080 can do is we can create a unit 256 5335 03:50:30,080 --> 03:50:34,560 collateral seed and a un 256 amount 5336 03:50:34,560 --> 03:50:37,479 collateral collateral and this is 5337 03:50:37,479 --> 03:50:38,840 actually really similar to the fuzz 5338 03:50:38,840 --> 03:50:42,199 tests so in your handlers whatever 5339 03:50:42,199 --> 03:50:43,880 parameters you have are going to be 5340 03:50:43,880 --> 03:50:45,800 randomized so we're going to pick a 5341 03:50:45,800 --> 03:50:48,159 random one of the valid collaterals to 5342 03:50:48,159 --> 03:50:50,120 deposit and we're going to pick a random 5343 03:50:50,120 --> 03:50:52,279 amount of collateral now I'm going to 5344 03:50:52,279 --> 03:50:55,359 write this function without any guard 5345 03:50:55,359 --> 03:50:57,319 rails and it's going to break and that's 5346 03:50:57,319 --> 03:50:58,479 okay we're going to fix it as we go 5347 03:50:58,479 --> 03:51:01,279 along but if we were to just not have 5348 03:51:01,279 --> 03:51:03,680 any guard rails on this at all we would 5349 03:51:03,680 --> 03:51:07,720 just say dsce do deposit 5350 03:51:07,720 --> 03:51:09,800 collateral 5351 03:51:09,800 --> 03:51:12,680 ceral and we would do you know 5352 03:51:12,680 --> 03:51:15,520 collateral and an amount collat Al we 5353 03:51:15,520 --> 03:51:17,359 could actually just have this be 5354 03:51:17,359 --> 03:51:20,600 address collateral and amount collateral 5355 03:51:20,600 --> 03:51:24,040 and we'd say deposit this collateral and 5356 03:51:24,040 --> 03:51:26,760 amount collateral and this of course is 5357 03:51:26,760 --> 03:51:28,520 probably going to break a lot right 5358 03:51:28,520 --> 03:51:29,720 because the collateral we're going to 5359 03:51:29,720 --> 03:51:31,040 pass is going to be wrong this is a 5360 03:51:31,040 --> 03:51:32,279 random address there are so many 5361 03:51:32,279 --> 03:51:34,199 addresses and the amount of collateral 5362 03:51:34,199 --> 03:51:35,720 could also break because deposit 5363 03:51:35,720 --> 03:51:38,560 collateral reverts on zero right but I 5364 03:51:38,560 --> 03:51:40,199 do want to show you what we're actually 5365 03:51:40,199 --> 03:51:43,840 going to do in our actual contract here 5366 03:51:43,840 --> 03:51:46,920 instead of having our Target contract be 5367 03:51:46,920 --> 03:51:49,439 the dsce what we're going to do is we're 5368 03:51:49,439 --> 03:51:51,520 going to say Handler is we're going to 5369 03:51:51,520 --> 03:51:52,960 import this Handler so we're going to do 5370 03:51:52,960 --> 03:51:56,239 import Handler 5371 03:51:56,239 --> 03:52:00,920 from handler. T.O Handler Handler we're 5372 03:52:00,920 --> 03:52:04,040 going to say Handler equals new 5373 03:52:04,040 --> 03:52:08,000 Handler dsce and DSC and now we're going 5374 03:52:08,000 --> 03:52:10,760 to say our Target contract is just the 5375 03:52:10,760 --> 03:52:13,840 Handler okay excuse me the address 5376 03:52:13,840 --> 03:52:16,840 Handler now with this the Handler only 5377 03:52:16,840 --> 03:52:19,800 has this one function so we're going to 5378 03:52:19,800 --> 03:52:21,520 call deposit collateral through the 5379 03:52:21,520 --> 03:52:23,399 Handler which is going to call our 5380 03:52:23,399 --> 03:52:25,080 engine and since this is the only 5381 03:52:25,080 --> 03:52:27,439 function for it to call this is all 5382 03:52:27,439 --> 03:52:28,760 we're going to see if we see our 5383 03:52:28,760 --> 03:52:31,000 invariance break so if I do Forge test 5384 03:52:31,000 --> 03:52:33,399 DM paste this in we're going to see it 5385 03:52:33,399 --> 03:52:35,920 break almost 5386 03:52:35,960 --> 03:52:38,239 instantly with an issue but it's only 5387 03:52:38,239 --> 03:52:39,560 going to be deposit collateral because 5388 03:52:39,560 --> 03:52:41,680 it's the only function that we have 5389 03:52:41,680 --> 03:52:43,359 right we run it again it's still going 5390 03:52:43,359 --> 03:52:45,040 to be deposit collateral cuz the only 5391 03:52:45,040 --> 03:52:49,120 function the Handler has yes makes sense 5392 03:52:49,120 --> 03:52:51,600 okay cool hopefully it makes sense in 5393 03:52:51,600 --> 03:52:53,920 our Foundry doomo we could say fail on 5394 03:52:53,920 --> 03:52:56,800 revert is false we could run this again 5395 03:52:56,800 --> 03:52:59,439 let's make this a little bit lower 128 5396 03:52:59,439 --> 03:53:01,520 and let's see if we get any valid runs 5397 03:53:01,520 --> 03:53:04,600 right let's see if we pick miraculously 5398 03:53:04,600 --> 03:53:08,640 a valid address and we get success but 5399 03:53:08,640 --> 03:53:10,000 oh it looks like we did do some valid 5400 03:53:10,000 --> 03:53:12,439 ones but we've got a ton of reverts here 5401 03:53:12,439 --> 03:53:14,520 we've got so many reverts here that 5402 03:53:14,520 --> 03:53:16,319 almost half of these runs almost half of 5403 03:53:16,319 --> 03:53:18,840 these calls were bad that's not a great 5404 03:53:18,840 --> 03:53:21,439 use of our and this isn't really super 5405 03:53:21,439 --> 03:53:24,120 helpful because of course of course our 5406 03:53:24,120 --> 03:53:25,520 invariant here is going to hold because 5407 03:53:25,520 --> 03:53:27,040 the only thing we've allowed our system 5408 03:53:27,040 --> 03:53:29,120 to do is deposit collateral right 5409 03:53:29,120 --> 03:53:32,199 ridiculous so if we turn this back to 5410 03:53:32,199 --> 03:53:35,560 true we run this again we're a course 5411 03:53:35,560 --> 03:53:37,120 going to break now because some of these 5412 03:53:37,120 --> 03:53:39,760 deposits are going to fail right so it 5413 03:53:39,760 --> 03:53:43,239 looks like if we call deposit collateral 5414 03:53:43,239 --> 03:53:45,080 with some horrible address 5415 03:53:45,080 --> 03:53:47,239 it fails right so that's one of the 5416 03:53:47,239 --> 03:53:49,680 first things we want to to have our 5417 03:53:49,680 --> 03:53:51,359 handler do we want to say hey you're 5418 03:53:51,359 --> 03:53:54,319 only allowed to deposit valid collateral 5419 03:53:54,319 --> 03:53:56,399 so what we can do in our Handler is we 5420 03:53:56,399 --> 03:53:57,920 can set that up hey you're only allowed 5421 03:53:57,920 --> 03:54:01,080 to deposit valid collateral instead of 5422 03:54:01,080 --> 03:54:03,520 passing any address as collateral we'll 5423 03:54:03,520 --> 03:54:06,080 say U 256 5424 03:54:06,080 --> 03:54:09,720 collateral seed okay and what we're 5425 03:54:09,720 --> 03:54:11,880 going to do with this seed is we're 5426 03:54:11,880 --> 03:54:14,359 going to have it pick from our two coll 5427 03:54:14,359 --> 03:54:16,080 lals we're going to have it randomly 5428 03:54:16,080 --> 03:54:19,479 pick either we or BTC so we're actually 5429 03:54:19,479 --> 03:54:21,680 going to create a function helper 5430 03:54:21,680 --> 03:54:24,520 functions we're going to say function 5431 03:54:24,520 --> 03:54:28,319 underscore get collateral collat from 5432 03:54:28,319 --> 03:54:32,640 seed un 256 collateral seed and we're 5433 03:54:32,640 --> 03:54:34,399 going to make this a private view 5434 03:54:34,399 --> 03:54:38,239 function it's going to return an ER an I 5435 03:54:38,239 --> 03:54:40,640 erc20 actually we're going to do an 5436 03:54:40,640 --> 03:54:44,680 erc20 Mock and I'll explain why in a bit 5437 03:54:44,680 --> 03:54:48,439 we got to import that import rc20 mock 5438 03:54:48,439 --> 03:54:52,840 from at open Zeppelin contracts mocks 5439 03:54:52,840 --> 03:54:55,800 slash what is it mock ERC 5440 03:54:55,800 --> 03:54:57,399 noc2 5441 03:54:57,399 --> 03:55:00,080 mo. cool and so what we're going to do 5442 03:55:00,080 --> 03:55:01,760 instead of this line is we're going to 5443 03:55:01,760 --> 03:55:05,399 use this line this function to do it 5444 03:55:05,399 --> 03:55:10,279 we're going to say if collateral collat 5445 03:55:10,279 --> 03:55:14,960 all seed modulo 2 cuz we're only we only 5446 03:55:14,960 --> 03:55:17,800 have two collaterals equals equals zero 5447 03:55:17,800 --> 03:55:23,199 then return we we can get we by sticking 5448 03:55:23,199 --> 03:55:28,080 at the top we'll say erc20 mock we ERC 5449 03:55:28,080 --> 03:55:31,159 20 mock WFT 5450 03:55:31,159 --> 03:55:34,359 Bitcoin and right in our Constructor we 5451 03:55:34,359 --> 03:55:38,159 can get all of our collateral tokens the 5452 03:55:38,159 --> 03:55:39,960 I made a function way down at the bottom 5453 03:55:39,960 --> 03:55:43,560 here called get collateral tokens which 5454 03:55:43,560 --> 03:55:45,680 returns the full array of collateral 5455 03:55:45,680 --> 03:55:47,080 tokens if you don't have this feel free 5456 03:55:47,080 --> 03:55:49,439 to pause and implement this what we can 5457 03:55:49,439 --> 03:55:53,880 do we can say address array memory 5458 03:55:53,880 --> 03:55:58,800 collateral collateral tokens equals dsce 5459 03:55:58,800 --> 03:56:01,960 doget collateral tokens like this and we 5460 03:56:01,960 --> 03:56:06,439 can say we is zero and R BTC is one 5461 03:56:06,439 --> 03:56:07,600 that's how we can get those collateral 5462 03:56:07,600 --> 03:56:11,600 tokens so we R BTC if collateral C 5463 03:56:11,600 --> 03:56:14,520 divided by 2 is z return We otherwise 5464 03:56:14,520 --> 03:56:17,080 return wrapped BTC so now we have a 5465 03:56:17,080 --> 03:56:20,520 function where we can only get a 5466 03:56:20,520 --> 03:56:23,520 valid collateral type so instead of just 5467 03:56:23,520 --> 03:56:25,920 depositing any collateral type we can 5468 03:56:25,920 --> 03:56:31,520 say uh erc20 mock colateral equals get 5469 03:56:31,520 --> 03:56:34,640 collateral from seed collateral seed and 5470 03:56:34,640 --> 03:56:36,880 now we're still depositing a random 5471 03:56:36,880 --> 03:56:40,000 collateral but this is oh it's got to be 5472 03:56:40,000 --> 03:56:43,120 address but this is a valid collateral 5473 03:56:43,120 --> 03:56:45,600 address so we're probably more likely to 5474 03:56:45,600 --> 03:56:47,199 actually pass a transaction that will 5475 03:56:47,199 --> 03:56:50,359 actually go through making us have more 5476 03:56:50,359 --> 03:56:52,000 solid more good random calls so let's 5477 03:56:52,000 --> 03:56:55,040 try to run this function 5478 03:56:55,800 --> 03:56:58,840 now great successful but we ran into an 5479 03:56:58,840 --> 03:57:00,479 error right let's see what the error is 5480 03:57:00,479 --> 03:57:04,080 so deposited collateral you U and 256 U 5481 03:57:04,080 --> 03:57:04,760 and 5482 03:57:04,760 --> 03:57:08,120 256 with this huge collateral 5483 03:57:08,120 --> 03:57:10,120 seed uh and 5484 03:57:10,120 --> 03:57:13,760 nothing looks like we failed here it's 5485 03:57:13,760 --> 03:57:17,040 clear let's rerun it with- vvvv so we 5486 03:57:17,040 --> 03:57:18,520 can see a little bit output of why we 5487 03:57:18,520 --> 03:57:21,800 actually failed okay we ran we still the 5488 03:57:21,800 --> 03:57:23,080 only function we're calling is deposit 5489 03:57:23,080 --> 03:57:25,319 collateral looks like this failed again 5490 03:57:25,319 --> 03:57:26,760 we called deposit collateral with some 5491 03:57:26,760 --> 03:57:29,040 weird args we can scroll up and see 5492 03:57:29,040 --> 03:57:32,600 exactly why we failed oh we ran to this 5493 03:57:32,600 --> 03:57:35,199 DSC engine needs more than zero so it 5494 03:57:35,199 --> 03:57:37,720 looks like we tried to deposit zero 5495 03:57:37,720 --> 03:57:41,359 collateral Mount collateral was Zero yep 5496 03:57:41,359 --> 03:57:43,479 okay yep so we tried to call was zero so 5497 03:57:43,479 --> 03:57:45,800 amount collateral was zero so we know 5498 03:57:45,800 --> 03:57:47,319 that this is going to fail so how can we 5499 03:57:47,319 --> 03:57:50,319 make it so that this doesn't fail or 5500 03:57:50,319 --> 03:57:52,159 maybe you're like hey like sometimes it 5501 03:57:52,159 --> 03:57:54,359 will be zero like whatever I I just want 5502 03:57:54,359 --> 03:57:55,720 a sanity check you can make fail and 5503 03:57:55,720 --> 03:57:57,960 revert false right and we can run this 5504 03:57:57,960 --> 03:57:59,920 again and now we'll see how often this 5505 03:57:59,920 --> 03:58:02,359 actually fails hopefully we cut down on 5506 03:58:02,359 --> 03:58:04,600 the amount of times it failed oops let's 5507 03:58:04,600 --> 03:58:07,120 remove those 5508 03:58:10,120 --> 03:58:12,520 V's great it actually it does look like 5509 03:58:12,520 --> 03:58:14,279 we cut down on the amount of reverts we 5510 03:58:14,279 --> 03:58:17,080 got not a lot but we we did cut down the 5511 03:58:17,080 --> 03:58:19,840 amount of reverts at least by adding 5512 03:58:19,840 --> 03:58:22,359 this bounding of the collateral types 5513 03:58:22,359 --> 03:58:23,920 but let's keep cutting down on these 5514 03:58:23,920 --> 03:58:26,359 reverts and potentially even have revert 5515 03:58:26,359 --> 03:58:28,760 on false be true right you're not always 5516 03:58:28,760 --> 03:58:30,399 going to have revert on false be able to 5517 03:58:30,399 --> 03:58:33,239 be true and sometimes it's quicker just 5518 03:58:33,239 --> 03:58:34,439 to have it false and write all your 5519 03:58:34,439 --> 03:58:36,199 invariance and stuff but if you want 5520 03:58:36,199 --> 03:58:39,080 kind of but is good to aim for this now 5521 03:58:39,080 --> 03:58:41,880 the downside of always aiming for this 5522 03:58:41,880 --> 03:58:44,720 is that if you make your handle too 5523 03:58:44,720 --> 03:58:47,760 specific maybe you'll actually narrow it 5524 03:58:47,760 --> 03:58:49,840 down and remove edge cases that would 5525 03:58:49,840 --> 03:58:52,000 break the system that are valid right so 5526 03:58:52,000 --> 03:58:53,279 it's kind of this balancing game you 5527 03:58:53,279 --> 03:58:55,199 have to play with these fuzzing tests 5528 03:58:55,199 --> 03:58:56,479 and whether fail and revert to be true 5529 03:58:56,479 --> 03:58:57,800 or false there's definitely a little bit 5530 03:58:57,800 --> 03:59:00,600 of an art to this so the more you do it 5531 03:59:00,600 --> 03:59:03,720 the better you'll get but in any case 5532 03:59:03,720 --> 03:59:05,880 collateral seed amount collateral we 5533 03:59:05,880 --> 03:59:07,640 need to now change it so that this 5534 03:59:07,640 --> 03:59:11,520 amount collateral is bounded between one 5535 03:59:11,520 --> 03:59:13,640 and some Max number right so we don't 5536 03:59:13,640 --> 03:59:16,279 want this to be able to be zero so what 5537 03:59:16,279 --> 03:59:18,680 we can do then instead is we can 5538 03:59:18,680 --> 03:59:20,640 actually we actually can bound this so 5539 03:59:20,640 --> 03:59:22,800 that in the same way we got a valid 5540 03:59:22,800 --> 03:59:24,960 collateral let's get a valid amount 5541 03:59:24,960 --> 03:59:27,840 collateral so we can say amount 5542 03:59:27,840 --> 03:59:31,439 collateral equals bound this is a 5543 03:59:31,439 --> 03:59:32,920 function that actually comes with STD 5544 03:59:32,920 --> 03:59:36,000 utils and it bounds the result to an 5545 03:59:36,000 --> 03:59:37,960 amount so we want to say okay this 5546 03:59:37,960 --> 03:59:40,399 amount collateral we're going to bound 5547 03:59:40,399 --> 03:59:44,680 our amount collateral to being between 5548 03:59:44,680 --> 03:59:46,680 one we don't want to be zero and then 5549 03:59:46,680 --> 03:59:49,199 some like really really big number what 5550 03:59:49,199 --> 03:59:51,159 I like to do is up here in the state 5551 03:59:51,159 --> 03:59:57,040 variables I'll do U 256 Max deposit size 5552 03:59:57,040 --> 04:00:01,520 equals and I'll do type uint 5553 04:00:01,520 --> 04:00:03,120 96. 5554 04:00:03,120 --> 04:00:06,720 Max and this allows us to get the max 5555 04:00:06,720 --> 04:00:10,359 uent 96 value why are we not doing the 5556 04:00:10,359 --> 04:00:14,279 max U 256 well if we do the max U 256 5557 04:00:14,279 --> 04:00:16,239 and we try to deposit more collateral 5558 04:00:16,239 --> 04:00:19,399 later if you do the max6 plus one you'll 5559 04:00:19,399 --> 04:00:20,960 get a revert so this is going to give us 5560 04:00:20,960 --> 04:00:23,399 a really really really big number but 5561 04:00:23,399 --> 04:00:25,159 we're at least not going to hit the 5562 04:00:25,159 --> 04:00:28,359 absolute top of amount of deposits we 5563 04:00:28,359 --> 04:00:30,120 can deposit so we're going to say we're 5564 04:00:30,120 --> 04:00:32,279 going to bound this amount collateral 5565 04:00:32,279 --> 04:00:36,159 between one and Max deposit size Okay 5566 04:00:36,159 --> 04:00:38,399 cool so let's put this back to false 5567 04:00:38,399 --> 04:00:40,439 let's run this test again and let's see 5568 04:00:40,439 --> 04:00:45,080 if we cut down on the reverse some more 5569 04:00:46,359 --> 04:00:48,760 aha a couple more right only a few 5570 04:00:48,760 --> 04:00:50,319 hundred but we did cut down on the 5571 04:00:50,319 --> 04:00:52,520 amount of reverts why because we're 5572 04:00:52,520 --> 04:00:55,279 passing valid well okay that one didn't 5573 04:00:55,279 --> 04:00:57,720 go so well but we are cutting down on 5574 04:00:57,720 --> 04:01:00,120 the amount of reverts piece by piece 5575 04:01:00,120 --> 04:01:02,920 we're able to bound the collateral but 5576 04:01:02,920 --> 04:01:06,640 there's more if we do failon revert back 5577 04:01:06,640 --> 04:01:09,560 to true we can see exactly why it's 5578 04:01:09,560 --> 04:01:13,359 failing right we'll do- vvv VV we can 5579 04:01:13,359 --> 04:01:14,960 see an examp example that is indeed 5580 04:01:14,960 --> 04:01:17,319 failing and who these are getting more 5581 04:01:17,319 --> 04:01:19,239 intricate right before it was just a 5582 04:01:19,239 --> 04:01:21,319 single call was breaking it oh okay a 5583 04:01:21,319 --> 04:01:22,960 single call still is breaking it but 5584 04:01:22,960 --> 04:01:23,800 they are getting a little bit more 5585 04:01:23,800 --> 04:01:26,720 intricate so looks like this one failed 5586 04:01:26,720 --> 04:01:28,720 because what we called deposit 5587 04:01:28,720 --> 04:01:30,600 collateral we scroll up we're getting 5588 04:01:30,600 --> 04:01:32,359 this insufficient allowance okay so 5589 04:01:32,359 --> 04:01:33,600 that's why we actually weren't cutting 5590 04:01:33,600 --> 04:01:35,319 down on the reverts at all because we're 5591 04:01:35,319 --> 04:01:37,040 getting insufficient allowance of course 5592 04:01:37,040 --> 04:01:39,279 we need to approve the protocol to 5593 04:01:39,279 --> 04:01:41,159 deposit this collateral so of course 5594 04:01:41,159 --> 04:01:43,159 this is breaking this is always going to 5595 04:01:43,159 --> 04:01:46,159 break so let's do a little prank so 5596 04:01:46,159 --> 04:01:49,600 we'll do vm. start prank we'll just do 5597 04:01:49,600 --> 04:01:54,279 message. sender and we H will allow this 5598 04:01:54,279 --> 04:01:56,120 message sender to Mint some of this 5599 04:01:56,120 --> 04:01:58,000 collateral so that they can actually 5600 04:01:58,000 --> 04:02:00,840 deposit it right so we can set this up 5601 04:02:00,840 --> 04:02:02,199 so that whoever's calling deposit 5602 04:02:02,199 --> 04:02:04,960 collateral actually has the collateral 5603 04:02:04,960 --> 04:02:07,120 and actually will approve to deposit the 5604 04:02:07,120 --> 04:02:08,960 collateral so and this is why I'm using 5605 04:02:08,960 --> 04:02:10,840 this erc20 mock so that we can actually 5606 04:02:10,840 --> 04:02:14,560 mint some of this collateral so we'll do 5607 04:02:14,560 --> 04:02:19,080 collateral. mint message. sender amount 5608 04:02:19,080 --> 04:02:21,920 collateral we'll do collateral 5609 04:02:21,920 --> 04:02:25,760 collateral. approve address 5610 04:02:25,760 --> 04:02:29,199 dsce for the amount collateral then we 5611 04:02:29,199 --> 04:02:31,399 can deposit it and then we'll do vm. 5612 04:02:31,399 --> 04:02:34,680 stop prank okay now let's clear this 5613 04:02:34,680 --> 04:02:37,920 let's have revert on false to be false 5614 04:02:37,920 --> 04:02:39,920 and let's see now if we cut down on the 5615 04:02:39,920 --> 04:02:44,960 amount of reverts that we get 5616 04:02:47,680 --> 04:02:49,399 oh and I added too many V's let's get 5617 04:02:49,399 --> 04:02:51,199 rid of those let's run this 5618 04:02:51,199 --> 04:02:53,439 again and yep there will be some brief 5619 04:02:53,439 --> 04:02:56,319 delays in here of course whoa we cut the 5620 04:02:56,319 --> 04:02:58,960 amount of reverts down to zero now all 5621 04:02:58,960 --> 04:03:00,840 of our function calls are passing which 5622 04:03:00,840 --> 04:03:04,720 means every single run was a valid run 5623 04:03:04,720 --> 04:03:07,600 meaning we're using our runs much more 5624 04:03:07,600 --> 04:03:10,880 wisely we're not wasting runs on failed 5625 04:03:10,880 --> 04:03:13,159 reverts so now I can even set this to 5626 04:03:13,159 --> 04:03:17,159 true run this again and we'll see this 5627 04:03:17,159 --> 04:03:22,080 passes and none of our runs failed right 5628 04:03:22,080 --> 04:03:24,640 so this means what does this mean from a 5629 04:03:24,640 --> 04:03:27,040 security standpoint it means that no 5630 04:03:27,040 --> 04:03:29,439 matter how often we call deposit 5631 04:03:29,439 --> 04:03:33,199 collateral no matter how much we deposit 5632 04:03:33,199 --> 04:03:36,560 our collateral we will never make this 5633 04:03:36,560 --> 04:03:39,880 invariant false which isn't saying too 5634 04:03:39,880 --> 04:03:42,159 much because of course we're not even 5635 04:03:42,159 --> 04:03:43,479 the total Supply is always zero of 5636 04:03:43,479 --> 04:03:46,359 course holds so this makes a lot of 5637 04:03:46,359 --> 04:03:48,880 sense so let's actually keep writing 5638 04:03:48,880 --> 04:03:51,080 more functions to do more with the 5639 04:03:51,080 --> 04:03:53,239 system but set them up so that whenever 5640 04:03:53,239 --> 04:03:54,880 we call them they're always going to be 5641 04:03:54,880 --> 04:03:57,880 valid calls 5642 04:03:58,960 --> 04:04:01,319 okay so now we're talking about redeem 5643 04:04:01,319 --> 04:04:03,080 collateral right okay cool so now 5644 04:04:03,080 --> 04:04:05,239 there's actually now we actually have a 5645 04:04:05,239 --> 04:04:07,640 function a valid function to deposit 5646 04:04:07,640 --> 04:04:09,520 collateral now let's actually have a 5647 04:04:09,520 --> 04:04:11,680 valid function to redeem collateral so 5648 04:04:11,680 --> 04:04:13,120 we're going to do the same thing 5649 04:04:13,120 --> 04:04:14,560 function 5650 04:04:14,560 --> 04:04:16,520 redeem 5651 04:04:16,520 --> 04:04:18,120 collateral 5652 04:04:18,120 --> 04:04:19,760 collateral we're going to do the same 5653 04:04:19,760 --> 04:04:21,239 thing it's going to take a un26 5654 04:04:21,239 --> 04:04:23,479 collateral seed for which collateral to 5655 04:04:23,479 --> 04:04:27,239 redeem un 256 amount 5656 04:04:27,239 --> 04:04:29,920 collateral so be a public and we're 5657 04:04:29,920 --> 04:04:31,680 going to do something very similar here 5658 04:04:31,680 --> 04:04:33,800 we're going to say we're only going to 5659 04:04:33,800 --> 04:04:35,640 choose a valid collateral by saying 5660 04:04:35,640 --> 04:04:40,359 erc20 mock collateral equals underscore 5661 04:04:40,359 --> 04:04:43,960 get collateral from seed collateral seed 5662 04:04:43,960 --> 04:04:46,399 and now we should only allow people to 5663 04:04:46,399 --> 04:04:49,199 redeem the maximum amount they have in 5664 04:04:49,199 --> 04:04:51,720 the system right so we're going to say 5665 04:04:51,720 --> 04:04:57,560 un 256 Max collateral collateral to 5666 04:04:57,560 --> 04:05:02,840 redeem equals DSC engine 5667 04:05:02,840 --> 04:05:06,920 engine dot get 5668 04:05:06,920 --> 04:05:10,199 total get 5669 04:05:10,199 --> 04:05:13,880 collateral balance of user do we have 5670 04:05:13,880 --> 04:05:15,760 this 5671 04:05:15,760 --> 04:05:18,520 function DSC 5672 04:05:18,520 --> 04:05:21,279 engine so this is one that I added in 5673 04:05:21,279 --> 04:05:22,880 it's going to get the collateral balance 5674 04:05:22,880 --> 04:05:24,600 of a user if you passing the user in the 5675 04:05:24,600 --> 04:05:26,239 token so if you want to pause and add 5676 04:05:26,239 --> 04:05:28,399 this in feel free to do so get 5677 04:05:28,399 --> 04:05:30,319 collateral balance of user where we add 5678 04:05:30,319 --> 04:05:32,800 the address of the 5679 04:05:32,800 --> 04:05:35,920 collateral and the message. sender so 5680 04:05:35,920 --> 04:05:38,000 it's going to get the total balance of a 5681 04:05:38,000 --> 04:05:41,239 user oh and this should be 5682 04:05:41,239 --> 04:05:44,080 dsce and then we're going to bounce the 5683 04:05:44,080 --> 04:05:46,040 amount collateral to this max amount 5684 04:05:46,040 --> 04:05:47,520 that they have to make these always 5685 04:05:47,520 --> 04:05:49,680 valid they should only be redeeming as 5686 04:05:49,680 --> 04:05:52,640 much as they put in the system amount 5687 04:05:52,640 --> 04:05:54,680 collateral equals 5688 04:05:54,680 --> 04:05:58,319 bound amount 5689 04:05:59,120 --> 04:06:03,600 collateral uh we're going to say redeem 5690 04:06:04,840 --> 04:06:08,120 between one and the max collateral to 5691 04:06:08,120 --> 04:06:09,760 redeem because we don't want them to 5692 04:06:09,760 --> 04:06:12,120 redeem zero of course and then we're 5693 04:06:12,120 --> 04:06:15,439 going to say it's going to be public 5694 04:06:15,439 --> 04:06:18,800 then we're going to say dsce do redeem 5695 04:06:18,800 --> 04:06:21,840 collateral address collateral and amount 5696 04:06:21,840 --> 04:06:24,319 collateral right so now we have two 5697 04:06:24,319 --> 04:06:26,600 functions to randomly call so we're 5698 04:06:26,600 --> 04:06:28,000 going to be depositing collateral and 5699 04:06:28,000 --> 04:06:30,359 redeeming collateral let's run this 5700 04:06:30,359 --> 04:06:32,399 invariant it only has two functions it 5701 04:06:32,399 --> 04:06:35,399 could call go to the found. toml fail 5702 04:06:35,399 --> 04:06:36,960 and revert is true oh and it looks like 5703 04:06:36,960 --> 04:06:39,319 we found an edge case so now we can read 5704 04:06:39,319 --> 04:06:40,960 this and see what it's doing so it's 5705 04:06:40,960 --> 04:06:45,359 depositing some collateral 1 38 1 and 82 5706 04:06:45,359 --> 04:06:48,880 and then it's Redeeming the collateral H 5707 04:06:48,880 --> 04:06:50,239 not actually sure what's going on so 5708 04:06:50,239 --> 04:06:51,520 let's add 5709 04:06:51,520 --> 04:06:53,640 this Dash 5710 04:06:53,640 --> 04:06:56,640 vvvv to see more into the actual 5711 04:06:56,640 --> 04:06:58,600 transaction that's failing oh my 5712 04:06:58,600 --> 04:07:00,960 goodness we got an even bigger one here 5713 04:07:00,960 --> 04:07:03,040 so what are we saying oh Max is less 5714 04:07:03,040 --> 04:07:05,080 than the min oh looks like I'm messing 5715 04:07:05,080 --> 04:07:07,359 up with my bounding so it's not even 5716 04:07:07,359 --> 04:07:09,880 really an issue so if we go back to the 5717 04:07:09,880 --> 04:07:13,199 Handler bound M collateral One Max 5718 04:07:13,199 --> 04:07:17,439 collateral Al to redeem ah okay so if 5719 04:07:17,439 --> 04:07:20,199 Max collateral to redeem is zero this 5720 04:07:20,199 --> 04:07:22,359 will break so we actually do need to 5721 04:07:22,359 --> 04:07:24,199 keep zero in here and then we can just 5722 04:07:24,199 --> 04:07:27,520 say if you know amount collateral equals 5723 04:07:27,520 --> 04:07:30,239 equals z we could say if it's zero 5724 04:07:30,239 --> 04:07:32,479 return or what we could do is we could 5725 04:07:32,479 --> 04:07:35,439 use this keyword called assume like vm. 5726 04:07:35,439 --> 04:07:38,239 assume which will say if the Boolean 5727 04:07:38,239 --> 04:07:40,199 expression evaluates to false the fuzzer 5728 04:07:40,199 --> 04:07:42,040 will discard the current fuzz inputs and 5729 04:07:42,040 --> 04:07:43,800 start a new fuzz run so we're going to 5730 04:07:43,800 --> 04:07:45,880 do if the amount collateral zero just 5731 04:07:45,880 --> 04:07:48,720 return don't call this function right 5732 04:07:48,720 --> 04:07:50,800 cuz this is going to fail so let's go 5733 04:07:50,800 --> 04:07:53,000 ahead and run this again now compiler 5734 04:07:53,000 --> 04:07:55,199 run 5735 04:07:56,319 --> 04:07:58,880 successful well would you look at that 5736 04:07:58,880 --> 04:08:02,760 we are now passing again so people can 5737 04:08:02,760 --> 04:08:05,720 if we go back to the Handler people can 5738 04:08:05,720 --> 04:08:07,800 now redeem collateral and all of these 5739 04:08:07,800 --> 04:08:10,600 redeeming are going to be valid and all 5740 04:08:10,600 --> 04:08:11,960 these deposits are going to be valid 5741 04:08:11,960 --> 04:08:14,000 right they can only redeem 5742 04:08:14,000 --> 04:08:16,359 valid Redemption amounts now here's 5743 04:08:16,359 --> 04:08:18,920 where this fail and revert equals true 5744 04:08:18,920 --> 04:08:22,520 is can be a little bit deceptive let's 5745 04:08:22,520 --> 04:08:25,319 say right now we're only letting you 5746 04:08:25,319 --> 04:08:28,279 redeem the max collateral to redeem 5747 04:08:28,279 --> 04:08:31,720 let's say there was a bug where a user 5748 04:08:31,720 --> 04:08:34,760 can redeem more than they have this fuzz 5749 04:08:34,760 --> 04:08:36,920 test wouldn't catch this it's because we 5750 04:08:36,920 --> 04:08:39,080 have this fail and revert is true if 5751 04:08:39,080 --> 04:08:41,720 this fail on revert was false and we 5752 04:08:41,720 --> 04:08:44,760 didn't have this line right and we just 5753 04:08:44,760 --> 04:08:48,479 said this is the max deposit size this 5754 04:08:48,479 --> 04:08:50,159 is a test where we might actually catch 5755 04:08:50,159 --> 04:08:54,279 that bug right and if we run this now oh 5756 04:08:54,279 --> 04:08:55,800 and with fail on revert is false we are 5757 04:08:55,800 --> 04:08:57,159 going to get a whole bunch of reverts we 5758 04:08:57,159 --> 04:08:59,399 are going to get bad transactions 5759 04:08:59,399 --> 04:09:01,399 however and you can see we can see the 5760 04:09:01,399 --> 04:09:03,119 number reverts over here and it actually 5761 04:09:03,119 --> 04:09:05,359 is way less than it was before which is 5762 04:09:05,359 --> 04:09:08,520 awesome and if we run it again still way 5763 04:09:08,520 --> 04:09:10,479 less than 8,000 which is what it was 5764 04:09:10,479 --> 04:09:12,880 before but if we have fail and revert 5765 04:09:12,880 --> 04:09:15,399 false we can actually catch this so The 5766 04:09:15,399 --> 04:09:17,560 Foundry team is actually working on 5767 04:09:17,560 --> 04:09:19,880 allowing some test to be fail in revert 5768 04:09:19,880 --> 04:09:22,600 and some test to be not fail in revert 5769 04:09:22,600 --> 04:09:25,199 so you can kind of pick which one for 5770 04:09:25,199 --> 04:09:26,359 which functions instead of kind of 5771 04:09:26,359 --> 04:09:29,640 having to blanket everything so just 5772 04:09:29,640 --> 04:09:32,359 keep in mind the dangers here okay of 5773 04:09:32,359 --> 04:09:34,000 always defaulting to fail on revert 5774 04:09:34,000 --> 04:09:36,159 equals true okay they have their 5775 04:09:36,159 --> 04:09:37,800 trade-offs and if you do fail on rever 5776 04:09:37,800 --> 04:09:40,159 equals false you can sometimes write 5777 04:09:40,159 --> 04:09:44,119 these handlers a lot quicker 5778 04:09:45,640 --> 04:09:48,319 but okay so we have some stuff here we 5779 04:09:48,319 --> 04:09:50,000 can deposit collateral we can redeem 5780 04:09:50,000 --> 04:09:52,560 collateral what else so we probably 5781 04:09:52,560 --> 04:09:53,640 should figure out a way to get some 5782 04:09:53,640 --> 04:09:55,640 total Supply right so what we're going 5783 04:09:55,640 --> 04:09:57,199 to do is now we're going to finally make 5784 04:09:57,199 --> 04:10:01,600 our mint function function mint 5785 04:10:01,600 --> 04:10:05,600 DSC public like like this and what do we 5786 04:10:05,600 --> 04:10:07,359 want to put in here well if we go to the 5787 04:10:07,359 --> 04:10:11,080 DSC engine go to Mint DSC go to this 5788 04:10:11,080 --> 04:10:13,640 mint DSC function it takes an amount of 5789 04:10:13,640 --> 04:10:16,119 DSC to Mint so we'll do the same thing 5790 04:10:16,119 --> 04:10:20,279 we'll say uint 256 amount and that's it 5791 04:10:20,279 --> 04:10:24,199 we'll mint a random amount so in here of 5792 04:10:24,199 --> 04:10:27,080 course we can't have amount be a number 5793 04:10:27,080 --> 04:10:29,760 of things right we can't have it be zero 5794 04:10:29,760 --> 04:10:32,080 and they would need to not have their 5795 04:10:32,080 --> 04:10:34,159 health Factor be broken so we're going 5796 04:10:34,159 --> 04:10:39,640 to say amount equals bound amount one 5797 04:10:39,640 --> 04:10:42,439 and let's say Max deposit size and then 5798 04:10:42,439 --> 04:10:45,439 additionally this amount better be more 5799 04:10:45,439 --> 04:10:48,279 than the value of the system right 5800 04:10:48,279 --> 04:10:49,800 because we have this revert if Health 5801 04:10:49,800 --> 04:10:52,960 factor is broken but maybe I don't even 5802 04:10:52,960 --> 04:10:55,080 want to do that narrowing down maybe I 5803 04:10:55,080 --> 04:10:56,119 just say screw it I'm going to make 5804 04:10:56,119 --> 04:10:58,920 revert on false revert on fail on revert 5805 04:10:58,920 --> 04:11:00,119 false and I'm just going to leave it 5806 04:11:00,119 --> 04:11:02,600 like that because I'm nervous that I'm 5807 04:11:02,600 --> 04:11:04,760 going to narrow it down too much so then 5808 04:11:04,760 --> 04:11:08,080 maybe I just go screw it bm. start prank 5809 04:11:08,080 --> 04:11:11,399 message. sender and we call in the DSC 5810 04:11:11,399 --> 04:11:16,840 engine mint DSC dce do mint DSC and the 5811 04:11:16,840 --> 04:11:20,800 amount then bm. stop Rank and this is 5812 04:11:20,800 --> 04:11:23,319 where actually you'll see sometimes some 5813 04:11:23,319 --> 04:11:28,479 people will have continue on revert and 5814 04:11:28,479 --> 04:11:31,800 a fail on revert fail on 5815 04:11:31,800 --> 04:11:34,000 revert see people some people can have 5816 04:11:34,000 --> 04:11:36,560 two types of folders continue on revert 5817 04:11:36,560 --> 04:11:40,319 is going to be the quicker looser test 5818 04:11:40,319 --> 04:11:42,399 where it might look like this the fail 5819 04:11:42,399 --> 04:11:43,640 on revert is going to going to make sure 5820 04:11:43,640 --> 04:11:45,199 that every single transaction that you 5821 04:11:45,199 --> 04:11:46,920 run your invariant test sued on is going 5822 04:11:46,920 --> 04:11:49,239 to pass I personally think it's good to 5823 04:11:49,239 --> 04:11:51,359 have both when I'm writing invariant 5824 04:11:51,359 --> 04:11:53,319 tests I actually start with to continue 5825 04:11:53,319 --> 04:11:55,239 on revert because they're faster often 5826 04:11:55,239 --> 04:11:57,319 times you can find bugs as long as you 5827 04:11:57,319 --> 04:12:00,239 narrow down them enough so for now let's 5828 04:12:00,239 --> 04:12:02,359 go ahead let's have this be false see if 5829 04:12:02,359 --> 04:12:03,720 we can find any issues like this so 5830 04:12:03,720 --> 04:12:07,199 we'll go up up so we'll run Forge test- 5831 04:12:07,199 --> 04:12:10,439 M just going to run this invariant test 5832 04:12:10,439 --> 04:12:14,920 boom let's see if we got any issues here 5833 04:12:14,920 --> 04:12:16,479 we'll see how many reverts we get as 5834 04:12:16,479 --> 04:12:18,920 well we got a similar amount of reverts 5835 04:12:18,920 --> 04:12:21,479 which is good similar amount of calls 5836 04:12:21,479 --> 04:12:23,279 which is all right and then actually if 5837 04:12:23,279 --> 04:12:26,159 we do- VV we can even see the Conant 5838 04:12:26,159 --> 04:12:28,479 that logs at the end it looks like it 5839 04:12:28,479 --> 04:12:30,880 hasn't found a way to Mint more tokens 5840 04:12:30,880 --> 04:12:32,479 than collateral in the system oh and 5841 04:12:32,479 --> 04:12:34,760 even it looks like the last run it ended 5842 04:12:34,760 --> 04:12:37,600 with a total supply of zero right but 5843 04:12:37,600 --> 04:12:40,119 maybe I'm paranoid let's actually go 5844 04:12:40,119 --> 04:12:41,880 back to true and let's just stay true 5845 04:12:41,880 --> 04:12:44,439 for the rest of the rest of this but 5846 04:12:44,439 --> 04:12:46,040 like I said I think it's good to have 5847 04:12:46,040 --> 04:12:50,199 both types of test anyways Handler so 5848 04:12:50,199 --> 04:12:52,880 this mint DSC we should only be able to 5849 04:12:52,880 --> 04:12:55,640 Mint DC if the amount is less than the 5850 04:12:55,640 --> 04:12:58,600 collateral so what we can do is we can 5851 04:12:58,600 --> 04:13:00,359 call this get account information which 5852 04:13:00,359 --> 04:13:02,880 gets the total collateral value in USD 5853 04:13:02,880 --> 04:13:06,199 and total DSC minted and make sure that 5854 04:13:06,199 --> 04:13:09,000 we're always going to Mint less than the 5855 04:13:09,000 --> 04:13:11,439 collateral value that we have so we can 5856 04:13:11,439 --> 04:13:14,800 say we can actually just copy these two 5857 04:13:14,800 --> 04:13:18,720 paste it in here equals dsce dot copy 5858 04:13:18,720 --> 04:13:23,560 this function paste message. sender so 5859 04:13:23,560 --> 04:13:25,399 we're going to get the total DSC minted 5860 04:13:25,399 --> 04:13:28,319 B value in USD and what we can do is we 5861 04:13:28,319 --> 04:13:31,040 can say we'll just have them always mint 5862 04:13:31,040 --> 04:13:34,080 the max DSC they can mint so we'll say 5863 04:13:34,080 --> 04:13:39,359 un 256 Max DSC to Mint 5864 04:13:39,359 --> 04:13:42,760 equals collateral value in USD we could 5865 04:13:42,760 --> 04:13:46,720 say the collateral value in USD / 2 5866 04:13:46,720 --> 04:13:49,720 minus the total DS minted better be 5867 04:13:49,720 --> 04:13:51,760 greater than zero right better not be a 5868 04:13:51,760 --> 04:13:54,040 negative number so we can even do like 5869 04:13:54,040 --> 04:13:55,680 an INT 5870 04:13:55,680 --> 04:13:59,520 256 and then if this is negative then 5871 04:13:59,520 --> 04:14:02,439 we're just going to return otherwise 5872 04:14:02,439 --> 04:14:04,479 we're going to say amount we're going to 5873 04:14:04,479 --> 04:14:08,119 equals bound this again say 5874 04:14:08,119 --> 04:14:12,560 amount Z comma Max D to Mint then we're 5875 04:14:12,560 --> 04:14:15,920 also going to say if amount equals zero 5876 04:14:15,920 --> 04:14:18,119 we're going to return oops and then 5877 04:14:18,119 --> 04:14:20,479 actually we need to grab this start 5878 04:14:20,479 --> 04:14:23,520 prank put this down here that's all we 5879 04:14:23,520 --> 04:14:26,399 need to do and we should probably just 5880 04:14:26,399 --> 04:14:29,760 grab these put them down here we don't 5881 04:14:29,760 --> 04:14:32,479 need both of these so we just need this 5882 04:14:32,479 --> 04:14:34,960 I think yeah we don't need both of these 5883 04:14:34,960 --> 04:14:38,199 we only need one of these so we'll do UN 5884 04:14:38,199 --> 04:14:41,119 2 we'll convert this back to un 5885 04:14:41,119 --> 04:14:43,920 256 F am out is zero 5886 04:14:43,920 --> 04:14:45,840 return this looks pretty good to me 5887 04:14:45,840 --> 04:14:48,640 let's see if we made this actually work 5888 04:14:48,640 --> 04:14:51,520 let's run this and now we can mint 5889 04:14:51,520 --> 04:14:53,040 DSC 5890 04:14:53,040 --> 04:14:56,640 successful and bada boom so we have some 5891 04:14:56,640 --> 04:15:01,319 calls we have no reverts this is 5892 04:15:02,680 --> 04:15:05,560 great but I keep getting total Supply is 5893 04:15:05,560 --> 04:15:08,439 zero down here so we plenty of we plenty 5894 04:15:08,439 --> 04:15:12,560 of rap Bitcoin are we ever calling this 5895 04:15:12,560 --> 04:15:13,920 function 5896 04:15:13,920 --> 04:15:17,119 H we keep getting a total supply of zero 5897 04:15:17,119 --> 04:15:19,600 down here why is that how could we 5898 04:15:19,600 --> 04:15:22,840 figure out if our mint DSC is actually 5899 04:15:22,840 --> 04:15:25,279 getting called let's first off see if 5900 04:15:25,279 --> 04:15:28,319 this mint dsse is even being called we 5901 04:15:28,319 --> 04:15:30,520 can use something called ghost variable 5902 04:15:30,520 --> 04:15:33,560 to track this so in here up at the top 5903 04:15:33,560 --> 04:15:37,640 we'll make a function called un 256 5904 04:15:37,640 --> 04:15:39,920 times mint is 5905 04:15:39,920 --> 04:15:45,279 called make this a public variable and 5906 04:15:45,279 --> 04:15:47,319 at the bottom of mint DSC we'll do times 5907 04:15:47,319 --> 04:15:49,840 mint is called 5908 04:15:49,840 --> 04:15:54,439 plus now back in our invariance we do a 5909 04:15:54,439 --> 04:15:59,119 console.log times mint called comma 5910 04:15:59,119 --> 04:16:01,640 handler. timesm called and then we'll 5911 04:16:01,640 --> 04:16:06,840 run our test again oops with- 5912 04:16:09,560 --> 04:16:13,040 VV and we can see oh mint is actually 5913 04:16:13,040 --> 04:16:16,119 never called how is that possible well 5914 04:16:16,119 --> 04:16:17,960 it must be because one of these returns 5915 04:16:17,960 --> 04:16:19,680 is hitting and it's not finishing this 5916 04:16:19,680 --> 04:16:22,040 call so we could keep moving this up to 5917 04:16:22,040 --> 04:16:24,000 figure out where it's actually being 5918 04:16:24,000 --> 04:16:27,199 called and to continue to debug this but 5919 04:16:27,199 --> 04:16:28,880 I already know how to debug this of 5920 04:16:28,880 --> 04:16:31,040 course because I've done this a while if 5921 04:16:31,040 --> 04:16:33,239 you want take this as a challenge for 5922 04:16:33,239 --> 04:16:37,239 you to answer the question why is this 5923 04:16:37,239 --> 04:16:40,199 never being called why is times Min 5924 04:16:40,199 --> 04:16:41,960 being called Never being called why is 5925 04:16:41,960 --> 04:16:43,439 it never finishing 5926 04:16:43,439 --> 04:16:45,359 so feel free to pause the video try to 5927 04:16:45,359 --> 04:16:47,800 debug this yourself and find out and 5928 04:16:47,800 --> 04:16:49,199 then I'll tell you the technique that I 5929 04:16:49,199 --> 04:16:51,640 used to debug this and figure out why 5930 04:16:51,640 --> 04:16:54,880 this mint DSC was never being 5931 04:16:56,319 --> 04:16:59,199 finished all right welcome back did you 5932 04:16:59,199 --> 04:17:00,520 actually pause the video did you 5933 04:17:00,520 --> 04:17:02,359 actually try to figure it out if you 5934 04:17:02,359 --> 04:17:04,040 didn't I'm giving you a second chance 5935 04:17:04,040 --> 04:17:06,319 pause the video go find out pause the 5936 04:17:06,319 --> 04:17:08,479 video and take this as your opportunity 5937 04:17:08,479 --> 04:17:12,040 to try to debug okay why is this not 5938 04:17:12,040 --> 04:17:16,040 getting hit why is this line not 5939 04:17:21,239 --> 04:17:24,119 hitting all right welcome back now there 5940 04:17:24,119 --> 04:17:25,479 was a couple different ways that I used 5941 04:17:25,479 --> 04:17:28,399 to actually debug this one of them was 5942 04:17:28,399 --> 04:17:30,920 having this times Min called plus equals 5943 04:17:30,920 --> 04:17:32,520 1 and then moving it up so I found the 5944 04:17:32,520 --> 04:17:35,159 line that it was breaking on once I 5945 04:17:35,159 --> 04:17:37,159 found the line that it was breaking on I 5946 04:17:37,159 --> 04:17:38,800 console. loged all the values of the 5947 04:17:38,800 --> 04:17:40,640 different variables around one of the 5948 04:17:40,640 --> 04:17:42,439 most important variables that I dumped 5949 04:17:42,439 --> 04:17:45,439 was going going to be the message. 5950 04:17:45,439 --> 04:17:47,560 sender so when you're working with 5951 04:17:47,560 --> 04:17:50,119 invariance remember it's going to call 5952 04:17:50,119 --> 04:17:52,960 this contract with a ton of different 5953 04:17:52,960 --> 04:17:56,119 functions and a ton of different calls 5954 04:17:56,119 --> 04:17:58,680 however it's also going to call them 5955 04:17:58,680 --> 04:17:59,800 with 5956 04:17:59,800 --> 04:18:03,760 random addresses as well so in order for 5957 04:18:03,760 --> 04:18:09,359 us to Mint DSC we need to only mint DSC 5958 04:18:09,359 --> 04:18:11,479 with an address that has actually 5959 04:18:11,479 --> 04:18:13,560 deposited collateral 5960 04:18:13,560 --> 04:18:14,960 because it's impossible for someone to 5961 04:18:14,960 --> 04:18:17,800 Mint DSC without them depositing 5962 04:18:17,800 --> 04:18:21,119 collateral now again if we restrict this 5963 04:18:21,119 --> 04:18:23,800 function like this maybe there is a case 5964 04:18:23,800 --> 04:18:25,479 where you can mint DSC without 5965 04:18:25,479 --> 04:18:26,960 depositing any collateral that we don't 5966 04:18:26,960 --> 04:18:29,000 know about this is again why it's 5967 04:18:29,000 --> 04:18:31,439 important to have some open invariant 5968 04:18:31,439 --> 04:18:33,840 tests some continue on revert and some 5969 04:18:33,840 --> 04:18:36,319 fail on revert as well so if we want to 5970 04:18:36,319 --> 04:18:38,680 have this be failon Reverb we would need 5971 04:18:38,680 --> 04:18:41,680 to only pick a message that sender that 5972 04:18:41,680 --> 04:18:44,359 has some deposit collateral so what we 5973 04:18:44,359 --> 04:18:45,880 can do is actually keep track of people 5974 04:18:45,880 --> 04:18:48,199 who have deposited collateral and then 5975 04:18:48,199 --> 04:18:50,000 when we go to Mint we just choose an 5976 04:18:50,000 --> 04:18:52,640 address from somebody who already has 5977 04:18:52,640 --> 04:18:55,439 deposited so how can we do that well we 5978 04:18:55,439 --> 04:18:58,359 can just keep track of an array of 5979 04:18:58,359 --> 04:18:59,880 addresses that 5980 04:18:59,880 --> 04:19:03,560 have lateral deposited so up at the top 5981 04:19:03,560 --> 04:19:05,080 well let's actually keep this times mint 5982 04:19:05,080 --> 04:19:07,279 as called we'll put it at the bottom 5983 04:19:07,279 --> 04:19:10,800 times M it's called Plus+ and this way 5984 04:19:10,800 --> 04:19:13,239 we know how to test this right if times 5985 04:19:13,239 --> 04:19:15,760 myth is called increases we've known we 5986 04:19:15,760 --> 04:19:18,080 will know that we fixed the actual issue 5987 04:19:18,080 --> 04:19:20,520 so times myth is called plus plus but 5988 04:19:20,520 --> 04:19:25,119 the top we'll do a address array public 5989 04:19:25,119 --> 04:19:29,080 users with collateral 5990 04:19:29,080 --> 04:19:32,000 deposited we'll copy this address and 5991 04:19:32,000 --> 04:19:34,640 now in deposit 5992 04:19:34,640 --> 04:19:36,600 collateral users with collateral 5993 04:19:36,600 --> 04:19:39,960 deposited. push message. sender now 5994 04:19:39,960 --> 04:19:42,199 there's some caveats here this will 5995 04:19:42,199 --> 04:19:44,319 double push obviously some people so 5996 04:19:44,319 --> 04:19:46,880 this will double push if the same 5997 04:19:46,880 --> 04:19:50,119 addresses push twice but for now let's 5998 04:19:50,119 --> 04:19:52,399 just keep it simple and let's go ahead 5999 04:19:52,399 --> 04:19:54,560 and just leave it like this we probably 6000 04:19:54,560 --> 04:19:56,319 should check to see if someone has 6001 04:19:56,319 --> 04:19:58,439 already deposited collateral but 6002 04:19:58,439 --> 04:19:59,720 whatever we're going to go with this for 6003 04:19:59,720 --> 04:20:02,560 now because simple and then now in our 6004 04:20:02,560 --> 04:20:05,080 mint DSC we can do something similar to 6005 04:20:05,080 --> 04:20:07,319 what we did with collateral so we'll do 6006 04:20:07,319 --> 04:20:10,159 a underscore U into 6007 04:20:10,159 --> 04:20:13,279 256 address seed 6008 04:20:13,279 --> 04:20:15,080 and instead of bounding to message. 6009 04:20:15,080 --> 04:20:18,560 Sender what we can do is we can say 6010 04:20:18,560 --> 04:20:23,080 address sender equals users with 6011 04:20:23,080 --> 04:20:25,399 collateral deposited it's going to be 6012 04:20:25,399 --> 04:20:27,720 the index of the users with collateral 6013 04:20:27,720 --> 04:20:31,960 deposited Andress seed mod uses with 6014 04:20:31,960 --> 04:20:33,960 collateral 6015 04:20:33,960 --> 04:20:37,640 deposited. length and now now instead of 6016 04:20:37,640 --> 04:20:39,159 message. sender we're going to use 6017 04:20:39,159 --> 04:20:41,439 sender in here same thing with down here 6018 04:20:41,439 --> 04:20:43,600 we're going to use send 6019 04:20:43,600 --> 04:20:45,840 now let's run this and see if mint is 6020 04:20:45,840 --> 04:20:48,359 ever actually 6021 04:20:51,199 --> 04:20:54,920 called aha so this was very helpful redo 6022 04:20:54,920 --> 04:20:58,800 it let's do- VV 6023 04:21:00,720 --> 04:21:03,119 VV ah okay so at least we're getting 6024 04:21:03,119 --> 04:21:04,279 something different here right we're 6025 04:21:04,279 --> 04:21:06,880 getting an error division or module by 6026 04:21:06,880 --> 04:21:09,000 zero ah okay of course we're getting 6027 04:21:09,000 --> 04:21:11,600 modu by zero because if the collateral 6028 04:21:11,600 --> 04:21:13,319 length is zero then obviously sender is 6029 04:21:13,319 --> 04:21:16,600 going to be zero so we can do if uses 6030 04:21:16,600 --> 04:21:19,319 with collateral. length equals equals z 6031 04:21:19,319 --> 04:21:20,520 then we're going to return we're going 6032 04:21:20,520 --> 04:21:24,720 to skip this one so let's run this 6033 04:21:27,319 --> 04:21:30,359 again okay so we're getting some stuff 6034 04:21:30,359 --> 04:21:33,720 passing let's actually just do a 2vs so 6035 04:21:33,720 --> 04:21:36,040 that it's easier to read than all those 6036 04:21:36,040 --> 04:21:39,040 events 6037 04:21:42,840 --> 04:21:45,640 aha total times mint was called is now 6038 04:21:45,640 --> 04:21:48,520 31 and we're getting a total Supply so 6039 04:21:48,520 --> 04:21:51,000 our mint DSC function in our Handler is 6040 04:21:51,000 --> 04:21:52,600 now actually working we're now 6041 04:21:52,600 --> 04:21:55,600 successfully calling mint DSC and it 6042 04:21:55,600 --> 04:21:58,760 looks like our protocol is holding up 6043 04:21:58,760 --> 04:22:02,000 all right fantastic so we're getting 6044 04:22:02,000 --> 04:22:04,199 closer to building this Handler to 6045 04:22:04,199 --> 04:22:07,520 actually have a solid Recreation of all 6046 04:22:07,520 --> 04:22:09,040 the possible functions we can actually 6047 04:22:09,040 --> 04:22:10,680 do in this system something I didn't 6048 04:22:10,680 --> 04:22:12,760 show you was we should pretty much much 6049 04:22:12,760 --> 04:22:15,399 always use a given invariant called 6050 04:22:15,399 --> 04:22:20,199 function invariant Getters should not 6051 04:22:20,199 --> 04:22:23,199 revert like this and we just put in here 6052 04:22:23,199 --> 04:22:26,800 all of our invariance like dsce do get 6053 04:22:26,800 --> 04:22:32,040 liquidation bonus dce do get Precision 6054 04:22:32,040 --> 04:22:35,199 Etc put all of our Getters in here and 6055 04:22:35,199 --> 04:22:37,520 oh and this could be public View and if 6056 04:22:37,520 --> 04:22:40,000 any of these revert then this will fail 6057 04:22:40,000 --> 04:22:42,920 this invariant test will call a ton of 6058 04:22:42,920 --> 04:22:45,640 different functions on the Handler and 6059 04:22:45,640 --> 04:22:47,840 if any of the function combinations 6060 04:22:47,840 --> 04:22:49,520 break any of our Getters we know we 6061 04:22:49,520 --> 04:22:51,600 broke an invariant this is a layup and 6062 04:22:51,600 --> 04:22:53,840 variant that everyone should always 100% 6063 04:22:53,840 --> 04:22:56,640 include a way to make sure that you're 6064 04:22:56,640 --> 04:22:57,960 including everything is you can run 6065 04:22:57,960 --> 04:23:02,479 something called Forge inspect DSC 6066 04:23:02,479 --> 04:23:06,159 engine methods and it'll print out all 6067 04:23:06,159 --> 04:23:09,560 the different methods that this function 6068 04:23:09,560 --> 04:23:12,560 has in addition to its function 6069 04:23:12,560 --> 04:23:14,600 collectors so you can kind of use this 6070 04:23:14,600 --> 04:23:15,960 as your checklist of all the different 6071 04:23:15,960 --> 04:23:19,239 functions you can call on a contract and 6072 04:23:19,239 --> 04:23:21,040 you can look for all the view functions 6073 04:23:21,040 --> 04:23:23,319 in here this is additionally why it's 6074 04:23:23,319 --> 04:23:25,119 great to have get in front of these 6075 04:23:25,119 --> 04:23:27,479 words because becomes very easy to 6076 04:23:27,479 --> 04:23:30,479 figure out which ones are 6077 04:23:32,319 --> 04:23:34,600 Getters but it doesn't reflect the whole 6078 04:23:34,600 --> 04:23:37,479 world right one of the other really 6079 04:23:37,479 --> 04:23:38,680 fantastic things we can do with the 6080 04:23:38,680 --> 04:23:41,800 Handler is we can both handle our DSC 6081 04:23:41,800 --> 04:23:43,760 engine but any other contract that we 6082 04:23:43,760 --> 04:23:47,840 want to simulate for as well and there's 6083 04:23:47,840 --> 04:23:49,680 a lot of things we want to take in mind 6084 04:23:49,680 --> 04:23:52,119 when writing these especially the other 6085 04:23:52,119 --> 04:23:54,239 contracts that we interact with what are 6086 04:23:54,239 --> 04:23:55,520 some of the other contracts that we 6087 04:23:55,520 --> 04:23:57,399 interact with well one of them is going 6088 04:23:57,399 --> 04:23:59,199 to be the price feed one of them is 6089 04:23:59,199 --> 04:24:02,119 going to be the we token the wrapped 6090 04:24:02,119 --> 04:24:04,520 Bitcoin token so our Handler should 6091 04:24:04,520 --> 04:24:07,279 probably also show people doing random 6092 04:24:07,279 --> 04:24:09,439 weird things with we and rap Bitcoin 6093 04:24:09,439 --> 04:24:10,600 right because people are going to do 6094 04:24:10,600 --> 04:24:12,040 random weird things with both of these 6095 04:24:12,040 --> 04:24:13,359 tokens and we want to make sure our 6096 04:24:13,359 --> 04:24:16,080 system can work with them appropriately 6097 04:24:16,080 --> 04:24:17,439 now I'm actually going to skip them for 6098 04:24:17,439 --> 04:24:20,040 now but I am 100% going to do one with 6099 04:24:20,040 --> 04:24:23,000 price feeds because price feeds are 6100 04:24:23,000 --> 04:24:25,640 definitely a system that can change and 6101 04:24:25,640 --> 04:24:27,479 definitely a system that greatly affects 6102 04:24:27,479 --> 04:24:30,239 our protocol so we're going to include 6103 04:24:30,239 --> 04:24:33,279 price feed updates in our Handler so 6104 04:24:33,279 --> 04:24:34,159 what we're going to do is we're going to 6105 04:24:34,159 --> 04:24:37,800 go ahead and do import a mock V3 6106 04:24:37,800 --> 04:24:42,760 aggregator from slash do do slash mock 6107 04:24:42,760 --> 04:24:47,960 SL mock V3 agregator doso or where is 6108 04:24:47,960 --> 04:24:52,479 this located oh just one this okay and 6109 04:24:52,479 --> 04:24:55,199 this mock V3 aggregator has some 6110 04:24:55,199 --> 04:24:56,640 functions that allow us to just easily 6111 04:24:56,640 --> 04:24:59,279 update and answer right which is what 6112 04:24:59,279 --> 04:25:01,319 something that we want to do we want our 6113 04:25:01,319 --> 04:25:03,199 protocol to be able to easily update 6114 04:25:03,199 --> 04:25:05,880 answers so we'll take this mock V3 6115 04:25:05,880 --> 04:25:08,680 aggregator and let's at least get the we 6116 04:25:08,680 --> 04:25:13,040 price from our system so so I have a 6117 04:25:13,040 --> 04:25:16,760 view function dsce doget collateral 6118 04:25:16,760 --> 04:25:19,479 token price feed and I'll pass the 6119 04:25:19,479 --> 04:25:23,560 address we 6120 04:25:25,239 --> 04:25:28,920 oops and I'll say eth and I'll make 6121 04:25:28,920 --> 04:25:33,720 another variable mock V3 agor public eth 6122 04:25:33,720 --> 04:25:38,279 USD price feed I'll say eth USD price 6123 04:25:38,279 --> 04:25:41,399 feed equals this and we just got to wrap 6124 04:25:41,399 --> 04:25:43,640 this up as a Mo V3 6125 04:25:43,640 --> 04:25:45,960 aggregator this and great now we have an 6126 04:25:45,960 --> 04:25:48,960 ethusd price feed and now we can add a 6127 04:25:48,960 --> 04:25:51,880 new function in here so we have mint DSC 6128 04:25:51,880 --> 04:25:55,119 deposit collateral redeem collateral we 6129 04:25:55,119 --> 04:25:59,680 can add a new one called function update 6130 04:25:59,680 --> 04:26:04,000 collateral price we'll do a uent 96 just 6131 04:26:04,000 --> 04:26:05,840 so that the number isn't too big new 6132 04:26:05,840 --> 04:26:08,239 price and then we could also randomize 6133 04:26:08,239 --> 04:26:10,319 the collateral but for now we'll just 6134 04:26:10,319 --> 04:26:13,040 have it be the ethusd so 6135 04:26:13,040 --> 04:26:18,479 we need to convert EU 96 to an INT 256 6136 04:26:18,479 --> 04:26:22,760 new price int equals in 256 new price 6137 04:26:22,760 --> 04:26:26,600 because price feeds take in 256s oop 6138 04:26:26,600 --> 04:26:30,319 sorry this should be in and to convert a 6139 04:26:30,319 --> 04:26:33,199 u 96 to an INT 256 we actually have to 6140 04:26:33,199 --> 04:26:39,239 wrap it as u in 256 first and then to n 6141 04:26:40,920 --> 04:26:45,199 96 do update answer or set price or 6142 04:26:45,199 --> 04:26:48,399 whatever we want to do to this new price 6143 04:26:48,399 --> 04:26:51,560 like this boom and now simple as that we 6144 04:26:51,560 --> 04:26:53,800 have an update collateral price well an 6145 04:26:53,800 --> 04:26:56,439 update eth price anyways so now we can 6146 04:26:56,439 --> 04:26:58,399 do three things in our system we can 6147 04:26:58,399 --> 04:27:01,040 update the price redeem collateral 6148 04:27:01,040 --> 04:27:05,199 deposit collateral and mint DSC so 6149 04:27:05,199 --> 04:27:07,159 before we actually run this what do you 6150 04:27:07,159 --> 04:27:09,880 think do you think we'll get n what do 6151 04:27:09,880 --> 04:27:13,960 you think we'll go on let's run this 6152 04:27:19,720 --> 04:27:22,279 oh it looks like it found a sequence it 6153 04:27:22,279 --> 04:27:24,640 found an issue the reason assertion 6154 04:27:24,640 --> 04:27:27,600 violated which means that our invariant 6155 04:27:27,600 --> 04:27:30,080 here was broken the we Value Plus 6156 04:27:30,080 --> 04:27:32,479 Bitcoin value now is no longer the total 6157 04:27:32,479 --> 04:27:34,720 Supply so let's scroll up let's see what 6158 04:27:34,720 --> 04:27:37,159 the issue is here all the way past 6159 04:27:37,159 --> 04:27:39,439 everything and if we read the sequence 6160 04:27:39,439 --> 04:27:42,239 we can figure out why this broke we see 6161 04:27:42,239 --> 04:27:43,840 exactly what happened okay so first it 6162 04:27:43,840 --> 04:27:46,920 called deposit collateral Okay cool so 6163 04:27:46,920 --> 04:27:50,359 we deposited some collateral and then we 6164 04:27:50,359 --> 04:27:53,479 minted some DSC okay cool we minted some 6165 04:27:53,479 --> 04:27:57,279 DSC with some stuff here and then we 6166 04:27:57,279 --> 04:28:00,319 updated the collateral price to 6167 04:28:00,319 --> 04:28:03,800 471 so as we know our Handler update 6168 04:28:03,800 --> 04:28:05,359 collateral price if we scroll the way to 6169 04:28:05,359 --> 04:28:07,920 the bottom update collateral 6170 04:28:07,920 --> 04:28:09,560 price 6171 04:28:09,560 --> 04:28:13,600 471 updates the eth collateral from 6172 04:28:13,600 --> 04:28:16,359 $2,000 which is 6173 04:28:16,359 --> 04:28:18,680 2008 6174 04:28:18,680 --> 04:28:20,199 to 6175 04:28:20,199 --> 04:28:24,199 471 so this remember is 2 1 2 3 4 5 6 7 6176 04:28:24,199 --> 04:28:28,600 8 it went from this to 471 so of course 6177 04:28:28,600 --> 04:28:31,479 it reverted right because people minted 6178 04:28:31,479 --> 04:28:33,399 a ton of collateral they deposited 6179 04:28:33,399 --> 04:28:36,560 collateral they minted a ton of DSC 6180 04:28:36,560 --> 04:28:38,800 right look at this input it's massive 6181 04:28:38,800 --> 04:28:41,439 and the system broke and if we run this 6182 04:28:41,439 --> 04:28:45,239 again with fewer V's right VV we'll be 6183 04:28:45,239 --> 04:28:47,880 able to see that total Supply we scroll 6184 04:28:47,880 --> 04:28:50,080 up we'll be able to see the we value rep 6185 04:28:50,080 --> 04:28:53,000 Bitcoin value and the total Supply here 6186 04:28:53,000 --> 04:28:54,560 so actually it looks like in this one it 6187 04:28:54,560 --> 04:28:58,119 set the new price to three so obviously 6188 04:28:58,119 --> 04:29:00,040 the we value is probably zero or just 6189 04:29:00,040 --> 04:29:02,760 about zero it let them mint DC because 6190 04:29:02,760 --> 04:29:04,680 originally the collateral was were was 6191 04:29:04,680 --> 04:29:06,080 worth something and now it's worth 6192 04:29:06,080 --> 04:29:09,880 almost nothing right so this is an 6193 04:29:09,880 --> 04:29:12,359 important thing for us to take in mind 6194 04:29:12,359 --> 04:29:15,840 in our system hey if the price drops or 6195 04:29:15,840 --> 04:29:19,720 spikes quickly our system is screwed our 6196 04:29:19,720 --> 04:29:21,840 system is busted and this is something 6197 04:29:21,840 --> 04:29:23,159 we would want to know about and 6198 04:29:23,159 --> 04:29:25,119 potentially go back in our code and fix 6199 04:29:25,119 --> 04:29:28,080 hey what do we do when the price 6200 04:29:28,080 --> 04:29:31,159 plummets in a single block right now we 6201 04:29:31,159 --> 04:29:33,600 have kind of this assumption in here 6202 04:29:33,600 --> 04:29:37,600 where we have this liquidation 10% bonus 6203 04:29:37,600 --> 04:29:39,800 and the collateral always needs to be 6204 04:29:39,800 --> 04:29:42,720 200% over collateralized with this we're 6205 04:29:42,720 --> 04:29:45,640 saying okay between 200% 6206 04:29:45,640 --> 04:29:48,479 overcollateralization and 110% 6207 04:29:48,479 --> 04:29:49,439 overcollateralization 6208 04:29:49,439 --> 04:29:52,040 as long as our system is within this 6209 04:29:52,040 --> 04:29:53,960 it's still safe I mean obviously it's 6210 04:29:53,960 --> 04:29:55,600 better if it's above this but as long as 6211 04:29:55,600 --> 04:29:57,159 our system is within this it's still 6212 04:29:57,159 --> 04:29:59,920 safe but if the price plummets of some 6213 04:29:59,920 --> 04:30:01,239 collateral and let's say that's the only 6214 04:30:01,239 --> 04:30:03,119 collateral maybe we get to 50% 6215 04:30:03,119 --> 04:30:05,000 collateralization rate and that would 6216 04:30:05,000 --> 04:30:06,840 break our entire system right that would 6217 04:30:06,840 --> 04:30:09,600 break our invariant and our system will 6218 04:30:09,600 --> 04:30:11,640 be screwed so we go back to the drawing 6219 04:30:11,640 --> 04:30:14,439 board figure out how to smooth this out 6220 04:30:14,439 --> 04:30:17,159 or we can say this is a known bug if the 6221 04:30:17,159 --> 04:30:19,279 price fluctuates or explodes too quickly 6222 04:30:19,279 --> 04:30:21,640 or too slowly this protocol becomes 6223 04:30:21,640 --> 04:30:23,600 worthless and that's kind of not a great 6224 04:30:23,600 --> 04:30:25,399 solution right so these are things we 6225 04:30:25,399 --> 04:30:26,640 absolutely want to keep in mind and 6226 04:30:26,640 --> 04:30:29,239 these are things that we can find with 6227 04:30:29,239 --> 04:30:31,560 invariant tests this is why they're so 6228 04:30:31,560 --> 04:30:34,560 important so for now in our Handler I'm 6229 04:30:34,560 --> 04:30:35,880 actually just going to even comment this 6230 04:30:35,880 --> 04:30:38,640 out because it does break our test Suite 6231 04:30:38,640 --> 04:30:39,800 but I'm going to put a little comment 6232 04:30:39,800 --> 04:30:42,439 here this breaks our 6233 04:30:42,439 --> 04:30:45,600 invariant test suite and this would 100% 6234 04:30:45,600 --> 04:30:47,600 be something that shows up in a smart 6235 04:30:47,600 --> 04:30:50,119 Contra audit saying hey if the price of 6236 04:30:50,119 --> 04:30:52,239 an asset plummets too quickly the 6237 04:30:52,239 --> 04:30:54,800 system's breaking because it breaks the 6238 04:30:54,800 --> 04:30:57,359 invariant all right great now there's a 6239 04:30:57,359 --> 04:30:59,640 few more things I want to teach only a 6240 04:30:59,640 --> 04:31:00,840 few and then we're done with this 6241 04:31:00,840 --> 04:31:03,720 section we're going to teach one some 6242 04:31:03,720 --> 04:31:07,840 proper Oracle use and then two we need 6243 04:31:07,840 --> 04:31:10,319 to write more tests which we're not 6244 04:31:10,319 --> 04:31:11,560 going to do I'm going to leave that to 6245 04:31:11,560 --> 04:31:13,359 you but we have a whole bunch of other 6246 04:31:13,359 --> 04:31:15,159 contracts in here like the decentralized 6247 04:31:15,159 --> 04:31:18,159 stable coin and then three some smart 6248 04:31:18,159 --> 04:31:22,159 contract audit preparedness some smart 6249 04:31:22,159 --> 04:31:24,680 contract audit preparation so let's 6250 04:31:24,680 --> 04:31:28,840 start with some Oracle proper 6251 04:31:30,319 --> 04:31:33,399 use so in our DSC engine we're of course 6252 04:31:33,399 --> 04:31:36,040 using an oracle right we're using chain 6253 04:31:36,040 --> 04:31:38,640 link price feeds now this is kind of an 6254 04:31:38,640 --> 04:31:41,040 assumption that we have in our protocol 6255 04:31:41,040 --> 04:31:43,359 right now he price feeds are just going 6256 04:31:43,359 --> 04:31:45,600 to work but price feeds are a system 6257 04:31:45,600 --> 04:31:47,520 just like anything else and we should 6258 04:31:47,520 --> 04:31:51,000 add some checks in our code here just to 6259 04:31:51,000 --> 04:31:53,920 make sure that if this breaks or if 6260 04:31:53,920 --> 04:31:56,359 something in here breaks our system 6261 04:31:56,359 --> 04:31:58,720 isn't broken so what we're going to do 6262 04:31:58,720 --> 04:32:00,159 is we're actually use that Library 6263 04:32:00,159 --> 04:32:03,000 methodology we made years ago to write 6264 04:32:03,000 --> 04:32:05,199 some checks on this price feed so I'm 6265 04:32:05,199 --> 04:32:07,239 going to make 6266 04:32:07,239 --> 04:32:09,920 a libraries folder and we're going to 6267 04:32:09,920 --> 04:32:13,840 make a new contract in here called 6268 04:32:14,840 --> 04:32:17,359 oracle. and what we want to do is we 6269 04:32:17,359 --> 04:32:18,920 want to check to make sure that these 6270 04:32:18,920 --> 04:32:21,279 prices aren't stale if we click on any 6271 04:32:21,279 --> 04:32:24,920 one of these prices like eth USD let's 6272 04:32:24,920 --> 04:32:27,359 scroll up to show more 6273 04:32:27,359 --> 04:32:30,600 details more details you can see they 6274 04:32:30,600 --> 04:32:33,239 have this heartbeat where a new price 6275 04:32:33,239 --> 04:32:35,560 should show up at least every 3600 6276 04:32:35,560 --> 04:32:38,399 seconds I believe what this is right yes 6277 04:32:38,399 --> 04:32:40,479 on theoa testnet we want to write some 6278 04:32:40,479 --> 04:32:42,560 checks to make sure that this is 6279 04:32:42,560 --> 04:32:44,760 actually updating every 3600 seconds and 6280 04:32:44,760 --> 04:32:46,600 if it's not we should probably pause the 6281 04:32:46,600 --> 04:32:49,520 functionality of our contract so we're 6282 04:32:49,520 --> 04:32:51,319 going to make a 6283 04:32:51,319 --> 04:32:52,840 spdx 6284 04:32:52,840 --> 04:32:56,960 license identifier MIT pragma solidity 6285 04:32:56,960 --> 04:33:00,239 like this we're going to do a library 6286 04:33:00,239 --> 04:33:02,680 Oracle lib and let's put a little natat 6287 04:33:02,680 --> 04:33:04,039 speec to explain what this is going to 6288 04:33:04,039 --> 04:33:08,400 do say add title Patrick Collins oops 6289 04:33:08,400 --> 04:33:12,439 title Oracle lib at author 6290 04:33:12,439 --> 04:33:14,879 Thor Patrick 6291 04:33:14,879 --> 04:33:20,520 Collins at notice this library is used 6292 04:33:20,520 --> 04:33:23,000 to check the chain 6293 04:33:23,000 --> 04:33:26,000 link Oracle for 6294 04:33:26,000 --> 04:33:31,279 stale if a price is stale function will 6295 04:33:31,279 --> 04:33:37,199 revert and render the dce engine 6296 04:33:37,199 --> 04:33:40,320 unusable this is by Design so we're 6297 04:33:40,320 --> 04:33:42,400 going to say hey if a CH price feed to 6298 04:33:42,400 --> 04:33:45,320 stale just stop don't let anything 6299 04:33:45,320 --> 04:33:47,320 happen because if a price is wrong if a 6300 04:33:47,320 --> 04:33:49,760 price is bad our whole protocol is kind 6301 04:33:49,760 --> 04:33:51,119 of bunked right so we want to just 6302 04:33:51,119 --> 04:33:55,039 freeze everything so we want the DSC 6303 04:33:55,039 --> 04:34:01,039 engine to freeze if prices become stale 6304 04:34:01,039 --> 04:34:06,199 so if the chain link Network explodes 6305 04:34:06,199 --> 04:34:09,639 and you have a lot of money locked in 6306 04:34:09,639 --> 04:34:12,600 the protocol too bad this is something 6307 04:34:12,600 --> 04:34:13,840 that's going to be a known issue right 6308 04:34:13,840 --> 04:34:15,520 if the chain link Network blows up and 6309 04:34:15,520 --> 04:34:18,160 all the prices become stale H you're 6310 04:34:18,160 --> 04:34:19,561 kind of screwed right and maybe this is 6311 04:34:19,561 --> 04:34:22,719 something we want to account for but for 6312 04:34:22,719 --> 04:34:24,080 now I'm just going to say that's a known 6313 04:34:24,080 --> 04:34:25,959 issue and we're going to move on and 6314 04:34:25,959 --> 04:34:27,359 this is where you'll see me start to get 6315 04:34:27,359 --> 04:34:30,680 more and more par particular about stuff 6316 04:34:30,680 --> 04:34:32,439 this is where as we get more and more 6317 04:34:32,439 --> 04:34:34,320 advanced this is where the details start 6318 04:34:34,320 --> 04:34:36,160 to matter more and more right all those 6319 04:34:36,160 --> 04:34:37,600 little little things that I kind of 6320 04:34:37,600 --> 04:34:39,520 gloss over they become to they start to 6321 04:34:39,520 --> 04:34:41,400 become more and more important as this 6322 04:34:41,400 --> 04:34:44,199 Bec comes closer and closer to a real 6323 04:34:44,199 --> 04:34:46,359 production product that should go to 6324 04:34:46,359 --> 04:34:50,000 audit right so let's create a stale 6325 04:34:50,000 --> 04:34:51,760 price check function so we'll create a 6326 04:34:51,760 --> 04:34:56,199 function stale price check and we'll 6327 04:34:56,199 --> 04:34:59,799 have this stale price check beond an 6328 04:34:59,799 --> 04:35:02,119 aggregator V3 interface. Soul so I'm 6329 04:35:02,119 --> 04:35:04,359 actually going to copy this paste it in 6330 04:35:04,359 --> 04:35:07,320 here toggle the word wrap and so as an 6331 04:35:07,320 --> 04:35:08,840 input parameter it's going to take 6332 04:35:08,840 --> 04:35:12,199 aggregated V3 interface price feed 6333 04:35:12,199 --> 04:35:14,879 this will be a public view which will 6334 04:35:14,879 --> 04:35:18,959 returns a uint 80 in 6335 04:35:18,959 --> 04:35:26,760 256 U 256 U 256 and a uint 80 the same 6336 04:35:26,760 --> 04:35:29,719 return value of the latest round data 6337 04:35:29,719 --> 04:35:32,119 function in an aggregator V3 interface 6338 04:35:32,119 --> 04:35:37,039 function like this okay cool and in here 6339 04:35:37,039 --> 04:35:38,639 what we're going to do is we're going to 6340 04:35:38,639 --> 04:35:45,320 call Price feed Dot latest latest round 6341 04:35:45,320 --> 04:35:47,359 data and I'm even just going to cheat a 6342 04:35:47,359 --> 04:35:49,600 little bit we're going to control click 6343 04:35:49,600 --> 04:35:52,799 into this we're going to copy this line 6344 04:35:52,799 --> 04:35:55,080 paste it here 6345 04:35:55,080 --> 04:35:57,320 equals boom just so I don't have to type 6346 04:35:57,320 --> 04:36:00,600 as much cool we have all those and what 6347 04:36:00,600 --> 04:36:02,561 we're going to say in here and we're 6348 04:36:02,561 --> 04:36:04,359 probably not even going to use all these 6349 04:36:04,359 --> 04:36:06,320 we're going to have some stale check 6350 04:36:06,320 --> 04:36:07,959 right so each one of these price feets 6351 04:36:07,959 --> 04:36:09,561 has their own heartbeat so we probably 6352 04:36:09,561 --> 04:36:11,920 should ask them what their heartbeat is 6353 04:36:11,920 --> 04:36:13,199 but I'm just going to hard codee it for 6354 04:36:13,199 --> 04:36:18,561 this one I'm going to say un 256 private 6355 04:36:18,561 --> 04:36:22,680 constant timeout equals 3 hours and this 6356 04:36:22,680 --> 04:36:26,119 is a constant in solidity it stands for 6357 04:36:26,119 --> 04:36:30,959 3 * 60 Minutes time 60 seconds 6358 04:36:30,959 --> 04:36:34,359 equals uh this many seconds so looks 6359 04:36:34,359 --> 04:36:35,799 like this heartbeat is actually much 6360 04:36:35,799 --> 04:36:37,400 longer than the one the chain link 6361 04:36:37,400 --> 04:36:41,199 should allow so 360 seconds is 3,600 6362 04:36:41,199 --> 04:36:43,000 seconds is just 1 hour right we're going 6363 04:36:43,000 --> 04:36:44,799 to give it 3 hours so what we're going 6364 04:36:44,799 --> 04:36:46,320 to say in here is we're going to say 6365 04:36:46,320 --> 04:36:48,600 first we're going to do a un6 seconds 6366 04:36:48,600 --> 04:36:52,561 since equals block. timestamp and then 6367 04:36:52,561 --> 04:36:55,799 we'll have uh excuse me 6368 04:36:55,799 --> 04:36:58,000 minus updated 6369 04:36:58,000 --> 04:37:01,240 app and then we'll say so this will get 6370 04:37:01,240 --> 04:37:03,719 the current block timestamp minus this 6371 04:37:03,719 --> 04:37:05,959 updated app so this should basically get 6372 04:37:05,959 --> 04:37:08,760 us the seconds since this price feed was 6373 04:37:08,760 --> 04:37:13,000 updated then we'll say if second since 6374 04:37:13,000 --> 04:37:15,400 it's greater than our timeout then we're 6375 04:37:15,400 --> 04:37:17,959 going to revert with a new 6376 04:37:17,959 --> 04:37:19,879 error 6377 04:37:19,879 --> 04:37:22,039 error or 6378 04:37:22,039 --> 04:37:26,760 cbor stale price this revert with stale 6379 04:37:26,760 --> 04:37:28,240 price and then we're just going to 6380 04:37:28,240 --> 04:37:32,520 return all of this stuff so return round 6381 04:37:32,520 --> 04:37:37,359 ID answer started at updated at answer 6382 04:37:37,359 --> 04:37:38,799 around and I'm going to change this name 6383 04:37:38,799 --> 04:37:45,359 to stale check latest round data now 6384 04:37:45,359 --> 04:37:48,240 what we can do since this is a library 6385 04:37:48,240 --> 04:37:51,160 on our price feed we can use this stale 6386 04:37:51,160 --> 04:37:53,359 check latest round data to automatically 6387 04:37:53,359 --> 04:37:57,959 check to see if the price is stale so 6388 04:37:57,959 --> 04:38:01,760 now in our DSC engine anytime we call 6389 04:38:01,760 --> 04:38:05,000 latest round data we just swap it out 6390 04:38:05,000 --> 04:38:07,799 for stale check latest round data so 6391 04:38:07,799 --> 04:38:10,959 long as at the top we go after our 6392 04:38:10,959 --> 04:38:13,760 errors we're going to put our types 6393 04:38:13,760 --> 04:38:16,039 after the errors we're going to put 6394 04:38:16,039 --> 04:38:19,561 types so this is where we would do using 6395 04:38:19,561 --> 04:38:22,199 Oracle lib or 6396 04:38:22,199 --> 04:38:24,320 aggregator B3 6397 04:38:24,320 --> 04:38:27,279 interface we need to import Oracle lib 6398 04:38:27,279 --> 04:38:31,561 let import Oracle lib from there's going 6399 04:38:31,561 --> 04:38:35,080 to be libraries 6400 04:38:35,080 --> 04:38:39,959 libraries Oracle li. so like this and 6401 04:38:39,959 --> 04:38:41,799 now yep any place we use latest round 6402 04:38:41,799 --> 04:38:44,039 data we can now use stale check latest 6403 04:38:44,039 --> 04:38:45,639 round data where we have this stale 6404 04:38:45,639 --> 04:38:48,879 check baked in and cool now we did a ton 6405 04:38:48,879 --> 04:38:51,199 of refactoring let's run Forge test just 6406 04:38:51,199 --> 04:38:53,279 run this whole test Suite including the 6407 04:38:53,279 --> 04:38:55,879 invariant test 6408 04:39:03,439 --> 04:39:06,000 Suite okay and stuff's looking good here 6409 04:39:06,000 --> 04:39:07,760 and you can see that it even took a lot 6410 04:39:07,760 --> 04:39:10,879 of extra time to run this last bit so 6411 04:39:10,879 --> 04:39:14,199 cool we've got a little check here great 6412 04:39:14,199 --> 04:39:16,240 we're not going to write some more tests 6413 04:39:16,240 --> 04:39:17,920 this is something that you should 100% 6414 04:39:17,920 --> 04:39:20,879 do we pull up our terminal here we run 6415 04:39:20,879 --> 04:39:25,439 Forge coverage what do you think we 6416 04:39:30,160 --> 04:39:32,959 get get this which you can see there's a 6417 04:39:32,959 --> 04:39:34,359 whole bunch of contracts that we need to 6418 04:39:34,359 --> 04:39:37,199 test this Oracle lib could probably use 6419 04:39:37,199 --> 04:39:39,200 its own test Suite even though it's 6420 04:39:39,200 --> 04:39:40,958 looking like a lot of it's tested we 6421 04:39:40,958 --> 04:39:42,160 probably should definitely test this 6422 04:39:42,160 --> 04:39:44,958 ourselves we need to write test for this 6423 04:39:44,958 --> 04:39:48,680 we probably want to test our uh our DSC 6424 04:39:48,680 --> 04:39:50,878 some more for sure so we should 6425 04:39:50,878 --> 04:39:52,320 definitely write some more tests I'm 6426 04:39:52,320 --> 04:39:54,958 going to leave that to you this little 6427 04:39:54,958 --> 04:39:57,520 little finger 6428 04:40:02,560 --> 04:40:04,680 here and then finally some smart 6429 04:40:04,680 --> 04:40:06,798 contract audit preparation so we talked 6430 04:40:06,798 --> 04:40:08,600 a little bit about what a smart contract 6431 04:40:08,600 --> 04:40:11,600 audit is and we haven't covered a whole 6432 04:40:11,600 --> 04:40:12,958 lot of security stuff yet we're going to 6433 04:40:12,958 --> 04:40:15,480 do that later in the course but a solid 6434 04:40:15,480 --> 04:40:16,760 place you can look is this audit 6435 04:40:16,760 --> 04:40:20,360 Readiness checklist from the nent XYZ 6436 04:40:20,360 --> 04:40:23,040 GitHub repo which has a lot of different 6437 04:40:23,040 --> 04:40:25,520 things that you should and keep in mind 6438 04:40:25,520 --> 04:40:28,000 when running your tests for those of you 6439 04:40:28,000 --> 04:40:30,360 looking to be really serious about 6440 04:40:30,360 --> 04:40:32,120 actually launching a protocol and really 6441 04:40:32,120 --> 04:40:33,798 having the security mindset that you 6442 04:40:33,798 --> 04:40:38,520 need as well be sure to get to this last 6443 04:40:38,520 --> 04:40:41,360 section in the course about intro to 6444 04:40:41,360 --> 04:40:43,080 security because this is where we're 6445 04:40:43,080 --> 04:40:45,280 going to give you a lot of that lower 6446 04:40:45,280 --> 04:40:48,080 level security stuff at least from a 6447 04:40:48,080 --> 04:40:50,040 smart contract developer perspective 6448 04:40:50,040 --> 04:40:51,400 we're going to give you all the basics 6449 04:40:51,400 --> 04:40:53,600 that you need to be aware of in order to 6450 04:40:53,600 --> 04:40:55,480 stay secure so we're not going to talk 6451 04:40:55,480 --> 04:40:58,200 about it too much in this one but it is 6452 04:40:58,200 --> 04:40:59,400 something that if we were to actually 6453 04:40:59,400 --> 04:41:01,120 launch this we would need to keep in 6454 04:41:01,120 --> 04:41:03,560 mind so I'll put a little soon Emoji 6455 04:41:03,560 --> 04:41:07,080 here for coming very 6456 04:41:08,680 --> 04:41:12,080 soon all right so with that all being 6457 04:41:12,080 --> 04:41:15,878 said we've done an absolute ton here 6458 04:41:15,878 --> 04:41:17,120 this is 6459 04:41:17,120 --> 04:41:19,760 1,000% a project you should push up to 6460 04:41:19,760 --> 04:41:23,680 your GitHub repo and this is 1,00% a 6461 04:41:23,680 --> 04:41:25,718 project that if you made it this far you 6462 04:41:25,718 --> 04:41:28,560 should be incredibly proud of yourself 6463 04:41:28,560 --> 04:41:31,280 this is the hardest most complicated 6464 04:41:31,280 --> 04:41:35,440 most advanced project in this course and 6465 04:41:35,440 --> 04:41:37,000 to be honest probably the most advanced 6466 04:41:37,000 --> 04:41:38,878 project you'll work on in almost all of 6467 04:41:38,878 --> 04:41:41,718 web 3 there's so much going on here we 6468 04:41:41,718 --> 04:41:44,160 learned about defi we learned about 6469 04:41:44,160 --> 04:41:45,600 advanced 6470 04:41:45,600 --> 04:41:47,958 state-of-the-art modern fuzzing 6471 04:41:47,958 --> 04:41:50,560 techniques we learned a tiny tiny bit 6472 04:41:50,560 --> 04:41:53,160 about security we used Oracles in a 6473 04:41:53,160 --> 04:41:55,798 safer way we wrote this crazy amazing 6474 04:41:55,798 --> 04:41:58,878 test Suite we wrote deploy scripts for 6475 04:41:58,878 --> 04:42:01,520 this we wrote We interacted with a 6476 04:42:01,520 --> 04:42:03,760 couple different libraries we learned 6477 04:42:03,760 --> 04:42:06,878 about this failon revert runs depths and 6478 04:42:06,878 --> 04:42:09,000 variance the only thing we didn't do was 6479 04:42:09,000 --> 04:42:11,440 write a proper read me which you 100% % 6480 04:42:11,440 --> 04:42:13,760 should write a proper readme and if you 6481 04:42:13,760 --> 04:42:15,480 want you can check out The Foundry defi 6482 04:42:15,480 --> 04:42:17,798 stablecoin readme to see how it actually 6483 04:42:17,798 --> 04:42:21,440 works of course even for me this was a 6484 04:42:21,440 --> 04:42:24,440 long difficult project for me to build 6485 04:42:24,440 --> 04:42:25,840 because there's just so much to think 6486 04:42:25,840 --> 04:42:28,280 about like I said this is a project that 6487 04:42:28,280 --> 04:42:30,440 I am planning on getting audited so what 6488 04:42:30,440 --> 04:42:32,920 you're going to see in this repo is 6489 04:42:32,920 --> 04:42:34,360 you're going to see kind of this main 6490 04:42:34,360 --> 04:42:35,520 branch which is what you're going to be 6491 04:42:35,520 --> 04:42:37,480 working on but I'm additionally going to 6492 04:42:37,480 --> 04:42:40,280 make a new branch called like audited or 6493 04:42:40,280 --> 04:42:41,560 post audit or something something like 6494 04:42:41,560 --> 04:42:43,878 that and if you want to follow and watch 6495 04:42:43,878 --> 04:42:45,718 this GitHub repo you can see the 6496 04:42:45,718 --> 04:42:47,320 progress and you can see the audit 6497 04:42:47,320 --> 04:42:49,000 reports that come out on here so that 6498 04:42:49,000 --> 04:42:51,878 you can be intimately familiar with this 6499 04:42:51,878 --> 04:42:53,798 codebase already because you wrote some 6500 04:42:53,798 --> 04:42:56,600 of the code and then also see as it 6501 04:42:56,600 --> 04:42:59,958 progresses through its security journey 6502 04:42:59,958 --> 04:43:01,280 and for those of you who are looking to 6503 04:43:01,280 --> 04:43:03,040 actually release production code you 6504 04:43:03,040 --> 04:43:05,760 definitely need to be at least aware of 6505 04:43:05,760 --> 04:43:07,798 how security works and the security 6506 04:43:07,798 --> 04:43:10,958 paths that your code should take but all 6507 04:43:10,958 --> 04:43:11,718 right 6508 04:43:11,718 --> 04:43:13,840 with all of this being said you know 6509 04:43:13,840 --> 04:43:17,600 what time it is it's time for you to get 6510 04:43:17,600 --> 04:43:21,480 a break you deserve it you should 100% 6511 04:43:21,480 --> 04:43:24,680 go take a lap push this code base up to 6512 04:43:24,680 --> 04:43:26,958 GitHub and actually clean it up a little 6513 04:43:26,958 --> 04:43:28,680 bit I'm going to be cleaning this up a 6514 04:43:28,680 --> 04:43:30,680 little bit before I push the rest of it 6515 04:43:30,680 --> 04:43:32,718 to this GitHub repo so you should clean 6516 04:43:32,718 --> 04:43:34,760 it up a little bit make it yours make it 6517 04:43:34,760 --> 04:43:37,120 the way that you want to make it maybe 6518 04:43:37,120 --> 04:43:39,080 even improve on it right we saw with the 6519 04:43:39,080 --> 04:43:41,320 invariant test that there is at least 6520 04:43:41,320 --> 04:43:43,520 one other glaring issue with this 6521 04:43:43,520 --> 04:43:47,360 protocol if the price of the assets 6522 04:43:47,360 --> 04:43:49,480 collapse too quickly our protocol 6523 04:43:49,480 --> 04:43:52,040 becomes insolvent so maybe you come up 6524 04:43:52,040 --> 04:43:54,240 with a method to fix it and then maybe 6525 04:43:54,240 --> 04:43:56,280 you launch your own stable coin why not 6526 04:43:56,280 --> 04:43:59,320 right in any case good luck to you take 6527 04:43:59,320 --> 04:44:02,958 that break we only have we're getting so 6528 04:44:02,958 --> 04:44:06,958 close we only have one two three more 6529 04:44:06,958 --> 04:44:09,680 lessons and these ones are actually 6530 04:44:09,680 --> 04:44:12,000 easier than the one we just did we're 6531 04:44:12,000 --> 04:44:13,600 going to learn about upgrades and 6532 04:44:13,600 --> 04:44:15,320 proxies we're going to learn about 6533 04:44:15,320 --> 04:44:17,120 governance and then we're going to do an 6534 04:44:17,120 --> 04:44:19,440 introduction to Smart contract security 6535 04:44:19,440 --> 04:44:21,440 these are much easier than everything 6536 04:44:21,440 --> 04:44:24,920 you've done so far so take the break 6537 04:44:24,920 --> 04:44:26,680 give yourself a pat on the back be 6538 04:44:26,680 --> 04:44:28,760 incredibly excited celebrate this win 6539 04:44:28,760 --> 04:44:30,680 this is a huge achievement getting this 6540 04:44:30,680 --> 04:44:35,520 far and I'll see you very soon three 6541 04:44:35,520 --> 04:44:38,520 more 6542 04:44:40,040 --> 04:44:43,280 left and as a bonus piece of content 6543 04:44:43,280 --> 04:44:45,160 here another one of the reasons that I 6544 04:44:45,160 --> 04:44:47,718 absolutely love the a protocol and the a 6545 04:44:47,718 --> 04:44:50,280 team is that they're just shipping 6546 04:44:50,280 --> 04:44:52,958 protocols and shipping amazing products 6547 04:44:52,958 --> 04:44:55,200 and features and services for the web3 6548 04:44:55,200 --> 04:44:58,360 community one of those protocols is 6549 04:44:58,360 --> 04:45:00,680 something called lens protocol which is 6550 04:45:00,680 --> 04:45:02,480 a decentralized social layer or a 6551 04:45:02,480 --> 04:45:03,958 decentralized social platform for 6552 04:45:03,958 --> 04:45:06,280 building social medias so to give us 6553 04:45:06,280 --> 04:45:07,718 some information about this we have the 6554 04:45:07,718 --> 04:45:09,480 head of devell for lens protocol and The 6555 04:45:09,480 --> 04:45:11,920 A Team Nat dbit to talk a little bit 6556 04:45:11,920 --> 04:45:15,000 more about lens hi my name is Nat dbit I 6557 04:45:15,000 --> 04:45:16,440 wanted to give you a quick introduction 6558 04:45:16,440 --> 04:45:18,560 to lens protocol and why it might be 6559 04:45:18,560 --> 04:45:20,718 interesting to you as a smart contractor 6560 04:45:20,718 --> 04:45:22,958 solidity engineer lens is the social 6561 04:45:22,958 --> 04:45:25,280 layer of web 3 it allows developers to 6562 04:45:25,280 --> 04:45:27,360 build social applications or to 6563 04:45:27,360 --> 04:45:29,040 implement social features into their 6564 04:45:29,040 --> 04:45:31,440 existing applications there are 4.9 6565 04:45:31,440 --> 04:45:32,840 billion people in the world today 6566 04:45:32,840 --> 04:45:35,200 already using social applications so 6567 04:45:35,200 --> 04:45:37,080 these types of apps provide a use case 6568 04:45:37,080 --> 04:45:39,120 that people already know understand and 6569 04:45:39,120 --> 04:45:41,638 value they also present a wide variety 6570 04:45:41,638 --> 04:45:43,680 of value propositions and opportunities 6571 04:45:43,680 --> 04:45:45,480 for developers to take advantage of and 6572 04:45:45,480 --> 04:45:47,958 build on and with web 3 features like 6573 04:45:47,958 --> 04:45:49,560 native payments ownership and 6574 04:45:49,560 --> 04:45:51,718 composability also provide a lot of 6575 04:45:51,718 --> 04:45:53,360 Primitives to build on that were not 6576 04:45:53,360 --> 04:45:55,000 available with traditional social 6577 04:45:55,000 --> 04:45:57,360 applications or infrastructure lens 6578 04:45:57,360 --> 04:45:59,120 allows developers to extend the core 6579 04:45:59,120 --> 04:46:01,160 smart contracts by building out their 6580 04:46:01,160 --> 04:46:03,320 own custom modules this would be similar 6581 04:46:03,320 --> 04:46:05,120 to as if Twitter Instagram or other 6582 04:46:05,120 --> 04:46:07,200 social applications allow developers to 6583 04:46:07,200 --> 04:46:09,480 send pull requests into their backends 6584 04:46:09,480 --> 04:46:12,000 and apis this this opens the door to a 6585 04:46:12,000 --> 04:46:13,520 lot of interesting and Powerful 6586 04:46:13,520 --> 04:46:14,798 functionality that we're seeing 6587 04:46:14,798 --> 04:46:16,680 developers integrate into their 6588 04:46:16,680 --> 04:46:18,958 applications build out new different 6589 04:46:18,958 --> 04:46:20,958 ideas but also integrate into other 6590 04:46:20,958 --> 04:46:23,840 parts of web 3 like defi in addition to 6591 04:46:23,840 --> 04:46:25,920 that you can call lend smart contracts 6592 04:46:25,920 --> 04:46:27,958 from other smart contracts so if you'd 6593 04:46:27,958 --> 04:46:29,360 like to build out something that is 6594 04:46:29,360 --> 04:46:31,600 composable with the web 3 social graph 6595 04:46:31,600 --> 04:46:33,920 lens is a great place to integrate if 6596 04:46:33,920 --> 04:46:35,680 you want to get started building on lens 6597 04:46:35,680 --> 04:46:38,440 check out the docs at docs. lens. XYZ 6598 04:46:38,440 --> 04:46:40,440 and be sure to check out how to deploy 6599 04:46:40,440 --> 04:46:42,440 the product call on your own so you can 6600 04:46:42,440 --> 04:46:44,080 check out the smart contract code and 6601 04:46:44,080 --> 04:46:46,280 play around with it and also look at how 6602 04:46:46,280 --> 04:46:48,680 to build out and create your own custom 6603 04:46:48,680 --> 04:46:53,320 modules thanks for checking this out 6604 04:47:11,240 --> 04:47:14,200 all right and welcome to lesson 13 6605 04:47:14,200 --> 04:47:16,520 boundary upgrades where we're going to 6606 04:47:16,520 --> 04:47:18,958 learn about upgradeable smart contracts 6607 04:47:18,958 --> 04:47:20,920 the pros and the cons now I've actually 6608 04:47:20,920 --> 04:47:22,320 done a couple different versions of this 6609 04:47:22,320 --> 04:47:23,718 type of video in the past so we're going 6610 04:47:23,718 --> 04:47:25,480 to go through an old video explaining 6611 04:47:25,480 --> 04:47:27,240 the concepts of this then we're going to 6612 04:47:27,240 --> 04:47:28,600 move over to remix and we're going to 6613 04:47:28,600 --> 04:47:30,760 explain an important subl lesson and 6614 04:47:30,760 --> 04:47:32,798 then we're going to go to our vs code 6615 04:47:32,798 --> 04:47:34,040 and we're actually going to do all of 6616 04:47:34,040 --> 04:47:36,040 this in Foundry upgradeable smart 6617 04:47:36,040 --> 04:47:37,480 contracts are something that I'm going 6618 04:47:37,480 --> 04:47:39,878 to go ahead and just say right out loud 6619 04:47:39,878 --> 04:47:42,240 we should deploy deoy as little as 6620 04:47:42,240 --> 04:47:44,680 possible we should not be defaulting to 6621 04:47:44,680 --> 04:47:46,600 upgradeable smart contracts it sounds 6622 04:47:46,600 --> 04:47:48,840 like a good idea in theory but we've 6623 04:47:48,840 --> 04:47:50,760 seen time and time again whenever a 6624 04:47:50,760 --> 04:47:52,840 protocol has a centralized control over 6625 04:47:52,840 --> 04:47:55,680 contracts issues Happen full stop so 6626 04:47:55,680 --> 04:47:57,200 when going through this course and when 6627 04:47:57,200 --> 04:47:59,400 learning about this keep this in mind 6628 04:47:59,400 --> 04:48:01,320 what are the downsides of upgradable 6629 04:48:01,320 --> 04:48:02,958 smart contracts and then you can begin 6630 04:48:02,958 --> 04:48:05,160 to ask questions have we seen examples 6631 04:48:05,160 --> 04:48:06,760 in real life where these downsides have 6632 04:48:06,760 --> 04:48:09,160 come to a fruition and I'll say right 6633 04:48:09,160 --> 04:48:12,280 now 100% the answer is yes so keep that 6634 04:48:12,280 --> 04:48:14,040 in mind when watching this and use this 6635 04:48:14,040 --> 04:48:16,280 knowledge to make yourself be an even 6636 04:48:16,280 --> 04:48:20,480 better smart contract Dev so let's 6637 04:48:21,760 --> 04:48:24,040 learn now I'm editing this video much 6638 04:48:24,040 --> 04:48:26,280 later after I filmed it hence why I have 6639 04:48:26,280 --> 04:48:28,280 a beard so I'll be jumping in from time 6640 04:48:28,280 --> 04:48:30,840 to time updating some of the sections 6641 04:48:30,840 --> 04:48:32,200 when deploying your smart contracts on 6642 04:48:32,200 --> 04:48:33,798 chain we all know that those smart 6643 04:48:33,798 --> 04:48:36,120 contracts are immutable or unchangeable 6644 04:48:36,120 --> 04:48:39,520 but what if I told you that they were 6645 04:48:39,520 --> 04:48:41,840 mutable well technically I wouldn't be 6646 04:48:41,840 --> 04:48:43,718 correct however smart contracts actually 6647 04:48:43,718 --> 04:48:46,160 can change all the time when people 6648 04:48:46,160 --> 04:48:48,240 transfer tokens when people stake in a 6649 04:48:48,240 --> 04:48:49,638 contract or really do any type of 6650 04:48:49,638 --> 04:48:51,320 functionality those smart contracts have 6651 04:48:51,320 --> 04:48:53,400 to update their balances and update 6652 04:48:53,400 --> 04:48:54,600 their mappings and update their 6653 04:48:54,600 --> 04:48:56,680 variables to reflect this the reason 6654 04:48:56,680 --> 04:48:58,440 that they're immutable is that the logic 6655 04:48:58,440 --> 04:49:00,240 itself never changes and will be on 6656 04:49:00,240 --> 04:49:02,600 chain like that forever so technically 6657 04:49:02,600 --> 04:49:03,920 yes once they are deployed they are 6658 04:49:03,920 --> 04:49:05,440 immutable and this is actually one of 6659 04:49:05,440 --> 04:49:07,040 the major benefits of smart contracts in 6660 04:49:07,040 --> 04:49:08,878 the first place that nobody can tamper 6661 04:49:08,878 --> 04:49:10,240 with or screw with our smart contracts 6662 04:49:10,240 --> 04:49:11,840 once we Dey deploy them however this can 6663 04:49:11,840 --> 04:49:13,600 be an issue if for example we want to 6664 04:49:13,600 --> 04:49:15,240 upgrade our smart contract or protocol 6665 04:49:15,240 --> 04:49:17,520 to do more things or we want to fix some 6666 04:49:17,520 --> 04:49:19,680 glaring bug or issue that we have now 6667 04:49:19,680 --> 04:49:21,638 even though we can't change the specific 6668 04:49:21,638 --> 04:49:23,360 code that's been deployed to an address 6669 04:49:23,360 --> 04:49:24,600 we can actually do a lot more than you 6670 04:49:24,600 --> 04:49:26,080 think and in this video we're going to 6671 04:49:26,080 --> 04:49:27,718 explain the different methodologies 6672 04:49:27,718 --> 04:49:29,680 behind upgrading your smart contracts 6673 04:49:29,680 --> 04:49:30,920 and then we're going to show you how to 6674 04:49:30,920 --> 04:49:32,760 do it with hard hat and open Zeppelin 6675 04:49:32,760 --> 04:49:34,600 huge shout out to a lot of open Zeppelin 6676 04:49:34,600 --> 04:49:36,040 and trailer bits articles that help me 6677 04:49:36,040 --> 04:49:38,080 put this video together uh and a number 6678 04:49:38,080 --> 04:49:39,680 of other sources as well links in the 6679 04:49:39,680 --> 04:49:41,958 description so let's get to it now at 6680 04:49:41,958 --> 04:49:44,400 first glance you might be thinking if 6681 04:49:44,400 --> 04:49:46,320 you can upgrade your smart contracts 6682 04:49:46,320 --> 04:49:50,400 then they're not really immutable then 6683 04:49:50,400 --> 04:49:52,040 and in a way you'd be right so when 6684 04:49:52,040 --> 04:49:53,080 explaining kind of the different 6685 04:49:53,080 --> 04:49:54,440 philosophies and patterns that we can 6686 04:49:54,440 --> 04:49:56,400 use here we do need to keep in mind the 6687 04:49:56,400 --> 04:49:58,200 philosophies and decentralization 6688 04:49:58,200 --> 04:49:59,680 implications that each one of these 6689 04:49:59,680 --> 04:50:01,480 patterns have as they do all have 6690 04:50:01,480 --> 04:50:03,320 different advantages and disadvantages 6691 04:50:03,320 --> 04:50:05,878 and yes some of the disadvantages here 6692 04:50:05,878 --> 04:50:07,840 are going to affect de centrality so we 6693 04:50:07,840 --> 04:50:09,240 need to keep that in mind and this is 6694 04:50:09,240 --> 04:50:10,840 why it's so important that before before 6695 04:50:10,840 --> 04:50:12,320 you go ahead and jump in and start 6696 04:50:12,320 --> 04:50:14,200 deploying upgradeable smart contracts 6697 04:50:14,200 --> 04:50:15,920 you understand the trade-offs so we're 6698 04:50:15,920 --> 04:50:17,520 going to talk about three different ways 6699 04:50:17,520 --> 04:50:19,360 to upgrade your smart contracts the 6700 04:50:19,360 --> 04:50:21,680 first one being the not really SLP 6701 04:50:21,680 --> 04:50:23,798 parameter ties way to upgrade your smart 6702 04:50:23,798 --> 04:50:26,520 contracts the social migration method 6703 04:50:26,520 --> 04:50:27,878 and then the method that you probably 6704 04:50:27,878 --> 04:50:30,000 have heard about which is proxies which 6705 04:50:30,000 --> 04:50:31,760 have a ton of subcategory like 6706 04:50:31,760 --> 04:50:33,600 metamorphic contracts transparent 6707 04:50:33,600 --> 04:50:35,638 upgradable proxies and Universal 6708 04:50:35,638 --> 04:50:37,400 upgradeable proxies so let's talk about 6709 04:50:37,400 --> 04:50:39,400 the not really upgrading method or the 6710 04:50:39,400 --> 04:50:41,120 parameterization method or whatever you 6711 04:50:41,120 --> 04:50:43,440 want to call it this is the simplest way 6712 04:50:43,440 --> 04:50:44,958 to think about upgrading your smart 6713 04:50:44,958 --> 04:50:46,718 contracts and it really isn't upgrading 6714 04:50:46,718 --> 04:50:48,440 our smart contracts because we can't 6715 04:50:48,440 --> 04:50:50,400 really change the logic of the smart 6716 04:50:50,400 --> 04:50:51,718 contract whatever logic that we've 6717 04:50:51,718 --> 04:50:53,840 written is there we also can't add new 6718 04:50:53,840 --> 04:50:55,520 storage or state variables so this is 6719 04:50:55,520 --> 04:50:57,958 really not really upgrading but it is 6720 04:50:57,958 --> 04:50:59,760 something to think about upgrades is 6721 04:50:59,760 --> 04:51:01,958 just parameterizing everything whatever 6722 04:51:01,958 --> 04:51:03,958 logic that we've deployed is there and 6723 04:51:03,958 --> 04:51:05,360 that's what we're interacting with this 6724 04:51:05,360 --> 04:51:06,440 function means we just have a whole 6725 04:51:06,440 --> 04:51:07,920 bunch of Setter functions and we can 6726 04:51:07,920 --> 04:51:10,000 update certain parameters like maybe we 6727 04:51:10,000 --> 04:51:12,320 have a reward parameter that gives out a 6728 04:51:12,320 --> 04:51:15,400 token at 1% every year or something like 6729 04:51:15,400 --> 04:51:16,958 that maybe we have a set of function 6730 04:51:16,958 --> 04:51:19,040 that says hey update that to 2% or 6731 04:51:19,040 --> 04:51:21,520 update that to 4% it's just a Setter 6732 04:51:21,520 --> 04:51:23,638 function that changes some variable now 6733 04:51:23,638 --> 04:51:25,000 the advantages here are obviously this 6734 04:51:25,000 --> 04:51:26,958 is really simple to implement the 6735 04:51:26,958 --> 04:51:29,160 disadvantage is that if you didn't think 6736 04:51:29,160 --> 04:51:31,320 of some logic or some functionality the 6737 04:51:31,320 --> 04:51:32,920 first time you deployed their smart 6738 04:51:32,920 --> 04:51:35,120 contract that's too bad you're stuck 6739 04:51:35,120 --> 04:51:37,080 with it you can't update the logic or 6740 04:51:37,080 --> 04:51:39,280 really update anything uh with the 6741 04:51:39,280 --> 04:51:41,400 parameterization AKA not really method 6742 04:51:41,400 --> 04:51:42,240 and the other thing you have to think 6743 04:51:42,240 --> 04:51:44,360 about is who the admins are who has 6744 04:51:44,360 --> 04:51:46,280 access to these Setter functions to 6745 04:51:46,280 --> 04:51:48,320 these updating functions if it's a 6746 04:51:48,320 --> 04:51:50,600 single person guess what you have a 6747 04:51:50,600 --> 04:51:52,320 centralized smart contract now of course 6748 04:51:52,320 --> 04:51:54,280 you can add a governance contract to be 6749 04:51:54,280 --> 04:51:56,400 the admin contract of your protocol and 6750 04:51:56,400 --> 04:51:57,878 that would be a decentralized way of 6751 04:51:57,878 --> 04:51:59,480 doing this so just keep that in mind you 6752 04:51:59,480 --> 04:52:01,160 can do this method just need a 6753 04:52:01,160 --> 04:52:02,920 governance protocol to do so another 6754 04:52:02,920 --> 04:52:04,558 example of this might be a contract 6755 04:52:04,558 --> 04:52:05,840 registry and this is something actually 6756 04:52:05,840 --> 04:52:07,920 that early versions of AA Used before 6757 04:52:07,920 --> 04:52:09,718 you call a function you actually check 6758 04:52:09,718 --> 04:52:11,680 some contract Reg indry that is updated 6759 04:52:11,680 --> 04:52:13,520 as a parameter by somebody and you get 6760 04:52:13,520 --> 04:52:15,080 routed to that contract and you do your 6761 04:52:15,080 --> 04:52:16,520 call there again this really doesn't 6762 04:52:16,520 --> 04:52:18,120 allow us to have the full functionality 6763 04:52:18,120 --> 04:52:20,400 of upgrades here you can argue that this 6764 04:52:20,400 --> 04:52:22,200 registry is a mix of one of the later 6765 04:52:22,200 --> 04:52:23,798 versions but for all intents and 6766 04:52:23,798 --> 04:52:25,240 purposes this doesn't really give us 6767 04:52:25,240 --> 04:52:26,558 that flexibility that we want for our 6768 04:52:26,558 --> 04:52:28,160 upgrades but some people might even 6769 04:52:28,160 --> 04:52:30,040 think that upgrading your smart contract 6770 04:52:30,040 --> 04:52:32,200 is ruining the decentral and one of the 6771 04:52:32,200 --> 04:52:33,520 things that Mak smart contracts so 6772 04:52:33,520 --> 04:52:35,600 potent is that they are immutable and 6773 04:52:35,600 --> 04:52:36,840 that this is one of the benefits that 6774 04:52:36,840 --> 04:52:38,680 they have so there are some people who 6775 04:52:38,680 --> 04:52:39,958 think that you shouldn't add any 6776 04:52:39,958 --> 04:52:42,200 customers ation or any upgrade ability 6777 04:52:42,200 --> 04:52:44,280 you should deploy your contract and then 6778 04:52:44,280 --> 04:52:46,240 that's it trb bits has actually argued 6779 04:52:46,240 --> 04:52:48,520 that if you deploy your contract knowing 6780 04:52:48,520 --> 04:52:50,558 that it can't be changed later you take 6781 04:52:50,558 --> 04:52:52,840 a little bit extra time making sure you 6782 04:52:52,840 --> 04:52:54,520 get everything right and there are often 6783 04:52:54,520 --> 04:52:56,920 less security vulnerabilities because 6784 04:52:56,920 --> 04:52:58,440 you're just setting it forgetting it and 6785 04:52:58,440 --> 04:53:00,360 not looking at it again now if I wanted 6786 04:53:00,360 --> 04:53:02,080 to upgrade a smart contract with this 6787 04:53:02,080 --> 04:53:04,120 philosophy in mind the philosophy that I 6788 04:53:04,120 --> 04:53:05,840 do want to keep my smart contracts 6789 04:53:05,840 --> 04:53:08,760 immutable we can instead use the social 6790 04:53:08,760 --> 04:53:10,878 migration method which I previously 6791 04:53:10,878 --> 04:53:12,798 called the Yeet method and now I think 6792 04:53:12,798 --> 04:53:14,080 it's less funny so we're just going to 6793 04:53:14,080 --> 04:53:15,760 stick with social migration the social 6794 04:53:15,760 --> 04:53:18,280 Yeet method or the migration method is 6795 04:53:18,280 --> 04:53:20,120 just when you deploy your new contract 6796 04:53:20,120 --> 04:53:22,320 not connected to the old contract in any 6797 04:53:22,320 --> 04:53:24,520 way and by social convention you tell 6798 04:53:24,520 --> 04:53:27,160 everybody hey hey this new contract this 6799 04:53:27,160 --> 04:53:28,600 new one that we just deployed yeah this 6800 04:53:28,600 --> 04:53:30,440 is the real one now and it's just by 6801 04:53:30,440 --> 04:53:32,558 Convention of people migrating and over 6802 04:53:32,558 --> 04:53:34,718 into using this new one that the upgrade 6803 04:53:34,718 --> 04:53:36,480 is done hence my slang name of social 6804 04:53:36,480 --> 04:53:39,000 Yeet because you Yeet the first one out 6805 04:53:39,000 --> 04:53:40,600 of the way and you move to the the 6806 04:53:40,600 --> 04:53:42,680 second 6807 04:53:42,680 --> 04:53:45,638 one I think I'm funny this has the 6808 04:53:45,638 --> 04:53:47,798 advantage of truly always saying hey 6809 04:53:47,798 --> 04:53:49,718 this is our am mutable smart contract 6810 04:53:49,718 --> 04:53:51,040 and this is our new one this is really 6811 04:53:51,040 --> 04:53:52,878 the truest definition of a mutable 6812 04:53:52,878 --> 04:53:55,200 because since you give it no way of 6813 04:53:55,200 --> 04:53:57,520 being upgraded in place then if somebody 6814 04:53:57,520 --> 04:54:00,040 calls that contract in 50,000 years in 6815 04:54:00,040 --> 04:54:02,558 the future it'll respond exactly the 6816 04:54:02,558 --> 04:54:04,840 same another huge disadvantage here is 6817 04:54:04,840 --> 04:54:06,520 that you have to have a totally new 6818 04:54:06,520 --> 04:54:09,200 contract address so if you're an erc20 6819 04:54:09,200 --> 04:54:10,600 token for example you have to go 6820 04:54:10,600 --> 04:54:13,160 convince all the exchanges to list your 6821 04:54:13,160 --> 04:54:15,120 new contract address as the actual 6822 04:54:15,120 --> 04:54:16,638 address keep in mind that when we do 6823 04:54:16,638 --> 04:54:18,680 this we do have to move the state of the 6824 04:54:18,680 --> 04:54:21,160 first one over to the second one so for 6825 04:54:21,160 --> 04:54:23,520 example if you're an ERC token moving to 6826 04:54:23,520 --> 04:54:25,798 a new version of that ERC token you do 6827 04:54:25,798 --> 04:54:27,558 have to have a way to take all those 6828 04:54:27,558 --> 04:54:29,320 mappings from the first contract and 6829 04:54:29,320 --> 04:54:30,958 move it to the second one obviously 6830 04:54:30,958 --> 04:54:32,200 there are ways to do this since 6831 04:54:32,200 --> 04:54:34,040 everything is on chain but if you have a 6832 04:54:34,040 --> 04:54:35,520 million transfer calls I don't want to 6833 04:54:35,520 --> 04:54:37,120 have to write the script that updates 6834 04:54:37,120 --> 04:54:38,558 everyone's balance and figures out what 6835 04:54:38,558 --> 04:54:39,840 everyone's balance is just so I can 6836 04:54:39,840 --> 04:54:41,120 migrate to my new version of the 6837 04:54:41,120 --> 04:54:43,400 contract so there is a ton of social 6838 04:54:43,400 --> 04:54:45,360 convention work here to do trailer bits 6839 04:54:45,360 --> 04:54:47,200 has actually written a fantastic blog on 6840 04:54:47,200 --> 04:54:50,920 upgrading from a V1 to a V2 or Etc with 6841 04:54:50,920 --> 04:54:52,718 this Yeet methodology and they give a 6842 04:54:52,718 --> 04:54:54,400 lot of steps for moving your storage and 6843 04:54:54,400 --> 04:54:55,878 your state variables over to the new 6844 04:54:55,878 --> 04:54:57,520 contract so Link in the description if 6845 04:54:57,520 --> 04:54:58,680 you want to read that now let's get to 6846 04:54:58,680 --> 04:55:00,360 our big ticket item so in order to have 6847 04:55:00,360 --> 04:55:02,760 a really robust upgrading mentality or 6848 04:55:02,760 --> 04:55:04,558 philosophy we need to have some type of 6849 04:55:04,558 --> 04:55:06,240 methodology or framework that can update 6850 04:55:06,240 --> 04:55:08,840 our state keep our contract address and 6851 04:55:08,840 --> 04:55:11,480 allow us to update any type of logic in 6852 04:55:11,480 --> 04:55:13,680 our smart contracts in an easy way which 6853 04:55:13,680 --> 04:55:16,360 leads us to our big ticket item the 6854 04:55:16,360 --> 04:55:20,160 proxies what's our big ticket 6855 04:55:20,558 --> 04:55:24,120 item proxies it's people proxies proxies 6856 04:55:24,120 --> 04:55:26,638 are the truest form of upgrades since a 6857 04:55:26,638 --> 04:55:28,160 user can keep interacting with the 6858 04:55:28,160 --> 04:55:30,520 protocols through these proxies and not 6859 04:55:30,520 --> 04:55:32,000 even notice that anything changed or 6860 04:55:32,000 --> 04:55:33,958 even got updated now these are also the 6861 04:55:33,958 --> 04:55:35,760 places where you can screw up the 6862 04:55:35,760 --> 04:55:38,280 easiest proxies use a lot of low-level 6863 04:55:38,280 --> 04:55:40,558 functionality and the main one being the 6864 04:55:40,558 --> 04:55:42,600 delegate call functionality delegate 6865 04:55:42,600 --> 04:55:44,718 call is a low-level function where the 6866 04:55:44,718 --> 04:55:47,798 code in the Target contract is executed 6867 04:55:47,798 --> 04:55:50,520 in the context of the calling contract 6868 04:55:50,520 --> 04:55:52,558 and message. sender and message. value 6869 04:55:52,558 --> 04:55:54,280 also don't change so you understand what 6870 04:55:54,280 --> 04:55:56,320 delegate call means now right great and 6871 04:55:56,320 --> 04:55:58,798 in English this means if I delegate call 6872 04:55:58,798 --> 04:56:02,440 a function in contract B from contract a 6873 04:56:02,440 --> 04:56:05,558 I will do contracts B's logic in 6874 04:56:05,558 --> 04:56:07,480 contract a so if contract B has a 6875 04:56:07,480 --> 04:56:09,718 function that says hey store this value 6876 04:56:09,718 --> 04:56:12,280 in a variable up top I'm going to store 6877 04:56:12,280 --> 04:56:15,320 that variable in contract a this is the 6878 04:56:15,320 --> 04:56:17,040 PowerHouse and this combined with the 6879 04:56:17,040 --> 04:56:20,120 fall Bank function allows us to delegate 6880 04:56:20,120 --> 04:56:22,320 all calls through a proxy contract 6881 04:56:22,320 --> 04:56:25,040 address to some other contract this 6882 04:56:25,040 --> 04:56:27,200 means that I can have one proxy contract 6883 04:56:27,200 --> 04:56:29,400 that will have the same address forever 6884 04:56:29,400 --> 04:56:32,080 and I can just point and Route people to 6885 04:56:32,080 --> 04:56:34,440 the correct implementation contract that 6886 04:56:34,440 --> 04:56:37,160 has the logic whenever I want to upgrade 6887 04:56:37,160 --> 04:56:39,440 I just deploy a new implementation 6888 04:56:39,440 --> 04:56:42,200 contract and point my proxy to that new 6889 04:56:42,200 --> 04:56:44,280 implementation now whenever a user calls 6890 04:56:44,280 --> 04:56:46,320 a function on the proxy contract I'm 6891 04:56:46,320 --> 04:56:48,680 going to delegate call it to the new 6892 04:56:48,680 --> 04:56:51,000 contract I can just call an admin only 6893 04:56:51,000 --> 04:56:53,120 function on my proxy contract let's call 6894 04:56:53,120 --> 04:56:54,520 it upgrade or something and I make all 6895 04:56:54,520 --> 04:56:56,240 the contract calls go to this new 6896 04:56:56,240 --> 04:56:57,480 contract when we're talking about 6897 04:56:57,480 --> 04:56:58,840 proxies there are four pieces of 6898 04:56:58,840 --> 04:57:00,718 terminology that we want to keep in mind 6899 04:57:00,718 --> 04:57:02,558 first is the implementation contract the 6900 04:57:02,558 --> 04:57:04,200 implementation contract has all of our 6901 04:57:04,200 --> 04:57:06,240 logic and all the pieces of our protocol 6902 04:57:06,240 --> 04:57:08,480 whenever we upgrade we actually launch a 6903 04:57:08,480 --> 04:57:10,440 brand new implementation contract the 6904 04:57:10,440 --> 04:57:13,120 proxy contract proxy points to which 6905 04:57:13,120 --> 04:57:15,600 implementation is the correct one and 6906 04:57:15,600 --> 04:57:17,760 routes everyone's calls to the correct 6907 04:57:17,760 --> 04:57:19,480 implementation contract you can think 6908 04:57:19,480 --> 04:57:21,958 the proxy contract sits on top of the 6909 04:57:21,958 --> 04:57:24,040 implementations the user the user is 6910 04:57:24,040 --> 04:57:25,760 going to be making contract and function 6911 04:57:25,760 --> 04:57:28,160 calls through the proxy contract and 6912 04:57:28,160 --> 04:57:30,520 then some type of admin the admin is the 6913 04:57:30,520 --> 04:57:32,280 one who's going to decide when to 6914 04:57:32,280 --> 04:57:35,160 upgrade and which contract to point to 6915 04:57:35,160 --> 04:57:36,718 in this scenario the other cool thing 6916 04:57:36,718 --> 04:57:38,680 about the proxy and delegate call is 6917 04:57:38,680 --> 04:57:40,600 that all my storage variabl are going to 6918 04:57:40,600 --> 04:57:43,280 be stored in the proxy contract and not 6919 04:57:43,280 --> 04:57:45,718 in the implementation contract this way 6920 04:57:45,718 --> 04:57:48,760 when I upgrade to a New Logic contract 6921 04:57:48,760 --> 04:57:51,000 all of my data will stay on the proxy 6922 04:57:51,000 --> 04:57:52,840 contract so whenever I want to update my 6923 04:57:52,840 --> 04:57:54,600 logic just point to a new implementation 6924 04:57:54,600 --> 04:57:56,600 contract if I want to add a new storage 6925 04:57:56,600 --> 04:57:59,320 variable or a new type of storage I just 6926 04:57:59,320 --> 04:58:01,080 add it in my logic contract and the 6927 04:58:01,080 --> 04:58:03,200 proxy contract will pick it up now using 6928 04:58:03,200 --> 04:58:05,240 proxies has a couple of gotas and we're 6929 04:58:05,240 --> 04:58:06,600 going to talk about the gachas and then 6930 04:58:06,600 --> 04:58:08,000 we're going to talk about the different 6931 04:58:08,000 --> 04:58:10,360 proxy contract methodologies because yes 6932 04:58:10,360 --> 04:58:11,798 there are many proxy contract 6933 04:58:11,798 --> 04:58:13,360 methodologies as well and this is why 6934 04:58:13,360 --> 04:58:15,080 tril bits doesn't really recommend using 6935 04:58:15,080 --> 04:58:16,840 upgradable proxies for your smart 6936 04:58:16,840 --> 04:58:18,718 contracts because they're fraught with a 6937 04:58:18,718 --> 04:58:20,920 lot of these potential issues not to 6938 04:58:20,920 --> 04:58:23,120 mention again you do still have some 6939 04:58:23,120 --> 04:58:24,360 type of admin who's going to be 6940 04:58:24,360 --> 04:58:26,360 upgrading your smart contracts now if 6941 04:58:26,360 --> 04:58:28,080 this is a governance protocol then great 6942 04:58:28,080 --> 04:58:29,920 you're decentralized but if this is a 6943 04:58:29,920 --> 04:58:31,320 single group or 6944 04:58:31,320 --> 04:58:34,480 entity then we have a problem the two 6945 04:58:34,480 --> 04:58:37,240 biggest gotas are storage clashes and 6946 04:58:37,240 --> 04:58:40,718 function selector clashes now what does 6947 04:58:40,718 --> 04:58:42,638 this mean when we use delegate call 6948 04:58:42,638 --> 04:58:45,400 remember we do the logic of contract B 6949 04:58:45,400 --> 04:58:48,320 inside contract a so if contract B says 6950 04:58:48,320 --> 04:58:50,878 we need to set value to two we go ahead 6951 04:58:50,878 --> 04:58:53,160 and set value to two but these smart 6952 04:58:53,160 --> 04:58:55,480 contracts are actually kind of dumb we 6953 04:58:55,480 --> 04:58:57,360 actually set the value of whatever is in 6954 04:58:57,360 --> 04:58:59,718 the same storage location on contract a 6955 04:58:59,718 --> 04:59:01,878 as contract B so if our contract looks 6956 04:59:01,878 --> 04:59:04,080 like this and we have two variables in 6957 04:59:04,080 --> 04:59:06,280 contract a we're still going to set the 6958 04:59:06,280 --> 04:59:08,440 first storage spot on contract a to the 6959 04:59:08,440 --> 04:59:10,400 new value this is really important to 6960 04:59:10,400 --> 04:59:12,080 know because this means we can only 6961 04:59:12,080 --> 04:59:14,040 append new storage variables in new 6962 04:59:14,040 --> 04:59:15,840 implementation contracts and we can't 6963 04:59:15,840 --> 04:59:18,360 reorder or change old ones this is 6964 04:59:18,360 --> 04:59:20,400 called storage clashing and in the 6965 04:59:20,400 --> 04:59:21,638 implementations we're going to talk 6966 04:59:21,638 --> 04:59:24,280 about they all address this issue the 6967 04:59:24,280 --> 04:59:26,240 next one is called function selector 6968 04:59:26,240 --> 04:59:28,240 clashes when we tell our proxies to 6969 04:59:28,240 --> 04:59:29,600 delegate call to one of these 6970 04:59:29,600 --> 04:59:31,638 implementations it uses what's called a 6971 04:59:31,638 --> 04:59:34,080 function selector to find a function the 6972 04:59:34,080 --> 04:59:36,280 function selector is a 4 byte hash of 6973 04:59:36,280 --> 04:59:37,798 the function name and the function 6974 04:59:37,798 --> 04:59:39,480 signature don't worry about the function 6975 04:59:39,480 --> 04:59:42,000 signature for now now it's possible that 6976 04:59:42,000 --> 04:59:43,958 a function in the implementation 6977 04:59:43,958 --> 04:59:47,160 contract has the same function selector 6978 04:59:47,160 --> 04:59:49,160 as an admin function in the proxy 6979 04:59:49,160 --> 04:59:50,878 contract which may cause you to do 6980 04:59:50,878 --> 04:59:52,360 accidentally a whole bunch of weird 6981 04:59:52,360 --> 04:59:54,520 stuff for example in this sample code in 6982 04:59:54,520 --> 04:59:56,440 front of you even though these functions 6983 04:59:56,440 --> 04:59:58,280 are totally different they actually have 6984 04:59:58,280 --> 05:00:00,520 the same function selector so yes we can 6985 05:00:00,520 --> 05:00:02,520 run into an issue where some harmless 6986 05:00:02,520 --> 05:00:05,638 function like get price has the same 6987 05:00:05,638 --> 05:00:08,480 function selector as upgrade proxy or 6988 05:00:08,480 --> 05:00:10,558 destroy proxy or something like app this 6989 05:00:10,558 --> 05:00:12,400 leads to our first out of the three 6990 05:00:12,400 --> 05:00:14,638 implementations of the proxy contracts 6991 05:00:14,638 --> 05:00:16,718 this is called the transparent proxy 6992 05:00:16,718 --> 05:00:19,120 pattern in this methodology admins are 6993 05:00:19,120 --> 05:00:21,520 only allowed to call admin functions and 6994 05:00:21,520 --> 05:00:23,120 they can't call any functions in the 6995 05:00:23,120 --> 05:00:25,718 implementation contract and users can 6996 05:00:25,718 --> 05:00:27,200 only call functions in the 6997 05:00:27,200 --> 05:00:29,040 implementation contract and not any 6998 05:00:29,040 --> 05:00:31,400 admin contracts this way you can't ever 6999 05:00:31,400 --> 05:00:32,680 accidentally have one of the two 7000 05:00:32,680 --> 05:00:35,120 swapping and having a function selector 7001 05:00:35,120 --> 05:00:36,760 clash and you running into a big issue 7002 05:00:36,760 --> 05:00:38,040 where you call a function you probably 7003 05:00:38,040 --> 05:00:39,480 shouldn't have if you're an admin you're 7004 05:00:39,480 --> 05:00:41,240 calling ad functions if you're a user 7005 05:00:41,240 --> 05:00:43,000 you're calling implementation functions 7006 05:00:43,000 --> 05:00:44,440 so if you're an admin and you build some 7007 05:00:44,440 --> 05:00:46,400 crazy awesome di5 protocol you better 7008 05:00:46,400 --> 05:00:47,600 come up with a new wallet address 7009 05:00:47,600 --> 05:00:49,480 because you can't participate the second 7010 05:00:49,480 --> 05:00:50,718 type of proxy we're going to talk about 7011 05:00:50,718 --> 05:00:52,798 is the universal upgradeable proxy or 7012 05:00:52,798 --> 05:00:54,510 the 7013 05:00:54,510 --> 05:00:57,798 [Music] 7014 05:00:57,798 --> 05:00:59,958 UPS this version of upgradeable 7015 05:00:59,958 --> 05:01:02,240 contracts actually puts all the logic of 7016 05:01:02,240 --> 05:01:05,120 upgrading in the implementation itself 7017 05:01:05,120 --> 05:01:07,040 this way the solidity compiler will 7018 05:01:07,040 --> 05:01:09,360 actually kick out and say hey we got two 7019 05:01:09,360 --> 05:01:10,680 functions in here that have the same 7020 05:01:10,680 --> 05:01:12,360 function selector this is also 7021 05:01:12,360 --> 05:01:14,080 advantageous because we have one less 7022 05:01:14,080 --> 05:01:15,920 read that we have to do we no longer 7023 05:01:15,920 --> 05:01:18,320 have to check in the proxy contract if 7024 05:01:18,320 --> 05:01:20,840 someone is an admin or not this saves on 7025 05:01:20,840 --> 05:01:22,760 gas of course and the proxy is also a 7026 05:01:22,760 --> 05:01:24,360 little bit smaller because of this the 7027 05:01:24,360 --> 05:01:25,718 issue is that if you deploy an 7028 05:01:25,718 --> 05:01:27,680 implementation contract without any 7029 05:01:27,680 --> 05:01:30,400 upgradeable functionality you're stuck 7030 05:01:30,400 --> 05:01:32,160 and it's back to the Yeet method with 7031 05:01:32,160 --> 05:01:34,520 you in the last pattern or methodology 7032 05:01:34,520 --> 05:01:35,958 that we're going to talk about is the 7033 05:01:35,958 --> 05:01:37,840 diamond pattern which does a number of 7034 05:01:37,840 --> 05:01:39,400 things but one of the biggest things 7035 05:01:39,400 --> 05:01:41,240 that it does it actually allows for 7036 05:01:41,240 --> 05:01:43,600 multiple implementation contracts this 7037 05:01:43,600 --> 05:01:45,718 addresses a couple different issues for 7038 05:01:45,718 --> 05:01:47,680 example if your contract is so big and 7039 05:01:47,680 --> 05:01:49,480 it doesn't fit into the one contract 7040 05:01:49,480 --> 05:01:51,638 maximum size you can just have multiple 7041 05:01:51,638 --> 05:01:53,400 contracts through this multi- 7042 05:01:53,400 --> 05:01:55,320 implementation method it also allows you 7043 05:01:55,320 --> 05:01:57,638 to make more granular upgrades like you 7044 05:01:57,638 --> 05:01:59,638 don't have to always deploy and upgrade 7045 05:01:59,638 --> 05:02:01,080 your entire smart contract you can just 7046 05:02:01,080 --> 05:02:02,520 upgrade little pieces of it if you've 7047 05:02:02,520 --> 05:02:04,040 chunked them out all the proxies 7048 05:02:04,040 --> 05:02:05,440 mentioned here have some type of 7049 05:02:05,440 --> 05:02:07,520 ethereum improvement proposal and most 7050 05:02:07,520 --> 05:02:09,080 of them are in the draft phase and at 7051 05:02:09,080 --> 05:02:10,798 the end of this explain we will do a 7052 05:02:10,798 --> 05:02:12,760 demo of showing you how the delegate 7053 05:02:12,760 --> 05:02:15,120 call function works and the end of the 7054 05:02:15,120 --> 05:02:17,920 demo is right now so let's look at 7055 05:02:17,920 --> 05:02:20,320 delegate 7056 05:02:20,440 --> 05:02:23,000 call now we're going to learn about how 7057 05:02:23,000 --> 05:02:24,798 to actually build these proxies how to 7058 05:02:24,798 --> 05:02:27,000 build these upgradeable smart contracts 7059 05:02:27,000 --> 05:02:28,958 and to do this we first need to learn 7060 05:02:28,958 --> 05:02:31,120 about this delegate call function and 7061 05:02:31,120 --> 05:02:32,760 it's going to be really similar to the 7062 05:02:32,760 --> 05:02:34,280 call function which we learned much 7063 05:02:34,280 --> 05:02:36,040 earlier if you haven't seen that be sure 7064 05:02:36,040 --> 05:02:38,320 to go back to our hard at nfts we have a 7065 05:02:38,320 --> 05:02:40,878 su lesson in there about evm op codes 7066 05:02:40,878 --> 05:02:42,798 and coding and calling and will give you 7067 05:02:42,798 --> 05:02:45,680 all the context for delegate call like I 7068 05:02:45,680 --> 05:02:47,558 said in the explainer it's very similar 7069 05:02:47,558 --> 05:02:49,320 to call however the way that I think 7070 05:02:49,320 --> 05:02:51,840 about it is one contract says oh I 7071 05:02:51,840 --> 05:02:53,558 really like your function I'm going to 7072 05:02:53,558 --> 05:02:55,440 borrow it myself and we're going to be 7073 05:02:55,440 --> 05:02:56,920 looking at solidity by example I'll 7074 05:02:56,920 --> 05:02:59,120 leave a description in the GitHub and 7075 05:02:59,120 --> 05:03:01,160 all the code for this will be in the 7076 05:03:01,160 --> 05:03:03,000 GitHub associated with this lesson as 7077 05:03:03,000 --> 05:03:05,360 well now we have two contracts we have 7078 05:03:05,360 --> 05:03:07,120 this contract B that we're going to be 7079 05:03:07,120 --> 05:03:08,958 deploying on remix and it looks like a 7080 05:03:08,958 --> 05:03:11,400 real minimum istic real simple contract 7081 05:03:11,400 --> 05:03:12,920 we have a couple of storage variables 7082 05:03:12,920 --> 05:03:14,400 here and then we have a function that 7083 05:03:14,400 --> 05:03:16,240 updates our values we have a function 7084 05:03:16,240 --> 05:03:19,280 called set bars and updates are ENT 7085 05:03:19,280 --> 05:03:21,638 public num now as we learned before 7086 05:03:21,638 --> 05:03:24,080 whenever we have some type of contract 7087 05:03:24,080 --> 05:03:26,160 with storage variables they get stored 7088 05:03:26,160 --> 05:03:28,558 in in this storage data structure that's 7089 05:03:28,558 --> 05:03:31,160 index starting from zero right now our 7090 05:03:31,160 --> 05:03:34,920 uint public num is at index0 our senders 7091 05:03:34,920 --> 05:03:37,920 at index one our values at index 2 Etc 7092 05:03:37,920 --> 05:03:40,878 now we're going to deploy a contract a 7093 05:03:40,878 --> 05:03:42,558 now this contract is actually going to 7094 05:03:42,558 --> 05:03:45,200 use the delegate call function now in 7095 05:03:45,200 --> 05:03:46,638 contract a this is going to look a 7096 05:03:46,638 --> 05:03:47,920 little bit different but it's still 7097 05:03:47,920 --> 05:03:50,040 going to have this set bars functions 7098 05:03:50,040 --> 05:03:51,360 except it's going to make a delegate 7099 05:03:51,360 --> 05:03:54,638 call function call to our contract B the 7100 05:03:54,638 --> 05:03:57,000 difference here is we're doing contract. 7101 05:03:57,000 --> 05:03:59,680 delegate call what this call does is 7102 05:03:59,680 --> 05:04:02,360 something very similar to call normally 7103 05:04:02,360 --> 05:04:04,558 if we did contract. call on this 7104 05:04:04,558 --> 05:04:06,558 contract we would just call this we 7105 05:04:06,558 --> 05:04:08,000 would just be calling this function set 7106 05:04:08,000 --> 05:04:10,920 bars which would update contracts B's 7107 05:04:10,920 --> 05:04:13,240 storage but instead we're saying hey 7108 05:04:13,240 --> 05:04:15,718 call that set bars function and then 7109 05:04:15,718 --> 05:04:18,000 pass this as an input parameter but call 7110 05:04:18,000 --> 05:04:21,760 it in our contract call it on contract a 7111 05:04:21,760 --> 05:04:23,558 we're kind of borrowing a function for 7112 05:04:23,558 --> 05:04:26,440 our contract and so instead what we're 7113 05:04:26,440 --> 05:04:28,680 going to do is we're going to borrow 7114 05:04:28,680 --> 05:04:30,400 this set bars and run the set bars 7115 05:04:30,400 --> 05:04:32,680 function over here now the difference is 7116 05:04:32,680 --> 05:04:35,120 instead of num equals num the variables 7117 05:04:35,120 --> 05:04:36,480 could be named different than the 7118 05:04:36,480 --> 05:04:39,120 variables on contract a so instead of 7119 05:04:39,120 --> 05:04:41,360 num equals numb our contract is going to 7120 05:04:41,360 --> 05:04:45,080 say hey whatever is that storage of Zero 7121 05:04:45,080 --> 05:04:46,760 have that equal to whatever we pass as 7122 05:04:46,760 --> 05:04:48,360 an input parameter and if that's a 7123 05:04:48,360 --> 05:04:50,320 little bit confusing just stay with me 7124 05:04:50,320 --> 05:04:51,638 let's go ahead and let's see this in 7125 05:04:51,638 --> 05:04:53,440 remix so I'm going to copy paste this 7126 05:04:53,440 --> 05:04:56,000 code into remix here so we can kind of 7127 05:04:56,000 --> 05:04:58,040 test and see what this looks like again 7128 05:04:58,040 --> 05:04:59,360 there's a link to this in the GitHub 7129 05:04:59,360 --> 05:05:01,080 repo associated with this course feel 7130 05:05:01,080 --> 05:05:02,600 free to pause the video to grab this 7131 05:05:02,600 --> 05:05:05,360 link it's solidity hyphen byy 7132 05:05:05,360 --> 05:05:08,280 example.org delegate call or you can 7133 05:05:08,280 --> 05:05:10,200 just grab the code directly from Lesson 7134 05:05:10,200 --> 05:05:12,680 16 hardhead upgrades so let's compile 7135 05:05:12,680 --> 05:05:13,798 this code and let me show you what I 7136 05:05:13,798 --> 05:05:15,440 mean so I'm going to compile it and 7137 05:05:15,440 --> 05:05:17,878 we'll go to the Run tab and first let's 7138 05:05:17,878 --> 05:05:20,718 deploy this contract B we'll hit deploy 7139 05:05:20,718 --> 05:05:22,840 we now have a contract num sender and 7140 05:05:22,840 --> 05:05:24,878 value or all blank we'll update the 7141 05:05:24,878 --> 05:05:26,958 number to something like 777 we'll hit 7142 05:05:26,958 --> 05:05:29,920 set vars set vars will change the 7143 05:05:29,920 --> 05:05:33,240 storage variable num to 777 and then 7144 05:05:33,240 --> 05:05:35,080 we're changing the sender and the value 7145 05:05:35,080 --> 05:05:37,520 sender and value is zero now let's 7146 05:05:37,520 --> 05:05:40,520 deploy contract a so we'll scroll back 7147 05:05:40,520 --> 05:05:43,360 up contract a deploy of course we're on 7148 05:05:43,360 --> 05:05:45,840 the JavaScript VM now we have this 7149 05:05:45,840 --> 05:05:48,120 contract a with numb value incenter are 7150 05:05:48,120 --> 05:05:51,320 also all blank but when we call set bars 7151 05:05:51,320 --> 05:05:53,718 it's going to borrow this set bars 7152 05:05:53,718 --> 05:05:56,480 function from contract B and run it in 7153 05:05:56,480 --> 05:05:59,440 contract a you can almost think of it as 7154 05:05:59,440 --> 05:06:03,680 if we're copying set vars and pasting it 7155 05:06:03,680 --> 05:06:06,798 into our contract a just for one run and 7156 05:06:06,798 --> 05:06:08,400 then immediately deleting it again 7157 05:06:08,400 --> 05:06:09,920 that's what this delegate call function 7158 05:06:09,920 --> 05:06:11,840 does so when I call set vars I'm going 7159 05:06:11,840 --> 05:06:14,440 to pass it this contract address as an 7160 05:06:14,440 --> 05:06:16,718 input parameter so it knows to call this 7161 05:06:16,718 --> 05:06:19,280 contract set vars function when I pass 7162 05:06:19,280 --> 05:06:22,558 it the address and I pass 987 since 7163 05:06:22,558 --> 05:06:24,480 we're borrowing the function we're not 7164 05:06:24,480 --> 05:06:27,480 going to update this num on contract B 7165 05:06:27,480 --> 05:06:29,160 we're going to update the num on 7166 05:06:29,160 --> 05:06:32,798 contract a so when I hit set bars we see 7167 05:06:32,798 --> 05:06:35,558 num now has 987 we see sender and we see 7168 05:06:35,558 --> 05:06:37,240 value still being zero here because 7169 05:06:37,240 --> 05:06:38,760 again we're borrowing this function and 7170 05:06:38,760 --> 05:06:40,680 running it here now the way that this 7171 05:06:40,680 --> 05:06:43,600 works is it actually doesn't look at the 7172 05:06:43,600 --> 05:06:46,440 names of our storage variables it looks 7173 05:06:46,440 --> 05:06:49,320 at the storage slots so when we borrow 7174 05:06:49,320 --> 05:06:52,160 this function using delegate call so we 7175 05:06:52,160 --> 05:06:53,840 could have this these variables we named 7176 05:06:53,840 --> 05:06:55,400 anything instead of num we could call 7177 05:06:55,400 --> 05:06:59,760 this first value sender we could call 7178 05:06:59,760 --> 05:07:02,798 something else and then value we could 7179 05:07:02,798 --> 05:07:06,520 call woo or whatever you want here and 7180 05:07:06,520 --> 05:07:08,080 when we borrow this function using 7181 05:07:08,080 --> 05:07:10,718 delegate call instead of us grabbing the 7182 05:07:10,718 --> 05:07:12,440 actual names of the variables our 7183 05:07:12,440 --> 05:07:14,040 contract will swap out these variable 7184 05:07:14,040 --> 05:07:16,520 names with the storage slot so it says 7185 05:07:16,520 --> 05:07:19,240 oh okay well in contract B you're 7186 05:07:19,240 --> 05:07:21,360 accessing the numb variable which is 7187 05:07:21,360 --> 05:07:24,000 which is at storage slot zero so when we 7188 05:07:24,000 --> 05:07:26,040 borrow set bars in contract a with 7189 05:07:26,040 --> 05:07:28,280 delegate call we'll 7190 05:07:28,280 --> 05:07:31,760 say storage 7191 05:07:31,760 --> 05:07:35,958 slot Z is going to equal that underscore 7192 05:07:35,958 --> 05:07:39,280 num which for this contract storage slot 7193 05:07:39,280 --> 05:07:42,760 Z is first value so we'll say first 7194 05:07:42,760 --> 05:07:46,638 value equals uncore num something else 7195 05:07:46,638 --> 05:07:48,638 is going to be storage Slot 2 so it's 7196 05:07:48,638 --> 05:07:50,958 going to say okay storage slot two we're 7197 05:07:50,958 --> 05:07:52,638 going to update storage slot two to 7198 05:07:52,638 --> 05:07:57,080 mess. Sender okay value here is storage 7199 05:07:57,080 --> 05:07:59,920 slot three so whatever's in storage slot 7200 05:07:59,920 --> 05:08:02,400 three will update with message. value 7201 05:08:02,400 --> 05:08:04,200 like this so that's essentially what's 7202 05:08:04,200 --> 05:08:06,400 going on behind the scenes so let's go 7203 05:08:06,400 --> 05:08:08,440 ahead let's delete those and 7204 05:08:08,440 --> 05:08:10,718 redeploy deploy them so we'll deploy 7205 05:08:10,718 --> 05:08:13,718 contract B we'll deploy contract a right 7206 05:08:13,718 --> 05:08:16,480 now in B once again if we do 1 2 3 for 7207 05:08:16,480 --> 05:08:19,760 set bars we have 1 2 3 and then contract 7208 05:08:19,760 --> 05:08:23,200 a now even though these variables have 7209 05:08:23,200 --> 05:08:25,200 different names we could grab contract 7210 05:08:25,200 --> 05:08:30,480 B's address paste it in do 654 hit set 7211 05:08:30,480 --> 05:08:33,878 bars and first value is now 654 so 7212 05:08:33,878 --> 05:08:35,958 delegate call allows us to borrow 7213 05:08:35,958 --> 05:08:38,520 functions and then just transposes 7214 05:08:38,520 --> 05:08:41,000 whatever is in here to the storage 7215 05:08:41,000 --> 05:08:43,120 location equivalents and the other thing 7216 05:08:43,120 --> 05:08:44,920 that's interesting is even if you don't 7217 05:08:44,920 --> 05:08:47,440 have variables it'll still save to 7218 05:08:47,440 --> 05:08:49,798 storage slots so in contract a if we 7219 05:08:49,798 --> 05:08:51,360 didn't have any of those variable names 7220 05:08:51,360 --> 05:08:53,360 storage slots 0 1 and two would still 7221 05:08:53,360 --> 05:08:55,400 get updated now here's where things can 7222 05:08:55,400 --> 05:08:57,680 get really interesting let's delete our 7223 05:08:57,680 --> 05:09:00,440 contract a again and let's change the 7224 05:09:00,440 --> 05:09:04,080 type of our contract A's first value to 7225 05:09:04,080 --> 05:09:07,558 from a uent to a Boolean let's save that 7226 05:09:07,558 --> 05:09:10,320 and now let's deploy contract a 7227 05:09:10,320 --> 05:09:12,680 now when we call set vars on our 7228 05:09:12,680 --> 05:09:16,120 contract a it's still going to use the 7229 05:09:16,120 --> 05:09:19,000 set vars function of contract B which 7230 05:09:19,000 --> 05:09:21,400 takes a uint and assigns the first 7231 05:09:21,400 --> 05:09:24,120 storage slot that number we pass it but 7232 05:09:24,120 --> 05:09:26,958 our first storage slot is now a Boolean 7233 05:09:26,958 --> 05:09:28,040 so what do you think's going to happen 7234 05:09:28,040 --> 05:09:30,200 now well let's try it out let's copy 7235 05:09:30,200 --> 05:09:33,120 contract B's address paste it in here 7236 05:09:33,120 --> 05:09:35,320 we'll pass we'll do 7237 05:09:35,320 --> 05:09:38,040 222 as our input parameter we'll hit set 7238 05:09:38,040 --> 05:09:41,000 bars our transaction actually does go 7239 05:09:41,000 --> 05:09:42,600 through and now when we look at first 7240 05:09:42,600 --> 05:09:46,558 value it says true huh that's really 7241 05:09:46,558 --> 05:09:49,558 weird what if we change set bars to zero 7242 05:09:49,558 --> 05:09:53,400 and hit set bars and now first value is 7243 05:09:53,400 --> 05:09:55,840 false in storage here when we add a 7244 05:09:55,840 --> 05:09:58,320 number with set vars it's going through 7245 05:09:58,320 --> 05:10:00,320 because it's just setting the storage 7246 05:10:00,320 --> 05:10:03,718 slot of the Boolean to a number and when 7247 05:10:03,718 --> 05:10:07,040 solidity reads it it goes oh well first 7248 05:10:07,040 --> 05:10:09,040 value is a Boolean so if it's anything 7249 05:10:09,040 --> 05:10:11,400 other than zero it's going to be true so 7250 05:10:11,400 --> 05:10:12,480 this is how you can actually get some 7251 05:10:12,480 --> 05:10:14,718 really weird results if your typings are 7252 05:10:14,718 --> 05:10:16,440 different or if your storage varials are 7253 05:10:16,440 --> 05:10:17,920 different what if we made this an 7254 05:10:17,920 --> 05:10:20,120 address so this is where working with 7255 05:10:20,120 --> 05:10:22,080 delegate call can get really weird and 7256 05:10:22,080 --> 05:10:25,520 really tricky really 7257 05:10:28,440 --> 05:10:31,440 fast all right now with all this being 7258 05:10:31,440 --> 05:10:33,718 said let's turn up the heat and let me 7259 05:10:33,718 --> 05:10:36,320 show you a small proxy a minimal proxy 7260 05:10:36,320 --> 05:10:40,000 example that shows how a contract can be 7261 05:10:40,000 --> 05:10:42,120 used as a singular address but the 7262 05:10:42,120 --> 05:10:45,878 underlying code can actually change and 7263 05:10:45,878 --> 05:10:47,040 all the code we're going to be working 7264 05:10:47,040 --> 05:10:49,680 with once again in the hard Hut upgrades 7265 05:10:49,680 --> 05:10:55,000 FCC subl lesson small proxy.com 7266 05:11:09,680 --> 05:11:11,200 if you want to just move on to learning 7267 05:11:11,200 --> 05:11:12,958 how to actually build these proxies 7268 05:11:12,958 --> 05:11:14,160 without really understanding what's 7269 05:11:14,160 --> 05:11:16,080 going on behind the scenes however it is 7270 05:11:16,080 --> 05:11:17,718 still really powerful if you do 7271 05:11:17,718 --> 05:11:18,878 understand what's going on behind the 7272 05:11:18,878 --> 05:11:20,840 scenes so I have this minimalistic 7273 05:11:20,840 --> 05:11:23,080 starting position right here I have 7274 05:11:23,080 --> 05:11:26,120 small proxy is proxy and I'm importing 7275 05:11:26,120 --> 05:11:29,000 this proxy doou thing from open Zeppelin 7276 05:11:29,000 --> 05:11:31,280 open Zeppelin has this minimalistic 7277 05:11:31,280 --> 05:11:33,400 proxy contract that we can use to 7278 05:11:33,400 --> 05:11:34,520 actually start working with this 7279 05:11:34,520 --> 05:11:36,480 delegate call now this contract uses a 7280 05:11:36,480 --> 05:11:38,120 lot of assembly or what's something 7281 05:11:38,120 --> 05:11:40,360 called Ule and it's an intermediate 7282 05:11:40,360 --> 05:11:42,040 language that can be compiled to bite 7283 05:11:42,040 --> 05:11:44,400 code for different backends it's a sort 7284 05:11:44,400 --> 05:11:47,520 of inline assembly inside solidity and 7285 05:11:47,520 --> 05:11:49,558 allows you to write really really lowl 7286 05:11:49,558 --> 05:11:52,000 code close to the op codes now we're not 7287 05:11:52,000 --> 05:11:53,718 going to go over Ule but I'll leave some 7288 05:11:53,718 --> 05:11:55,798 links to the Ule documentation if you 7289 05:11:55,798 --> 05:11:57,840 want to learn more even if you're a 7290 05:11:57,840 --> 05:12:00,280 really Advanced user you really want to 7291 05:12:00,280 --> 05:12:02,878 try to use as little Ule as possible 7292 05:12:02,878 --> 05:12:05,480 because since it is so much lower level 7293 05:12:05,480 --> 05:12:08,480 it is much easier to screw things up 7294 05:12:08,480 --> 05:12:09,958 however like I said for this example we 7295 05:12:09,958 --> 05:12:11,760 are going to be using a little bit of 7296 05:12:11,760 --> 05:12:13,798 Ule now in this proy that we're going to 7297 05:12:13,798 --> 05:12:16,520 be doing we have this delegate function 7298 05:12:16,520 --> 05:12:19,320 which inside this inline assembly which 7299 05:12:19,320 --> 05:12:21,840 is Ule it does a whole lot of really 7300 05:12:21,840 --> 05:12:23,680 lowlevel stuff but the main thing that 7301 05:12:23,680 --> 05:12:25,360 it does is it goes ahead and it does 7302 05:12:25,360 --> 05:12:27,280 this delegate call functionality if we 7303 05:12:27,280 --> 05:12:30,320 look here we can see it's using a 7304 05:12:30,320 --> 05:12:32,920 fallback function and a receive function 7305 05:12:32,920 --> 05:12:35,440 so whatever it receives a function it 7306 05:12:35,440 --> 05:12:37,958 doesn't recognize it'll call fallback 7307 05:12:37,958 --> 05:12:41,480 and fallback calls are delegate function 7308 05:12:41,480 --> 05:12:44,400 so anytime a proxy contract receives 7309 05:12:44,400 --> 05:12:46,000 data for a function it doesn't 7310 05:12:46,000 --> 05:12:49,120 recognizes it sends it over to some 7311 05:12:49,120 --> 05:12:51,320 implementation to some implementation 7312 05:12:51,320 --> 05:12:53,040 contract where it will call it with 7313 05:12:53,040 --> 05:12:55,520 delegate call in our minimalistic 7314 05:12:55,520 --> 05:12:58,040 example here we have a function called 7315 05:12:58,040 --> 05:13:00,440 set implementation which will change 7316 05:13:00,440 --> 05:13:02,080 where those delegate calls are going to 7317 05:13:02,080 --> 05:13:04,240 be sending this could be equivalent to 7318 05:13:04,240 --> 05:13:06,240 like upgrading your smart contract and 7319 05:13:06,240 --> 05:13:09,760 then we have implementation here to read 7320 05:13:09,760 --> 05:13:12,000 where that implementation contract is 7321 05:13:12,000 --> 05:13:14,280 now to work with proxies we really don't 7322 05:13:14,280 --> 05:13:16,320 want to have anything in storage cuz if 7323 05:13:16,320 --> 05:13:18,160 we do delegate call and that delegate 7324 05:13:18,160 --> 05:13:20,120 call changes some storage we're going to 7325 05:13:20,120 --> 05:13:22,480 screw up our contract's storage the one 7326 05:13:22,480 --> 05:13:24,280 caveat though to this we do still need 7327 05:13:24,280 --> 05:13:26,840 to store that implementation's address 7328 05:13:26,840 --> 05:13:30,240 somewhere so we can call it so EIP 1976 7329 05:13:30,240 --> 05:13:31,920 is called the standard proxy storage 7330 05:13:31,920 --> 05:13:34,200 slot which is an ethereum improvement 7331 05:13:34,200 --> 05:13:36,320 proposal for having certain storage 7332 05:13:36,320 --> 05:13:39,600 slots specifically used for Pro 7333 05:13:39,600 --> 05:13:41,798 and in our minimalistic example here we 7334 05:13:41,798 --> 05:13:44,000 set by 32 private constant 7335 05:13:44,000 --> 05:13:47,360 implementation slot to that location in 7336 05:13:47,360 --> 05:13:49,920 storage and we'll say okay whatever is 7337 05:13:49,920 --> 05:13:53,600 at this storage slot is going to be the 7338 05:13:53,600 --> 05:13:56,440 location of the implementation address 7339 05:13:56,440 --> 05:13:58,320 so the way our proxy is going to work is 7340 05:13:58,320 --> 05:14:00,400 any contract that calls this proxy 7341 05:14:00,400 --> 05:14:02,160 contract if it's not this set 7342 05:14:02,160 --> 05:14:03,638 implementation function it's going to 7343 05:14:03,638 --> 05:14:06,200 pass it over to whatever is inside the 7344 05:14:06,200 --> 05:14:08,280 implementation slot address that's what 7345 05:14:08,280 --> 05:14:09,680 we're going to build here so we have 7346 05:14:09,680 --> 05:14:12,360 this small proxy is proxy and we'll 7347 05:14:12,360 --> 05:14:15,160 create a real minimalistic contract so 7348 05:14:15,160 --> 05:14:17,798 we'll say contract 7349 05:14:17,798 --> 05:14:20,240 implmentation a and we'll just give it a 7350 05:14:20,240 --> 05:14:22,878 un 256 public 7351 05:14:22,878 --> 05:14:27,718 value and then function set value un 256 7352 05:14:27,718 --> 05:14:32,400 new value public we'll say value 7353 05:14:32,400 --> 05:14:35,520 equals new value and so this is going to 7354 05:14:35,520 --> 05:14:38,360 be our implementation so now anytime 7355 05:14:38,360 --> 05:14:40,920 somebody calls small proxy our small 7356 05:14:40,920 --> 05:14:43,080 proxy contract it's going to delegate 7357 05:14:43,080 --> 05:14:45,798 call it it over to our 7358 05:14:45,798 --> 05:14:48,718 implementation a and then save the 7359 05:14:48,718 --> 05:14:51,120 storage in our small proxy address so 7360 05:14:51,120 --> 05:14:53,718 we're going to call our small proxy with 7361 05:14:53,718 --> 05:14:56,520 the data to use this set value function 7362 05:14:56,520 --> 05:14:58,400 selector so let's make it a little 7363 05:14:58,400 --> 05:15:00,080 easier just to figure out how to get 7364 05:15:00,080 --> 05:15:01,718 that data by creating a new helper 7365 05:15:01,718 --> 05:15:03,360 function we'll do 7366 05:15:03,360 --> 05:15:08,680 function get data to transact 7367 05:15:09,200 --> 05:15:12,040 and we can get the data using the ai. 7368 05:15:12,040 --> 05:15:13,558 encode with signature that we learned in 7369 05:15:13,558 --> 05:15:16,440 an earlier lesson so function get data 7370 05:15:16,440 --> 05:15:19,920 to transact we'll pass it to un 256 7371 05:15:19,920 --> 05:15:22,958 number to update so we'll give this the 7372 05:15:22,958 --> 05:15:26,080 number we want to call a new value we'll 7373 05:15:26,080 --> 05:15:28,558 have this be a public pure that's going 7374 05:15:28,558 --> 05:15:31,320 to return a 7375 05:15:31,320 --> 05:15:34,840 bytes memory and we'll just say return 7376 05:15:34,840 --> 05:15:36,680 ai. 7377 05:15:36,680 --> 05:15:39,958 encode with 7378 05:15:39,958 --> 05:15:44,280 signature set value uint 7379 05:15:44,280 --> 05:15:47,798 256 comma number to 7380 05:15:47,798 --> 05:15:50,240 update so you'll remember this from our 7381 05:15:50,240 --> 05:15:52,320 call anything section and if you don't 7382 05:15:52,320 --> 05:15:53,558 remember how to do that remember to 7383 05:15:53,558 --> 05:15:55,760 refer back to our nft section to learn 7384 05:15:55,760 --> 05:15:58,040 how to call anything and use ABI do 7385 05:15:58,040 --> 05:16:00,718 encode ai. encode with it signature and 7386 05:16:00,718 --> 05:16:03,280 call anything with its raw bytes we're 7387 05:16:03,280 --> 05:16:05,680 going to get the data to transact and we 7388 05:16:05,680 --> 05:16:09,480 know that when we call implementation a 7389 05:16:09,480 --> 05:16:10,920 from our small proxy we're going to 7390 05:16:10,920 --> 05:16:13,558 update our small proxy storage so we'll 7391 05:16:13,558 --> 05:16:15,040 create a little function in solidity 7392 05:16:15,040 --> 05:16:18,160 just to read our storage in small proxy 7393 05:16:18,160 --> 05:16:20,400 so we're going to say function read 7394 05:16:20,400 --> 05:16:23,840 storage this will just be a public view 7395 05:16:23,840 --> 05:16:25,200 we'll do 7396 05:16:25,200 --> 05:16:31,520 returns returns you went 256 value at 7397 05:16:31,520 --> 05:16:35,120 storage sloted zero and we are going to 7398 05:16:35,120 --> 05:16:36,638 use a little bit of assembly here since 7399 05:16:36,638 --> 05:16:39,040 we are doing all this low-level stuff 7400 05:16:39,040 --> 05:16:41,280 and we're going to call the SL load op 7401 05:16:41,280 --> 05:16:43,840 code to read the value at storage slot 7402 05:16:43,840 --> 05:16:46,480 0o we'll say value at storage slot 0o 7403 05:16:46,480 --> 05:16:48,600 and we're going to set it and then in 7404 05:16:48,600 --> 05:16:50,400 assembly this is how we set things we're 7405 05:16:50,400 --> 05:16:53,120 going to set it equal to SL load of 7406 05:16:53,120 --> 05:16:55,240 storage slot zero and then it will 7407 05:16:55,240 --> 05:16:58,200 return this value here so we're reading 7408 05:16:58,200 --> 05:16:59,958 directly from Storage oops and then we 7409 05:16:59,958 --> 05:17:02,798 need a little parentheses here sorry so 7410 05:17:02,798 --> 05:17:04,718 now so let's go ahead and deploy our 7411 05:17:04,718 --> 05:17:06,920 small proxy and let's deploy our 7412 05:17:06,920 --> 05:17:10,000 implementation a now our small proxy has 7413 05:17:10,000 --> 05:17:12,600 a function called set implementation so 7414 05:17:12,600 --> 05:17:14,798 we're saying okay anytime we call this 7415 05:17:14,798 --> 05:17:17,040 proxy contract we're going to delegate 7416 05:17:17,040 --> 05:17:20,040 call the functions over to here so we're 7417 05:17:20,040 --> 05:17:22,320 going to grab implementations A's 7418 05:17:22,320 --> 05:17:26,638 address paste it into set implementation 7419 05:17:26,638 --> 05:17:30,480 777 so this is the data of set value un 7420 05:17:30,480 --> 05:17:33,320 256 with that number to update and coded 7421 05:17:33,320 --> 05:17:36,200 in it so if we call our small proxy with 7422 05:17:36,200 --> 05:17:38,558 this data our proxy contract is going to 7423 05:17:38,558 --> 05:17:42,240 go oh okay this is a function uh I don't 7424 05:17:42,240 --> 05:17:44,040 I don't see that function here we're 7425 05:17:44,040 --> 05:17:46,360 going to call our fallback right which 7426 05:17:46,360 --> 05:17:49,120 again is coming from open Zeppelin and 7427 05:17:49,120 --> 05:17:50,480 our fallback is going to do this 7428 05:17:50,480 --> 05:17:52,760 delegate which is this lowlevel stuff 7429 05:17:52,760 --> 05:17:54,160 but it's basically just doing a delegate 7430 05:17:54,160 --> 05:17:55,920 call we're going to call our fallback 7431 05:17:55,920 --> 05:17:58,520 function and then we're going to get the 7432 05:17:58,520 --> 05:18:00,558 function in the implementation a we're 7433 05:18:00,558 --> 05:18:02,320 going to borrow this function and we're 7434 05:18:02,320 --> 05:18:05,600 going to use it on our on ourself so if 7435 05:18:05,600 --> 05:18:07,920 I copy this the implementation has been 7436 05:18:07,920 --> 05:18:09,958 set to being this address down here so 7437 05:18:09,958 --> 05:18:11,638 all the logic is going to be down here 7438 05:18:11,638 --> 05:18:14,280 so when I go ahead and I grab this I 7439 05:18:14,280 --> 05:18:15,920 paste it into call data and I hit 7440 05:18:15,920 --> 05:18:17,718 transact looks like it went successfully 7441 05:18:17,718 --> 05:18:20,600 went through if I read storage now we 7442 05:18:20,600 --> 05:18:22,360 see that it is indeed 7443 05:18:22,360 --> 05:18:25,440 777 which is incredibly exciting now 7444 05:18:25,440 --> 05:18:27,320 this is incredibly beneficial because 7445 05:18:27,320 --> 05:18:29,040 now let's say we want to go and update 7446 05:18:29,040 --> 05:18:31,120 our code right we don't like contract 7447 05:18:31,120 --> 05:18:33,360 implementation anymore so let's go ahead 7448 05:18:33,360 --> 05:18:36,400 copy contract implementation a and we'll 7449 05:18:36,400 --> 05:18:39,718 make a new one called implementation B 7450 05:18:39,718 --> 05:18:41,520 now let's say whenever somebody calls 7451 05:18:41,520 --> 05:18:44,760 set value we do value equals new Value 7452 05:18:44,760 --> 05:18:49,040 Plus 1 or plus two let's go ahead let's 7453 05:18:49,040 --> 05:18:52,320 save this let's compile this and let's 7454 05:18:52,320 --> 05:18:55,480 deploy implementation B we'll grab 7455 05:18:55,480 --> 05:18:58,120 implementation B's contract address 7456 05:18:58,120 --> 05:19:00,440 we'll call it on set implementation in 7457 05:19:00,440 --> 05:19:02,920 our proxy and essentially we have now 7458 05:19:02,920 --> 05:19:05,798 upgraded from implementation a to 7459 05:19:05,798 --> 05:19:09,360 implementation B now if we use this same 7460 05:19:09,360 --> 05:19:11,878 data here we're still going to call set 7461 05:19:11,878 --> 05:19:15,280 value with 777 but instead we're now 7462 05:19:15,280 --> 05:19:18,040 delegate calling to implementation b 7463 05:19:18,040 --> 05:19:21,480 instead of implementation a so if I call 7464 05:19:21,480 --> 05:19:23,638 if I put this data into the lowle call 7465 05:19:23,638 --> 05:19:25,798 data and I hit transa it looks like it 7466 05:19:25,798 --> 05:19:28,638 went through now I read storage and now 7467 05:19:28,638 --> 05:19:29,440 is 7468 05:19:29,440 --> 05:19:32,440 779 since doing value equals new Value 7469 05:19:32,440 --> 05:19:35,520 Plus two so this is a minimalistic 7470 05:19:35,520 --> 05:19:38,480 example of how upgrading actually works 7471 05:19:38,480 --> 05:19:40,400 now now this is incredibly beneficial 7472 05:19:40,400 --> 05:19:42,320 because we can always just tell people 7473 05:19:42,320 --> 05:19:44,320 hey make all your function calls to 7474 05:19:44,320 --> 05:19:46,958 small proxy and you'll be good to go but 7475 05:19:46,958 --> 05:19:49,558 like I said before this also means that 7476 05:19:49,558 --> 05:19:51,400 the developers of this protocol can 7477 05:19:51,400 --> 05:19:54,000 essentially change the underlying logic 7478 05:19:54,000 --> 05:19:55,920 at any time this is why it is so 7479 05:19:55,920 --> 05:19:58,360 important to be sure to read contracts 7480 05:19:58,360 --> 05:20:00,558 and check to see who has the developer 7481 05:20:00,558 --> 05:20:02,958 keys and if a contract can be updated if 7482 05:20:02,958 --> 05:20:04,718 a contract can be updated and a single 7483 05:20:04,718 --> 05:20:06,600 person can update it well guess what you 7484 05:20:06,600 --> 05:20:08,240 have a single centralized point of 7485 05:20:08,240 --> 05:20:09,958 failure and technically the contract 7486 05:20:09,958 --> 05:20:12,400 isn't even decentralized now something 7487 05:20:12,400 --> 05:20:14,360 else I was talking about in the video is 7488 05:20:14,360 --> 05:20:17,000 function clashes function selector 7489 05:20:17,000 --> 05:20:19,600 clashes right now whenever we call set 7490 05:20:19,600 --> 05:20:22,080 implementation the proxy function set 7491 05:20:22,080 --> 05:20:23,600 implementation gets called because we 7492 05:20:23,600 --> 05:20:25,040 don't trigger the fallback because we 7493 05:20:25,040 --> 05:20:26,920 can see the function is here however if 7494 05:20:26,920 --> 05:20:28,120 I have a function called set 7495 05:20:28,120 --> 05:20:30,798 implementation in our implementation 7496 05:20:30,798 --> 05:20:32,878 this one can never be called whenever we 7497 05:20:32,878 --> 05:20:34,878 send a function signature of set 7498 05:20:34,878 --> 05:20:36,840 implementation it's always going to call 7499 05:20:36,840 --> 05:20:38,760 the one on the proxy this is this is 7500 05:20:38,760 --> 05:20:40,120 where the transparent proxy that we're 7501 05:20:40,120 --> 05:20:41,638 going to be working with can help us out 7502 05:20:41,638 --> 05:20:43,520 here and the up Universal upgradeable 7503 05:20:43,520 --> 05:20:45,360 proxy can help us too and I'm not going 7504 05:20:45,360 --> 05:20:47,480 to go too much deeper into these now but 7505 05:20:47,480 --> 05:20:49,200 we've left some links in the GitHub 7506 05:20:49,200 --> 05:20:51,200 repository to teach you more about these 7507 05:20:51,200 --> 05:20:53,878 selector clashes and how those two proxy 7508 05:20:53,878 --> 05:20:55,280 patterns that I just mentioned the 7509 05:20:55,280 --> 05:20:57,440 transparent and Universal upgradeable 7510 05:20:57,440 --> 05:20:59,240 can get around these if you're confused 7511 05:20:59,240 --> 05:21:00,520 by anything in here go into this 7512 05:21:00,520 --> 05:21:01,878 discussions thread and make a new 7513 05:21:01,878 --> 05:21:03,718 discussion about proxies make a new 7514 05:21:03,718 --> 05:21:05,840 discussion about the assembly about the 7515 05:21:05,840 --> 05:21:08,040 Ule set implementation this is a great 7516 05:21:08,040 --> 05:21:09,760 time to connect with other people taking 7517 05:21:09,760 --> 05:21:12,160 the course and ask questions here cuz I 7518 05:21:12,160 --> 05:21:13,760 know that this is a really Advanced 7519 05:21:13,760 --> 05:21:16,718 section and requires you having gone 7520 05:21:16,718 --> 05:21:18,200 through a lot of those subl lessons that 7521 05:21:18,200 --> 05:21:19,958 we've gone before and if it takes you a 7522 05:21:19,958 --> 05:21:21,480 couple times of playing around with 7523 05:21:21,480 --> 05:21:23,920 solidity and playing around with remix I 7524 05:21:23,920 --> 05:21:26,120 definitely recommend you do so this is a 7525 05:21:26,120 --> 05:21:28,120 section where seeing really is believing 7526 05:21:28,120 --> 05:21:29,840 and I want you to jump into remix and I 7527 05:21:29,840 --> 05:21:31,360 want you to test this and I want you to 7528 05:21:31,360 --> 05:21:33,280 play around with this and see what you 7529 05:21:33,280 --> 05:21:37,280 can break and Fiddle with 7530 05:21:56,160 --> 05:21:57,400 all right so now that we've learned all 7531 05:21:57,400 --> 05:21:59,440 about this delegate call function and 7532 05:21:59,440 --> 05:22:01,280 what it can do and the power of it let's 7533 05:22:01,280 --> 05:22:03,160 actually put it to use let's actually 7534 05:22:03,160 --> 05:22:06,120 build an upgradeable contract example so 7535 05:22:06,120 --> 05:22:09,160 we can really see this power so let's go 7536 05:22:09,160 --> 05:22:10,520 ahead and let's start doing this of 7537 05:22:10,520 --> 05:22:12,040 course all the code that we're going to 7538 05:22:12,040 --> 05:22:13,798 be going over with is available in the 7539 05:22:13,798 --> 05:22:15,638 GitHub repo associated with this course 7540 05:22:15,638 --> 05:22:17,718 if you want to follow along so let's go 7541 05:22:17,718 --> 05:22:21,080 ahead make a new directory boundary 7542 05:22:21,080 --> 05:22:22,680 upgrades 7543 05:22:22,680 --> 05:22:27,600 f23 let's code boundary upgrades f23 or 7544 05:22:27,600 --> 05:22:30,440 file open folder you know the drill now 7545 05:22:30,440 --> 05:22:31,840 I know in that video we talked a lot 7546 05:22:31,840 --> 05:22:34,440 about this transparent proxy pattern 7547 05:22:34,440 --> 05:22:36,440 versus the uup UPS we're actually going 7548 05:22:36,440 --> 05:22:40,040 to be teaching the U PS Pro in The UU PS 7549 05:22:40,040 --> 05:22:42,760 proxy the upgrade is handled by the 7550 05:22:42,760 --> 05:22:44,920 implementation and can eventually be 7551 05:22:44,920 --> 05:22:47,520 removed and this is incredibly important 7552 05:22:47,520 --> 05:22:50,520 especially if we want to heed my warning 7553 05:22:50,520 --> 05:22:52,600 I made at the beginning where we want to 7554 05:22:52,600 --> 05:22:54,480 really do this as little as possible and 7555 05:22:54,480 --> 05:22:57,440 this is kind of a crutch to get started 7556 05:22:57,440 --> 05:22:59,400 and we've seen issues where if you have 7557 05:22:59,400 --> 05:23:00,680 upgradeability and you have a 7558 05:23:00,680 --> 05:23:02,840 centralized entity that can upgrade and 7559 05:23:02,840 --> 05:23:04,798 change your contracts those are often 7560 05:23:04,798 --> 05:23:07,798 exploited so the U UPS proxy allows us 7561 05:23:07,798 --> 05:23:10,840 to eventually remove the upgradeability 7562 05:23:10,840 --> 05:23:13,440 so that the code is truly immutable and 7563 05:23:13,440 --> 05:23:15,638 also U ups proxies are technically a 7564 05:23:15,638 --> 05:23:17,600 little bit cheaper to deploy you can 7565 05:23:17,600 --> 05:23:19,638 read more about the differences between 7566 05:23:19,638 --> 05:23:21,558 the two proxy types in the documentation 7567 05:23:21,558 --> 05:23:23,240 for open Zeppelin and additionally if 7568 05:23:23,240 --> 05:23:25,080 you go to the contract wizard in open 7569 05:23:25,080 --> 05:23:28,360 Zeppelin most of these types in here 7570 05:23:28,360 --> 05:23:31,680 have a upgradeability option with 7571 05:23:31,680 --> 05:23:34,718 ufs that we could even open in remix and 7572 05:23:34,718 --> 05:23:36,080 see exactly what some of these might 7573 05:23:36,080 --> 05:23:37,840 look like so we're going to be going 7574 05:23:37,840 --> 05:23:39,200 through that with our our own example 7575 05:23:39,200 --> 05:23:41,080 here so we're going to go ahead we're in 7576 05:23:41,080 --> 05:23:44,558 our folder we're going to do Forge 7577 05:23:44,558 --> 05:23:47,600 knit of course and we're going to go and 7578 05:23:47,600 --> 05:23:50,520 delete these once again bye goodbye 7579 05:23:50,520 --> 05:23:54,000 goodbye delete with the trash I held 7580 05:23:54,000 --> 05:23:56,280 down command so I can select all three 7581 05:23:56,280 --> 05:23:59,120 and let's create a very minimal contract 7582 05:23:59,120 --> 05:24:00,558 that we're going to actually upgrade so 7583 05:24:00,558 --> 05:24:03,520 we're going to say a box V1 do soul and 7584 05:24:03,520 --> 05:24:04,920 this is going to be what we're going to 7585 05:24:04,920 --> 05:24:08,120 upgrade so in this box V1 we're going to 7586 05:24:08,120 --> 05:24:09,520 do 7587 05:24:09,520 --> 05:24:11,240 spdx 7588 05:24:11,240 --> 05:24:15,480 license identify MIT you know the 7589 05:24:15,480 --> 05:24:20,120 drill pragma solidity 0.8 r with a 7590 05:24:20,120 --> 05:24:21,520 little carrot 7591 05:24:21,520 --> 05:24:26,680 here contract box V1 like this cool now 7592 05:24:26,680 --> 05:24:29,120 so since we're using the U UPS proxy 7593 05:24:29,120 --> 05:24:31,798 we're actually going to need to add all 7594 05:24:31,798 --> 05:24:34,320 of the proxy upgradeability into this 7595 05:24:34,320 --> 05:24:36,600 contract as well so we can actually 7596 05:24:36,600 --> 05:24:38,200 borrow from open Zeppelin kind of like 7597 05:24:38,200 --> 05:24:40,280 what we're seeing here because they have 7598 05:24:40,280 --> 05:24:42,040 all of these contracts already created 7599 05:24:42,040 --> 05:24:44,680 for us but let's actually just write out 7600 05:24:44,680 --> 05:24:46,760 what our what we'd want our box to do 7601 05:24:46,760 --> 05:24:48,360 first and then we'll go ahead and do 7602 05:24:48,360 --> 05:24:52,280 that so we'll say a un 256 internal 7603 05:24:52,280 --> 05:24:55,680 value or number a little bit less 7604 05:24:55,680 --> 05:24:58,878 confusing we'll have a function get 7605 05:24:58,878 --> 05:25:02,320 number public view or better yet 7606 05:25:02,320 --> 05:25:07,320 external external view returns un 7607 05:25:07,320 --> 05:25:09,958 256 turn 7608 05:25:09,958 --> 05:25:12,760 number all of this is something you all 7609 05:25:12,760 --> 05:25:15,480 get because you did the stable coin 7610 05:25:15,480 --> 05:25:20,000 video which was a monster of a project 7611 05:25:20,000 --> 05:25:24,920 version external your returns you 7612 05:25:24,920 --> 05:25:29,958 256 return one and that's it this is all 7613 05:25:29,958 --> 05:25:32,280 our box is going to do now let's copy 7614 05:25:32,280 --> 05:25:35,160 this box let's go to our source we'll 7615 05:25:35,160 --> 05:25:38,600 create a box v2. Soul paste all that in 7616 05:25:38,600 --> 05:25:41,520 here and let's change this a little bit 7617 05:25:41,520 --> 05:25:44,160 let's have the version return two get 7618 05:25:44,160 --> 05:25:46,040 number will still return number but 7619 05:25:46,040 --> 05:25:49,638 let's add a function function 7620 05:25:49,638 --> 05:25:53,120 set number un 7621 05:25:53,120 --> 05:25:57,878 256 number this be external null like 7622 05:25:57,878 --> 05:26:00,920 this but so here's oh and this rename 7623 05:26:00,920 --> 05:26:04,638 this to box V2 Okay cool so now we have 7624 05:26:04,638 --> 05:26:08,040 our box V1 we're going to deploy box V1 7625 05:26:08,040 --> 05:26:10,000 and and then at the contract address of 7626 05:26:10,000 --> 05:26:12,920 box V1 we're going to upgrade it to box 7627 05:26:12,920 --> 05:26:16,480 V2 now remember though if we upgraded it 7628 05:26:16,480 --> 05:26:19,360 changed some storage variables we would 7629 05:26:19,360 --> 05:26:21,798 mess it up right so if you go to the op 7630 05:26:21,798 --> 05:26:24,360 zeppin documentation let's actually 7631 05:26:24,360 --> 05:26:27,558 reopen this back up in remix here we can 7632 05:26:27,558 --> 05:26:30,558 see kind of what's going on here so this 7633 05:26:30,558 --> 05:26:32,280 token example that they have they say 7634 05:26:32,280 --> 05:26:35,558 it's initializable upgradeable ownable 7635 05:26:35,558 --> 05:26:38,480 and uu PS upgradeable they say it's all 7636 05:26:38,480 --> 05:26:40,760 this stuff so for us we're going to go 7637 05:26:40,760 --> 05:26:42,840 ahead and we're going to install this 7638 05:26:42,840 --> 05:26:45,120 open Zeppelin contracts upgradeable 7639 05:26:45,120 --> 05:26:46,400 package and this is actually at a 7640 05:26:46,400 --> 05:26:48,040 different open Zeppelin repo we look up 7641 05:26:48,040 --> 05:26:53,240 open Zeppelin contracts upgrade 7642 05:26:57,400 --> 05:27:00,200 GitHub there's this open Zeppelin slopen 7643 05:27:00,200 --> 05:27:02,440 Zeppelin contracts upgradable repository 7644 05:27:02,440 --> 05:27:04,480 that we can use and this is the one that 7645 05:27:04,480 --> 05:27:06,040 we're going to want to install so we're 7646 05:27:06,040 --> 05:27:11,000 going to copy this like that Forge 7647 05:27:11,000 --> 05:27:16,240 install this this D- no- 7648 05:27:18,040 --> 05:27:20,240 commit all right great we've installed 7649 05:27:20,240 --> 05:27:22,320 it now we can start using these 7650 05:27:22,320 --> 05:27:24,760 contracts upgradable code in here let's 7651 05:27:24,760 --> 05:27:28,680 just go in here do a rem mappings equals 7652 05:27:28,680 --> 05:27:33,840 we'll say at open Zeppelin SLC 7653 05:27:33,840 --> 05:27:36,120 contracts 7654 05:27:36,120 --> 05:27:39,520 upgrade a 7655 05:27:39,520 --> 05:27:42,718 equals lib 7656 05:27:42,718 --> 05:27:45,760 slop dein and let's do a toggle word 7657 05:27:45,760 --> 05:27:50,520 wrap Zeppelin contracts gradable SLC 7658 05:27:50,520 --> 05:27:52,400 contracts hopefully that looks right 7659 05:27:52,400 --> 05:27:54,558 let's go to lib opens up a contracts 7660 05:27:54,558 --> 05:27:56,280 upgradable yep that looks right okay 7661 05:27:56,280 --> 05:27:59,558 cool so now we can start using this so 7662 05:27:59,558 --> 05:28:01,600 in this remix example it can be a little 7663 05:28:01,600 --> 05:28:02,878 bit confusing because there's a lot of 7664 05:28:02,878 --> 05:28:05,480 imports going on right so let's kind of 7665 05:28:05,480 --> 05:28:07,638 break this down so the first thing that 7666 05:28:07,638 --> 05:28:09,160 we're going to need need is going to be 7667 05:28:09,160 --> 05:28:12,520 this U UPS upgradable contract and I'm 7668 05:28:12,520 --> 05:28:14,080 just going to copy this line feel free 7669 05:28:14,080 --> 05:28:16,320 to copy this line as well or you know 7670 05:28:16,320 --> 05:28:18,000 write it out yourself and I'm going to 7671 05:28:18,000 --> 05:28:20,400 toggle the word wrap here of course 7672 05:28:20,400 --> 05:28:22,680 we're going to do named Imports because 7673 05:28:22,680 --> 05:28:25,360 we're good Engineers uh what did I mess 7674 05:28:25,360 --> 05:28:28,440 up oh I don't need this versioning okay 7675 05:28:28,440 --> 05:28:30,600 cool and if we control click into this 7676 05:28:30,600 --> 05:28:32,160 or we command click into this or we just 7677 05:28:32,160 --> 05:28:35,320 open this we can see exactly what is in 7678 05:28:35,320 --> 05:28:39,958 this upgradeable code here we have this 7679 05:28:39,958 --> 05:28:42,520 uyu PS upgradable contract it's 7680 05:28:42,520 --> 05:28:45,400 initializable it's some proxy stuff you 7681 05:28:45,400 --> 05:28:48,958 know yada yada yada we scroll down got 7682 05:28:48,958 --> 05:28:49,798 some 7683 05:28:49,798 --> 05:28:53,280 modifiers it has this function upgrade 7684 05:28:53,280 --> 05:28:56,280 to address new implementation upgrade to 7685 05:28:56,280 --> 05:28:59,280 and call and it has this Gap at the 7686 05:28:59,280 --> 05:29:01,878 bottom so let's talk about these so this 7687 05:29:01,878 --> 05:29:05,160 upgrade 2 function this is the function 7688 05:29:05,160 --> 05:29:07,480 that gets called when we want to upgrade 7689 05:29:07,480 --> 05:29:11,760 our box so we're going to say our box is 7690 05:29:11,760 --> 05:29:14,520 uus U UPS upgradeable and then it's 7691 05:29:14,520 --> 05:29:16,320 going to say hey uh should be marked 7692 05:29:16,320 --> 05:29:18,920 abstract because we don't have all of 7693 05:29:18,920 --> 05:29:21,400 the functions defined in here and this 7694 05:29:21,400 --> 05:29:22,760 contract is actually known as an 7695 05:29:22,760 --> 05:29:25,120 abstract contract abstract contracts 7696 05:29:25,120 --> 05:29:27,160 have some functions defined and some 7697 05:29:27,160 --> 05:29:29,320 functions undefined and they expect 7698 05:29:29,320 --> 05:29:32,798 their child classes to implement those 7699 05:29:32,798 --> 05:29:35,200 functions so for example at the bottom 7700 05:29:35,200 --> 05:29:37,080 here we have this authorized upgrade 7701 05:29:37,080 --> 05:29:39,920 function which isn't defined so an 7702 05:29:39,920 --> 05:29:41,718 abstract contact basically says hey you 7703 05:29:41,718 --> 05:29:43,958 better Define this authorized upgrade 7704 05:29:43,958 --> 05:29:45,558 function because I'm not going to do it 7705 05:29:45,558 --> 05:29:47,200 you need to do this and this can be 7706 05:29:47,200 --> 05:29:48,878 helpful for a lot of reasons right for 7707 05:29:48,878 --> 05:29:50,680 example let's say you want to make sure 7708 05:29:50,680 --> 05:29:53,638 to not forget to add some function so 7709 05:29:53,638 --> 05:29:54,600 this is something that we're going to 7710 05:29:54,600 --> 05:29:56,558 need to Define these are how we're going 7711 05:29:56,558 --> 05:29:58,878 to actually do the upgrades we see that 7712 05:29:58,878 --> 05:30:00,360 this is you know obviously the function 7713 05:30:00,360 --> 05:30:01,760 that we're going to have to Define so we 7714 05:30:01,760 --> 05:30:05,200 can add authorization parameters in here 7715 05:30:05,200 --> 05:30:06,958 such as who can actually do the 7716 05:30:06,958 --> 05:30:08,840 authorization so if I toggle the word 7717 05:30:08,840 --> 05:30:10,360 wrap we can see function that should 7718 05:30:10,360 --> 05:30:12,240 revert when message. sender is not 7719 05:30:12,240 --> 05:30:14,400 authorized to upgrade the contract so 7720 05:30:14,400 --> 05:30:16,480 for example if you wanted your proxy to 7721 05:30:16,480 --> 05:30:19,718 be owned by a dow you would update this 7722 05:30:19,718 --> 05:30:22,080 to be ownable or only owner or something 7723 05:30:22,080 --> 05:30:24,000 like that but then they call this 7724 05:30:24,000 --> 05:30:26,360 upgrade to and call U UPS which if we 7725 05:30:26,360 --> 05:30:28,920 control click into or we just go to 7726 05:30:28,920 --> 05:30:31,400 where it's defined uh we toggle the word 7727 05:30:31,400 --> 05:30:33,400 wrap here we can see this performs the 7728 05:30:33,400 --> 05:30:34,920 implementation upgrade with Security 7729 05:30:34,920 --> 05:30:36,958 checks for ups proxies and additional 7730 05:30:36,958 --> 05:30:40,240 setup so if we scroll into here this is 7731 05:30:40,240 --> 05:30:42,558 the code that's actually doing the 7732 05:30:42,558 --> 05:30:45,840 upgrade and it's really low level right 7733 05:30:45,840 --> 05:30:48,120 so we're call this function set 7734 05:30:48,120 --> 05:30:51,798 implementation which now routes all 7735 05:30:51,798 --> 05:30:53,798 contract calls via delegate call to this 7736 05:30:53,798 --> 05:30:56,000 new implementation address that we set 7737 05:30:56,000 --> 05:30:57,638 and then it does a couple of checks in 7738 05:30:57,638 --> 05:30:59,440 here and then it calls upgrade to and 7739 05:30:59,440 --> 05:31:02,600 call which which calls upgrade two which 7740 05:31:02,600 --> 05:31:03,760 call set up 7741 05:31:03,760 --> 05:31:06,160 implementation which mainly just sets 7742 05:31:06,160 --> 05:31:09,120 the implementation slot which basically 7743 05:31:09,120 --> 05:31:10,920 like I was saying before it says okay 7744 05:31:10,920 --> 05:31:12,638 all contracts are going to be routed to 7745 05:31:12,638 --> 05:31:15,558 this address now right but if we go all 7746 05:31:15,558 --> 05:31:18,000 the way back all the way back that's 7747 05:31:18,000 --> 05:31:19,680 kind of the long of what this upgrade 2 7748 05:31:19,680 --> 05:31:21,440 is going to do right so it's going to 7749 05:31:21,440 --> 05:31:23,440 change where we route all of our 7750 05:31:23,440 --> 05:31:26,400 delegate calls and then if we have any 7751 05:31:26,400 --> 05:31:28,840 data we want to send it'll call this as 7752 05:31:28,840 --> 05:31:30,320 well the data is obviously going to be 7753 05:31:30,320 --> 05:31:32,718 none uh if you just call upgrade to 7754 05:31:32,718 --> 05:31:34,120 anyways I'm getting kind of in the weeds 7755 05:31:34,120 --> 05:31:35,878 here right this is the function that it 7756 05:31:35,878 --> 05:31:38,520 has set up to to do the proxy stuff we 7757 05:31:38,520 --> 05:31:40,760 showed before right this changes the 7758 05:31:40,760 --> 05:31:43,798 implementation address so we obviously 7759 05:31:43,798 --> 05:31:46,080 need this we also need to implement our 7760 05:31:46,080 --> 05:31:48,520 own authorized upgrade function so we 7761 05:31:48,520 --> 05:31:50,760 can just go ahead and copy this and in 7762 05:31:50,760 --> 05:31:54,440 here we'll paste it in here and for us 7763 05:31:54,440 --> 05:31:56,080 uh we need to make this override instead 7764 05:31:56,080 --> 05:31:57,840 of virtual I'm just going to leave it 7765 05:31:57,840 --> 05:32:00,000 blank because I don't want any I don't 7766 05:32:00,000 --> 05:32:02,160 really care I don't want any 7767 05:32:02,160 --> 05:32:04,120 authorization stuff in here right but 7768 05:32:04,120 --> 05:32:05,878 this is where I put like you know 7769 05:32:05,878 --> 05:32:08,638 require or excuse me if you know 7770 05:32:08,638 --> 05:32:11,840 message. sender does not equal owner you 7771 05:32:11,840 --> 05:32:15,878 know revert right right but for us I 7772 05:32:15,878 --> 05:32:17,080 don't really care so I'm just going to 7773 05:32:17,080 --> 05:32:18,480 leave it blank right and so this is 7774 05:32:18,480 --> 05:32:21,120 going to say anybody can upgrade this so 7775 05:32:21,120 --> 05:32:22,600 this is why this is going to be the most 7776 05:32:22,600 --> 05:32:24,320 important one right this U UPS 7777 05:32:24,320 --> 05:32:26,440 upgradable is doing all that proxy 7778 05:32:26,440 --> 05:32:28,520 upgrade stuff all right cool so what 7779 05:32:28,520 --> 05:32:31,080 else now you'll also notice here at the 7780 05:32:31,080 --> 05:32:33,958 bottom there is this Gap thing now you 7781 05:32:33,958 --> 05:32:35,840 can read the docs here this empty 7782 05:32:35,840 --> 05:32:37,878 reserve space is put in place to allow 7783 05:32:37,878 --> 05:32:39,520 future versions to add new variables 7784 05:32:39,520 --> 05:32:41,718 without shifting down storage in The 7785 05:32:41,718 --> 05:32:44,400 Inheritance chain what what what what 7786 05:32:44,400 --> 05:32:46,080 the heck what are you talking about 7787 05:32:46,080 --> 05:32:48,240 storage gaps the reason these proxies 7788 05:32:48,240 --> 05:32:52,040 work is that they point to the slot 7789 05:32:52,040 --> 05:32:55,480 number instead of the name of variables 7790 05:32:55,480 --> 05:32:58,200 so if I were to upgrade from box V one 7791 05:32:58,200 --> 05:33:02,440 to box V 2 and I put in another other 7792 05:33:02,440 --> 05:33:06,120 number here guess what if I did this 7793 05:33:06,120 --> 05:33:08,638 upgrade whatever number number was in 7794 05:33:08,638 --> 05:33:11,798 box V one is now set to other number 7795 05:33:11,798 --> 05:33:15,200 instead of number this is because 7796 05:33:15,200 --> 05:33:17,320 proxy's point to storage slots they 7797 05:33:17,320 --> 05:33:19,200 don't point to variable names they point 7798 05:33:19,200 --> 05:33:21,840 to storage slots if we upgrade to V2 7799 05:33:21,840 --> 05:33:24,400 other number is now in storage slot zero 7800 05:33:24,400 --> 05:33:26,680 whereas number was in storage slot zero 7801 05:33:26,680 --> 05:33:29,680 over here right so what is the best 7802 05:33:29,680 --> 05:33:33,360 practice to do is to have this Gap at 7803 05:33:33,360 --> 05:33:37,080 the bottom so a u into 256 you can put 7804 05:33:37,080 --> 05:33:38,798 really whatever you want here which 7805 05:33:38,798 --> 05:33:40,480 basically just says heyy we're going to 7806 05:33:40,480 --> 05:33:43,558 save 50 storage slots in storage for 7807 05:33:43,558 --> 05:33:45,120 adding new variables in the future you 7808 05:33:45,120 --> 05:33:48,878 could have this be you know 100 you know 7809 05:33:48,878 --> 05:33:50,798 whatever whatever amount you want right 7810 05:33:50,798 --> 05:33:52,600 and this is just so that in the future 7811 05:33:52,600 --> 05:33:54,400 if you need to add more storage slots 7812 05:33:54,400 --> 05:33:57,080 you don't collide with other places that 7813 05:33:57,080 --> 05:33:59,280 storage is already being reserved so 7814 05:33:59,280 --> 05:34:00,600 it's really important that they have 7815 05:34:00,600 --> 05:34:04,320 this Gap especially for U UPS if this 7816 05:34:04,320 --> 05:34:06,638 doesn't make sense definitely 100% be 7817 05:34:06,638 --> 05:34:08,520 asking questions about it and you'll see 7818 05:34:08,520 --> 05:34:10,400 a lot of people will ma even manually 7819 05:34:10,400 --> 05:34:12,920 add their own gaps sometimes so 7820 05:34:12,920 --> 05:34:15,280 sometimes you'll even see somebody go 7821 05:34:15,280 --> 05:34:18,080 you know like Gap two or whatever they 7822 05:34:18,080 --> 05:34:19,520 want right so that they have additional 7823 05:34:19,520 --> 05:34:22,120 gaps whatever they want to do cool so 7824 05:34:22,120 --> 05:34:24,760 this is why we need 7825 05:34:25,760 --> 05:34:29,360 this now right like this we could ship 7826 05:34:29,360 --> 05:34:31,798 this code and this would be upgradeable 7827 05:34:31,798 --> 05:34:34,320 now of course box V2 is not upgradeable 7828 05:34:34,320 --> 05:34:37,360 um but box V one would be upgradeable so 7829 05:34:37,360 --> 05:34:38,360 why 7830 05:34:38,360 --> 05:34:40,360 is remix adding all this other stuff 7831 05:34:40,360 --> 05:34:42,760 what is initializable well let's copy 7832 05:34:42,760 --> 05:34:44,680 this and let's paste it in and you can 7833 05:34:44,680 --> 05:34:47,120 also see all this weird initialized 7834 05:34:47,120 --> 05:34:49,000 stuff what what what is going on with 7835 05:34:49,000 --> 05:34:53,280 that well let's let's add this in and of 7836 05:34:53,280 --> 05:34:55,360 course we're going to do a named proxy 7837 05:34:55,360 --> 05:34:57,160 if you want to write this out yourself 7838 05:34:57,160 --> 05:34:59,480 feel free uh excuse me we're going to 7839 05:34:59,480 --> 05:35:01,440 remove this because we did not add the 7840 05:35:01,440 --> 05:35:04,440 version in here uh initializable so what 7841 05:35:04,440 --> 05:35:06,680 is this why is open Zeppelin telling me 7842 05:35:06,680 --> 05:35:08,760 I got do I got to do this initializable 7843 05:35:08,760 --> 05:35:10,160 thing and it's doing all this weird 7844 05:35:10,160 --> 05:35:12,798 initialized stuff so let's go into 7845 05:35:12,798 --> 05:35:14,558 initializable oh and if it doesn't let 7846 05:35:14,558 --> 05:35:18,040 you do that let's open up our file 7847 05:35:18,040 --> 05:35:19,920 explorer we'll look for initializable 7848 05:35:19,920 --> 05:35:23,040 Doo and let's see let's read this so if 7849 05:35:23,040 --> 05:35:24,520 you want to pause and you want to read 7850 05:35:24,520 --> 05:35:25,798 this I'm even going to toggle the word 7851 05:35:25,798 --> 05:35:26,958 wrap if you want to pause and you want 7852 05:35:26,958 --> 05:35:29,558 to read this feel free to do that but 7853 05:35:29,558 --> 05:35:31,200 the main thing we know is this piece 7854 05:35:31,200 --> 05:35:33,920 here since proxy contracts do not make 7855 05:35:33,920 --> 05:35:35,878 use of a Constructor it's common to move 7856 05:35:35,878 --> 05:35:37,638 Constructor logic to an external 7857 05:35:37,638 --> 05:35:39,600 initializer function might be thinking 7858 05:35:39,600 --> 05:35:42,240 wait wait wait wait what they don't use 7859 05:35:42,240 --> 05:35:47,040 a Constructor remember storage is stored 7860 05:35:47,040 --> 05:35:50,600 in the proxy not the 7861 05:35:50,600 --> 05:35:53,200 implementation so we have proxy which 7862 05:35:53,200 --> 05:35:55,440 will direct I'll call to the 7863 05:35:55,440 --> 05:35:58,320 implementation but it's like borrowing 7864 05:35:58,320 --> 05:36:00,400 the implementation functions right so 7865 05:36:00,400 --> 05:36:02,798 all the storage is happening over here 7866 05:36:02,798 --> 05:36:05,440 if the implementation has a Constructor 7867 05:36:05,440 --> 05:36:08,680 and it sets number to one right the 7868 05:36:08,680 --> 05:36:14,280 proxy will have number still set to zero 7869 05:36:14,280 --> 05:36:16,400 cuz the when you deploy a contract The 7870 05:36:16,400 --> 05:36:18,320 Constructor gets called from the 7871 05:36:18,320 --> 05:36:20,840 implementation so proxy contracts or 7872 05:36:20,840 --> 05:36:22,160 contracts that are meant to be used via 7873 05:36:22,160 --> 05:36:24,760 proxy don't use the Constructor so what 7874 05:36:24,760 --> 05:36:28,160 you need to do is then with a proxy you 7875 05:36:28,160 --> 05:36:31,600 need to First you need to deploy 7876 05:36:31,600 --> 05:36:34,760 implementation then you need to call 7877 05:36:34,760 --> 05:36:37,638 some initializer 7878 05:36:37,638 --> 05:36:40,040 function now the initializer function is 7879 05:36:40,040 --> 05:36:43,080 going to be essentially your Constructor 7880 05:36:43,080 --> 05:36:44,638 except for it's going to be called on 7881 05:36:44,638 --> 05:36:46,520 the proxy so that's kind of the 7882 05:36:46,520 --> 05:36:48,680 difference if that doesn't make sense 7883 05:36:48,680 --> 05:36:50,080 please ask questions about it because 7884 05:36:50,080 --> 05:36:53,718 it's really important so Constructors 7885 05:36:53,718 --> 05:36:56,480 are not used in proxy contracts so what 7886 05:36:56,480 --> 05:36:59,360 a lot of upgradable contracts do is they 7887 05:36:59,360 --> 05:37:01,600 first they call this disable 7888 05:37:01,600 --> 05:37:03,798 initializers function in Constructors 7889 05:37:03,798 --> 05:37:05,080 like you see here and I'm actually going 7890 05:37:05,080 --> 05:37:07,680 to copy this paste it into my box View 7891 05:37:07,680 --> 05:37:12,600 one like this and then often you'll see 7892 05:37:12,600 --> 05:37:15,000 this comment I'm going to paste this 7893 05:37:15,000 --> 05:37:17,638 over here because sometimes different 7894 05:37:17,638 --> 05:37:19,840 lters will say hey you're using a 7895 05:37:19,840 --> 05:37:22,480 Constructor in a upgradeable contract 7896 05:37:22,480 --> 05:37:24,240 you shouldn't do that so we just say hey 7897 05:37:24,240 --> 05:37:26,680 like let it happen right what this 7898 05:37:26,680 --> 05:37:28,958 disable initializers function does is it 7899 05:37:28,958 --> 05:37:31,878 says hey don't let any initialization 7900 05:37:31,878 --> 05:37:35,440 happen so if we go see this we say 7901 05:37:35,440 --> 05:37:37,558 require that you're not initializing 7902 05:37:37,558 --> 05:37:39,520 right you can't call any initializer 7903 05:37:39,520 --> 05:37:42,558 functions so you could just as easily 7904 05:37:42,558 --> 05:37:45,040 just just do this right they're kind of 7905 05:37:45,040 --> 05:37:47,920 the same but this is much more of verose 7906 05:37:47,920 --> 05:37:49,760 right this is making sure hey don't do 7907 05:37:49,760 --> 05:37:52,200 any initializers Constructor don't do 7908 05:37:52,200 --> 05:37:55,080 anything so this and this are almost the 7909 05:37:55,080 --> 05:37:57,480 same but this is more verbose as you 7910 05:37:57,480 --> 05:38:00,080 know verbose is better but let's say we 7911 05:38:00,080 --> 05:38:02,558 do want a Constructor what we would do 7912 05:38:02,558 --> 05:38:05,558 is we' add an initializer so we do 7913 05:38:05,558 --> 05:38:09,000 something like function 7914 05:38:11,718 --> 05:38:15,760 initialize public initializer 7915 05:38:15,760 --> 05:38:18,400 initi 7916 05:38:18,400 --> 05:38:20,320 initializer and of course we'd say our 7917 05:38:20,320 --> 05:38:23,680 boxy one is initializable public 7918 05:38:23,680 --> 05:38:26,958 initializer like this and we would add 7919 05:38:26,958 --> 05:38:28,840 whatever we want to initialize and then 7920 05:38:28,840 --> 05:38:31,000 what we're going to do is we're going to 7921 05:38:31,000 --> 05:38:34,320 deploy the contract and then we're going 7922 05:38:34,320 --> 05:38:36,558 to have the proxy immediately call our 7923 05:38:36,558 --> 05:38:38,718 initialized function instead of doing 7924 05:38:38,718 --> 05:38:40,440 this Constructor instead of the 7925 05:38:40,440 --> 05:38:42,120 implementation having the storage we 7926 05:38:42,120 --> 05:38:44,840 want the proxy to have the storage 7927 05:38:44,840 --> 05:38:46,680 typically you'll see initializer 7928 05:38:46,680 --> 05:38:49,878 functions have two underscores like this 7929 05:38:49,878 --> 05:38:52,120 so if we want our box V1 to have an 7930 05:38:52,120 --> 05:38:56,360 owner right we can't set storage we 7931 05:38:56,360 --> 05:38:58,840 can't do like owner equals message. 7932 05:38:58,840 --> 05:39:01,360 sender in the Constructor like we 7933 05:39:01,360 --> 05:39:05,360 normally do with a ownable contract 7934 05:39:05,360 --> 05:39:07,680 instead we need to put that in the 7935 05:39:07,680 --> 05:39:10,120 initializer function so what a lot of 7936 05:39:10,120 --> 05:39:13,400 people will do is they'll add double 7937 05:39:13,400 --> 05:39:16,920 underscore like ownable uncore in it 7938 05:39:16,920 --> 05:39:18,240 like let's say we did want this to be 7939 05:39:18,240 --> 05:39:19,958 upgrade uh ownable and actually let's go 7940 05:39:19,958 --> 05:39:21,718 ahead and add that right now we're going 7941 05:39:21,718 --> 05:39:23,798 to add this ownable upgradeable I'm 7942 05:39:23,798 --> 05:39:25,440 going to paste it 7943 05:39:25,440 --> 05:39:30,958 in remove that ownable from like this 7944 05:39:30,958 --> 05:39:33,558 comma I know I kind of was quick but I 7945 05:39:33,558 --> 05:39:35,160 uh I know you all are Advanced enough to 7946 05:39:35,160 --> 05:39:37,440 kind of know what I just did here I 7947 05:39:37,440 --> 05:39:38,760 Factor this a little bit so that's a 7948 05:39:38,760 --> 05:39:41,320 named import remove the version and now 7949 05:39:41,320 --> 05:39:42,958 we're doing this ownable upgradable 7950 05:39:42,958 --> 05:39:44,400 instead of ownable I'll explain what 7951 05:39:44,400 --> 05:39:46,360 that means in a second oh and this 7952 05:39:46,360 --> 05:39:47,878 actually 7953 05:39:47,878 --> 05:39:51,440 goes here a note if you see this 7954 05:39:51,440 --> 05:39:53,600 linearization of inheritance graph 7955 05:39:53,600 --> 05:39:55,958 impossible this means that we're trying 7956 05:39:55,958 --> 05:39:58,638 to inherit stuff in the wrong order so 7957 05:39:58,638 --> 05:40:00,680 we'll just do this oh is that the right 7958 05:40:00,680 --> 05:40:03,680 order no that's still the wrong order 7959 05:40:03,680 --> 05:40:05,558 that the right order cool that's the 7960 05:40:05,558 --> 05:40:07,798 right order okay so what what you'll see 7961 05:40:07,798 --> 05:40:10,680 is these upgradeable initializable 7962 05:40:10,680 --> 05:40:12,440 functions will be prepended with double 7963 05:40:12,440 --> 05:40:14,600 underscores right and so what this is 7964 05:40:14,600 --> 05:40:17,160 doing is it's sets the owner it's 7965 05:40:17,160 --> 05:40:18,958 setting the owner to message. sender and 7966 05:40:18,958 --> 05:40:21,558 if I command click into this or command 7967 05:40:21,558 --> 05:40:25,520 click into this we look up ownable andit 7968 05:40:25,520 --> 05:40:27,280 we see it does this ownable andit 7969 05:40:27,280 --> 05:40:29,320 Unchained all it's doing is calling 7970 05:40:29,320 --> 05:40:31,958 transfer ownership right that's it this 7971 05:40:31,958 --> 05:40:34,200 is essentially the same as setting owner 7972 05:40:34,200 --> 05:40:37,120 equals message. sender right but we're 7973 05:40:37,120 --> 05:40:38,478 just just prepending with the double 7974 05:40:38,478 --> 05:40:39,798 underscore to say hey this is an 7975 05:40:39,798 --> 05:40:42,320 initializer function treated as such 7976 05:40:42,320 --> 05:40:43,958 right it should only be called in this 7977 05:40:43,958 --> 05:40:46,760 initialize section additionally most of 7978 05:40:46,760 --> 05:40:49,638 the time you'll see this initializer 7979 05:40:49,638 --> 05:40:51,400 function which doesn't do anything but 7980 05:40:51,400 --> 05:40:53,000 it's best practice to have this in here 7981 05:40:53,000 --> 05:40:56,160 to say hey this is a u UPS upgradeable 7982 05:40:56,160 --> 05:40:58,878 contract we're going to treat it as such 7983 05:40:58,878 --> 05:41:00,638 okay so you'll see this in here a lot 7984 05:41:00,638 --> 05:41:02,958 too but I know I kind of went through 7985 05:41:02,958 --> 05:41:04,280 this a little bit quickly but let's just 7986 05:41:04,280 --> 05:41:06,840 do a quick recap of what we learned we 7987 05:41:06,840 --> 05:41:09,760 can't use Constructors with our proxies 7988 05:41:09,760 --> 05:41:12,558 a Constructor adds storage to the 7989 05:41:12,558 --> 05:41:16,520 implementation and if we have a proxy 7990 05:41:16,520 --> 05:41:19,280 pointing to an implementation the 7991 05:41:19,280 --> 05:41:20,798 implementation would have the storage of 7992 05:41:20,798 --> 05:41:22,760 where the owner is and we need the proxy 7993 05:41:22,760 --> 05:41:24,080 to have the storage of where the owner 7994 05:41:24,080 --> 05:41:26,760 is so we need to call initialize through 7995 05:41:26,760 --> 05:41:29,520 the proxy not through the implementation 7996 05:41:29,520 --> 05:41:31,520 the initializer function is essentially 7997 05:41:31,520 --> 05:41:33,760 a Constructor for proxies you can almost 7998 05:41:33,760 --> 05:41:36,558 think of it like that we want to preen 7999 05:41:36,558 --> 05:41:38,280 them with this double underscore to know 8000 05:41:38,280 --> 05:41:39,360 that they're 8001 05:41:39,360 --> 05:41:41,478 initializers and we don't want any 8002 05:41:41,478 --> 05:41:43,920 initializers to run in the Constructor 8003 05:41:43,920 --> 05:41:46,558 and we only want them to run once you'll 8004 05:41:46,558 --> 05:41:49,160 also see this initializer modifier which 8005 05:41:49,160 --> 05:41:51,280 basically just says hey you can only 8006 05:41:51,280 --> 05:41:53,798 initialize one time right a contract can 8007 05:41:53,798 --> 05:41:55,680 only be initialized once otherwise it'll 8008 05:41:55,680 --> 05:41:58,798 error great and that's pretty much going 8009 05:41:58,798 --> 05:42:00,760 to be our box so now we made this 8010 05:42:00,760 --> 05:42:04,240 ownable so since this is ownable down 8011 05:42:04,240 --> 05:42:06,160 here in our authorized upgrade we could 8012 05:42:06,160 --> 05:42:08,760 add an only owner modifier in here so 8013 05:42:08,760 --> 05:42:10,200 that only the owner could upgrade it if 8014 05:42:10,200 --> 05:42:12,320 we wanted to but we're not going to add 8015 05:42:12,320 --> 05:42:13,798 that but I wanted to add this ownable in 8016 05:42:13,798 --> 05:42:15,600 it here just to show you hey like so 8017 05:42:15,600 --> 05:42:16,798 this is what you do instead of working 8018 05:42:16,798 --> 05:42:18,760 with Constructors so that's our box V 8019 05:42:18,760 --> 05:42:22,200 one let's fix our box V two so our box V 8020 05:42:22,200 --> 05:42:23,638 two we're going to do pretty much the 8021 05:42:23,638 --> 05:42:25,040 same thing so we're going to grab all 8022 05:42:25,040 --> 05:42:27,840 these paste it in here I'm going to 8023 05:42:27,840 --> 05:42:30,600 toggle the word wrap we're going to make 8024 05:42:30,600 --> 05:42:33,240 this 8025 05:42:33,240 --> 05:42:35,840 initializable uh ownable 8026 05:42:35,840 --> 05:42:38,520 upgradeable and UPS 8027 05:42:38,520 --> 05:42:41,478 upgradable is like this we're going to 8028 05:42:41,478 --> 05:42:45,478 need to add authorized upgrade which 8029 05:42:45,478 --> 05:42:46,680 we're going to keep blank but if you 8030 05:42:46,680 --> 05:42:48,718 want to play with this you can obviously 8031 05:42:48,718 --> 05:42:50,638 implement this we're going to want to 8032 05:42:50,638 --> 05:42:53,798 add a Constructor and an initializer so 8033 05:42:53,798 --> 05:42:55,520 we're going to copy both of these and 8034 05:42:55,520 --> 05:42:57,280 just paste it right in here a 8035 05:42:57,280 --> 05:42:59,638 Constructor which disables Constructors 8036 05:42:59,638 --> 05:43:01,240 and then an initializer which is going 8037 05:43:01,240 --> 05:43:03,120 to be really what we want and that's 8038 05:43:03,120 --> 05:43:05,240 pretty much it that's the only changes 8039 05:43:05,240 --> 05:43:07,160 that we need to make now we can make 8040 05:43:07,160 --> 05:43:08,760 make this even smaller right we could 8041 05:43:08,760 --> 05:43:10,080 say okay cool we don't care about 8042 05:43:10,080 --> 05:43:11,440 upgrades we don't care about 8043 05:43:11,440 --> 05:43:14,240 initializing and we just say goodbye 8044 05:43:14,240 --> 05:43:15,718 this doesn't need initializer this 8045 05:43:15,718 --> 05:43:17,558 doesn't need any of that stuff and we 8046 05:43:17,558 --> 05:43:19,478 could even delete all this if we wanted 8047 05:43:19,478 --> 05:43:22,718 and now it's just UPS upgradeable right 8048 05:43:22,718 --> 05:43:25,240 without any initializer and without any 8049 05:43:25,240 --> 05:43:26,958 Constructors and we could do the same 8050 05:43:26,958 --> 05:43:28,478 thing with box VI one but I just wanted 8051 05:43:28,478 --> 05:43:30,240 to demo what it look would look like if 8052 05:43:30,240 --> 05:43:32,840 you did want a Constructor but now that 8053 05:43:32,840 --> 05:43:36,440 we've done all this work 8054 05:43:37,920 --> 05:43:40,680 let's actually show you this in action 8055 05:43:40,680 --> 05:43:43,040 so we're going to deploy box V1 we're 8056 05:43:43,040 --> 05:43:44,280 going to get an address we're going to 8057 05:43:44,280 --> 05:43:47,280 use that proxy address to make sure that 8058 05:43:47,280 --> 05:43:49,920 these functions work we're going to then 8059 05:43:49,920 --> 05:43:52,760 deploy box V2 we're going to point our 8060 05:43:52,760 --> 05:43:55,718 proxy to box V2 which will essentially 8061 05:43:55,718 --> 05:43:57,400 mean we upgraded our contract and it's 8062 05:43:57,400 --> 05:44:00,080 all going to be from the same address so 8063 05:44:00,080 --> 05:44:02,600 let's do this we're going to make a 8064 05:44:02,600 --> 05:44:05,000 little two scripts we're going to make 8065 05:44:05,000 --> 05:44:08,600 one called deploy box. s soul and a new 8066 05:44:08,600 --> 05:44:13,638 one called up upgrade box. s.o so we're 8067 05:44:13,638 --> 05:44:15,040 going to deploy the box and then we're 8068 05:44:15,040 --> 05:44:17,718 going to upgrade it so let's deploy it 8069 05:44:17,718 --> 05:44:21,160 spdx you know the drill 8070 05:44:21,160 --> 05:44:25,080 license ragma solidity 8071 05:44:25,080 --> 05:44:30,600 0.818 contract deploy box is script 8072 05:44:30,600 --> 05:44:37,760 import script from Forge STD script. 8073 05:44:37,760 --> 05:44:40,440 so we're going to need to imple 8074 05:44:40,440 --> 05:44:45,040 import box V1 from 8075 05:44:45,040 --> 05:44:50,680 docbox V1 doso function run 8076 05:44:50,680 --> 05:44:54,240 external external returns I'm going to 8077 05:44:54,240 --> 05:44:55,320 I'm going to return address and you'll 8078 05:44:55,320 --> 05:44:58,400 see why in a bit returns address address 8079 05:44:58,400 --> 05:45:03,240 proxy equals deploy box turn proxy and 8080 05:45:03,240 --> 05:45:05,558 I'm going to create a deploy box 8081 05:45:05,558 --> 05:45:07,520 function function deploy box so this is 8082 05:45:07,520 --> 05:45:10,600 a little bit different this will be a 8083 05:45:10,600 --> 05:45:13,840 public returns 8084 05:45:13,840 --> 05:45:15,638 address and what we're going to do is 8085 05:45:15,638 --> 05:45:21,120 say box V1 box equals new box V1 this is 8086 05:45:21,120 --> 05:45:23,680 going to be our what our implementation 8087 05:45:23,680 --> 05:45:26,400 this is going to be our logic right this 8088 05:45:26,400 --> 05:45:29,360 is where our proxy is going to point to 8089 05:45:29,360 --> 05:45:31,478 delegate call right to borrow those 8090 05:45:31,478 --> 05:45:34,558 functions now we're going to need to get 8091 05:45:34,558 --> 05:45:37,200 a proxy on top of this the pro proxy 8092 05:45:37,200 --> 05:45:40,200 that we're going to use is the ERC 8093 05:45:40,200 --> 05:45:42,958 1967 proxy and we can learn more about 8094 05:45:42,958 --> 05:45:45,400 this proxy in the open Zeppelin 8095 05:45:45,400 --> 05:45:47,240 documentation we can learn more about 8096 05:45:47,240 --> 05:45:50,878 this at the EIP for ERC 8097 05:45:50,878 --> 05:45:54,440 1976 whatever you want to do but it's 8098 05:45:54,440 --> 05:45:56,040 going to be this proxy that we're going 8099 05:45:56,040 --> 05:45:59,000 to use to point to our implementation 8100 05:45:59,000 --> 05:46:01,280 right to point to our box V one which 8101 05:46:01,280 --> 05:46:05,478 has all this upgradable logic in it so 8102 05:46:05,478 --> 05:46:07,200 we're going to import that we're going 8103 05:46:07,200 --> 05:46:10,760 to import ERC 8104 05:46:10,760 --> 05:46:14,760 1967 proxy from and this is actually 8105 05:46:14,760 --> 05:46:16,920 just a regular open Zeppelin contract so 8106 05:46:16,920 --> 05:46:21,000 we're going to do Forge install open 8107 05:46:21,000 --> 05:46:25,160 Zeppelin slash open Zeppelin contracts 8108 05:46:25,160 --> 05:46:28,680 this is just a regular one contracts 8109 05:46:28,680 --> 05:46:32,320 oops uh D- no- commit we're going to go 8110 05:46:32,320 --> 05:46:35,638 to our boundary. toml and this is a 8111 05:46:35,638 --> 05:46:37,400 remapping 8112 05:46:37,400 --> 05:46:42,958 comma open Zeppelin SLC contracts equals 8113 05:46:42,958 --> 05:46:46,680 lib slopen 8114 05:46:46,680 --> 05:46:49,920 Zeppelin open Zeppelin Dash contracts I 8115 05:46:49,920 --> 05:46:52,558 think that's what it is right lib open 8116 05:46:52,558 --> 05:46:54,558 Zeppelin contracts and opens up on 8117 05:46:54,558 --> 05:46:56,478 contracts upgradeable okay opens up on 8118 05:46:56,478 --> 05:47:00,558 contracts SLC contracts like this and oh 8119 05:47:00,558 --> 05:47:02,400 Port this from 8120 05:47:02,400 --> 05:47:09,200 atop Zeppelin contracts proxy RC 8121 05:47:09,200 --> 05:47:12,240 1967 uh like this so this is the proxy 8122 05:47:12,240 --> 05:47:15,000 type we're using now we're going to say 8123 05:47:15,000 --> 05:47:21,040 ERC 1967 proxy equals new erc1 8124 05:47:21,040 --> 05:47:24,040 1967 proxy and if we command click onto 8125 05:47:24,040 --> 05:47:26,400 this or control click or just look at 8126 05:47:26,400 --> 05:47:28,798 this this does have a Constructor we 8127 05:47:28,798 --> 05:47:31,760 pass in the logic and the data so we 8128 05:47:31,760 --> 05:47:35,320 basically say hey proxy this address is 8129 05:47:35,320 --> 05:47:37,160 going to be the implementation the logic 8130 05:47:37,160 --> 05:47:39,240 that you're going to use and then any 8131 05:47:39,240 --> 05:47:42,958 data you want to pass to the initializer 8132 05:47:42,958 --> 05:47:45,240 right if we look at upgrade to and call 8133 05:47:45,240 --> 05:47:48,160 call upgrade two and then if there's any 8134 05:47:48,160 --> 05:47:51,760 initializers call that afterwards right 8135 05:47:51,760 --> 05:47:52,920 uh we're not going to have any 8136 05:47:52,920 --> 05:47:54,320 initializer stuff so we're not going to 8137 05:47:54,320 --> 05:47:56,240 do that but we're just going to say 8138 05:47:56,240 --> 05:47:59,360 address box because we just deploy this 8139 05:47:59,360 --> 05:48:02,718 box no data this is where like I said 8140 05:48:02,718 --> 05:48:04,120 this is where we' put the initial add 8141 05:48:04,120 --> 05:48:05,478 stuff we wanted but then we're just 8142 05:48:05,478 --> 05:48:08,680 going to say return and address proxy so 8143 05:48:08,680 --> 05:48:10,400 cool and let's just make sure stuff is 8144 05:48:10,400 --> 05:48:13,040 working Forge 8145 05:48:13,040 --> 05:48:15,080 build or at least we're going to make 8146 05:48:15,080 --> 05:48:18,520 sure code is compiling and source file 8147 05:48:18,520 --> 05:48:20,558 that's not specified required compile 8148 05:48:20,558 --> 05:48:21,600 version that's cuz we haven't 8149 05:48:21,600 --> 05:48:23,280 implemented that that's fine cool this 8150 05:48:23,280 --> 05:48:24,718 looks good and we're going to write some 8151 05:48:24,718 --> 05:48:26,400 tests to show you this actually working 8152 05:48:26,400 --> 05:48:29,680 in a little bit 8153 05:48:31,080 --> 05:48:33,920 but cool so we have a deploy box let's 8154 05:48:33,920 --> 05:48:35,958 actually write an upgrade box script now 8155 05:48:35,958 --> 05:48:37,478 and then we'll write a text 8156 05:48:37,478 --> 05:48:40,840 to show both of them spdx 8157 05:48:40,840 --> 05:48:45,760 license identifier MIT pragma solidity 8158 05:48:45,760 --> 05:48:51,240 0.818 contract upgrade box is script 8159 05:48:51,240 --> 05:48:58,280 import script from forg STD script. Soul 8160 05:48:58,280 --> 05:49:02,520 script scripts cool function run 8161 05:49:02,520 --> 05:49:04,040 external 8162 05:49:04,040 --> 05:49:07,240 returns address 8163 05:49:07,240 --> 05:49:09,400 I'm going to have this 8164 05:49:09,400 --> 05:49:12,320 external oops I'm going to have this use 8165 05:49:12,320 --> 05:49:13,798 the most recently 8166 05:49:13,798 --> 05:49:16,440 deployed box so to do that we're going 8167 05:49:16,440 --> 05:49:17,958 to actually 8168 05:49:17,958 --> 05:49:20,360 install Forge 8169 05:49:20,360 --> 05:49:24,440 install chain Excel org D boundary 8170 05:49:24,440 --> 05:49:27,200 devops D- no- commit like I said there 8171 05:49:27,200 --> 05:49:30,760 might be a better devops tool than this 8172 05:49:30,760 --> 05:49:33,240 you can find the recommended devops tool 8173 05:49:33,240 --> 05:49:35,478 the GitHub repo associated with this 8174 05:49:35,478 --> 05:49:37,240 recommended tool is going to be Foundry 8175 05:49:37,240 --> 05:49:38,920 devops so great so that's what we're 8176 05:49:38,920 --> 05:49:40,760 going to use I've worked with this one 8177 05:49:40,760 --> 05:49:42,680 before so we're going to say address 8178 05:49:42,680 --> 05:49:48,040 most recently deployed equals devops 8179 05:49:48,040 --> 05:49:51,160 tools. getet most 8180 05:49:51,160 --> 05:49:55,718 recently deployed going to be deep me 8181 05:49:55,718 --> 05:49:57,718 and this is where we're going to get the 8182 05:49:57,718 --> 05:49:59,320 ERC 8183 05:49:59,320 --> 05:50:03,878 1976 proxy let me oops toggle the word 8184 05:50:03,878 --> 05:50:08,798 WP here move this down on block dochain 8185 05:50:08,798 --> 05:50:12,920 ID oh let's import that as well import 8186 05:50:12,920 --> 05:50:15,160 from oh oh and I got to go to The 8187 05:50:15,160 --> 05:50:16,440 Foundry 8188 05:50:16,440 --> 05:50:22,080 doo do a little bit from libf Foundry 8189 05:50:22,080 --> 05:50:23,840 devops 8190 05:50:23,840 --> 05:50:29,958 SRC devops tools. s looks good get most 8191 05:50:29,958 --> 05:50:32,718 recently I spelled that wrong get most 8192 05:50:32,718 --> 05:50:35,080 recent deployment so we're going to get 8193 05:50:35,080 --> 05:50:37,160 the most recently deployed 8194 05:50:37,160 --> 05:50:40,600 proxy first and you'll see Y in a second 8195 05:50:40,600 --> 05:50:43,600 and first we're going to actually deploy 8196 05:50:43,600 --> 05:50:48,400 box V2 so we're going to import box V2 8197 05:50:48,400 --> 05:50:54,160 from SRC boox v2. Soul oops s so we're 8198 05:50:54,160 --> 05:50:57,160 going to do a little vm. start 8199 05:50:57,160 --> 05:50:59,400 broadcast did we do that in box deploy 8200 05:50:59,400 --> 05:51:02,760 box oh we should do that here back in 8201 05:51:02,760 --> 05:51:04,240 the deploy box we're going to do vm. 8202 05:51:04,240 --> 05:51:06,718 start broadcast 8203 05:51:06,718 --> 05:51:09,120 and then a stop 8204 05:51:09,120 --> 05:51:12,878 broadcast that okay bm. St broadcast in 8205 05:51:12,878 --> 05:51:16,080 the upgrade box we're going to say box 8206 05:51:16,080 --> 05:51:22,760 V2 new box equals new box V2 bm. stop 8207 05:51:22,760 --> 05:51:25,000 broadcast and then we're going to say 8208 05:51:25,000 --> 05:51:29,520 address proxy equals upgrade box and I'm 8209 05:51:29,520 --> 05:51:33,280 modularizing this for our test cases but 8210 05:51:33,280 --> 05:51:34,520 we're going to say we're going to create 8211 05:51:34,520 --> 05:51:36,080 a new function called upgrade box where 8212 05:51:36,080 --> 05:51:38,520 we're going to pass pass in the proxy 8213 05:51:38,520 --> 05:51:41,120 address right this is the proxy address 8214 05:51:41,120 --> 05:51:44,638 and then the address of our new box and 8215 05:51:44,638 --> 05:51:47,558 then we're going to say return proxy so 8216 05:51:47,558 --> 05:51:48,878 we're going to create this function 8217 05:51:48,878 --> 05:51:53,680 upgrade box which has our previous proxy 8218 05:51:53,680 --> 05:51:56,160 and then the address of our new box for 8219 05:51:56,160 --> 05:51:58,680 us to do so we're going to say function 8220 05:51:58,680 --> 05:52:03,120 upgrade box address proxy address uh 8221 05:52:03,120 --> 05:52:07,638 address new box this be a public returns 8222 05:52:07,638 --> 05:52:12,440 address do vm. start broadcast so okay 8223 05:52:12,440 --> 05:52:14,040 so we're going to call upgrade box we 8224 05:52:14,040 --> 05:52:16,120 have the proxy address and we want to 8225 05:52:16,120 --> 05:52:19,000 call upgrade on this right since this is 8226 05:52:19,000 --> 05:52:21,440 just an address we can't call upgrade on 8227 05:52:21,440 --> 05:52:23,718 an address we're going to need to call 8228 05:52:23,718 --> 05:52:27,478 upgrade on using the ABI of box V1 right 8229 05:52:27,478 --> 05:52:30,558 so let's import that as well import box 8230 05:52:30,558 --> 05:52:36,680 V1 from Sr cbox V1 doou we're going to 8231 05:52:36,680 --> 05:52:41,600 say box V1 proxy equals box V1 of the 8232 05:52:41,600 --> 05:52:44,680 proxy address like this and what we're 8233 05:52:44,680 --> 05:52:48,680 going to do is now that we've given our 8234 05:52:48,680 --> 05:52:52,638 proxy address box V1 ABI we can now just 8235 05:52:52,638 --> 05:52:57,638 call proxy do upgrade 2 since box V1 is 8236 05:52:57,638 --> 05:52:59,878 U UPS upgradeable means it has access to 8237 05:52:59,878 --> 05:53:00,760 this 8238 05:53:00,760 --> 05:53:05,000 function address new box this we're 8239 05:53:05,000 --> 05:53:07,080 going to upgrade it and we're just do 8240 05:53:07,080 --> 05:53:08,920 vm. stop 8241 05:53:08,920 --> 05:53:11,760 broadcast like this we could also 8242 05:53:11,760 --> 05:53:14,120 optionally wrap this in box V2 or we 8243 05:53:14,120 --> 05:53:16,280 could you know do a direct function call 8244 05:53:16,280 --> 05:53:18,080 whatever we want to do now we'll just 8245 05:53:18,080 --> 05:53:19,160 return 8246 05:53:19,160 --> 05:53:20,680 address 8247 05:53:20,680 --> 05:53:23,638 proxy and that's it right so what is 8248 05:53:23,638 --> 05:53:25,200 this doing when we actually upgrade well 8249 05:53:25,200 --> 05:53:26,920 we deploy Box 2 8250 05:53:26,920 --> 05:53:29,440 implementation we are calling this 8251 05:53:29,440 --> 05:53:31,120 upgrade box function and what we're 8252 05:53:31,120 --> 05:53:33,200 doing is all we're doing is we're saying 8253 05:53:33,200 --> 05:53:37,240 hey point the proxy to this new place 8254 05:53:37,240 --> 05:53:40,680 that's it we're saying proxy contract 8255 05:53:40,680 --> 05:53:44,958 now points to this new address that's it 8256 05:53:44,958 --> 05:53:48,360 don't believe me all right let's write a 8257 05:53:49,878 --> 05:53:53,240 test so new file and we're going to 8258 05:53:53,240 --> 05:53:54,520 write some crappy tests here we're just 8259 05:53:54,520 --> 05:53:56,080 going to do the whole Suite just to show 8260 05:53:56,080 --> 05:54:00,440 you the power deploy and upgrade test. 8261 05:54:00,440 --> 05:54:02,878 T.O you know the drill 8262 05:54:02,878 --> 05:54:06,958 spdx license identifier MIT 8263 05:54:06,958 --> 05:54:12,840 contract deploy and upgrade test is test 8264 05:54:12,840 --> 05:54:18,558 import test from Forge STD test. Soul 8265 05:54:18,558 --> 05:54:21,080 fragma solidity 8266 05:54:21,080 --> 05:54:23,120 0.818 we got to do a whole bunch more 8267 05:54:23,120 --> 05:54:25,000 Imports we're going to need to import 8268 05:54:25,000 --> 05:54:30,718 deploy box from do script SL deploy boox 8269 05:54:30,718 --> 05:54:34,120 dos. sole import upgrade box you know 8270 05:54:34,120 --> 05:54:35,920 this is what we're actually testing from 8271 05:54:35,920 --> 05:54:39,840 do/ script D upgrade box. s.o readying 8272 05:54:39,840 --> 05:54:45,520 to import box V1 from yep thank you 8273 05:54:45,520 --> 05:54:48,080 GitHub co-pilot and box V2 we're 8274 05:54:48,080 --> 05:54:49,520 probably going to need to import more 8275 05:54:49,520 --> 05:54:52,878 stuff but it's fine function set up what 8276 05:54:52,878 --> 05:54:55,760 do we want to set up here well let's get 8277 05:54:55,760 --> 05:55:02,760 a deploy box public deployer upgrade box 8278 05:55:02,760 --> 05:55:04,798 public 8279 05:55:04,798 --> 05:55:07,920 upgrader we'll do an address public 8280 05:55:07,920 --> 05:55:10,718 owner equals make 8281 05:55:10,718 --> 05:55:15,440 addr owner we're going to say deployer 8282 05:55:15,440 --> 05:55:19,120 equals new deploy box upgrader upgrader 8283 05:55:19,120 --> 05:55:22,680 equals new upgrade box okay cool and we 8284 05:55:22,680 --> 05:55:23,760 probably should write a whole bunch of 8285 05:55:23,760 --> 05:55:25,600 tests but for this video I'm just going 8286 05:55:25,600 --> 05:55:26,638 to write 8287 05:55:26,638 --> 05:55:32,240 one say box V one public box V1 and 8288 05:55:32,240 --> 05:55:36,760 we'll say box V one equals deployer do 8289 05:55:36,760 --> 05:55:39,520 run we're going to have to wrap box V1 8290 05:55:39,520 --> 05:55:43,400 like this boom so we have our box V1 it 8291 05:55:43,400 --> 05:55:45,600 gets deployed our deployer run is a 8292 05:55:45,600 --> 05:55:48,558 deploy BLX run Returns the address of 8293 05:55:48,558 --> 05:55:52,360 the proxy not the address of the Box V1 8294 05:55:52,360 --> 05:55:54,280 right Returns the address of the proxy 8295 05:55:54,280 --> 05:55:56,958 not the box vi1 so we can even call this 8296 05:55:56,958 --> 05:56:01,240 to make it more clear box vi1 public 8297 05:56:01,240 --> 05:56:03,680 proxy since this is actually a proxy 8298 05:56:03,680 --> 05:56:05,080 address actually let's do this even 8299 05:56:05,080 --> 05:56:07,558 better address public proxy and this is 8300 05:56:07,558 --> 05:56:09,440 why before we working with addresses 8301 05:56:09,440 --> 05:56:12,000 because it can be kind of confusing to 8302 05:56:12,000 --> 05:56:13,958 know what it is the proxy right the the 8303 05:56:13,958 --> 05:56:18,520 proxy and it right now points to box V1 8304 05:56:18,520 --> 05:56:20,638 cool hopefully that's more clear so now 8305 05:56:20,638 --> 05:56:22,878 we'll do a function test 8306 05:56:22,878 --> 05:56:25,080 upgrades like this and we'll just test 8307 05:56:25,080 --> 05:56:28,520 the whole darn system so we have the box 8308 05:56:28,520 --> 05:56:30,878 we have the proxy address point to box 8309 05:56:30,878 --> 05:56:34,600 V1 now in our upgrade box in our run we 8310 05:56:34,600 --> 05:56:36,520 went ahead and did all this but for now 8311 05:56:36,520 --> 05:56:37,958 let's just use this upgrade box 8312 05:56:37,958 --> 05:56:40,798 functionality because so we can so I can 8313 05:56:40,798 --> 05:56:42,958 show you a little bit more granularly 8314 05:56:42,958 --> 05:56:44,600 what's actually happening right so test 8315 05:56:44,600 --> 05:56:46,638 upgrade let's let's deploy our own box 8316 05:56:46,638 --> 05:56:49,680 V2 we do it in run we could 100% do it 8317 05:56:49,680 --> 05:56:52,080 in run but I want to do it myself so I'm 8318 05:56:52,080 --> 05:56:56,000 going to say box V2 equals box V2 or Box 8319 05:56:56,000 --> 05:57:00,920 2 equals new box V2 vm. 8320 05:57:00,920 --> 05:57:04,200 prank box 32 box two now I'm going to 8321 05:57:04,200 --> 05:57:09,000 say address proxy equals upgrader do 8322 05:57:09,000 --> 05:57:12,920 upgrade box proxy which is just an 8323 05:57:12,920 --> 05:57:17,558 address address of box two so our 8324 05:57:17,558 --> 05:57:20,718 upgrade box upgrade box here all it's 8325 05:57:20,718 --> 05:57:23,440 doing is saying okay cool get the proxy 8326 05:57:23,440 --> 05:57:25,958 for box V1 and now point it to V2 so 8327 05:57:25,958 --> 05:57:28,240 we're calling that in our test here and 8328 05:57:28,240 --> 05:57:31,520 now we could do unit 256 expected value 8329 05:57:31,520 --> 05:57:34,718 equals 2 and we could say assert equal 8330 05:57:34,718 --> 05:57:35,878 oh actually we don't we shouldn't even 8331 05:57:35,878 --> 05:57:37,718 set this let's not even set this right 8332 05:57:37,718 --> 05:57:39,400 so this address is going to be the same 8333 05:57:39,400 --> 05:57:41,400 we're never resetting this address we're 8334 05:57:41,400 --> 05:57:43,200 being very clear about that boom proxy 8335 05:57:43,200 --> 05:57:45,558 deployed here boom we're never even 8336 05:57:45,558 --> 05:57:47,280 we're never Chang the address so it's 8337 05:57:47,280 --> 05:57:49,320 always going to be this address now we 8338 05:57:49,320 --> 05:57:52,000 can do though is we can Sayer equal 8339 05:57:52,000 --> 05:57:57,478 expected value is box V2 on the proxy 8340 05:57:57,478 --> 05:58:00,840 address. version if this works this 8341 05:58:00,840 --> 05:58:03,200 means that on the proxy address which 8342 05:58:03,200 --> 05:58:05,400 originally pointed to boxy one it's now 8343 05:58:05,400 --> 05:58:08,638 pointing to box box V2 we could also do 8344 05:58:08,638 --> 05:58:13,760 box V2 proxy dot set value or what do we 8345 05:58:13,760 --> 05:58:16,958 call it box V2 set number set number 8346 05:58:16,958 --> 05:58:20,280 number we say like seven or something 8347 05:58:20,280 --> 05:58:24,638 right and we could say assert equal 8348 05:58:24,638 --> 05:58:30,320 7A comma box V2 roxy. getet number like 8349 05:58:30,320 --> 05:58:32,520 this and we could even add another 8350 05:58:32,520 --> 05:58:38,200 function test proxy starts at as box V1 8351 05:58:38,200 --> 05:58:43,160 right public we could say vm. expect 8352 05:58:43,160 --> 05:58:48,080 revert with calling th this function 8353 05:58:48,080 --> 05:58:50,840 because set number doesn't exist on box 8354 05:58:50,840 --> 05:58:52,798 V1 so we try calling set number on the 8355 05:58:52,798 --> 05:58:54,760 proxy it should fail so let's run these 8356 05:58:54,760 --> 05:58:58,320 one at a time Forge test- M this should 8357 05:58:58,320 --> 05:59:00,280 indeed 8358 05:59:00,280 --> 05:59:03,360 revert great and then let's test this 8359 05:59:03,360 --> 05:59:05,840 and this should indeed upgrade Forge 8360 05:59:05,840 --> 05:59:10,520 test t-m test upgrades oh we failed 8361 05:59:10,520 --> 05:59:14,120 let's get some granular detail here- VB 8362 05:59:14,120 --> 05:59:17,600 left is seven right is zero set number 8363 05:59:17,600 --> 05:59:20,120 get number oops we forgot to actually 8364 05:59:20,120 --> 05:59:22,920 add set number in here so let's do 8365 05:59:22,920 --> 05:59:26,000 number equals underscore number whoops 8366 05:59:26,000 --> 05:59:28,760 now let's run that 8367 05:59:29,120 --> 05:59:32,360 test and boom it passes we have 8368 05:59:32,360 --> 05:59:36,760 successfully upgraded our box this is 8369 05:59:36,760 --> 05:59:40,878 very exciting 8370 05:59:46,320 --> 05:59:49,000 so let wrap this up I know we went 8371 05:59:49,000 --> 05:59:50,760 through this really quickly I want you 8372 05:59:50,760 --> 05:59:52,798 to ask a ton of questions about this 8373 05:59:52,798 --> 05:59:55,280 process ton of questions about this ask 8374 05:59:55,280 --> 05:59:56,680 them in the discussion ask them on 8375 05:59:56,680 --> 05:59:58,760 Twitter ask them to your buddies 8376 05:59:58,760 --> 06:00:01,520 whatever you want to do we just learned 8377 06:00:01,520 --> 06:00:03,840 about upgrades like I said I highly 8378 06:00:03,840 --> 06:00:07,478 recommend you do not default to this and 8379 06:00:07,478 --> 06:00:10,320 you do not let protocols that have 8380 06:00:10,320 --> 06:00:12,920 upgradeable smart contracts keep this 8381 06:00:12,920 --> 06:00:14,958 centralization Vector because it is if 8382 06:00:14,958 --> 06:00:16,478 you have an upgradeable smart contract 8383 06:00:16,478 --> 06:00:18,718 that means that a group could change the 8384 06:00:18,718 --> 06:00:21,520 logic of that code at some point or be 8385 06:00:21,520 --> 06:00:25,120 coerced to change the logic Etc but this 8386 06:00:25,120 --> 06:00:26,920 is an incredibly powerful primitive to 8387 06:00:26,920 --> 06:00:29,638 know about especially delegate call 8388 06:00:29,638 --> 06:00:32,478 we're almost done push this up to GitHub 8389 06:00:32,478 --> 06:00:35,400 add this to your portfolio take a break 8390 06:00:35,400 --> 06:00:36,680 congratulations 8391 06:00:36,680 --> 06:00:38,240 I'm going to go ahead and deploy this to 8392 06:00:38,240 --> 06:00:39,958 a test now just to show you what it will 8393 06:00:39,958 --> 06:00:42,840 look like on ether scan feel free to 8394 06:00:42,840 --> 06:00:45,440 just watch sit back relax you've done a 8395 06:00:45,440 --> 06:00:47,798 ton of amazing work to make my life 8396 06:00:47,798 --> 06:00:51,520 easier I'm going to borrow a make file 8397 06:00:51,520 --> 06:00:54,718 and just kind of tweak it let's change 8398 06:00:54,718 --> 06:00:57,920 let's just delete all this stuff deploy 8399 06:00:57,920 --> 06:01:03,320 it's going to be deploy box box upgrade 8400 06:01:03,320 --> 06:01:07,000 P script upgrade box 8401 06:01:07,000 --> 06:01:13,200 box make deploy args equal Das Das 8402 06:01:13,200 --> 06:01:15,600 Network 8403 06:01:15,600 --> 06:01:18,798 aoia all right so I ran the deploy we 8404 06:01:18,798 --> 06:01:20,600 deployed two contracts let's grab this 8405 06:01:20,600 --> 06:01:23,718 one firsta ether scan let me show you 8406 06:01:23,718 --> 06:01:25,680 what this looks like looks like this one 8407 06:01:25,680 --> 06:01:28,240 didn't quite verify correctly I could go 8408 06:01:28,240 --> 06:01:30,040 ahead and manually verify it if I wanted 8409 06:01:30,040 --> 06:01:32,240 but let's look at the second one looks 8410 06:01:32,240 --> 06:01:33,240 like neither one of these actually 8411 06:01:33,240 --> 06:01:35,200 verified correctly but that's fine I can 8412 06:01:35,200 --> 06:01:37,520 go to the my broadcast see which one is 8413 06:01:37,520 --> 06:01:40,080 which we can go to both these runs let's 8414 06:01:40,080 --> 06:01:43,520 see the first one okay we did create box 8415 06:01:43,520 --> 06:01:44,680 and so this is going to be the address 8416 06:01:44,680 --> 06:01:48,840 of box V1 so I copy this paste it in 8417 06:01:48,840 --> 06:01:51,040 here I can go ahead and verify this 8418 06:01:51,040 --> 06:01:52,600 manually later but I know that this is 8419 06:01:52,600 --> 06:01:55,840 my box V one we'll go back to broadcast 8420 06:01:55,840 --> 06:01:59,200 click this one scroll down uh this one 8421 06:01:59,200 --> 06:02:01,718 doesn't have a contract name but I know 8422 06:02:01,718 --> 06:02:03,878 that this one is going to be my proxy 8423 06:02:03,878 --> 06:02:07,958 address so I go to your browser let's 8424 06:02:07,958 --> 06:02:10,478 open keep them both up paste it in here 8425 06:02:10,478 --> 06:02:11,798 this one didn't get verified correctly 8426 06:02:11,798 --> 06:02:14,400 either but that's okay so this one's our 8427 06:02:14,400 --> 06:02:17,240 box this one's our proxy and this is the 8428 06:02:17,240 --> 06:02:19,080 one that's more important right this 8429 06:02:19,080 --> 06:02:22,878 proxy is pointing to this implementation 8430 06:02:22,878 --> 06:02:26,638 okay now we're going to call our upgrade 8431 06:02:26,638 --> 06:02:28,958 we're just doing make deploy make 8432 06:02:28,958 --> 06:02:32,320 upgrade like this oh and I 8433 06:02:32,320 --> 06:02:34,798 needl since I'm using Foundry devops I 8434 06:02:34,798 --> 06:02:38,120 need ffi equals true remember only do 8435 06:02:38,120 --> 06:02:40,400 that if you are confident about what 8436 06:02:40,400 --> 06:02:42,920 you're running do make 8437 06:02:42,920 --> 06:02:45,878 upgrade so this actually is a bit of a 8438 06:02:45,878 --> 06:02:47,840 bug with this script that I just found 8439 06:02:47,840 --> 06:02:50,478 out I do need to update my run latest to 8440 06:02:50,478 --> 06:02:52,878 ERC 8441 06:02:52,878 --> 06:02:56,440 1967 proxy I either need to upgrade my 8442 06:02:56,440 --> 06:02:59,958 script to look for null or I need to 8443 06:02:59,958 --> 06:03:01,760 upgrade Foundry to add this for the 8444 06:03:01,760 --> 06:03:03,798 proxy name so I'm just going to manually 8445 06:03:03,798 --> 06:03:05,920 add this in we're going to hit clear 8446 06:03:05,920 --> 06:03:07,798 we're going to rerun it look like that 8447 06:03:07,798 --> 06:03:10,478 is a bit of a bug sorry about that but 8448 06:03:10,478 --> 06:03:12,878 now that it knows that this is the ERC 8449 06:03:12,878 --> 06:03:16,040 1967 proxy we're actually running this 8450 06:03:16,040 --> 06:03:18,840 script now to upgrade the box and the 8451 06:03:18,840 --> 06:03:20,120 first thing that we're going to do is 8452 06:03:20,120 --> 06:03:22,240 we're going to deploy our box V2 which 8453 06:03:22,240 --> 06:03:24,160 for some reason is actually verifying 8454 06:03:24,160 --> 06:03:25,958 looks like something was just wrong with 8455 06:03:25,958 --> 06:03:29,000 the first time I did it but all good if 8456 06:03:29,000 --> 06:03:32,040 I paste this in we now have a box V2 8457 06:03:32,040 --> 06:03:33,478 being created and it looks like it's 8458 06:03:33,478 --> 06:03:36,040 trying to verify right now oh and looks 8459 06:03:36,040 --> 06:03:37,558 this one actually did verify so if we 8460 06:03:37,558 --> 06:03:39,920 refresh this one so it looks like our 8461 06:03:39,920 --> 06:03:43,120 box V2 is here we place this in it looks 8462 06:03:43,120 --> 06:03:44,680 like this one did actually verify which 8463 06:03:44,680 --> 06:03:47,920 is great box Fe 2 now if we go back to 8464 06:03:47,920 --> 06:03:51,280 our proxy we hit refresh we can see we 8465 06:03:51,280 --> 06:03:54,600 called an upgrade two on this contract 8466 06:03:54,600 --> 06:03:57,680 now whenever we call functions on this 8467 06:03:57,680 --> 06:04:00,320 it'll actually point to box V2 right so 8468 06:04:00,320 --> 06:04:02,120 we can even grab the contract address 8469 06:04:02,120 --> 06:04:05,760 we'll use a little cast send paste this 8470 06:04:05,760 --> 06:04:08,840 in here we'll call get number like this 8471 06:04:08,840 --> 06:04:11,440 or actually excuse me cast call cast 8472 06:04:11,440 --> 06:04:16,478 call and of course Das Das RPC URL seia 8473 06:04:16,478 --> 06:04:20,440 RPC URL this we can see that it starts 8474 06:04:20,440 --> 06:04:22,478 off at zero that number starts off as 8475 06:04:22,478 --> 06:04:25,400 zero then we can call well we can do 8476 06:04:25,400 --> 06:04:29,920 send instead set number takes a u into 8477 06:04:29,920 --> 06:04:32,000 256 we'll set the number 8478 06:04:32,000 --> 06:04:38,040 277 RPC D- private key private key 8479 06:04:38,040 --> 06:04:39,440 remember you're going to need to do 8480 06:04:39,440 --> 06:04:42,200 source. to do this we'll send a 8481 06:04:42,200 --> 06:04:44,920 transaction to actually update update 8482 06:04:44,920 --> 06:04:47,360 this and we're calling the proxy right 8483 06:04:47,360 --> 06:04:49,478 we're not calling this box V2 address 8484 06:04:49,478 --> 06:04:52,280 we're calling it right on the proxy then 8485 06:04:52,280 --> 06:04:55,760 if I go up up and we do call again we're 8486 06:04:55,760 --> 06:04:58,760 now going to get 4D for the binary and 8487 06:04:58,760 --> 06:05:00,360 if we do cast 8488 06:05:00,360 --> 06:05:04,878 d-2 base paste DEC for decimal we 8489 06:05:04,878 --> 06:05:06,558 convert this hex to decimal and we see 8490 06:05:06,558 --> 06:05:09,280 we get 77 back so we have successfully 8491 06:05:09,280 --> 06:05:11,718 deployed and worked with a proxy this is 8492 06:05:11,718 --> 06:05:16,520 so awesome take a break we got two more 8493 06:05:16,520 --> 06:05:18,600 you're so close you're almost done we 8494 06:05:18,600 --> 06:05:20,760 got two more scrolling all the way down 8495 06:05:20,760 --> 06:05:23,160 founder governance another quick one and 8496 06:05:23,160 --> 06:05:26,080 then introduction to security and then 8497 06:05:26,080 --> 06:05:28,558 I'm going to send you on your way to 8498 06:05:28,558 --> 06:05:30,440 make the future of Finance the future of 8499 06:05:30,440 --> 06:05:32,798 smart contracts better place you're so 8500 06:05:32,798 --> 06:05:35,360 close take a break get ice cream post on 8501 06:05:35,360 --> 06:05:38,558 Twitter I will see you 8502 06:05:55,760 --> 06:05:59,080 soon all right welcome back we're almost 8503 06:05:59,080 --> 06:06:03,000 there everybody let's scroll on down to 8504 06:06:03,000 --> 06:06:05,958 lesson 14 8505 06:06:05,958 --> 06:06:08,840 we are now on Foundry dowg governance 8506 06:06:08,840 --> 06:06:10,558 and of course all the code that we're 8507 06:06:10,558 --> 06:06:11,878 going to be working with is in this 8508 06:06:11,878 --> 06:06:13,760 GitHub repo so if you want to follow 8509 06:06:13,760 --> 06:06:16,160 along feel free to go here now there's a 8510 06:06:16,160 --> 06:06:18,798 ton of conceptual stuff to go over 8511 06:06:18,798 --> 06:06:20,840 before we get into this but basically 8512 06:06:20,840 --> 06:06:22,120 we're going to learn everything about 8513 06:06:22,120 --> 06:06:24,478 what a dow is what they're for and how 8514 06:06:24,478 --> 06:06:27,120 to use them and as we're going along and 8515 06:06:27,120 --> 06:06:29,160 as we're learning about Dows I want you 8516 06:06:29,160 --> 06:06:31,160 to take some time to read these two 8517 06:06:31,160 --> 06:06:33,080 articles they're both from vitalic and 8518 06:06:33,080 --> 06:06:34,920 they address a lot of the main concerns 8519 06:06:34,920 --> 06:06:37,680 with dows one of them are that Dows are 8520 06:06:37,680 --> 06:06:39,520 not corporations where decentralization 8521 06:06:39,520 --> 06:06:42,000 in autonomous organization matters and 8522 06:06:42,000 --> 06:06:44,478 this article came out in response to a 8523 06:06:44,478 --> 06:06:46,280 lot of people having really hard 8524 06:06:46,280 --> 06:06:47,798 coordinating doubts because since 8525 06:06:47,798 --> 06:06:49,440 they're decentralized autonomous 8526 06:06:49,440 --> 06:06:52,360 organizations and it can be difficult 8527 06:06:52,360 --> 06:06:54,680 sometimes to coordinate with so many 8528 06:06:54,680 --> 06:06:57,000 people and so many heads so this article 8529 06:06:57,000 --> 06:06:59,040 is really interesting where Dows work 8530 06:06:59,040 --> 06:07:00,680 really well and sometimes where they 8531 06:07:00,680 --> 06:07:03,000 don't and this one is one of my favorite 8532 06:07:03,000 --> 06:07:05,558 articles about plutocracy plutocracy is 8533 06:07:05,558 --> 06:07:08,878 is government by the wealthy this is 8534 06:07:08,878 --> 06:07:11,000 something that obviously sounds bad and 8535 06:07:11,000 --> 06:07:13,000 something we want to avoid in this new 8536 06:07:13,000 --> 06:07:15,160 system that we're creating if we default 8537 06:07:15,160 --> 06:07:17,558 to typical erc20 based governance we're 8538 06:07:17,558 --> 06:07:19,360 going to run into a lot of issues 8539 06:07:19,360 --> 06:07:21,478 outlined in this post if that doesn't 8540 06:07:21,478 --> 06:07:22,718 make sense to you right now don't worry 8541 06:07:22,718 --> 06:07:24,520 it will and we'll come back to this in a 8542 06:07:24,520 --> 06:07:26,280 little bit but first let's learn about 8543 06:07:26,280 --> 06:07:28,558 Dows what Dows look like and how they 8544 06:07:28,558 --> 06:07:31,920 actually work especially as developers 8545 06:07:31,920 --> 06:07:33,878 so I made some videos about this in the 8546 06:07:33,878 --> 06:07:36,520 past let's go ahead and watch those and 8547 06:07:36,520 --> 06:07:38,958 we'll come 8548 06:07:40,200 --> 06:07:42,718 back now before we learn how to code a 8549 06:07:42,718 --> 06:07:44,558 dow we should learn what a DA is and 8550 06:07:44,558 --> 06:07:46,360 again I've already made a video that I 8551 06:07:46,360 --> 06:07:47,600 put a lot of work into so we're going to 8552 06:07:47,600 --> 06:07:49,360 watch what a DA is from a high level 8553 06:07:49,360 --> 06:07:50,840 first then we're going to learn how to 8554 06:07:50,840 --> 06:07:53,160 code a 8555 06:07:54,280 --> 06:07:57,600 da now Dows or decentralized autonomous 8556 06:07:57,600 --> 06:07:59,760 organizations is a bit of an overload of 8557 06:07:59,760 --> 06:08:01,878 term but it typically describes any 8558 06:08:01,878 --> 06:08:04,478 group that is governed by a transparent 8559 06:08:04,478 --> 06:08:06,440 set of rules found on a blockchain or 8560 06:08:06,440 --> 06:08:08,958 smart contract and I say overloaded 8561 06:08:08,958 --> 06:08:10,920 because some people say Bitcoin is Dow 8562 06:08:10,920 --> 06:08:12,360 because the miners could choose whether 8563 06:08:12,360 --> 06:08:13,958 or not to upgrade their software other 8564 06:08:13,958 --> 06:08:15,718 people think that Dows must use 8565 06:08:15,718 --> 06:08:17,200 transparent smart contracts which have 8566 06:08:17,200 --> 06:08:19,320 the rules ingrained right into them and 8567 06:08:19,320 --> 06:08:20,878 then other people think Dow is just a 8568 06:08:20,878 --> 06:08:23,040 buzzword so they just slap the name 8569 06:08:23,040 --> 06:08:24,638 really onto any organization so that 8570 06:08:24,638 --> 06:08:26,040 they can get some clout and this makes 8571 06:08:26,040 --> 06:08:28,040 her sad 8572 06:08:28,040 --> 06:08:29,760 Patrick and this is enough to be 8573 06:08:29,760 --> 06:08:31,718 confused with the da which was an 8574 06:08:31,718 --> 06:08:34,600 implementation of a DA back in 2016 8575 06:08:34,600 --> 06:08:35,958 which set the record for the largest 8576 06:08:35,958 --> 06:08:37,920 hack at that time so there's a lot of 8577 06:08:37,920 --> 06:08:39,080 different ways to think about it and the 8578 06:08:39,080 --> 06:08:40,760 Dow term is used in a lot of different 8579 06:08:40,760 --> 06:08:42,958 ways but in essence imagine if all of 8580 06:08:42,958 --> 06:08:45,360 the users of Google were given voting 8581 06:08:45,360 --> 06:08:47,920 power into what Google should do next 8582 06:08:47,920 --> 06:08:50,280 and the rules of the voting was mutable 8583 06:08:50,280 --> 06:08:52,958 transparent and decentralized this 8584 06:08:52,958 --> 06:08:54,878 solves an age-old problem of trust 8585 06:08:54,878 --> 06:08:57,080 centrality and transparency and giving 8586 06:08:57,080 --> 06:08:59,160 the power to the users of different 8587 06:08:59,160 --> 06:09:01,760 protocols and applications instead of 8588 06:09:01,760 --> 06:09:03,558 everything happening behind closed doors 8589 06:09:03,558 --> 06:09:05,200 and this voting piece is a Cornerstone 8590 06:09:05,200 --> 06:09:07,478 of how these operate this decentralized 8591 06:09:07,478 --> 06:09:08,840 governance if you will and it can be 8592 06:09:08,840 --> 06:09:11,400 summarized by company or organization 8593 06:09:11,400 --> 06:09:14,680 operated exclusively through code and to 8594 06:09:14,680 --> 06:09:15,718 really understand all this we're going 8595 06:09:15,718 --> 06:09:17,718 to look under the hood of the protocol 8596 06:09:17,718 --> 06:09:19,440 that's setting the precedent for all 8597 06:09:19,440 --> 06:09:21,718 other Dows and compound then once we 8598 06:09:21,718 --> 06:09:23,600 look at compound we'll understand what 8599 06:09:23,600 --> 06:09:25,440 goes into building one of these and all 8600 06:09:25,440 --> 06:09:26,718 the trade-offs all the different 8601 06:09:26,718 --> 06:09:28,958 architectural choices mean for your 8602 06:09:28,958 --> 06:09:31,160 group and then in my next video I'm 8603 06:09:31,160 --> 06:09:33,200 going to have a full codal tutorial for 8604 06:09:33,200 --> 06:09:34,920 developers looking to build one of these 8605 06:09:34,920 --> 06:09:36,680 themselves but be absolutely sure to 8606 06:09:36,680 --> 06:09:37,840 watch the rest of this video because 8607 06:09:37,840 --> 06:09:38,958 it's going to give you all the 8608 06:09:38,958 --> 06:09:40,920 architectural fundamentals so you can 8609 06:09:40,920 --> 06:09:42,638 make Intelligent Decisions when you get 8610 06:09:42,638 --> 06:09:44,400 to that section and be sure that you and 8611 06:09:44,400 --> 06:09:45,920 your Dow friends smash the like And 8612 06:09:45,920 --> 06:09:47,478 subscribe button so that we can keep 8613 06:09:47,478 --> 06:09:50,320 giving you the best engineer first 8614 06:09:50,320 --> 06:09:51,718 content on the planet when it comes to 8615 06:09:51,718 --> 06:09:54,200 Smart contracts let's get into it so 8616 06:09:54,200 --> 06:09:56,360 here we have the compound protocol it's 8617 06:09:56,360 --> 06:09:58,160 a borrowing and lending application that 8618 06:09:58,160 --> 06:10:00,558 allows users to borrow and lend their 8619 06:10:00,558 --> 06:10:02,320 assets and everything about this 8620 06:10:02,320 --> 06:10:05,000 application is built in smart contracts 8621 06:10:05,000 --> 06:10:06,558 now often times they're going to want to 8622 06:10:06,558 --> 06:10:07,958 do a lot of new things maybe they want 8623 06:10:07,958 --> 06:10:10,000 to add a new token to allow borrowing 8624 06:10:10,000 --> 06:10:11,080 and lending maybe they're going to want 8625 06:10:11,080 --> 06:10:13,680 to change some of the apy parameters 8626 06:10:13,680 --> 06:10:14,760 maybe they're going to want to block 8627 06:10:14,760 --> 06:10:16,440 certain coins there's a lot of different 8628 06:10:16,440 --> 06:10:18,160 things that they might want to do so 8629 06:10:18,160 --> 06:10:19,840 that's where we're going to go ahead to 8630 06:10:19,840 --> 06:10:23,080 governance this is where you can find a 8631 06:10:23,080 --> 06:10:25,080 user interface for a list of all the 8632 06:10:25,080 --> 06:10:27,200 proposals and all the different ballots 8633 06:10:27,200 --> 06:10:29,400 that came to be so here's a list of some 8634 06:10:29,400 --> 06:10:31,120 of the governance proposals that this 8635 06:10:31,120 --> 06:10:32,840 protocol has actually been making to 8636 06:10:32,840 --> 06:10:34,680 improve and let's look at one of these 8637 06:10:34,680 --> 06:10:36,798 proposals that currently actually in 8638 06:10:36,798 --> 06:10:38,798 process so if we click on the proposal 8639 06:10:38,798 --> 06:10:40,440 we can actually see everything about 8640 06:10:40,440 --> 06:10:42,760 this proposal who voted for who voted 8641 06:10:42,760 --> 06:10:45,240 against and the proposal history here 8642 06:10:45,240 --> 06:10:46,600 now the first thing to one of these 8643 06:10:46,600 --> 06:10:48,200 proposals is somebody has to actually 8644 06:10:48,200 --> 06:10:50,160 create the proposal in a proposed 8645 06:10:50,160 --> 06:10:51,920 transaction and we actually see that 8646 06:10:51,920 --> 06:10:54,240 proposed transaction right here if we 8647 06:10:54,240 --> 06:10:57,040 click on this and we scroll down we can 8648 06:10:57,040 --> 06:10:58,680 actually see the exact parameters they 8649 06:10:58,680 --> 06:11:01,120 used to make this proposal let's go 8650 06:11:01,120 --> 06:11:02,760 ahead and decode the input data and we 8651 06:11:02,760 --> 06:11:04,558 can see this is exactly what this 8652 06:11:04,558 --> 06:11:06,400 proposal looks like the way that they're 8653 06:11:06,400 --> 06:11:08,920 typically divided is they have a list of 8654 06:11:08,920 --> 06:11:11,080 addresses and a list of functions to 8655 06:11:11,080 --> 06:11:12,638 call on those addresses and then 8656 06:11:12,638 --> 06:11:14,958 obviously the parameters to pass those 8657 06:11:14,958 --> 06:11:17,760 addresses so this proposal is saying hey 8658 06:11:17,760 --> 06:11:20,478 I would like to call support Market on 8659 06:11:20,478 --> 06:11:23,558 this address set Reserve Factor on this 8660 06:11:23,558 --> 06:11:25,320 address here are the parameters we're 8661 06:11:25,320 --> 06:11:27,160 going to pass they're obviously encoded 8662 06:11:27,160 --> 06:11:28,920 with bytes and then here's the 8663 06:11:28,920 --> 06:11:31,240 description string of what this is doing 8664 06:11:31,240 --> 06:11:32,958 and why we're actually doing this the 8665 06:11:32,958 --> 06:11:34,520 reason we have to do this proposal 8666 06:11:34,520 --> 06:11:36,080 governance process say is that these 8667 06:11:36,080 --> 06:11:38,400 contracts likely have access controls 8668 06:11:38,400 --> 06:11:40,040 where only the owner of these contracts 8669 06:11:40,040 --> 06:11:42,120 can actually call these two functions 8670 06:11:42,120 --> 06:11:44,360 and the owner of these two contracts is 8671 06:11:44,360 --> 06:11:46,760 likely going to be this governance sty 8672 06:11:46,760 --> 06:11:48,760 and values to zero just means that we're 8673 06:11:48,760 --> 06:11:50,280 not going to send any eth along with 8674 06:11:50,280 --> 06:11:52,280 these transactions once a proposal has 8675 06:11:52,280 --> 06:11:54,478 been created after a short delay it 8676 06:11:54,478 --> 06:11:56,520 becomes active and this is when people 8677 06:11:56,520 --> 06:11:58,400 can actually start voting on them this 8678 06:11:58,400 --> 06:12:00,520 delay between a proposal and an active 8679 06:12:00,520 --> 06:12:02,360 vote can be changed or modified 8680 06:12:02,360 --> 06:12:04,718 depending on your doubt then people have 8681 06:12:04,718 --> 06:12:07,160 some time to start voting on them and if 8682 06:12:07,160 --> 06:12:09,240 it passes which this one overwhelmingly 8683 06:12:09,240 --> 06:12:11,958 did it reaches succeeded if we click on 8684 06:12:11,958 --> 06:12:14,160 this transaction again and we go to the 8685 06:12:14,160 --> 06:12:16,240 compound governance contract and we 8686 06:12:16,240 --> 06:12:19,798 scroll down to contract right as proxy 8687 06:12:19,798 --> 06:12:23,440 we can actually see the exact function 8688 06:12:23,440 --> 06:12:25,718 that the people called to vote namely 8689 06:12:25,718 --> 06:12:29,320 cast by vote cast vote by signature and 8690 06:12:29,320 --> 06:12:31,160 cast vote with reason we'll talk a 8691 06:12:31,160 --> 06:12:32,680 little bit about the exact differences 8692 06:12:32,680 --> 06:12:34,520 between these in our next video but 8693 06:12:34,520 --> 06:12:35,718 these are the function that they're 8694 06:12:35,718 --> 06:12:37,920 actually calling and if you go to the 8695 06:12:37,920 --> 06:12:41,400 compound app and we go over to vote this 8696 06:12:41,400 --> 06:12:42,958 is a user interface you can actually 8697 06:12:42,958 --> 06:12:44,958 vote through to make it easier if you're 8698 06:12:44,958 --> 06:12:47,320 not as techsavvy so you can vote right 8699 06:12:47,320 --> 06:12:49,920 through this app. compound. Finance or 8700 06:12:49,920 --> 06:12:50,958 you can just send the transaction 8701 06:12:50,958 --> 06:12:54,000 yourself once all those votes happen it 8702 06:12:54,000 --> 06:12:56,840 reaches this cued stage now what does 8703 06:12:56,840 --> 06:12:59,680 cued mean well before a proposal 8704 06:12:59,680 --> 06:13:01,400 actually becomes active there's a 8705 06:13:01,400 --> 06:13:04,360 minimum delay between a proposal passing 8706 06:13:04,360 --> 06:13:07,040 and a proposal being executed so 8707 06:13:07,040 --> 06:13:09,320 somebody has to call this cued function 8708 06:13:09,320 --> 06:13:11,280 and it only can be called if a vote 8709 06:13:11,280 --> 06:13:14,320 passes and it says okay that proposal ID 8710 06:13:14,320 --> 06:13:16,440 has been cued and we're going to execute 8711 06:13:16,440 --> 06:13:19,520 it soon now if we go to a different 8712 06:13:19,520 --> 06:13:21,160 proposal like this one for example we 8713 06:13:21,160 --> 06:13:23,200 can see it has been executed we can see 8714 06:13:23,200 --> 06:13:26,638 somebody called this executed 8715 06:13:26,638 --> 06:13:29,878 function and they executed proposal 82 8716 06:13:29,878 --> 06:13:31,878 so this is going to be a full example of 8717 06:13:31,878 --> 06:13:34,320 the life cycle of a proposal going 8718 06:13:34,320 --> 06:13:36,360 through this process now there are a 8719 06:13:36,360 --> 06:13:38,360 couple that even failed whole punch of 8720 06:13:38,360 --> 06:13:40,638 you voted against this and if you scroll 8721 06:13:40,638 --> 06:13:42,280 down you can see it was created it was 8722 06:13:42,280 --> 06:13:44,160 active and the majority of people voted 8723 06:13:44,160 --> 06:13:46,520 against so that's where it stops now 8724 06:13:46,520 --> 06:13:48,400 often times just putting one of these 8725 06:13:48,400 --> 06:13:50,760 proposals through isn't enough to really 8726 06:13:50,760 --> 06:13:53,080 Garner some votes for it you generally 8727 06:13:53,080 --> 06:13:55,240 want a forum or some type of discussion 8728 06:13:55,240 --> 06:13:57,520 place to talk about these proposals and 8729 06:13:57,520 --> 06:13:59,360 why you like them or don't like them 8730 06:13:59,360 --> 06:14:01,080 often times a discourse is one of the 8731 06:14:01,080 --> 06:14:03,080 main places that people are going to 8732 06:14:03,080 --> 06:14:05,240 argue for why something is good or why 8733 06:14:05,240 --> 06:14:07,080 is bad so people can vote on these 8734 06:14:07,080 --> 06:14:09,600 changes and again snapshot might be one 8735 06:14:09,600 --> 06:14:11,520 of these tools that you use to figure 8736 06:14:11,520 --> 06:14:12,958 out if your community even wants 8737 06:14:12,958 --> 06:14:14,958 something before it even goes to vote 8738 06:14:14,958 --> 06:14:16,798 you could join one of these and with 8739 06:14:16,798 --> 06:14:18,520 your tokens actually vote on things 8740 06:14:18,520 --> 06:14:20,120 without them being executed just to get 8741 06:14:20,120 --> 06:14:22,120 the sentiment or like I said before you 8742 06:14:22,120 --> 06:14:23,638 could build your protocol in a way that 8743 06:14:23,638 --> 06:14:25,080 snapshot actually helps you with the 8744 06:14:25,080 --> 06:14:27,600 voting process all right now you've seen 8745 06:14:27,600 --> 06:14:29,400 the protocol that has been influencing 8746 06:14:29,400 --> 06:14:31,360 all the other Dows on how to vote now 8747 06:14:31,360 --> 06:14:33,840 you know now that we know what a dow 8748 06:14:33,840 --> 06:14:35,160 looks like let's talk about the the 8749 06:14:35,160 --> 06:14:36,798 architecture and tools that go into 8750 06:14:36,798 --> 06:14:38,360 building one of these and additionally 8751 06:14:38,360 --> 06:14:40,120 the trade-offs that they have and the 8752 06:14:40,120 --> 06:14:41,638 first thing to talk about here is going 8753 06:14:41,638 --> 06:14:44,320 to be the voting mechanism now voting in 8754 06:14:44,320 --> 06:14:46,798 decentralized governance is critical to 8755 06:14:46,798 --> 06:14:48,520 these Dows because sometimes they do 8756 06:14:48,520 --> 06:14:50,320 need to update and change to keep with 8757 06:14:50,320 --> 06:14:52,320 the times not all protocols need to have 8758 06:14:52,320 --> 06:14:54,440 a dow but those that do need to have a 8759 06:14:54,440 --> 06:14:56,478 dow need a way for participants to 8760 06:14:56,478 --> 06:14:58,760 engage this is one of the most important 8761 06:14:58,760 --> 06:15:00,920 questions to ask and to tell your 8762 06:15:00,920 --> 06:15:03,478 communities how do I participate how do 8763 06:15:03,478 --> 06:15:05,558 I engage in this D 8764 06:15:05,558 --> 06:15:07,600 how do I help make decisions and you'll 8765 06:15:07,600 --> 06:15:10,120 find this is a bit of a tricky problem 8766 06:15:10,120 --> 06:15:12,798 to solve now an easy approach to this 8767 06:15:12,798 --> 06:15:14,878 problem is going to be using an erc20 or 8768 06:15:14,878 --> 06:15:18,160 an nft token as voting power similar to 8769 06:15:18,160 --> 06:15:20,000 what we saw with compound use the comp 8770 06:15:20,000 --> 06:15:22,520 token to vote for different proposals 8771 06:15:22,520 --> 06:15:24,558 seems simple enough right boom problem 8772 06:15:24,558 --> 06:15:27,120 solved hooray now this actually might be 8773 06:15:27,120 --> 06:15:28,958 the right approach for certain Dows but 8774 06:15:28,958 --> 06:15:31,000 it also runs the risk of actually being 8775 06:15:31,000 --> 06:15:33,040 less Fair because when you tokenize the 8776 06:15:33,040 --> 06:15:35,280 voting power you're essentially auction 8777 06:15:35,280 --> 06:15:37,360 off this voting power to whoever's got 8778 06:15:37,360 --> 06:15:39,400 the deepest Pockets Whoever has the most 8779 06:15:39,400 --> 06:15:41,798 money gets to pick the changes so if 8780 06:15:41,798 --> 06:15:43,080 it's only the rich people who get to 8781 06:15:43,080 --> 06:15:45,280 vote then it's highly likely that all 8782 06:15:45,280 --> 06:15:47,080 the changes in the protocol are going to 8783 06:15:47,080 --> 06:15:49,440 benefit the rich which doesn't really 8784 06:15:49,440 --> 06:15:51,160 seem like that great of an improvement 8785 06:15:51,160 --> 06:15:53,400 over our current world nfts are 8786 06:15:53,400 --> 06:15:54,520 interesting because they have this 8787 06:15:54,520 --> 06:15:57,520 non-fungible component but yet even they 8788 06:15:57,520 --> 06:15:59,320 still run into this issue additionally 8789 06:15:59,320 --> 06:16:00,920 if you buy a whole bunch of votes you 8790 06:16:00,920 --> 06:16:02,760 make a bad decision and then sell all 8791 06:16:02,760 --> 06:16:05,200 your votes you as an individual don't 8792 06:16:05,200 --> 06:16:06,878 really get punished you just punish the 8793 06:16:06,878 --> 06:16:09,120 group as a whole but you being malicious 8794 06:16:09,120 --> 06:16:10,440 you can get away with pretty scot-free 8795 06:16:10,440 --> 06:16:12,360 now again this voting mechanism is going 8796 06:16:12,360 --> 06:16:14,840 to be correct for some groups but for 8797 06:16:14,840 --> 06:16:17,718 other groups maybe not it really just 8798 06:16:17,718 --> 06:16:19,360 depends on what your Dow and Community 8799 06:16:19,360 --> 06:16:20,958 setup is going to look like now the next 8800 06:16:20,958 --> 06:16:22,638 one we're going to talk about is skin in 8801 06:16:22,638 --> 06:16:24,360 the game now vitalic has actually 8802 06:16:24,360 --> 06:16:25,798 written a lot about this and I highly 8803 06:16:25,798 --> 06:16:27,718 recommend you read his article Link in 8804 06:16:27,718 --> 06:16:29,600 the description to see that the skin in 8805 06:16:29,600 --> 06:16:31,280 the game method means that whenever you 8806 06:16:31,280 --> 06:16:33,878 make a decision your vote is recorded 8807 06:16:33,878 --> 06:16:35,760 and if that decision leads to a bad 8808 06:16:35,760 --> 06:16:39,080 outcome your tokens are asked you get 8809 06:16:39,080 --> 06:16:41,160 punished for making evil or bad 8810 06:16:41,160 --> 06:16:43,120 decisions for your Dow and your protocol 8811 06:16:43,120 --> 06:16:44,600 I like this mentality because even if 8812 06:16:44,600 --> 06:16:46,520 you buy a ton of tokens and decide to be 8813 06:16:46,520 --> 06:16:48,320 evil with it you can be held accountable 8814 06:16:48,320 --> 06:16:50,520 for your bad decisions now the hardest 8815 06:16:50,520 --> 06:16:52,080 part about this though is going to be 8816 06:16:52,080 --> 06:16:54,920 how do we decide as a community what is 8817 06:16:54,920 --> 06:16:56,760 a bad outcome how do we actually punish 8818 06:16:56,760 --> 06:16:59,240 these people and that's easy because the 8819 06:16:59,240 --> 06:17:00,840 answer 8820 06:17:00,840 --> 06:17:03,680 is I'm not sure now the third method of 8821 06:17:03,680 --> 06:17:05,718 this voting mechanism is probably one of 8822 06:17:05,718 --> 06:17:07,558 the most interesting ones but also the 8823 06:17:07,558 --> 06:17:09,680 hardest ones to implement and this is 8824 06:17:09,680 --> 06:17:12,478 proof of personhood or participation 8825 06:17:12,478 --> 06:17:14,600 imagine that all users of the compound 8826 06:17:14,600 --> 06:17:17,160 protocol were given a single vote simply 8827 06:17:17,160 --> 06:17:19,280 because they used the protocol and even 8828 06:17:19,280 --> 06:17:21,120 if they had a thousand wallets they use 8829 06:17:21,120 --> 06:17:23,958 the protocol one human being means one 8830 06:17:23,958 --> 06:17:25,958 vote this would be amazing and a far 8831 06:17:25,958 --> 06:17:27,878 more fair implementation where votes 8832 06:17:27,878 --> 06:17:29,958 couldn't actually just be bought the 8833 06:17:29,958 --> 06:17:31,798 issue however comes in something known 8834 06:17:31,798 --> 06:17:34,520 as civil resistance how can we be sure 8835 06:17:34,520 --> 06:17:36,600 that it's one vote equals one 8836 06:17:36,600 --> 06:17:38,600 participant and not one participant 8837 06:17:38,600 --> 06:17:40,160 pretending to be thousands of different 8838 06:17:40,160 --> 06:17:41,840 people so they get more votes this 8839 06:17:41,840 --> 06:17:43,920 method hasn't really been solved yet but 8840 06:17:43,920 --> 06:17:45,280 I'm willing to bet some very clever 8841 06:17:45,280 --> 06:17:47,160 engineer will do some amazing chain link 8842 06:17:47,160 --> 06:17:49,320 integration because proof of personhood 8843 06:17:49,320 --> 06:17:51,280 is basically just offchain data that can 8844 06:17:51,280 --> 06:17:53,040 be delivered on chain and that's exactly 8845 06:17:53,040 --> 06:17:54,718 where chain link shines now as you can 8846 06:17:54,718 --> 06:17:57,160 see all these methods and even more that 8847 06:17:57,160 --> 06:17:58,958 you probably think of aren't that 8848 06:17:58,958 --> 06:18:01,320 far-fetched and we actually see these 8849 06:18:01,320 --> 06:18:03,200 exact same methods happening in the real 8850 06:18:03,200 --> 06:18:05,400 world proof a person who or approve of 8851 06:18:05,400 --> 06:18:07,400 participation might just be the exact 8852 06:18:07,400 --> 06:18:09,200 same as kind of the regular government 8853 06:18:09,200 --> 06:18:10,920 voting that we see every day in the 8854 06:18:10,920 --> 06:18:12,958 United States at least one person gets 8855 06:18:12,958 --> 06:18:15,400 to vote for one president you can't go 8856 06:18:15,400 --> 06:18:16,840 around making a whole bunch of fake 8857 06:18:16,840 --> 06:18:18,520 people and voting for president but in 8858 06:18:18,520 --> 06:18:20,840 companies the erc20 voting standard kind 8859 06:18:20,840 --> 06:18:22,760 of applies the more shares of a company 8860 06:18:22,760 --> 06:18:24,878 you have maybe the more voting power you 8861 06:18:24,878 --> 06:18:26,360 have in that company so we can draw 8862 06:18:26,360 --> 06:18:28,160 parallels between the real world and how 8863 06:18:28,160 --> 06:18:29,680 voting and governance is going to work 8864 06:18:29,680 --> 06:18:31,440 in our smart contracts and in fact you 8865 06:18:31,440 --> 06:18:32,920 should draw parallels and look for 8866 06:18:32,920 --> 06:18:34,958 inspiration from the web TW space now 8867 06:18:34,958 --> 06:18:36,558 when it comes to implementation of the 8868 06:18:36,558 --> 06:18:38,600 voting I put them into two categories 8869 06:18:38,600 --> 06:18:40,600 onchain voting and offchain voting 8870 06:18:40,600 --> 06:18:42,558 onchain voting is exactly what we saw 8871 06:18:42,558 --> 06:18:44,520 with compound there's a smart contract 8872 06:18:44,520 --> 06:18:46,840 on chain you're a voter you call some 8873 06:18:46,840 --> 06:18:48,680 function called vote with your metamask 8874 06:18:48,680 --> 06:18:50,798 your Ledger or whatever send a 8875 06:18:50,798 --> 06:18:53,160 transaction and boom you voted congrats 8876 06:18:53,160 --> 06:18:54,638 you can wear your little thicker Now 8877 06:18:54,638 --> 06:18:56,440 call that function and you send a 8878 06:18:56,440 --> 06:18:59,958 transaction you send a transaction H 8879 06:18:59,958 --> 06:19:01,520 what do transactions use that are kind 8880 06:19:01,520 --> 06:19:04,600 of annoying and kind of costly uh oh 8881 06:19:04,600 --> 06:19:06,160 that that's right gas imagine you have 8882 06:19:06,160 --> 06:19:07,760 10,000 people in your community and it 8883 06:19:07,760 --> 06:19:10,478 costs $100 to vote per person you are 8884 06:19:10,478 --> 06:19:14,000 now costing your community $1 million 8885 06:19:14,000 --> 06:19:15,638 anytime you want to change anything this 8886 06:19:15,638 --> 06:19:17,000 is obviously insane and not very 8887 06:19:17,000 --> 06:19:18,638 sustainable for your community the pro 8888 06:19:18,638 --> 06:19:20,080 here is that the architecture is really 8889 06:19:20,080 --> 06:19:21,520 easy everything's going to be 8890 06:19:21,520 --> 06:19:22,878 transparent everything's going to be on 8891 06:19:22,878 --> 06:19:24,798 chain and that's really good but yes the 8892 06:19:24,798 --> 06:19:26,240 con is that you're going to break the 8893 06:19:26,240 --> 06:19:27,878 bank account for a lot of people 8894 06:19:27,878 --> 06:19:29,200 potentially now there are a lot of 8895 06:19:29,200 --> 06:19:31,200 variations of this to help solve some of 8896 06:19:31,200 --> 06:19:32,760 these problems especially the gas 8897 06:19:32,760 --> 06:19:34,040 problem one of the ones that I'm 8898 06:19:34,040 --> 06:19:35,680 incredibly excited for is this one 8899 06:19:35,680 --> 06:19:37,878 called Governor C where they use some 8900 06:19:37,878 --> 06:19:39,798 random sampling to do some quadratic 8901 06:19:39,798 --> 06:19:41,558 voting to help reduce cost while 8902 06:19:41,558 --> 06:19:43,440 increasing civil resistance you want to 8903 06:19:43,440 --> 06:19:45,200 learn more about that one too be sure to 8904 06:19:45,200 --> 06:19:46,878 read about it in the description so 8905 06:19:46,878 --> 06:19:49,000 onchain voting is the simplest one here 8906 06:19:49,000 --> 06:19:52,160 but let's talk about offchain voting how 8907 06:19:52,160 --> 06:19:54,000 could you possibly vote off chain in a 8908 06:19:54,000 --> 06:19:56,200 decentralized context relax relax you 8909 06:19:56,200 --> 06:19:58,040 can vote off chain and still have it be 8910 06:19:58,040 --> 06:20:00,478 100% decentralized you can actually sign 8911 06:20:00,478 --> 06:20:03,440 a transaction and sign a vote without 8912 06:20:03,440 --> 06:20:04,878 actually sending to a blockchain and 8913 06:20:04,878 --> 06:20:06,600 therefore without actually spending any 8914 06:20:06,600 --> 06:20:09,160 gas instead what you can do is send that 8915 06:20:09,160 --> 06:20:11,160 signed transaction to a decentralized 8916 06:20:11,160 --> 06:20:13,718 database like ipfs count up all the 8917 06:20:13,718 --> 06:20:16,440 votes in ipfs and then when time comes 8918 06:20:16,440 --> 06:20:18,638 deliver the result of that data through 8919 06:20:18,638 --> 06:20:20,400 something like an oracle like chain link 8920 06:20:20,400 --> 06:20:22,280 to the blockchain all in a single 8921 06:20:22,280 --> 06:20:24,240 transaction alternatively what you could 8922 06:20:24,240 --> 06:20:26,200 do is you could replay all these signed 8923 06:20:26,200 --> 06:20:28,280 transactions in a single transaction to 8924 06:20:28,280 --> 06:20:30,558 save gas this can reduce the voting cost 8925 06:20:30,558 --> 06:20:33,080 by up to 99% right now this is an 8926 06:20:33,080 --> 06:20:34,240 implementation and one of the most 8927 06:20:34,240 --> 06:20:35,920 popular ways to do this is through 8928 06:20:35,920 --> 06:20:38,718 snapshot and I'm just dying for someone 8929 06:20:38,718 --> 06:20:40,160 to make a chain link integration because 8930 06:20:40,160 --> 06:20:41,878 it's going to be so much safer more 8931 06:20:41,878 --> 06:20:43,840 secure and better and blah blah blah 8932 06:20:43,840 --> 06:20:45,680 dying for it this is your call to action 8933 06:20:45,680 --> 06:20:47,280 go build this thing this offchain voting 8934 06:20:47,280 --> 06:20:49,240 mechanism obviously saves a ton of gas 8935 06:20:49,240 --> 06:20:51,080 to the community and can be a more 8936 06:20:51,080 --> 06:20:52,400 efficient way to store these 8937 06:20:52,400 --> 06:20:54,320 transactions anyways however it needs to 8938 06:20:54,320 --> 06:20:56,280 be implemented very carefully if you run 8939 06:20:56,280 --> 06:20:57,798 your entire Dow through a centralized 8940 06:20:57,798 --> 06:21:00,160 Oracle you are essentially reintroducing 8941 06:21:00,160 --> 06:21:02,280 a centralized intermediary and ruining 8942 06:21:02,280 --> 06:21:04,000 the decentral of your application so 8943 06:21:04,000 --> 06:21:05,360 don't do that and if you made it to this 8944 06:21:05,360 --> 06:21:06,958 point in the video give yourself a 8945 06:21:06,958 --> 06:21:08,280 little path in the back you're doing 8946 06:21:08,280 --> 06:21:10,478 fantastic learning is fantastic like I 8947 06:21:10,478 --> 06:21:12,120 said I have a video coming out after 8948 06:21:12,120 --> 06:21:13,680 this one that's going to show you end to 8949 06:21:13,680 --> 06:21:15,160 endend how to build one of these from 8950 06:21:15,160 --> 06:21:16,718 scratch let's learn about some of the 8951 06:21:16,718 --> 06:21:18,840 tools that you can use to help get you 8952 06:21:18,840 --> 06:21:20,240 up to speed quicker now there are a 8953 06:21:20,240 --> 06:21:22,040 number of no code solutions that can go 8954 06:21:22,040 --> 06:21:23,558 into building one of these Dows Dow 8955 06:21:23,558 --> 06:21:25,920 stack 8956 06:21:26,760 --> 06:21:29,040 Aragon just kidding this is Aragon 8957 06:21:29,040 --> 06:21:30,920 colony and DOW house are all 8958 06:21:30,920 --> 06:21:33,000 Alternatives that can actually help you 8959 06:21:33,000 --> 06:21:35,040 with the Ops side of running a and 8960 06:21:35,040 --> 06:21:36,600 building a doubt however if you want 8961 06:21:36,600 --> 06:21:38,040 more granular control and you don't want 8962 06:21:38,040 --> 06:21:39,120 to have to pay any of the fees 8963 06:21:39,120 --> 06:21:40,798 associated with these protocols you 8964 06:21:40,798 --> 06:21:42,160 might want to do it from scratch now 8965 06:21:42,160 --> 06:21:43,520 let's talk about some of the more Cody 8966 06:21:43,520 --> 06:21:45,280 solutions that you can use Snapshot is 8967 06:21:45,280 --> 06:21:46,878 one of the most popular tools out there 8968 06:21:46,878 --> 06:21:49,478 for both getting the sentiment of a dow 8969 06:21:49,478 --> 06:21:51,478 and actually performing that execution 8970 06:21:51,478 --> 06:21:53,160 users can vote through this protocol 8971 06:21:53,160 --> 06:21:54,840 with their actual tokens those 8972 06:21:54,840 --> 06:21:57,000 transaction gets stored in ipfs but none 8973 06:21:57,000 --> 06:21:58,840 of it actually gets executed unless the 8974 06:21:58,840 --> 06:22:00,878 Dow chooses to so this can be a great 8975 06:22:00,878 --> 06:22:03,920 way to get the feel for what your Dow 8976 06:22:03,920 --> 06:22:05,080 wants to do 8977 06:22:05,080 --> 06:22:06,600 and optionally you can send the 8978 06:22:06,600 --> 06:22:08,840 transactions and execute the votes as 8979 06:22:08,840 --> 06:22:10,638 well I highly recommend checking out 8980 06:22:10,638 --> 06:22:12,760 zodiac which is a suite of Dow based 8981 06:22:12,760 --> 06:22:14,680 tools for you to implement into your 8982 06:22:14,680 --> 06:22:16,400 Dows as well tally is another one of 8983 06:22:16,400 --> 06:22:18,638 these uis that allow people to see and 8984 06:22:18,638 --> 06:22:20,120 actually vote and interact with these 8985 06:22:20,120 --> 06:22:22,080 smart contracts through user interface 8986 06:22:22,080 --> 06:22:23,160 for those of you who don't know about 8987 06:22:23,160 --> 06:22:25,520 nosis safe you absolutely should nosis 8988 06:22:25,520 --> 06:22:27,478 safe is a multisig wallet and the reason 8989 06:22:27,478 --> 06:22:28,878 that I put this on the list even though 8990 06:22:28,878 --> 06:22:30,478 it's adds kind of this centrality 8991 06:22:30,478 --> 06:22:32,520 component is that most Dows in the 8992 06:22:32,520 --> 06:22:34,718 beginning are probably going to start 8993 06:22:34,718 --> 06:22:36,638 with some type of centrality it's much 8994 06:22:36,638 --> 06:22:38,160 easier to be fast when you don't have 8995 06:22:38,160 --> 06:22:39,840 thousands of people to wait for a vote 8996 06:22:39,840 --> 06:22:41,638 and in the beginning any protocol is 8997 06:22:41,638 --> 06:22:43,240 going to be centralized to some degree 8998 06:22:43,240 --> 06:22:45,320 anyways using a multisig where voting 8999 06:22:45,320 --> 06:22:47,680 happens through only a few key members 9000 06:22:47,680 --> 06:22:49,360 can be good in the beginning for your 9001 06:22:49,360 --> 06:22:52,000 Dows and often emergencies as well but 9002 06:22:52,000 --> 06:22:53,878 just keep in mind when you add one of 9003 06:22:53,878 --> 06:22:55,320 these you are adding this level of 9004 06:22:55,320 --> 06:22:57,280 centrality and then of course open Zepp 9005 06:22:57,280 --> 06:22:59,080 and contracts we love open up in 9006 06:22:59,080 --> 06:23:00,440 contracts these are the contracts that 9007 06:23:00,440 --> 06:23:02,478 we're going to be basing our Dow codal 9008 06:23:02,478 --> 06:23:04,240 on all right so that's all the tools 9009 06:23:04,240 --> 06:23:06,160 that's the architecture one more thing 9010 06:23:06,160 --> 06:23:08,680 before I let you go legality the future 9011 06:23:08,680 --> 06:23:10,120 of dows is interesting for all these 9012 06:23:10,120 --> 06:23:11,638 reasons we just talked about but 9013 06:23:11,638 --> 06:23:13,638 especially on a legal front does it make 9014 06:23:13,638 --> 06:23:15,200 sense for a DA to live by the same 9015 06:23:15,200 --> 06:23:17,040 regulation as another company how could 9016 06:23:17,040 --> 06:23:18,638 you even force a DA to do something 9017 06:23:18,638 --> 06:23:20,240 you'd have to force them to all vote a 9018 06:23:20,240 --> 06:23:21,798 certain way if the government tells you 9019 06:23:21,798 --> 06:23:24,280 to it's it's a little gray it's hard to 9020 06:23:24,280 --> 06:23:26,160 nail down who to even keep accountable 9021 06:23:26,160 --> 06:23:27,920 for these Dows in the United States at 9022 06:23:27,920 --> 06:23:29,878 least you can actually form your own Dow 9023 06:23:29,878 --> 06:23:31,718 and have it legally recognized in the 9024 06:23:31,718 --> 06:23:33,400 state of Wyoming this is something I 9025 06:23:33,400 --> 06:23:35,000 want to do someone just have to see what 9026 06:23:35,000 --> 06:23:38,120 happens there w at this point you have 9027 06:23:38,120 --> 06:23:40,478 been injected with all the Dow knowledge 9028 06:23:40,478 --> 06:23:42,878 you need to succeed and Thrive with this 9029 06:23:42,878 --> 06:23:44,718 new amazing technology and these new 9030 06:23:44,718 --> 06:23:47,690 amazing Concepts and it's time to build 9031 06:23:47,690 --> 06:23:50,770 [Music] 9032 06:23:54,400 --> 06:23:56,920 baby now we are still going to build a 9033 06:23:56,920 --> 06:23:59,478 dow from scratch but we do have our 9034 06:23:59,478 --> 06:24:02,120 special guest Juliet from the Aragon 9035 06:24:02,120 --> 06:24:04,400 team to go over a no code solution that 9036 06:24:04,400 --> 06:24:06,280 you can use as an alternative hello 9037 06:24:06,280 --> 06:24:09,200 everyone uh my name is Julia chier I'm 9038 06:24:09,200 --> 06:24:11,520 the developer advocate for Aragon and 9039 06:24:11,520 --> 06:24:13,760 today we'll be building a dow without 9040 06:24:13,760 --> 06:24:18,080 using a single line of code let's go so 9041 06:24:18,080 --> 06:24:20,360 to do that I'll be using Aragon as my 9042 06:24:20,360 --> 06:24:22,878 Dow framework and to give you a bit of a 9043 06:24:22,878 --> 06:24:25,360 better overview as to how Aragon 9044 06:24:25,360 --> 06:24:27,718 architecture is set up um Dows are 9045 06:24:27,718 --> 06:24:30,280 essentially composed of a smart contract 9046 06:24:30,280 --> 06:24:31,958 which is the one that contains all of 9047 06:24:31,958 --> 06:24:34,558 the assets that the organization manages 9048 06:24:34,558 --> 06:24:37,000 um so it acts as a treasury and then all 9049 06:24:37,000 --> 06:24:39,280 of the other functionality for Dows is 9050 06:24:39,280 --> 06:24:41,440 really enabled through plugins and we'll 9051 06:24:41,440 --> 06:24:44,360 see how this uh how this is represented 9052 06:24:44,360 --> 06:24:45,920 itself throughout the process that we'll 9053 06:24:45,920 --> 06:24:49,558 build so we go into ab. ar.org we go 9054 06:24:49,558 --> 06:24:52,798 into creating a da um first we'll see 9055 06:24:52,798 --> 06:24:54,878 the process uh that we'll be taking in 9056 06:24:54,878 --> 06:24:57,080 order to do that and then we can get 9057 06:24:57,080 --> 06:25:00,520 right into it so firstly we need to 9058 06:25:00,520 --> 06:25:02,440 choose the blockchain which our Dow will 9059 06:25:02,440 --> 06:25:05,000 be deployed in um I'll choose orally for 9060 06:25:05,000 --> 06:25:06,920 the sake of Simplicity and playing with 9061 06:25:06,920 --> 06:25:09,440 testnet um then we get to describe our 9062 06:25:09,440 --> 06:25:13,520 Dow so we can have a developer Dow um 9063 06:25:13,520 --> 06:25:16,200 which can be something like this we can 9064 06:25:16,200 --> 06:25:19,400 set up our ens we can set up our logo a 9065 06:25:19,400 --> 06:25:23,360 description a dow for developers um and 9066 06:25:23,360 --> 06:25:25,240 we can add in additional oh we can add 9067 06:25:25,240 --> 06:25:27,400 in additional links um as well that 9068 06:25:27,400 --> 06:25:28,760 represent the 9069 06:25:28,760 --> 06:25:30,840 organization then we'll go into defining 9070 06:25:30,840 --> 06:25:33,360 the membership this is the what will 9071 06:25:33,360 --> 06:25:35,440 Define who is able to participate in the 9072 06:25:35,440 --> 06:25:38,120 governance of these assets um and 9073 06:25:38,120 --> 06:25:40,520 currently our app supports two different 9074 06:25:40,520 --> 06:25:43,240 types of uh membership definitions and 9075 06:25:43,240 --> 06:25:46,040 governance mechanisms one of them is 9076 06:25:46,040 --> 06:25:48,440 token holders which enables holders of 9077 06:25:48,440 --> 06:25:50,440 specific tokens to vote in the 9078 06:25:50,440 --> 06:25:52,520 organization and then we have multi-c 9079 06:25:52,520 --> 06:25:54,920 members which establishes a specific 9080 06:25:54,920 --> 06:25:57,200 Quorum that needs to be met in order for 9081 06:25:57,200 --> 06:25:59,840 a proposal to go through now to give you 9082 06:25:59,840 --> 06:26:02,280 some context both of these on the back 9083 06:26:02,280 --> 06:26:05,120 are plugins um which essentially extend 9084 06:26:05,120 --> 06:26:07,360 the functionality of what Dows can do 9085 06:26:07,360 --> 06:26:09,600 through enable them this specific uh 9086 06:26:09,600 --> 06:26:11,920 decisionmaking process in order to 9087 06:26:11,920 --> 06:26:14,360 manage the assets of the 9088 06:26:14,360 --> 06:26:16,320 organization however keep in mind this 9089 06:26:16,320 --> 06:26:18,360 can be anything and plugins can also 9090 06:26:18,360 --> 06:26:20,638 work to move uh funds they can be 9091 06:26:20,638 --> 06:26:22,360 treasury management mechanisms they can 9092 06:26:22,360 --> 06:26:23,840 be different types of coordination 9093 06:26:23,840 --> 06:26:25,958 Styles um and so really the 9094 06:26:25,958 --> 06:26:27,558 possibilities are endless when we speak 9095 06:26:27,558 --> 06:26:29,920 about uh customizing these types of 9096 06:26:29,920 --> 06:26:31,320 onchain 9097 06:26:31,320 --> 06:26:33,040 organizations then we're going to create 9098 06:26:33,040 --> 06:26:35,040 a token um this this can be something 9099 06:26:35,040 --> 06:26:37,360 like developer token we can call it 9100 06:26:37,360 --> 06:26:41,280 something like DVP um I can set up say a 9101 06:26:41,280 --> 06:26:44,600 th000 tokens for myself or add any other 9102 06:26:44,600 --> 06:26:46,558 address that I want to transfer uh some 9103 06:26:46,558 --> 06:26:50,520 funds to um then I establish what is the 9104 06:26:50,520 --> 06:26:52,958 minimum amount of tokens that someone 9105 06:26:52,958 --> 06:26:55,280 needs in order to create a proposal I'll 9106 06:26:55,280 --> 06:26:57,600 say 10 uh I could also just say like 9107 06:26:57,600 --> 06:27:00,200 allow any wet out there to vote um and 9108 06:27:00,200 --> 06:27:02,280 create proposals at which point uh there 9109 06:27:02,280 --> 06:27:04,840 is a risk of proposal spam so keeping 9110 06:27:04,840 --> 06:27:07,638 that in mind I'll just say that we need 9111 06:27:07,638 --> 06:27:10,958 um say 10 tokens in order to create a 9112 06:27:10,958 --> 06:27:13,400 proposal then we'll go through our 9113 06:27:13,400 --> 06:27:15,080 governance settings so what is the 9114 06:27:15,080 --> 06:27:17,040 minimum support threshold needed the 9115 06:27:17,040 --> 06:27:18,520 minimum participation needed for a 9116 06:27:18,520 --> 06:27:21,000 proposal to be valid how much times the 9117 06:27:21,000 --> 06:27:23,558 minimum duration the uh proposal needs 9118 06:27:23,558 --> 06:27:26,120 to be out there in order for it to uh be 9119 06:27:26,120 --> 06:27:28,478 counting the votes and then we can say 9120 06:27:28,478 --> 06:27:30,600 that we don't want early execution which 9121 06:27:30,600 --> 06:27:33,040 means that we wait for the entire time 9122 06:27:33,040 --> 06:27:35,680 of the duration of the proposal as well 9123 06:27:35,680 --> 06:27:37,718 as uh not enable people to change their 9124 06:27:37,718 --> 06:27:39,760 vote after they' voted I can also vote 9125 06:27:39,760 --> 06:27:42,558 yes to that and then the last step is 9126 06:27:42,558 --> 06:27:44,600 simply revising um those parameters that 9127 06:27:44,600 --> 06:27:47,200 have set so out of everything that we've 9128 06:27:47,200 --> 06:27:49,878 defined only the blockchain selection is 9129 06:27:49,878 --> 06:27:51,718 the one thing that we cannot change 9130 06:27:51,718 --> 06:27:53,760 later on since that's the blockchain 9131 06:27:53,760 --> 06:27:55,520 through which we're deploying our Dow 9132 06:27:55,520 --> 06:27:57,478 but really everything else um we can 9133 06:27:57,478 --> 06:27:59,400 change it later later on with a proposal 9134 06:27:59,400 --> 06:28:01,920 vote um and so the idea here is to 9135 06:28:01,920 --> 06:28:04,360 really enable these types of immutable 9136 06:28:04,360 --> 06:28:07,240 organizations to mutate and evolve with 9137 06:28:07,240 --> 06:28:11,080 time um so I'll sign in my proposal I go 9138 06:28:11,080 --> 06:28:13,920 back into my deployment um and so at 9139 06:28:13,920 --> 06:28:16,600 this stage what's happening on the back 9140 06:28:16,600 --> 06:28:18,718 is that Aragon is deploying this Dow 9141 06:28:18,718 --> 06:28:21,400 construct instance with uh the specific 9142 06:28:21,400 --> 06:28:23,878 plugins defined installed as we 9143 06:28:23,878 --> 06:28:25,600 determined throughing through the Dow 9144 06:28:25,600 --> 06:28:26,840 creation 9145 06:28:26,840 --> 06:28:29,718 process so once that's done we'll can 9146 06:28:29,718 --> 06:28:31,680 come into the dashboard of our Dow we'll 9147 06:28:31,680 --> 06:28:33,600 be able to see who is a member of our 9148 06:28:33,600 --> 06:28:36,280 organiz ation how many tokens are hold I 9149 06:28:36,280 --> 06:28:39,520 can create proposals I can make deposits 9150 06:28:39,520 --> 06:28:41,840 um and so this is where we'll be 9151 06:28:41,840 --> 06:28:44,240 managing the organization um keep in 9152 06:28:44,240 --> 06:28:46,840 mind that this is a a pretty um default 9153 06:28:46,840 --> 06:28:48,840 template that Aragon has set up as our 9154 06:28:48,840 --> 06:28:51,520 initial drafts we have a lot of 9155 06:28:51,520 --> 06:28:53,440 iterations coming in terms of the type 9156 06:28:53,440 --> 06:28:55,120 of plugins you'll be able to use the 9157 06:28:55,120 --> 06:28:57,160 different types of uh decision-making 9158 06:28:57,160 --> 06:28:59,160 strategies you'll be able to implement 9159 06:28:59,160 --> 06:29:01,080 and so the customizability really is 9160 06:29:01,080 --> 06:29:05,520 endless thank you very much 9161 06:29:07,320 --> 06:29:09,080 all right welcome back you now know a 9162 06:29:09,080 --> 06:29:11,920 ton about Dows and how they work and 9163 06:29:11,920 --> 06:29:14,478 we're are going to go over one of these 9164 06:29:14,478 --> 06:29:16,920 plutocracy Dows right one of these erc20 9165 06:29:16,920 --> 06:29:19,400 based Dows and how to build one yourself 9166 06:29:19,400 --> 06:29:22,840 from scratch however I want to challenge 9167 06:29:22,840 --> 06:29:25,878 you to not default to this a lot of 9168 06:29:25,878 --> 06:29:27,920 protocols default to okay I'm just going 9169 06:29:27,920 --> 06:29:29,600 to launch a governance token we're going 9170 06:29:29,600 --> 06:29:31,878 to be good to go and then a couple years 9171 06:29:31,878 --> 06:29:33,520 down the line they realized that that 9172 06:29:33,520 --> 06:29:35,478 was a horrible decision and they have a 9173 06:29:35,478 --> 06:29:37,080 hard time scaling their model they run 9174 06:29:37,080 --> 06:29:39,840 into issues where they can't separate 9175 06:29:39,840 --> 06:29:41,798 speculation on the token from the 9176 06:29:41,798 --> 06:29:43,798 utility of the token and there's a ton 9177 06:29:43,798 --> 06:29:46,000 of issues with that model so anybody 9178 06:29:46,000 --> 06:29:47,320 who's watching this if I catch you 9179 06:29:47,320 --> 06:29:49,478 launching a DA in the future and you do 9180 06:29:49,478 --> 06:29:52,400 not have a foolproof white paper as to 9181 06:29:52,400 --> 06:29:54,680 why you need a governance token I will 9182 06:29:54,680 --> 06:29:56,320 be very disappointed in you I know that 9183 06:29:56,320 --> 06:29:57,680 doesn't really matter to you but I will 9184 06:29:57,680 --> 06:29:59,478 be very disappointed in you and you will 9185 06:29:59,478 --> 06:30:04,160 make me sad so please don't make me sad 9186 06:30:05,160 --> 06:30:06,400 but we are going to learn this 9187 06:30:06,400 --> 06:30:10,000 methodology and how to do it so let's 9188 06:30:10,000 --> 06:30:12,478 jump in because guess what we only have 9189 06:30:12,478 --> 06:30:15,080 two lessons left and then you're ready 9190 06:30:15,080 --> 06:30:18,240 to go on your way all right you know 9191 06:30:18,240 --> 06:30:23,878 where to begin mkd boundary Dow f23 code 9192 06:30:23,878 --> 06:30:28,558 boundary Dow f23 or file open let's pull 9193 06:30:28,558 --> 06:30:30,798 up our vs code at this time you should 9194 06:30:30,798 --> 06:30:35,920 be pros at this part Forge in it 9195 06:30:36,160 --> 06:30:39,240 I don't want these three files boom boom 9196 06:30:39,240 --> 06:30:43,120 boom goodbye boom so let's do a little 9197 06:30:43,120 --> 06:30:44,440 read me and let's talk about what we're 9198 06:30:44,440 --> 06:30:48,878 going to do so one we are going to have 9199 06:30:48,878 --> 06:30:50,080 we're going to have a contract 9200 06:30:50,080 --> 06:30:53,398 controlled by a dow 100% controlled by a 9201 06:30:53,398 --> 06:30:55,680 dow every transaction that the Dow wants 9202 06:30:55,680 --> 06:30:58,398 to send has to be voted on and we will 9203 06:30:58,398 --> 06:31:00,718 use erc20 tokens for voting which like I 9204 06:31:00,718 --> 06:31:02,478 said this is kind of the default model 9205 06:31:02,478 --> 06:31:03,920 that the industry is currently using but 9206 06:31:03,920 --> 06:31:05,240 but I'm telling you it's not a great 9207 06:31:05,240 --> 06:31:07,280 model please research better ones and 9208 06:31:07,280 --> 06:31:09,478 make web 3 better but all right and this 9209 06:31:09,478 --> 06:31:11,080 is the model used by pretty much every 9210 06:31:11,080 --> 06:31:13,798 Dow at the moment compound Unis swap a 9211 06:31:13,798 --> 06:31:15,558 etc so I'm showing you the most popular 9212 06:31:15,558 --> 06:31:17,600 model but yes I'm telling you please 9213 06:31:17,600 --> 06:31:18,798 don't default to this please don't 9214 06:31:18,798 --> 06:31:21,280 always use this but let's start with 9215 06:31:21,280 --> 06:31:23,040 this first one we're going to have a 9216 06:31:23,040 --> 06:31:25,040 contract controlled by the Dow so let's 9217 06:31:25,040 --> 06:31:28,000 create a minimal contract that we can 9218 06:31:28,000 --> 06:31:30,200 vote on stuff for and we're going to do 9219 06:31:30,200 --> 06:31:31,718 something pretty similar to what we've 9220 06:31:31,718 --> 06:31:34,160 already done we're going to do SRC new 9221 06:31:34,160 --> 06:31:36,320 file we're going to make a box. soul 9222 06:31:36,320 --> 06:31:39,558 real similar to what we did before right 9223 06:31:39,558 --> 06:31:43,398 spdx license identifier MIT pragma 9224 06:31:43,398 --> 06:31:44,840 solidity 9225 06:31:44,840 --> 06:31:48,958 0.818 little carrot contract box let's 9226 06:31:48,958 --> 06:31:51,920 close this like this like we said we're 9227 06:31:51,920 --> 06:31:53,360 going to make this ownable it's going to 9228 06:31:53,360 --> 06:31:55,398 be controlled by the Dow so we're going 9229 06:31:55,398 --> 06:31:58,760 to do a little open Zeppelin work Forge 9230 06:31:58,760 --> 06:32:03,120 install open Zeppelin slash open 9231 06:32:03,120 --> 06:32:04,638 Zeppelin 9232 06:32:04,638 --> 06:32:08,280 contracts D- no- commit and since we're 9233 06:32:08,280 --> 06:32:10,200 doing this we're going to need to open 9234 06:32:10,200 --> 06:32:11,440 up our 9235 06:32:11,440 --> 06:32:14,558 founder. paste this in here uh I copied 9236 06:32:14,558 --> 06:32:16,080 this from a previous project opens up 9237 06:32:16,080 --> 06:32:18,200 on/ contracts lib opens up and blah blah 9238 06:32:18,200 --> 06:32:20,520 blah now since we have that we can do 9239 06:32:20,520 --> 06:32:26,000 import ownable from atop Zeppelin SLC 9240 06:32:26,000 --> 06:32:28,558 contracts SL 9241 06:32:28,558 --> 06:32:33,000 accessable Doo and our box is going to 9242 06:32:33,000 --> 06:32:35,000 be ownable and the Dow is going to own 9243 06:32:35,000 --> 06:32:38,200 it tracks all right cool going to be 9244 06:32:38,200 --> 06:32:41,760 real minimal contract un 256 private 9245 06:32:41,760 --> 06:32:44,558 number this obviously should be like 9246 06:32:44,558 --> 06:32:49,040 sore number right uh event number 9247 06:32:49,040 --> 06:32:52,680 changed looks like that function 9248 06:32:52,680 --> 06:32:57,920 store un 256 new value or new number 9249 06:32:57,920 --> 06:33:00,120 public this will be our only owner 9250 06:33:00,120 --> 06:33:03,478 function which this store function will 9251 06:33:03,478 --> 06:33:06,718 be only ownable will be only callable by 9252 06:33:06,718 --> 06:33:09,760 our Dow we'll say number equals new 9253 06:33:09,760 --> 06:33:11,878 number we'll even 9254 06:33:11,878 --> 06:33:15,558 emit number changed with the new number 9255 06:33:15,558 --> 06:33:18,360 this is sore number and then we'll do a 9256 06:33:18,360 --> 06:33:22,520 function read number or get number get 9257 06:33:22,520 --> 06:33:26,958 number public or external view returns 9258 06:33:26,958 --> 06:33:28,080 un 9259 06:33:28,080 --> 06:33:31,320 256 return s number and that's it that's 9260 06:33:31,320 --> 06:33:33,440 the whole contract boom that's it this 9261 06:33:33,440 --> 06:33:35,200 is kind of looks like box kind of looks 9262 06:33:35,200 --> 06:33:36,600 like symbol storage it's kind of an 9263 06:33:36,600 --> 06:33:39,840 amalgamation of both but this is it cool 9264 06:33:39,840 --> 06:33:41,320 we'll make sure it actually 9265 06:33:41,320 --> 06:33:44,638 compiles with Forge build make sure it 9266 06:33:44,638 --> 06:33:46,440 compiles looks like it's compiling okay 9267 06:33:46,440 --> 06:33:49,958 cool awesome so we have our box what's 9268 06:33:49,958 --> 06:33:53,478 next well every transaction that the Dow 9269 06:33:53,478 --> 06:33:55,160 wants to send has to be voted on we will 9270 06:33:55,160 --> 06:33:57,878 use erc20 tokens for voting so let's 9271 06:33:57,878 --> 06:34:03,200 create our our voting token here 9272 06:34:04,718 --> 06:34:07,840 new file go token. soul and this is 9273 06:34:07,840 --> 06:34:09,920 going to be our voting token now we 9274 06:34:09,920 --> 06:34:12,280 could implement this from scratch but 9275 06:34:12,280 --> 06:34:14,240 again what we can do for this is 9276 06:34:14,240 --> 06:34:15,600 actually just come to the contracts 9277 06:34:15,600 --> 06:34:18,920 wizard of open Zeppelin go to erc20 and 9278 06:34:18,920 --> 06:34:22,040 select votes and literally just copy 9279 06:34:22,040 --> 06:34:25,920 this to clipboard and paste it in or 9280 06:34:25,920 --> 06:34:28,920 copy this ourselves and paste it in here 9281 06:34:28,920 --> 06:34:30,600 this is going to be our voting token 9282 06:34:30,600 --> 06:34:33,080 that's it so let me talk to you about 9283 06:34:33,080 --> 06:34:35,440 what we just copy pasted and we're going 9284 06:34:35,440 --> 06:34:39,520 to update this to be named Imports 9285 06:34:39,520 --> 06:34:41,958 because named Imports are awesome this 9286 06:34:41,958 --> 06:34:43,958 draft erc20 permit we're just going to 9287 06:34:43,958 --> 06:34:48,040 import this erc20 permit and then erc20 9288 06:34:48,040 --> 06:34:50,040 votes so I'm just going to talk you 9289 06:34:50,040 --> 06:34:52,120 through what this is actually doing so 9290 06:34:52,120 --> 06:34:55,320 it's a regular erc20 token exactly as 9291 06:34:55,320 --> 06:34:57,360 we've seen but has these two kind of 9292 06:34:57,360 --> 06:35:01,000 extensions let me toggle the word wrap 9293 06:35:01,000 --> 06:35:02,680 and we have the opens up and contracts 9294 06:35:02,680 --> 06:35:04,320 installed okay pool so I'm going to 9295 06:35:04,320 --> 06:35:07,160 control command click into them again 9296 06:35:07,160 --> 06:35:09,320 you can open up the file viewer and you 9297 06:35:09,320 --> 06:35:10,440 know type the actual name of the 9298 06:35:10,440 --> 06:35:13,120 contract if you want erc20 permit what 9299 06:35:13,120 --> 06:35:16,760 does this actually do this allows 9300 06:35:16,760 --> 06:35:19,958 approvals to be made via signatures 9301 06:35:19,958 --> 06:35:22,160 basically you can sign a transaction 9302 06:35:22,160 --> 06:35:24,478 without sending it and let somebody else 9303 06:35:24,478 --> 06:35:27,080 send the transaction that's it it's 9304 06:35:27,080 --> 06:35:29,798 based off of this EIP we don't really 9305 06:35:29,798 --> 06:35:32,080 need to understand this too much but if 9306 06:35:32,080 --> 06:35:33,360 you're interested I would definitely 9307 06:35:33,360 --> 06:35:34,638 recommend recommend looking into it the 9308 06:35:34,638 --> 06:35:37,520 more important one is this erc20 votes 9309 06:35:37,520 --> 06:35:39,920 contract and like I said it's based off 9310 06:35:39,920 --> 06:35:42,958 of this compound voting in delegating it 9311 06:35:42,958 --> 06:35:45,558 does a lot of important things here 9312 06:35:45,558 --> 06:35:49,080 number one it keeps a history of each 9313 06:35:49,080 --> 06:35:51,798 account's voting power keeps snapshots 9314 06:35:51,798 --> 06:35:53,718 of them now this is important because 9315 06:35:53,718 --> 06:35:55,760 when you start a vote you want to use a 9316 06:35:55,760 --> 06:35:57,200 historical snapshot you don't want to 9317 06:35:57,200 --> 06:35:59,160 use the current balance of everyone's 9318 06:35:59,160 --> 06:36:01,000 tokens at the moment somebody could do 9319 06:36:01,000 --> 06:36:02,478 something called a flash loan which is 9320 06:36:02,478 --> 06:36:04,120 something we didn't talk about 9321 06:36:04,120 --> 06:36:06,760 but they basically can use this defi 9322 06:36:06,760 --> 06:36:09,558 thing to get a ton of tokens for a very 9323 06:36:09,558 --> 06:36:11,478 very short period of time and crash the 9324 06:36:11,478 --> 06:36:14,040 vote so we use a historical value and 9325 06:36:14,040 --> 06:36:15,638 the way to get that historical value is 9326 06:36:15,638 --> 06:36:18,638 to keep checkpoints of people's values 9327 06:36:18,638 --> 06:36:21,000 we can also use a delegate function on 9328 06:36:21,000 --> 06:36:23,398 our tokens sometimes we trust a person 9329 06:36:23,398 --> 06:36:26,200 more than we trust our understanding of 9330 06:36:26,200 --> 06:36:28,920 a topic so we can actually hold on to 9331 06:36:28,920 --> 06:36:30,878 our tokens still that tokens are still 9332 06:36:30,878 --> 06:36:32,680 ours but we can delegate the voting 9333 06:36:32,680 --> 06:36:35,080 rights of our tokens to somebody else 9334 06:36:35,080 --> 06:36:37,240 these are some of the two main features 9335 06:36:37,240 --> 06:36:40,120 that this token has if we scroll down we 9336 06:36:40,120 --> 06:36:42,280 can see we have this checkpoints 9337 06:36:42,280 --> 06:36:44,760 function which gets checkpoints and 9338 06:36:44,760 --> 06:36:47,160 there's this checkpoints mapping address 9339 06:36:47,160 --> 06:36:49,240 to checkpoint we can see the checkpoint 9340 06:36:49,240 --> 06:36:52,280 object says from block and then votes 9341 06:36:52,280 --> 06:36:55,360 and if we scroll down there's this Apon 9342 06:36:55,360 --> 06:36:57,680 token transfer function which anytime 9343 06:36:57,680 --> 06:37:00,320 you transfer a token this gets called it 9344 06:37:00,320 --> 06:37:02,638 ends up calling this move voting power 9345 06:37:02,638 --> 06:37:04,120 which does a whole bunch of of stuff in 9346 06:37:04,120 --> 06:37:06,958 here but the main things are it calls 9347 06:37:06,958 --> 06:37:09,160 this right checkpoint function so 9348 06:37:09,160 --> 06:37:12,080 anytime you move tokens by delegating 9349 06:37:12,080 --> 06:37:14,120 tokens or transferring your tokens you 9350 06:37:14,120 --> 06:37:16,080 write a new checkpoint right so you say 9351 06:37:16,080 --> 06:37:18,798 hey at this block this was people's 9352 06:37:18,798 --> 06:37:20,360 voting power and you don't do it for 9353 06:37:20,360 --> 06:37:21,760 every single block right you don't 9354 06:37:21,760 --> 06:37:23,000 calculate someone's checkpoint in every 9355 06:37:23,000 --> 06:37:24,520 single block you only calculate the 9356 06:37:24,520 --> 06:37:25,958 updates in the checkpoints so if we 9357 06:37:25,958 --> 06:37:28,200 scroll to the top for example says from 9358 06:37:28,200 --> 06:37:30,240 block this would be like you know let's 9359 06:37:30,240 --> 06:37:32,638 say you minted let's say you bought a 9360 06:37:32,638 --> 06:37:35,398 token at block and then you sold it at 9361 06:37:35,398 --> 06:37:37,718 block 25 this would just have two 9362 06:37:37,718 --> 06:37:39,840 checkpoints block five and block 25 9363 06:37:39,840 --> 06:37:42,080 that's it it adds some gas for every 9364 06:37:42,080 --> 06:37:43,920 single transfer you make but it allows 9365 06:37:43,920 --> 06:37:46,280 us to do these sophisticated voting 9366 06:37:46,280 --> 06:37:48,160 methodologies so and that's really it 9367 06:37:48,160 --> 06:37:49,958 other than that it's a basic erc20 token 9368 06:37:49,958 --> 06:37:51,760 just has these checkpoints has these 9369 06:37:51,760 --> 06:37:54,000 additional delegate functions as well 9370 06:37:54,000 --> 06:37:56,760 and that's really it and cool like 9371 06:37:56,760 --> 06:37:58,958 that's it our governance token is done 9372 06:37:58,958 --> 06:38:02,558 that's all you need to do 9373 06:38:04,000 --> 06:38:05,840 what else well guess what the open 9374 06:38:05,840 --> 06:38:07,760 Zeppelin wizard can help us with even 9375 06:38:07,760 --> 06:38:10,398 more CU what do we need next well we 9376 06:38:10,398 --> 06:38:12,920 need a doubt right we need a system we 9377 06:38:12,920 --> 06:38:15,840 need a contract to actually manage these 9378 06:38:15,840 --> 06:38:19,240 votes right and guess what the wizard 9379 06:38:19,240 --> 06:38:21,718 has one called Governor we can go 9380 06:38:21,718 --> 06:38:24,160 through this and update settings so we 9381 06:38:24,160 --> 06:38:25,558 can update the name if we want I'm going 9382 06:38:25,558 --> 06:38:27,558 to leave it as my Governor a voting 9383 06:38:27,558 --> 06:38:30,120 delay which is anytime somebody proposes 9384 06:38:30,120 --> 06:38:31,520 something it says okay we're going to 9385 06:38:31,520 --> 06:38:34,000 wait at least one block for the voting 9386 06:38:34,000 --> 06:38:37,040 to start maybe you do something like 100 9387 06:38:37,040 --> 06:38:39,600 blocks you know it depends on your chain 9388 06:38:39,600 --> 06:38:42,080 the voting period for how long the 9389 06:38:42,080 --> 06:38:44,478 voting will actually last a proposal 9390 06:38:44,478 --> 06:38:47,200 threshold where the minimum number of 9391 06:38:47,200 --> 06:38:49,398 votes an account must have to create a 9392 06:38:49,398 --> 06:38:51,520 proposal most of the time this is kept 9393 06:38:51,520 --> 06:38:55,040 zero a quarum percentage percentage of 9394 06:38:55,040 --> 06:38:56,958 tokens that need to vote in order for 9395 06:38:56,958 --> 06:38:59,320 something to pass if there's 100 tokens 9396 06:38:59,320 --> 06:39:01,680 in circulation at least four tokens need 9397 06:39:01,680 --> 06:39:04,160 to vote for it to pass make these 9398 06:39:04,160 --> 06:39:07,320 settings updatable or not this is this 9399 06:39:07,320 --> 06:39:08,920 is to make the contracts fully 9400 06:39:08,920 --> 06:39:10,680 compatible with Bravo which is the 9401 06:39:10,680 --> 06:39:13,160 compound implementation the type of 9402 06:39:13,160 --> 06:39:15,360 votes you can have erc20 votes you can 9403 06:39:15,360 --> 06:39:18,440 have ERC 721 votes which is nft or 9404 06:39:18,440 --> 06:39:21,600 comple a time lock controller anytime a 9405 06:39:21,600 --> 06:39:24,558 vote passes give users some amount of 9406 06:39:24,558 --> 06:39:26,520 time to leave the Dow before it's 9407 06:39:26,520 --> 06:39:28,798 implemented and then upgradeability we 9408 06:39:28,798 --> 06:39:30,478 probably want to avoid this as much as 9409 06:39:30,478 --> 06:39:32,160 we can because like I said before 9410 06:39:32,160 --> 06:39:34,558 security and with that I'm just going to 9411 06:39:34,558 --> 06:39:36,478 leave all the settings to be default we 9412 06:39:36,478 --> 06:39:39,040 can literally copy this into our 9413 06:39:39,040 --> 06:39:40,920 contract and not really have to do 9414 06:39:40,920 --> 06:39:44,360 anything so new file my 9415 06:39:44,360 --> 06:39:47,958 myor do Soul B this in and let's talk 9416 06:39:47,958 --> 06:39:49,718 about what is going on in this contract 9417 06:39:49,718 --> 06:39:51,600 what are these different functions so 9418 06:39:51,600 --> 06:39:55,760 first off let's update this to named 9419 06:40:00,920 --> 06:40:03,360 Imports and we also need to import IA 9420 06:40:03,360 --> 06:40:05,040 votes this is going to come from 9421 06:40:05,040 --> 06:40:07,200 Governor votes we're going to need to 9422 06:40:07,200 --> 06:40:08,920 import this time lock controller which 9423 06:40:08,920 --> 06:40:11,160 is going to come from this Governor time 9424 06:40:11,160 --> 06:40:13,958 lock and then we're going have to import 9425 06:40:13,958 --> 06:40:15,958 I Governor which is going to come from 9426 06:40:15,958 --> 06:40:18,760 governor and that's it so just update 9427 06:40:18,760 --> 06:40:20,320 these to named Imports and we're good to 9428 06:40:20,320 --> 06:40:21,920 go so let's walk through what this is 9429 06:40:21,920 --> 06:40:23,798 doing so my Governor is governor 9430 06:40:23,798 --> 06:40:25,160 governor settings Governor County blah 9431 06:40:25,160 --> 06:40:27,040 blah blah all this stuff I'm going to 9432 06:40:27,040 --> 06:40:28,718 give you the high level overview of what 9433 06:40:28,718 --> 06:40:31,240 all of these does this Governor contract 9434 06:40:31,240 --> 06:40:32,798 if we command click into it or control 9435 06:40:32,798 --> 06:40:34,040 click into it 9436 06:40:34,040 --> 06:40:36,240 the core of the governance system this 9437 06:40:36,240 --> 06:40:38,600 Governor contract we control click into 9438 06:40:38,600 --> 06:40:39,920 it it brings us to Governor votes but 9439 06:40:39,920 --> 06:40:41,160 let's go to the governor contract this 9440 06:40:41,160 --> 06:40:44,680 is kind of the main contract here and 9441 06:40:44,680 --> 06:40:46,920 how it works is it keeps this mapping 9442 06:40:46,920 --> 06:40:49,680 basically of proposals right each one of 9443 06:40:49,680 --> 06:40:53,478 these proposals have a vote start a vote 9444 06:40:53,478 --> 06:40:56,718 ended and then an executed or canceled 9445 06:40:56,718 --> 06:40:59,040 and that's really it every single time 9446 06:40:59,040 --> 06:41:01,280 somebody submits a proposal this mapping 9447 06:41:01,280 --> 06:41:04,638 gets updated and if we scroll down we 9448 06:41:04,638 --> 06:41:07,600 have this state function this is the 9449 06:41:07,600 --> 06:41:09,840 function that tells us if a proposal has 9450 06:41:09,840 --> 06:41:11,760 passed based off of this mapping of 9451 06:41:11,760 --> 06:41:14,360 proposals if it's executed if it's 9452 06:41:14,360 --> 06:41:17,878 cancelled what's the deadline has a quum 9453 06:41:17,878 --> 06:41:21,920 been reached Etc this is what most user 9454 06:41:21,920 --> 06:41:24,760 interfaces are going to read to see if a 9455 06:41:24,760 --> 06:41:26,840 vote has passed or not keep scrolling 9456 06:41:26,840 --> 06:41:29,360 down we can see a proposal snapshot 9457 06:41:29,360 --> 06:41:31,240 which is basically getting the deadline 9458 06:41:31,240 --> 06:41:33,558 when the proposal is due we can see some 9459 06:41:33,558 --> 06:41:35,718 stuff like a proposal threshold and then 9460 06:41:35,718 --> 06:41:37,718 a whole bunch of unimplemented functions 9461 06:41:37,718 --> 06:41:39,558 like Quorum reached vote success get 9462 06:41:39,558 --> 06:41:41,680 votes Etc and this is because this 9463 06:41:41,680 --> 06:41:43,478 Governor function is this abstract 9464 06:41:43,478 --> 06:41:45,920 contract meant to be extended on and 9465 06:41:45,920 --> 06:41:49,120 implemented with Governor votes is one 9466 06:41:49,120 --> 06:41:51,120 of these extensions where it says okay 9467 06:41:51,120 --> 06:41:53,280 erc20 token voting that's the way we're 9468 06:41:53,280 --> 06:41:55,120 going to do votes and this has a 9469 06:41:55,120 --> 06:41:57,600 function like get votes which reads the 9470 06:41:57,600 --> 06:41:59,520 token voting weight from the tokens 9471 06:41:59,520 --> 06:42:02,160 built-in snapshot mechanism to get how 9472 06:42:02,160 --> 06:42:03,920 often somebody can vote 9473 06:42:03,920 --> 06:42:05,878 uh and that type of thing now in our 9474 06:42:05,878 --> 06:42:08,280 governor one of the main functions that 9475 06:42:08,280 --> 06:42:11,200 we has is going to be propose and these 9476 06:42:11,200 --> 06:42:13,520 are going to be the variables it takes 9477 06:42:13,520 --> 06:42:16,120 in targets is just going to be like a 9478 06:42:16,120 --> 06:42:17,840 list of different contracts to make a 9479 06:42:17,840 --> 06:42:20,718 transaction to values is going to be a 9480 06:42:20,718 --> 06:42:23,680 list of values to send with each Target 9481 06:42:23,680 --> 06:42:25,200 call data now that we know all about 9482 06:42:25,200 --> 06:42:27,680 call data is going to be the byes data 9483 06:42:27,680 --> 06:42:29,080 that we're going to send with each 9484 06:42:29,080 --> 06:42:31,920 transaction and then just a description 9485 06:42:31,920 --> 06:42:33,638 right so for example this might be like 9486 06:42:33,638 --> 06:42:37,718 0 x0 blah blah blah some address here 9487 06:42:37,718 --> 06:42:39,520 and if it's just one it would just be 9488 06:42:39,520 --> 06:42:42,520 like you know one address here it's two 9489 06:42:42,520 --> 06:42:45,200 you know it might be like this Etc 9490 06:42:45,200 --> 06:42:47,120 values might be like you know something 9491 06:42:47,120 --> 06:42:49,520 like zero meaning the transaction we 9492 06:42:49,520 --> 06:42:52,040 send to this address we're just going to 9493 06:42:52,040 --> 06:42:54,440 pass zero value you know if there's two 9494 06:42:54,440 --> 06:42:57,440 transactions with two addresses like 0x 9495 06:42:57,440 --> 06:42:59,000 blah blah blah something or other you 9496 06:42:59,000 --> 06:43:02,000 know we could have two values right call 9497 06:43:02,000 --> 06:43:03,680 datas are going to be 9498 06:43:03,680 --> 06:43:07,120 the call data for sending to Here and 9499 06:43:07,120 --> 06:43:08,958 Now that we know about function 9500 06:43:08,958 --> 06:43:11,958 selectors function signatures and Etc we 9501 06:43:11,958 --> 06:43:14,600 know that with this this call datas 9502 06:43:14,600 --> 06:43:17,240 section allows us to make allows us to 9503 06:43:17,240 --> 06:43:20,240 call any function and really send any 9504 06:43:20,240 --> 06:43:22,680 data to this target that we want so it 9505 06:43:22,680 --> 06:43:25,398 makes it completely modular and then 9506 06:43:25,398 --> 06:43:26,878 some description right and this is 9507 06:43:26,878 --> 06:43:29,120 really just for the internal state of 9508 06:43:29,120 --> 06:43:30,878 the governor you know it might be 9509 06:43:30,878 --> 06:43:33,958 something like proposal 9510 06:43:33,958 --> 06:43:40,160 to send money to Dawn right and so if 9511 06:43:40,160 --> 06:43:42,478 this was like one two3 this is Don's 9512 06:43:42,478 --> 06:43:45,320 address cool we've just made a proposal 9513 06:43:45,320 --> 06:43:49,440 to send money to Don right if this was 9514 06:43:49,440 --> 06:43:51,718 you know like like the link token 9515 06:43:51,718 --> 06:43:53,520 contract for example we could have this 9516 06:43:53,520 --> 06:43:56,760 be zero we could find the function 9517 06:43:56,760 --> 06:43:59,080 selector for transfer we could be like 9518 06:43:59,080 --> 06:44:02,040 transfer function 9519 06:44:02,040 --> 06:44:04,878 selector and encode you know and then 9520 06:44:04,878 --> 06:44:07,080 encode all the data for the for 9521 06:44:07,080 --> 06:44:09,080 transferring some link we could then say 9522 06:44:09,080 --> 06:44:12,558 proposal to send link to Chris or 9523 06:44:12,558 --> 06:44:14,440 whatever we wanted to do here right so 9524 06:44:14,440 --> 06:44:16,478 this is how we can propose any 9525 06:44:16,478 --> 06:44:19,360 transaction for our Dow to do and send 9526 06:44:19,360 --> 06:44:21,000 proposals so really really important 9527 06:44:21,000 --> 06:44:23,920 function here and if we go through it we 9528 06:44:23,920 --> 06:44:26,558 can see it hashes the proposal it does a 9529 06:44:26,558 --> 06:44:28,840 whole bunch of checks in here it adds 9530 06:44:28,840 --> 06:44:31,440 our proposals to that proposals mapping 9531 06:44:31,440 --> 06:44:33,680 it gets a deadline picks a snapshot for 9532 06:44:33,680 --> 06:44:36,920 votes and everything emits a log and 9533 06:44:36,920 --> 06:44:40,080 importantly it returns a proposal ID The 9534 06:44:40,080 --> 06:44:44,160 Proposal ID is a hash of all the inputs 9535 06:44:44,160 --> 06:44:47,478 so that this proposal ID will be unique 9536 06:44:47,478 --> 06:44:50,000 then we have an uh another important 9537 06:44:50,000 --> 06:44:52,760 function called execute and all of the 9538 06:44:52,760 --> 06:44:55,240 inputs you can see are the same as the 9539 06:44:55,240 --> 06:44:58,240 inputs up here right the only difference 9540 06:44:58,240 --> 06:45:00,200 is it's saying a bytes 32 description 9541 06:45:00,200 --> 06:45:02,520 hash this is just going to be the 9542 06:45:02,520 --> 06:45:05,320 encrypt description from up here and 9543 06:45:05,320 --> 06:45:07,398 it's this function that gets called o 9544 06:45:07,398 --> 06:45:09,398 excuse me and it's this function this ex 9545 06:45:09,398 --> 06:45:11,160 Cube function that gets called once a 9546 06:45:11,160 --> 06:45:13,600 proposal has passed to actually send 9547 06:45:13,600 --> 06:45:17,240 that proposal right so here it first 9548 06:45:17,240 --> 06:45:19,478 reverse engineers and gets the proposal 9549 06:45:19,478 --> 06:45:22,080 ID from all those inputs which should be 9550 06:45:22,080 --> 06:45:24,360 the same as what was proposed and then 9551 06:45:24,360 --> 06:45:25,878 says okay let's get the status of that 9552 06:45:25,878 --> 06:45:27,760 proposal State based off of how the 9553 06:45:27,760 --> 06:45:30,718 votes go and if it's good to go then go 9554 06:45:30,718 --> 06:45:33,478 ahead and execute it right so we do some 9555 06:45:33,478 --> 06:45:35,878 execute stuff here the execute internal 9556 06:45:35,878 --> 06:45:38,040 function actually sends it with some 9557 06:45:38,040 --> 06:45:39,360 familiar stuff here right we're doing 9558 06:45:39,360 --> 06:45:43,000 targets. call values call datas Etc this 9559 06:45:43,000 --> 06:45:46,240 is this lowlevel functionality here it's 9560 06:45:46,240 --> 06:45:49,000 got some before execute stuff some after 9561 06:45:49,000 --> 06:45:50,280 Execute stuff we don't really need to 9562 06:45:50,280 --> 06:45:52,798 worry too much about that we can vote to 9563 06:45:52,798 --> 06:45:55,920 cancel transactions we can get votes and 9564 06:45:55,920 --> 06:45:57,280 then finally another incredibly 9565 06:45:57,280 --> 06:45:58,798 important function is going to be cast 9566 06:45:58,798 --> 06:46:01,320 vote and there's some derivatives of 9567 06:46:01,320 --> 06:46:03,320 this like cast vote with reason reason 9568 06:46:03,320 --> 06:46:05,558 cast vote with reason and pams cast vote 9569 06:46:05,558 --> 06:46:07,000 by Sig whole bunch of different ways to 9570 06:46:07,000 --> 06:46:10,000 cast vote but the main thing this cast 9571 06:46:10,000 --> 06:46:11,558 vote thing does is it calls this 9572 06:46:11,558 --> 06:46:13,440 internal cast vote function which calls 9573 06:46:13,440 --> 06:46:16,040 this internal cast vote function and 9574 06:46:16,040 --> 06:46:18,440 this is the actual function it calls so 9575 06:46:18,440 --> 06:46:20,280 it gets the proposal based off the 9576 06:46:20,280 --> 06:46:23,478 proposal ID it gets the weight of your 9577 06:46:23,478 --> 06:46:25,878 vote based off of the snapshot of the 9578 06:46:25,878 --> 06:46:28,120 tokens that you hold calls this internal 9579 06:46:28,120 --> 06:46:29,920 count vote function which basically just 9580 06:46:29,920 --> 06:46:33,600 updates some internal State variables 9581 06:46:33,600 --> 06:46:35,520 to add your votes to them and then 9582 06:46:35,520 --> 06:46:37,878 admits an event that's it right and 9583 06:46:37,878 --> 06:46:39,558 updates your vote based off of whatever 9584 06:46:39,558 --> 06:46:42,240 your weight is which in the system that 9585 06:46:42,240 --> 06:46:44,200 we're using the more tokens you have the 9586 06:46:44,200 --> 06:46:47,200 more you your vote counts and those are 9587 06:46:47,200 --> 06:46:49,080 the main functions here so hopefully 9588 06:46:49,080 --> 06:46:50,558 that made sense I know I kind of went 9589 06:46:50,558 --> 06:46:52,040 through that a little bit quickly but 9590 06:46:52,040 --> 06:46:53,478 that's kind of the basics of the 9591 06:46:53,478 --> 06:46:56,040 governor now Governor is the main 9592 06:46:56,040 --> 06:46:58,160 contract Governor settings is just an 9593 06:46:58,160 --> 06:47:00,320 extension to add stuff like voting delay 9594 06:47:00,320 --> 06:47:03,000 voting period voting threshold Etc 9595 06:47:03,000 --> 06:47:05,478 Governor counting simple is also an 9596 06:47:05,478 --> 06:47:07,600 extension uh which is just an extension 9597 06:47:07,600 --> 06:47:10,520 for a very simple counting mechanism 9598 06:47:10,520 --> 06:47:13,200 this extension allows our votes to be 9599 06:47:13,200 --> 06:47:16,120 against for and abstain and so we have 9600 06:47:16,120 --> 06:47:18,680 this proposal vote for each proposal 9601 06:47:18,680 --> 06:47:21,200 they get this struck and this is where 9602 06:47:21,200 --> 06:47:23,040 those votes get counted we have against 9603 06:47:23,040 --> 06:47:25,440 votes for votes abstain votes and then a 9604 06:47:25,440 --> 06:47:27,600 mapping of who has actually voted 9605 06:47:27,600 --> 06:47:30,280 Governor votes just implements this get 9606 06:47:30,280 --> 06:47:33,360 votes contract and Governor votes quorum 9607 06:47:33,360 --> 06:47:37,080 fraction this is just an extension or 9608 06:47:37,080 --> 06:47:39,878 token weight extraction and then we have 9609 06:47:39,878 --> 06:47:41,320 this important time lock controller 9610 06:47:41,320 --> 06:47:44,478 function every single Dow should 100% 9611 06:47:44,478 --> 06:47:46,638 always have this Governor time lock 9612 06:47:46,638 --> 06:47:49,160 control using this model means the 9613 06:47:49,160 --> 06:47:51,120 proposal will be operated by the time 9614 06:47:51,120 --> 06:47:53,958 lock controller and not the governor 9615 06:47:53,958 --> 06:47:56,718 thus the assets and permissions must be 9616 06:47:56,718 --> 06:47:59,200 attached to the time lock controller so 9617 06:47:59,200 --> 06:48:00,958 we're actually going to deploy a 9618 06:48:00,958 --> 06:48:03,040 contract called time lock that's going 9619 06:48:03,040 --> 06:48:06,280 to own the governor anytime our governor 9620 06:48:06,280 --> 06:48:08,360 contract votes on something and says 9621 06:48:08,360 --> 06:48:11,440 okay this vote has passed let's executed 9622 06:48:11,440 --> 06:48:13,520 the time lock controller will say okay 9623 06:48:13,520 --> 06:48:17,200 cool but you have to wait X days or 9624 06:48:17,200 --> 06:48:20,878 something you can't execute without some 9625 06:48:20,878 --> 06:48:22,840 delay and it's really important to have 9626 06:48:22,840 --> 06:48:26,440 this because if a dow votes on something 9627 06:48:26,440 --> 06:48:29,120 that a lot of people in the Dow hate it 9628 06:48:29,120 --> 06:48:31,600 gives them time to get out of the Dow 9629 06:48:31,600 --> 06:48:34,240 before they get steamr if you will so 9630 06:48:34,240 --> 06:48:35,958 really important to have this and that's 9631 06:48:35,958 --> 06:48:38,600 pretty much it so we have some uh voting 9632 06:48:38,600 --> 06:48:41,958 delay voting period Quorum State propose 9633 06:48:41,958 --> 06:48:43,760 we have all these uh which are just 9634 06:48:43,760 --> 06:48:46,520 calling super right that's pretty much 9635 06:48:46,520 --> 06:48:49,840 it and that's all we really need for our 9636 06:48:49,840 --> 06:48:51,878 governor contract right we have this 9637 06:48:51,878 --> 06:48:54,120 Constructor which takes the token that 9638 06:48:54,120 --> 06:48:56,558 we vote on as an input parameter and the 9639 06:48:56,558 --> 06:48:58,680 timelock controller that's going to 9640 06:48:58,680 --> 06:49:01,200 block us from block us from executing 9641 06:49:01,200 --> 06:49:03,120 proposals right away time lock 9642 06:49:03,120 --> 06:49:04,638 controller contract is going to own our 9643 06:49:04,638 --> 06:49:07,040 governor controller contract and that's 9644 06:49:07,040 --> 06:49:09,360 actually the last contract we need to 9645 06:49:09,360 --> 06:49:12,398 create a Time lock. Soul so this is the 9646 06:49:12,398 --> 06:49:14,040 only one we have to build ourselves but 9647 06:49:14,040 --> 06:49:15,760 we actually barely have to build it 9648 06:49:15,760 --> 06:49:19,040 ourselves SPX license identifier pragma 9649 06:49:19,040 --> 06:49:22,080 solidity 0. 9650 06:49:22,080 --> 06:49:27,120 8.18 carat contract time lock and we we 9651 06:49:27,120 --> 06:49:29,000 barely have to implement this because 9652 06:49:29,000 --> 06:49:30,798 open Zeppelin already has one of these 9653 06:49:30,798 --> 06:49:33,280 so we just say our time lock is 9654 06:49:33,280 --> 06:49:36,520 time lock controller and then we can 9655 06:49:36,520 --> 06:49:40,240 just import it import at open Zeppelin 9656 06:49:40,240 --> 06:49:45,760 SL contracts SLG governance time lock 9657 06:49:45,760 --> 06:49:49,040 controller. let's import it like this 9658 06:49:49,040 --> 06:49:51,840 from I spell that right time lock 9659 06:49:51,840 --> 06:49:54,718 controller okay that's more right 9660 06:49:54,718 --> 06:49:57,920 governance governance contracts 9661 06:49:57,920 --> 06:50:01,478 contracts open Zeppelin open Zeppelin 9662 06:50:01,478 --> 06:50:04,680 time lock cont controller time lock 9663 06:50:04,680 --> 06:50:07,600 controller okay cool that looks good now 9664 06:50:07,600 --> 06:50:09,120 we'll just do a 9665 06:50:09,120 --> 06:50:11,080 Constructor and we'll pass some 9666 06:50:11,080 --> 06:50:14,120 parameters in here we'll say U 256 Min 9667 06:50:14,120 --> 06:50:16,398 delay which we'll put a little comma 9668 06:50:16,398 --> 06:50:18,878 here this is going to be the Min delay 9669 06:50:18,878 --> 06:50:22,798 is how long you have to wait before 9670 06:50:22,798 --> 06:50:25,000 executing we're going to have to pass 9671 06:50:25,000 --> 06:50:26,840 and we can find all of these in the 9672 06:50:26,840 --> 06:50:28,920 Constructor of 9673 06:50:28,920 --> 06:50:32,360 this m delay proposers executors and 9674 06:50:32,360 --> 06:50:34,080 admin right this is the Constructor of 9675 06:50:34,080 --> 06:50:35,360 the time lock controller so we're going 9676 06:50:35,360 --> 06:50:36,638 to need to have this be the Constructor 9677 06:50:36,638 --> 06:50:39,360 of hours as well M delay we're going to 9678 06:50:39,360 --> 06:50:40,478 need 9679 06:50:40,478 --> 06:50:45,160 proposers is the list of addresses that 9680 06:50:45,160 --> 06:50:47,680 can propose so we need an initial list 9681 06:50:47,680 --> 06:50:49,558 of proposers we're going to update this 9682 06:50:49,558 --> 06:50:52,520 so that it can be anybody and then xeq 9683 06:50:52,520 --> 06:50:57,000 tors is the list of addresses that can 9684 06:50:57,000 --> 06:50:59,398 execute execute we're going to update 9685 06:50:59,398 --> 06:51:02,680 this as well so that anytime a vote pass 9686 06:51:02,680 --> 06:51:05,718 is anybody can execute it so M 9687 06:51:05,718 --> 06:51:10,160 delay address array memory proposers 9688 06:51:10,160 --> 06:51:13,840 address array memory 9689 06:51:13,840 --> 06:51:17,160 xter boom and since this time lock 9690 06:51:17,160 --> 06:51:19,600 controller parent class has a 9691 06:51:19,600 --> 06:51:22,440 Constructor I'm tle the word WP we going 9692 06:51:22,440 --> 06:51:24,920 to put those in here and it's just 9693 06:51:24,920 --> 06:51:27,080 exactly the Constructor parameters we 9694 06:51:27,080 --> 06:51:30,920 created in delay proposers 9695 06:51:30,920 --> 06:51:33,160 xors computers 9696 06:51:33,160 --> 06:51:34,200 and then we also have to give it an 9697 06:51:34,200 --> 06:51:35,520 admin which we're just going to say is 9698 06:51:35,520 --> 06:51:37,360 message. sender for now and we need to 9699 06:51:37,360 --> 06:51:39,520 give it an admin at first so that we can 9700 06:51:39,520 --> 06:51:41,200 eventually move the Tim lock controllers 9701 06:51:41,200 --> 06:51:43,520 admin so that only the Dow can do 9702 06:51:43,520 --> 06:51:45,558 anything with the time lock controller 9703 06:51:45,558 --> 06:51:47,718 and you'll see us do that in a minute 9704 06:51:47,718 --> 06:51:49,000 but those are all the contracts that we 9705 06:51:49,000 --> 06:51:50,638 need that's it we've written the 9706 06:51:50,638 --> 06:51:52,320 contracts we're done like I said I'm 9707 06:51:52,320 --> 06:51:54,240 going to do this kind of quickly I do 9708 06:51:54,240 --> 06:51:56,280 recommend that after this you go back 9709 06:51:56,280 --> 06:51:57,520 and you really try to understand what's 9710 06:51:57,520 --> 06:51:59,000 going on here because I I know I'm kind 9711 06:51:59,000 --> 06:52:00,558 of doing this quickly but now we're 9712 06:52:00,558 --> 06:52:01,920 going to write a test to show it 9713 06:52:01,920 --> 06:52:03,398 actually work and that's it I'm not 9714 06:52:03,398 --> 06:52:05,440 going to write a deploy script here uh 9715 06:52:05,440 --> 06:52:06,798 if you want to go back and you want to 9716 06:52:06,798 --> 06:52:08,798 write a deploy script that's great but 9717 06:52:08,798 --> 06:52:10,160 I'm just going to write a test to show 9718 06:52:10,160 --> 06:52:12,958 you basically endtoend exactly the 9719 06:52:12,958 --> 06:52:17,120 process that happens in one of these 9720 06:52:18,798 --> 06:52:21,360 Dows so let's go ahead let's write the 9721 06:52:21,360 --> 06:52:26,120 test my Governor test. 9722 06:52:26,120 --> 06:52:28,160 T.O and the other reason that I'm going 9723 06:52:28,160 --> 06:52:29,680 a little bit quicker here is because at 9724 06:52:29,680 --> 06:52:31,958 this point you understand a lot of this 9725 06:52:31,958 --> 06:52:34,120 right and you know and should know how 9726 06:52:34,120 --> 06:52:36,360 to go back and flush this repo out right 9727 06:52:36,360 --> 06:52:39,520 with scripts with tests unit tests fuzz 9728 06:52:39,520 --> 06:52:41,478 tests Etc and you know how to make this 9729 06:52:41,478 --> 06:52:44,600 project badass and I'm just I just want 9730 06:52:44,600 --> 06:52:46,120 to give you the new stuff right teach 9731 06:52:46,120 --> 06:52:47,360 you the new stuff for here teach you 9732 06:52:47,360 --> 06:52:50,320 about the Dow stuff so let's write some 9733 06:52:50,320 --> 06:52:52,840 test just so that we can really see this 9734 06:52:52,840 --> 06:52:55,040 process work end to end and we're just 9735 06:52:55,040 --> 06:52:56,798 going to have one giant test just to 9736 06:52:56,798 --> 06:52:59,120 show the whole process so you know the 9737 06:52:59,120 --> 06:53:02,920 drill pragma solidity here contract my 9738 06:53:02,920 --> 06:53:06,320 Governor test is test you already know 9739 06:53:06,320 --> 06:53:11,240 we're going to import test from Forge 9740 06:53:11,240 --> 06:53:13,398 STD test. 9741 06:53:13,398 --> 06:53:16,840 soul and we're going to need a function 9742 06:53:16,840 --> 06:53:20,478 function setup it's going to be Public 9743 06:53:20,478 --> 06:53:22,760 public of course and we're going to need 9744 06:53:22,760 --> 06:53:24,798 a whole bunch of stuff in here function 9745 06:53:24,798 --> 06:53:26,920 setup oops what are we going to need 9746 06:53:26,920 --> 06:53:28,920 well we're probably going to need at 9747 06:53:28,920 --> 06:53:31,520 least these four contracts so let's go 9748 06:53:31,520 --> 06:53:33,320 ahead and import them 9749 06:53:33,320 --> 06:53:35,398 import 9750 06:53:35,398 --> 06:53:38,320 myga nor 9751 06:53:38,320 --> 06:53:46,958 from as Maga veror do Soul import box 9752 06:53:46,958 --> 06:53:50,000 from Sr cbox 9753 06:53:50,000 --> 06:53:54,440 Doo import time lock from here hit up 9754 06:53:54,440 --> 06:53:57,638 co-pilot taking over a little bit import 9755 06:53:57,638 --> 06:53:59,760 gov token okay it failed it almost took 9756 06:53:59,760 --> 06:54:03,080 over here from SRC go 9757 06:54:03,080 --> 06:54:06,558 token. s oh is that not what it's called 9758 06:54:06,558 --> 06:54:08,240 ah sorry let's change this name to gov 9759 06:54:08,240 --> 06:54:12,398 token like this it's good okay so now 9760 06:54:12,398 --> 06:54:14,520 let's actually deploy some of these so 9761 06:54:14,520 --> 06:54:18,878 our gov token say actually let's create 9762 06:54:18,878 --> 06:54:21,120 each one of these my governor 9763 06:54:21,120 --> 06:54:28,120 governor box box time lock time lock go 9764 06:54:28,120 --> 06:54:32,398 token go token cool first thing that 9765 06:54:32,398 --> 06:54:35,320 we're going to need is our gov token 9766 06:54:35,320 --> 06:54:38,520 equals new gov token and I know this 9767 06:54:38,520 --> 06:54:40,958 doesn't take any input parameters but we 9768 06:54:40,958 --> 06:54:44,558 should give our gov token we should 9769 06:54:44,558 --> 06:54:46,760 actually mint some of these this gov 9770 06:54:46,760 --> 06:54:49,280 token doesn't have a mint function so 9771 06:54:49,280 --> 06:54:50,958 I'm just going to create a mint function 9772 06:54:50,958 --> 06:54:53,200 but in your system maybe you just have 9773 06:54:53,200 --> 06:54:54,920 the Constructor mint to the message. 9774 06:54:54,920 --> 06:54:56,320 sender I'm just going to go ahead and 9775 06:54:56,320 --> 06:55:01,718 add this function mint address 2 256 9776 06:55:01,718 --> 06:55:03,360 amount 9777 06:55:03,360 --> 06:55:06,040 make this public um this probably isn't 9778 06:55:06,040 --> 06:55:07,958 what you want your token to have you 9779 06:55:07,958 --> 06:55:09,240 probably don't want anyone to be able to 9780 06:55:09,240 --> 06:55:11,160 Mint any tokens but this is what I'm 9781 06:55:11,160 --> 06:55:14,040 going to do mint to amount that's it 9782 06:55:14,040 --> 06:55:16,240 there's an internal function called mint 9783 06:55:16,240 --> 06:55:17,958 that we're going to call so I'm just 9784 06:55:17,958 --> 06:55:20,080 going to say I'm going to Mint to some 9785 06:55:20,080 --> 06:55:22,200 user I'm going say 9786 06:55:22,200 --> 06:55:26,360 address public user equals make address 9787 06:55:26,360 --> 06:55:28,680 this is going to be our user here do 9788 06:55:28,680 --> 06:55:33,798 this capitals user like this say gov 9789 06:55:33,798 --> 06:55:38,718 token. mint user and we'll do un 256 9790 06:55:38,718 --> 06:55:40,200 public 9791 06:55:40,200 --> 06:55:44,840 constants yeah initial Supply equals 100 9792 06:55:44,840 --> 06:55:46,958 ether we're going to Mint ourselves 100 9793 06:55:46,958 --> 06:55:50,878 ether boom all right cool now something 9794 06:55:50,878 --> 06:55:53,600 that you'll often forget is that just 9795 06:55:53,600 --> 06:55:55,638 because you minted tokens doesn't mean 9796 06:55:55,638 --> 06:55:58,440 you've have any voting power so we want 9797 06:55:58,440 --> 06:56:03,000 to run gov token. deligate delegate 9798 06:56:03,000 --> 06:56:04,638 function and we're just going to 9799 06:56:04,638 --> 06:56:06,440 delegate to ourselves so we're saying 9800 06:56:06,440 --> 06:56:09,360 hey we now have 100 tokens we're going 9801 06:56:09,360 --> 06:56:11,320 to delegate those 100 tokens voting 9802 06:56:11,320 --> 06:56:13,600 power to ourselves you could delegate it 9803 06:56:13,600 --> 06:56:15,120 to somebody else if you wanted to but 9804 06:56:15,120 --> 06:56:17,718 for our test we're going to do it for us 9805 06:56:17,718 --> 06:56:20,240 and we need to actually do vm. start 9806 06:56:20,240 --> 06:56:22,958 prank user because only the user can 9807 06:56:22,958 --> 06:56:25,240 delegate tokens to themselves and we 9808 06:56:25,240 --> 06:56:26,920 might as well stay as the user for these 9809 06:56:26,920 --> 06:56:28,840 next ones just that we can start off as 9810 06:56:28,840 --> 06:56:31,080 owners of the contracts we're about to 9811 06:56:31,080 --> 06:56:34,718 do so now that we have our governance 9812 06:56:34,718 --> 06:56:37,440 token next what we want to deploy is our 9813 06:56:37,440 --> 06:56:39,520 time lock because in order to deploy our 9814 06:56:39,520 --> 06:56:41,558 governor we're going to need both the 9815 06:56:41,558 --> 06:56:43,440 gov token and the time lock so we have 9816 06:56:43,440 --> 06:56:44,878 the gov token let's actually create the 9817 06:56:44,878 --> 06:56:46,878 time lock now so we're going to say time 9818 06:56:46,878 --> 06:56:51,200 lock equals new time lock and what does 9819 06:56:51,200 --> 06:56:56,680 this take M delay posers executors so 9820 06:56:56,680 --> 06:56:59,200 let's create those going to scroll up 9821 06:56:59,200 --> 06:57:02,478 we're going to make un 256 public 9822 06:57:02,478 --> 06:57:07,718 constant Min delay delay equals 9823 06:57:07,718 --> 06:57:11,000 360 0 which is going to be 1 hour this 9824 06:57:11,000 --> 06:57:15,040 is the delay after a vote passes right 9825 06:57:15,040 --> 06:57:18,440 so this is the time locks M delay nobody 9826 06:57:18,440 --> 06:57:20,958 can actually execute a past proposal 9827 06:57:20,958 --> 06:57:22,958 until an hour goes by boom we'll do M 9828 06:57:22,958 --> 06:57:26,320 delay what else do we need we need the 9829 06:57:26,320 --> 06:57:29,398 proposers for stuff and we'll just make 9830 06:57:29,398 --> 06:57:32,638 this at the top address array Pro 9831 06:57:32,638 --> 06:57:36,080 proposers address array 9832 06:57:36,080 --> 06:57:39,240 executors and we'll just leave it blank 9833 06:57:39,240 --> 06:57:41,958 because we don't care proposers 9834 06:57:41,958 --> 06:57:43,958 executors and this is actually how with 9835 06:57:43,958 --> 06:57:45,440 the time lock controller you say anyone 9836 06:57:45,440 --> 06:57:47,680 can propose and anybody can execute you 9837 06:57:47,680 --> 06:57:50,680 just leave these arrays blank cool looks 9838 06:57:50,680 --> 06:57:53,240 good okay now that we have that we 9839 06:57:53,240 --> 06:57:54,920 finally can deploy our governor contract 9840 06:57:54,920 --> 06:57:58,478 CU we have these two so we'll say my or 9841 06:57:58,478 --> 06:57:59,680 what do we call it do we just call it 9842 06:57:59,680 --> 06:58:03,080 Governor yep governor 9843 06:58:03,080 --> 06:58:06,520 equals new my 9844 06:58:06,520 --> 06:58:08,920 Governor go 9845 06:58:08,920 --> 06:58:13,080 token time lock like that now we have to 9846 06:58:13,080 --> 06:58:15,280 Grant some roles so the time lock 9847 06:58:15,280 --> 06:58:17,080 actually starts with some default roles 9848 06:58:17,080 --> 06:58:20,360 and we need to Grant Governor a whole 9849 06:58:20,360 --> 06:58:21,798 bunch of roles and then we need to 9850 06:58:21,798 --> 06:58:25,440 remove ourselves as the admin of the 9851 06:58:25,440 --> 06:58:27,398 time lock right we don't want single 9852 06:58:27,398 --> 06:58:29,520 centralized entity to have power over it 9853 06:58:29,520 --> 06:58:31,280 so first we got to get some of the roles 9854 06:58:31,280 --> 06:58:34,600 so we're going to say byes 32 proposer 9855 06:58:34,600 --> 06:58:36,798 roll and the way that the time lock has 9856 06:58:36,798 --> 06:58:40,120 roles is it hashes the names of stuff so 9857 06:58:40,120 --> 06:58:43,520 we'll say proposer rooll equals time 9858 06:58:43,520 --> 06:58:44,840 lock 9859 06:58:44,840 --> 06:58:47,798 proposer Ro there's this function on the 9860 06:58:47,798 --> 06:58:50,440 time lock called proposer role uh and 9861 06:58:50,440 --> 06:58:52,760 we're going to allow this proposal Ro to 9862 06:58:52,760 --> 06:58:55,200 be just the governor so only the 9863 06:58:55,200 --> 06:58:57,520 governor can propose stuff to the time 9864 06:58:57,520 --> 06:59:03,120 lock say bytes 32 xq tour Q 9865 06:59:03,120 --> 06:59:05,878 rooll is going to be Tim lock. executor 9866 06:59:05,878 --> 06:59:08,360 ro we're going to give this to anybody 9867 06:59:08,360 --> 06:59:09,958 by setting it to the zero address and 9868 06:59:09,958 --> 06:59:13,920 then byes 32 admin roll right now that's 9869 06:59:13,920 --> 06:59:17,760 us time lock dot nope it's going to be 9870 06:59:17,760 --> 06:59:21,000 time lock admin roll and so like I said 9871 06:59:21,000 --> 06:59:22,798 these are just kind of the um these are 9872 06:59:22,798 --> 06:59:25,040 hashes in the time lock and we just need 9873 06:59:25,040 --> 06:59:27,120 to update these we're going to say time 9874 06:59:27,120 --> 06:59:30,280 lock time lock dot it has a grant roll 9875 06:59:30,280 --> 06:59:31,760 function we're going to Grant this 9876 06:59:31,760 --> 06:59:34,920 propos propos a role to the governor so 9877 06:59:34,920 --> 06:59:37,840 only the governor can actually propose 9878 06:59:37,840 --> 06:59:41,520 stuff to the time lock or say time lock. 9879 06:59:41,520 --> 06:59:47,718 Grant roll xq rooll to address zero 9880 06:59:47,718 --> 06:59:49,878 which means anybody can execute a past 9881 06:59:49,878 --> 06:59:52,840 proposal and then time lock. revoke 9882 06:59:52,840 --> 06:59:58,040 rooll revoke the admin role from user so 9883 06:59:58,040 --> 07:00:01,600 the user will no longer be the admin and 9884 07:00:01,600 --> 07:00:05,840 then we'll say bm. stop prank this then 9885 07:00:05,840 --> 07:00:07,840 we just need a new box box equals new 9886 07:00:07,840 --> 07:00:10,240 box and now we're going to transfer 9887 07:00:10,240 --> 07:00:12,478 ownership of the box to the time lock 9888 07:00:12,478 --> 07:00:14,440 the time lock is owned by the Dow so 9889 07:00:14,440 --> 07:00:17,280 we're going to say box. transfer 9890 07:00:17,280 --> 07:00:23,040 ownership to address time lock so 9891 07:00:23,040 --> 07:00:26,478 transfer ownership a common 9892 07:00:26,478 --> 07:00:28,680 misconception here is oh shouldn't we 9893 07:00:28,680 --> 07:00:31,478 transfer to the Dow no we should 9894 07:00:31,478 --> 07:00:33,360 transfer to to the time lock so the time 9895 07:00:33,360 --> 07:00:36,680 lock owns the Dow so the time lock owns 9896 07:00:36,680 --> 07:00:38,240 the Dow and the Dow owns the time lock 9897 07:00:38,240 --> 07:00:40,760 it's kind of this weird two-way 9898 07:00:40,760 --> 07:00:42,958 relationship but it's the time lock that 9899 07:00:42,958 --> 07:00:45,040 gets the ultimate say on where stuff 9900 07:00:45,040 --> 07:00:48,478 goes so we transfer it to the down and 9901 07:00:48,478 --> 07:00:50,878 so we can even do a quick test function 9902 07:00:50,878 --> 07:00:54,638 test can't update box 9903 07:00:54,638 --> 07:00:59,920 with governance public oops public we'll 9904 07:00:59,920 --> 07:01:05,440 say vm. expect revert box. store one 9905 07:01:05,440 --> 07:01:08,080 like this since our 9906 07:01:08,080 --> 07:01:11,080 box has a store function that's only 9907 07:01:11,080 --> 07:01:13,120 owner the owner is now the time lock SL 9908 07:01:13,120 --> 07:01:16,920 the Dow oops if I try to run this test 9909 07:01:16,920 --> 07:01:20,558 right Forge test DM paste this in this 9910 07:01:20,558 --> 07:01:23,240 should pass 9911 07:01:23,240 --> 07:01:25,958 because you can't update the Box unless 9912 07:01:25,958 --> 07:01:27,160 it's through 9913 07:01:27,160 --> 07:01:28,920 governance and this will also be our 9914 07:01:28,920 --> 07:01:31,240 sanity check to make sure our uh all of 9915 07:01:31,240 --> 07:01:33,160 our code is compos which it is okay 9916 07:01:33,160 --> 07:01:35,638 great let's keep going so now let's make 9917 07:01:35,638 --> 07:01:39,878 a giant function function test 9918 07:01:39,878 --> 07:01:44,040 governance updates box public and this 9919 07:01:44,040 --> 07:01:47,080 will show you the exact process from a 9920 07:01:47,080 --> 07:01:50,320 code standpoint of how one of these Dows 9921 07:01:50,320 --> 07:01:52,718 actually work so first let's get started 9922 07:01:52,718 --> 07:01:59,200 un 256 value to store is going to be 888 9923 07:01:59,200 --> 07:02:01,958 boom so we're going to update our box 9924 07:02:01,958 --> 07:02:03,240 right this box here we're going to 9925 07:02:03,240 --> 07:02:07,000 update this new number to 888 okay let's 9926 07:02:07,000 --> 07:02:09,040 come up with a proposal right remember 9927 07:02:09,040 --> 07:02:11,398 if we show up my Governor we click into 9928 07:02:11,398 --> 07:02:14,680 governor in uh and we click into 9929 07:02:14,680 --> 07:02:17,638 Governor function 9930 07:02:17,638 --> 07:02:20,360 propose right here first thing we need 9931 07:02:20,360 --> 07:02:23,320 to do to kick off anything is propose 9932 07:02:23,320 --> 07:02:25,638 something hey we are going to propose 9933 07:02:25,638 --> 07:02:29,160 that box updates the stored value to 888 9934 07:02:29,160 --> 07:02:30,878 so we're going to need to have all this 9935 07:02:30,878 --> 07:02:33,558 stuff in here Target values call datas 9936 07:02:33,558 --> 07:02:35,200 descriptions so let's start with 9937 07:02:35,200 --> 07:02:36,680 description because that's simplest 9938 07:02:36,680 --> 07:02:41,958 right string memory D description equals 9939 07:02:41,958 --> 07:02:46,280 store one in box that's it that's our 9940 07:02:46,280 --> 07:02:49,478 description pretty easy right what else 9941 07:02:49,478 --> 07:02:51,200 do we need okay we're going to need some 9942 07:02:51,200 --> 07:02:53,680 call datas oh well we know what call 9943 07:02:53,680 --> 07:02:55,878 datas are that's that function selector 9944 07:02:55,878 --> 07:02:57,360 and all that other stuff that we learned 9945 07:02:57,360 --> 07:03:00,478 in that nft one right so we can say 9946 07:03:00,478 --> 07:03:02,478 bytes memory 9947 07:03:02,478 --> 07:03:05,160 so if you go back to The Foundry nft f23 9948 07:03:05,160 --> 07:03:09,520 we go to SRC subl lesson we can remember 9949 07:03:09,520 --> 07:03:11,920 oh wait we can we can call anything that 9950 07:03:11,920 --> 07:03:14,718 we want right and all we got to do is 9951 07:03:14,718 --> 07:03:18,040 encode stuff right like ai. encode with 9952 07:03:18,040 --> 07:03:20,840 signature or encode with selector or 9953 07:03:20,840 --> 07:03:23,200 whatever we want to do here so we can 9954 07:03:23,200 --> 07:03:26,080 actually say oh okay so this allows us 9955 07:03:26,080 --> 07:03:29,478 to make any function call that we want 9956 07:03:29,478 --> 07:03:32,320 so we'll encode it by saying encode 9957 07:03:32,320 --> 07:03:35,718 function call equals we want to well 9958 07:03:35,718 --> 07:03:36,958 what do we want to call well we want to 9959 07:03:36,958 --> 07:03:39,840 call this store function ah so we know 9960 07:03:39,840 --> 07:03:41,600 the function signature of store is going 9961 07:03:41,600 --> 07:03:45,440 to be store unit 256 so we can say ai. 9962 07:03:45,440 --> 07:03:48,920 encode with signature we're going to 9963 07:03:48,920 --> 07:03:52,798 call the store function like this and 9964 07:03:52,798 --> 07:03:54,920 then what else do we need well we need 9965 07:03:54,920 --> 07:03:59,760 the value like this and that's it cool 9966 07:03:59,760 --> 07:04:02,320 let me toggle the word rep here cool so 9967 07:04:02,320 --> 07:04:04,040 we have the encoded function call so we 9968 07:04:04,040 --> 07:04:07,160 have the call datas that we need to call 9969 07:04:07,160 --> 07:04:09,920 in okay great what do we need next and 9970 07:04:09,920 --> 07:04:11,360 we're going kind of in this reverse 9971 07:04:11,360 --> 07:04:14,920 order values uh well we don't really 9972 07:04:14,920 --> 07:04:17,200 care to send any value right so let's 9973 07:04:17,200 --> 07:04:20,200 even just go to the top here we'll make 9974 07:04:20,200 --> 07:04:24,360 a un 256 array values it'll start off as 9975 07:04:24,360 --> 07:04:30,000 blank we'll just say values. push Z cool 9976 07:04:30,000 --> 07:04:31,478 we'll just have it be empty we're not 9977 07:04:31,478 --> 07:04:33,840 going to send any eth and then the final 9978 07:04:33,840 --> 07:04:35,878 thing we need is targets so we want to 9979 07:04:35,878 --> 07:04:38,398 call this on the box contract right oh 9980 07:04:38,398 --> 07:04:41,718 and actually call datas is also an array 9981 07:04:41,718 --> 07:04:43,600 so we're going to do a couple things 9982 07:04:43,600 --> 07:04:45,600 we're going to do 9983 07:04:45,600 --> 07:04:48,320 values all 9984 07:04:48,320 --> 07:04:52,920 datas and targets targets right so the 9985 07:04:52,920 --> 07:04:55,878 call datas is a un 256 no call datas is 9986 07:04:55,878 --> 07:04:59,520 a bytes array targets is an address 9987 07:04:59,520 --> 07:05:01,958 array so we're going to say values. push 9988 07:05:01,958 --> 07:05:05,280 zero call datas do push that encoded 9989 07:05:05,280 --> 07:05:07,638 function call that we just made and then 9990 07:05:07,638 --> 07:05:11,878 targets. push the address of the box 9991 07:05:11,878 --> 07:05:14,878 right so now we have all that now we can 9992 07:05:14,878 --> 07:05:17,958 finally call our proposed function and 9993 07:05:17,958 --> 07:05:20,440 what does this proposed function return 9994 07:05:20,440 --> 07:05:23,240 returns this un 256 proposal ID and we 9995 07:05:23,240 --> 07:05:26,160 need that to keep going so first thing 9996 07:05:26,160 --> 07:05:29,200 we're going to do is propose to the Dow 9997 07:05:29,200 --> 07:05:31,638 propose to the Dow so say you and 256 9998 07:05:31,638 --> 07:05:36,120 six proposal ID equals 9999 07:05:36,120 --> 07:05:41,840 governor. propose the targets values 10000 07:05:41,840 --> 07:05:44,718 call datas and 10001 07:05:44,718 --> 07:05:47,040 description boom and we got our proposal 10002 07:05:47,040 --> 07:05:50,638 ID just like that now we can even view 10003 07:05:50,638 --> 07:05:53,718 the state of this proposal now right 10004 07:05:53,718 --> 07:05:55,958 because we can call that state function 10005 07:05:55,958 --> 07:05:58,120 so we can do even like a little 10006 07:05:58,120 --> 07:05:59,638 console.log 10007 07:05:59,638 --> 07:06:03,000 proposal state uh we can call a state 10008 07:06:03,000 --> 07:06:05,398 function like this actually get a 10009 07:06:05,398 --> 07:06:07,080 copilot has this right except for we 10010 07:06:07,080 --> 07:06:09,360 want to turn this into the U 256 version 10011 07:06:09,360 --> 07:06:10,520 so we'll say un 10012 07:06:10,520 --> 07:06:16,000 256 governor. State proposal ID like 10013 07:06:16,000 --> 07:06:20,200 this and if we look up function State 10014 07:06:20,200 --> 07:06:22,798 we'll find this there's like a million 10015 07:06:22,798 --> 07:06:26,160 of these in our governor. soul it 10016 07:06:26,160 --> 07:06:28,398 returns this proposal state which if we 10017 07:06:28,398 --> 07:06:30,320 control or command click on that we can 10018 07:06:30,320 --> 07:06:31,718 see all the different states that can 10019 07:06:31,718 --> 07:06:34,958 have pending active canel defeated 10020 07:06:34,958 --> 07:06:37,718 succeeded cued expired executed so 10021 07:06:37,718 --> 07:06:40,120 pending obviously is going to be zero 10022 07:06:40,120 --> 07:06:41,718 active is obviously going to be one 10023 07:06:41,718 --> 07:06:44,558 canceled is going to be two Etc right 10024 07:06:44,558 --> 07:06:45,600 and of course we're going to need to 10025 07:06:45,600 --> 07:06:49,798 import console from test Okay cool so 10026 07:06:49,798 --> 07:06:53,200 this should return what should return 10027 07:06:53,200 --> 07:06:56,920 pending it should return a zero and it 10028 07:06:56,920 --> 07:06:58,558 hasn't started yet it's not active 10029 07:06:58,558 --> 07:07:01,520 because there's a delay right so we want 10030 07:07:01,520 --> 07:07:04,600 want to actually update our fake 10031 07:07:04,600 --> 07:07:07,798 blockchain to move past that so we'll 10032 07:07:07,798 --> 07:07:10,840 just do vm. warp we'll use these cheat 10033 07:07:10,840 --> 07:07:16,040 codes remember lock. timestamp plus and 10034 07:07:16,040 --> 07:07:20,120 then we'll just do voting delay you went 10035 07:07:20,120 --> 07:07:24,478 six public constant voting delay equals 10036 07:07:24,478 --> 07:07:29,398 one this is how many blocks till a vote 10037 07:07:29,398 --> 07:07:32,520 is active right and from our wizard we 10038 07:07:32,520 --> 07:07:34,840 just left this as one oh go back to 10039 07:07:34,840 --> 07:07:36,718 Governor we just left our voting delay 10040 07:07:36,718 --> 07:07:40,520 as one so this voting delay is one so uh 10041 07:07:40,520 --> 07:07:42,360 plus voting delay and then actually 10042 07:07:42,360 --> 07:07:44,798 we'll do voting delay plus one and then 10043 07:07:44,798 --> 07:07:48,040 we'll also do roll to increase the time 10044 07:07:48,040 --> 07:07:51,440 as well bm. warp EXC going me to 10045 07:07:51,440 --> 07:07:54,280 increase the number block. 10046 07:07:54,280 --> 07:07:59,160 number plus voting delay plus one now if 10047 07:07:59,160 --> 07:08:01,478 you view the state it should be active 10048 07:08:01,478 --> 07:08:05,120 so now it'll be active cool two after a 10049 07:08:05,120 --> 07:08:08,160 proposal starts because we've proposed 10050 07:08:08,160 --> 07:08:10,558 enough time has passed we now can 10051 07:08:10,558 --> 07:08:12,600 actually start voting on something so 10052 07:08:12,600 --> 07:08:14,798 I'm going to do this cast vote function 10053 07:08:14,798 --> 07:08:16,680 but I'm going to do cast vote with 10054 07:08:16,680 --> 07:08:19,040 reason because I like cast voting with a 10055 07:08:19,040 --> 07:08:20,760 reason so we're going to do string 10056 07:08:20,760 --> 07:08:26,360 memory reason equals cuz blue frog is 10057 07:08:26,360 --> 07:08:28,558 cool that's going to be my reason that's 10058 07:08:28,558 --> 07:08:29,840 why I'm going to vote for something 10059 07:08:29,840 --> 07:08:32,040 because blue frog is cool in order to 10060 07:08:32,040 --> 07:08:33,958 vote we're going to need to call this 10061 07:08:33,958 --> 07:08:37,638 cast vote function if we go to Governor 10062 07:08:37,638 --> 07:08:40,120 we look for the function Vote or excuse 10063 07:08:40,120 --> 07:08:42,638 me cast if we go on our search bar we 10064 07:08:42,638 --> 07:08:45,958 can look for function cast vote there's 10065 07:08:45,958 --> 07:08:48,398 a whole bunch of cast votes we can see 10066 07:08:48,398 --> 07:08:51,440 in here we need to pass a proposal ID 10067 07:08:51,440 --> 07:08:52,280 and 10068 07:08:52,280 --> 07:08:54,600 support how do we know support works 10069 07:08:54,600 --> 07:08:56,040 well let's look at this Casto internal 10070 07:08:56,040 --> 07:08:59,200 function this Caso internal function 10071 07:08:59,200 --> 07:09:02,040 down here support 10072 07:09:02,040 --> 07:09:04,558 count vote we got to look at this count 10073 07:09:04,558 --> 07:09:08,320 vote function so let's look for function 10074 07:09:08,320 --> 07:09:11,600 count vote okay this is the abstract 10075 07:09:11,600 --> 07:09:13,878 implementation let's we don't want 10076 07:09:13,878 --> 07:09:15,878 Governor compatibility Bravo we want 10077 07:09:15,878 --> 07:09:17,360 Governor counting simple because that's 10078 07:09:17,360 --> 07:09:19,558 we're using Okay support how does 10079 07:09:19,558 --> 07:09:23,040 support work aha okay so there's this 10080 07:09:23,040 --> 07:09:25,920 vote type struct that it's checking for 10081 07:09:25,920 --> 07:09:28,398 let's go to the vote type okay against 10082 07:09:28,398 --> 07:09:30,798 for abstain so against is going to be 10083 07:09:30,798 --> 07:09:31,840 zero 10084 07:09:31,840 --> 07:09:34,958 four is going to be one and abstain is 10085 07:09:34,958 --> 07:09:37,440 going to be two so what do I want to do 10086 07:09:37,440 --> 07:09:39,360 I want to write vote four so we're going 10087 07:09:39,360 --> 07:09:42,120 to do one so we're going to say we have 10088 07:09:42,120 --> 07:09:48,360 our reason un 8 vot way equals 1 which 10089 07:09:48,360 --> 07:09:52,040 means I'm voting for voting yes if you 10090 07:09:52,040 --> 07:09:55,798 will and now let's do our vm. prank our 10091 07:09:55,798 --> 07:09:59,440 user or our voter and we'll do 10092 07:09:59,440 --> 07:10:01,920 governor. cast vote 10093 07:10:01,920 --> 07:10:07,600 with reason proposal ID vote way and 10094 07:10:07,600 --> 07:10:11,160 reason reason all right cool now that we 10095 07:10:11,160 --> 07:10:13,520 voted let's speed up this voting period 10096 07:10:13,520 --> 07:10:14,958 because everyone voted because we're the 10097 07:10:14,958 --> 07:10:16,680 only voter let's speed up this voting 10098 07:10:16,680 --> 07:10:19,958 period and we set the voting period 10099 07:10:19,958 --> 07:10:22,320 automatically if we go back to our my 10100 07:10:22,320 --> 07:10:25,878 Governor right voting period was set to 10101 07:10:25,878 --> 07:10:29,600 one week in here this number represents 10102 07:10:29,600 --> 07:10:31,760 one week so we're we're going to speed 10103 07:10:31,760 --> 07:10:33,440 up this voting period I'm going to copy 10104 07:10:33,440 --> 07:10:36,398 this number scroll up to the top we'll 10105 07:10:36,398 --> 07:10:41,120 do U into 256 public constant uh excuse 10106 07:10:41,120 --> 07:10:45,638 me voting period equals this oh and this 10107 07:10:45,638 --> 07:10:48,680 should have a semicolon voting period 10108 07:10:48,680 --> 07:10:50,958 we're going to speed through the voting 10109 07:10:50,958 --> 07:10:53,478 period I'm going to just copy these 10110 07:10:53,478 --> 07:10:54,638 change this to 10111 07:10:54,638 --> 07:10:58,760 voting period voting period now now that 10112 07:10:58,760 --> 07:11:01,600 we' voted and the voting period is done 10113 07:11:01,600 --> 07:11:04,398 what do we do well we actually need to 10114 07:11:04,398 --> 07:11:09,718 cue the TX before we can actually for 10115 07:11:09,718 --> 07:11:11,958 execute so we have to cue it first so 10116 07:11:11,958 --> 07:11:14,638 there's a queuing process so on our my 10117 07:11:14,638 --> 07:11:18,718 Governor there's a function function q 10118 07:11:18,718 --> 07:11:20,280 and a whole bunch of them have this let 10119 07:11:20,280 --> 07:11:22,520 me open this up function 10120 07:11:22,520 --> 07:11:26,000 Q on this Governor timelock controller 10121 07:11:26,000 --> 07:11:28,398 there's this queuing bit because a que 10122 07:11:28,398 --> 07:11:31,240 means it's passed but we have to wait to 10123 07:11:31,240 --> 07:11:33,520 queue it up we just pass the same bits 10124 07:11:33,520 --> 07:11:36,320 that we originally did for propose so 10125 07:11:36,320 --> 07:11:38,718 we're going to go back here we'll say 10126 07:11:38,718 --> 07:11:40,958 the only difference is we need to Hash 10127 07:11:40,958 --> 07:11:43,000 the description first so we'll say bytes 10128 07:11:43,000 --> 07:11:48,600 32 D description hash equals check 10129 07:11:48,600 --> 07:11:54,000 256 api. in code pack description now we 10130 07:11:54,000 --> 07:11:57,920 can call governor. Q propal ID uh excuse 10131 07:11:57,920 --> 07:12:00,000 me this is not right 10132 07:12:00,000 --> 07:12:04,360 c.q um um targets we're going to pass 10133 07:12:04,360 --> 07:12:08,280 the same values that we proposed with so 10134 07:12:08,280 --> 07:12:11,600 that we guarantee it's the same proposal 10135 07:12:11,600 --> 07:12:15,000 so targets values 10136 07:12:15,000 --> 07:12:19,160 function uh call datas and description 10137 07:12:19,160 --> 07:12:21,398 hash oh we don't need that zero we're 10138 07:12:21,398 --> 07:12:23,080 going to queue it and then we're going 10139 07:12:23,080 --> 07:12:24,200 to 10140 07:12:24,200 --> 07:12:29,200 finally wait this m delay which we said 10141 07:12:29,200 --> 07:12:31,240 hey after a vote passes we need to wait 10142 07:12:31,240 --> 07:12:33,398 this m delay before we can execute so 10143 07:12:33,398 --> 07:12:35,920 I'm going to copy this again paste it 10144 07:12:35,920 --> 07:12:38,080 here instead of voting period this going 10145 07:12:38,080 --> 07:12:41,958 to be M delay M delay and then finally 10146 07:12:41,958 --> 07:12:44,958 we can execute so we'll do governor. 10147 07:12:44,958 --> 07:12:46,958 xute it's going to be the same as what 10148 07:12:46,958 --> 07:12:49,520 we did to queue it up I even just copy 10149 07:12:49,520 --> 07:12:52,680 this whole thing paste it here targets 10150 07:12:52,680 --> 07:12:55,840 values call Dat is description hash 10151 07:12:55,840 --> 07:12:59,600 X let me spell this right and now 10152 07:12:59,600 --> 07:13:04,558 finally we can do assert box dot get 10153 07:13:04,558 --> 07:13:08,798 number equals the value that we 10154 07:13:08,798 --> 07:13:11,798 stored 10155 07:13:12,120 --> 07:13:15,520 boom we even do like a console 10156 07:13:15,520 --> 07:13:20,000 diog box value box sucket 10157 07:13:20,558 --> 07:13:24,120 number okay copy this open our terminal 10158 07:13:24,120 --> 07:13:27,920 clear porch test DM paste - VV so we can 10159 07:13:27,920 --> 07:13:31,600 see the console. logs pull it up 10160 07:13:31,600 --> 07:13:34,360 is this what we've been waiting 10161 07:13:34,360 --> 07:13:38,600 for no we ran into an issue we see these 10162 07:13:38,600 --> 07:13:40,638 two logs proposal state is zero we're 10163 07:13:40,638 --> 07:13:43,360 getting vote currently not active so 10164 07:13:43,360 --> 07:13:44,840 let's add a couple more of these so we 10165 07:13:44,840 --> 07:13:47,840 can see where it failed it looks like it 10166 07:13:47,840 --> 07:13:50,760 got to the cast vote with reason and it 10167 07:13:50,760 --> 07:13:53,240 failed so let's go to our test so it 10168 07:13:53,240 --> 07:13:55,000 looks like it proposed okay it looks 10169 07:13:55,000 --> 07:13:57,280 like this failed so it says it's 10170 07:13:57,280 --> 07:14:00,040 currently not active so did we mess up 10171 07:14:00,040 --> 07:14:03,558 our voting delay ah it's cuz I did warp 10172 07:14:03,558 --> 07:14:06,440 for both this should be vm. roll let's 10173 07:14:06,440 --> 07:14:08,120 see do we do oh we messed that up for 10174 07:14:08,120 --> 07:14:11,958 all of them let's fix this roll roll 10175 07:14:11,958 --> 07:14:14,878 roll okay cool let clear let's run it 10176 07:14:14,878 --> 07:14:18,240 again hold this 10177 07:14:21,000 --> 07:14:25,478 up oh my goodness it passed and we can 10178 07:14:25,478 --> 07:14:28,840 see in the logs proposal state was Zero 10179 07:14:28,840 --> 07:14:30,840 we waited and then it became IM active 10180 07:14:30,840 --> 07:14:32,718 and and then finally after we got 10181 07:14:32,718 --> 07:14:34,798 through all the different states we 10182 07:14:34,798 --> 07:14:37,878 finally were able to update the box I 10183 07:14:37,878 --> 07:14:39,000 know we kind of sped through this 10184 07:14:39,000 --> 07:14:41,200 section but like I said at this point 10185 07:14:41,200 --> 07:14:42,718 you're becoming a pretty sophisticated 10186 07:14:42,718 --> 07:14:45,160 smart contract engineer and I really 10187 07:14:45,160 --> 07:14:48,520 wanted to show you the process of voting 10188 07:14:48,520 --> 07:14:51,718 of a typical Dow from today and for you 10189 07:14:51,718 --> 07:14:54,558 to see it from the coding side as well 10190 07:14:54,558 --> 07:14:56,958 now if you want to go further with this 10191 07:14:56,958 --> 07:14:59,040 I highly recommend you look into some of 10192 07:14:59,040 --> 07:15:00,878 the different methodologies and some of 10193 07:15:00,878 --> 07:15:02,160 the the newer methodologies that 10194 07:15:02,160 --> 07:15:03,958 researchers are coming out with for 10195 07:15:03,958 --> 07:15:06,200 people to actually do votes because like 10196 07:15:06,200 --> 07:15:10,360 I said plutocracy is not the best so if 10197 07:15:10,360 --> 07:15:13,240 you want to go farther try experimenting 10198 07:15:13,240 --> 07:15:15,600 with other ways to build Dows maybe 10199 07:15:15,600 --> 07:15:18,920 flesh out your own doubt maybe deploy a 10200 07:15:18,920 --> 07:15:21,320 doubt do whatever you want to do here 10201 07:15:21,320 --> 07:15:25,120 but in any case you should be incredibly 10202 07:15:25,120 --> 07:15:27,398 proud of yourself because you got 10203 07:15:27,398 --> 07:15:29,000 through the hardest lesson of this whole 10204 07:15:29,000 --> 07:15:31,478 course already which was the stable one 10205 07:15:31,478 --> 07:15:32,680 and then you just learned this 10206 07:15:32,680 --> 07:15:36,360 incredibly powerful Advanced course in 10207 07:15:36,360 --> 07:15:39,718 how Dows actually 10208 07:15:41,240 --> 07:15:45,160 work students we're almost at our end 10209 07:15:45,160 --> 07:15:48,798 there is one more lesson for us to do 10210 07:15:48,798 --> 07:15:50,520 and it's our smart contract and security 10211 07:15:50,520 --> 07:15:53,520 auditing for developers do not leave 10212 07:15:53,520 --> 07:15:55,958 this course without watching this I want 10213 07:15:55,958 --> 07:15:58,160 you to have at least the fundamental 10214 07:15:58,160 --> 07:16:00,398 knowledge of where you should look for 10215 07:16:00,398 --> 07:16:02,798 security help what you should have in 10216 07:16:02,798 --> 07:16:05,958 mind for security especially now that 10217 07:16:05,958 --> 07:16:07,600 you have the ability to go out and 10218 07:16:07,600 --> 07:16:10,760 deploy some amazing contracts I'm not 10219 07:16:10,760 --> 07:16:12,798 going to teach you how to audit step by 10220 07:16:12,798 --> 07:16:14,478 step here right but I am going to teach 10221 07:16:14,478 --> 07:16:16,200 you here's what you should be looking 10222 07:16:16,200 --> 07:16:19,080 for in an auditor here's how you you can 10223 07:16:19,080 --> 07:16:21,558 begin to start thinking about security 10224 07:16:21,558 --> 07:16:23,520 and I am working on some security and 10225 07:16:23,520 --> 07:16:26,600 auditing education material as well to 10226 07:16:26,600 --> 07:16:28,360 make those of you who do want to go down 10227 07:16:28,360 --> 07:16:31,600 that path successful so 10228 07:16:31,600 --> 07:16:33,840 give yourself a pat on the back do a 10229 07:16:33,840 --> 07:16:37,240 little dance do a little celebration get 10230 07:16:37,240 --> 07:16:40,398 some ice cream and I'll see you in the 10231 07:16:40,398 --> 07:16:43,680 next one and then I will I will release 10232 07:16:43,680 --> 07:16:48,240 you to the world so pause the video and 10233 07:16:48,240 --> 07:16:50,718 I'll see you 10234 07:16:52,080 --> 07:16:54,718 soon and as a special piece of bonus 10235 07:16:54,718 --> 07:16:56,958 content we have Harrison here to give us 10236 07:16:56,958 --> 07:17:00,120 some tips on gas optimizations so enjoy 10237 07:17:00,120 --> 07:17:02,478 hey guys I'm Harr Leo and I'm the CTO 10238 07:17:02,478 --> 07:17:05,240 and co-founder of pop punk LLC where 10239 07:17:05,240 --> 07:17:08,160 we're building Gaslight GG uh Gaslight 10240 07:17:08,160 --> 07:17:10,878 GG is a audit firm but we focus 10241 07:17:10,878 --> 07:17:12,920 specifically on gas optimization for 10242 07:17:12,920 --> 07:17:15,440 protocols to ensure that your gas costs 10243 07:17:15,440 --> 07:17:17,638 are as low as possible uh in addition 10244 07:17:17,638 --> 07:17:19,200 we're happy to announce that we're now 10245 07:17:19,200 --> 07:17:21,718 building uh hyper optimized public goods 10246 07:17:21,718 --> 07:17:23,718 tools for evm developers to make sure 10247 07:17:23,718 --> 07:17:25,398 that everyone has access to just the 10248 07:17:25,398 --> 07:17:27,440 best and the cheapest contracts so 10249 07:17:27,440 --> 07:17:30,040 Patrick invited me on here to show an 10250 07:17:30,040 --> 07:17:31,798 example of some some common ways that 10251 07:17:31,798 --> 07:17:33,478 you might be using too much gas in your 10252 07:17:33,478 --> 07:17:35,600 smart contracts Without Really noticing 10253 07:17:35,600 --> 07:17:37,040 so I have a quick example here we're 10254 07:17:37,040 --> 07:17:39,040 going to look at an airdrop contract 10255 07:17:39,040 --> 07:17:40,840 this is a super basic airdrop contract 10256 07:17:40,840 --> 07:17:42,840 which I'm calling airdrop bad or bad 10257 07:17:42,840 --> 07:17:45,240 airdrop uh very simple we have a token 10258 07:17:45,240 --> 07:17:47,798 we're using a counter to iterate um and 10259 07:17:47,798 --> 07:17:49,958 count the number of transfers uh so as 10260 07:17:49,958 --> 07:17:51,440 you see in the Constructor we set the 10261 07:17:51,440 --> 07:17:53,000 token here uh and then we have our 10262 07:17:53,000 --> 07:17:55,398 airdrop function so we have two 10263 07:17:55,398 --> 07:17:57,240 parameters um for this function the 10264 07:17:57,240 --> 07:18:00,000 first one is a memory um array of 10265 07:18:00,000 --> 07:18:01,840 addresses which is the recipients and 10266 07:18:01,840 --> 07:18:04,798 then we have a memory array of un 2506s 10267 07:18:04,798 --> 07:18:06,320 for the amounts the first thing we're 10268 07:18:06,320 --> 07:18:07,600 doing is we're checking to make sure 10269 07:18:07,600 --> 07:18:09,040 that the amounts match otherwise we're 10270 07:18:09,040 --> 07:18:10,680 going to revert the next thing here 10271 07:18:10,680 --> 07:18:13,000 which is really common um is making sure 10272 07:18:13,000 --> 07:18:15,478 that you're batching your your actions 10273 07:18:15,478 --> 07:18:17,360 so if we see in this case we have to 10274 07:18:17,360 --> 07:18:19,520 Traverse through the entire uh input 10275 07:18:19,520 --> 07:18:22,478 argument array a fulltime just so we can 10276 07:18:22,478 --> 07:18:24,120 transfer the number of tokens from the 10277 07:18:24,120 --> 07:18:26,200 message. sender to the contract so let's 10278 07:18:26,200 --> 07:18:27,958 say there's 50 amounts we'd have to do 10279 07:18:27,958 --> 07:18:29,440 50 separate transfer from it's 10280 07:18:29,440 --> 07:18:31,760 definitely not ideal uh and then lastly 10281 07:18:31,760 --> 07:18:33,760 here we have a super basic Loop where 10282 07:18:33,760 --> 07:18:37,200 we're just iterating through the array 10283 07:18:37,200 --> 07:18:39,478 um and we are transferring the tokens 10284 07:18:39,478 --> 07:18:40,920 from the contract to the user with the 10285 07:18:40,920 --> 07:18:43,520 respected amounts here uh lastly we can 10286 07:18:43,520 --> 07:18:45,478 see that we're iterating this counter 10287 07:18:45,478 --> 07:18:48,478 every single time in the array or every 10288 07:18:48,478 --> 07:18:50,240 time in the loop yeah so keep that in 10289 07:18:50,240 --> 07:18:51,920 mind so now if we look at a more ideal 10290 07:18:51,920 --> 07:18:53,440 example of this the first thing that 10291 07:18:53,440 --> 07:18:54,558 you're going to notice is we're using a 10292 07:18:54,558 --> 07:18:56,520 mutable for the token a mutable is good 10293 07:18:56,520 --> 07:18:57,558 if you're obviously never going to 10294 07:18:57,558 --> 07:18:59,398 change something because we set this 10295 07:18:59,398 --> 07:19:01,280 value in the Constructor and it'll be 10296 07:19:01,280 --> 07:19:03,000 included in the contract deployed by 10297 07:19:03,000 --> 07:19:04,958 code which is cheaper to read from every 10298 07:19:04,958 --> 07:19:07,798 time if we look now here we've changed 10299 07:19:07,798 --> 07:19:09,478 our arguments for our function the call 10300 07:19:09,478 --> 07:19:11,040 data and we've added a new one called 10301 07:19:11,040 --> 07:19:13,520 total amount this is done so we don't 10302 07:19:13,520 --> 07:19:15,920 have to do two Loops through the array 10303 07:19:15,920 --> 07:19:18,080 we can do one so if you see here the 10304 07:19:18,080 --> 07:19:19,120 first thing that we're doing is we're 10305 07:19:19,120 --> 07:19:21,280 transferring this total amount from the 10306 07:19:21,280 --> 07:19:23,680 message. sender to this contract so 10307 07:19:23,680 --> 07:19:25,520 that's going to cut a large amount of 10308 07:19:25,520 --> 07:19:26,878 gas because we're only going to wind up 10309 07:19:26,878 --> 07:19:29,798 looping one time so if we see here we 10310 07:19:29,798 --> 07:19:31,000 we've changed the way that we're going 10311 07:19:31,000 --> 07:19:32,718 through the loop technically we don't 10312 07:19:32,718 --> 07:19:34,600 have to Cache this array length because 10313 07:19:34,600 --> 07:19:36,920 we're using call data but if you do see 10314 07:19:36,920 --> 07:19:38,040 here it's the same thing we're 10315 07:19:38,040 --> 07:19:39,920 transferring tokens from this contract 10316 07:19:39,920 --> 07:19:42,520 to the recipient uh we're also using 10317 07:19:42,520 --> 07:19:45,440 unchecked for our Loop iterator because 10318 07:19:45,440 --> 07:19:47,280 it's not going to overflow cuz the odds 10319 07:19:47,280 --> 07:19:49,638 of someone doing an airdrop to more than 10320 07:19:49,638 --> 07:19:52,280 the max U 256 of addresses is kind of 10321 07:19:52,280 --> 07:19:54,958 crazy um and the final thing to look at 10322 07:19:54,958 --> 07:19:56,600 here we're using an unchecked block 10323 07:19:56,600 --> 07:19:58,718 again but rather than updating this 10324 07:19:58,718 --> 07:20:01,080 state value of transfers every single 10325 07:20:01,080 --> 07:20:03,398 time and paying that base G gas cost 10326 07:20:03,398 --> 07:20:05,558 each time we can just update this amount 10327 07:20:05,558 --> 07:20:07,878 at the end with the bulk length so these 10328 07:20:07,878 --> 07:20:09,398 are small things that you can think 10329 07:20:09,398 --> 07:20:10,600 about when you're writing your smart 10330 07:20:10,600 --> 07:20:12,440 contract to ensure that you're saving as 10331 07:20:12,440 --> 07:20:14,600 much gas as possible if we look at the 10332 07:20:14,600 --> 07:20:18,080 results of this the airdrop bad costs uh 10333 07:20:18,080 --> 07:20:19,958 1, 10334 07:20:19,958 --> 07:20:24,600 94,6 190 gas and the airdrop good cost 10335 07:20:24,600 --> 07:20:27,398 44842 gas so we've essentially saved 10336 07:20:27,398 --> 07:20:31,080 600,000 gas just by doing Simple things 10337 07:20:31,080 --> 07:20:32,360 you know it doesn't make the code 10338 07:20:32,360 --> 07:20:34,280 unreadable um and you're just making 10339 07:20:34,280 --> 07:20:36,080 sure that you know the end users of this 10340 07:20:36,080 --> 07:20:37,478 will not have to spend a crazy amount of 10341 07:20:37,478 --> 07:20:39,558 money so I want to thank Patrick for 10342 07:20:39,558 --> 07:20:41,718 having me on here uh you can find me on 10343 07:20:41,718 --> 07:20:45,240 Twitter atpunk onchain um or the 10344 07:20:45,240 --> 07:20:47,878 business account is atpunk LLC if you're 10345 07:20:47,878 --> 07:20:49,680 a small protocol or a large protocol 10346 07:20:49,680 --> 07:20:51,520 feel free to reach out for gas Audits 10347 07:20:51,520 --> 07:20:53,478 and uh pretty soon in the near future 10348 07:20:53,478 --> 07:20:55,680 you'll be able to go to gasl light.gg 10349 07:20:55,680 --> 07:20:57,520 and you'll be able to deploy super cheap 10350 07:20:57,520 --> 07:20:59,840 super gas optimized smart contracts you 10351 07:20:59,840 --> 07:21:01,600 know through no and just pressing one 10352 07:21:01,600 --> 07:21:04,638 button thank 10353 07:21:22,398 --> 07:21:28,120 you all right welcome back to our last 10354 07:21:28,120 --> 07:21:30,958 lesson of the course I hope you're 10355 07:21:30,958 --> 07:21:34,840 excited because I sure am in this last 10356 07:21:34,840 --> 07:21:38,440 lesson we're going to give you just a 10357 07:21:38,440 --> 07:21:41,600 teasing of smart contract security and 10358 07:21:41,600 --> 07:21:44,200 auditing this is especially for 10359 07:21:44,200 --> 07:21:46,718 developers we're not going to go over 10360 07:21:46,718 --> 07:21:49,478 everything security however in here I'm 10361 07:21:49,478 --> 07:21:52,040 going to leave a ton of places where you 10362 07:21:52,040 --> 07:21:55,398 can learn and grow and absolutely be 10363 07:21:55,398 --> 07:21:57,718 sure to sign up for web3 education. 10364 07:21:57,718 --> 07:21:59,478 because we're going to put a whole lot 10365 07:21:59,478 --> 07:22:01,320 more security information information in 10366 07:22:01,320 --> 07:22:04,080 here as well but we are going to give 10367 07:22:04,080 --> 07:22:06,638 you the foundational information that 10368 07:22:06,638 --> 07:22:08,958 you need to even begin to start thinking 10369 07:22:08,958 --> 07:22:12,320 about where to go and how to work with 10370 07:22:12,320 --> 07:22:15,000 security in the space we have this repo 10371 07:22:15,000 --> 07:22:16,798 Denver security to talk a little bit 10372 07:22:16,798 --> 07:22:19,240 more about security and how it works but 10373 07:22:19,240 --> 07:22:21,240 I want to give you all a couple of stats 10374 07:22:21,240 --> 07:22:23,680 before we even jump into security to 10375 07:22:23,680 --> 07:22:26,160 impress upon you why this is so 10376 07:22:26,160 --> 07:22:28,360 important this is probably the stat 10377 07:22:28,360 --> 07:22:30,840 that's the most jarring to me according 10378 07:22:30,840 --> 07:22:33,878 to chain analysis as of last year 2022 10379 07:22:33,878 --> 07:22:36,958 about $3.8 billion doll was stolen in 10380 07:22:36,958 --> 07:22:40,760 crypto hacks and about 3.1 billion of 10381 07:22:40,760 --> 07:22:45,040 that 3.8 billion was specifically Defi 10382 07:22:45,040 --> 07:22:48,440 and if we go to defi Lama last year defi 10383 07:22:48,440 --> 07:22:50,718 had a total value locked of around 50 10384 07:22:50,718 --> 07:22:55,000 billion so if we do the math there 3.1 10385 07:22:55,000 --> 07:23:00,958 ID 50 we're talking around 6% of all def 10386 07:23:00,958 --> 07:23:02,798 was hacked last year that's like walking 10387 07:23:02,798 --> 07:23:04,760 up to a bank and saying hey can I put my 10388 07:23:04,760 --> 07:23:06,280 money in you and the bank going yeah 10389 07:23:06,280 --> 07:23:08,958 sure just ah heads up there's a 6% 10390 07:23:08,958 --> 07:23:11,160 chance all your money will be gone next 10391 07:23:11,160 --> 07:23:13,600 year that is an insanely horrible 10392 07:23:13,600 --> 07:23:16,200 statistic and it's why we need to put so 10393 07:23:16,200 --> 07:23:18,040 much more focus and so much more 10394 07:23:18,040 --> 07:23:20,878 emphasis on security there's a popular 10395 07:23:20,878 --> 07:23:23,240 website called rec. newws that has a 10396 07:23:23,240 --> 07:23:25,000 leaderboard that if you go to you can 10397 07:23:25,000 --> 07:23:28,920 see a lot of the top biggest hacks 10398 07:23:28,920 --> 07:23:31,360 hugest hacks to ever existed did went 10399 07:23:31,360 --> 07:23:33,600 off of code that was unaudited 10400 07:23:33,600 --> 07:23:35,798 unreviewed by Security Professionals 10401 07:23:35,798 --> 07:23:38,920 unaudited unaudited unaudited uh Na Na 10402 07:23:38,920 --> 07:23:41,958 un audited unaudited unaudited unaudited 10403 07:23:41,958 --> 07:23:44,558 out of scope unaudited out of scope and 10404 07:23:44,558 --> 07:23:47,200 it is not acceptable some of these hacks 10405 07:23:47,200 --> 07:23:49,958 were absolutely massive losing over half 10406 07:23:49,958 --> 07:23:52,558 a billion dollars so if you're a 10407 07:23:52,558 --> 07:23:55,360 protocol or if you developers go on to 10408 07:23:55,360 --> 07:23:57,680 build protocols here is what you want to 10409 07:23:57,680 --> 07:23:59,840 think about you can maybe spend $2 10410 07:23:59,840 --> 07:24:02,520 million of your budget on security or 10411 07:24:02,520 --> 07:24:05,760 get hacked for $200 million the spending 10412 07:24:05,760 --> 07:24:08,638 money on security is a 99% reduction 10413 07:24:08,638 --> 07:24:10,558 cost so if you approach security from a 10414 07:24:10,558 --> 07:24:13,318 business perspective it absolutely 100% 10415 07:24:13,318 --> 07:24:15,160 makes sense for you to spend resources 10416 07:24:15,160 --> 07:24:18,440 on security so to kick start this video 10417 07:24:18,440 --> 07:24:20,080 all of you protocol devs will probably 10418 07:24:20,080 --> 07:24:21,920 want to get a smart contract security 10419 07:24:21,920 --> 07:24:24,360 audit at some point a security Focus 10420 07:24:24,360 --> 07:24:26,280 code review before you launch your 10421 07:24:26,280 --> 07:24:29,478 protocol so let's start there I've made 10422 07:24:29,478 --> 07:24:31,440 a video recently on what a smart 10423 07:24:31,440 --> 07:24:33,760 contract audit is and how to prepare for 10424 07:24:33,760 --> 07:24:35,360 a smart contract audit that we're going 10425 07:24:35,360 --> 07:24:38,160 to watch first because even if you don't 10426 07:24:38,160 --> 07:24:39,798 want to become a smart contract auditor 10427 07:24:39,798 --> 07:24:41,798 yourself you'll need to know what a 10428 07:24:41,798 --> 07:24:44,000 smart contract audit is for when your 10429 07:24:44,000 --> 07:24:45,440 protocol is going to go launch to 10430 07:24:45,440 --> 07:24:48,798 mainten it so let's start 10431 07:24:49,638 --> 07:24:52,120 there a smart contract audit is a 10432 07:24:52,120 --> 07:24:54,558 timeboxed Security based code review on 10433 07:24:54,558 --> 07:24:56,478 your smart contract system an Auditor's 10434 07:24:56,478 --> 07:24:58,398 goal is to find as many vulnerabilities 10435 07:24:58,398 --> 07:25:01,520 as possible and educate the protocol on 10436 07:25:01,520 --> 07:25:03,840 best security best practices and coding 10437 07:25:03,840 --> 07:25:05,840 best practices moving forward Auditors 10438 07:25:05,840 --> 07:25:08,280 use a combination of manual review and 10439 07:25:08,280 --> 07:25:09,798 automated tools to find these 10440 07:25:09,798 --> 07:25:11,478 vulnerabilities now why are these so 10441 07:25:11,478 --> 07:25:13,000 important why is it critical that you 10442 07:25:13,000 --> 07:25:14,558 get an audit before deploying your 10443 07:25:14,558 --> 07:25:16,600 codebase to a live blockchain well for 10444 07:25:16,600 --> 07:25:18,240 starters there are entire websites 10445 07:25:18,240 --> 07:25:20,160 dedicated to how many hacks happen last 10446 07:25:20,160 --> 07:25:23,280 year we saw the most value ever stolen 10447 07:25:23,280 --> 07:25:25,280 from Smart contracts with almost $4 10448 07:25:25,280 --> 07:25:27,520 billion stolen due to the immutability 10449 07:25:27,520 --> 07:25:29,318 of the blockchain once a smart contract 10450 07:25:29,318 --> 07:25:30,958 is deployed you can't change it so you 10451 07:25:30,958 --> 07:25:32,680 better get it right the blockchain is a 10452 07:25:32,680 --> 07:25:34,638 permissionless adversarial environment 10453 07:25:34,638 --> 07:25:36,000 and your protocol needs to be prepared 10454 07:25:36,000 --> 07:25:37,718 for malicious users but even more so 10455 07:25:37,718 --> 07:25:39,240 than that an audit can improve your 10456 07:25:39,240 --> 07:25:41,280 developers team's understanding of code 10457 07:25:41,280 --> 07:25:43,000 improving their speed and Effectiveness 10458 07:25:43,000 --> 07:25:44,760 in implementing features moving forward 10459 07:25:44,760 --> 07:25:45,958 and it can teach your team the latest 10460 07:25:45,958 --> 07:25:47,840 and greatest Tooling in the space often 10461 07:25:47,840 --> 07:25:49,760 just one smart contract audit isn't even 10462 07:25:49,760 --> 07:25:51,398 enough and protocols go on a security 10463 07:25:51,398 --> 07:25:53,478 Journey that includes many Audits and 10464 07:25:53,478 --> 07:25:55,440 many different services like formal 10465 07:25:55,440 --> 07:25:57,798 verification competitive Audits and Bug 10466 07:25:57,798 --> 07:25:59,240 Bounty programs we'll break these down 10467 07:25:59,240 --> 07:26:01,080 in a future video there are a lot of 10468 07:26:01,080 --> 07:26:02,638 companies that offer smart contract 10469 07:26:02,638 --> 07:26:04,638 auditing services like trail of bits 10470 07:26:04,638 --> 07:26:06,840 consensus diligence open Zeppelin Sigma 10471 07:26:06,840 --> 07:26:09,280 Prime spearit Dow mix bites watch pug 10472 07:26:09,280 --> 07:26:11,600 trust and of course cyphon additionally 10473 07:26:11,600 --> 07:26:13,318 there's a lot of independent Auditors 10474 07:26:13,318 --> 07:26:14,920 that do great work as well a typical 10475 07:26:14,920 --> 07:26:16,798 audit looks like this price and timeline 10476 07:26:16,798 --> 07:26:18,280 first a protocol needs to reach out and 10477 07:26:18,280 --> 07:26:19,958 they can reach out before or after their 10478 07:26:19,958 --> 07:26:21,638 code is actually finished ideally they 10479 07:26:21,638 --> 07:26:23,280 reach out sometime before their code is 10480 07:26:23,280 --> 07:26:25,200 finished so the audit can have time to 10481 07:26:25,200 --> 07:26:26,798 slot them in once they reach out the 10482 07:26:26,798 --> 07:26:28,600 protocol and Auditors will discuss how 10483 07:26:28,600 --> 07:26:30,240 long the auto will take based off of 10484 07:26:30,240 --> 07:26:32,440 scope and code complexity the scope of 10485 07:26:32,440 --> 07:26:34,680 the audit is going to be the exact files 10486 07:26:34,680 --> 07:26:35,920 and commit hash that's going to be 10487 07:26:35,920 --> 07:26:37,718 audited how long the audit usually 10488 07:26:37,718 --> 07:26:39,878 depends on how many lines of code SL 10489 07:26:39,878 --> 07:26:42,718 complexity you can see a very very rough 10490 07:26:42,718 --> 07:26:44,920 approximation of how long an audit takes 10491 07:26:44,920 --> 07:26:46,478 on your screen now of course this 10492 07:26:46,478 --> 07:26:48,840 depends firm to firm audit to audit and 10493 07:26:48,840 --> 07:26:51,000 tool to Tool so take these with a very 10494 07:26:51,000 --> 07:26:52,840 large grain of salt additionally it's 10495 07:26:52,840 --> 07:26:54,878 this duration that sets the price and 10496 07:26:54,878 --> 07:26:56,680 same thing at the time of recording 10497 07:26:56,680 --> 07:26:59,160 Prices range wildly depending on who's 10498 07:26:59,160 --> 07:27:00,280 doing the audit how many people people 10499 07:27:00,280 --> 07:27:01,840 are doing the audit how complex the code 10500 07:27:01,840 --> 07:27:03,240 is and more and these initial 10501 07:27:03,240 --> 07:27:04,680 conversations are really just to get a 10502 07:27:04,680 --> 07:27:07,040 ballpark estimate and Slot you into the 10503 07:27:07,040 --> 07:27:08,878 auditor schedule commit hash down 10504 07:27:08,878 --> 07:27:10,318 payment start date once you have a 10505 07:27:10,318 --> 07:27:12,080 commit hash you can finalize the start 10506 07:27:12,080 --> 07:27:14,360 date and final price the commit hash is 10507 07:27:14,360 --> 07:27:16,160 the unique ID of the codebase that 10508 07:27:16,160 --> 07:27:17,478 you're working with so the Auditors can 10509 07:27:17,478 --> 07:27:19,440 know exactly what code they're going to 10510 07:27:19,440 --> 07:27:21,240 be looking at some Auditors will ask for 10511 07:27:21,240 --> 07:27:22,958 a down payment in order to schedule you 10512 07:27:22,958 --> 07:27:25,120 in audit Begins the Auditors will use 10513 07:27:25,120 --> 07:27:26,840 every tool in their Arsenal to find as 10514 07:27:26,840 --> 07:27:28,478 many vulnerabilities in your code as 10515 07:27:28,478 --> 07:27:29,798 possible we'll give you some tricks in a 10516 07:27:29,798 --> 07:27:31,840 minute to make this a successful step 10517 07:27:31,840 --> 07:27:33,398 initial report after the time period 10518 07:27:33,398 --> 07:27:34,878 ends the Auditors will give you an 10519 07:27:34,878 --> 07:27:36,440 initial report that looks something like 10520 07:27:36,440 --> 07:27:38,160 this with all their findings listed by 10521 07:27:38,160 --> 07:27:40,840 severity usually categorized into highs 10522 07:27:40,840 --> 07:27:43,958 mediums lows informational noncritical 10523 07:27:43,958 --> 07:27:45,920 and gas efficiencies High mediums and 10524 07:27:45,920 --> 07:27:48,318 low represent the severity of impact and 10525 07:27:48,318 --> 07:27:49,798 likelihood of each vulnerability 10526 07:27:49,798 --> 07:27:51,920 informational gas and non-critical are 10527 07:27:51,920 --> 07:27:53,280 findings to improve the efficiency of 10528 07:27:53,280 --> 07:27:55,840 her code code structure readability and 10529 07:27:55,840 --> 07:27:57,600 best practice improvements that are not 10530 07:27:57,600 --> 07:27:59,080 necessarily vulnerabilities but more 10531 07:27:59,080 --> 07:28:00,638 ways to improve your code mitigation 10532 07:28:00,638 --> 07:28:02,440 Begins the protocols team will then have 10533 07:28:02,440 --> 07:28:03,958 an agreed upon time to fix the 10534 07:28:03,958 --> 07:28:05,478 vulnerabilities found in the initial 10535 07:28:05,478 --> 07:28:07,080 Auto report sometimes depending on the 10536 07:28:07,080 --> 07:28:09,040 severity of the findings this might mean 10537 07:28:09,040 --> 07:28:10,558 you have to start from scratch but more 10538 07:28:10,558 --> 07:28:12,160 times than not you can just implement 10539 07:28:12,160 --> 07:28:13,600 the recommendations the Auditors give 10540 07:28:13,600 --> 07:28:15,280 you this is usually much shorter than 10541 07:28:15,280 --> 07:28:17,318 the audit itself final report after the 10542 07:28:17,318 --> 07:28:18,958 protocol makes these changes the audit 10543 07:28:18,958 --> 07:28:20,558 team will do a final audit report 10544 07:28:20,558 --> 07:28:22,600 exclusively on the fixes made to address 10545 07:28:22,600 --> 07:28:23,958 the issues brought up in the initial 10546 07:28:23,958 --> 07:28:25,798 report then hopefully the protocol and 10547 07:28:25,798 --> 07:28:27,160 Auditors have a great experience 10548 07:28:27,160 --> 07:28:28,398 together and will work together in the 10549 07:28:28,398 --> 07:28:30,520 future to keep web 3 secure now there 10550 07:28:30,520 --> 07:28:32,160 are a few key things that you can do to 10551 07:28:32,160 --> 07:28:34,160 make sure your audit is successful as 10552 07:28:34,160 --> 07:28:35,398 possible to get the most out of your 10553 07:28:35,398 --> 07:28:36,878 audit you should have clear 10554 07:28:36,878 --> 07:28:39,120 documentation a robust test Suite 10555 07:28:39,120 --> 07:28:41,080 ideally including fuzz or invariant 10556 07:28:41,080 --> 07:28:42,718 tests code should be commented and 10557 07:28:42,718 --> 07:28:44,718 readable modern best practices are 10558 07:28:44,718 --> 07:28:46,280 followed there should be an established 10559 07:28:46,280 --> 07:28:48,398 Communication channel between developers 10560 07:28:48,398 --> 07:28:50,680 and Auditors during the audit and an 10561 07:28:50,680 --> 07:28:52,240 initial video walkthrough of the code 10562 07:28:52,240 --> 07:28:53,920 should be done before the audit starts 10563 07:28:53,920 --> 07:28:55,878 the most important part of the process 10564 07:28:55,878 --> 07:28:57,718 is going to be during the audit to get 10565 07:28:57,718 --> 07:28:59,160 the best results you want to think of 10566 07:28:59,160 --> 07:29:01,200 you and your auditor working together as 10567 07:29:01,200 --> 07:29:02,958 a team one of the best ways to do this 10568 07:29:02,958 --> 07:29:04,680 is to have a dedicated Channel where 10569 07:29:04,680 --> 07:29:06,120 Auditors can ask questions to the 10570 07:29:06,120 --> 07:29:07,920 developers the developers will always 10571 07:29:07,920 --> 07:29:09,920 and forever have more context over the 10572 07:29:09,920 --> 07:29:11,760 codebase than the Auditors ever will 10573 07:29:11,760 --> 07:29:13,638 because they have spent so much more 10574 07:29:13,638 --> 07:29:15,200 time working on the codebase and the 10575 07:29:15,200 --> 07:29:17,080 more documentation context and 10576 07:29:17,080 --> 07:29:18,440 information that you can give to the 10577 07:29:18,440 --> 07:29:20,000 Auditors the better this way it can be 10578 07:29:20,000 --> 07:29:21,318 easy for anybody to walk through the 10579 07:29:21,318 --> 07:29:22,600 code and understand what it's supposed 10580 07:29:22,600 --> 07:29:25,520 to do in fact 80% of all bugs are 10581 07:29:25,520 --> 07:29:27,440 actually business logic implementation 10582 07:29:27,440 --> 07:29:29,280 bugs this means that these are bugs that 10583 07:29:29,280 --> 07:29:30,520 have nothing to do with with some weird 10584 07:29:30,520 --> 07:29:32,520 coding error and are just somebody not 10585 07:29:32,520 --> 07:29:33,920 knowing what the protocol should be 10586 07:29:33,920 --> 07:29:35,558 doing so it's vitally important that the 10587 07:29:35,558 --> 07:29:37,638 Auditors understand what the code should 10588 07:29:37,638 --> 07:29:39,398 be doing having a modern test suite and 10589 07:29:39,398 --> 07:29:41,080 tooling can also make Auditors spend 10590 07:29:41,080 --> 07:29:43,040 less time fidgeting with your tooling 10591 07:29:43,040 --> 07:29:45,558 and more time finding issues post audit 10592 07:29:45,558 --> 07:29:47,240 we highly encourage you to take the 10593 07:29:47,240 --> 07:29:48,600 recommendations your Auditors give you 10594 07:29:48,600 --> 07:29:50,680 seriously additionally after an audit if 10595 07:29:50,680 --> 07:29:52,638 you make a change to your code base that 10596 07:29:52,638 --> 07:29:55,280 new code is now un audited code it 10597 07:29:55,280 --> 07:29:57,520 doesn't matter how small the changes 10598 07:29:57,520 --> 07:30:00,000 we've seen a ton of protocols saying oh 10599 07:30:00,000 --> 07:30:01,680 I'll just slip in one line of code and 10600 07:30:01,680 --> 07:30:03,318 sure enough that's the line of code that 10601 07:30:03,318 --> 07:30:04,958 gets exploited and often depending on 10602 07:30:04,958 --> 07:30:06,440 the seriousness of your protocol and how 10603 07:30:06,440 --> 07:30:08,718 many users you want to use it one audit 10604 07:30:08,718 --> 07:30:10,240 might not even be enough working with 10605 07:30:10,240 --> 07:30:11,878 multiple Auditors and getting more eyes 10606 07:30:11,878 --> 07:30:13,318 on your code will give you a better 10607 07:30:13,318 --> 07:30:15,398 chance of finding more vulnerabilities 10608 07:30:15,398 --> 07:30:17,878 what an audit isn't now here's the thing 10609 07:30:17,878 --> 07:30:20,318 an audit doesn't mean that your code is 10610 07:30:20,318 --> 07:30:22,638 bug free an audit is a security Journey 10611 07:30:22,638 --> 07:30:24,520 between the protocol and the auditor to 10612 07:30:24,520 --> 07:30:26,558 find as many bugs as possible and teach 10613 07:30:26,558 --> 07:30:28,240 the protocol different methodologies to 10614 07:30:28,240 --> 07:30:30,478 stay more secure in the future sec is a 10615 07:30:30,478 --> 07:30:32,398 continuous process that is always 10616 07:30:32,398 --> 07:30:34,040 evolving no matter how much experience 10617 07:30:34,040 --> 07:30:36,718 someone has people at all levels have 10618 07:30:36,718 --> 07:30:38,160 missed vulnerabilities on the 10619 07:30:38,160 --> 07:30:39,558 unfortunate day that that happens be 10620 07:30:39,558 --> 07:30:41,240 sure that you and your auditor can jump 10621 07:30:41,240 --> 07:30:43,120 on a call quickly to try to remedy the 10622 07:30:43,120 --> 07:30:44,798 situation and maybe consider getting 10623 07:30:44,798 --> 07:30:46,360 insurance for your protocol as well so 10624 07:30:46,360 --> 07:30:47,920 now with that being said now you have a 10625 07:30:47,920 --> 07:30:49,760 good idea of what a smart contract audit 10626 07:30:49,760 --> 07:30:51,920 entails and what to expect end to end a 10627 07:30:51,920 --> 07:30:53,638 smart contract audit is a security 10628 07:30:53,638 --> 07:30:55,920 Journey endtoend leveling up your 10629 07:30:55,920 --> 07:30:57,878 protocol so that you can have all the 10630 07:30:57,878 --> 07:31:00,080 best practices and security know how to 10631 07:31:00,080 --> 07:31:01,840 deploy your code to a live blockchain 10632 07:31:01,840 --> 07:31:03,398 forever and of course if you're looking 10633 07:31:03,398 --> 07:31:04,920 for an audit be sure to reach out to the 10634 07:31:04,920 --> 07:31:07,000 cyphon team Link in the description and 10635 07:31:07,000 --> 07:31:08,920 as always stay safe out there and we'll 10636 07:31:08,920 --> 07:31:11,798 see you next 10637 07:31:13,160 --> 07:31:16,360 time all right great welcome back so now 10638 07:31:16,360 --> 07:31:17,520 that you know a little bit more about 10639 07:31:17,520 --> 07:31:19,520 what a smart contract audit is let's 10640 07:31:19,520 --> 07:31:21,360 talk about the process that smart 10641 07:31:21,360 --> 07:31:22,680 contract Auditors and Security 10642 07:31:22,680 --> 07:31:24,638 Professionals take and the tools that 10643 07:31:24,638 --> 07:31:27,760 they use security is both for protocol 10644 07:31:27,760 --> 07:31:30,798 developers and for auditors you as a 10645 07:31:30,798 --> 07:31:32,600 smart contract developer need to know 10646 07:31:32,600 --> 07:31:34,240 all these tools and probably should use 10647 07:31:34,240 --> 07:31:36,318 all these tools before you even go to 10648 07:31:36,318 --> 07:31:38,478 audit at the end of the day it is you 10649 07:31:38,478 --> 07:31:40,200 who is responsible for the codebase that 10650 07:31:40,200 --> 07:31:43,318 you launch and additionally you often 10651 07:31:43,318 --> 07:31:46,040 can't just do security at the end you 10652 07:31:46,040 --> 07:31:48,760 have to have security built in from the 10653 07:31:48,760 --> 07:31:51,318 architecture from day one for example if 10654 07:31:51,318 --> 07:31:52,958 you build a car and the car is a total 10655 07:31:52,958 --> 07:31:54,920 piece of garbage and you say hey cool 10656 07:31:54,920 --> 07:31:56,398 it's time to race you're probably just 10657 07:31:56,398 --> 07:31:58,280 going to have to start over and and it's 10658 07:31:58,280 --> 07:31:59,520 not a very good use of time for you to 10659 07:31:59,520 --> 07:32:02,160 even go to the audit there are some 10660 07:32:02,160 --> 07:32:04,718 tools like security which which have 10661 07:32:04,718 --> 07:32:07,000 opinionated security and code quality 10662 07:32:07,000 --> 07:32:09,478 standard for solity smart contracts and 10663 07:32:09,478 --> 07:32:11,200 we showed you the simple security 10664 07:32:11,200 --> 07:32:15,120 toolkit from n XYZ for great places to 10665 07:32:15,120 --> 07:32:17,398 read before you go to audit and make 10666 07:32:17,398 --> 07:32:20,120 sure you have checked off or you've 10667 07:32:20,120 --> 07:32:22,360 looked at this doc to make sure you're 10668 07:32:22,360 --> 07:32:24,718 even ready to go to audit anyways let's 10669 07:32:24,718 --> 07:32:27,160 talk about the audit process so there is 10670 07:32:27,160 --> 07:32:29,280 no Silver Bullet but typically the smart 10671 07:32:29,280 --> 07:32:31,040 contract audit process goes through a 10672 07:32:31,040 --> 07:32:33,760 process of manual review and then using 10673 07:32:33,760 --> 07:32:35,600 tools and there are a ton of tools 10674 07:32:35,600 --> 07:32:37,558 Security Professionals use to make sure 10675 07:32:37,558 --> 07:32:39,920 code is secure now manual review is 10676 07:32:39,920 --> 07:32:41,440 probably one of the most important if 10677 07:32:41,440 --> 07:32:43,558 not the most important tool that we have 10678 07:32:43,558 --> 07:32:45,398 in our toolbox manual review is 10679 07:32:45,398 --> 07:32:47,680 literally going through the code line by 10680 07:32:47,680 --> 07:32:49,200 line and going through the docs and 10681 07:32:49,200 --> 07:32:51,520 making sure the code actually does what 10682 07:32:51,520 --> 07:32:53,080 it's supposed to do there's kind of a 10683 07:32:53,080 --> 07:32:55,120 joke in the developer Community where if 10684 07:32:55,120 --> 07:32:56,798 you just read the documentation for 15 10685 07:32:56,798 --> 07:32:58,680 minutes you'd be way better prepared to 10686 07:32:58,680 --> 07:33:00,398 actually do code but a lot of people 10687 07:33:00,398 --> 07:33:03,478 don't do that so step one on any audit 10688 07:33:03,478 --> 07:33:05,360 is really getting some understanding of 10689 07:33:05,360 --> 07:33:08,360 what the protocol should do most bugs 10690 07:33:08,360 --> 07:33:11,280 are actually business logic issues and 10691 07:33:11,280 --> 07:33:13,638 the only way you'll know if business 10692 07:33:13,638 --> 07:33:15,798 logic is wrong is if you understand what 10693 07:33:15,798 --> 07:33:18,200 the protocol or the business should do 10694 07:33:18,200 --> 07:33:20,040 and repetition is the mother's skill so 10695 07:33:20,040 --> 07:33:22,240 the more you read code the more you read 10696 07:33:22,240 --> 07:33:23,958 docs the more you do these audits the 10697 07:33:23,958 --> 07:33:26,718 better you'll get for example for 10698 07:33:26,718 --> 07:33:28,798 example if we had a contract like this 10699 07:33:28,798 --> 07:33:31,318 this caught with past we have this set 10700 07:33:31,318 --> 07:33:33,520 number function this code technically 10701 07:33:33,520 --> 07:33:36,000 isn't wrong right it'll compile fine 10702 07:33:36,000 --> 07:33:39,000 it'll deploy fine but maybe if we read 10703 07:33:39,000 --> 07:33:41,080 the docs and we read that set number was 10704 07:33:41,080 --> 07:33:44,160 supposed to set number to new number we 10705 07:33:44,160 --> 07:33:45,558 would then read this function and go oh 10706 07:33:45,558 --> 07:33:47,040 my gosh you're setting it to new number 10707 07:33:47,040 --> 07:33:49,920 plus one that's clearly wrong right and 10708 07:33:49,920 --> 07:33:51,280 the only way we would have caught that 10709 07:33:51,280 --> 07:33:52,878 bug is by understanding what the 10710 07:33:52,878 --> 07:33:55,080 function should be doing so that's 10711 07:33:55,080 --> 07:33:56,878 manual review now let's talk about some 10712 07:33:56,878 --> 07:33:58,200 of the other tools that we have in our 10713 07:33:58,200 --> 07:34:01,040 tool box here the first line of defense 10714 07:34:01,040 --> 07:34:03,638 is going to be test Suites which is why 10715 07:34:03,638 --> 07:34:06,920 we spent so long in this course writing 10716 07:34:06,920 --> 07:34:09,318 test Suites all the most popular 10717 07:34:09,318 --> 07:34:11,160 Frameworks out there have test Suites I 10718 07:34:11,160 --> 07:34:12,440 don't really need to go over them 10719 07:34:12,440 --> 07:34:15,440 because you have already done so static 10720 07:34:15,440 --> 07:34:17,398 analysis this is something we haven't 10721 07:34:17,398 --> 07:34:20,000 gone over static analysis is going to be 10722 07:34:20,000 --> 07:34:22,318 automatically checking code for issues 10723 07:34:22,318 --> 07:34:24,478 without executing anything hence the 10724 07:34:24,478 --> 07:34:27,280 debugging is static most of these tools 10725 07:34:27,280 --> 07:34:28,760 were sort of just dumbly look for 10726 07:34:28,760 --> 07:34:31,040 keywords in specific spefic orders and 10727 07:34:31,040 --> 07:34:33,240 AI is actually going to be an example of 10728 07:34:33,240 --> 07:34:35,920 static analysis as well and we'll show 10729 07:34:35,920 --> 07:34:37,840 you an example of all of these once I 10730 07:34:37,840 --> 07:34:40,040 get through this fuzz testing we've gone 10731 07:34:40,040 --> 07:34:42,680 over fuzz testing involves providing 10732 07:34:42,680 --> 07:34:45,280 random data as inputs during testing uh 10733 07:34:45,280 --> 07:34:47,000 we have stateful fuzz testing which 10734 07:34:47,000 --> 07:34:48,280 we've definitely gone over in this 10735 07:34:48,280 --> 07:34:50,280 course as well differential testing 10736 07:34:50,280 --> 07:34:52,040 which we aren't really going to go over 10737 07:34:52,040 --> 07:34:53,680 but it's a way to actually write the 10738 07:34:53,680 --> 07:34:56,440 same code multiple times and compare the 10739 07:34:56,440 --> 07:34:58,798 code against each other but then finally 10740 07:34:58,798 --> 07:35:01,398 there's formal verification now formal 10741 07:35:01,398 --> 07:35:03,840 verification is a generic term for 10742 07:35:03,840 --> 07:35:05,958 applying formal methods to verify the 10743 07:35:05,958 --> 07:35:08,878 correctness of Hardware or software 10744 07:35:08,878 --> 07:35:11,600 applying FM means anything based off of 10745 07:35:11,600 --> 07:35:13,878 mathematical proofs in software often 10746 07:35:13,878 --> 07:35:15,558 used as a proof of correctness or proof 10747 07:35:15,558 --> 07:35:18,600 of bug or just mathematically prove that 10748 07:35:18,600 --> 07:35:21,200 something in your code can happen this 10749 07:35:21,200 --> 07:35:23,520 can be a little bit confusing obviously 10750 07:35:23,520 --> 07:35:26,240 um but to me the way that I generalize 10751 07:35:26,240 --> 07:35:28,240 it is you basically take your solidity 10752 07:35:28,240 --> 07:35:29,760 function you take your solidity code and 10753 07:35:29,760 --> 07:35:32,360 you convert it to math and math can be 10754 07:35:32,360 --> 07:35:34,240 solved math has right and wrong answers 10755 07:35:34,240 --> 07:35:37,200 1 + 1 equals 2 right so we convert our 10756 07:35:37,200 --> 07:35:39,638 solidity code to math and then 10757 07:35:39,638 --> 07:35:42,000 mathematically prove or disprove it 10758 07:35:42,000 --> 07:35:45,200 symbolic execution is one of these 10759 07:35:45,200 --> 07:35:48,040 formal verification these formal methods 10760 07:35:48,040 --> 07:35:50,318 and symbolic execution involves 10761 07:35:50,318 --> 07:35:54,000 converting our code to a mathematical 10762 07:35:54,000 --> 07:35:56,280 expression there's some tools here like 10763 07:35:56,280 --> 07:36:00,080 Matt Manor Z3 zeror even the solidity 10764 07:36:00,080 --> 07:36:01,680 compiler actually has a symbolic 10765 07:36:01,680 --> 07:36:04,280 execution ciment there is a fantastic 10766 07:36:04,280 --> 07:36:07,200 article on hackam D by Paulina who goes 10767 07:36:07,200 --> 07:36:09,160 over some of these different symbolic 10768 07:36:09,160 --> 07:36:11,160 execution clients and Compares them to 10769 07:36:11,160 --> 07:36:12,680 each other I left a link to this of 10770 07:36:12,680 --> 07:36:14,240 course in the GitHub repo associated 10771 07:36:14,240 --> 07:36:16,680 with this course symbolic execution is 10772 07:36:16,680 --> 07:36:19,318 one of these tools that is the most time 10773 07:36:19,318 --> 07:36:21,280 intensive and a lot of protocols and a 10774 07:36:21,280 --> 07:36:23,318 lot of audit firms don't even go down 10775 07:36:23,318 --> 07:36:25,240 this route because even symbolic 10776 07:36:25,240 --> 07:36:27,600 execution isn't a silver bullet it's not 10777 07:36:27,600 --> 07:36:31,000 a guarantee that some code has no bugs 10778 07:36:31,000 --> 07:36:33,520 it's not a guarantee code is bug free 10779 07:36:33,520 --> 07:36:34,840 but especially if you have some 10780 07:36:34,840 --> 07:36:37,360 incredibly math computationally heavy 10781 07:36:37,360 --> 07:36:39,520 process symbolic execution or form of 10782 07:36:39,520 --> 07:36:42,200 verification might be a good tool to use 10783 07:36:42,200 --> 07:36:45,200 AI tools AI tools are a work in progress 10784 07:36:45,200 --> 07:36:47,958 long story short sometimes they're okay 10785 07:36:47,958 --> 07:36:49,120 and sometimes they're absolutely 10786 07:36:49,120 --> 07:36:51,080 terrible all right so now that we know 10787 07:36:51,080 --> 07:36:52,840 some of these tools let's actually see 10788 07:36:52,840 --> 07:36:55,040 them in action and I'm going to go kind 10789 07:36:55,040 --> 07:36:58,120 of quick here but what we could do is if 10790 07:36:58,120 --> 07:37:00,440 we're in the GitHub repo associated with 10791 07:37:00,440 --> 07:37:02,318 this course you can actually download 10792 07:37:02,318 --> 07:37:04,280 this Denver security re now this does 10793 07:37:04,280 --> 07:37:07,200 not have the- f23 at the end of it 10794 07:37:07,200 --> 07:37:09,240 because this is a lesson that I've done 10795 07:37:09,240 --> 07:37:11,160 in the past but it is associated with 10796 07:37:11,160 --> 07:37:13,120 this course what we'll do though is 10797 07:37:13,120 --> 07:37:14,520 we'll just clone it we're not going to 10798 07:37:14,520 --> 07:37:16,000 build a project from scratch here we're 10799 07:37:16,000 --> 07:37:17,000 just going to look at some code 10800 07:37:17,000 --> 07:37:19,280 ourselves so we'll do get clone paste it 10801 07:37:19,280 --> 07:37:22,558 in do code Denver security we'll open 10802 07:37:22,558 --> 07:37:25,558 that up in a new tab here and I and I 10803 07:37:25,558 --> 07:37:27,558 want to just show you how bugs can be 10804 07:37:27,558 --> 07:37:29,680 found with some of these different tools 10805 07:37:29,680 --> 07:37:33,558 here so in our SRC folder we've got a 10806 07:37:33,558 --> 07:37:35,440 couple of files here we have caught with 10807 07:37:35,440 --> 07:37:37,798 fuzz caught with manual review CAU with 10808 07:37:37,798 --> 07:37:40,040 slither staple fo symbolic CAU with test 10809 07:37:40,040 --> 07:37:42,878 Etc each of these has a bug in them 10810 07:37:42,878 --> 07:37:44,718 where one of these methods was going to 10811 07:37:44,718 --> 07:37:47,398 be how you catch it our first one CAU 10812 07:37:47,398 --> 07:37:49,920 with manual CAU with manual review we'll 10813 07:37:49,920 --> 07:37:52,040 do a toggle word wrap the way we can 10814 07:37:52,040 --> 07:37:53,920 catch this bug is just with manual 10815 07:37:53,920 --> 07:37:57,080 review right so we have this function do 10816 07:37:57,080 --> 07:38:00,878 math and the comments or the docs here 10817 07:38:00,878 --> 07:38:04,360 say Dev adds two to number two add and 10818 07:38:04,360 --> 07:38:06,600 returns it we can then easily read the 10819 07:38:06,600 --> 07:38:08,160 code and say oh my goodness we're only 10820 07:38:08,160 --> 07:38:11,798 adding one boom that's an issue that's a 10821 07:38:11,798 --> 07:38:13,200 bug that's something that we would want 10822 07:38:13,200 --> 07:38:15,318 to fix that's how we would catch this 10823 07:38:15,318 --> 07:38:18,798 one next CAU with test. soul this is the 10824 07:38:18,798 --> 07:38:21,558 one that we saw in the presentation that 10825 07:38:21,558 --> 07:38:24,318 we gave set number should set the number 10826 07:38:24,318 --> 07:38:26,638 a unit test would easily catch that this 10827 07:38:26,638 --> 07:38:29,520 was wrong all right what's next static 10828 07:38:29,520 --> 07:38:31,680 analysis or Slither now this is 10829 07:38:31,680 --> 07:38:33,160 something we haven't really gone over 10830 07:38:33,160 --> 07:38:35,120 but one of the most prevalent hacks in 10831 07:38:35,120 --> 07:38:37,440 this industry is going to be a something 10832 07:38:37,440 --> 07:38:39,760 called a re-entrancy attack and you can 10833 07:38:39,760 --> 07:38:41,440 read more about it in the solidity by 10834 07:38:41,440 --> 07:38:43,798 example re-entrancy I've left a link to 10835 07:38:43,798 --> 07:38:45,798 this of course in the get AO associated 10836 07:38:45,798 --> 07:38:47,600 with this course but they're actually 10837 07:38:47,600 --> 07:38:49,638 tools that will automatically check for 10838 07:38:49,638 --> 07:38:52,520 stuff like this if I pull up my terminal 10839 07:38:52,520 --> 07:38:54,200 and I use this tool slyther I've already 10840 07:38:54,200 --> 07:38:55,718 gotten installed you can go ahead and 10841 07:38:55,718 --> 07:38:56,920 check out the install instructions 10842 07:38:56,920 --> 07:39:00,160 yourself but if I run slyther Dot 10843 07:39:00,160 --> 07:39:03,520 it'll automatically compile my codebase 10844 07:39:03,520 --> 07:39:05,200 and then it'll give this massive print 10845 07:39:05,200 --> 07:39:07,760 out of a ton of the issues that it found 10846 07:39:07,760 --> 07:39:10,638 and right at the top right in red you 10847 07:39:10,638 --> 07:39:14,360 can see hey there's a withdraw issue 10848 07:39:14,360 --> 07:39:16,000 there's a re-entrance issue with your 10849 07:39:16,000 --> 07:39:18,000 withdraw function and then obviously we 10850 07:39:18,000 --> 07:39:20,718 could go back update it rerun Slither 10851 07:39:20,718 --> 07:39:23,120 and this would go away so a lot of these 10852 07:39:23,120 --> 07:39:25,600 issues have tools that you can just run 10853 07:39:25,600 --> 07:39:27,558 and it'll automatically detect so 10854 07:39:27,558 --> 07:39:28,958 slyther is one of these tools that I 10855 07:39:28,958 --> 07:39:30,280 think everyone should run on their 10856 07:39:30,280 --> 07:39:32,558 codebase before going to audit what's 10857 07:39:32,558 --> 07:39:35,360 next we've caught with fuzz which we 10858 07:39:35,360 --> 07:39:36,680 know what fuzz is we learned about 10859 07:39:36,680 --> 07:39:39,558 fuzzing we have this massive function we 10860 07:39:39,558 --> 07:39:43,120 see this here say should never return 10861 07:39:43,120 --> 07:39:48,440 zero so there's probably a fuzz test so 10862 07:39:48,440 --> 07:39:50,680 if we look in our fuzz test we actually 10863 07:39:50,680 --> 07:39:53,840 smartly wrote a test fuzz here where we 10864 07:39:53,840 --> 07:39:56,240 say hey the return number from that 10865 07:39:56,240 --> 07:39:58,280 function should never be zero we can 10866 07:39:58,280 --> 07:40:02,318 then grab that Forge test Forge test 10867 07:40:02,318 --> 07:40:06,600 d-mt what's it called test fuzz and we 10868 07:40:06,600 --> 07:40:10,240 see we get a failing test here we see we 10869 07:40:10,240 --> 07:40:12,878 found that if it passes 1 10870 07:40:12,878 --> 07:40:15,878 265 it actually will fail come back to 10871 07:40:15,878 --> 07:40:18,200 our test here and we can see there's a 10872 07:40:18,200 --> 07:40:21,398 conditional if my number equals 1265 10873 07:40:21,398 --> 07:40:26,200 1265 modular 1265 is going to be 0 + 1 - 10874 07:40:26,200 --> 07:40:29,398 1 * 1 is 1 which means it return zero we 10875 07:40:29,398 --> 07:40:30,798 don't want this to return return zero 10876 07:40:30,798 --> 07:40:33,958 which means we break our 10877 07:40:43,878 --> 07:40:46,360 invariant if we call change value with 10878 07:40:46,360 --> 07:40:50,600 zero it'll set my value to zero up here 10879 07:40:50,600 --> 07:40:52,160 and then we can call do more math again 10880 07:40:52,160 --> 07:40:55,920 with zero we're going to get 0id 1 + 0 10881 07:40:55,920 --> 07:40:58,680 return Z and boom this will break as 10882 07:40:58,680 --> 07:41:01,718 well and then finally we have symbolic 10883 07:41:01,718 --> 07:41:04,040 here where we can actually just use the 10884 07:41:04,040 --> 07:41:06,478 solidity compiler to do symbolic 10885 07:41:06,478 --> 07:41:09,000 execution we have these two functions 10886 07:41:09,000 --> 07:41:11,840 Function One Function One symbolic where 10887 07:41:11,840 --> 07:41:13,478 we have this revert in here and this is 10888 07:41:13,478 --> 07:41:15,878 going to be the bug in solidity we can 10889 07:41:15,878 --> 07:41:18,318 add asserts to our code base and we'll 10890 07:41:18,318 --> 07:41:20,318 go over a video that explains this more 10891 07:41:20,318 --> 07:41:22,398 in depth to tell our solidity compiler 10892 07:41:22,398 --> 07:41:24,718 hey this should not hit and this should 10893 07:41:24,718 --> 07:41:27,040 hit this conditional should not be hit 10894 07:41:27,040 --> 07:41:29,600 this one should hit in Foundry in our 10895 07:41:29,600 --> 07:41:32,680 Foundry doomo we can add information in 10896 07:41:32,680 --> 07:41:35,440 here to send to the solidity compiler 10897 07:41:35,440 --> 07:41:36,958 we're going to tell that our solidity 10898 07:41:36,958 --> 07:41:40,080 compiler to use the CHC model Checker 10899 07:41:40,080 --> 07:41:42,240 time out of 1000 and to look for those 10900 07:41:42,240 --> 07:41:45,878 assert keywords and if we just run Forge 10901 07:41:45,878 --> 07:41:49,840 build or Forge build-- fors to force it 10902 07:41:49,840 --> 07:41:52,760 to rebuild it'll compile and it'll give 10903 07:41:52,760 --> 07:41:56,600 us this output here saying warning CHC 10904 07:41:56,600 --> 07:41:59,280 assertion violation happens here meaning 10905 07:41:59,280 --> 07:42:02,878 it found an issue it found an input such 10906 07:42:02,878 --> 07:42:06,080 that this assert line hit so if we pull 10907 07:42:06,080 --> 07:42:08,600 up our terminal we can scroll down we 10908 07:42:08,600 --> 07:42:11,440 can see exactly we can see exactly what 10909 07:42:11,440 --> 07:42:12,718 the input is 10910 07:42:12,718 --> 07:42:15,360 8088 is the input that will allow that 10911 07:42:15,360 --> 07:42:20,558 to hit if we look 8088 / 4 does indeed 10912 07:42:20,558 --> 07:42:22,520 meet this criteria so it would go ahead 10913 07:42:22,520 --> 07:42:24,558 and revert here so those are some of the 10914 07:42:24,558 --> 07:42:26,240 tools we have in our toolbox we'll go 10915 07:42:26,240 --> 07:42:27,958 over symbolic execution a little bit 10916 07:42:27,958 --> 07:42:32,718 more but let's wrap this lesson 10917 07:42:34,040 --> 07:42:37,280 up so to talk to us more about the 10918 07:42:37,280 --> 07:42:39,398 process of manual reviewing we have 10919 07:42:39,398 --> 07:42:41,398 legendary ethereum smart contract 10920 07:42:41,398 --> 07:42:44,000 researcher tincho to walk us through his 10921 07:42:44,000 --> 07:42:47,160 process in a real process of him going 10922 07:42:47,160 --> 07:42:49,478 through ens he did an audit on ens Just 10923 07:42:49,478 --> 07:42:51,440 for kicks and ended up getting paid out 10924 07:42:51,440 --> 07:42:53,398 $100,000 for finding a critical 10925 07:42:53,398 --> 07:42:55,240 vulnerability so let's learn from 10926 07:42:55,240 --> 07:43:00,520 legendary manual reviewer tincho 10927 07:43:01,878 --> 07:43:03,958 this is Tino ethereum security 10928 07:43:03,958 --> 07:43:06,200 researcher previous lead auditor at open 10929 07:43:06,200 --> 07:43:09,440 Zepp and creator of damn vulnerable Defi 10930 07:43:09,440 --> 07:43:11,360 and today we have the pleasure of 10931 07:43:11,360 --> 07:43:13,318 talking with tincho going over his 10932 07:43:13,318 --> 07:43:15,478 auditing process so that you can learn 10933 07:43:15,478 --> 07:43:18,920 how to make damn un vulnerable 10934 07:43:18,920 --> 07:43:21,080 defi to do this we're going to be doing 10935 07:43:21,080 --> 07:43:23,638 a live mock audit of the ethereum name 10936 07:43:23,638 --> 07:43:25,760 service GitHub seeing exactly some of 10937 07:43:25,760 --> 07:43:27,200 the tools and techniques that tinel 10938 07:43:27,200 --> 07:43:29,440 would use to audit this I don't have 10939 07:43:29,440 --> 07:43:32,760 super formal auditing process I really 10940 07:43:32,760 --> 07:43:35,600 think that everybody will find their own 10941 07:43:35,600 --> 07:43:38,000 ways I will just show you briefly some 10942 07:43:38,000 --> 07:43:40,000 things that I do today link to the full 10943 07:43:40,000 --> 07:43:41,718 interview in the description let's get 10944 07:43:41,718 --> 07:43:44,718 froggy so this is a repository for inss 10945 07:43:44,718 --> 07:43:45,878 the first thing that I would do is 10946 07:43:45,878 --> 07:43:47,600 obviously go to repository you would 10947 07:43:47,600 --> 07:43:49,798 clone the repository to my local 10948 07:43:49,798 --> 07:43:51,558 environment but if you're like very 10949 07:43:51,558 --> 07:43:53,200 unfamiliar you should probably go to the 10950 07:43:53,200 --> 07:43:55,160 documentation at least I don't know read 10951 07:43:55,160 --> 07:43:56,798 the introduction now he says there's no 10952 07:43:56,798 --> 07:43:58,478 formal audit process but this sounds 10953 07:43:58,478 --> 07:44:00,280 like a good step one download the code 10954 07:44:00,280 --> 07:44:03,160 read the documentation read the 10955 07:44:03,160 --> 07:44:04,718 documentation here we have the 10956 07:44:04,718 --> 07:44:06,920 architecture is telling us like already 10957 07:44:06,920 --> 07:44:09,200 some keywords that we will need to 10958 07:44:09,200 --> 07:44:10,680 understand at some point such as what a 10959 07:44:10,680 --> 07:44:13,200 registry is what a resolver is already I 10960 07:44:13,200 --> 07:44:14,840 will get familiar with this probably 10961 07:44:14,840 --> 07:44:17,120 these are contracts that I'm about to 10962 07:44:17,120 --> 07:44:19,240 see in the code and so on and so forth 10963 07:44:19,240 --> 07:44:20,760 one thing that you can do also after 10964 07:44:20,760 --> 07:44:22,718 reading some documentation is looking at 10965 07:44:22,718 --> 07:44:24,840 audit reports if we go to the actual 10966 07:44:24,840 --> 07:44:26,558 code we will realize that there are lots 10967 07:44:26,558 --> 07:44:28,680 of things wait a minute what the heck is 10968 07:44:28,680 --> 07:44:31,398 that look logo what wonky text editor is 10969 07:44:31,398 --> 07:44:33,240 tincho using that my friends would be 10970 07:44:33,240 --> 07:44:36,160 vscodium it's different from vs code vs 10971 07:44:36,160 --> 07:44:37,958 code is a product owned by Microsoft 10972 07:44:37,958 --> 07:44:39,920 that actually sends a lot of your usage 10973 07:44:39,920 --> 07:44:42,120 information over to Microsoft vscodium 10974 07:44:42,120 --> 07:44:43,920 doesn't do this they have removed I 10975 07:44:43,920 --> 07:44:46,638 think Telemetry and some related to 10976 07:44:46,638 --> 07:44:48,318 Microsoft tjo said he's just been trying 10977 07:44:48,318 --> 07:44:49,760 it out recently but maybe it's a 10978 07:44:49,760 --> 07:44:51,718 security Alpha leak so multiple 10979 07:44:51,718 --> 07:44:54,120 contracts in here already we see that 10980 07:44:54,120 --> 07:44:57,200 lots of folders it's using harad from 10981 07:44:57,200 --> 07:44:59,360 what I can tell these days I like that 10982 07:44:59,360 --> 07:45:01,080 use Foundry more than those that use 10983 07:45:01,080 --> 07:45:03,000 herut so in that case what I would do is 10984 07:45:03,000 --> 07:45:05,160 I created another folder in which I have 10985 07:45:05,160 --> 07:45:07,120 a Foundry local setup why do you like 10986 07:45:07,120 --> 07:45:08,558 Foundry better why do you make this 10987 07:45:08,558 --> 07:45:11,638 Foundry local setup it's faster and I 10988 07:45:11,638 --> 07:45:14,718 can write quick test only using solidity 10989 07:45:14,718 --> 07:45:16,798 I will do whatever thing that I want to 10990 07:45:16,798 --> 07:45:19,318 do here but just an easy way to have 10991 07:45:19,318 --> 07:45:21,280 something quick and dirty to test things 10992 07:45:21,280 --> 07:45:23,558 quickly bring and use the tools that 10993 07:45:23,558 --> 07:45:25,478 you're most familiar and best with I 10994 07:45:25,478 --> 07:45:26,798 think that's super important and don't 10995 07:45:26,798 --> 07:45:28,478 be afraid to bring your disgustingly 10996 07:45:28,478 --> 07:45:30,958 horrible dirty dirty 10997 07:45:30,958 --> 07:45:33,638 tests but anyway already we saw that 10998 07:45:33,638 --> 07:45:35,798 it's quite complex so what I would do in 10999 07:45:35,798 --> 07:45:37,718 this case is there is a command L 11000 07:45:37,718 --> 07:45:39,878 utility that I would use which is called 11001 07:45:39,878 --> 07:45:42,440 celoc celo will help you count lines of 11002 07:45:42,440 --> 07:45:44,240 code and so I would use celo it would 11003 07:45:44,240 --> 07:45:46,120 give me a nice output that you can 11004 07:45:46,120 --> 07:45:48,760 actually pass to a CSV and instead of 11005 07:45:48,760 --> 07:45:51,440 doing this here what I would usually do 11006 07:45:51,440 --> 07:45:53,798 is I would move that to a spreadsheet I 11007 07:45:53,798 --> 07:45:56,080 have the scope for in right all these 11008 07:45:56,080 --> 07:45:58,360 files are now ordered here and now I can 11009 07:45:58,360 --> 07:46:00,760 have a view in terms of how many files 11010 07:46:00,760 --> 07:46:02,718 do I have how complex they might be so 11011 07:46:02,718 --> 07:46:05,080 apparently I have 59 files and now I 11012 07:46:05,080 --> 07:46:07,280 know the name wrapper will be one of the 11013 07:46:07,280 --> 07:46:09,318 most complex conts perhaps right because 11014 07:46:09,318 --> 07:46:11,920 it has more than 700 lines of code 11015 07:46:11,920 --> 07:46:13,878 another approach to do this scoping 11016 07:46:13,878 --> 07:46:16,200 phase you can actually use this tool by 11017 07:46:16,200 --> 07:46:17,878 consensus which is called solidity 11018 07:46:17,878 --> 07:46:19,760 metric so you can run it on a project 11019 07:46:19,760 --> 07:46:21,718 and it will actually give you a nice 11020 07:46:21,718 --> 07:46:24,080 report of the code ways and level of 11021 07:46:24,080 --> 07:46:26,840 complexity and then I have a column 11022 07:46:26,840 --> 07:46:29,878 stating where the thing that I do is not 11023 07:46:29,878 --> 07:46:31,920 started it's in progress or it's done so 11024 07:46:31,920 --> 07:46:33,558 this is his next step he either uses 11025 07:46:33,558 --> 07:46:35,478 solidity metrics or CL loock ranks out 11026 07:46:35,478 --> 07:46:36,760 the contracts that he needs to audit 11027 07:46:36,760 --> 07:46:38,520 based on complexity and starts going 11028 07:46:38,520 --> 07:46:40,080 through it moving contracts from not 11029 07:46:40,080 --> 07:46:42,200 started to in progress to done a very 11030 07:46:42,200 --> 07:46:44,878 organized approach when you is alone in 11031 07:46:44,878 --> 07:46:47,080 might seem silly but when you work in 11032 07:46:47,080 --> 07:46:49,160 teams it's quite important as the a 11033 07:46:49,160 --> 07:46:50,920 progresses I will be less and less 11034 07:46:50,920 --> 07:46:52,638 focused on this file because probably 11035 07:46:52,638 --> 07:46:54,398 this is super complex and will related 11036 07:46:54,398 --> 07:46:56,360 to either but most of all it's very 11037 07:46:56,360 --> 07:46:58,120 useful at least for me at the beginning 11038 07:46:58,120 --> 07:47:00,318 of the just understand what am I looking 11039 07:47:00,318 --> 07:47:02,718 at once I have this table I usually 11040 07:47:02,718 --> 07:47:04,798 start with the little Legos and then I 11041 07:47:04,798 --> 07:47:07,398 go move up in complex so in this case I 11042 07:47:07,398 --> 07:47:08,760 will probably I don't know start with 11043 07:47:08,760 --> 07:47:11,558 the erc20 recoverable contract here it 11044 07:47:11,558 --> 07:47:13,360 is and it's quite short say okay it's 11045 07:47:13,360 --> 07:47:15,520 ownable inheriting from open saing as 11046 07:47:15,520 --> 07:47:17,280 aitor probably I can take that for 11047 07:47:17,280 --> 07:47:18,920 granted which is out of the scope and I 11048 07:47:18,920 --> 07:47:20,878 will assume that's working correctly and 11049 07:47:20,878 --> 07:47:23,120 it has a single function to recover fun 11050 07:47:23,120 --> 07:47:24,718 okay it has access control so this is 11051 07:47:24,718 --> 07:47:26,240 probably fine as long as they are 11052 07:47:26,240 --> 07:47:28,478 handling access controls in the right 11053 07:47:28,478 --> 07:47:30,600 way this is fine and it's actually doing 11054 07:47:30,600 --> 07:47:32,200 this right so we start with the small 11055 07:47:32,200 --> 07:47:33,840 little building blocks or Legos as 11056 07:47:33,840 --> 07:47:35,440 tincho said and now you're going to see 11057 07:47:35,440 --> 07:47:38,160 Tino's brain start switching into how 11058 07:47:38,160 --> 07:47:40,638 can I break this as an aor you might 11059 07:47:40,638 --> 07:47:42,920 start wondering where this is good for 11060 07:47:42,920 --> 07:47:45,318 any token out there right where it's 11061 07:47:45,318 --> 07:47:47,600 possible to actually execute a transfer 11062 07:47:47,600 --> 07:47:49,878 on any address that the owner passes 11063 07:47:49,878 --> 07:47:51,760 here and where that could be problematic 11064 07:47:51,760 --> 07:47:54,798 for behaved erc20 tokens and if you're 11065 07:47:54,798 --> 07:47:57,040 familiar with usdt for example that 11066 07:47:57,040 --> 07:47:58,920 could be problematic in this ah now 11067 07:47:58,920 --> 07:48:00,000 we're seeing him drawing on his 11068 07:48:00,000 --> 07:48:02,920 expertise knowing that usdt is a weird 11069 07:48:02,920 --> 07:48:06,200 token usdt actually doesn't return a 11070 07:48:06,200 --> 07:48:08,360 Boolean on its transfer froms whereas a 11071 07:48:08,360 --> 07:48:10,478 lot of other tokens actually do what 11072 07:48:10,478 --> 07:48:12,440 usdt when you see that only owner 11073 07:48:12,440 --> 07:48:15,798 function do you think okay is this a dow 11074 07:48:15,798 --> 07:48:18,760 is there a single person who controls 11075 07:48:18,760 --> 07:48:20,080 what are your thoughts at least at the 11076 07:48:20,080 --> 07:48:21,600 beginning I wouldn't worry about it too 11077 07:48:21,600 --> 07:48:22,840 much at some point I will read 11078 07:48:22,840 --> 07:48:24,878 documentation about roles of this but 11079 07:48:24,878 --> 07:48:26,360 yeah at some point I should probably 11080 07:48:26,360 --> 07:48:27,478 understand at some point Who's the 11081 07:48:27,478 --> 07:48:30,280 actual owner okay say that you think 11082 07:48:30,280 --> 07:48:32,680 that this is okay so what I would do 11083 07:48:32,680 --> 07:48:34,398 usually is I would take notes in the 11084 07:48:34,398 --> 07:48:36,638 code right so in this case I would say 11085 07:48:36,638 --> 07:48:39,000 like Access Control okay for example 11086 07:48:39,000 --> 07:48:41,440 just to have a note saying that I was 11087 07:48:41,440 --> 07:48:42,760 here or you can have a question like 11088 07:48:42,760 --> 07:48:45,280 Patrick said Is this governance right 11089 07:48:45,280 --> 07:48:47,318 let's say this was an issue so I would 11090 07:48:47,318 --> 07:48:49,040 do this shouldn't be I know let's say 11091 07:48:49,040 --> 07:48:51,718 shouldn't be owner another thing that I 11092 07:48:51,718 --> 07:48:53,638 do to take notes is actually have noes 11093 07:48:53,638 --> 07:48:56,718 files in the same place very R nodes 11094 07:48:56,718 --> 07:48:59,318 very having a file where I can quickly 11095 07:48:59,318 --> 07:49:01,718 dump ideas that I have some point things 11096 07:49:01,718 --> 07:49:04,280 so go well I would have an issues list 11097 07:49:04,280 --> 07:49:07,520 here I will start listing I know in line 11098 07:49:07,520 --> 07:49:10,398 blah blah blah of file blah blah blah 11099 07:49:10,398 --> 07:49:12,878 issues take notes in the code in a notes 11100 07:49:12,878 --> 07:49:15,240 file somewhere just have a place where 11101 07:49:15,240 --> 07:49:17,160 you can dump thoughts maybe you can even 11102 07:49:17,160 --> 07:49:20,000 use a note taking plugin I don't use any 11103 07:49:20,000 --> 07:49:22,360 plugins that's because I don't like 11104 07:49:22,360 --> 07:49:25,280 having the UI clut too much with stuff 11105 07:49:25,280 --> 07:49:29,280 yeah no taking extension was a dumb idea 11106 07:49:29,280 --> 07:49:30,520 and one thing that you have to be 11107 07:49:30,520 --> 07:49:32,080 careful when you start with the very 11108 07:49:32,080 --> 07:49:34,398 little things so it's very easy to go 11109 07:49:34,398 --> 07:49:36,680 deep into the rabbit hole of the single 11110 07:49:36,680 --> 07:49:38,478 thing that you are trying to AIT is as 11111 07:49:38,478 --> 07:49:40,878 for example I have very little knowledge 11112 07:49:40,878 --> 07:49:42,958 of DNS I'm not like super familiar with 11113 07:49:42,958 --> 07:49:45,360 the and I got like very like down into 11114 07:49:45,360 --> 07:49:47,958 DNS because I was just paying attention 11115 07:49:47,958 --> 07:49:50,240 to this single resolver and that led me 11116 07:49:50,240 --> 07:49:52,520 to realize that after I know two days I 11117 07:49:52,520 --> 07:49:54,958 was growing familiar with DNS but I was 11118 07:49:54,958 --> 07:49:57,120 losing the big picture of actually hey 11119 07:49:57,120 --> 07:49:59,718 I'm actually putting ens so it's very 11120 07:49:59,718 --> 07:50:01,840 easy to go deep into the rabbit hole of 11121 07:50:01,840 --> 07:50:04,680 the single thing that you trying to 11122 07:50:04,680 --> 07:50:06,760 audit remember to jump out of the rabbit 11123 07:50:06,760 --> 07:50:08,878 holes perfect so at some points in 11124 07:50:08,878 --> 07:50:10,680 during the audit you will realize that 11125 07:50:10,680 --> 07:50:12,958 you might need to test things so in this 11126 07:50:12,958 --> 07:50:15,638 case I saw these two functions one is 11127 07:50:15,638 --> 07:50:18,280 going from my bites thing she returning 11128 07:50:18,280 --> 07:50:20,000 an address and the other is doing the 11129 07:50:20,000 --> 07:50:22,318 other way around it you might say it's 11130 07:50:22,318 --> 07:50:24,760 okay but if you're lazy you can actually 11131 07:50:24,760 --> 07:50:27,398 use fun to help you in that so what I 11132 07:50:27,398 --> 07:50:30,360 did in this case I used my very hungry 11133 07:50:30,360 --> 07:50:33,240 hacky boundary repo that I have in here 11134 07:50:33,240 --> 07:50:35,638 and I actually copied these two 11135 07:50:35,638 --> 07:50:38,638 functions to a contract like I just took 11136 07:50:38,638 --> 07:50:40,520 them not even trying to do fancy Imports 11137 07:50:40,520 --> 07:50:42,958 or whatever just BR copying and pasting 11138 07:50:42,958 --> 07:50:45,718 them here and I did a quick test uh F 11139 07:50:45,718 --> 07:50:47,638 test in which I provide an address I 11140 07:50:47,638 --> 07:50:49,558 pass that address to this for function 11141 07:50:49,558 --> 07:50:52,280 and the result of that I pass it to the 11142 07:50:52,280 --> 07:50:54,478 other function and I want to make sure 11143 07:50:54,478 --> 07:50:57,080 that I always get the same address as I 11144 07:50:57,080 --> 07:50:58,600 said a lot of times yes you're going to 11145 07:50:58,600 --> 07:51:00,200 write code and not just do manual 11146 07:51:00,200 --> 07:51:02,080 reviews T here wrote what's called a 11147 07:51:02,080 --> 07:51:03,718 fuzz test which you should absolutely 11148 07:51:03,718 --> 07:51:04,840 smash that like button because we're 11149 07:51:04,840 --> 07:51:06,000 going to have a video on that sometime 11150 07:51:06,000 --> 07:51:08,040 in the future use your tools to validate 11151 07:51:08,040 --> 07:51:09,718 findings that you have an inkling are 11152 07:51:09,718 --> 07:51:12,000 wrong in the cases where I have to set 11153 07:51:12,000 --> 07:51:14,600 up more complex stuff perhaps having a 11154 07:51:14,600 --> 07:51:16,958 separate folder with a single project is 11155 07:51:16,958 --> 07:51:18,958 not that convenient because I would need 11156 07:51:18,958 --> 07:51:21,398 to set up the whole in system and that 11157 07:51:21,398 --> 07:51:23,478 wouldn't be that convenient so in those 11158 07:51:23,478 --> 07:51:26,478 cases I would go to the actual testing 11159 07:51:26,478 --> 07:51:28,200 environment of the project if you're 11160 07:51:28,200 --> 07:51:30,000 doing a private out how important is the 11161 07:51:30,000 --> 07:51:32,280 process of of talking and interacting 11162 07:51:32,280 --> 07:51:34,398 and and keeping Communications with the 11163 07:51:34,398 --> 07:51:36,280 client open I would say it's almost 11164 07:51:36,280 --> 07:51:39,160 fundamental usually developers will have 11165 07:51:39,160 --> 07:51:42,398 much more context than you as an auditor 11166 07:51:42,398 --> 07:51:44,958 on what the system is intending to do so 11167 07:51:44,958 --> 07:51:46,718 you can spend a whole week trying to 11168 07:51:46,718 --> 07:51:48,760 figure out on your own where this 11169 07:51:48,760 --> 07:51:51,318 modifier should be in this function or 11170 07:51:51,318 --> 07:51:53,160 not but if you actually send a question 11171 07:51:53,160 --> 07:51:54,958 to the client and tell them hey should 11172 07:51:54,958 --> 07:51:56,398 this be here or not and they will tell 11173 07:51:56,398 --> 07:51:58,318 yes it should be here you can see it in 11174 07:51:58,318 --> 07:52:00,280 test blah blah blah you can see them as 11175 07:52:00,280 --> 07:52:02,240 companions during the O and you should 11176 07:52:02,240 --> 07:52:04,080 rely on them you can rely on the client 11177 07:52:04,080 --> 07:52:07,240 AO protocol is your code good you think 11178 07:52:07,240 --> 07:52:11,280 so hell yeah my work is done having said 11179 07:52:11,280 --> 07:52:13,318 that it's also important not to trust 11180 07:52:13,318 --> 07:52:15,360 too much at the end of the day they are 11181 07:52:15,360 --> 07:52:18,958 trusting you as the expert so in this in 11182 07:52:18,958 --> 07:52:21,318 that sense I would advise okay keep the 11183 07:52:21,318 --> 07:52:24,120 clients at hand ask questions but also 11184 07:52:24,120 --> 07:52:26,600 be detached enough since they built the 11185 07:52:26,600 --> 07:52:29,040 code they have spent more time time 11186 07:52:29,040 --> 07:52:30,360 thinking about the code and looking at 11187 07:52:30,360 --> 07:52:32,280 the code than you ever will ask them 11188 07:52:32,280 --> 07:52:33,840 questions and don't be afraid to ask 11189 07:52:33,840 --> 07:52:35,638 questions I guess at this point what's 11190 07:52:35,638 --> 07:52:37,520 the next step are we wrapping up now are 11191 07:52:37,520 --> 07:52:39,280 we writing the report up what do we do 11192 07:52:39,280 --> 07:52:42,000 next the thing is I always get the 11193 07:52:42,000 --> 07:52:43,398 feeling that you can be looking at a 11194 07:52:43,398 --> 07:52:46,120 system forever it there's always one 11195 07:52:46,120 --> 07:52:47,760 additional line that you can check 11196 07:52:47,760 --> 07:52:49,478 there's always one additional attack 11197 07:52:49,478 --> 07:52:51,280 that you can think of one additional 11198 07:52:51,280 --> 07:52:53,318 potential C my vulnerability and 11199 07:52:53,318 --> 07:52:55,920 everything so what I do is I time bound 11200 07:52:55,920 --> 07:52:57,718 myself to have a certain level of 11201 07:52:57,718 --> 07:52:59,080 confidence when you're shipping the 11202 07:52:59,080 --> 07:53:01,318 report that you did your best and you 11203 07:53:01,318 --> 07:53:03,798 thought of every single possible attack 11204 07:53:03,798 --> 07:53:05,718 or vulnerability that you could think of 11205 07:53:05,718 --> 07:53:07,718 in that limited amount of time time 11206 07:53:07,718 --> 07:53:08,520 bound 11207 07:53:08,520 --> 07:53:11,360 yourself when you're going through this 11208 07:53:11,360 --> 07:53:14,120 code how are you thinking of different 11209 07:53:14,120 --> 07:53:15,680 attacks like when you're looking at a 11210 07:53:15,680 --> 07:53:17,478 piece of code how do you get that 11211 07:53:17,478 --> 07:53:18,958 context of what different types of 11212 07:53:18,958 --> 07:53:20,718 attacks to think of yeah I don't have a 11213 07:53:20,718 --> 07:53:22,920 checklist very difficult to translate 11214 07:53:22,920 --> 07:53:25,680 experience in doing it have this 11215 07:53:25,680 --> 07:53:28,080 adversarial mindset of try to at least 11216 07:53:28,080 --> 07:53:29,920 there lots of knowledge that can come 11217 07:53:29,920 --> 07:53:32,360 from all every single day reading 11218 07:53:32,360 --> 07:53:34,200 vulnerability reports every single 11219 07:53:34,200 --> 07:53:36,760 daying or reading responsible 11220 07:53:36,760 --> 07:53:38,840 disclosures that are published reading 11221 07:53:38,840 --> 07:53:40,840 all this reports I read newsletters like 11222 07:53:40,840 --> 07:53:43,318 I don't know I have this constant influx 11223 07:53:43,318 --> 07:53:45,600 of in security related information to 11224 07:53:45,600 --> 07:53:47,798 solidity that little by little I think 11225 07:53:47,798 --> 07:53:49,318 you start growing the intuitions the 11226 07:53:49,318 --> 07:53:51,440 experience that actually help you 11227 07:53:51,440 --> 07:53:53,760 identify quickly things that can happen 11228 07:53:53,760 --> 07:53:56,280 in smart contracts and always remember 11229 07:53:56,280 --> 07:53:58,638 that you can miss things there's no per 11230 07:53:58,638 --> 07:54:01,200 auditor I think that everybody has 11231 07:54:01,200 --> 07:54:03,718 audited sufficient enough and complex 11232 07:54:03,718 --> 07:54:06,040 enough systems they have all mied issues 11233 07:54:06,040 --> 07:54:08,360 and it's okay security is a is a thing 11234 07:54:08,360 --> 07:54:09,718 that we have to approach from many 11235 07:54:09,718 --> 07:54:12,280 different angles and auditing is just 11236 07:54:12,280 --> 07:54:14,680 one thing that must be done but is not 11237 07:54:14,680 --> 07:54:16,318 the only one knowing that you're doing 11238 07:54:16,318 --> 07:54:17,558 your best in that knowing that you're 11239 07:54:17,558 --> 07:54:19,080 putting your best effort every day 11240 07:54:19,080 --> 07:54:21,558 growing your skills learning grow and 11241 07:54:21,558 --> 07:54:23,280 intuition and experienceing you 11242 07:54:23,280 --> 07:54:25,638 something that I always say is to audit 11243 07:54:25,638 --> 07:54:28,798 to me is 50% finding vulnerability and 11244 07:54:28,798 --> 07:54:31,680 50% delivering readable report once the 11245 07:54:31,680 --> 07:54:33,478 client starts fixing the issues they 11246 07:54:33,478 --> 07:54:35,360 will send you the fixes for the issues 11247 07:54:35,360 --> 07:54:37,040 and what you have to do at that point is 11248 07:54:37,040 --> 07:54:39,360 actually review the fixes and make sure 11249 07:54:39,360 --> 07:54:42,040 that not only the vulnerability that you 11250 07:54:42,040 --> 07:54:44,000 highlighted in the report is fixed but 11251 07:54:44,000 --> 07:54:45,600 actually this have been introduced by 11252 07:54:45,600 --> 07:54:47,760 the fix and then you wrap up your whole 11253 07:54:47,760 --> 07:54:50,040 auditing process with writing a very 11254 07:54:50,040 --> 07:54:52,638 good report and take the time to do so 11255 07:54:52,638 --> 07:54:54,280 once you give them the report they will 11256 07:54:54,280 --> 07:54:56,200 go ahead and fix the issues come back 11257 07:54:56,200 --> 07:54:57,958 say hey we fixed them and then it's your 11258 07:54:57,958 --> 07:54:59,558 job to make sure that they fix the 11259 07:54:59,558 --> 07:55:02,638 issues and they didn't reintroduce new 11260 07:55:02,638 --> 07:55:05,440 bugs let's say you give your audit 11261 07:55:05,440 --> 07:55:08,040 report done your time box you've done as 11262 07:55:08,040 --> 07:55:09,680 much as you can you think you did a good 11263 07:55:09,680 --> 07:55:14,280 job four months goes by oh my God 100 11264 07:55:14,280 --> 07:55:16,200 million dollar hack they've ended it up 11265 07:55:16,200 --> 07:55:19,520 on wrecked what do you do what happens 11266 07:55:19,520 --> 07:55:21,718 let me approach you slowly okay all we 11267 07:55:21,718 --> 07:55:24,120 first say I have always been of the idea 11268 07:55:24,120 --> 07:55:26,478 a security code review should be 11269 07:55:26,478 --> 07:55:28,840 valuable enough beyond the fact that I 11270 07:55:28,840 --> 07:55:31,360 find or not find a critical issue so I 11271 07:55:31,360 --> 07:55:33,520 should be able to provide value to 11272 07:55:33,520 --> 07:55:35,120 whoever is working with me to whoever is 11273 07:55:35,120 --> 07:55:37,958 trusting me beyond the fact that I did 11274 07:55:37,958 --> 07:55:41,000 or did not find a crial isue obviously 11275 07:55:41,000 --> 07:55:43,080 the less critical issues that you miss 11276 07:55:43,080 --> 07:55:45,160 the better the safer and perhaps they 11277 07:55:45,160 --> 07:55:47,240 miss something and that can happen and 11278 07:55:47,240 --> 07:55:49,478 has happened and will continue to happen 11279 07:55:49,478 --> 07:55:51,958 but it's naive to think in my opinion 11280 07:55:51,958 --> 07:55:54,040 that just because an auditor miss 11281 07:55:54,040 --> 07:55:56,240 something whole blame of thing is on the 11282 07:55:56,240 --> 07:55:57,920 audit this I think is a really important 11283 07:55:57,920 --> 07:55:59,718 final thought you as an auditor it is 11284 07:55:59,718 --> 07:56:01,920 not solely your job to make sure their 11285 07:56:01,920 --> 07:56:04,680 code is bug free you share that 11286 07:56:04,680 --> 07:56:06,718 responsibility with the client however 11287 07:56:06,718 --> 07:56:08,920 this doesn't give you free range to suck 11288 07:56:08,920 --> 07:56:11,440 at your job people will notice tcho 11289 07:56:11,440 --> 07:56:12,520 thank you so much for being here thank 11290 07:56:12,520 --> 07:56:14,600 you for all you've done for the web3 11291 07:56:14,600 --> 07:56:16,360 community and I'm sure everyone will get 11292 07:56:16,360 --> 07:56:18,160 a lot from this okay padrick thank you 11293 07:56:18,160 --> 07:56:20,430 for inviting me bye bye 11294 07:56:20,430 --> 07:56:33,069 [Music] 11295 07:56:38,478 --> 07:56:40,440 so to understand symbolic execution a 11296 07:56:40,440 --> 07:56:42,120 little bit more we've got some members 11297 07:56:42,120 --> 07:56:44,080 from the trail of bits team to teach us 11298 07:56:44,080 --> 07:56:48,040 about symbolic execution and formal 11299 07:56:49,878 --> 07:56:52,318 verification formal verification is the 11300 07:56:52,318 --> 07:56:54,240 act of proving or disproving a given 11301 07:56:54,240 --> 07:56:55,920 property of a system using a 11302 07:56:55,920 --> 07:56:58,200 mathematical model symbolic execution is 11303 07:56:58,200 --> 07:57:00,240 is one technique used for formal 11304 07:57:00,240 --> 07:57:03,040 verification symbolic execution explores 11305 07:57:03,040 --> 07:57:05,200 different paths in a program creating a 11306 07:57:05,200 --> 07:57:07,318 mathematical representation for each 11307 07:57:07,318 --> 07:57:09,160 path or more plainly converting your 11308 07:57:09,160 --> 07:57:10,840 code to a set of mathematical 11309 07:57:10,840 --> 07:57:12,600 Expressions is this the Silver Bullet 11310 07:57:12,600 --> 07:57:14,160 for your auditing Journey let's get 11311 07:57:14,160 --> 07:57:16,638 froggy I had the absolute pleasure of 11312 07:57:16,638 --> 07:57:18,440 interviewing trailer bits head of 11313 07:57:18,440 --> 07:57:20,280 engineering Joselyn and security 11314 07:57:20,280 --> 07:57:22,558 engineer from Trailer bits as well Troy 11315 07:57:22,558 --> 07:57:25,558 about fuzzing testing methodologies and 11316 07:57:25,558 --> 07:57:27,760 importantly formal verification I have 11317 07:57:27,760 --> 07:57:29,718 links to the full interviews with both 11318 07:57:29,718 --> 07:57:31,440 of them in the description and in this 11319 07:57:31,440 --> 07:57:33,280 video we're going to summarize a lot of 11320 07:57:33,280 --> 07:57:35,200 that and focus on what formal 11321 07:57:35,200 --> 07:57:37,120 verification and symbolic execution 11322 07:57:37,120 --> 07:57:39,120 actually are but to do that we need a 11323 07:57:39,120 --> 07:57:40,680 quick refresher on some of the testing 11324 07:57:40,680 --> 07:57:42,558 methodologies that we use in web 3 if 11325 07:57:42,558 --> 07:57:43,958 you haven't seen my invariant testing 11326 07:57:43,958 --> 07:57:45,760 video be sure to watch that first before 11327 07:57:45,760 --> 07:57:47,638 coming here and to really understand 11328 07:57:47,638 --> 07:57:50,120 this you do need at least a high level 11329 07:57:50,120 --> 07:57:51,718 understanding of solidity as well what 11330 07:57:51,718 --> 07:57:54,280 tools do we have in our toolbox to have 11331 07:57:54,280 --> 07:57:56,440 high Assurance about our program layer 11332 07:57:56,440 --> 07:57:58,558 one the unit test obviously you have 11333 07:57:58,558 --> 07:58:00,558 unit test which you do a very specific 11334 07:58:00,558 --> 07:58:03,120 thing like this function does this and 11335 07:58:03,120 --> 07:58:04,558 you start looking at things like test 11336 07:58:04,558 --> 07:58:07,160 code coverage like statement and Branch 11337 07:58:07,160 --> 07:58:09,080 coverage and I think that's kind of the 11338 07:58:09,080 --> 07:58:10,760 bare minimum the absolute bare minimum 11339 07:58:10,760 --> 07:58:13,080 in security is a unit test for example 11340 07:58:13,080 --> 07:58:14,520 if this is our solidity and we have a 11341 07:58:14,520 --> 07:58:17,120 set number function which should set our 11342 07:58:17,120 --> 07:58:18,840 number variable to whatever new number 11343 07:58:18,840 --> 07:58:20,440 is a unit test would be able to catch 11344 07:58:20,440 --> 07:58:22,080 this and if we're using Foundry we could 11345 07:58:22,080 --> 07:58:23,600 get an output that looks like this so 11346 07:58:23,600 --> 07:58:25,080 I'm assertion failed and we can go see 11347 07:58:25,080 --> 07:58:26,718 our test and see that when we call set 11348 07:58:26,718 --> 07:58:28,398 number toine number we assert that we 11349 07:58:28,398 --> 07:58:30,398 get what we want and if that breaks cool 11350 07:58:30,398 --> 07:58:32,360 our unit tests have saved us Foundry 11351 07:58:32,360 --> 07:58:34,558 hard hat apworks truffle brownie and all 11352 07:58:34,558 --> 07:58:36,080 the most popular Frameworks have unit 11353 07:58:36,080 --> 07:58:38,440 test built in Layer Two the fuzz test 11354 07:58:38,440 --> 07:58:41,440 fuzzing is where you take random inputs 11355 07:58:41,440 --> 07:58:43,520 and run them through your program as you 11356 07:58:43,520 --> 07:58:45,160 have to Define things in your code that 11357 07:58:45,160 --> 07:58:47,318 you always want to hold true and fuzz 11358 07:58:47,318 --> 07:58:49,760 testing is the new bare minimum for web 11359 07:58:49,760 --> 07:58:51,920 3 security because I decree and as Troy 11360 07:58:51,920 --> 07:58:53,440 said you need to understand the property 11361 07:58:53,440 --> 07:58:55,080 or invariant of your system to do 11362 07:58:55,080 --> 07:58:56,680 fuzzing once you have your property 11363 07:58:56,680 --> 07:58:58,600 defined you throw random data at your 11364 07:58:58,600 --> 07:59:00,798 system in order to break that property 11365 07:59:00,798 --> 07:59:02,080 if you find something that breaks it you 11366 07:59:02,080 --> 07:59:03,558 know you have an edge case that you need 11367 07:59:03,558 --> 07:59:05,680 to refactor your code to handle for 11368 07:59:05,680 --> 07:59:07,840 example if I have a function like do 11369 07:59:07,840 --> 07:59:09,878 more math and I know that this function 11370 07:59:09,878 --> 07:59:11,558 should never return zero I can throw a 11371 07:59:11,558 --> 07:59:13,200 whole bunch of random numbers in here to 11372 07:59:13,200 --> 07:59:15,080 try to get it to return zero I can have 11373 07:59:15,080 --> 07:59:16,760 a fuzz test that looks like this which 11374 07:59:16,760 --> 07:59:17,920 passes in a whole bunch of random 11375 07:59:17,920 --> 07:59:19,638 numbers and tries to assert that we 11376 07:59:19,638 --> 07:59:21,440 never get zero and we can see from our 11377 07:59:21,440 --> 07:59:23,440 fuzz test it was able to pick a random 11378 07:59:23,440 --> 07:59:25,280 number in order to break our invariant 11379 07:59:25,280 --> 07:59:27,160 or property now like I said we recently 11380 07:59:27,160 --> 07:59:29,280 did a video on fuzz testing or invariant 11381 07:59:29,280 --> 07:59:31,520 testing and I highly recommend everybody 11382 07:59:31,520 --> 07:59:33,200 watch that video because I de create 11383 07:59:33,200 --> 07:59:35,478 Foundry AK kidna and consensus diligence 11384 07:59:35,478 --> 07:59:37,080 fuzzer are some popular fuzz testing 11385 07:59:37,080 --> 07:59:39,080 tools okay what's next layer three 11386 07:59:39,080 --> 07:59:41,478 static analysis so the things that we've 11387 07:59:41,478 --> 07:59:43,478 been discussing unit testing and fuzz 11388 07:59:43,478 --> 07:59:46,440 testing or dynamic testing which Dynamic 11389 07:59:46,440 --> 07:59:47,600 just means like you're you're actually 11390 07:59:47,600 --> 07:59:49,638 doing something Dynamic analysis is when 11391 07:59:49,638 --> 07:59:52,398 we run or we execute our code in unit 11392 07:59:52,398 --> 07:59:55,040 and fuzz tests we do exactly this we are 11393 07:59:55,040 --> 07:59:57,440 running our code to try to break it in 11394 07:59:57,440 --> 08:00:00,040 static analysis we just look at our code 11395 08:00:00,040 --> 08:00:02,318 or have some tool look at our code for 11396 08:00:02,318 --> 08:00:04,318 example this code here has a classic 11397 08:00:04,318 --> 08:00:06,318 re-entrancy vulnerability in our 11398 08:00:06,318 --> 08:00:08,040 withdrawal function if we run a static 11399 08:00:08,040 --> 08:00:09,600 analysis tool like slyther it'll 11400 08:00:09,600 --> 08:00:11,478 automatically detect hey you have a 11401 08:00:11,478 --> 08:00:13,280 re-entrancy vulnerability and this is 11402 08:00:13,280 --> 08:00:15,398 great for very quickly picking out very 11403 08:00:15,398 --> 08:00:16,958 specific parts of your code that are 11404 08:00:16,958 --> 08:00:19,120 known to be bad practice some popular 11405 08:00:19,120 --> 08:00:20,558 tools for static analysis are going to 11406 08:00:20,558 --> 08:00:22,240 be Slither created by the trail bits 11407 08:00:22,240 --> 08:00:24,240 team and even the solidity compiler 11408 08:00:24,240 --> 08:00:25,878 could be considered a static analysis 11409 08:00:25,878 --> 08:00:27,318 tool so now that we have a little bit of 11410 08:00:27,318 --> 08:00:28,760 a backstory on some of the popular 11411 08:00:28,760 --> 08:00:30,600 layers of testing and keeping ourselves 11412 08:00:30,600 --> 08:00:32,520 secure let's jump into formal 11413 08:00:32,520 --> 08:00:34,878 verification and symbolic execution now 11414 08:00:34,878 --> 08:00:37,638 layer four formal verification so on a 11415 08:00:37,638 --> 08:00:40,280 high level formal verification is going 11416 08:00:40,280 --> 08:00:43,240 to be the act of proving or disproving a 11417 08:00:43,240 --> 08:00:45,760 given property of the system this is 11418 08:00:45,760 --> 08:00:47,440 usually done through a mathematical 11419 08:00:47,440 --> 08:00:49,200 model of the system and the property 11420 08:00:49,200 --> 08:00:51,160 there's that word again property you're 11421 08:00:51,160 --> 08:00:53,120 seeing that almost no matter what you're 11422 08:00:53,120 --> 08:00:55,040 doing in your testing you need to 11423 08:00:55,040 --> 08:00:56,958 understand the properties of your system 11424 08:00:56,958 --> 08:00:58,600 and right there JN gave us some of the 11425 08:00:58,600 --> 08:01:00,680 keys between fuzz testing and formal 11426 08:01:00,680 --> 08:01:02,760 verification fuzz testing tries to break 11427 08:01:02,760 --> 08:01:04,798 Properties by throwing random data at 11428 08:01:04,798 --> 08:01:07,000 your system whereas formal verification 11429 08:01:07,000 --> 08:01:08,520 tries to break properties using 11430 08:01:08,520 --> 08:01:10,558 mathematical proofs and there are many 11431 08:01:10,558 --> 08:01:12,478 different ways to do formal verification 11432 08:01:12,478 --> 08:01:15,398 such as symbolic execution and Abstract 11433 08:01:15,398 --> 08:01:16,760 interpretation for this video we're 11434 08:01:16,760 --> 08:01:19,000 going to focus on symbolic execution as 11435 08:01:19,000 --> 08:01:20,318 that's one of the most popular ways 11436 08:01:20,318 --> 08:01:22,160 currently done in web 3 symbolic 11437 08:01:22,160 --> 08:01:24,398 execution is then one of the techniques 11438 08:01:24,398 --> 08:01:25,680 that you can use to do formal 11439 08:01:25,680 --> 08:01:27,958 verification symbolic execution on a 11440 08:01:27,958 --> 08:01:30,040 high level is going to be a technique 11441 08:01:30,040 --> 08:01:31,798 where you are going to have your program 11442 08:01:31,798 --> 08:01:33,638 and you're going to try to explore the 11443 08:01:33,638 --> 08:01:35,600 different path of the program for every 11444 08:01:35,600 --> 08:01:37,520 execution paths you're going to create a 11445 08:01:37,520 --> 08:01:39,440 mathematical representation additionally 11446 08:01:39,440 --> 08:01:40,840 if you want to learn more about symbolic 11447 08:01:40,840 --> 08:01:43,200 execution outside of web 3 I left a link 11448 08:01:43,200 --> 08:01:44,840 in the description to an MIT open 11449 08:01:44,840 --> 08:01:46,878 courseware video which does a great 11450 08:01:46,878 --> 08:01:49,200 rundown of symbolic execution now let's 11451 08:01:49,200 --> 08:01:51,440 look at Jin's example of using formal 11452 08:01:51,440 --> 08:01:53,680 verification and symbolic execution 11453 08:01:53,680 --> 08:01:55,200 let's say this is our function that we 11454 08:01:55,200 --> 08:01:57,160 want to do formal verification on and 11455 08:01:57,160 --> 08:01:59,160 we're going to do symbolic execution to 11456 08:01:59,160 --> 08:02:00,840 do form of verification well let's go 11457 08:02:00,840 --> 08:02:02,280 back to what formal verification 11458 08:02:02,280 --> 08:02:04,240 actually is formal verification is the 11459 08:02:04,240 --> 08:02:06,200 act of proving or disproving a given 11460 08:02:06,200 --> 08:02:08,200 property of a system using a 11461 08:02:08,200 --> 08:02:09,840 mathematical model so the first thing we 11462 08:02:09,840 --> 08:02:11,200 need to do is figure out what we want to 11463 08:02:11,200 --> 08:02:13,360 prove or disprove for our demo we should 11464 08:02:13,360 --> 08:02:15,478 say our invariant this should never 11465 08:02:15,478 --> 08:02:16,718 revert and that's what we're going to 11466 08:02:16,718 --> 08:02:18,638 try to prove or disprove now this might 11467 08:02:18,638 --> 08:02:20,478 seem like a silly example but you can 11468 08:02:20,478 --> 08:02:22,120 imagine that this was a function called 11469 08:02:22,120 --> 08:02:24,200 withdraw money and you want users to 11470 08:02:24,200 --> 08:02:26,200 always be able to get their money out 11471 08:02:26,200 --> 08:02:28,280 which would seem like a much less silly 11472 08:02:28,280 --> 08:02:30,080 example symbolic execution is going to 11473 08:02:30,080 --> 08:02:32,558 be creating a mathematical formula for 11474 08:02:32,558 --> 08:02:35,080 this function f from our code we're 11475 08:02:35,080 --> 08:02:36,920 going to convert this function to a 11476 08:02:36,920 --> 08:02:39,798 mathematical logical representation of 11477 08:02:39,798 --> 08:02:42,600 every execution path from our code once 11478 08:02:42,600 --> 08:02:44,280 we have a set of math functions we can 11479 08:02:44,280 --> 08:02:46,280 push those into a solver which will tell 11480 08:02:46,280 --> 08:02:48,558 us if a property is true or false or if 11481 08:02:48,558 --> 08:02:50,200 our invariant is true or false when we 11482 08:02:50,200 --> 08:02:51,558 talk about the different Paths of our 11483 08:02:51,558 --> 08:02:54,040 function we can imagine okay one path 11484 08:02:54,040 --> 08:02:55,600 that our function can take is going to 11485 08:02:55,600 --> 08:02:59,200 be a + 1 being returned that's going to 11486 08:02:59,200 --> 08:03:01,478 be one of our paths but we can also 11487 08:03:01,478 --> 08:03:04,798 imagine that a + 1 is actually going to 11488 08:03:04,798 --> 08:03:07,398 overflow and therefore revert if we pass 11489 08:03:07,398 --> 08:03:11,680 in the maximum U 256 A+ 1 would revert 11490 08:03:11,680 --> 08:03:14,000 so that would be a second path that our 11491 08:03:14,000 --> 08:03:15,638 function could take and a symbolic 11492 08:03:15,638 --> 08:03:17,958 execution tool would find this second 11493 08:03:17,958 --> 08:03:19,840 path for us programmatically and then of 11494 08:03:19,840 --> 08:03:21,920 course convert these to a set of 11495 08:03:21,920 --> 08:03:23,760 mathematical Expressions this set of 11496 08:03:23,760 --> 08:03:25,120 mathematical Expressions might look 11497 08:03:25,120 --> 08:03:28,000 something like this a and also not a 11498 08:03:28,000 --> 08:03:30,000 imagine if you ask somebody can this set 11499 08:03:30,000 --> 08:03:31,920 of mathematical Expressions be true at 11500 08:03:31,920 --> 08:03:33,638 the same time the answer would obviously 11501 08:03:33,638 --> 08:03:36,600 be no it is impossible for both a to be 11502 08:03:36,600 --> 08:03:39,080 true and a to be false this is what our 11503 08:03:39,080 --> 08:03:40,878 solver is going to figure out for us but 11504 08:03:40,878 --> 08:03:42,520 with much more complicated Expressions 11505 08:03:42,520 --> 08:03:44,280 than this in our example the solver is 11506 08:03:44,280 --> 08:03:47,318 known as a SAT solver or an smt solver 11507 08:03:47,318 --> 08:03:48,318 and there many different types of 11508 08:03:48,318 --> 08:03:49,958 solvers but I'm not going to go too deep 11509 08:03:49,958 --> 08:03:51,600 into that right now running our symbolic 11510 08:03:51,600 --> 08:03:53,958 execution tool we would see two paths 11511 08:03:53,958 --> 08:03:55,478 our first path going to be if we give it 11512 08:03:55,478 --> 08:03:57,878 the maximum size of a un 256 we try TR 11513 08:03:57,878 --> 08:03:59,958 add one to it the function will revert 11514 08:03:59,958 --> 08:04:02,160 solidity doesn't allow you to add one to 11515 08:04:02,160 --> 08:04:04,638 the maximum size of a un 256 the other 11516 08:04:04,638 --> 08:04:07,120 path is anything lower than the max size 11517 08:04:07,120 --> 08:04:10,000 of un 256 we'll just add plus one and 11518 08:04:10,000 --> 08:04:11,638 then return so those are going to be our 11519 08:04:11,638 --> 08:04:15,920 two paths path one a is not 2 ra 256 - 1 11520 08:04:15,920 --> 08:04:18,878 and then a returns normally and a is 2 11521 08:04:18,878 --> 08:04:21,840 ra 256 and it reverts however instead of 11522 08:04:21,840 --> 08:04:24,920 a b c not a etc our symbolic execution 11523 08:04:24,920 --> 08:04:26,360 tool will give us an output that might 11524 08:04:26,360 --> 08:04:28,040 look something like this now what you're 11525 08:04:28,040 --> 08:04:31,478 seeing on screen is an smt lib language 11526 08:04:31,478 --> 08:04:33,240 and it's a language specifically made 11527 08:04:33,240 --> 08:04:35,440 for working with these solvers to solve 11528 08:04:35,440 --> 08:04:37,398 our mathematical representations of our 11529 08:04:37,398 --> 08:04:39,000 code I'm not going to go over too deeply 11530 08:04:39,000 --> 08:04:40,680 what this is doing but you can consider 11531 08:04:40,680 --> 08:04:42,520 this just a list of Boolean Expressions 11532 08:04:42,520 --> 08:04:44,680 kind of like a and not a and if a 11533 08:04:44,680 --> 08:04:48,280 therefore B and A B and C Etc now if you 11534 08:04:48,280 --> 08:04:50,200 take this code and paste it into a tool 11535 08:04:50,200 --> 08:04:51,920 like Z3 or run it locally on your 11536 08:04:51,920 --> 08:04:54,280 machine it'll give you an output that 11537 08:04:54,280 --> 08:04:57,120 looks something like this sat and sat 11538 08:04:57,120 --> 08:04:58,760 this first that is saying they were able 11539 08:04:58,760 --> 08:05:01,638 to find an input for path 1 and they 11540 08:05:01,638 --> 08:05:03,958 were able to find an input for path two 11541 08:05:03,958 --> 08:05:06,520 they were able to satisfy the booleans 11542 08:05:06,520 --> 08:05:08,520 in those different paths since it was 11543 08:05:08,520 --> 08:05:11,600 able to satisfy an input for path two 11544 08:05:11,600 --> 08:05:14,398 and we know that path 2 reverts we know 11545 08:05:14,398 --> 08:05:17,000 that this invariant is now broken 11546 08:05:17,000 --> 08:05:18,638 because our invariant is it must never 11547 08:05:18,638 --> 08:05:20,558 revert and our mathematical 11548 08:05:20,558 --> 08:05:22,120 representation said hey I've 11549 08:05:22,120 --> 08:05:24,240 mathematically proven that there is a 11550 08:05:24,240 --> 08:05:26,878 scenario where path 2 is executed and 11551 08:05:26,878 --> 08:05:29,638 your function reverts so sat here means 11552 08:05:29,638 --> 08:05:31,638 we mathematically proved that this 11553 08:05:31,638 --> 08:05:34,160 invariant breaks now I manually created 11554 08:05:34,160 --> 08:05:36,638 this smt lib list with the help of chat 11555 08:05:36,638 --> 08:05:39,120 GPT however symbolic execution tools 11556 08:05:39,120 --> 08:05:42,000 like mantacore hvm and even the solidity 11557 08:05:42,000 --> 08:05:45,360 smt Checker can give you this smt output 11558 08:05:45,360 --> 08:05:48,440 but all those tools come with a Z3 buil 11559 08:05:48,440 --> 08:05:50,840 in so they'll even just skip this step 11560 08:05:50,840 --> 08:05:53,240 and just give you hey is my invariant 11561 08:05:53,240 --> 08:05:55,440 broken or not even the solidity compiler 11562 08:05:55,440 --> 08:05:57,280 itself can do this entire process behind 11563 08:05:57,280 --> 08:05:59,558 the me explore the paths convert the 11564 08:05:59,558 --> 08:06:01,160 paths to a set of Boolean and check to 11565 08:06:01,160 --> 08:06:02,840 see if those paths are reachable or not 11566 08:06:02,840 --> 08:06:05,000 using the so compiler we can run with 11567 08:06:05,000 --> 08:06:06,840 model Checker engine and we can look for 11568 08:06:06,840 --> 08:06:09,920 an overflow in small soul. soul and if 11569 08:06:09,920 --> 08:06:11,958 we run this you'll see the solidity 11570 08:06:11,958 --> 08:06:13,798 compiler was able to do symbolic 11571 08:06:13,798 --> 08:06:16,600 execution to find out hey if I add the 11572 08:06:16,600 --> 08:06:18,878 maximum unit 256 as an input to here 11573 08:06:18,878 --> 08:06:20,718 you're going to get an overflow and that 11574 08:06:20,718 --> 08:06:22,718 function is going to revert now 11575 08:06:22,718 --> 08:06:24,280 obviously reverts are pretty easy to 11576 08:06:24,280 --> 08:06:27,240 find but we can even add assert a does 11577 08:06:27,240 --> 08:06:29,958 not equal 1 rerun this but instead of 11578 08:06:29,958 --> 08:06:32,080 overflow look for asserts and we would 11579 08:06:32,080 --> 08:06:34,040 see that again it was able to 11580 08:06:34,040 --> 08:06:36,240 mathematically find an input to break 11581 08:06:36,240 --> 08:06:38,398 our assert or our invariant said if you 11582 08:06:38,398 --> 08:06:40,318 add zero you're going to get one asserts 11583 08:06:40,318 --> 08:06:41,520 are what you're going to use if you want 11584 08:06:41,520 --> 08:06:43,398 to use more complicated and more 11585 08:06:43,398 --> 08:06:45,840 specific symbolic execution rather than 11586 08:06:45,840 --> 08:06:47,718 just overflow or underflows tool like 11587 08:06:47,718 --> 08:06:49,878 mantor will also give you an output with 11588 08:06:49,878 --> 08:06:52,280 the revert as well as a much more hypers 11589 08:06:52,280 --> 08:06:54,360 specific list of S&T Li that it's 11590 08:06:54,360 --> 08:06:57,200 inputting into its Z3 solver so a lot of 11591 08:06:57,200 --> 08:06:59,120 stuff just happened here let's recap we 11592 08:06:59,120 --> 08:07:01,680 built some solidity we understood our 11593 08:07:01,680 --> 08:07:03,798 invariant and the next two steps 11594 08:07:03,798 --> 08:07:05,680 happened at the same time with sulk or 11595 08:07:05,680 --> 08:07:07,558 manacore we used a symbolic execution 11596 08:07:07,558 --> 08:07:09,558 tool like the built-in one to solidity 11597 08:07:09,558 --> 08:07:11,200 to create a set of bluing Expressions 11598 08:07:11,200 --> 08:07:13,000 that represent every execution path of 11599 08:07:13,000 --> 08:07:14,638 our code and then we dumped them into a 11600 08:07:14,638 --> 08:07:16,638 solver like Z3 to see if our property 11601 08:07:16,638 --> 08:07:18,600 could be broken just by running this one 11602 08:07:18,600 --> 08:07:20,478 function solidity was able to do all 11603 08:07:20,478 --> 08:07:22,840 those steps behind the scenes for us we 11604 08:07:22,840 --> 08:07:24,638 go through a full walkthrough of this 11605 08:07:24,638 --> 08:07:26,878 example with the interview with JN so be 11606 08:07:26,878 --> 08:07:28,120 sure to check that out as well if you 11607 08:07:28,120 --> 08:07:29,638 want to learn more and don't be afraid 11608 08:07:29,638 --> 08:07:31,240 if this seems a little bit complicated 11609 08:07:31,240 --> 08:07:33,120 be sure to ask questions leave comments 11610 08:07:33,120 --> 08:07:35,080 in the descriptions and leave a like as 11611 08:07:35,080 --> 08:07:36,318 well and hopefully I'm going to clear 11612 08:07:36,318 --> 08:07:38,280 this up for you sometimes the solver 11613 08:07:38,280 --> 08:07:40,000 might not be able to solve the equation 11614 08:07:40,000 --> 08:07:42,398 too like if the equation is too complex 11615 08:07:42,398 --> 08:07:43,920 you usually provide a time oute to the 11616 08:07:43,920 --> 08:07:46,080 server just because you know if you have 11617 08:07:46,080 --> 08:07:48,120 to invert hash function you know good 11618 08:07:48,120 --> 08:07:49,440 luck to do that with the server well so 11619 08:07:49,440 --> 08:07:51,040 you're saying this isn't a silver bullet 11620 08:07:51,040 --> 08:07:53,558 for auditing like any technology even 11621 08:07:53,558 --> 08:07:55,360 formal verification and symbolic 11622 08:07:55,360 --> 08:07:57,520 execution abstract interpretation the 11623 08:07:57,520 --> 08:07:59,840 these are not a one-sized fits-all 11624 08:07:59,840 --> 08:08:01,558 approach using symbolic execution can 11625 08:08:01,558 --> 08:08:03,000 run into something called The Path 11626 08:08:03,000 --> 08:08:05,200 explosion problem where there's too many 11627 08:08:05,200 --> 08:08:07,760 paths for a computer to explore in a 11628 08:08:07,760 --> 08:08:09,718 reasonable amount of time and a solver 11629 08:08:09,718 --> 08:08:11,558 would never be able to finish how 11630 08:08:11,558 --> 08:08:13,478 practical is it to to take all these 11631 08:08:13,478 --> 08:08:16,200 steps how hard is this to really do well 11632 08:08:16,200 --> 08:08:17,958 there are a couple of things to consider 11633 08:08:17,958 --> 08:08:19,318 the first one is that this specific 11634 08:08:19,318 --> 08:08:21,798 technique symbolic execution as a couple 11635 08:08:21,798 --> 08:08:24,080 of limitation as you're going to explore 11636 08:08:24,080 --> 08:08:25,958 the different path of the program there 11637 08:08:25,958 --> 08:08:27,680 is a problem which is called the path 11638 08:08:27,680 --> 08:08:29,478 explosion problem which is basically if 11639 08:08:29,478 --> 08:08:31,000 you have too many paths to explore in 11640 08:08:31,000 --> 08:08:32,840 the program it's going to take forever 11641 08:08:32,840 --> 08:08:34,638 if you have an infinite Loop this 11642 08:08:34,638 --> 08:08:36,878 technique require significant effort to 11643 08:08:36,878 --> 08:08:38,958 be used you need to understand how they 11644 08:08:38,958 --> 08:08:40,160 work and you need to understand their 11645 08:08:40,160 --> 08:08:43,160 limitation and how to help them and also 11646 08:08:43,160 --> 08:08:44,958 significant effort to be maintained at 11647 08:08:44,958 --> 08:08:47,080 the end of the day I think what really 11648 08:08:47,080 --> 08:08:49,398 matters are the properties if you want 11649 08:08:49,398 --> 08:08:51,638 to know if a bug can occur and if the 11650 08:08:51,638 --> 08:08:53,240 property can be broken you don't 11651 08:08:53,240 --> 08:08:55,440 necessarily need formal method for that 11652 08:08:55,440 --> 08:08:57,638 and you can use a further which is way 11653 08:08:57,638 --> 08:08:59,520 easier to use and provide like kind of 11654 08:08:59,520 --> 08:09:02,440 the same type of value something that is 11655 08:09:02,440 --> 08:09:04,520 sometimes difficult for people is really 11656 08:09:04,520 --> 08:09:06,398 to understand how to create this 11657 08:09:06,398 --> 08:09:08,520 property and how to create enance for 11658 08:09:08,520 --> 08:09:10,760 themselves if someone wants to you know 11659 08:09:10,760 --> 08:09:12,878 learn a bit more we have this website 11660 08:09:12,878 --> 08:09:15,478 it's called secur track.com where we go 11661 08:09:15,478 --> 08:09:18,600 a lot of guidance and best practices and 11662 08:09:18,600 --> 08:09:20,718 among other we have tutorial on how to 11663 08:09:20,718 --> 08:09:23,240 use Aina how to define inv variant how 11664 08:09:23,240 --> 08:09:25,398 to think about properties there's a high 11665 08:09:25,398 --> 08:09:27,360 skill requirement to using these tools 11666 08:09:27,360 --> 08:09:29,440 itively at the moment people are working 11667 08:09:29,440 --> 08:09:31,280 on making them easier and easier like 11668 08:09:31,280 --> 08:09:33,478 the built-in solidity smt Checker 11669 08:09:33,478 --> 08:09:36,520 however as Jon's been saying sometimes a 11670 08:09:36,520 --> 08:09:39,080 sufficiently powerful fuzzer is all you 11671 08:09:39,080 --> 08:09:41,920 need and if you combine a fuzzer with a 11672 08:09:41,920 --> 08:09:44,398 symbolic execution back end the fuzzer 11673 08:09:44,398 --> 08:09:46,840 can pick better random numbers based off 11674 08:09:46,840 --> 08:09:48,878 of the symbolic execution and maybe you 11675 08:09:48,878 --> 08:09:50,120 can find all the answers you need 11676 08:09:50,120 --> 08:09:51,600 without having to do a formal 11677 08:09:51,600 --> 08:09:54,160 verification test suite and the most 11678 08:09:54,160 --> 08:09:57,760 important piece here is that even this 11679 08:09:57,760 --> 08:10:01,240 isn't a guarantee your code is bug free 11680 08:10:01,240 --> 08:10:03,520 all it does is mathematically prove your 11681 08:10:03,520 --> 08:10:06,200 code does that one specific thing 11682 08:10:06,200 --> 08:10:09,160 correctly I'm hoping as AI takes off 11683 08:10:09,160 --> 08:10:11,478 doing a lot of this will become much 11684 08:10:11,478 --> 08:10:13,878 easier and I guess we'll have to see but 11685 08:10:13,878 --> 08:10:16,558 for now hopefully you learned at least 11686 08:10:16,558 --> 08:10:18,440 the basics of symbolic execution if 11687 08:10:18,440 --> 08:10:20,360 you'd like to learn more leave a comment 11688 08:10:20,360 --> 08:10:21,718 in the description if you want to see 11689 08:10:21,718 --> 08:10:23,638 more videos about form of verification 11690 08:10:23,638 --> 08:10:25,600 let me know but the takeaway from this 11691 08:10:25,600 --> 08:10:27,798 is that you should become a stateful 11692 08:10:27,798 --> 08:10:30,360 fuzzing wizard thanks all for watching 11693 08:10:30,360 --> 08:10:32,840 be absolutely sure to go watch the 11694 08:10:32,840 --> 08:10:34,920 interview with Troy and Joselyn links in 11695 08:10:34,920 --> 08:10:37,638 the description and we'll see you next 11696 08:10:37,638 --> 08:10:38,820 time 11697 08:10:38,820 --> 08:10:51,580 [Music] 11698 08:10:58,520 --> 08:11:00,360 now for those of you who are looking to 11699 08:11:00,360 --> 08:11:02,000 become Security Professionals smart 11700 08:11:02,000 --> 08:11:04,280 contract security Wizards there are a 11701 08:11:04,280 --> 08:11:06,240 ton of resources you should actually 11702 08:11:06,240 --> 08:11:08,760 check out I've left a link of course in 11703 08:11:08,760 --> 08:11:10,520 the GitHub repo associated with this 11704 08:11:10,520 --> 08:11:13,040 course to this comprehensive list of 11705 08:11:13,040 --> 08:11:15,080 known attack vectors and common anti 11706 08:11:15,080 --> 08:11:17,798 patterns it's got a ton of different 11707 08:11:17,798 --> 08:11:20,080 solidity attack vectors that you should 11708 08:11:20,080 --> 08:11:21,958 100% know about additionally if you want 11709 08:11:21,958 --> 08:11:24,160 to learn more on doing security there's 11710 08:11:24,160 --> 08:11:26,318 a phenomenal game by a developer named 11711 08:11:26,318 --> 08:11:29,160 tincho called damn vulnerable defi which 11712 08:11:29,160 --> 08:11:31,280 is a way to learn offensive security of 11713 08:11:31,280 --> 08:11:33,240 defi smart contracts in ethereum and 11714 08:11:33,240 --> 08:11:35,280 they've got these amazing games they you 11715 08:11:35,280 --> 08:11:38,718 can check out and play to learn security 11716 08:11:38,718 --> 08:11:40,840 ethernut is another game that is 11717 08:11:40,840 --> 08:11:43,240 absolutely phenomenal that will help you 11718 08:11:43,240 --> 08:11:46,120 to learn and understand solidity and 11719 08:11:46,120 --> 08:11:47,840 smart contract vulnerabilities for those 11720 08:11:47,840 --> 08:11:48,878 of you who are looking to become 11721 08:11:48,878 --> 08:11:50,718 Auditors definitely be sure to check out 11722 08:11:50,718 --> 08:11:53,240 solid it which is this amazing tool for 11723 08:11:53,240 --> 08:11:55,638 you to check out audit reports from the 11724 08:11:55,638 --> 08:11:57,160 top smart contract security Prof 11725 08:11:57,160 --> 08:11:59,398 Professionals in the entire industry of 11726 08:11:59,398 --> 08:12:01,638 course be sure to check us out at cyphon 11727 08:12:01,638 --> 08:12:03,000 we do smart contract security and 11728 08:12:03,000 --> 08:12:05,398 auditing as well if you are building a 11729 08:12:05,398 --> 08:12:07,040 protocol yourself definitely be sure to 11730 08:12:07,040 --> 08:12:08,878 check us out for your next project for 11731 08:12:08,878 --> 08:12:11,080 your next protocol for any and all of 11732 08:12:11,080 --> 08:12:13,398 you absolutely subscribe to web3 11733 08:12:13,398 --> 08:12:15,558 education. deev we have some amazing 11734 08:12:15,558 --> 08:12:17,600 surprises coming for you in the next 11735 08:12:17,600 --> 08:12:19,160 couple of months and I don't want you to 11736 08:12:19,160 --> 08:12:21,440 miss it there's going to be some awesome 11737 08:12:21,440 --> 08:12:23,120 opportunities for you to participate 11738 08:12:23,120 --> 08:12:26,200 learn and grow in top security so be 11739 08:12:26,200 --> 08:12:28,558 sure to put your email in here and this 11740 08:12:28,558 --> 08:12:30,280 is really just the tip of the iceberg 11741 08:12:30,280 --> 08:12:32,478 there's so much more to go when it comes 11742 08:12:32,478 --> 08:12:34,440 to security we are working on more 11743 08:12:34,440 --> 08:12:37,160 Security based education as well so stay 11744 08:12:37,160 --> 08:12:40,120 tuned for that but the prerequisite for 11745 08:12:40,120 --> 08:12:42,280 the security course is this monster 11746 08:12:42,280 --> 08:12:44,318 Foundry course anyway so you have 11747 08:12:44,318 --> 08:12:46,680 already taken the first step to becoming 11748 08:12:46,680 --> 08:12:49,920 a smart contract security badass as well 11749 08:12:49,920 --> 08:12:52,160 but all right guess 11750 08:12:52,160 --> 08:12:58,080 what that's it for the security lesson 11751 08:13:19,318 --> 08:13:22,280 congratulations I and the web3 community 11752 08:13:22,280 --> 08:13:25,280 as a whole want to congratulate you for 11753 08:13:25,280 --> 08:13:28,280 completing this absolutely 11754 08:13:28,280 --> 08:13:30,920 monstrosity of a tutorial you have done 11755 08:13:30,920 --> 08:13:34,440 an amazing job to get this far and to 11756 08:13:34,440 --> 08:13:36,600 watch me talking to you right now and if 11757 08:13:36,600 --> 08:13:38,240 you haven't finished the course go back 11758 08:13:38,240 --> 08:13:41,478 and finish it before coming here we have 11759 08:13:41,478 --> 08:13:44,280 learned so much on this journey and I 11760 08:13:44,280 --> 08:13:47,398 can say from the bottom of my soul that 11761 08:13:47,398 --> 08:13:49,240 I am so glad to have you in the web3 11762 08:13:49,240 --> 08:13:51,360 space smart contract space the 11763 08:13:51,360 --> 08:13:53,318 blockchain space the cryptocurrency 11764 08:13:53,318 --> 08:13:56,798 space we are so excited that you're here 11765 08:13:56,798 --> 08:13:58,920 I'm really looking forward to seeing you 11766 08:13:58,920 --> 08:14:01,718 in the web 3 in the blockchain community 11767 08:14:01,718 --> 08:14:04,040 now a lot of people ask well where do I 11768 08:14:04,040 --> 08:14:06,440 go now I didn't have all this new found 11769 08:14:06,440 --> 08:14:08,360 knowledge I'm armed with the 11770 08:14:08,360 --> 08:14:11,120 intelligence of the web 3 developer 11771 08:14:11,120 --> 08:14:13,440 space well I've left some links in the 11772 08:14:13,440 --> 08:14:15,840 GitHub repository to lend you to those 11773 08:14:15,840 --> 08:14:17,920 next steps but the biggest thing that 11774 08:14:17,920 --> 08:14:20,240 you can do for yourself right now is go 11775 08:14:20,240 --> 08:14:23,000 take what you've learned and apply it 11776 08:14:23,000 --> 08:14:25,440 somewhere this is going to be probably 11777 08:14:25,440 --> 08:14:27,478 the most thorough course you will ever 11778 08:14:27,478 --> 08:14:30,160 go through in this space and you can go 11779 08:14:30,160 --> 08:14:32,160 tutorial to tutorial and boot camp to 11780 08:14:32,160 --> 08:14:34,718 boot camp all you want but at some point 11781 08:14:34,718 --> 08:14:37,160 you have to make that leap and you have 11782 08:14:37,160 --> 08:14:40,280 to dive in and that's where the majority 11783 08:14:40,280 --> 08:14:42,440 of the growth is going to be anyways so 11784 08:14:42,440 --> 08:14:43,958 if you're here wondering where to go 11785 08:14:43,958 --> 08:14:46,920 next go join a hackathon go start 11786 08:14:46,920 --> 08:14:49,638 jumping onto issues on GitHub repos go 11787 08:14:49,638 --> 08:14:51,680 start applying for Grants go start 11788 08:14:51,680 --> 08:14:53,760 applying for jobs and say I took 11789 08:14:53,760 --> 08:14:55,280 Patrick's massive course here's my 11790 08:14:55,280 --> 08:14:58,160 GitHub repo work on a personal Project 11791 08:14:58,160 --> 08:15:00,600 work on somebody else's project take 11792 08:15:00,600 --> 08:15:02,478 this knowledge and apply it the 11793 08:15:02,478 --> 08:15:04,280 challenges that you'll run into and the 11794 08:15:04,280 --> 08:15:06,360 challenges that you'll face really 11795 08:15:06,360 --> 08:15:08,398 trying to do something without me 11796 08:15:08,398 --> 08:15:10,798 handholding you is where you're going to 11797 08:15:10,798 --> 08:15:13,718 learn 10 times as much is what you've 11798 08:15:13,718 --> 08:15:15,680 learned here I've walked you as deep 11799 08:15:15,680 --> 08:15:18,120 down this Rabbit Hole as I can take you 11800 08:15:18,120 --> 08:15:20,558 now it's up to you to go out and do 11801 08:15:20,558 --> 08:15:22,840 something with it so thank you everybody 11802 08:15:22,840 --> 08:15:25,200 who helped me create this course thank 11803 08:15:25,200 --> 08:15:27,718 you for taking this course and I so 11804 08:15:27,718 --> 08:15:30,000 excited to see you in the community and 11805 08:15:30,000 --> 08:15:32,920 see what you build and see what we can 11806 08:15:32,920 --> 08:15:37,600 create with this technology863890

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.