# transfer()

### transfer()

![](https://cdn-images-1.medium.com/max/800/1*ATY-2GJJl4_yUHIZlbQJag.png)

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).

![](/files/dEE90NqIeNP7EuSWnMS1)

* 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,&#x20;
  * 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.&#x20;
  * owner = service contract
  * *transfer(owner, to, amount) -> \_transfer(Vendor, userA, amt)*

transfer() calls two other functions, `_msgSender()` and `_transfer()`.

1. **\_msgSender()**&#x20;

\_msgSender() is inherited from Context.sol, which is an abstract contract, defined as follows:

![](/files/UC62vYVShP63lj24wI5c)

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`.&#x20;

From documentation:&#x20;

> Replacement for msg.sender. Returns the actual sender of a transaction: msg.sender for regular transactions, and the end-user for GSN relayed calls

{% hint style="info" %}
If `a` wants to send 10 ERC-20 tokens to `b`, then `a` calls `transfer(b, 10)`. If `b` now checks his balance (e.g. because `a` informs him that he has just sent him 10 tokens), he will find that it has increased by 10 tokens. If your friends want to send tokens to each other, `transfer` is the way.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://calnix.gitbook.io/eth-dev/ercs-and-eips/erc-20.sol/transfer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
