-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dex: Swap alt fee tokens for native fee token (#4643)
## Describe your changes Moves fee handling out of the Transaction's ActionHandler and into the component, which accumulates fees (in any token), and adds hooks to the DEX thatadd chain-submitted swaps for any non-native fee tokens into the native token. These are then accumulated back into the fee component. The base fee and tip are tracked separately through this whole process, in order to support the intended behaviour (the base fee is burned to account for the _chain_'s resource use, the tip is sent to the proposer to encourage them to include transactions in fee priority order). However, tip handling is not currently implemented, so both are burned. Later, the funding/distribution components should extract the tip and send it to the proposer's funding streams. However, until a robust fee market develops, this form of proposer incentivization can be deferred. The accounting will be there for it when it arises. ## Issue ticket number and link #4328 ## Checklist before requesting a review - [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: > consensus-breaking but not state-breaking, no migrations necessary --------- Co-authored-by: Erwan Or <[email protected]>
- Loading branch information
1 parent
85dc97f
commit 5e23b95
Showing
26 changed files
with
992 additions
and
115 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
mod common; | ||
|
||
use self::common::TempStorageExt; | ||
use cnidarium::{ArcStateDeltaExt, StateDelta, TempStorage}; | ||
use cnidarium_component::ActionHandler; | ||
use penumbra_asset::asset; | ||
use penumbra_dex::{ | ||
component::ValueCircuitBreakerRead, | ||
swap::{SwapPlaintext, SwapPlan}, | ||
TradingPair, | ||
}; | ||
use penumbra_fee::Fee; | ||
use penumbra_keys::{test_keys, Address}; | ||
use penumbra_num::Amount; | ||
use penumbra_sct::component::source::SourceContext; | ||
use rand_core::SeedableRng; | ||
use std::{ops::Deref, sync::Arc}; | ||
|
||
#[tokio::test] | ||
/// Minimal reproduction of a bug in the DEX VCB swap flow tracking. | ||
/// | ||
/// Overview: The DEX VCB was double-counting swap flows for a same asset | ||
/// by computing: `aggregate = (delta + aggregate) + aggregate`, instead | ||
/// it should compute: `aggregate = delta + aggregate`. | ||
/// This bug was fixed in #4643. | ||
async fn dex_vcb_tracks_multiswap() -> anyhow::Result<()> { | ||
let mut rng = rand_chacha::ChaChaRng::seed_from_u64(1776); | ||
let storage = TempStorage::new().await?.apply_default_genesis().await?; | ||
let mut state = Arc::new(StateDelta::new(storage.latest_snapshot())); | ||
|
||
// Create the first swap: | ||
let gm = asset::Cache::with_known_assets().get_unit("gm").unwrap(); | ||
let gn = asset::Cache::with_known_assets().get_unit("gn").unwrap(); | ||
let trading_pair = TradingPair::new(gm.id(), gn.id()); | ||
|
||
let delta_1 = Amount::from(100_000u64); | ||
let delta_2 = Amount::from(0u64); | ||
let fee = Fee::default(); | ||
let claim_address: Address = test_keys::ADDRESS_0.deref().clone(); | ||
let plaintext = | ||
SwapPlaintext::new(&mut rng, trading_pair, delta_1, delta_2, fee, claim_address); | ||
|
||
let swap_plan = SwapPlan::new(&mut rng, plaintext.clone()); | ||
let swap_one = swap_plan.swap(&test_keys::FULL_VIEWING_KEY); | ||
|
||
let mut state_tx = state.try_begin_transaction().unwrap(); | ||
state_tx.put_mock_source(1u8); | ||
swap_one.check_and_execute(&mut state_tx).await?; | ||
|
||
// Observe the DEX VCB has been credited: | ||
let gm_vcb_amount = state_tx | ||
.get_dex_vcb_for_asset(&gm.id()) | ||
.await? | ||
.expect("we just accumulated a swap"); | ||
assert_eq!( | ||
gm_vcb_amount, | ||
100_000u128.into(), | ||
"the DEX VCB does not contain swap 1" | ||
); | ||
|
||
// Let's add another swap: | ||
let swap_two = swap_one.clone(); | ||
swap_two.check_and_execute(&mut state_tx).await?; | ||
let gm_vcb_amount = state_tx | ||
.get_dex_vcb_for_asset(&gm.id()) | ||
.await? | ||
.expect("we accumulated two swaps"); | ||
assert_eq!( | ||
gm_vcb_amount, | ||
200_000u128.into(), | ||
"the DEX VCB does not contain swap 2" | ||
); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.