函数在计算和gas使用中的使用

Use of functions in calculation and gas use

为什么以下不起作用?

uint transactionFee = (msg.value * 2) / 100;
uint transactionReward = (msg.value * 2) / 100;
uint receiverMoney = msg.value - limitTransactionCosts(transactionFee - transactionReward);

The contract won't allow this transaction to be executed

虽然这是有效的:

uint transactionFee = (msg.value * 2) / 100;
uint transactionReward = (msg.value * 2) / 100;
transactionFee = limitTransactionCosts(transactionFee);
transactionReward = limitTransactionCosts(transactionReward);

uint receiverMoney = msg.value - transactionFee - transactionReward;

50.002 Gas

您的代码应该按预期工作,因为没有错误。 确保您的 limitTransactionCosts 具有有效的 return 类型 (int/float)。 请添加功能代码进一步分析。

发现我的错误。无论 limitTransactionCosts 是多少,我都会发送交易费用和奖励。

我知道的代码是这样的:

// Calculate costs
    uint transactionCosts = (msg.value * 4) / 1000; 
    transactionCosts = limitTransactionCosts(transactionCosts);

    uint transactionFee = transactionCosts / 2;
    uint transactionReward = transactionCosts / 2;

    uint receiverMoney = msg.value - transactionCosts;