Smart Contract Tutorial - Vending Machine Smart Contract in Solidity

Sdílet
Vložit
  • čas přidán 28. 08. 2024
  • 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...

Komentáře • 91

  • @chachanne
    @chachanne Před 2 lety +21

    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  Před 2 lety +2

      Thanks so much chachanne I love to hear that. never give up - you got this!! : )

    • @paulo1838
      @paulo1838 Před 2 lety +1

      I agree best tutorial by far. I've just started my journey as a developer and found this video very easy to understand.

  • @yinanliu8980
    @yinanliu8980 Před 6 měsíci +1

    Thanks for sharing! Just started learning and found your video very helpful

  • @jamqui23
    @jamqui23 Před 2 lety +13

    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  Před 2 lety

      hey Jamilton thanks a lot for that feedback...glad to hear that!!

  • @andreixperience
    @andreixperience Před rokem +1

    It's incredible to see that people like you put this kind of knowledge on CZcams for free. It's great for the space and people that are at the beginning of their web3 journey.

  • @syouske4496
    @syouske4496 Před 2 lety +2

    I have been hitting your videos lol.. most blockchain tutorials doesnt start from scratch but you do. Absolute gem. Thanks man

  • @josephdelgiorgio3997
    @josephdelgiorgio3997 Před 2 lety +2

    This was a great video for solidity learners. I have been all over CZcams for practice projects and this was one of my favorites...great job!

  • @ifsrkr
    @ifsrkr Před 2 lety +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.

  • @ketankudikyal5128
    @ketankudikyal5128 Před 2 lety +3

    One of the best solidity video out there ! Thanks

  • @zubairzia0
    @zubairzia0 Před 2 lety +1

    the architecture overview made everything come together, great work!

  • @pillpollen3154
    @pillpollen3154 Před rokem +1

    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

  • @rye5099
    @rye5099 Před 2 lety +1

    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

  • @AkosuaBiggles
    @AkosuaBiggles Před rokem

    Your videos are explanatory I’m following all your tutorials to write smart contract code for my publication.

  • @hasfocus
    @hasfocus Před 2 lety

    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!

  • @Juwon04
    @Juwon04 Před rokem

    Bro, you gave me a good night. I enjoyed the video. Thanks a lot.👌

  • @sumitapathak2900
    @sumitapathak2900 Před rokem

    simple but worthy watch. I am learnign solidity rn and this helped me learn a lot.

  • @samiesmilz
    @samiesmilz Před 2 lety +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.

  • @derzoouvdanocze260
    @derzoouvdanocze260 Před rokem

    without a doubt the best videos for solidity. Tysm, I hope more videos are on their way

  • @indranilbakshi9582
    @indranilbakshi9582 Před rokem

    thank you so much for you i am able to complete my college project

  • @xx1slimeball
    @xx1slimeball Před 2 lety +1

    dem donut must be rly friggin good

  • @tanhaho2681
    @tanhaho2681 Před 2 lety

    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 🌍

  • @saim_islam14
    @saim_islam14 Před 2 lety +13

    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  Před 2 lety +1

      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 Před 2 lety +1

      Right, I did the example using 4 Ether (as opposed to 4 quintzillion or whaterver Wei), and it worked fine.

    • @Road2Mythic755
      @Road2Mythic755 Před rokem

      I was goin to say what Knowledge Motion said. Nonetheless, good tutorial 😁

  • @126Natethegreat
    @126Natethegreat Před rokem +1

    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.

  • @jm7990
    @jm7990 Před rokem

    thanks again Jonathan! great job!

  • @algotrader6912
    @algotrader6912 Před 2 lety +1

    Please keeping making more. Great video 🔥🔥

  • @dylankatsch6296
    @dylankatsch6296 Před rokem

    thx man, great video
    you deliver the content in such a way that makes it so clear to understand

  • @codebykamal2385
    @codebykamal2385 Před rokem

    Best tutorial
    Don't worry we got it.

  • @84chillimilli
    @84chillimilli Před měsícem

    amazing tutorial can you make videos on basics of block chain like gas fee and architecture

  • @tanmaybiswas007
    @tanmaybiswas007 Před 2 lety

    Just awesome, lucid to understand...👍👍👍

  • @newborn7850
    @newborn7850 Před 2 lety +1

    We need more examples like this thanku

  • @kieransant2055
    @kieransant2055 Před 2 lety +1

    Great Job!
    I learned a lot, really helpful

  • @moteteletsa2034
    @moteteletsa2034 Před 2 lety

    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.

  • @bestzenyatta3362
    @bestzenyatta3362 Před 2 lety

    thanks a lot bro for all these video

  • @0xRizwan
    @0xRizwan Před rokem +1

    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....

  • @ahmadzafar6053
    @ahmadzafar6053 Před 2 lety +1

    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

  • @0xcuadrosweb3recruiting20

    Thank you for sharing!

  • @Rapptor22
    @Rapptor22 Před rokem

    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.

  • @alielorf9025
    @alielorf9025 Před 2 lety

    thanks

  • @valentinrusinaru5543
    @valentinrusinaru5543 Před 2 lety

    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

  • @MrDeiviz09
    @MrDeiviz09 Před 2 lety +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  Před 2 lety +6

      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  Před 2 lety +2

      or 'address(this).balance' to reference the entire balance of the smart contract

    • @0xRizwan
      @0xRizwan Před rokem

      @@BlockExplorerMedia In addition to withdraw function, You also need to make the owner address payable for fund transfer.

  • @vincerob9660
    @vincerob9660 Před rokem

    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

  • @yoshikagarner6655
    @yoshikagarner6655 Před 2 lety +1

    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  Před 2 lety +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.

  • @dejans001
    @dejans001 Před rokem

    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)

  • @limbuscode
    @limbuscode Před 2 lety +1

    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  Před 2 lety +4

      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!

  • @expattrading
    @expattrading Před 2 lety

    My only question is, how do we put a cap on the amount of donuts to restock? you could add them infinitely.

  • @UCHIHA587
    @UCHIHA587 Před rokem

    What is the use of , address (this) and why we use this

  • @arghavan9325
    @arghavan9325 Před 2 lety

    Can we send a notification to the owner to check if they want to restock or not (before automatic restocking)?

  • @bhok5228
    @bhok5228 Před 2 lety

    good video, could the vending machine sell an erc20 or erc 721 token?

  • @javierdash1
    @javierdash1 Před 2 lety

    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!

  • @ibrahimyildirim7290
    @ibrahimyildirim7290 Před 2 lety

    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?

  • @adarshsinha1237
    @adarshsinha1237 Před 2 lety

    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  Před 2 lety +1

      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 Před 2 lety

      @@BlockExplorerMedia Oh interesting, thanks for clarifying that!

  • @Qubitmyst
    @Qubitmyst Před 2 lety

    Should set an maximum number uint for restock , i did not try to call other contract and write 10 trillions donut available :)

  • @osamarizk
    @osamarizk Před 2 lety

    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 Před rokem

      You can used transfer function

  • @artem4002
    @artem4002 Před rokem

    What is the point of creating a mapping? One can store a simple variable instead of mapping, can't one?

  • @larry_the
    @larry_the Před 2 lety +1

    Can't believe you left your address in this video.

  • @paulo1838
    @paulo1838 Před 2 lety

    My transactions have been pending for 10 minutes now. is this normal ?

  • @anuoluwapoadeleke7265
    @anuoluwapoadeleke7265 Před 2 lety

    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 Před 2 lety +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 Před 2 lety +1

      @@kokoras12000 thanks! Did this eventually

  • @thinkcrypto8643
    @thinkcrypto8643 Před 2 lety

    please do a flash loan course

  • @cs7a30piyushjoshi2
    @cs7a30piyushjoshi2 Před rokem

    where's my donut

  • @Captivatingndc
    @Captivatingndc Před 2 lety +1

    First!

  • @python360
    @python360 Před 2 lety

    Is that a typo? : Vending Machine Smart Cuntract in Solidity
    C u n t r a c t ? 🤣🤣🤣🤣🤣

    • @BlockExplorerMedia
      @BlockExplorerMedia  Před 2 lety +1

      Oh my...a mistake indeed!! Fixed it - thanks for letting me know : D

    • @python360
      @python360 Před 2 lety +1

      @@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  Před 2 lety +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 Před 2 lety

      @@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  Před 2 lety +2

      I think you're getting in at a great time. Still early and tons of opportunities...sky is the limit!

  • @PhillipWilliams
    @PhillipWilliams Před 2 lety

    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?