如何在退出智能合约期间解决 'Undeclared identifier'?

How can resolve 'Undeclared identifier' during withdraw from smart contract?

我创建了一个数组,其中包含之前存入一定数量 ETH 的所有用户的每个地址和数量,然后使用 'transfer' 函数(在 : retireMyCoins() 内)检索数量和地址使用列表中合同的用户。然后用户可以提取他的 ETH。

编译合约时,在控制台的最后一个函数“retireMyCoins”中returns出现以下错误:'Undeclared identifier'.

pragma solidity ^0.4.17;

contract myVault {
                     
    address[] public users;                      
    uint[] public totalDeposited;                

    function sendToken(address user, uint amount) public payable {
        require(msg.value > 0.001 ether);      
        
        user = msg.sender;                      
        amount = msg.value;  

        users.push(msg.sender);                 
        totalDeposited.push(msg.value);        

    }

    function getUsers() public view returns (address[]) {    
        return users;
    }

    function getAmount() public view returns (uint[]) {
        return totalDeposited;
    }

    function retireMyCoins() public {
        require(user[msg.sender]); 
        require(amount[msg.value]); 
        user.transfer(this.amount); 
    }
}

您必须在合同开头创建金额作为商店变量。此外,为了使其按预期工作,您应该映射每个用户的余额,如下所示:

...
mapping( address => uint ) balances;
function sendToken(address user, uint amount) public payable {
    balances[msg.sender] = amount;
...
}

然后您就可以允许提现了:

function retireMyCoins() public {
   uint amountToWithdraw = balances[msg.sender]
   balances[msg.sender] = 0; 
   msg.sender.transfer(amountToWithdraw); 
}

记得在转账前将用户余额清零,如上。