Commit Graph

84 Commits

Author SHA1 Message Date
Ricardo Guilherme Schmidt
458e07cc70 chore(RLN): import rln-contracts 2025-06-05 13:56:52 +02:00
r4bbit
bfba516b28 chore: use stable foundry on CI 2025-06-04 14:52:31 +02:00
r4bbit
1304846f2c feat(Karma): add AccessControl to Karma
This commit introduces `AccessControl` capabilities to Karma. The reason this is done so that there can be multiple actors in the system with different privileges.

The main changes done here are:

- Introduce internal functions for most of the `Karma` specific logic (this is necessary to allow properly extending the contract's functionality via modifiers) later on
- Inherit AccessControlUpgradeable contract
- Introduce OPERATOR_ROLE next to the already provided DEFAULT_ADMIN_ROLE

This is an alternative solution to the PR in #209.
Instead of providing an upgrade version, this commit is a breaking
change as it introduces a storage layout conflict.

BREAKING CHANGE

This commit introduces a storage layout conflict.
Redeployment required.

Closes #207
2025-05-14 11:23:06 +02:00
r4bbit
05cccfc662 refactor(StakeManager): move lockUntil into StakeVault
Most of the reasoning has been discussed in #208.
Primarily what's happening here is:

- `StakeManager.VaultData.lockUntil` has been removed
- `StakeVault.lockUntil` has been introduced
- `StakeVault.updateLockUntil(uint256 value)` has been introduced
- `StakeVault.leave()` performs normal `leave()` action but keeps funds
  in vault if lockup period hasn't expired
- `StakeVault.withdrawFromVault()` has been introduced to withdraw
  remaining *stake* funds in vault

BREAKING CHANGE:

- `StakeManager.VaultData.lockUntil` has been removed
- `StakeVault.lockUntil` has been introduced
- `StakeVault.leave()` performs normal `leave()` action but keeps funds
  in vault if lockup period hasn't expired

Closes #208
2025-05-13 14:05:02 +02:00
r4bbit
10b6743d55 fix(Karma): prevent overflow errors when issuing rewards
As described in #206 it's theoretically possible to cause overflows when
either Karma is issued via `mint()` or `setReward()` because both
functions don't take the supply of the other "domain" into account.

This commit fixes this by performing an arithmetic operation on
`super.totalSupply()`, `totalDistributorAllocation` and the newly issued
`amount`.

If there's an overflow, the code will cause an overflow and reverts.
This ensure overflows can't happen *after* rewards have been issued.

Closes #206
2025-05-13 13:36:02 +02:00
r4bbit
309d765731 refactor(DeploymentConfig): remove proxy implementation address
This was a bandaid solution to easily allow for upgrade scripts.
We've changed those now to expect environment variables instead.
This allows us to change the dependencies without committing them to
version control.
2025-04-02 16:56:16 +02:00
r4bbit
22feed8dba fix(StakeManager): don't allow migrating to unregistered vaults
This was uncovered when a user tried to migrate a `StakeVault` from one
deployed `StakeManager` to a vault that was registered with a different
`StakeManager` instance.

The result was that the data of the vault has been indeed migrated to
the new vault, however, since the vault queries stake data from the
`StakeManager`, the new vault is unable to properly calculated balances
to withdraw funds, among other things.

It shouldn't be possible to migrate to a vault that isn't registered
with the system a user migrates from, in the first place.

This commit ensures that by reverting when the `migrateTo` address isn't
a registered vault with the `StakeManager`.
2025-04-01 16:10:02 +02:00
r4bbit
801740f74f !refactor(RewardsStreamerMP): rename RewardsStreamerMP to StakeManager
This renames `RewardsStreamerMP` to `StakeManager`. The original name
only exists because the project has started with different versions of
the contract. Since the contract is no longer just dealing with MPs but
actually distributes rewards, it makes sense to make this the official
stake manager of the protocol.

**BREAKING CHANGE:**

- `RewardsStreamerMP` is now `StakeManager`
- `StakingManager__*` error selectors are now `StakeManager__*`
  selectors
2025-03-28 14:45:31 +01:00
r4bbit
7bd0c16872 !refactor(RewardsStreamerMP): remove VaultData.mpStaked
After doing somme work on merging `compound()` into `updateVault()`, we
noticed that both, `vault.mpStaked` and `vault.mpAccrued` are always
equal.

Therefore, we're removing `vault.mpStaked`.

BREAKING CHANGE:

- `VaultData.mpStaked` no longer exists, use `VaultData.mpAccrued`
instead.

- `Compound(address,uint256)` is now `VaultUpdated(address,uint256,uint256)`

- `AccountLeft(address)` is now `VaultLeft(address)`
2025-03-26 13:19:05 +01:00
r4bbit
ce982b9ce5 !refactor(Karma): remove mintAllowance()
This is no longer a necessary requirement.
This commit removes `mintAllowance()`, it's internal counterpart and its
usage.

BREAKING CHANGE:

`mintAllowance()` no longer exists.

Closes #192
2025-03-26 08:33:31 +01:00
r4bbit
695a208804 !refactor: rename getStakedBalance() -> stakedBalanceOf()
This is to align with all the other `mp*Of` and `mp*OfAccount`
functions as discussed in #188

BREAKING CHANGE:

`getStakedBalance(address)` is now `stakedBalanceOf(address)`

Closes #188
2025-03-25 20:25:46 +01:00
r4bbit
8e4aa682c1 !refactor(RewardsStreamerMP): rename internal functions and rewardIndex
This commit does the following changes:

`_getVaultPendingMP()` -> _`vaultPendingMP()`
`_liveTotalMP()` -> `_totalMP()`
`_liveRewardIndex()` -> `_rewardIndex()`
`updateRewardIndex()` -> `_updateRewardIndex()`

BREAKING CHANGE:

The previous public `rewardIndex` field is now called `lastRewardIndex`.

Closes #189
2025-03-25 17:43:41 +01:00
Ricardo Guilherme Schmidt
8df475aab8 fix(StakeManager): Allow extending the lock after increasing stake to allow account reaching absolute max MP
fix(MultiplierPointMath): Fix helper function to correctly estimate avaliable lock time

chore(spec): remove additional field which does not exist anymore
2025-03-25 17:14:17 +01:00
r4bbit
2e01e0d03b refactor(RewardsStreamerMP): merge compound() with _updateVault()
This commit merges `compound()` into `_updateVault()` as there's no
reason not to update vault without compounding it.

This was discussed in #187.

BREAKING CHANGE: A couple of APIs have been removed or replaced.

-> inline _compound into _updateVault()
-> remove compond() in favor of updateVaultMP()
-> rename updateVaultMP() to updateVault()
-> rename compoundAccount() to updateAccount()

Closes #187
2025-03-25 16:09:04 +01:00
Ricardo Guilherme Schmidt
0385522821 chore(tests): use UndefinedError in some cases 2025-03-21 18:21:57 +01:00
Ricardo Guilherme Schmidt
400923aed0 # This is a combination of 6 commits.
# This is the 1st commit message:

feat(RewardsStreamerMP.t): improve fuzz tests to test revert cases

fix(StakeMath): prevent uint256 overflow error
fix(StakeMath): prevent unstaking zero

# This is the commit message #2:

fix(StakeMath): prevent unstaking zero

# This is the commit message #3:

refactor(fuzz-tests): enhance readibility + test for unstake

# This is the commit message #4:

chore(tests): add compoud fuzz test

# This is the commit message #5:

chore(tests): Add Compound Fuzz Test

# This is the commit message #6:

chore (tests): fix fuzz test Unstake to always call compound function before unstake.
2025-03-21 18:21:57 +01:00
r4bbit
dabcf5c990 fix(RewardsStreamerMP): remove double totalMPStaked substraction 2025-03-20 14:03:28 +01:00
r4bbit
4fa3eb06e0 feat(RewardsStreamerMP): allow for staking multiple times with lock
This commit enables users to stake multiple times into the same vault
with a lock increase.

If their total lock increase goes beyond the maximum lock period,
staking will revert. In this case users will have to create a new vault.

Closes #152

Co-authored-by: Andrea Franz <andrea@gravityblast.com>
2025-03-19 15:36:55 +01:00
Andrea Franz
47b9fd0ebe chore(KarmaNFT): update deploy scripts with proper svg strings 2025-03-14 16:01:34 +01:00
r4bbit
5e2dcbabd1 feat(RewardsStreamerMP): add function to compound all MPs for an account
Closes #175
2025-03-14 15:00:26 +01:00
r4bbit
619b541d2a fix(RewardsStreamerMP): prevent attack causes accounts to not accrue MP
Closes #176
2025-03-14 13:25:09 +01:00
Andrea Franz
54fd86ddf4 chore(KarmaNFT): use image_data as field when there's a raw inlined image 2025-03-14 13:16:25 +01:00
r4bbit
ccd4bec8c7 docs: adjust docs to latest changes 2025-03-12 11:18:41 +01:00
Andrea Franz
4d10993cc9 chore(certora): add rule to test the sum of all distributors allocation 2025-03-11 13:25:00 +01:00
r4bbit
ab541a818e refactor: remove unnecessary usage of __Ownable__Init
In this version we only need to call `transferOwnership()` (which is
also what's called by `__Ownable__init`).
2025-03-10 13:16:49 +01:00
Ricardo Guilherme Schmidt
e4d21b6caf feat(RewardsStreamerMP): enable extending lock period 2025-03-06 16:50:40 +01:00
Andrea Franz
32bb2356dc chore(KarmaNFT): use deploy script in tests 2025-03-06 11:50:07 +01:00
Andrea Franz
96e50c6818 chore(KarmaNFT): add deploy script and remove nft address var in the metadata generators 2025-03-05 11:52:30 +01:00
r4bbit
aa1addbfcd chore: downgrade to paris
We put `cancun` as evm version into the foundry toml by mistake and
ended up building on top of it with certain assumptions.

Turns out that the network we're deploying to does not support that
version so we have to compile with `paris`.

This however, also requires a downgrade of the open zeppelin libraries,
which in turn requires changes in our code base. Primarily related to
initialization of `OwnableUpgradeable` and upgrades via UUPSUpgradeable.

This commit makes all the necessary changes.
2025-03-05 11:47:30 +01:00
Andrea Franz
62684698e5 chore(RewardsStreamerMP): remove unused ReentrancyGuardUpgradeable 2025-03-05 10:32:30 +01:00
Andrea Franz
aa3442b577 feat(Karma): make karma upgradeable 2025-02-28 14:39:15 +01:00
r4bbit
ef3abad941 refactor(TrustedCodehashAccess): add storage gap for upgrade safety
Closes #131
2025-02-28 12:07:45 +01:00
r4bbit
60081c14fb refactor: cleanup, move code around, and adjust deployscript 2025-02-28 12:05:18 +01:00
r4bbit
5755138d8a refactor(StakeMath): remove unused errors 2025-02-28 11:48:03 +01:00
Andrea Franz
ed3577f8c4 feat(Karma): allocate external rewards using the Karma contract 2025-02-27 20:48:39 +01:00
Ricardo Guilherme Schmidt
54e80e117f chore(tests): add fuzz test for rewards 2025-02-27 11:25:20 +01:00
r4bbit
a51d5199ae docs(RewardsStreamerMP): add natspec comments 2025-02-26 08:31:29 +01:00
r4bbit
7575e965a2 refactor(RewardsStreamerMP.t.sol): fix test that causes stack too deep
error

One of our tests uses too many local variables and causes stack too deep
errors unless optimized `--via-ir`.

We've introduced `--via-ir` to work around that until a proper fix is
done. The downside of using that option is that compilation times get
significantly longer.

This commit restructures the test such that it doesn't use as many local
variables.
2025-02-25 15:06:58 +01:00
Ricardo Guilherme Schmidt
3bd9c46fb3 chore(tests): Add fuzz tests 2025-02-24 15:29:10 +01:00
Andrea Franz
50b6d1a545 chore(XPToken): rename XP to Karma 2025-02-21 11:43:12 +01:00
r4bbit
807f480d97 test(RewardsStreamerMP): ensure newly added fields are checked in tests
Those were missing and now added.
Because of this we're running into stack to deep errors in test, so we
have to enable `via_ir` by default for now until we split things up.
2025-02-20 17:26:56 +01:00
Andrea Franz
58f43d07ce feature(RewardsStreamerMP): add compound function and track rewards accrued by each vault 2025-02-20 17:15:17 +01:00
r4bbit
3455068e3c refactor(IStakeManager): introduce events
A bunch of state changing functions were not emitting any events.
This commit adds them.
2025-02-20 13:32:41 +01:00
r4bbit
4086dda6e2 refactor(IStakeManager): move error codes to interface and remove unused
ones

There are a bunch of error codes that are either similar to other ones
or not used at all.

This commit moves them to the interface and removes the ones that aren't
used anymore. Part of the reason we have so many unused errors becuase
they had been "reintroduced" in `StakeMath`, which we'll revisit as
well as described in #130
2025-02-20 13:10:21 +01:00
r4bbit
6f199313ec feat(RewardsStreamerMP): allow vaults to migrate to other vaults
This commit introduces a function `migrateToVault(address)` that allows
`StakeVault`s to migrate to other `StakeVault` instances.

The idea is that, when an upgrade was done on the stake manager, it
might introduces functions that can't be accessed through the stake
vaults that are out there.

Users will have to create new stake vault instances that provide the
necessary functionality.

`migrateToVault()` allows them to do so.

Closes #127
2025-02-19 17:58:15 +01:00
r4bbit
59226ef107 test(RewardsStreamerMP): ensure lock() reverts when vault is empty
Closes #126
2025-02-19 17:29:03 +01:00
Andrea Franz
6b31d3944f feat(RewardsStreamerMP): vaults shares are stakedBalance + mpStaked 2025-02-19 16:53:12 +01:00
r4bbit
fb79e249fb fix(RewardsStreamerMP): ensure registerVault reverts in emergency mode
Originally, I just wanted to simplify the certora rule that,
when emergency mode is enabled, only a few selected functions can be
called.

Instead of listing all the view function as "allowed", I've moved to
using CVLs `isView` flag on the function under verification.

This however uncovered a violation where
`RewardsStreamerMP.registerVault` is allowed to be called in emergency
mode.

In theory there's no harm in registering a vault when the system is in
emergency mode, but semantically it doesn't really make sense.

`registerVault` has been accidentally added to `isViewFunction()`.
This commit fixes this by adding `onlyNotEmergencyMode` modifier to
`registerVault()`.
2025-02-19 16:17:10 +01:00
Andrea Franz
51dcb884b3 chore(RewardsStreamerMP): remove MPs from shares/weights calculations 2025-02-18 10:20:22 +01:00
r4bbit
70a7f30d2a feat: introduce proxy clones
This commit introduces proxy clones to make create `StakeVault`s as
cheap as possible.

Major changes here are:

- Introduce `VaultFactory` which takes care of creating clones and
  registering them with the stake manager
- Make `StakeVault` and `Initializable` so it can be used as a
  "template" contract to later have proxies point to it
- Adjust the deployment script to also deploy `VaultFactory` and ensure
  The proxy is whitelisted in the stake manager
- Make use of the new proxy clones in tests
- Add a test for `TrustedCodehashAccess` that ensures the proxy
  whitelisting works and setting up a (malicious) proxy is not going to
  work

Closes #101
2025-02-06 13:21:01 +01:00