繁体   English   中英

如何修复 Solidity 上的 Stack Too Deep 错误?

[英]How do I fix Stack Too Deep Error on Solidity?

在稳固性上,我不断收到“堆栈太深”的错误。 我想知道是否有人可以通过以下代码帮助我解决此问题:

function _getTValues(uint256 tAmount) private view returns (uint256, uint256, uint256, uint256, uint256, uint256) {
        uint256 tFee = calculateTaxFee(tAmount);
        uint256 tLiquidity = calculateLiquidityFee(tAmount);
        uint256 tDev = calculateDevFee(tAmount);
        uint256 tBurn = calculateBurnFee(tAmount);
        uint256 tCharity = calculateCharityFee(tAmount);
        uint256 tTransferAmount = tAmount.sub(tFee).sub(tLiquidity);
                tTransferAmount = tTransferAmount.sub(tDev);
                tTransferAmount = tTransferAmount.sub(tCharity);
                tTransferAmount = tTransferAmount.sub(tBurn);

        return (tTransferAmount, tFee, tLiquidity, tDev, tBurn, tCharity);
    }

谢谢!

您不能拥有超过 16 个局部变量 (iirc),但在这种特殊情况下,您并不真正需要它们。 Safemoon 有很多冗余代码,尤其是在getValuestransfer函数中。 I suggest you have a look at the way this is done in SafeToken - https://github.com/solidity-guru/safetoken/blob/main/safetoken.sol

_getTValues计算所有这些 t 值的唯一原因是从tTransferAmount中减去它们......但您可以只使用所有费用的总和,例如

function _getTValues(uint256 tAmount) ... {
    uint256 tFeesSum = calculateSumOfFees(tAmount);
    uint256 tTransferAmount = tAmount - tFeesSum; // no need to use SafeMath in solidity ^0.8
    return tTransferAmount;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM