ethereum - 无法在 Remix 和 Metamask 上向用户退还多余的以太币
问题描述
构建众筹应用程序。一旦达到目标,对项目的任何以太贡献都将通过 msg.sender.transfer(excess) 或 msg.sender.send(excess) 退还。
在 Remix 和 Metamask 上进行了广泛的测试(通过 Web3 部署)。关键问题是多余的资金会从发件人的账户中全额扣除,并且不会退还给发件人。
pragma solidity ^0.5.0;
contract Funds {
uint public maximumValue = 100;
uint public currentValue = 0;
// addFunds function that takes in amount in ether
function addFunds (uint _amount) public payable {
// Converts wei (msg.value) to ether
require(msg.value / 1000000000000000000 == _amount);
// if the sum of currentValue and amount is greater than maximum value, refund the excess
if (currentValue + _amount > maximumValue){
uint excess = currentValue + _amount - maximumValue;
currentValue = maximumValue;
msg.sender.transfer(excess);
} else {
currentValue += _amount;
}
}
}
解决方案
看起来你的合约中的所有值都是以太币,所以你可能想要msg.sender.transfer(excess * 1 ether)
. 我的猜测是退款是有效的,但它会退回您没有注意到的少量 wei。
IMO,更好的解决方法是在任何地方使用 wei:
pragma solidity ^0.5.0;
contract Funds {
uint public maximumValue = 100 ether;
uint public currentValue = 0;
function addFunds() external payable {
// if the sum of currentValue and amount is greater than maximum value, refund the excess
if (currentValue + msg.value > maximumValue){
uint excess = currentValue + msg.value - maximumValue;
currentValue = maximumValue;
msg.sender.transfer(excess);
} else {
currentValue += msg.value;
}
}
}
推荐阅读
- javascript - 客户端的渲染速度比我在服务器端 nodeJS 上的数据加载速度快
- authentication - 您如何处理微服务中的授权和身份验证?
- docker - IBM Cloud 中的 Kubernetes 部署对我来说失败了
- php - 为什么 laravel 没有返回特定 ID 的标识
- r - 闪亮:actionButton 的问题(甚至没有侵入信号
- c# - 如何在 C# 中使用 Windows 个人用户证书对任意字符串进行签名
- ios - 创建一个带有前导/尾随褪色边缘的 UIView
- postgresql - 如何从 csv 复制到 Postgres 表并添加列?
- json - Alamofire 无法将 NSCFString 类型转换为字典
- java - 使用数据库创建新对象