Let's take a look at two ways to send Tokens in SmartContract
Transfer()
The "transfer" function is a simple and direct way to send tokens from one address to another. It is commonly used when the sender wants to transfer tokens from their own address without involving a third party.
// called by sender
transfer("0x744b08n92.......", 1 Ether);
// receiver ------- // amount
Approve & TransferFrom()
The combination of the "approve" and "transferFrom" functions is used when the sender wants to authorize a third party (spender) to transfer tokens on their behalf. This method provides more flexibility and control over the token transfers.
Steps
The sender (token owner) calls the "approve" function and specifies the spender's address and the number of tokens they are approving for transfer.
// called by Sender approve('0x744b08n92....', 1 Ether ); // sender ------ // amount
The smart contract updates the allowance for the spender, indicating the maximum amount of tokens they can transfer on behalf of the owner.
Once the approval is successful, the spender can call the "transferFrom" function, specifying the owner's address, the Receiver's address, and the number of tokens to transfer.
// called by Buyers transferFrom('0x744b08n92....','0x692baC93...', 1 Ether ); // sender ------- // receiver --- // amount
The smart contract checks if the spender is authorized to transfer the specified number of tokens on behalf of the owner. It verifies if the spender's allowance is sufficient and if the owner has enough tokens.
If the validation is successful, the tokens are transferred from the owner's address to the Receiver's address, and the spender's allowance is reduced accordingly.
Keep Learning Keep Building...
For Amazing Content Follow me on Twitter
until then see you in next tech-blog