Smart Contract Tutorial - Vending Machine Smart Contract in Solidity

Поділитися
Вставка
  • Опубліковано 1 січ 2022
  • 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/jspruance/block-ex...
    Remix IDE:
    remix.ethereum.org/
  • Наука та технологія

КОМЕНТАРІ • 92

  • @chachanne
    @chachanne 2 роки тому +20

    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 роки тому +2

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

    • @paulo1838
      @paulo1838 2 роки тому +1

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

  • @andreixperience
    @andreixperience Рік тому +1

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

  • @jamqui23
    @jamqui23 2 роки тому +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  2 роки тому

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

  • @yinanliu8980
    @yinanliu8980 4 місяці тому +1

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

  • @syouske4496
    @syouske4496 2 роки тому +2

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

  • @sillystuff6247
    @sillystuff6247 Рік тому

    vending machine is an excellent smart contract example!

  • @josephdelgiorgio3997
    @josephdelgiorgio3997 Рік тому +2

    This was a great video for solidity learners. I have been all over UA-cam 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.

  • @pillpollen3154
    @pillpollen3154 10 місяців тому +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

  • @zubairzia0
    @zubairzia0 2 роки тому +1

    the architecture overview made everything come together, great work!

  • @rye5099
    @rye5099 Рік тому +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

  • @126Natethegreat
    @126Natethegreat Рік тому +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.

  • @derzoouvdanocze260
    @derzoouvdanocze260 Рік тому

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

  • @AkosuaBiggles
    @AkosuaBiggles Рік тому

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

  • @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 🌍

  • @hasfocus
    @hasfocus Рік тому

    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!

  • @ketankudikyal5128
    @ketankudikyal5128 2 роки тому +3

    One of the best solidity video out there ! Thanks

  • @olatundunabdul-hamid9078
    @olatundunabdul-hamid9078 Рік тому

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

  • @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

  • @jm7990
    @jm7990 Рік тому

    thanks again Jonathan! great job!

  • @tanmaybiswas007
    @tanmaybiswas007 2 роки тому

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

  • @indranilbakshi9582
    @indranilbakshi9582 Рік тому

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

  • @algotrader6912
    @algotrader6912 2 роки тому +1

    Please keeping making more. Great video 🔥🔥

  • @kieransant2055
    @kieransant2055 2 роки тому +1

    Great Job!
    I learned a lot, really helpful

  • @xx1slimeball
    @xx1slimeball 2 роки тому +1

    dem donut must be rly friggin good

  • @bestzenyatta3362
    @bestzenyatta3362 2 роки тому

    thanks a lot bro for all these video

  • @knowledgemotion2038
    @knowledgemotion2038 2 роки тому +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  2 роки тому +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 Рік тому +1

      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 😁

  • @samiesmilz
    @samiesmilz 2 роки тому +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.

  • @0xcuadrosweb3recruiting20
    @0xcuadrosweb3recruiting20 2 роки тому

    Thank you for sharing!

  • @codebykamal2385
    @codebykamal2385 Рік тому

    Best tutorial
    Don't worry we got it.

  • @newborn7850
    @newborn7850 2 роки тому +1

    We need more examples like this thanku

  • @moteteletsa2034
    @moteteletsa2034 Рік тому

    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.

  • @alielorf9025
    @alielorf9025 Рік тому

    thanks

  • @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

  • @arghavan9325
    @arghavan9325 2 роки тому

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

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

  • @yoshikagarner6655
    @yoshikagarner6655 2 роки тому +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  2 роки тому +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 Рік тому

    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)

  • @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 роки тому +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 2 роки тому

      @@BlockExplorerMedia Oh interesting, thanks for clarifying that!

  • @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?

  • @bhok5228
    @bhok5228 2 роки тому

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

  • @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

  • @expattrading
    @expattrading Рік тому

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

  • @ahmadzafar6053
    @ahmadzafar6053 2 роки тому +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

  • @limbuscode
    @limbuscode 2 роки тому +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  2 роки тому +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!

  • @0xRizwan
    @0xRizwan Рік тому +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....

  • @UCHIHA587
    @UCHIHA587 Рік тому

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

  • @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 роки тому +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 роки тому +1

      @@kokoras12000 thanks! Did this eventually

  • @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

  • @MrDeiviz09
    @MrDeiviz09 2 роки тому +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 роки тому +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  2 роки тому +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.

  • @Captivatingndc
    @Captivatingndc 2 роки тому +1

    First!

  • @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?

  • @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!

  • @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 :)

  • @titobaba5362
    @titobaba5362 9 місяців тому

    2²⁵⁶ is 'four million something' ?

  • @thinkcrypto8643
    @thinkcrypto8643 2 роки тому

    please do a flash loan course

  • @larry_the
    @larry_the 2 роки тому +1

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

  • @cs7a30piyushjoshi2
    @cs7a30piyushjoshi2 Рік тому

    where's my donut

  • @python360
    @python360 2 роки тому

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

    • @BlockExplorerMedia
      @BlockExplorerMedia  2 роки тому +1

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

    • @python360
      @python360 2 роки тому +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  2 роки тому +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 роки тому +2

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

  • @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?