Skip to content

Commit

Permalink
Merge pull request #155 from threshold-network/auto-increase
Browse files Browse the repository at this point in the history
Adds `autoIncrease` flag to increase authorizations in `topUp`
  • Loading branch information
cygnusv authored Nov 20, 2023
2 parents 4b569b2 + 624ead6 commit 116e669
Show file tree
Hide file tree
Showing 4 changed files with 326 additions and 5 deletions.
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;

//
//
// 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 (
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

0 comments on commit 116e669

Please sign in to comment.