Transfer
轉移可用數量的 ETH 時恢復錯誤
我正在嘗試編寫獎勵池分配系統,但我無法執行以下操作(按此順序)。
- A存款X金額(user_deposit)
- B存款Y金額(user_deposit)
- C 存款 Z 獎勵金額(depositRewards)
(順序在以下 2 個步驟中無關緊要)
- A 退出
- B 退出
第二次取款總是失敗並出現以下錯誤:交易已恢復到初始狀態。注意:如果您發送值並且您發送的值應該小於您目前的餘額,則呼叫的函式應該是應付的。
// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0; contract ETHPool { mapping(address => bool) internal team_members; uint256 public poolValue; uint256 internal rewards; address internal contractAdmin; uint256 public rewards_timestamp; uint256 public claimable_rewards; struct User_Transactions { uint256 amount; uint256 deposit_timestamp; } mapping(address => User_Transactions[]) internal deposits; constructor() { contractAdmin = msg.sender; } function update_team(address _memberAddress, bool _isAllowed) public { require(msg.sender == contractAdmin, "Function only callable by contract admin."); team_members[_memberAddress] = _isAllowed; } function depositRewards() public payable { require(team_members[msg.sender] == true, "Only team members are allowed to deposit rewards"); rewards += msg.value; claimable_rewards += msg.value; rewards_timestamp = block.timestamp; } function user_deposit() public payable { deposits[msg.sender].push(User_Transactions(msg.value, block.timestamp)); poolValue += msg.value; } function withdraw() public { require(deposits[msg.sender].length > 0, "You have no deposits on your behalf."); uint256 valueAddedUntilNow; uint256 totalValueAdded; for (uint i=0; i < deposits[msg.sender].length; i++) { if (deposits[msg.sender][i].deposit_timestamp < rewards_timestamp) { valueAddedUntilNow += deposits[msg.sender][i].amount; } totalValueAdded += deposits[msg.sender][i].amount; } uint256 share_of_pool = (valueAddedUntilNow * 100) / poolValue; payable(msg.sender).transfer((rewards / 100) * share_of_pool + valueAddedUntilNow); claimable_rewards -= (rewards / 100) * share_of_pool; poolValue -= valueAddedUntilNow; delete deposits[msg.sender]; } }
當你執行
A withdraws
你減少poolValue
。然後,當您根據這個新B withdraws
的執行和計算時,它將導致民意調查份額為. 這將導致應該添加到應該轉移的金額上的獎勵等於,但此時我們不再擁有所有可用的,作為 A 已經要求的一部分。share_of_pool``poolValue``100``rewards``rewards