Posts

Showing posts from July, 2023

Yul: How to Concatenate String in Solidity Inline Assembly & How Much Gas Savings

Image
  String is often a very tricky data type to work with in Solidity as it consists and many other fields such as the identifier and the length of it. As string can go beyond 32 bytes, similarly to array, it is necessary to ensure the entire thread of strings are being stored and presented. Concatenation method is not available in Solidity inline assembly but it can be done by the manual displacement of the memory blocks. Below is how traditional concatenation works in Solidity: function concatTraditional() public pure returns(string memory) {   string memory A = "Hello ";   string memory B = "world";   return string.concat(A, B); } Without optimisation, this contract cost 196,783 gas and running it cost 1,127 gas. Below is how it is done with Yul (with explanation through commenting): function concatInAssembly() public pure returns(string memory) {   string memory A = "Hello ";   string memory B = "world";   // Strings can be define through functi...

Yul: The Gas Difference Between Regular Revert, Revert in Assembly and Revert in Assembly Using Bytes32

Image
  It is imperative to return clear error messages in all programming as it helps to segmentise where your code went wrong. In solidity, most error cases will cause a revert in the execution but there are scenarios where it will not. Especially if you are coding in Yul, a lot of error handling has to be done manually.  There are many logical methods to reduce the need for error handling if you structure your codes well. This post I am going to compare the gas difference between 3 kinds of revert in Solidity. 1. Regular Revert function RegularError() external pure {   // This is the simplest way to revert and therefore no explanation   // is needed.   revert("0x1"); } Contract (without optimisation) gas cost: 114588 2. Revert in Assembly function AssemblyError() external pure {   assembly {     mstore(0x80, shl(0xe5, 0x461bcd))     // Above is the method name for Error(String)     // shl is to further save the gas by not storing ...

Yul: Push, Pop & Return Array Object in Full Solidity Inline Assembly (Jul 2023)

Image
With cryptocurrency entering winter, the requirement of blockchain technology declines by the day. It is therefore a good time to pick up blockchain skills as more people have the time to share or they do not feel the need to hide their expertise. Solidity is the most commonly used language for smart contracts as most chains existed under EVM standard. If we go deeper into solidity, it merely compiles your codes into opcode, which is short for operation code. System only recognises machine codes which are so called the low level language. By using such language not only it allows more control over your code and it also better optimises the memory usage. The only downside is that machine codes are not as readable as the high level programming language. Solidity allows infusion of near-assembly codes. Yul is not exactly machine code but more like an intermediary between high level and low level codes. We cannot go full machine code in Solidity and Yul is the best we can do. Most operatio...