v2-core icon indicating copy to clipboard operation
v2-core copied to clipboard

Store dynamic bytes to call swap function with solidity assembly

Open Neronjust2017 opened this issue 1 year ago • 4 comments

Hi, I want to save gas with solidity inline assembly when calling UniswapV2 pair's swap function. Here is my code:

pragma solidity >=0.8.0;

import "./interface/IERC20.sol";
import "./lib/SafeTransfer.sol";

contract Mycontract {

    // transfer(address,uint256)
    bytes4 internal constant TRANSFER = 0xa9059cbb;
    // swap(uint256,uint256,address,bytes)
    bytes4 internal constant PAIR_SWAP = 0x022c0d9f;

    // Contructor sets the only user
    receive() external payable {}

    fallback() external payable {
        assembly {

            let pair := shr(96, calldataload(0x00))
        
            let tokenAmountOut := calldataload(0x14)
            
            mstore(0x00, PAIR_SWAP)
            mstore(0x04, tokenAmountOut)
            mstore(0x24, 0)
            mstore(0x44, address())
            mstore(0x64, ???)  // I want the length of this is zer0, what value shoud it be?
            
            let s := call(sub(gas(), 5000), pair, 0, 0x00, ???, 0, 0) // what the length should be?
            
        }       
    }        
}

The parameters of swap function are:

function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data)

I can store uint, and address variables in memory, but I want calldata_data be zero-length bytes, so what value should be stored from 0x64? Thanks!

Neronjust2017 avatar Feb 26 '23 09:02 Neronjust2017