mirror of https://github.com/nucypher/nucypher.git
Fix after rebasing
parent
c3827779b1
commit
cdee46ba47
|
@ -6,7 +6,6 @@ pragma solidity ^0.8.0;
|
|||
import "aragon/interfaces/IERC900History.sol";
|
||||
import "contracts/NuCypherToken.sol";
|
||||
import "contracts/lib/Bits.sol";
|
||||
import "contracts/lib/Snapshot.sol";
|
||||
import "contracts/proxy/Upgradeable.sol";
|
||||
import "zeppelin/math/Math.sol";
|
||||
import "zeppelin/token/ERC20/SafeERC20.sol";
|
||||
|
@ -72,7 +71,6 @@ contract StakingEscrowStub is Upgradeable {
|
|||
contract StakingEscrow is Upgradeable, IERC900History {
|
||||
|
||||
using Bits for uint256;
|
||||
using Snapshot for uint128[];
|
||||
using SafeERC20 for NuCypherToken;
|
||||
|
||||
/**
|
||||
|
@ -141,12 +139,11 @@ contract StakingEscrow is Upgradeable, IERC900History {
|
|||
|
||||
uint256[] stub8; // former slot for pastDowntime
|
||||
uint256[] stub9; // former slot for subStakes
|
||||
uint128[] history; // TODO keep or remove?
|
||||
uint128[] stub10; // former slot for history
|
||||
|
||||
}
|
||||
|
||||
// indices for flags (0-4 were in use, skip it in future)
|
||||
// uint8 internal constant SNAPSHOTS_DISABLED_INDEX = 3;
|
||||
uint8 internal constant MERGED_INDEX = 5;
|
||||
|
||||
uint256 internal constant ACCEPTABLE_STAKING_ERROR = 10**15;
|
||||
|
@ -164,13 +161,13 @@ contract StakingEscrow is Upgradeable, IERC900History {
|
|||
mapping (address => address) private stub3; // former slot for stakerFromWorker
|
||||
|
||||
mapping (uint16 => uint256) private stub4; // former slot for lockedPerPeriod
|
||||
uint128[] public balanceHistory; // outdated
|
||||
uint128[] private stub5; // former slot for balanceHistory
|
||||
|
||||
address private stub5; // former slot for PolicyManager
|
||||
address private stub6; // former slot for Adjudicator
|
||||
address private stub7; // former slot for WorkLock
|
||||
address private stub6; // former slot for PolicyManager
|
||||
address private stub7; // former slot for Adjudicator
|
||||
address private stub8; // former slot for WorkLock
|
||||
|
||||
mapping (uint16 => uint256) private stub8; // last former slot for lockedPerPeriod
|
||||
mapping (uint16 => uint256) private stub9; // last former slot for lockedPerPeriod
|
||||
|
||||
/**
|
||||
* @notice Constructor sets address of token contract and parameters for staking
|
||||
|
@ -305,7 +302,6 @@ contract StakingEscrow is Upgradeable, IERC900History {
|
|||
);
|
||||
info.value -= _value;
|
||||
|
||||
addSnapshot(info, - int256(_value));
|
||||
token.safeTransfer(msg.sender, _value);
|
||||
emit Withdrawn(msg.sender, _value);
|
||||
}
|
||||
|
@ -431,16 +427,10 @@ contract StakingEscrow is Upgradeable, IERC900History {
|
|||
//------------------ ERC900 connectors ----------------------
|
||||
|
||||
function totalStakedForAt(address _owner, uint256 _blockNumber) public view override returns (uint256) {
|
||||
if (isUpgrade == UPGRADE_TRUE) {
|
||||
return stakerInfo[_owner].history.getValueAt(_blockNumber);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
function totalStakedAt(uint256 _blockNumber) public view override returns (uint256) {
|
||||
if (isUpgrade == UPGRADE_TRUE) {
|
||||
return balanceHistory.getValueAt(_blockNumber);
|
||||
}
|
||||
return token.totalSupply();
|
||||
}
|
||||
|
||||
|
|
|
@ -83,7 +83,6 @@ contract StakingInterface is BaseStakingInterface {
|
|||
event WithdrawnAsStaker(address indexed sender, uint256 value);
|
||||
event PolicyFeeWithdrawn(address indexed sender, uint256 value);
|
||||
event MinFeeRateSet(address indexed sender, uint256 value);
|
||||
event SnapshotSet(address indexed sender, bool snapshotsEnabled);
|
||||
event Bid(address indexed sender, uint256 depositedETH);
|
||||
event Claimed(address indexed sender, uint256 claimedTokens);
|
||||
event Refund(address indexed sender, uint256 refundETH);
|
||||
|
@ -141,15 +140,6 @@ contract StakingInterface is BaseStakingInterface {
|
|||
emit MinFeeRateSet(msg.sender, _minFeeRate);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Set `snapshots` parameter in the staking escrow
|
||||
* @param _enableSnapshots Value for parameter
|
||||
*/
|
||||
function setSnapshots(bool _enableSnapshots) public onlyDelegateCall {
|
||||
escrow.setSnapshots(_enableSnapshots);
|
||||
emit SnapshotSet(msg.sender, _enableSnapshots);
|
||||
}
|
||||
|
||||
/**
|
||||
* @notice Bid for tokens by transferring ETH
|
||||
*/
|
||||
|
|
|
@ -72,12 +72,8 @@ def test_staker(testerchain, token, escrow, staking_contract, staking_contract_i
|
|||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = staking_interface.functions.withdrawAsStaker(100).transact({'from': owner})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = staking_interface.functions.setSnapshots(False).transact({'from': owner})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
|
||||
# staker_withdraws = staking_contract_interface.events.WithdrawnAsStaker.createFilter(fromBlock='latest')
|
||||
snapshots_logs = staking_contract_interface.events.SnapshotSet.createFilter(fromBlock='latest')
|
||||
staker_withdraws = staking_contract_interface.events.WithdrawnAsStaker.createFilter(fromBlock='latest')
|
||||
|
||||
# Use stakers methods through the staking contract
|
||||
value = 1600
|
||||
|
@ -92,16 +88,11 @@ def test_staker(testerchain, token, escrow, staking_contract, staking_contract_i
|
|||
assert token.functions.balanceOf(escrow.address).call() == escrow_balance + value
|
||||
assert token.functions.balanceOf(staking_contract.address).call() == 9 * value
|
||||
|
||||
# Test snapshots
|
||||
tx = staking_contract_interface.functions.setSnapshots(True).transact({'from': owner})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert escrow.functions.snapshots().call()
|
||||
|
||||
events = snapshots_logs.get_all_entries()
|
||||
assert 1 == len(events)
|
||||
events = staker_withdraws.get_all_entries()
|
||||
assert len(events) == 1
|
||||
event_args = events[0]['args']
|
||||
assert owner == event_args['sender']
|
||||
assert event_args['snapshotsEnabled']
|
||||
assert event_args['sender'] == owner
|
||||
assert event_args['value'] == value
|
||||
|
||||
|
||||
def test_policy(testerchain, policy_manager, staking_contract, staking_contract_interface):
|
||||
|
|
|
@ -205,7 +205,7 @@ def test_request_merge(testerchain, threshold_staking, escrow):
|
|||
assert escrow.functions.stakerInfo(staker1).call()[OPERATOR_SLOT] == operator1
|
||||
assert threshold_staking.functions.operators(operator1).call()[0] == 0
|
||||
assert len(merge_requests_log.get_all_entries()) == 1
|
||||
_snapshots_enabled, merged = escrow.functions.getFlags(staker1).call()
|
||||
merged = escrow.functions.getFlags(staker1).call()
|
||||
assert not merged
|
||||
|
||||
# Request can be done only with the same operator
|
||||
|
@ -245,7 +245,7 @@ def test_request_merge(testerchain, threshold_staking, escrow):
|
|||
assert threshold_staking.functions.operators(operator2).call()[0] == 2 * value
|
||||
|
||||
assert len(merge_requests_log.get_all_entries()) == 2
|
||||
_snapshots_enabled, merged = escrow.functions.getFlags(staker2).call()
|
||||
merged = escrow.functions.getFlags(staker2).call()
|
||||
assert not merged
|
||||
|
||||
# Request can be done only with the same operator
|
||||
|
@ -293,7 +293,7 @@ def test_confirm_merge(testerchain, threshold_staking, escrow):
|
|||
testerchain.wait_for_receipt(tx)
|
||||
tx = escrow.functions.confirmMerge(staker).transact()
|
||||
testerchain.wait_for_receipt(tx)
|
||||
_snapshots_enabled, merged = escrow.functions.getFlags(staker).call()
|
||||
merged = escrow.functions.getFlags(staker).call()
|
||||
assert merged
|
||||
|
||||
events = merge_confirmations_log.get_all_entries()
|
||||
|
@ -312,7 +312,7 @@ def test_withdraw(testerchain, token, worklock, threshold_staking, escrow):
|
|||
withdrawal_log = escrow.events.Withdrawn.createFilter(fromBlock='latest')
|
||||
|
||||
# Deposit some tokens
|
||||
value = NU(15_000, 'NU').to_nunits()
|
||||
value = NU(ONE_HOUR, 'NU').to_nunits() # Exclude rounding error
|
||||
tx = token.functions.transfer(worklock.address, 10 * value).transact({'from': creator})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
tx = worklock.functions.depositFromWorkLock(staker, value, 0).transact()
|
||||
|
@ -355,22 +355,13 @@ def test_withdraw(testerchain, token, worklock, threshold_staking, escrow):
|
|||
testerchain.time_travel(seconds=40 * 60)
|
||||
released = value - escrow.functions.getVestedTokens(staker).call()
|
||||
|
||||
# Can't withdraw 0 tokens
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = escrow.functions.withdraw(0).transact({'from': staker})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
|
||||
# Can't withdraw more than released
|
||||
to_withdraw = released + rate # +rate because in new tx timestamp will be one second more
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = escrow.functions.withdraw(to_withdraw + 1).transact({'from': staker})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
|
||||
# Only staker can withdraw stake
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = escrow.functions.withdraw(1).transact({'from': operator})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
|
||||
to_withdraw += rate
|
||||
tx = escrow.functions.withdraw(to_withdraw).transact({'from': staker})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert escrow.functions.getAllTokens(staker).call() == value - to_withdraw
|
||||
|
@ -390,6 +381,16 @@ def test_withdraw(testerchain, token, worklock, threshold_staking, escrow):
|
|||
tx = escrow.functions.withdraw(unstaked + 1).transact({'from': staker})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
|
||||
# Can't withdraw 0 tokens
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = escrow.functions.withdraw(0).transact({'from': staker})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
|
||||
# Only staker can withdraw stake
|
||||
with pytest.raises((TransactionFailed, ValueError)):
|
||||
tx = escrow.functions.withdraw(1).transact({'from': operator})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
|
||||
tx = escrow.functions.withdraw(unstaked).transact({'from': staker})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
assert escrow.functions.getAllTokens(staker).call() == value // 2
|
||||
|
|
|
@ -199,7 +199,7 @@ def test_snapshots(testerchain, token, escrow, worklock, threshold_staking, toke
|
|||
# Finally, the first staker withdraws some tokens
|
||||
tx = threshold_staking.functions.requestMerge(staker1, staker1).transact()
|
||||
testerchain.wait_for_receipt(tx)
|
||||
tx = threshold_staking.functions.setMinStaked(staker1, balance_staker1).transact()
|
||||
tx = threshold_staking.functions.setMinStaked(staker1, initial_deposit).transact()
|
||||
testerchain.wait_for_receipt(tx)
|
||||
tx = escrow.functions.confirmMerge(staker1).transact()
|
||||
testerchain.wait_for_receipt(tx)
|
||||
|
@ -211,16 +211,8 @@ def test_snapshots(testerchain, token, escrow, worklock, threshold_staking, toke
|
|||
withdrawal = 42
|
||||
tx = escrow.functions.withdraw(withdrawal).transact({'from': staker1})
|
||||
testerchain.wait_for_receipt(tx)
|
||||
last_balance_staker1 = balance_staker1 - withdrawal
|
||||
last_balance_staker1 = initial_deposit - withdrawal
|
||||
assert last_balance_staker1 == escrow.functions.getAllTokens(staker1).call()
|
||||
|
||||
expected_staker1_balance.add_value(last_balance_staker1)
|
||||
expected_global_balance.add_value(last_balance_staker1 + deposit_staker2)
|
||||
assert expected_staker1_balance == get_staker_history_from_storage(staker1)
|
||||
assert expected_global_balance == get_global_history_from_storage()
|
||||
|
||||
now = testerchain.get_block_number()
|
||||
assert last_balance_staker1 == escrow.functions.totalStakedForAt(staker1, now).call()
|
||||
assert last_balance_staker1 + deposit_staker2 == escrow.functions.totalStakedAt(now).call()
|
||||
assert balance_staker1 == escrow.functions.totalStakedForAt(staker1, now - 1).call()
|
||||
assert balance_staker1 + deposit_staker2 == escrow.functions.totalStakedAt(now - 1).call()
|
||||
assert escrow.functions.totalStakedForAt(staker1, now).call() == 0
|
||||
assert escrow.functions.totalStakedAt(now).call() == token_economics.total_supply
|
||||
|
|
Loading…
Reference in New Issue