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/ - Наука та технологія
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.
Thanks so much chachanne I love to hear that. never give up - you got this!! : )
I agree best tutorial by far. I've just started my journey as a developer and found this video very easy to understand.
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.
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.
hey Jamilton thanks a lot for that feedback...glad to hear that!!
Thanks for sharing! Just started learning and found your video very helpful
I have been hitting your videos lol.. most blockchain tutorials doesnt start from scratch but you do. Absolute gem. Thanks man
vending machine is an excellent smart contract example!
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!
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.
Thanks, appreciate the kind words!
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
the architecture overview made everything come together, great work!
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
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.
without a doubt the best videos for solidity. Tysm, I hope more videos are on their way
Your videos are explanatory I’m following all your tutorials to write smart contract code for my publication.
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 🌍
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!
One of the best solidity video out there ! Thanks
Thanks a lot Ketan!
i concur
Bro, you gave me a good night. I enjoyed the video. Thanks a lot.👌
simple but worthy watch. I am learnign solidity rn and this helped me learn a lot.
thx man, great video
you deliver the content in such a way that makes it so clear to understand
thanks again Jonathan! great job!
Just awesome, lucid to understand...👍👍👍
thank you so much for you i am able to complete my college project
Please keeping making more. Great video 🔥🔥
Thanks! More on the way this week : )
Great Job!
I learned a lot, really helpful
That's great to hear - thanks a lot Kieran!
dem donut must be rly friggin good
thanks a lot bro for all these video
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.
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!
Right, I did the example using 4 Ether (as opposed to 4 quintzillion or whaterver Wei), and it worked fine.
I was goin to say what Knowledge Motion said. Nonetheless, good tutorial 😁
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.
Thank you for sharing!
Best tutorial
Don't worry we got it.
Thanks codeby. More smart contract tutorials on the way soon.
We need more examples like this thanku
Glad you liked it...more on the way - thanks!
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.
thanks
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
Can we send a notification to the owner to check if they want to restock or not (before automatic restocking)?
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.
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.
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.
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)
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
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.
@@BlockExplorerMedia Oh interesting, thanks for clarifying that!
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?
good video, could the vending machine sell an erc20 or erc 721 token?
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
You can used transfer function
My only question is, how do we put a cap on the amount of donuts to restock? you could add them infinitely.
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
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.
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!
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....
What is the use of , address (this) and why we use this
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?
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.
@@kokoras12000 thanks! Did this eventually
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
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
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);
}
or 'address(this).balance' to reference the entire balance of the smart contract
@@BlockExplorerMedia In addition to withdraw function, You also need to make the owner address payable for fund transfer.
First!
My transactions have been pending for 10 minutes now. is this normal ?
What is the point of creating a mapping? One can store a simple variable instead of mapping, can't one?
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!
Should set an maximum number uint for restock , i did not try to call other contract and write 10 trillions donut available :)
2²⁵⁶ is 'four million something' ?
please do a flash loan course
Can't believe you left your address in this video.
lol none of those are current thankfully
where's my donut
Is that a typo? : Vending Machine Smart Cuntract in Solidity
C u n t r a c t ? 🤣🤣🤣🤣🤣
Oh my...a mistake indeed!! Fixed it - thanks for letting me know : D
@@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?
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!
@@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!
I think you're getting in at a great time. Still early and tons of opportunities...sky is the limit!
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?