如何通过提取智能合约启用正确的用户识别?
有了这份智能合约,用户可以存入以太并撤回以太。合同应存储每个用户和存款,并随时撤回该金额。在代码中没有错误,但是不幸的是,取消提取的功能无法正常工作,实际上,以太词保留在合同中,也不会退还给所有者。
contract vaultCoin {
uint amount;
address user;
uint amountDeposit;
function vaultCoin() public {
dev = msg.sender;
}
address public dev;
address[] public users;
uint[] public totalDeposited;
mapping(address => uint) balances;
function sendToken() public payable {
balances[msg.sender] = amount;
require(msg.value > 0.001 ether);
user = msg.sender;
amountDeposit = msg.value;
users.push(user);
totalDeposited.push(amountDeposit);
}
function getUsers() public view returns (address[]) {
return users;
}
function getAmount() public view returns (uint[]) {
return totalDeposited;
}
function retireMyCoins() public payable {
require(msg.sender == user);
require(msg.value == amountDeposit);
balances[msg.sender] = 0;
msg.sender.transfer(balances[msg.sender]);
}
}
With this smart contract, users can deposit ether and withdraw ether. The contract should store each user and the deposit made, allowing the amount to be withdrawn at any time. Within the code there are no errors, but unfortunately the function with which the withdrawal is made does not work properly, in fact the ethers remain within the contract and are not returned to the owner.
contract vaultCoin {
uint amount;
address user;
uint amountDeposit;
function vaultCoin() public {
dev = msg.sender;
}
address public dev;
address[] public users;
uint[] public totalDeposited;
mapping(address => uint) balances;
function sendToken() public payable {
balances[msg.sender] = amount;
require(msg.value > 0.001 ether);
user = msg.sender;
amountDeposit = msg.value;
users.push(user);
totalDeposited.push(amountDeposit);
}
function getUsers() public view returns (address[]) {
return users;
}
function getAmount() public view returns (uint[]) {
return totalDeposited;
}
function retireMyCoins() public payable {
require(msg.sender == user);
require(msg.value == amountDeposit);
balances[msg.sender] = 0;
msg.sender.transfer(balances[msg.sender]);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码首先将余额设置为0
,然后将已经零的值传输给用户。
解决方案:
值此中间步骤重新输入攻击。
Your code sets the balance to 0 at first
And then transfers the already zeroed-out value to the user.
Solution:
This intermediate step prevents reentrancy attack.