This tutorial explains how to transfer ERC20 tokens in Solidity smart contracts using two primary methods: the transfer() function for direct transfers requiring the caller to have sufficient token balance, and the transferFrom() function for delegated transfers that require the caller to first approve the spender address using the approve() function. The transfer() method takes two arguments (recipient and amount) and deducts tokens from the caller's balance, while transferFrom() takes three arguments (sender, recipient, and amount) and transfers tokens from the sender's balance to the recipient, enabling decentralized exchange functionality.
Solidity ERC20 Token Transfers: approve, transfer, transferFrom
Added:hello is this Julian and you are on it the blogs and in this video I'm going to show you how you can transfer ES 20 token in solidity yes 20 is a standard that described how we can implement token on the ethereum blockchain so with 20 token you can create your own coin on the ethereum blockchain most tokens represent some form of value and a lot of them are traded in decentralized exchanges alright that's it well introduction so let's get started transferring as 20 token in a smart contract is very common so it's very important that you understand how to do it so make sure to watch this video until the end so first in this video I'll show you how we can import and yes 20 token in our smart contract then I'll show you the transfer API of just win token and finally we'll see the transfer from API that allow you to send token on behalf of someone else by the way I created a free email course to teach you how I got my first remote blockchain job I paid $100,000 a year so if you want to learn all my secrets you just have to sign up at this URL so first we'll import and ask 20 token in our smart contract and for that I created another smart contract to represent my yes 20 tokens so this is this contract token here and actually I didn't write any code myself all I did was I inherited from this year C 20 contract here so this is key word means that this contractor can inherit from this other one yes 20 and yes e 20 where did I get it from where I got it from here from this input statement so I already explained you import statement in the remix editor and it's also possible to give an you are L that points to a location on the web so here I give directly the location of the smart contract that I want to import in my contract and you've probably heard of open Zeppelin before so this is a library for solidity smart contract and later I'll do a series just on that but for now just be aware that you can use the smart contract for creating yes 20 token so if you curious you can go and get up to this address to check the code of the s20 token of open Z plane but this video is not about building an ask 20 token is just interacting with an ask 20 token so I'm not gonna go through over the code of the s20 token so back to our token here in remix and let's go back to our initial contract and so we're gonna import our 20 token so let's import token dot saw so now we'll be able to interact with this contract ok so next let's see how we can use the transfer function of IES 20 token and for that I'll quickly have a look at the code of the es 20 token on opens Epling on github so let me scroll down and here we see the implementation of the transfer function and all we care about is the two argument that we have to provide to this function so we need to provide the recipient and the amounts when we call this function that means that we want to send this many token to this address and this will be taken of our own balance so that means that the calling smart contract must have enough token otherwise this operation will fail by the way you don't have to call the transfer function from a smart contract it can also be a regular address but in both cases you need to make sure that the calling address as enough token all right so back to our remix edit or and we're gonna create a function to call this transfer function so let's call our function transfer and inside we're gonna declare a variable of type token so that's the last 20 token to create it ourselves and we give it a name token and here we instantiate it that way and between parentheses will give the address of the token so in a previous video when I show you how we can interact with other smart contract I show you how we can create a function to import an address inside our contract so if you don't know how to do this check out this video otherwise if you already know the address of the s20 token then you can just manually give this address like this 0 X a blah blah blah actually for our example I take an address that already exists oh let me go in the deploy and run tab and let's say if I select I don't know the last address generated by remix I'm gonna copy this I'm gonna pass this and this is just for a for the example of course this will not be the address of the token smart contract here okay we terminate our statement and once we have our token pointer then we can execute method on it so to execute the transfer method then very simply use the dot notation plus the name of the function and the first argument is the recipient so for example if we wanted to send some money to the color of our function then would be MSG Center and then you specify how many token you want sense for example 100 token and that's it now you know how to transfer just win token simple right so this is the first API of SC but there is another one so that's transferred from transferred front allows to transfer tokens on behalf of someone else and this is particularly useful in the case of decentralized exchanges so let's define the term here so first we have an owner so that's the address that actually on the token then we have an operator so that's the that's another address that will be approve by the owner to spend a certain amount of token and to do this approval then we will use this approved function so for the sake of the example let's create another smart contract that will be the owner and transfer token will be the operator so let's create this contract owner and we're gonna create a transfer function as well and so from this function we're gonna call a function of transfer token that will use the transfer from function of the s20 token before we can do this we need to approve the transfer token contract so let's copy this line here inside our transfer function so that we can have a pointer to the token smart contract then we're gonna approve transfer token and actually I don't remember the order of the argument so let's have a quick look on github so here in a proof function first argument is the spender then the amount okay so first we need the address of transfer token so let's take another address as an example of course this is just an example and in reality this will not be this address and after that we specify an amount well actually I want to create an argument for that so so that it's a little bit more flexible okay and finally we're gonna call the transfer token function so for that let's create another pointer so transfer token equal transfer token and here I take the address of this other smart contract okay finally I can call a function on these contracts or transfer token dot transfer from so I haven't created yet this function but I will do it and I will specify the amount okay so now in the transfer token function let's create these functions transfer from so we recreate our pointer inside this function okay and this time we're gonna transfer from some token transfer from and I don't remember the order of the argument so let's check out the code so this is the code on github and transfer from so we have the center and we have a recipient and an amount three argument so the sender here is owner so this is the address that code transfer from in transfer token then the recipient well actually we haven't specified any recipients we need this argument so here let's add an argument we call this recipient the same thing in our transfer function below address recipient and here when we call our transfer from we also specify the recipient okay so here we first we call this function we specify recipient amount then here transfer token and transfer from recipient amount and this then we arrive here and finally we just forward these two arguments recipient and the mount you'd ask 20 token I want you to transfer this amount of token and you're gonna take it from these balance and you're gonna send it to this balance but of course for this to work you need to first to have approve the spender so if you have proved the spender but you try to spend more than what was approved then this is going to fail and finally if at some point in the future you want to revoke an approval then you can call approve again with the same address here but instead here of a man you put 0 so that's it for how you can send us 20 token in a solidity smart contract so in the next video I'll explain you what our constructor and solidity is smart contract thanks for watching and see you for the next video
Up Next

ERC20 Token Deployment on Sepolia Testnet: A Practical Guide
@BRDigitech
326 views•2025-03-16

IFS Therapy Demonstration: Complete Session with Unburdening
@IFSCA
95.9K views•2021-01-13

FastAPI vs Flask vs Django: Choosing the Right Python Web Framework
@TechWithTim
302.5K views•2024-05-26

Game of Thrones Opening Credits: A Cinematic Analysis
@gameofthrones
46.3M views•2011-04-18
Related Study Plans & Knowledge Roadmaps
Structured learning paths in General & Interdisciplinary Studies






































