【问题标题】:Solidity - Stack Too DeepSolidity - 堆栈太深
【发布时间】:2021-08-14 10:55:29
【问题描述】:

我是编码新手,在 Solidity 中找不到解决 Stack too deep 错误的方法

function _getValues(uint256 tAmount) private view returns (uint256, uint256, uint256, uint256, uint256, uint256, uint256, uint256) {
    (uint256 tTransferAmount, uint256 tFee, uint256 tLiquidity, uint256 tCharity, uint256 tMarketing) = _getTValues(tAmount);
    (uint256 rAmount, uint256 rTransferAmount, uint256 rFee) = _getRValues(tAmount, tFee, tLiquidity, tCharity, tMarketing, _getRate());
    return (rAmount, rTransferAmount, rFee, tTransferAmount, tFee, tLiquidity, tCharity, tMarketing);
}

添加 tMarketing 后,我在第 3 行收到错误消息。

我尝试使用此处显示的逻辑解决我的问题:https://soliditydeveloper.com/stacktoodeep

我只是不知道如何正确应用它......

我需要帮助。谢谢大家

这是 _getValues() 函数的依赖项 - _getTValues() 和 _getRValues()(以及它们的依赖项)

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.4;

合约 STACKTOODEEP {

uint256 public _taxFee = 3; // 3% Redistributed to all holders
uint256 private _previousTaxFee = _taxFee;

uint256 public _liquidityFee = 3; // 3% Sent to PancakSwap Liquidity Pool
uint256 private _previousLiquidityFee = _liquidityFee;

uint256 public _charityFee = 2; // 2% Sent to Charity Wallet
uint256 private _previousCharityFee = _charityFee;

uint256 public _marketingFee = 2; // 2% Sent to Marketing Wallet
uint256 private _previousMarketingFee = _marketingFee;

function calculateTaxFee(uint256 _amount) private view returns (uint256) {
    return _amount.mul(_taxFee).div(
        10**2
    );
}

function calculateLiquidityFee(uint256 _amount) private view returns (uint256) {
    return _amount.mul(_liquidityFee).div(
        10**2
    );
}

function calculateCharityFee(uint256 _amount) private view returns (uint256) {
    return _amount.mul(_charityFee).div(
        10**2
    );
}

function calculateMarketingFee(uint256 _amount) private view returns (uint256) {
    return _amount.mul(_marketingFee).div(
        10**2
    );
}

function _getValues(uint256 tAmount) private view returns (uint256, uint256, uint256, uint256, uint256, uint256, uint256, uint256) {
    (uint256 tTransferAmount, uint256 tFee, uint256 tLiquidity, uint256 tCharity, uint256 tMarketing) = _getTValues(tAmount);
    (uint256 rAmount, uint256 rTransferAmount, uint256 rFee) = _getRValues(tAmount, tFee, tLiquidity, tCharity, tMarketing, _getRate());
    return (rAmount, rTransferAmount, rFee, tTransferAmount, tFee, tLiquidity, tCharity, tMarketing);
}

function _getTValues(uint256 tAmount) private view returns (uint256, uint256, uint256, uint256, uint256) {
    uint256 tFee = calculateTaxFee(tAmount);
    uint256 tLiquidity = calculateLiquidityFee(tAmount);
    uint256 tCharity = calculateCharityFee(tAmount);
    uint256 tMarketing = calculateMarketingFee(tAmount);
    uint256 tTransferAmount = tAmount.sub(tFee).sub(tLiquidity);
    return (tTransferAmount, tFee, tLiquidity, tCharity, tMarketing);
}

function _getRValues(uint256 tAmount, uint256 tFee, uint256 tLiquidity, uint256 tCharity, uint256 tMarketing, uint256 currentRate) private pure returns (uint256, uint256, uint256) {
    uint256 rAmount = tAmount.mul(currentRate);
    uint256 rFee = tFee.mul(currentRate);
    uint256 rLiquidity = tLiquidity.mul(currentRate);
    uint256 rCharity = tCharity.mul(currentRate);
    uint256 rMarketing = tMarketing.mul(currentRate);
    uint256 rTransferAmount = rAmount.sub(rFee).sub(rLiquidity).sub(rCharity).sub(rMarketing);
    return (rAmount, rTransferAmount, rFee);
}

}

【问题讨论】:

  • 请编辑您的问题并分享 _getValues() 函数的依赖项 - _getTValues()_getRValues()(及其依赖项),以便重现(和解决)您的问题。
  • 已编辑以添加 _getValues() 函数的依赖项 - _getTValues() 和 _getRValues()(及其依赖项)@PetrHejda
  • 看来从_getTValues函数调用的这一行uint256 tMarketing = calculateMarketingFee(tAmount);失败了。是否也可以将其作为依赖项包含在内?
  • 嗨,对不起,我说过我是编码新手,所以我不太擅长分享我的代码。我已经编辑了我的问题。我希望这次你有你需要的一切。非常感谢你帮助我,我很感激@PetrHejda

标签: solidity


【解决方案1】:
(uint256 rAmount, uint256 rTransferAmount, uint256 rFee) = _getRValues(tAmount, tFee, tLiquidity, tCharity, tMarketing, _getRate());

此行会产生错误(请参阅下面的消息),因为您的代码中没有定义 _getRate() 函数。

DeclarationError:未声明的标识符。 --> Test.sol:45:125: | 45 | (uint256 rAmount, uint256 rTransferAmount, uint256 rFee) = _getRValues(tAmount, tFee, tLiquidity, tCharity, tMarketing, _getRate()); | ^^^^^^^^

从上下文来看,您似乎是从现有来源复制部分代码。确保您也包含_getRate() 函数。

出于测试目的,我创建了一个像这样的虚拟函数:

function _getRate() private view returns (uint256) {
    return 1;
}

那么您将遇到 SafeMath 库的未定义函数的问题,特别是 mul()(乘法)、div()(除法)和 sub()(减法)。

由于您使用的是 Solidity 0.8,因此不需要 SafeMath(但在旧版本中您会需要它),您可以将函数替换为标准算术运算。

示例 1:

_amount.mul(_taxFee).div(
    10**2
);

可以替换为

(_amount * _taxFee) / (10**2);

示例 2:

tAmount.sub(tFee).sub(tLiquidity);

可以替换为

tAmount - tFee - tLiquidity;

【讨论】:

  • 您好,感谢您的回复。我的问题中没有包含 _getRate() 函数,但我将它与 SafeMath 一起使用。我唯一的错误是:CompilerError:堆栈太深,尝试删除局部变量。 --> 合约/X.sol:828:80: |第828章(uint256 rAmount, uint256 rTransferAmount, uint256 rFee) = _getRValues(tAmount, tFee, tLiquidity, tCharity, tMarketing, _getRate()); | ^^^^^^^
  • 我必须编辑:函数 _getValues(uint256 tAmount) 私有视图返回 (uint256, uint256, uint256, uint256, uint256, uint256, uint256, uint256) { (uint256 tTransferAmount, uint256 tFee, uint256 uint256 tCharity, uint256 tMarketing) = _getTValues(tAmount); (uint256 rAmount, uint256 rTransferAmount, uint256 rFee) = _getRValues(tAmount, tFee, tLiquidity, tCharity, tMarketing, _getRate());返回(rAmount、rTransferAmount、rFee、tTransferAmount、tFee、tLiquidity、tCharity、tMarketing); } 使用soliditydeveloper.com/stacktoodeep 我只是不知道如何
  • @cquenneville 我现在不知道如何解决“堆栈太深”错误。但是请在下次的问题中更清楚地描述您的问题。例如,通过粘贴确切的错误消息和任何人都可以复制粘贴到他们的 IDE 并获得相同错误消息的代码。它使故障排除变得更容易。
  • 感谢您的评论我下次会做得更好
猜你喜欢
  • 2021-09-16
  • 2012-03-04
  • 2013-10-05
  • 2011-11-17
  • 2012-07-24
  • 2016-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多