Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds autoIncrease flag to increase authorizations in topUp #155

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions contracts/staking/IStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,16 @@ interface IStaking {
//

/// @notice Increases the amount of the stake for the given staking provider.
/// If `autoIncrease` flag is true then the amount will be added for
/// all authorized applications.
/// @dev The sender of this transaction needs to have the amount approved to
/// transfer to the staking contract.
function topUp(address stakingProvider, uint96 amount) external;

/// @notice Toggle `autoIncrease` flag. If true then the complete amount
/// in top-up will be added to already authorized applications.
function toggleAutoAuthorizationIncrease(address stakingProvider) external;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function toggleAutoAuthorizationIncrease(address stakingProvider) external;
function toggleAutoIncrease(address stakingProvider) external;

Copy link
Contributor Author

@vzotova vzotova Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about that, I want names explicit as possible. If you sure it's better then I'll update


//
//
// Undelegating a stake (unstaking)
Expand Down Expand Up @@ -278,6 +284,12 @@ interface IStaking {
view
returns (uint256);

/// @notice Returns auto-increase flag.
function getAutoIncreaseFlag(address stakingProvider)
external
view
returns (bool);

/// @notice Returns staked amount of NU for the specified staking provider.
function stakedNu(address stakingProvider) external view returns (uint256);

Expand Down
65 changes: 65 additions & 0 deletions contracts/staking/TokenStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
mapping(address => AppAuthorization) authorizations;
address[] authorizedApplications;
uint256 startStakingTimestamp;
bool autoIncrease;
}

struct AppAuthorization {
Expand Down Expand Up @@ -150,6 +151,10 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
);
event AuthorizationCeilingSet(uint256 ceiling);
event ToppedUp(address indexed stakingProvider, uint96 amount);
event AutoIncreaseToggled(
address indexed stakingProvider,
bool autoIncrease
);
event Unstaked(address indexed stakingProvider, uint96 amount);
event TokensSeized(
address indexed stakingProvider,
Expand Down Expand Up @@ -575,6 +580,8 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
//

/// @notice Increases the amount of the stake for the given staking provider.
/// If `autoIncrease` flag is true then the amount will be added for
/// all authorized applications.
/// @dev The sender of this transaction needs to have the amount approved to
/// transfer to the staking contract.
function topUp(address stakingProvider, uint96 amount) external override {
Expand All @@ -590,6 +597,54 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
emit ToppedUp(stakingProvider, amount);
increaseStakeCheckpoint(stakingProvider, amount);
token.safeTransferFrom(msg.sender, address(this), amount);

if (!stakingProviderStruct.autoIncrease) {
return;
}

// increase authorization for all authorized app
for (
lukasz-zimnoch marked this conversation as resolved.
Show resolved Hide resolved
uint256 i = 0;
i < stakingProviderStruct.authorizedApplications.length;
i++
) {
address application = stakingProviderStruct.authorizedApplications[
i
];
AppAuthorization storage authorization = stakingProviderStruct
.authorizations[application];
uint96 fromAmount = authorization.authorized;
authorization.authorized += amount;
emit AuthorizationIncreased(
stakingProvider,
application,
fromAmount,
authorization.authorized
);
IApplication(application).authorizationIncreased(
stakingProvider,
fromAmount,
authorization.authorized
);
}
}

/// @notice Toggle `autoIncrease` flag. If true then the complete amount
/// in top-up will be added to already authorized applications.
function toggleAutoAuthorizationIncrease(address stakingProvider)
external
override
onlyAuthorizerOf(stakingProvider)
{
StakingProviderInfo storage stakingProviderStruct = stakingProviders[
stakingProvider
];
stakingProviderStruct.autoIncrease = !stakingProviderStruct
.autoIncrease;
emit AutoIncreaseToggled(
stakingProvider,
stakingProviderStruct.autoIncrease
);
}

//
Expand Down Expand Up @@ -918,6 +973,16 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
return stakingProviders[stakingProvider].startStakingTimestamp;
}

/// @notice Returns auto-increase flag.
function getAutoIncreaseFlag(address stakingProvider)
external
view
override
returns (bool)
{
return stakingProviders[stakingProvider].autoIncrease;
}

/// @notice Returns staked amount of NU for the specified staking provider.
function stakedNu(address stakingProvider)
external
Expand Down
18 changes: 15 additions & 3 deletions docs/rfc-1-staking-contract.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,15 @@ protect against DoSing slashing queue. Can only be called by the governance.

==== `topUp(address stakingProvider, uint96 amount) external`

Increases the amount of the stake for the given staking provider. The sender of this
transaction needs to have the amount approved to transfer to the staking
contract.
Increases the amount of the stake for the given staking provider. If `autoIncrease`
flag is true then the amount will be added for all authorized applications.
The sender of this transaction needs to have the amount approved to transfer
to the staking contract.

==== `toggleAutoAuthorizationIncrease(address stakingProvider) external`

Toggle auto authorization increase flag. If true then all amount in top-up
will be added to already authorized applications.

=== Undelegating a stake (unstaking)

Expand Down Expand Up @@ -374,6 +380,12 @@ Returns start staking timestamp for T/NU stake. This value is set at most once,
and only when a stake is created with T or NU tokens. If a stake is created
from a legacy KEEP stake, this value will remain as zero.


==== `getAutoIncreaseFlag(address stakingProvider) external view returns (bool)`

Returns auto-increase flag. If flag is true then any topped up amount will be added to
existing authorizations.

==== `stakedNu(address stakingProvider) external view returns (uint256)`

Returns staked amount of NU for the specified staking provider
Expand Down
Loading