Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

2
  • The general purpose is for the whole token-exchange deal (tokens transferred from your account into the contract, and tokens or ether transferred from the contract to your account) to take place within the same (i.e., atomic) transaction. If you first transfer tokens from your account to the contract (i.e., call a function of the token contract) and then call a function of the contract itself, then the entire exchange deal will span over several transactions, and possibly over several blocks. Commented Dec 5, 2020 at 19:10
  • It is only possible to do this exchange in a single transaction, when you transfer ether to the contract (by passing it in msg.value to the contract function). But when transferring tokens, you actually need to call a function in another contract (the token contract). So instead, you call function approve (in the token contract), and then call a function in the contract itself, which performs the token transfer (by calling tranferFrom) on your behalf. Commented Dec 5, 2020 at 19:13