Ethereum: Web3 Contract (React) – Safeerc20 Low level called ERC20 Token (USDT)
As a developer who works in an intelligent contract or in a web3 application, it is likely to be familiar with the concept of interacting with different blockchain networks. In this article, we will explore a problem that has been reported by several users and developers.
The problem
The problem is related to the Low Safeerc20 level call mechanism on the Ethereum web3. Specifically, when a contract built in Ethereum (such as Esterscan) is used, try to call a function with an ERC20 token (like USDT) triggers a failure.
The symptoms
The symptoms are as follows:
- The smart contract works well if you try to use it with ETH (Ethereum).
- However, when the same contract is used in a Token ERC20 such as USDT, find problems.
- The error message typically indicates that the call failed, but does not provide specific details.
The solution
To solve this problem, you have some options:
- Use a different token : instead of trying to use the token erc20 (USDT), try to use another token that Ethereum admits, such as Dai or Link.
- Implement a setback mechanism : You can implement an response mechanism in your contract that tries to call the function with a Token ERC20 before resorting to ETH if necessary.
Example of code
Here is a fragment of example code in solidity (the programming language used for Ethereum’s intelligent contracts) that demonstrates how it could implement a response mechanism:
`SOLIDITY
Pragma solidity ^0.8.0;
SAFEERCALBACK CONTRACT {
CALLFUNCTION FUNCTION () Public {
// First, try to call the function with Eth
if (msg.sender.balance> 1010 * 18) {
// If you succeed, return the value of the variable
uint256 result = msg.value / 10 ** 18;
Return result;
}
// If you are not successful, try to call the function with a token erc20
address [] memory tokens = [address (0x89f8c7ce9e77FBC2A3BC6BB5DA1BF6CC4BFC94CE)];
for (uint256 i = 0; i
// Obtain the Token contract
Token contract = token (address (tokens [i]));
// Try to call the function with the token
uint256 results = contract.callfunction ();
// If you succeed, return the value of the variable
if (result> 0) {
Return result;
}
}
// If it is not successful for both methods, reverse the transaction
revert();
}
}
Struct Token {
address the public token;
}
`
Conclusion
In this article, we have explored a problem that has been informed by several users and developers related to low Safeerc20 level calls that fails with ERC20 tokens when Ethereum is used. We have also provided an example of solidity code that demonstrates how to implement an response mechanism for such cases.
By understanding the cause of the problem and implementing an adequate solution, you must be able to solve these problems and continue developing your intelligent contract or web3 application.