transfer()
Last updated
Last updated
transfer() is called on when we want to initiate a transfer of tokens from ourselves (the function caller) to another address.
Moves amount
tokens from the caller’s account to recipient
.
Returns a boolean value indicating whether the operation succeeded.
One usage of this would be in a dApp Vendor contract that sells ''yourToken'' for a price. The Vendor will transfer tokens from itself to the user when a purchase it made. For example, userA wants to buy tokens from a Vendor (service contract).
userA calls buyTokens() which is a function of the service contract (Vendor.sol), and sends some amount of ETH.
after inventory checks clear, service contract calls token contract (yourToken.sol), instructing a transfer(to = msg.sender, tokenQty)
msg.sender -> userA,
because within the scope of buyTokens(), caller was userA.
this gets passed on as a parameter object.
service contract is telling token contract that it wants to transfer tokens to userA.
Inside the scope of transfer(msg.sender, tokenQty),
_msgSender() -> service contract
because from Context.sol's perspective, _msgSender was called by service contract.
owner = service contract
transfer(owner, to, amount) -> _transfer(Vendor, userA, amt)
transfer() calls two other functions, _msgSender()
and _transfer()
.
_msgSender()
_msgSender() is inherited from Context.sol, which is an abstract contract, defined as follows:
From Context.sol
's perspective, the function caller is the service contract, so msg.sender within this scope returns Vendor's address to be captured in owner
.
From documentation:
Replacement for msg.sender. Returns the actual sender of a transaction: msg.sender for regular transactions, and the end-user for GSN relayed calls