Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions Scarb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ dependencies = [
[[package]]
name = "starkware_utils"
version = "1.0.0"
source = "git+https://github.com/starkware-libs/starkware-starknet-utils?rev=e1955423808045de868987b8fb0b43f5cbdf5699#e1955423808045de868987b8fb0b43f5cbdf5699"
source = "git+https://github.com/starkware-libs/starkware-starknet-utils?rev=7b46975d5612fb1b920bb248941030bf6c295d44#7b46975d5612fb1b920bb248941030bf6c295d44"
dependencies = [
"openzeppelin",
]

[[package]]
name = "starkware_utils_testing"
version = "1.0.0"
source = "git+https://github.com/starkware-libs/starkware-starknet-utils?rev=e1955423808045de868987b8fb0b43f5cbdf5699#e1955423808045de868987b8fb0b43f5cbdf5699"
source = "git+https://github.com/starkware-libs/starkware-starknet-utils?rev=7b46975d5612fb1b920bb248941030bf6c295d44#7b46975d5612fb1b920bb248941030bf6c295d44"
dependencies = [
"openzeppelin",
"snforge_std",
Expand Down
4 changes: 2 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ edition = "2024_07"
[dependencies]
starknet = "2.12.0"
openzeppelin = "2.0.0"
starkware_utils = { git = "https://github.com/starkware-libs/starkware-starknet-utils", rev = "e1955423808045de868987b8fb0b43f5cbdf5699" }
starkware_utils = { git = "https://github.com/starkware-libs/starkware-starknet-utils", rev = "7b46975d5612fb1b920bb248941030bf6c295d44" }

[dev-dependencies]
assert_macros = "2.12.0"
snforge_std = "0.49.0"
starkware_utils_testing = { git = "https://github.com/starkware-libs/starkware-starknet-utils", rev = "e1955423808045de868987b8fb0b43f5cbdf5699" }
starkware_utils_testing = { git = "https://github.com/starkware-libs/starkware-starknet-utils", rev = "7b46975d5612fb1b920bb248941030bf6c295d44" }

[scripts]
test = "snforge test"
Expand Down
69 changes: 68 additions & 1 deletion docs/spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,14 @@
- [L2 Reward supplier contract](#l2-reward-supplier-contract)
- [Functions](#functions-2)
- [calculate\_current\_epoch\_rewards](#calculate_current_epoch_rewards)
- [update\_current\_epoch\_block\_rewards](#update_current_epoch_block_rewards)
- [update\_unclaimed\_rewards\_from\_staking\_contract](#update_unclaimed_rewards_from_staking_contract)
- [claim\_rewards](#claim_rewards-2)
- [contract\_parameters\_v1](#contract_parameters_v1-2)
- [on\_receive](#on_receive)
- [get\_alpha](#get_alpha)
- [get\_block\_duration\_config](#get_block_duration_config)
- [set\_block\_duration\_config](#set_block_duration_config)
- [Events](#events-2)
- [Mint Request](#mint-request)
- [Minting Curve Contract](#minting-curve-contract)
Expand Down Expand Up @@ -236,6 +239,7 @@
- [CONSENSUS\_REWARDS\_IS\_ACTIVE](#consensus_rewards_is_active)
- [INVALID\_STAKER](#invalid_staker)
- [INVALID\_TOKEN\_DECIMALS](#invalid_token_decimals)
- [INVALID\_MIN\_MAX\_BLOCK\_DURATION](#invalid_min_max_block_duration)
- [Structs](#structs)
- [StakerPoolInfoV1](#stakerpoolinfov1)
- [StakerInfoV1](#stakerinfov1)
Expand All @@ -253,6 +257,7 @@
- [AttestationInfo](#attestationinfo)
- [EpochInfo](#epochinfo)
- [MintingCurveContractInfo](#mintingcurvecontractinfo)
- [BlockDurationConfig](#blockdurationconfig)
- [Type aliases](#type-aliases)
- [Amount](#amount)
- [Commission](#commission)
Expand Down Expand Up @@ -395,6 +400,7 @@ classDiagram
erc20_dispatcher,
l1_reward_supplier,
calculate_current_epoch_rewards()
update_current_epoch_block_rewards()
update_unclaimed_rewards_from_staking_contract()
claim_rewards()
on_receive()
Expand Down Expand Up @@ -2215,7 +2221,8 @@ Only staking contract can execute.
fn calculate_current_epoch_rewards(self: @TContractState) -> (Amount, Amount)
```
#### description <!-- omit from toc -->
Return the amount of rewards for the current epoch (for STRK and BTC).
Returns the amount of rewards for the current epoch (for STRK and BTC).
Used only before the consensus rewards mechanism is activated.
#### return <!-- omit from toc -->
rewards: ([Amount](#amount), [Amount](#amount)) - the rewards for the current epoch, in FRI, for STRK and BTC (respectively).
#### emits <!-- omit from toc -->
Expand All @@ -2229,6 +2236,27 @@ rewards: ([Amount](#amount), [Amount](#amount)) - the rewards for the current ep
#### access control <!-- omit from toc -->
Any address can execute.

### update_current_epoch_block_rewards
```rust
fn update_current_epoch_block_rewards(ref self: TContractState) -> (Amount, Amount);
```
#### description <!-- omit from toc -->
Returns the amount of block rewards for the current epoch (for STRK and BTC).
Used after the consensus rewards mechanism is activated.
#### return <!-- omit from toc -->
rewards: ([Amount](#amount), [Amount](#amount)) - the block rewards for the current epoch, in FRI, for STRK and BTC (respectively).
#### emits <!-- omit from toc -->
#### errors <!-- omit from toc -->
1. [CALLER\_IS\_NOT\_STAKING\_CONTRACT](#caller_is_not_staking_contract)
#### logic <!-- omit from toc -->
1. Update average block duration.
2. Invoke the Minting Curve's [yearly_mint](#yearly-mint) to receive the theoretic yearly amount of rewards.
2. Calculate block total rewards according to the yearly mint and average block duration.
3. Calculate the fraction of the rewards dedicated to BTC pools.
4. Subtract the BTC rewards from the total to get the STRK rewards.
#### access control <!-- omit from toc -->
Only staking contract.

### update_unclaimed_rewards_from_staking_contract
```rust
fn update_unclaimed_rewards_from_staking_contract(ref self: TContractState, rewards: Amount)
Expand Down Expand Up @@ -2320,6 +2348,36 @@ Returns the alpha parameter, as percentage, used when computing BTC rewards.
#### access control <!-- omit from toc -->
Any address can execute.

### get_block_duration_config
```rust
fn get_block_duration_config(self: @TContractState) -> BlockdurationConfig;
```
#### description <!-- omit from toc -->
Returns [BlockdurationConfig](#blockdurationconfig).
#### emits <!-- omit from toc -->
#### errors <!-- omit from toc -->
#### pre-condition <!-- omit from toc -->
#### logic <!-- omit from toc -->
#### access control <!-- omit from toc -->
Any address can execute.

### get_block_duration_config
```rust
fn set_block_duration_config(ref self: TContractState, block_duration_config: BlockdurationConfig);
```
#### description <!-- omit from toc -->
Set the block duration configuration.
#### emits <!-- omit from toc -->
#### errors <!-- omit from toc -->
1. [ONLY\_APP\_GOVERNOR](#only_app_governor)
2. [INVALID\_MIN\_MAX\_BLOCK\_duration](#invalid_min_max_block_duration)
#### pre-condition <!-- omit from toc -->
1. 0 < `block_duration_config.weighted_avg_factor` <= 100
2. 0 < `block_duration_config.min_block_duration` <= `block_duration_config.max_block_duration`
#### logic <!-- omit from toc -->
#### access control <!-- omit from toc -->
Only app governor.

## Events
### Mint Request
| data | type | keyed |
Expand Down Expand Up @@ -2773,6 +2831,9 @@ Only token admin.
### INVALID_STAKER
"Staker is invalid for getting rewards"

### INVALID_MIN_MAX_BLOCK_DURATION
"Invalid min/max block duration"

# Structs
### StakerPoolInfoV1
| name | type |
Expand Down Expand Up @@ -2894,6 +2955,12 @@ Only token admin.
| c_num | Inflation |
| c_denom | Inflation |

### BlockDurationConfig
| name | type |
| ------------------- | ---- |
| min_block_duration | u64 |
| max_block_duration | u64 |

# Type aliases
### Amount
Amount: u128
Expand Down
3 changes: 3 additions & 0 deletions src/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ pub(crate) const K: u8 = 2;
pub(crate) const ALPHA: u128 = 25;
/// Denominator used to scale `ALPHA` when computing BTC and STRK weights.
pub(crate) const ALPHA_DENOMINATOR: u128 = 100;

/// Number of seconds in one year.
pub(crate) const SECONDS_IN_YEAR: u64 = 365 * 24 * 60 * 60;
4 changes: 4 additions & 0 deletions src/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub(crate) enum InternalError {
UNEXPECTED_INTERNAL_MEMBER_INFO_VERSION,
INVALID_REWARDS_TRACE_IDX,
MISSING_CLASS_HASH,
INVALID_BLOCK_NUMBER,
INVALID_BLOCK_TIMESTAMP,
}

impl DescribableInternalError of Describable<InternalError> {
Expand All @@ -73,6 +75,8 @@ impl DescribableInternalError of Describable<InternalError> {
InternalError::UNEXPECTED_INTERNAL_MEMBER_INFO_VERSION => "Unexpected VInternalPoolMemberInfo version",
InternalError::INVALID_REWARDS_TRACE_IDX => "Invalid cumulative rewards trace idx",
InternalError::MISSING_CLASS_HASH => "Missing class hash",
InternalError::INVALID_BLOCK_NUMBER => "Invalid block number",
InternalError::INVALID_BLOCK_TIMESTAMP => "Invalid block timestamp",
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/flow_test/flow_ideas.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
- member change balance and test with view of current epoch balance

## `update_rewards`
- staker with only btc pool.
- staker with empty pool (STRK + BTC).
- staker with 2 btc pools with different decimals.
- staker immediately/one epoch after he called intent.
- update rewards for 2 different blocks in the same epoch - should be same rewards.
- Change epoch len in blocks - rewards should be changed.
Expand Down Expand Up @@ -49,3 +47,15 @@ more ideas:
- enable token, update rewards, advance epoch, update rewards, advance epoch, update rewards - token does not get rewards until after 2 epochs
- same as above with disable (can be implemented together as one test)
- enable token A and disable token B, next epoch upgrade, test views and rewards.

## block rewards by timestamp
- advance blocks with different block times and check the avg is calculated correctly
- update_rewards for blocks in same epoch - same rewards, then advance epoch, different rewards, update rewards for blocks in same epoch - same rewards.
- update rewards is not called every block, still rewards is updated correctly (miss block, miss first block in epoch, miss epoch)
- set block time config and test rewards after

## rewards by timestamp - migration
- set_consensus_rewards to future epoch, call update_rewards before consensus epoch and after, test rewards.
- set_consensus_rewards to future epoch, call update_rewards only after consensus epoch, test rewards.
- set_consensus_rewards to curr_epoch + 2. test rewards before and after. tets avg block time is update correctly.
- set_consensus_rewards, update_rewards, then set_consensus_rewards to later epoch, update_rewards, then set_consensus_rewards to earlier epoch, update_rewards, test avg block time.
29 changes: 13 additions & 16 deletions src/flow_test/flows.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ use staking::staking::objects::{EpochInfoTrait, NormalizedAmountTrait, StakerVer
use staking::staking::staking::Staking::V3_PREV_CONTRACT_VERSION;
use staking::staking::utils::STRK_WEIGHT_FACTOR;
use staking::test_utils::constants::{
BTC_18D_CONFIG, BTC_8D_CONFIG, BTC_DECIMALS_18, BTC_DECIMALS_8, EPOCH_DURATION, PUBLIC_KEY,
STRK_BASE_VALUE, TEST_BTC_DECIMALS, TEST_MIN_BTC_FOR_REWARDS, TEST_ONE_BTC,
AVG_BLOCK_DURATION, BTC_18D_CONFIG, BTC_8D_CONFIG, BTC_DECIMALS_18, BTC_DECIMALS_8,
EPOCH_DURATION, PUBLIC_KEY, STRK_BASE_VALUE, TEST_BTC_DECIMALS, TEST_MIN_BTC_FOR_REWARDS,
TEST_ONE_BTC,
};
use staking::test_utils::{
calculate_pool_member_rewards, calculate_staker_btc_pool_rewards_v2,
advance_blocks, calculate_pool_member_rewards, calculate_staker_btc_pool_rewards_v2,
calculate_staker_strk_rewards_v2, calculate_staker_strk_rewards_with_balances_v2,
calculate_staker_strk_rewards_with_balances_v3, calculate_strk_pool_rewards_v1,
calculate_strk_pool_rewards_v2, calculate_strk_pool_rewards_v3,
Expand All @@ -50,9 +51,7 @@ use starkware_utils::errors::{Describable, ErrorDisplay};
use starkware_utils::math::abs::wide_abs_diff;
use starkware_utils::math::utils::mul_wide_and_div;
use starkware_utils::time::time::{Time, TimeDelta};
use starkware_utils_testing::test_utils::{
advance_block_number_global, assert_panic_with_error, cheat_caller_address_once,
};
use starkware_utils_testing::test_utils::{assert_panic_with_error, cheat_caller_address_once};

/// Flow - Basic Stake:
/// Staker - Stake with pool - cover if pool_enabled=true
Expand Down Expand Up @@ -8173,9 +8172,9 @@ pub(crate) impl DelegatorRewardsAttestationConsensusFlowImpl of FlowTrait<

system.advance_k_epochs();
system.update_rewards(:staker, disable_rewards: false);
advance_block_number_global(blocks: 1);
advance_blocks(blocks: 1, block_duration: AVG_BLOCK_DURATION);
system.update_rewards(:staker, disable_rewards: false);
advance_block_number_global(blocks: 1);
advance_blocks(blocks: 1, block_duration: AVG_BLOCK_DURATION);
system.update_rewards(:staker, disable_rewards: false);
let expected_rewards = calculate_strk_pool_rewards_v3(
staker_address: staker.staker.address, :staking_contract, :minting_curve_contract,
Expand Down Expand Up @@ -8297,9 +8296,9 @@ pub(crate) impl DelegatorChangeBalanceRewardsAttestationConsensusFlowImpl of Flo

system.advance_k_epochs();
system.update_rewards(:staker, disable_rewards: false);
advance_block_number_global(blocks: 1);
advance_blocks(blocks: 1, block_duration: AVG_BLOCK_DURATION);
system.update_rewards(:staker, disable_rewards: false);
advance_block_number_global(blocks: 1);
advance_blocks(blocks: 1, block_duration: AVG_BLOCK_DURATION);
system.update_rewards(:staker, disable_rewards: false);
let expected_rewards = calculate_strk_pool_rewards_v3(
staker_address: staker.staker.address, :staking_contract, :minting_curve_contract,
Expand Down Expand Up @@ -8371,7 +8370,6 @@ pub(crate) impl DelegatorBeforeUpgradeRewardsConsensusFlowImpl of FlowTrait<
amount_own: stake_amount,
pool_amount: pool_balance,
:commission,
:staking_contract,
:minting_curve_contract,
);
assert!(expected_rewards.is_non_zero());
Expand All @@ -8381,9 +8379,9 @@ pub(crate) impl DelegatorBeforeUpgradeRewardsConsensusFlowImpl of FlowTrait<

system.advance_k_epochs();
system.update_rewards(:staker, disable_rewards: false);
advance_block_number_global(blocks: 1);
advance_blocks(blocks: 1, block_duration: AVG_BLOCK_DURATION);
system.update_rewards(:staker, disable_rewards: false);
advance_block_number_global(blocks: 1);
advance_blocks(blocks: 1, block_duration: AVG_BLOCK_DURATION);
system.update_rewards(:staker, disable_rewards: false);
let expected_rewards = calculate_strk_pool_rewards_v3(
staker_address: staker.staker.address, :staking_contract, :minting_curve_contract,
Expand Down Expand Up @@ -8465,7 +8463,6 @@ pub(crate) impl DelegatorChangeBalanceBeforeUpgradeRewardsConsensusFlowImpl of F
amount_own: stake_amount,
pool_amount: pool_balance,
:commission,
:staking_contract,
:minting_curve_contract,
);
assert!(expected_rewards.is_non_zero());
Expand All @@ -8475,9 +8472,9 @@ pub(crate) impl DelegatorChangeBalanceBeforeUpgradeRewardsConsensusFlowImpl of F

system.advance_k_epochs();
system.update_rewards(:staker, disable_rewards: false);
advance_block_number_global(blocks: 1);
advance_blocks(blocks: 1, block_duration: AVG_BLOCK_DURATION);
system.update_rewards(:staker, disable_rewards: false);
advance_block_number_global(blocks: 1);
advance_blocks(blocks: 1, block_duration: AVG_BLOCK_DURATION);
system.update_rewards(:staker, disable_rewards: false);
let expected_rewards = calculate_strk_pool_rewards_v3(
staker_address: staker.staker.address, :staking_contract, :minting_curve_contract,
Expand Down
Loading