ethereum - 将代币的价格固定为美元,但购买 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 每美元)。
我在这里做错了什么?
解决方案
推荐阅读
- python - 值从函数返回为 None
- r - 错误:`data` 必须是数据框,或由`fortify()`强制的其他对象,而不是 Date 类的 S3 对象
- python - 列表的 N 个不同排列的随机样本
- python - 可以使用 Tensorflow Keras 函数式 API 模型训练 Tensorflow 变量吗?可以在功能 API 模型中使用 Tensorflow 操作吗?
- python - 从 Node.Js 应用程序运行 Python 脚本
- javascript - 从 youtube 等数据库加载视频时,如何显示加载 gif
- javascript - 如何正确地将输入元素的值呈现到 div 中?
- python - 如何使用 argparse 或 sys avrg 在命令行中设置变量值
- javascript - 如何创建构造函数?
- reactjs - 反应映射 - 映射 slug 名称时提供关键道具