首页 > 解决方案 > 将代币的价格固定为美元,但购买 ETH?

问题描述

  function _getTokenAmount(uint weiAmount) internal override view returns(uint256) {
      super._getTokenAmount(weiAmount);
      int currentPrice = priceFeed.getLatestPrice();
      uint weiToEightDec = weiAmount / (10 * 10 ** 10);
      uint exchangeRatePerWei = 1000000000000000000 / uint(currentPrice)  ;
      uint usdAmount = weiToEightDec* exchangeRatePerWei;
      uint amountToGive = usdAmount * 25;
      return(amountToGive);
  }

小数让我失望。我想先得到 1 ETH 对 USD 的汇率,然后从中得到汇率。使用汇率,我将把它乘以计算美元金额的 ETH 数量,然后用它来给出正确数量的代币(1 美元 @ 0.04 = 25 每美元)。

我在这里做错了什么?

标签: ethereumsoliditysmartcontractserc20chainlink

解决方案


推荐阅读