Why does it take two transactions to send ERC20 Token?❤️BCH

0 795
Avatar for hasson
Written by
4 years ago

In fact, sending Ethereum is quite in our intuition, that is, sending money to others, and others receive it. However, sending ERC20 tokens seems not so simple. Why?

The nature of ERC-20 tokens.

ERC20 tokens only use the memory under the smart contract of ERC20 to store books that record the amount held by each address. Since the object is a smart contract, we need to review the second section mentioned. The difference between a personal account (Externally Owned Account, EOA) and a contract account (Contract Account), because smart contracts have no subjective consciousness, actions need to be issued by EOA Transaction to trigger. Therefore, when the contract address receives other tokens than ETH, it will not know. Because the change is "memory under another smart contract"!

Let's actually look at an example: suppose I want to exchange the 82 cDAIs I hold into 0.0092 ETH through the decentralized exchange Uniswap. How can I do this technically?

Prior knowledge: cDAI is the ERC20 Token that Compound returns to me after I deposit the DAI into Compound to lend. Therefore, cDAI is tracked by the memory (ledger) under the cDAI smart contract managed by Compound.


My Token Swap requirement is in the conventional logic: I send my cDAI token to Uniswap, and Uniswap sends me ETH.
But things are not so simple. Since Uniswap is decentralized, it is operated by smart contracts. Therefore, when Uniswap receives the cDAI token, it does not look like a person and can open the Metamask to see if it has been received, and then send it to me after confirming that it has been received. Therefore, special methods are needed to implement such a process.

Let ’s just open the door and see the answer. If the answer

is explained in metaphors, the method is:

1. I first go to the bank (ERC20 Token smart contract) to open the authorization, allowing the other party (third-party smart contract) to go to my account and withdraw money (ERC20 Token ) And set the "Amount that can be withdrawn".

2. Then I wrote the other party a "check with the amount" and asked the other party to take the check to the bank to pick up the money in my account.

Because the object (smart contract) is a piece of code, in this way, the smart contract can confirm the extraction of the token and perform subsequent contract logic operations.

Therefore, to return to the blockchain world, it must be carried out through the following two transaction steps:

Tx1: Approve (authorization)

I must first send a transaction to the "Compound cDAI Token smart contract" (bank),
authorizing "Uniswap's cDAI to ETH smart contract "(the counterparty)
can go to" Compound's cDAI Token smart contract "(bank) to
withdraw my" cDAI Token "(money).

Tx2: Swap Token (Exchange)

Subsequently, I sent another transaction (check)
for "exchanging cDAI to ETH" to "Uniswap's cDAI to ETH transaction pair smart contract" (the counterparty) triggered,
Let "Uniswap's cDAI to ETH trading pair smart contract" (the counterparty)
to "Compound's cDAI Token smart contract" (bank)
withdraw my 82 "cDAI Token" (money).
"Uniswap's cDAI to ETH transaction pair smart contract" (the other party) withdraws the token (money) and sends me 0.0092 ETH.

As shown in the diagram below:

Then we take everyone to take a practical operation to understand the technical logic behind it.

Tx1: Approve (authorization)

First, when we come to the Uniswap exchange, select the types of cryptocurrencies of Input and Output and enter the amount to be exchanged at Input, we can see "Unlock" next to the cDAI Token that I want to convert to ETH. "Button. This is what required me to do "Tx1: Authorization".

After clicking the "Unlock" button, the "approved" transaction will pop up for me to sign. Once signed, I approved Uniswap to be able to withdraw my cDAI permissions from Compound.

After the transaction is confirmed, we can find the transaction on Etherscan with Txn Hash. It can be found that this transaction was issued to "Compound's cDAI ERC20 Token Smart Contract".

After clicking into this transaction, you can see that the function of approve is called. I agree that the contract address of "Uniswap's ETH-cDAI trading pair" can be used to withdraw my cDAI, and the authorized withdrawal amount is "maximum".

Some people may be wondering why don't set the amount to the amount I want to exchange: 82 is just fine. The reason is to facilitate re-authorization in the future without re-authorization. Normally, when the approve function of a smart contract is called, the maximum value is set to be on by default, so that in the future, it is unnecessary to perform the approve operation again.

However, the part to pay attention to is that for "smart contracts that may have vulnerabilities or are not trusted", if you still open the permissions to the maximum value by default, you will face the risk of token theft.

After completing "Tx1: Approve Authorization", you can see that the "Unlock" button disappears and the "Swap" button can be clicked.

Tx2: Swap Token is

then able to perform the Swap Token action of converting my cDAI to ETH.

After signing this transaction through Metamask, we went to Etherscan and tracked this transaction: we can find that this transaction was sent from my wallet to "Uniswap's ETH-cDAI transaction pair smart contract".

After

clicking into this transaction, you can see that two things have been executed in this transaction: 1. Transfer 82 cDAI from "Compound's cDAI smart contract" to "Uniswap's ETH-cDAI transaction pair contract address".
2. Transfer 0.0092 ETH from "Uniswap's ETH-cDAI Trading Pair Contract Address" to me.

Then we can see 0.0092 ETH transferred to my wallet address in "Internal Txns" of "Uniswap's ETH-cDAI trading pair contract address"; and 82 cDAI in "Erc20 Token Txns" from "Compound's cDAI intelligence Contract "is transferred to" Uniswap's ETH-cDAI Trading Pair Contract Address ".

At the same time, in the "Compound's cDAI smart contract", this transaction can also see the record of 82 cDAI being transferred out.

Final thoughts

1. There is a fundamental difference between sending ETH and sending ERC20 Token to smart contracts. ETH is push-based. Simply send ETH over it. The sending of ERC20 Token is pull-based, allowing the contract to actively extract the Token.
2. You must first send a transaction to the ERC20 contract for authorization, allowing a contract address to be able to withdraw to the ERC20 smart contract to withdraw the ERC20 Token I hold.
3. Send another transaction to trigger the contract address to extract ERC20 Token and subsequent logical operations.

At this point, I believe readers have fully understood the technical logic behind sending ERC20 Tokens to smart contracts.

In the next section, we will introduce "Oracle (oracle)", a technology that acts as a bridge between the real world and the blockchain, and introduces you to the implementation of major projects such as MakerDAO and Compound. Stay tuned!

If any of the above

A. Not clear enough.
B. Writing suggestions for improvement.




2
$ 1.00
$ 1.00 from @Read.Cash
Sponsors of hasson
empty
empty
empty
Avatar for hasson
Written by
4 years ago

Comments