Commit Graph

81 Commits

Author SHA1 Message Date
Ricardo Guilherme Schmidt b9a83c9d61
Refactor(StakeManager) remove unused Account struct 2024-09-29 17:49:07 -03:00
Ricardo Guilherme Schmidt 87ba390162
refactor(StakeManager): optimize account storage by splitting commonly changing values of rarely changing values 2024-09-29 17:46:32 -03:00
Ricardo Guilherme Schmidt 978b4dcf30
refactor(StakeManager): optimize stake and lock functionality by removing mintBonusMP function 2024-09-26 16:07:06 -03:00
Ricardo Guilherme Schmidt 769f32f718 refactor(StakeManager): replace epoch.startTime with a global startTime
As now all epochEnd() are multiples of EPOCH_SIZE, we can drop the startTime from the struct Epoch, and avoid storing an information that can be cheaply calculated on the fly.

- Refactored the StakeManager contract to optimize the finalization of epochs and execution of accounts and epochs.
- Replaced the Epoch.startTime with a new global variable startTime to keep track of the start time of the contract.
- Modified the finalizeEpoch function to use a temporary variable for the current epoch and calculate the epoch reward only for the current epoch.
- Updated the migrationInitialize function to set the start time using the new startTime variable.
- Modified the epochEnd function to calculate the end time of the current epoch using the startTime variable.
- Modified the newEpoch function to calculate the number of the next epoch based on the startTime variable.

refactor(StakeManager): set startTime as immutable, load startTime from previousManager at constructor, verify startTime is correct on initializeMigration
2024-09-26 15:41:35 -03:00
Ricardo Guilherme Schmidt 4a261e8b48 refactor(StakeManager): optimize epoch finalization and execution of accounts and epochs
This commit refactors the StakeManager contract to optimize the finalization and execution of epochs. It updates the function finalizeEpoch() to new function finalizeEpoch(uint256 _limitEpoch) that releases rewards for the current epoch and increases the epoch up to the specified limit. This allows for more efficient processing of epochs.

The changes also include updates to the executeEpoch() and executeAccount() functions to utilize the new finalizeEpoch() function. Additionally, a new function newEpoch() is introduced to calculate the last epoch that can be processed based on the current time. Now the executeAccount(account,limit) will also process epochs up to the specified limit.

Added a overload of the executeAccount to process the account up to the newEpoch()

Added a overload of the executeEpoch to allow the process of epochs up to a certain limit.

All methods now that finalizeEpoch will process the epochs up to the newEpoch().

These changes improve the overall performance, user expirience and reliability of the StakeManager contract.

chore(StakeManagerStartMigration.spec): add new function executeEpoch(uint256) to blockedWhenMigrating

fix(StakeManager.sol): Replace the check for pending migration in migrationInitialize with noPendingMigration modifier to avoid code duplication
2024-09-26 15:41:35 -03:00
Ricardo Guilherme Schmidt 142137dee7 fix(StakeManager): fix finalizeEpoch to use last epoch epochEnd and add tests to catch error 2024-09-26 15:41:35 -03:00
Ricardo Guilherme Schmidt 3e5361f9be fix(StakeManager): change order of call of mintBonusMP to fix intended behavior. 2024-09-12 11:44:48 +02:00
Ricardo Guilherme Schmidt 8683376160 feat(StakeManager): optimizations on processAccount 2024-09-11 20:09:08 +02:00
Ricardo Guilherme Schmidt b19182ad20 chore(StakeManager): remove unnecessary `finalizeEpoch` modifier from `migrateTo`
Fixes #109
2024-09-11 16:43:59 +02:00
Ricardo Guilherme Schmidt 58ca65120f chore(StakeManager: remove irrelevant code from `_mintBonusMP` function
Fixes #108
2024-09-11 16:17:17 +02:00
Ricardo Guilherme Schmidt b2a0d0cf25 fix(StakeManager): use while loop instead of hacky for loop in process account
chore: lint again with foundry updated
2024-09-11 15:18:24 +02:00
r4bbit 5dec595a20 feat(StakeManager): implement multiplier points estimation
This commit introduces the internal accounting logic for accrueing
multiplier points, that will later be used to determine how many
experience points an account is eligible to.

The majority of the work here was done by @3esmit.
2024-09-10 08:51:35 +02:00
r4bbit f90a3ce365 chore(StakeManager): formatting as foundry changed its mind 2024-08-27 14:46:22 +02:00
r4bbit 4a04b46e14 refactor(StakeManager): initialMP -> bonusMP, currentMP -> totalMP
After discussing this offline, we've decided that the naming of these
properties was misleading. This commit performs the following changes:

- `account.initialMP` becomes `account.bonusMP`
- `account.currentMP` becomes `account.totalMP`

Rationale:

`initialMP` indicates that this is an immutable field which is not the
case as in scenarios where accounts increase the `lock()` time, they'll
also increase their bonus multiplier (`initialMP`).

`currentMP` was misleading in combination with `initialMP`. Really what
it reflects is the total multiplier points of an account **including**
its bonus multiplier points.
2024-06-25 13:13:02 -03:00
r4bbit d18df07b28 refactor(StakeManager): make function names more descriptive
Some of the functions on our contracts were confusing.
This commit changes them so they describe what they actually do.
2024-06-20 15:48:27 -03:00
Ricardo Guilherme Schmidt cbd40aef8a fix(StakeManager): lock function checks for MIN_LOCKUP_PERIOD 2024-03-12 11:41:41 -03:00
Ricardo Guilherme Schmidt 694608a629 chore(StakeManager): removed duplicate error type 2024-03-12 11:41:41 -03:00
Ricardo Guilherme Schmidt c764142909 chore(StakeManager): change revert to use custom error 2024-03-12 11:41:41 -03:00
r4bbit da007451a4 fix(StakeManager): ensure `currentEpoch` is `0` when migrating
A previous manager can only migrate once, because the migration address
is locked in. A **new** manager is always aware of its previous manager.

This means, when a migration happens and is initialized, we know for
sure it's always the first time this is happening.

We probably don't want a migration to take place if the new manager has
already processed epochs, so we're adding a check that its
`currentEpoch` must be `0`.

This also ensures one of its invariants isn't violated:

`epochsOnlyIncrease` and `highEpochsAreNull`.
2024-03-05 10:21:26 -03:00
r4bbit 5cdd54a884 fix(StakeManager): don't allow migration initialization while migrating
This is actually a bug that the certora prover found.
The rule `epochStaysSameOnMigration` failed because a previous
`StakeManager` could call `migrationInitialize` and change
`currentEpoch` on a next `StakeManager`, even though the next `StakeManager`
might be in migration itself (which means the `currentEpoch` is now
allowed to change).

This commit fixes this by ensure `migrationInitialize()` will revert if
the `StakeManager` already has a `migration` on going.
2024-03-05 10:21:26 -03:00
Ricardo Guilherme Schmidt 6c358dab9e fix(StakeManager): use OpenZeppelin Math to avoid precision loss in int divisions 2024-02-26 10:07:54 -03:00
Ricardo Guilherme Schmidt 92ff9bff21 refactor(StakeManager): account initialize in currentEpoch 2024-02-26 10:07:54 -03:00
Ricardo Guilherme Schmidt 17f859577a refactor(StakeManager): change MIN_LOCKUP_PERIOD to 2 weeks 2024-02-23 11:56:03 -03:00
Ricardo Guilherme Schmidt 294c691d1a chore(StakeManager): mark TODOs on division precision loss 2024-02-23 10:57:17 -03:00
Ricardo Guilherme Schmidt 2a762d6a41 fix(StakeManager): use a correct MP formula 2024-02-23 10:57:17 -03:00
Ricardo Guilherme Schmidt c356954302 fix(StakeManager): check for valid migration address 2024-02-23 10:57:17 -03:00
Ricardo Guilherme Schmidt f06168c8ce fix(StakeManager): properly init accs and checks init 2024-02-23 10:57:17 -03:00
Ricardo Guilherme Schmidt c9ed9dd833 refactor(StakeManager): refactor multiplier points logic 2024-02-23 10:57:17 -03:00
Ricardo Guilherme Schmidt 03bc6559ae fix: StakeManager migration fixes and certora rules 2024-02-20 09:08:00 +01:00
Nurit Dor 14248a285b chore: certora setup for stakemanager and vault 2024-02-20 09:04:23 +01:00
r4bbit 4e411f2b37 test(StakeManager): add test to check that inital MPs are minted
This adds a test that ensures multiplier points are minted with a 1:1
ratio to the stake token amount.

This scenario covers the case where no lock up time is set during
staking.
2024-01-19 11:44:25 +01:00
r4bbit 74ff357142 fix(StakeVault): make unstaking actually work
Unstaking didn't actually work because it was using `transferFrom()` on the
`StakeVault` with the `from` address being the vault itself.
This would result in an approval error because the vault isn't creating
any approvals to spend its own funds.

The solution is to use `transfer` instead and ensuring the return value
is checked.
2024-01-19 09:57:34 +01:00
r4bbit edc44e0c6a
chore: make linter happy 2024-01-16 12:12:26 +01:00
r4bbit d9a64559a2
fix(StakeManager): add checks for whether lockup period is in range (#39)
This commit introduces `MIN_LOCKUP_PERIOD` and `MAX_LOCKUP_PERIOD` and
makes use of them within `StakeManager.stake()` and
`StakeManager.lock()` accordingly.

When users deposit tokens into their vault via `stake()`, they can
provide an optional lockup time. If the value is `0` it implies users do
not want to lock their stake.

If the value is `> 0` it has to be within the range of
`MIN_LOCKUP_PERIOD` and `MAX_LOCKUP_PERIOD`.

Properly addresses #15
2023-12-06 12:10:07 +01:00
r4bbit f259286e98
feat: introduce `VaultFactory` (#38)
This commit introduces a first version of a `VaultFactory` that later
will be extended to be capable of instantiating reward vaults and
possible keep track of vault instances per owner.

As a first step, this implementation comes with a `createVault()`
function which takes care of creating vaults.

Because `VaultFactory` also knows about `StakeManager` it can derive the
manager's address and stake token from it when creating vaults, allowing
the API to be without arguments.

Partially addresses #37
2023-11-07 09:49:22 +01:00
r4bbit b5e513ce49
refactor(StakeVault): introduce `stakedToken()` API (#36)
Because the `stakedToken` property is `immutable`, solhint recommends to
make it in all caps. This commit changes the property to adhere to that
rule and also makes the property private.

To access the `stakedToken` there's now a `stakedToken()` function on
the contract.
2023-11-07 09:40:38 +01:00
r4bbit 70c74e2c2c
refactor(StakeVault): use custom error over error strings (#35) 2023-11-07 09:38:00 +01:00
r4bbit 6f591dd674
refactor(StakeManager): use custom errors everywhere (#30) 2023-10-10 15:49:34 +02:00
r4bbit 03d2dcf3e8
refactor(StakeManager): add custom `StakeManager__FundsLocked` error (#29) 2023-10-10 15:44:16 +02:00
r4bbit eeffcfe7d7
refactor(StakeManager): use custom error in `onlyVault` modifier (#28)
Also introduce tests that ensure the error is actually emitted.
2023-10-10 15:32:46 +02:00
r4bbit 2e7c5148b4
refactor: migrate repository to foundry-template (#6)
This commit migrates the repo to our foundry template, which ensures we
have consistent tooling across smart contract repositories that are
maintained by Vac.

This removes all hardhat related files and workflows and replaces them
with more perfomant foundry workflows.

It also sets up tests, CI and linting.
2023-09-12 18:37:30 +02:00
Frederico Teixeira 0e95ca428f Vault's contract gets ERC20 and Manager
as arguments
2023-08-21 08:21:59 +02:00
Frederico Teixeira d9d6a6e633 function signature changed from "pure" to "view",
as it access block.timestap.
2023-07-24 17:16:44 +02:00
Frederico Teixeira 9d0bb25715 introduced a ^ in the solidity compiler version 2023-07-24 17:16:03 +02:00
Martin Strobl 56f007e3b7 added MP boost limit check
- limit checked when locking incl. the increased limit due to bonus
- limit checked when processing only for accounts that are not locked
2023-07-11 15:47:55 +02:00
Martin Strobl a731962d32 Repair minting MPs with lock
- The "free MPs" in the amount of 100% of stake that are minted on deposit are now not minted anymore duplicitely in the lock function
2023-07-11 12:08:30 +02:00
Martin Strobl fe5cd4b517 Initial MP mint repaired
- Now, MPs are minted on the newly staked amount only (previously was entire stake).
- The bonus MPs are calculated correctly such that there is a 1x bonus per year of lock.
- Still TBD: MPs are not minted  for existing stake if current lock extended (or newly introduced), there is no check for max boost MP ceiling
2023-07-11 11:56:34 +02:00
Ricardo Guilherme Schmidt fcb8dca9a6
mintInitialMultiplier dt+1 2023-07-09 16:00:49 -03:00
Ricardo Guilherme Schmidt c1570fa471
add optimism minime 2023-07-08 17:10:37 -03:00
Ricardo Guilherme Schmidt 17d1816570
fixes: initial multiplier mint, leave, unstake 2023-07-06 09:12:01 -03:00