Skip to content

Commit 8ebe787

Browse files
cronokirbyconorsch
authored andcommitted
Implement "Bank" extension trait for liquidity tournament (#5035)
## Describe your changes This trait is responsible for the actual accounting of moving funds around, to be later consumed by the end epoch handler, which figures out what portion of the rewards needs to go where. The bank will just actually move fractions of its budget in the community pool to the requested locations, atomically. Testing deferred. ## Checklist before requesting a review - [x] I have added guiding text to explain how a reviewer should test these changes. - [x] If this code contains consensus-breaking changes, I have added the "consensus-breaking" label. Otherwise, I declare my belief that there are not consensus-breaking changes, for the following reason: > yes indeed
1 parent 5cde7f6 commit 8ebe787

File tree

5 files changed

+110
-1
lines changed

5 files changed

+110
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/core/component/dex/src/component/position_manager.rs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use async_trait::async_trait;
77
use cnidarium::{EscapedByteSlice, StateRead, StateWrite};
88
use futures::Stream;
99
use futures::StreamExt;
10-
use penumbra_sdk_asset::{asset, Balance};
10+
use penumbra_sdk_asset::{asset, Balance, Value, STAKING_TOKEN_ASSET_ID};
11+
use penumbra_sdk_num::Amount;
1112
use penumbra_sdk_proto::DomainType;
1213
use penumbra_sdk_proto::{StateReadProto, StateWriteProto};
1314
use tap::Tap;
@@ -488,6 +489,57 @@ pub trait PositionManager: StateWrite + PositionRead {
488489

489490
Ok(reserves)
490491
}
492+
493+
/// This adds extra rewards in the form of staking tokens to the reserves of a position.
494+
#[tracing::instrument(level = "debug", skip(self))]
495+
async fn reward_position(
496+
&mut self,
497+
position_id: position::Id,
498+
reward: Amount,
499+
) -> anyhow::Result<()> {
500+
let prev_state = self
501+
.position_by_id(&position_id)
502+
.await?
503+
.ok_or_else(|| anyhow::anyhow!("rewarding unknown position {}", position_id))?;
504+
// The new state is the result of adding the staking token to the reserves,
505+
// or doing nothing if for some reason this position does not have the staking token.
506+
let new_state = {
507+
let mut new_state = prev_state.clone();
508+
let pair = prev_state.phi.pair;
509+
let to_increment = if pair.asset_1() == *STAKING_TOKEN_ASSET_ID {
510+
&mut new_state.reserves.r1
511+
} else if pair.asset_2() == *STAKING_TOKEN_ASSET_ID {
512+
&mut new_state.reserves.r2
513+
} else {
514+
tracing::error!("pair {} does not contain staking asset", pair);
515+
return Ok(());
516+
};
517+
*to_increment = to_increment.checked_add(&reward).expect(&format!(
518+
"failed to add reward {} to reserves {}",
519+
reward, *to_increment
520+
));
521+
// Ok, you'd think we'd be done here, alas, the [`guard_invalid_transitions`] function
522+
// will complain if the position has already been withdrawn, but the sequence has not yet been incremented!
523+
new_state.state = match prev_state.state {
524+
position::State::Opened => position::State::Opened,
525+
position::State::Closed => position::State::Closed,
526+
position::State::Withdrawn { sequence } => position::State::Withdrawn {
527+
sequence: sequence.saturating_add(1),
528+
},
529+
};
530+
new_state
531+
};
532+
self.update_position(&position_id, Some(prev_state), new_state)
533+
.await?;
534+
// At this point, we can credit the VCB, because the update passed.
535+
// This is a credit because the reward has moved value *into* the DEX.
536+
self.dex_vcb_credit(Value {
537+
asset_id: *STAKING_TOKEN_ASSET_ID,
538+
amount: reward,
539+
})
540+
.await?;
541+
Ok(())
542+
}
491543
}
492544

493545
impl<T: StateWrite + ?Sized + Chandelier> PositionManager for T {}

crates/core/component/funding/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ component = [
1313
"cnidarium",
1414
"penumbra-sdk-proto/cnidarium",
1515
"penumbra-sdk-community-pool/component",
16+
"penumbra-sdk-dex/component",
1617
"penumbra-sdk-distributions/component",
1718
"penumbra-sdk-governance/component",
1819
"penumbra-sdk-sct/component",
@@ -26,6 +27,7 @@ parallel = [
2627
"ark-groth16/parallel",
2728
"decaf377/parallel",
2829
"decaf377-rdsa/parallel",
30+
"penumbra-sdk-dex/parallel",
2931
"penumbra-sdk-tct/parallel",
3032
"penumbra-sdk-shielded-pool/parallel",
3133
"penumbra-sdk-governance/parallel"
@@ -45,6 +47,7 @@ futures = {workspace = true, optional = true}
4547
metrics = {workspace = true, optional = true}
4648
penumbra-sdk-asset = {workspace = true, default-features = true}
4749
penumbra-sdk-community-pool = {workspace = true, default-features = false}
50+
penumbra-sdk-dex = {workspace = true, default-features = false}
4851
penumbra-sdk-distributions = {workspace = true, default-features = false}
4952
penumbra-sdk-governance = {workspace = true, default-features = false}
5053
penumbra-sdk-keys = {workspace = true, default-features = false}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
use async_trait::async_trait;
2+
use cnidarium::StateWrite;
3+
use penumbra_sdk_asset::{Value, STAKING_TOKEN_ASSET_ID};
4+
use penumbra_sdk_dex::component::PositionManager as _;
5+
use penumbra_sdk_dex::lp::position;
6+
use penumbra_sdk_keys::Address;
7+
use penumbra_sdk_num::Amount;
8+
use penumbra_sdk_sct::component::clock::EpochRead as _;
9+
use penumbra_sdk_sct::CommitmentSource;
10+
use penumbra_sdk_shielded_pool::component::NoteManager as _;
11+
use penumbra_sdk_txhash::TransactionId;
12+
13+
#[allow(dead_code)]
14+
#[async_trait]
15+
/// The bank strictly controls issuance of rewards in the liquidity tournament.
16+
///
17+
/// This ensures that rewards do not exceed the issuance budget, and are immediately
18+
/// debited from the appropriate source (which happens to be the community pool),
19+
/// and credited towards the appropriate destination (i.e. positions or new notes).
20+
pub trait Bank: StateWrite + Sized {
21+
/// Move a fraction of our issuance budget towards an address, by minting a note.
22+
async fn reward_to_voter(
23+
&mut self,
24+
reward: Amount,
25+
voter: &Address,
26+
tx_hash: TransactionId,
27+
) -> anyhow::Result<()> {
28+
let epoch = self
29+
.get_current_epoch()
30+
.await
31+
.expect("should be able to read current epoch");
32+
self.mint_note(
33+
Value {
34+
asset_id: *STAKING_TOKEN_ASSET_ID,
35+
amount: reward,
36+
},
37+
voter,
38+
CommitmentSource::LiquidityTournamentReward {
39+
epoch: epoch.index,
40+
tx_hash,
41+
},
42+
)
43+
.await?;
44+
Ok(())
45+
}
46+
47+
/// Move a fraction of our issuance budget towards a position, increasing its reserves.
48+
async fn reward_to_position(&mut self, reward: Amount, lp: position::Id) -> anyhow::Result<()> {
49+
self.reward_position(lp, reward).await?;
50+
Ok(())
51+
}
52+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
pub mod bank;
12
pub mod nullifier;
23
pub mod votes;

0 commit comments

Comments
 (0)