首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >减小CallData尺寸

减小CallData尺寸
EN

Ethereum用户
提问于 2022-11-17 21:47:09
回答 1查看 93关注 0票数 1

我有一个实现,需要将0x参数(从API)发送到另一个函数的逻辑中使用的交换函数。

要通过一个struct传递数据,将在函数调用时使用该结构填充数据。

我发现0x的呼叫数据很大,需要大量的气体。

下面是一些代码来解释我在做什么:

代码语言:javascript
复制
//0x swap struct

struct OxSwap {
    bytes swapCallData;
    IERC20 sellToken;
    IERC20 buyToken;
    address spender;
    address payable swapTarget;
}

function _setCallData(
        bytes calldata swapCallData
    ) private {
        oxswapstruct.swapCallData = swapCallData;
    }

//Setting this struct variable costs 1+ million gas. 

//Can calldata size be reduced? 

有办法减少API调用生成的调用数据的大小吗?

我正试图尽可能地降低汽油成本,而大的0x呼叫数据大小是一个问题。

谢谢。

@Sky,谢谢你的帮助。

我不能提供完整的代码,但这是相关的部分。

代码语言:javascript
复制
//0x swap struct

struct OxSwap {
    bytes swapCallData;
    IERC20 sellToken;
    IERC20 buyToken;
    address spender;
    address payable swapTarget;
}

contract Example is Interface, Ownable {

/* Interface is where the swap is being fired. It's an external interface. Ideally, the calldata would be sent in the function parameters, but this implementation does not allow for that, given that the external contract interface has set parameters and the calldata can't be inserted in this step. So, the struct is being created to send the 0x data. */

--- State Variables   ---

    OxSwap public oxAPIswap;

--- Struct Setters ---

//calldata variable is set seperately from the rest of the struct to identify gas costs of just setting this variable 

function _setOxAPIswapStruct(
        address spender,
        address payable swapTarget,
        IERC20 sellToken,
        IERC20 buyToken
    ) private {
        oxAPIswap.spender = spender;
        oxAPIswap.swapTarget = swapTarget;
        oxAPIswap.sellToken = sellToken;
        oxAPIswap.buyToken = buyToken;
    }

    function _setOxAPICallData(
        bytes calldata swapCallData
    ) private {

        oxAPIswap.swapCallData = swapCallData;
    }

....

//Swap function 

function _swapAssets()
        private
    {
        require(
            oxAPIswap.sellToken.approve(oxAPIswap.spender, type(uint256).max)
        );
        (bool success, ) = oxAPIswap.swapTarget.call{value: msg.value}(
        oxAPIswap.swapCallData
        );
        
        require(success, "Swap failed. Low Dai balance / error.");

        payable(msg.sender).transfer(address(this).balance);
    }


//Swap data is being captured external to the contract and then sent to the contract via the struct setter. 

//I'm looking to find out how to radically reduce the call data being sent with 0x calls. For example, can the calldata be packed to eliminate 0s? 

//Sample call data:

'Swap Call Data': '0x6af479b200000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000001e8d026c4d56105000000000000000000000000000000000000000000000000000066bd1ca1749622840000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002b6b175474e89094c44da98b954eedeac495271d0f0001f4c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000000000000000000000869584cd00000000000000000000000010000000000000000000000000000000000000110000000000000000000000000000000000000000000000e27b435a91637772ec', 'Guaranteed Price': '0.000821015485425686'}
....
EN

回答 1

Ethereum用户

发布于 2022-11-22 01:19:17

您正在发布的有效负载是“仅”500个字节,我看到了10k字节以北的calldata。你确定这就是问题所在吗?您是否尝试过使用像温柔这样的应用程序来调试您的调用数据?

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/139580

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档