Skip to content

Store dynamic bytes to call swap function with solidity assemblyΒ #188

@Neronjust2017

Description

@Neronjust2017

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions