Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions contracts/staking/IStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ interface IStaking {
uint96 amount
) external;

/// @notice Increases the authorization of the given staking provider for
/// all applications by all stake amount. Can only be called by
/// the given staking provider’s authorizer.
/// @dev Calls `authorizationIncreased` callback on each application to
/// notify the applications about authorization change.
/// See `IApplication`.
function increaseAuthorization(address stakingProvider) external;

/// @notice Requests decrease of the authorization for the given staking
/// provider on the given application by the provided amount.
/// It may not change the authorized amount immediatelly. When
Expand Down
142 changes: 99 additions & 43 deletions contracts/staking/TokenStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,6 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
uint96 amount
) external override onlyAuthorizerOf(stakingProvider) {
require(amount > 0, "Parameters must be specified");
ApplicationInfo storage applicationStruct = applicationInfo[
application
];
require(
applicationStruct.status == ApplicationStatus.APPROVED,
"Application is not approved"
);

StakingProviderInfo storage stakingProviderStruct = stakingProviders[
stakingProvider
];
Expand All @@ -369,18 +361,64 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
application
);
require(availableTValue >= amount, "Not enough stake to authorize");
authorization.authorized += amount;
emit AuthorizationIncreased(
stakingProvider,
application,
fromAmount,
authorization.authorized
);
IApplication(application).authorizationIncreased(
stakingProvider,
fromAmount,
authorization.authorized
increaseAuthorizationInternal(stakingProvider, application, amount);
}

/// @notice Increases the authorization of the given staking provider for
/// all applications by all stake amount. Can only be called by
/// the given staking provider’s authorizer.
/// @dev Calls `authorizationIncreased` callback on each application to
/// notify the applications about authorization change.
/// See `IApplication`.
function increaseAuthorization(address stakingProvider)
external
override
onlyAuthorizerOf(stakingProvider)
{
StakingProviderInfo storage stakingProviderStruct = stakingProviders[
stakingProvider
];
bool increased = false;
uint96 tStake = stakingProviderStruct.tStake;
uint256 authorizedApplications = stakingProviderStruct
.authorizedApplications
.length;

for (uint256 i = 0; i < applications.length; i++) {
address application = applications[i];
if (
applicationInfo[application].status ==
ApplicationStatus.DISABLED
) {
continue;
}

uint96 amount = getAvailableToAuthorize(
stakingProvider,
application
);

// new application
if (amount == tStake) {
stakingProviderStruct.authorizedApplications.push(application);
authorizedApplications++;
}
Comment on lines +402 to +405
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does it make sense to put this if block inside the if (amount > 0) block below? This can remove the need to make this check when amount == 0, which in any case wouldn't imply pushing a new app.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not fan of nested if(s) and for(s) so basically here I'm trying to make it easier to read in expense of one additional check. If you think it's not great approach then I can change it

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's not a strong opinion. If you prefer it this way, I'm fine with that 👍

if (amount > 0) {
increaseAuthorizationInternal(
stakingProvider,
application,
amount
);
increased = true;
}
}

require(
authorizationCeiling == 0 ||
authorizedApplications <= authorizationCeiling,
"Too many applications"
);
require(increased, "Nothing to increase");
}

/// @notice Requests decrease of all authorizations for the given staking
Expand All @@ -399,7 +437,7 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
StakingProviderInfo storage stakingProviderStruct = stakingProviders[
stakingProvider
];
uint96 deauthorizing = 0;
bool deauthorizing = false;
for (
uint256 i = 0;
i < stakingProviderStruct.authorizedApplications.length;
Expand All @@ -417,11 +455,11 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
application,
authorized
);
deauthorizing += authorized;
deauthorizing = true;
}
}

require(deauthorizing > 0, "Nothing was authorized");
require(deauthorizing, "Nothing was authorized");
}

/// @notice Called by the application at its discretion to approve the
Expand Down Expand Up @@ -611,27 +649,7 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
address application = stakingProviderStruct.authorizedApplications[
i
];
require(
applicationInfo[application].status ==
ApplicationStatus.APPROVED,
"Application is not approved"
);

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
);
increaseAuthorizationInternal(stakingProvider, application, amount);
}
}

Expand Down Expand Up @@ -991,6 +1009,44 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
}
}

/// @notice Increases the authorization of the given staking provider for
/// the given application by the given amount.
/// @dev Calls `authorizationIncreased` callback on the given application to
/// notify the application about authorization change.
/// See `IApplication`.
function increaseAuthorizationInternal(
address stakingProvider,
address application,
uint96 amount
) internal {
ApplicationInfo storage applicationStruct = applicationInfo[
application
];
require(
applicationStruct.status == ApplicationStatus.APPROVED,
"Application is not approved"
);

StakingProviderInfo storage stakingProviderStruct = stakingProviders[
stakingProvider
];
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 Delegate voting power from the stake associated to the
/// `stakingProvider` to a `delegatee` address. Caller must be the owner
/// of this stake.
Expand Down
7 changes: 7 additions & 0 deletions docs/rfc-1-staking-contract.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,13 @@ the given amount. Calls `authorizationIncreased(address stakingProvider, uint96
callback on the given application to notify the application. Can only be called
by the given provider's authorizer.

==== `increaseAuthorization(address stakingProvider) external onlyAuthorizerOf(stakingProvider)`

Increases the authorization of the given staking provider for all applications by all
stake amount. Calls `authorizationIncreased` callback on each application to notify
the applications about authorization change. Can only be called by the given staking
provider’s authorizer.

==== `requestAuthorizationDecrease(address stakingProvider, address application, uint96 amount) external onlyAuthorizerOf(stakingProvider)`

Requests decrease of the authorization for the given staking provider on the given
Expand Down
Loading