Тёмный

Smart Contract Tutorial - Vending Machine Smart Contract in Solidity 

Block Explorer
Подписаться 40 тыс.
Просмотров 31 тыс.
50% 1

In this smart contract tutorial video we build a basic vending machine smart contract using Solidity. Learn how to send and receive funds, work with mappings, address types and uints. Deploy and test the contract using the online Remix IDE.
Source code on GitHub:
github.com/jsp...
Remix IDE:
remix.ethereum...

Опубликовано:

 

2 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 91   
@yinanliu8980
@yinanliu8980 8 месяцев назад
Thanks for sharing! Just started learning and found your video very helpful
@rye5099
@rye5099 2 года назад
Definitely feel I am absorbing more here than most other videos I’ve seen. Appreciate your efforts and seeing your mistakes helps me see how to problem solve a solution. It’s common for us all. Thanks happy subbed
@chachanne
@chachanne 2 года назад
By far the best tutorial I ever watch on the topic. What a joy, when you understand things and can interact with the program you just built. I started my journey in smart contract development just few months ago and I was overwhelmed by all the other tutos I followed before. Yours are easy to follow along and practical. Thanks a lot for the inspiration. I will not give up on this path, thanks to you. You made me realize that it is doable.
@BlockExplorerMedia
@BlockExplorerMedia 2 года назад
Thanks so much chachanne I love to hear that. never give up - you got this!! : )
@paulo1838
@paulo1838 2 года назад
I agree best tutorial by far. I've just started my journey as a developer and found this video very easy to understand.
@saim_islam14
@saim_islam14 2 года назад
30:20 the problem wasn't for gas fee or etc, it was a problem of remix IDE, you paste the value wei but while you scroll, value becomes zero for a UI bug.
@BlockExplorerMedia
@BlockExplorerMedia 2 года назад
You might be right about that, it happened to me before where I pasted some value and somehow the field got emptied after scrolling down...thanks for the heads up!
@hasfocus
@hasfocus 2 года назад
Right, I did the example using 4 Ether (as opposed to 4 quintzillion or whaterver Wei), and it worked fine.
@Road2Mythic755
@Road2Mythic755 Год назад
I was goin to say what Knowledge Motion said. Nonetheless, good tutorial 😁
@jamqui23
@jamqui23 2 года назад
Thank you for the videos so well designed. Do not worry about the mistakes that help us understand much more than if everything is perfect.
@BlockExplorerMedia
@BlockExplorerMedia 2 года назад
hey Jamilton thanks a lot for that feedback...glad to hear that!!
@0xRizwan
@0xRizwan Год назад
Hii, I found error in contract. In line 23.... msg.value is >= amount * 2 ether..... Let say if u pay 50 ether and request 5 donuts.. it will give you 5 donuts without error but u have given 50 ethers. So to avoid this mistake....Refer below code.... Line 23---- msg.value is == amount * 2 ether........just replace > with = By the way, nice tutorial....
@samiesmilz
@samiesmilz 2 года назад
The problem was simply ( the value in the box was resetting to zero when you scrolled ) - it wasn't gas-related. You could have also selected ether and put 4 for value. Thank you for the insightful tutorials, l will check out more of your videos.
@ahmadzafar6053
@ahmadzafar6053 2 года назад
This is the best tutorial on blockchain uptill now. I just have a problem. I have developed my own erc20 token and i want my lottery contract to send and receive my token instead of ether please tell me how to do it. I need help on this
@syouske4496
@syouske4496 2 года назад
I have been hitting your videos lol.. most blockchain tutorials doesnt start from scratch but you do. Absolute gem. Thanks man
@josephdelgiorgio3997
@josephdelgiorgio3997 2 года назад
This was a great video for solidity learners. I have been all over RU-vid for practice projects and this was one of my favorites...great job!
@ifsrkr
@ifsrkr 2 года назад
Excellent video. Made learning of smart contracts so easy. Your videos spoon feed us. Keep it up. Your way of teaching is way too cool.
@BlockExplorerMedia
@BlockExplorerMedia 2 года назад
Thanks, appreciate the kind words!
@pillpollen3154
@pillpollen3154 Год назад
Super nice tutorial! Love that we so easy can play around on the Remix website without downloading, installing, debuging why the installation doesnt work etc
@MrDeiviz09
@MrDeiviz09 2 года назад
I'm starting with solidity and I still don't have the concepts very clear, so I don't know if the question is silly, but where does the ether that is sent when you buy a donut go? Because I see that the balance of the owner's account does not change
@BlockExplorerMedia
@BlockExplorerMedia 2 года назад
hey David. that's actually a great question. the ether you send in to buy a donut goes to the smart contract itself. a smart contract can receive and hold funds just like any wallet. it's the 'payable' modifier on the 'purchase' function that allows the contract to receive funds. in order to get paid, the owner of the contract would have to withdraw funds via a separate function call. The current contract doesn't implement such a function, but it would look like this: function withdraw(uint _amount) external { require(msg.sender == owner, "Only owner can call"); payable(msg.sender).transfer(_amount); }
@BlockExplorerMedia
@BlockExplorerMedia 2 года назад
or 'address(this).balance' to reference the entire balance of the smart contract
@0xRizwan
@0xRizwan Год назад
@@BlockExplorerMedia In addition to withdraw function, You also need to make the owner address payable for fund transfer.
@xx1slimeball
@xx1slimeball 2 года назад
dem donut must be rly friggin good
@126Natethegreat
@126Natethegreat 2 года назад
Tank you for this! I've been following your tutorials, you explain things very well. Hopefully I can make my own mature project some day.
@larry_the
@larry_the 2 года назад
Can't believe you left your address in this video.
@BlockExplorerMedia
@BlockExplorerMedia 2 года назад
lol none of those are current thankfully
@84chillimilli
@84chillimilli 2 месяца назад
amazing tutorial can you make videos on basics of block chain like gas fee and architecture
@Qubitmyst
@Qubitmyst 2 года назад
Should set an maximum number uint for restock , i did not try to call other contract and write 10 trillions donut available :)
@andreixperience
@andreixperience Год назад
It's incredible to see that people like you put this kind of knowledge on RU-vid for free. It's great for the space and people that are at the beginning of their web3 journey.
@paulo1838
@paulo1838 2 года назад
My transactions have been pending for 10 minutes now. is this normal ?
@artem4002
@artem4002 Год назад
What is the point of creating a mapping? One can store a simple variable instead of mapping, can't one?
@limbuscode
@limbuscode 2 года назад
Talking about the purchase function, you tested using the same address as the owner address (msg.sender == owner), how come it worked storing the correct amount of donuts? Could you explain this? I expected something like: donutBalances[0x123] -= 1; donutBalances[0x123] += 1; So the donutBalance of 0x123 would be 100, not 1 or 99.
@BlockExplorerMedia
@BlockExplorerMedia 2 года назад
Hey great question. 'msg.sender' means different things depending on the context of where it's used. In the constructor it refers to the address that deployed the function. In any other function, it refers to the address that called that function. Does that answer your question? Sorry if I didn't make it clear - I talk about this in a upcoming video that I'm currently working on. Thanks!
@Juwon04
@Juwon04 Год назад
Bro, you gave me a good night. I enjoyed the video. Thanks a lot.👌
@ketankudikyal5128
@ketankudikyal5128 2 года назад
One of the best solidity video out there ! Thanks
@BlockExplorerMedia
@BlockExplorerMedia 2 года назад
Thanks a lot Ketan!
@ayencoscolfield3312
@ayencoscolfield3312 4 месяца назад
i concur
@yoshikagarner6655
@yoshikagarner6655 2 года назад
I do have a question. So, if an attacker wanted to disrupt this vending machine smart contract, would he or she be able to do so with the parameters you currently have set in place? I've been really studying your most recent videos on smart contract security and I want to learn as much as I can on how to prevent any attacks on the smart contracts I will develop/create in the future. Thank you, in advance, for your guidance.
@BlockExplorerMedia
@BlockExplorerMedia 2 года назад
great question! i don't any security vulnerabilities in the vending machine contract. the 'restock' function is protected via access control on line 23 (require(msg.sender == owner). also the vending machine only updates it's own internal state (donutBalances) when someone makes a purchase. since there is no transfer of funds outside the contract as there is in the 'EtherBank' contract - there's no opportuity for a reentrency attack to occur.
@vincerob9660
@vincerob9660 Год назад
just subscribed to your channel - your content is good and thank you for the practical examples. However, I have no coding experience and I was just about to buy a Python tutorial, would that help? thanks again
@indranilbakshi9582
@indranilbakshi9582 Год назад
thank you so much for you i am able to complete my college project
@Rapptor22
@Rapptor22 Год назад
30:14 When purchasing donuts, you don't have to account for gas fees in msg.value. They will get added on top of that automatically. The number you have inserted into the value column just kept disappearing without you noticing.
@dejans001
@dejans001 Год назад
I do have a question. In few last lines you defined how can someone send a transaction, however, I didn't see you specified anywhere whom the receiver should be. Is that a mistake or I missed something? Also I saw balance of your acc was 95.99ether and you used it from the same account so it should have been 99.99 (because those 4 eth would come to your address anyways)
@zubairzia0
@zubairzia0 2 года назад
the architecture overview made everything come together, great work!
@moteteletsa2034
@moteteletsa2034 2 года назад
Hi, this indeed is a great, great video. I have learned quite a lot from it. However, the only missing link for me is that, I see two balances, which are the donut balances and the contract balance. You have very clearly shown how to work with the donut balances but I am still clueless as to how to work with contract balance. How can I see/view how much is the contract balance? How can I use it in the contract. For instance, my understanding is that in the real world, I also need to spend some eth to restock, and I must spend the eth I received from sales (this is excluding gas fees I pay to blockchain I guess). But otherwise thank you, this is indeed a great tutorial.
@ibrahimyildirim7290
@ibrahimyildirim7290 2 года назад
You enter the wei value on the remix ide and you make the purchase, but when I make the purchase with truffle and ganazhe, I get the error 'You must pay at least 2 ether per donut', what should I do here?
@AkosuaBiggles
@AkosuaBiggles Год назад
Your videos are explanatory I’m following all your tutorials to write smart contract code for my publication.
@UCHIHA587
@UCHIHA587 Год назад
What is the use of , address (this) and why we use this
@hasfocus
@hasfocus 2 года назад
Coming from a desktop apps background, my biggest challenge is adapting to the blockchain paradigm but the length of the tutorials (just right) and teaching style set off little light bulbs in my head. Thanks!
@javierdash1
@javierdash1 2 года назад
Hi! It is a nice tutorial, but needs an important function: withdraw. Otherwise the money will stay in the contract forever and there is no way to get the funds!
@tanhaho2681
@tanhaho2681 2 года назад
Wow! Wow! Woowow! Just wow! This is my second tutorial from you channel and you are so damn good in it I love the way you explain stuff... I owe you a thanks in person 🌍
@expattrading
@expattrading 2 года назад
My only question is, how do we put a cap on the amount of donuts to restock? you could add them infinitely.
@sumitapathak2900
@sumitapathak2900 Год назад
simple but worthy watch. I am learnign solidity rn and this helped me learn a lot.
@dylankatsch6296
@dylankatsch6296 Год назад
thx man, great video you deliver the content in such a way that makes it so clear to understand
@derzoouvdanocze260
@derzoouvdanocze260 Год назад
without a doubt the best videos for solidity. Tysm, I hope more videos are on their way
@bhok5228
@bhok5228 2 года назад
good video, could the vending machine sell an erc20 or erc 721 token?
@arghavan9325
@arghavan9325 2 года назад
Can we send a notification to the owner to check if they want to restock or not (before automatic restocking)?
@valentinrusinaru5543
@valentinrusinaru5543 2 года назад
30:00 you don't need to send more than 4 eth to buy the 2 donuts. each excess eth sent will be lost. the gas fee is separated
@algotrader6912
@algotrader6912 2 года назад
Please keeping making more. Great video 🔥🔥
@BlockExplorerMedia
@BlockExplorerMedia 2 года назад
Thanks! More on the way this week : )
@jm7990
@jm7990 Год назад
thanks again Jonathan! great job!
@osamarizk
@osamarizk 2 года назад
Thank you for this amazing vedio , but regarding purchase fucntion , should we use (send or transfer function) to transfer msg.value to the Owner ? , please advise
@0xRizwan
@0xRizwan Год назад
You can used transfer function
@tanmaybiswas007
@tanmaybiswas007 2 года назад
Just awesome, lucid to understand...👍👍👍
@kieransant2055
@kieransant2055 2 года назад
Great Job! I learned a lot, really helpful
@BlockExplorerMedia
@BlockExplorerMedia 2 года назад
That's great to hear - thanks a lot Kieran!
@newborn7850
@newborn7850 2 года назад
We need more examples like this thanku
@BlockExplorerMedia
@BlockExplorerMedia 2 года назад
Glad you liked it...more on the way - thanks!
@anuoluwapoadeleke7265
@anuoluwapoadeleke7265 2 года назад
Thanks for this video Block Explorer. I noticed a slight issue though, if the value passed is let's say 20 ether and the purchased donuts are just 8 ( which should cost 16 ether ) it still deducts 20 ether from the balance. How can we fix this?
@kokoras12000
@kokoras12000 2 года назад
in purchase function change the require(msg.value >= amount * 2 ether, "You must pay at least 2 ether per donut"); to require(msg.value == amount * 2 ether, "You must pay at least 2 ether per donut"); you change the >= to == Also, don't forget to deploy again the contract.
@anuoluwapoadeleke7265
@anuoluwapoadeleke7265 2 года назад
@@kokoras12000 thanks! Did this eventually
@adarshsinha1237
@adarshsinha1237 2 года назад
Very new to this - great tutorial! Just one basic question - when payments are sent along with a function invocation on a smart contract, does that value go to the owner? I assumed yes but just wanted to clarify
@BlockExplorerMedia
@BlockExplorerMedia 2 года назад
it goes to the smart contract being invoked, as long as that contract has a payable and external 'receive' or 'fallback' function. in order for the owner of the smart contract to get the funds they would have to implement a withdraw function in the contract.
@adarshsinha1237
@adarshsinha1237 2 года назад
@@BlockExplorerMedia Oh interesting, thanks for clarifying that!
@codebykamal2385
@codebykamal2385 2 года назад
Best tutorial Don't worry we got it.
@BlockExplorerMedia
@BlockExplorerMedia 2 года назад
Thanks codeby. More smart contract tutorials on the way soon.
@bestzenyatta3362
@bestzenyatta3362 2 года назад
thanks a lot bro for all these video
@alielorf9025
@alielorf9025 2 года назад
thanks
@thinkcrypto8643
@thinkcrypto8643 2 года назад
please do a flash loan course
@0xcuadrosweb3recruiting20
@0xcuadrosweb3recruiting20 2 года назад
Thank you for sharing!
@PhillipWilliams
@PhillipWilliams 2 года назад
This was a great video to see how to make a smart contract. One thing I wanted to learn more about was, in this contract, how to make it work with Value and Purchase QTY. So in my code I priced the Product at .0001 ether. I paid 2 Ether (msg.value) but asked to purchase 2 products. I see the vending machine deducted the 2 (down to 98) but the ether I paid was used to buy the product, but the extra is just lost. Where is it?
@cs7a30piyushjoshi2
@cs7a30piyushjoshi2 Год назад
where's my donut
@Captivatingndc
@Captivatingndc 2 года назад
First!
@python360
@python360 2 года назад
Is that a typo? : Vending Machine Smart Cuntract in Solidity C u n t r a c t ? 🤣🤣🤣🤣🤣
@BlockExplorerMedia
@BlockExplorerMedia 2 года назад
Oh my...a mistake indeed!! Fixed it - thanks for letting me know : D
@python360
@python360 2 года назад
@@BlockExplorerMedia I thought it was funny, but if I'd done similar I'd want someone to tell me! Quick question : How long did you take to get to professional level for Solidity?
@BlockExplorerMedia
@BlockExplorerMedia 2 года назад
For me it was 4 months from starting to learn Solidity to getting a job as a Solidity developer, but I had a 15 year background in software engineering before that and also I was studying it and doing projects almost every day during that time. Once you know one programming language well (Javascript for me) then learning other ones is much easier. But I am still continuing to learn a lot about Solidity and blockchain even now!
@python360
@python360 2 года назад
@@BlockExplorerMedia Thank you for that answer, I'll keep watching your videos and do project work every day in that case! I'm late to the party, but can see the potential for it to disrupt more and more industries over next few years. Really like the typos and fixes in your videos as well btw!
@BlockExplorerMedia
@BlockExplorerMedia 2 года назад
I think you're getting in at a great time. Still early and tons of opportunities...sky is the limit!
Далее
ХОККЕЙНАЯ КЛЮШКА ИЗ БУДУЩЕГО?
00:29
How to Become a Blockchain Developer
25:33
Просмотров 55 тыс.