首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我们用1000来分摊我们的费用?

为什么我们用1000来分摊我们的费用?
EN

Ethereum用户
提问于 2019-03-06 14:01:50
回答 1查看 196关注 0票数 0
代码语言:javascript
复制
function transferToAddress(address _to, uint _value, bytes memory _data) internal returns (bool success) {
        require (_to != msg.sender && _to != address(0));
        uint fee = calculateFee(_value);
        require (_value > 0);
        require (balances[msg.sender] >= _value);
        require (balances[_to].add(_value) >= balances[_to]);
        uint sendAmount = _value.sub(fee);
        balances[msg.sender] = balances[msg.sender].sub(_value);
        balances[_to] = balances[_to].add(sendAmount);
        if (fee > 0) {
            balances[owner] = balances[owner].add(fee);
            emit Transfer(msg.sender, owner, fee,_data);
        }
        holderIsExist(_to);
        emit Transfer(msg.sender, _to, _value, _data);
        emit Transfer(msg.sender, _to, _value);
        return true;
    }


function calculateFee(uint _amount)public view returns(uint){
            uint fee = (_amount.mul(basisPointsRate)).div(1000);
            if (fee > maximumFee) {
                    fee = maximumFee;
            }
            if (fee < minimumFee) {
                fee = minimumFee;
            }
            return fee;
        }
EN

回答 1

Ethereum用户

回答已采纳

发布于 2019-03-06 14:08:56

这不是为什么除以1000。这可能是没有被提升到权力的第10。背后的原因是

您不能在智能契约中使用小数。

因此,如果你想要有x%的费用呢?

你需要使用逻辑

代码语言:javascript
复制
fee = amount * x / 100;

但是,x的最小值是1。因此,如果您希望x值低到0.1,您可以使用:

代码语言:javascript
复制
fee = amount * x / 1000;

因此,当您需要的费用是10 %,在以后的情况下,您需要使用x= 100。

这类似于为什么我们在ERC-20令牌中有小数。

建议

我个人的优势是有这样的划分是使用两个价值观的组合。

  • basisPointsRate_N (分子)
  • basisPointsRate_D (分母)

费用=金额* basisPointsRate_N / basisPointsRate_D

现在,您可以设置您想要的最小值。

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

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

复制
相关文章

相似问题

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