Skip to content

Commit 116e669

Browse files
authored
Merge pull request #155 from threshold-network/auto-increase
Adds `autoIncrease` flag to increase authorizations in `topUp`
2 parents 4b569b2 + 624ead6 commit 116e669

File tree

4 files changed

+326
-5
lines changed

4 files changed

+326
-5
lines changed

contracts/staking/IStaking.sol

+12
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,16 @@ interface IStaking {
158158
//
159159

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

167+
/// @notice Toggle `autoIncrease` flag. If true then the complete amount
168+
/// in top-up will be added to already authorized applications.
169+
function toggleAutoAuthorizationIncrease(address stakingProvider) external;
170+
165171
//
166172
//
167173
// Undelegating a stake (unstaking)
@@ -278,6 +284,12 @@ interface IStaking {
278284
view
279285
returns (uint256);
280286

287+
/// @notice Returns auto-increase flag.
288+
function getAutoIncreaseFlag(address stakingProvider)
289+
external
290+
view
291+
returns (bool);
292+
281293
/// @notice Returns staked amount of NU for the specified staking provider.
282294
function stakedNu(address stakingProvider) external view returns (uint256);
283295

contracts/staking/TokenStaking.sol

+65
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
6060
mapping(address => AppAuthorization) authorizations;
6161
address[] authorizedApplications;
6262
uint256 startStakingTimestamp;
63+
bool autoIncrease;
6364
}
6465

6566
struct AppAuthorization {
@@ -150,6 +151,10 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
150151
);
151152
event AuthorizationCeilingSet(uint256 ceiling);
152153
event ToppedUp(address indexed stakingProvider, uint96 amount);
154+
event AutoIncreaseToggled(
155+
address indexed stakingProvider,
156+
bool autoIncrease
157+
);
153158
event Unstaked(address indexed stakingProvider, uint96 amount);
154159
event TokensSeized(
155160
address indexed stakingProvider,
@@ -575,6 +580,8 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
575580
//
576581

577582
/// @notice Increases the amount of the stake for the given staking provider.
583+
/// If `autoIncrease` flag is true then the amount will be added for
584+
/// all authorized applications.
578585
/// @dev The sender of this transaction needs to have the amount approved to
579586
/// transfer to the staking contract.
580587
function topUp(address stakingProvider, uint96 amount) external override {
@@ -590,6 +597,54 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
590597
emit ToppedUp(stakingProvider, amount);
591598
increaseStakeCheckpoint(stakingProvider, amount);
592599
token.safeTransferFrom(msg.sender, address(this), amount);
600+
601+
if (!stakingProviderStruct.autoIncrease) {
602+
return;
603+
}
604+
605+
// increase authorization for all authorized app
606+
for (
607+
uint256 i = 0;
608+
i < stakingProviderStruct.authorizedApplications.length;
609+
i++
610+
) {
611+
address application = stakingProviderStruct.authorizedApplications[
612+
i
613+
];
614+
AppAuthorization storage authorization = stakingProviderStruct
615+
.authorizations[application];
616+
uint96 fromAmount = authorization.authorized;
617+
authorization.authorized += amount;
618+
emit AuthorizationIncreased(
619+
stakingProvider,
620+
application,
621+
fromAmount,
622+
authorization.authorized
623+
);
624+
IApplication(application).authorizationIncreased(
625+
stakingProvider,
626+
fromAmount,
627+
authorization.authorized
628+
);
629+
}
630+
}
631+
632+
/// @notice Toggle `autoIncrease` flag. If true then the complete amount
633+
/// in top-up will be added to already authorized applications.
634+
function toggleAutoAuthorizationIncrease(address stakingProvider)
635+
external
636+
override
637+
onlyAuthorizerOf(stakingProvider)
638+
{
639+
StakingProviderInfo storage stakingProviderStruct = stakingProviders[
640+
stakingProvider
641+
];
642+
stakingProviderStruct.autoIncrease = !stakingProviderStruct
643+
.autoIncrease;
644+
emit AutoIncreaseToggled(
645+
stakingProvider,
646+
stakingProviderStruct.autoIncrease
647+
);
593648
}
594649

595650
//
@@ -918,6 +973,16 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
918973
return stakingProviders[stakingProvider].startStakingTimestamp;
919974
}
920975

976+
/// @notice Returns auto-increase flag.
977+
function getAutoIncreaseFlag(address stakingProvider)
978+
external
979+
view
980+
override
981+
returns (bool)
982+
{
983+
return stakingProviders[stakingProvider].autoIncrease;
984+
}
985+
921986
/// @notice Returns staked amount of NU for the specified staking provider.
922987
function stakedNu(address stakingProvider)
923988
external

docs/rfc-1-staking-contract.adoc

+15-3
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,15 @@ protect against DoSing slashing queue. Can only be called by the governance.
282282

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

285-
Increases the amount of the stake for the given staking provider. The sender of this
286-
transaction needs to have the amount approved to transfer to the staking
287-
contract.
285+
Increases the amount of the stake for the given staking provider. If `autoIncrease`
286+
flag is true then the amount will be added for all authorized applications.
287+
The sender of this transaction needs to have the amount approved to transfer
288+
to the staking contract.
289+
290+
==== `toggleAutoAuthorizationIncrease(address stakingProvider) external`
291+
292+
Toggle auto authorization increase flag. If true then all amount in top-up
293+
will be added to already authorized applications.
288294

289295
=== Undelegating a stake (unstaking)
290296

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

383+
384+
==== `getAutoIncreaseFlag(address stakingProvider) external view returns (bool)`
385+
386+
Returns auto-increase flag. If flag is true then any topped up amount will be added to
387+
existing authorizations.
388+
377389
==== `stakedNu(address stakingProvider) external view returns (uint256)`
378390

379391
Returns staked amount of NU for the specified staking provider

0 commit comments

Comments
 (0)