Working with Legacy Contracts in Solidity
Recently I interacted a lot with Solidity smart contracts of strong and mature projects such as AAVE, Uniswap (v1-v2), MakerDAO, etc. These contracts were written using old solidity versions (>0.7.0).
I believe that every smart contract dev would eventually have to connect to one of such projects.
What’s more, reading their (sometimes) outdated code is very beneficial in terms of learning security and gas efficiency aspects of development. As much as we love working with cutting-edge tech at MiKi, oftentimes, you can’t escape legacy smart contracts.
I want to talk more about the tricky aspects of connection to such contracts, using as an example the smart contract of the USDT(Tether) token, which should be familiar to most crypto users.
What’s wrong with USDT?
The contract was deployed and submitted for verification in 2017 with the 0.4.17 solc version.
You may be surprised, but USDT, being one of the most popular tokens, is not ERC20 compatible.
I discovered it after trying to call the contract using IERC20(ERC20 interface) from the OpenZeppelin library. I received the op-code error.
The USDT contract has the following individual properties:
- Its transfer(), transferFrom() and approve() functions have no return value (should be bool)
- Reapprove process is burdensome (you have to send 2 transactions if you want to change the allowance amount)
- The contract has the functionality to use Blacklist (punish evil users)
- The contract has an owner (ownership not renounced)
- It has migration functionality (the token can migrate to another contract and the current one will be deprecated)
- It uses outdated syntax unfamiliar to modern devs or compilers (constant keyword for function declaration, sub() and add() functions usage) and relies on the SafeMath library
- Some of the functions’ and methods’ notes are inaccurate :)
Luckily, the contract is well-structured, pretty simple, flattened, and has its interfaces defined. Thus, it’s possible to connect to it with modern versions of solidity with minor changes.
If I missed some interesting features or development aspects, please feel free to mention them in the comments. Thank you for reading!
Written by Vladislav Lenskii for MiKi Digital
Need blockchain help? Feel free to book a call or contact us