Skip to content

Commit

Permalink
TokenStaking: restricts topUp auto increase to only approved apps (…
Browse files Browse the repository at this point in the history
…not paused, not disabled)
  • Loading branch information
vzotova committed Nov 29, 2023
1 parent c05d8d0 commit 0dacff1
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 71 deletions.
11 changes: 10 additions & 1 deletion contracts/staking/TokenStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,10 @@ contract TokenStaking is Initializable, IStaking, Checkpoints {
/// application.
/// @dev Calls `authorizationDecreaseRequested` callback
/// for each authorized application. See `IApplication`.
function requestAuthorizationDecrease(address stakingProvider) external {
function requestAuthorizationDecrease(address stakingProvider)
external
override
{
StakingProviderInfo storage stakingProviderStruct = stakingProviders[
stakingProvider
];
Expand Down Expand Up @@ -608,6 +611,12 @@ 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;
Expand Down
177 changes: 107 additions & 70 deletions test/staking/TokenStaking.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1984,89 +1984,126 @@ describe("TokenStaking", () => {
await tToken
.connect(stakingProvider)
.approve(tokenStaking.address, topUpAmount)
tx = await tokenStaking
.connect(stakingProvider)
.topUp(stakingProvider.address, topUpAmount)
})

Check failure on line 1988 in test/staking/TokenStaking.test.js

View workflow job for this annotation

GitHub Actions / code-lint-and-format

Delete `······`
context("when one of applications is paused", () => {
it("should revert", async () => {
await tokenStaking
.connect(deployer)
.setPanicButton(application1Mock.address, panicButton.address)
await tokenStaking
.connect(panicButton)
.pauseApplication(application1Mock.address)

it("should update T staked amount", async () => {
await assertStake(stakingProvider.address, expectedAmount)
await expect(
tokenStaking
.connect(stakingProvider)
.topUp(stakingProvider.address, topUpAmount)
).to.be.revertedWith("Application is not approved")
})
})

Check failure on line 2005 in test/staking/TokenStaking.test.js

View workflow job for this annotation

GitHub Actions / code-lint-and-format

Delete `······`
context("when one of applications is disabled", () => {
it("should revert", async () => {
await tokenStaking
.connect(deployer)
.disableApplication(application2Mock.address)

it("should not increase available amount to authorize", async () => {
expect(
await tokenStaking.getAvailableToAuthorize(
stakingProvider.address,
application1Mock.address
)
).to.equal(0)
expect(
await tokenStaking.getAvailableToAuthorize(
stakingProvider.address,
application2Mock.address
)
).to.equal(amount.sub(authorized2))
await expect(
tokenStaking
.connect(stakingProvider)
.topUp(stakingProvider.address, topUpAmount)
).to.be.revertedWith("Application is not approved")
})
})

it("should increase authorized amount", async () => {
expect(
await tokenStaking.getMaxAuthorization(stakingProvider.address)
).to.equal(expectedAmount)
})
context("when all applications are approved", () => {

Check failure on line 2020 in test/staking/TokenStaking.test.js

View workflow job for this annotation

GitHub Actions / code-lint-and-format

Delete `⏎········`

beforeEach(async () => {
tx = await tokenStaking
.connect(stakingProvider)
.topUp(stakingProvider.address, topUpAmount)
})

it("should emit ToppedUp event", async () => {
await expect(tx)
.to.emit(tokenStaking, "ToppedUp")
.withArgs(stakingProvider.address, topUpAmount)
})
it("should update T staked amount", async () => {
await assertStake(stakingProvider.address, expectedAmount)
})

it("should increase authorized amounts", async () => {
expect(
await tokenStaking.authorizedStake(
stakingProvider.address,
application1Mock.address
)
).to.equal(expectedAmount)
expect(
await tokenStaking.authorizedStake(
stakingProvider.address,
application2Mock.address
)
).to.equal(authorized2.add(topUpAmount))
})
it("should not increase available amount to authorize", async () => {
expect(
await tokenStaking.getAvailableToAuthorize(
stakingProvider.address,
application1Mock.address
)
).to.equal(0)
expect(
await tokenStaking.getAvailableToAuthorize(
stakingProvider.address,
application2Mock.address
)
).to.equal(amount.sub(authorized2))
})

it("should inform application", async () => {
await assertApplicationStakingProviders(
application1Mock,
stakingProvider.address,
expectedAmount,
Zero
)
await assertApplicationStakingProviders(
application2Mock,
stakingProvider.address,
authorized2.add(topUpAmount),
Zero
)
})
it("should increase authorized amount", async () => {
expect(
await tokenStaking.getMaxAuthorization(stakingProvider.address)
).to.equal(expectedAmount)
})

it("should emit AuthorizationIncreased", async () => {
await expect(tx)
.to.emit(tokenStaking, "AuthorizationIncreased")
.withArgs(
it("should emit ToppedUp event", async () => {
await expect(tx)
.to.emit(tokenStaking, "ToppedUp")
.withArgs(stakingProvider.address, topUpAmount)
})

it("should increase authorized amounts", async () => {
expect(
await tokenStaking.authorizedStake(
stakingProvider.address,
application1Mock.address
)
).to.equal(expectedAmount)
expect(
await tokenStaking.authorizedStake(
stakingProvider.address,
application2Mock.address
)
).to.equal(authorized2.add(topUpAmount))
})

it("should inform application", async () => {
await assertApplicationStakingProviders(
application1Mock,
stakingProvider.address,
application1Mock.address,
authorized1,
expectedAmount
expectedAmount,
Zero
)
await expect(tx)
.to.emit(tokenStaking, "AuthorizationIncreased")
.withArgs(
await assertApplicationStakingProviders(
application2Mock,
stakingProvider.address,
application2Mock.address,
authorized2,
authorized2.add(topUpAmount)
authorized2.add(topUpAmount),
Zero
)
})

it("should emit AuthorizationIncreased", async () => {
await expect(tx)
.to.emit(tokenStaking, "AuthorizationIncreased")
.withArgs(
stakingProvider.address,
application1Mock.address,
authorized1,
expectedAmount
)
await expect(tx)
.to.emit(tokenStaking, "AuthorizationIncreased")
.withArgs(
stakingProvider.address,
application2Mock.address,
authorized2,
authorized2.add(topUpAmount)
)
})
})
})
})
Expand Down Expand Up @@ -2130,7 +2167,7 @@ describe("TokenStaking", () => {
.toggleAutoAuthorizationIncrease(stakingProvider.address)
})

it("should enable auto increase flag", async () => {
it("should disable auto increase flag", async () => {
expect(
await tokenStaking.getAutoIncreaseFlag(stakingProvider.address)
).to.equal(false)
Expand Down

0 comments on commit 0dacff1

Please sign in to comment.