diff --git a/Cargo.lock b/Cargo.lock index a9dc81493d..1a0e78885d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1233,7 +1233,7 @@ dependencies = [ [[package]] name = "cnidarium" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "async-trait", @@ -1269,7 +1269,7 @@ dependencies = [ [[package]] name = "cnidarium-component" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "async-trait", @@ -1307,7 +1307,7 @@ dependencies = [ [[package]] name = "cometindex" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "async-trait", @@ -1668,7 +1668,7 @@ dependencies = [ [[package]] name = "decaf377-fmd" -version = "0.80.8" +version = "0.80.9" dependencies = [ "ark-ff", "ark-serialize", @@ -1683,7 +1683,7 @@ dependencies = [ [[package]] name = "decaf377-frost" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ff", @@ -1698,7 +1698,7 @@ dependencies = [ [[package]] name = "decaf377-ka" -version = "0.80.8" +version = "0.80.9" dependencies = [ "ark-ff", "decaf377", @@ -4214,7 +4214,7 @@ dependencies = [ [[package]] name = "pcli" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ff", @@ -4295,7 +4295,7 @@ dependencies = [ [[package]] name = "pclientd" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "assert_cmd", @@ -4347,7 +4347,7 @@ dependencies = [ [[package]] name = "pd" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ff", @@ -4500,7 +4500,7 @@ dependencies = [ [[package]] name = "penumbra-app" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ff", @@ -4589,7 +4589,7 @@ dependencies = [ [[package]] name = "penumbra-asset" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ff", @@ -4629,7 +4629,7 @@ dependencies = [ [[package]] name = "penumbra-auction" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ff", @@ -4684,7 +4684,7 @@ dependencies = [ [[package]] name = "penumbra-auto-https" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "axum-server", @@ -4696,7 +4696,7 @@ dependencies = [ [[package]] name = "penumbra-bench" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-bls12-377", @@ -4740,7 +4740,7 @@ dependencies = [ [[package]] name = "penumbra-community-pool" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ff", @@ -4772,7 +4772,7 @@ dependencies = [ [[package]] name = "penumbra-compact-block" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ff", @@ -4807,7 +4807,7 @@ dependencies = [ [[package]] name = "penumbra-custody" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "argon2", @@ -4843,7 +4843,7 @@ dependencies = [ [[package]] name = "penumbra-dex" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ff", @@ -4905,7 +4905,7 @@ dependencies = [ [[package]] name = "penumbra-distributions" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "async-trait", @@ -4923,7 +4923,7 @@ dependencies = [ [[package]] name = "penumbra-eddy" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ff", @@ -4941,7 +4941,7 @@ dependencies = [ [[package]] name = "penumbra-fee" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ff", @@ -4968,7 +4968,7 @@ dependencies = [ [[package]] name = "penumbra-funding" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "async-trait", @@ -4991,7 +4991,7 @@ dependencies = [ [[package]] name = "penumbra-governance" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ff", @@ -5045,7 +5045,7 @@ dependencies = [ [[package]] name = "penumbra-ibc" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ff", @@ -5082,7 +5082,7 @@ dependencies = [ [[package]] name = "penumbra-keys" -version = "0.80.8" +version = "0.80.9" dependencies = [ "aes", "anyhow", @@ -5129,7 +5129,7 @@ dependencies = [ [[package]] name = "penumbra-measure" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "bytesize", @@ -5147,7 +5147,7 @@ dependencies = [ [[package]] name = "penumbra-mock-client" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "cnidarium", @@ -5164,7 +5164,7 @@ dependencies = [ [[package]] name = "penumbra-mock-consensus" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "bytes", @@ -5184,7 +5184,7 @@ dependencies = [ [[package]] name = "penumbra-mock-tendermint-proxy" -version = "0.80.8" +version = "0.80.9" dependencies = [ "hex", "pbjson-types", @@ -5199,7 +5199,7 @@ dependencies = [ [[package]] name = "penumbra-num" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ff", @@ -5236,7 +5236,7 @@ dependencies = [ [[package]] name = "penumbra-parameter-setup" -version = "0.1.0" +version = "0.80.9" dependencies = [ "ark-groth16", "ark-serialize", @@ -5253,7 +5253,7 @@ dependencies = [ [[package]] name = "penumbra-proof-params" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ec", @@ -5281,7 +5281,7 @@ dependencies = [ [[package]] name = "penumbra-proof-setup" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ec", @@ -5308,7 +5308,7 @@ dependencies = [ [[package]] name = "penumbra-proto" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "async-trait", @@ -5342,7 +5342,7 @@ dependencies = [ [[package]] name = "penumbra-sct" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ff", @@ -5378,7 +5378,7 @@ dependencies = [ [[package]] name = "penumbra-shielded-pool" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ff", @@ -5432,7 +5432,7 @@ dependencies = [ [[package]] name = "penumbra-stake" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ff", @@ -5485,7 +5485,7 @@ dependencies = [ [[package]] name = "penumbra-tct" -version = "0.80.8" +version = "0.80.9" dependencies = [ "ark-ed-on-bls12-377", "ark-ff", @@ -5517,7 +5517,7 @@ dependencies = [ [[package]] name = "penumbra-tct-property-test" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "futures", @@ -5529,7 +5529,7 @@ dependencies = [ [[package]] name = "penumbra-tct-visualize" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "axum", @@ -5559,7 +5559,7 @@ dependencies = [ [[package]] name = "penumbra-tendermint-proxy" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "chrono", @@ -5591,7 +5591,7 @@ dependencies = [ [[package]] name = "penumbra-test-subscriber" -version = "0.80.8" +version = "0.80.9" dependencies = [ "tracing", "tracing-subscriber 0.3.18", @@ -5599,7 +5599,7 @@ dependencies = [ [[package]] name = "penumbra-tower-trace" -version = "0.80.8" +version = "0.80.9" dependencies = [ "futures", "hex", @@ -5620,7 +5620,7 @@ dependencies = [ [[package]] name = "penumbra-transaction" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-ff", @@ -5673,7 +5673,7 @@ dependencies = [ [[package]] name = "penumbra-txhash" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "blake2b_simd 1.0.2", @@ -5686,7 +5686,7 @@ dependencies = [ [[package]] name = "penumbra-view" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-std", @@ -5744,7 +5744,7 @@ dependencies = [ [[package]] name = "penumbra-wallet" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-std", @@ -5830,7 +5830,7 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pindexer" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "chrono", @@ -5925,7 +5925,7 @@ dependencies = [ [[package]] name = "pmonitor" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "assert_cmd", @@ -7724,7 +7724,7 @@ checksum = "734676eb262c623cec13c3155096e08d1f8f29adce39ba17948b18dad1e54142" [[package]] name = "summonerd" -version = "0.80.8" +version = "0.80.9" dependencies = [ "anyhow", "ark-groth16", diff --git a/Cargo.toml b/Cargo.toml index ce35a1ee9a..e2ba1a4492 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -105,7 +105,7 @@ push = false [workspace.package] authors = ["Penumbra Labs f64 { + (a * b).sqrt() + } + /// Candlestick data, unmoored from the prison of a particular block height. /// /// In other words, this can represent candlesticks which span arbitrary windows, @@ -57,6 +61,31 @@ mod candle { self.direct_volume += that.direct_volume; self.swap_volume += that.swap_volume; } + + /// Mix this candle with a candle going in the opposite direction of the pair. + pub fn mix(&mut self, op: &Self) { + // We use the geometric mean, resulting in all the prices in a.mix(b) being + // the inverse of the prices in b.mix(a), and the volumes being equal. + self.close /= geo_mean(self.close, op.close); + self.open /= geo_mean(self.open, op.open); + self.low = self.low.min(1.0 / op.low); + self.high = self.high.min(1.0 / op.high); + // Using the closing price to look backwards at volume. + self.direct_volume += op.direct_volume / self.close; + self.swap_volume += op.swap_volume / self.close; + } + + /// Flip this candle to get the equivalent in the other direction. + pub fn flip(&self) -> Self { + Self { + open: 1.0 / self.open, + close: 1.0 / self.close, + low: 1.0 / self.low, + high: 1.0 / self.high, + direct_volume: self.direct_volume / self.close, + swap_volume: self.swap_volume / self.close, + } + } } impl From for Candle { @@ -501,14 +530,15 @@ mod summary { SELECT asset_start, asset_end, (dex_ex_pairs_summary.price - greatest(price_then, 0.000001)) / greatest(price_then, 0.000001) * 100 AS price_change, - liquidity * eligible_denoms.price AS liquidity, - direct_volume_over_window * eligible_denoms.price as dv, - swap_volume_over_window * eligible_denoms.price as sv, + liquidity * ed_end.price AS liquidity, + direct_volume_over_window * ed_start.price AS dv, + swap_volume_over_window * ed_start.price AS sv, trades_over_window as trades FROM dex_ex_pairs_summary - JOIN eligible_denoms - ON eligible_denoms.asset = asset_end - WHERE the_window = $3 + JOIN eligible_denoms AS ed_end + ON ed_end.asset = asset_end + JOIN eligible_denoms AS ed_start + ON ed_start.asset = asset_start ), sums AS ( SELECT @@ -589,6 +619,26 @@ mod summary { } } +mod metadata { + use super::*; + + pub async fn set(dbtx: &mut PgTransaction<'_>, quote_asset: asset::Id) -> anyhow::Result<()> { + sqlx::query( + " + INSERT INTO dex_ex_metadata + VALUES (1, $1) + ON CONFLICT (id) DO UPDATE + SET id = EXCLUDED.id, + quote_asset_id = EXCLUDED.quote_asset_id + ", + ) + .bind(quote_asset.to_bytes()) + .execute(dbtx.as_mut()) + .await?; + Ok(()) + } +} + #[derive(Debug, Default, Clone, Copy)] struct PairMetrics { trades: f64, @@ -616,14 +666,20 @@ impl Events { } fn with_candle(&mut self, pair: DirectedTradingPair, candle: Candle) { - match self.candles.get_mut(&pair) { - None => { - self.candles.insert(pair, candle); - } - Some(current) => { - current.merge(&candle); + // Populate both this pair and the flipped pair, and if the flipped pair + // is already populated, we need to mix the two candles together. + let flip = pair.flip(); + let new_candle = match self.candles.get(&flip).cloned() { + None => candle, + Some(flipped) => { + let mut out = candle; + out.mix(&flipped); + out } - } + }; + + self.candles.insert(pair, new_candle); + self.candles.insert(flip, new_candle.flip()); } fn metric(&mut self, pair: &DirectedTradingPair) -> &mut PairMetrics { @@ -775,6 +831,7 @@ impl AppView for Component { dbtx: &mut PgTransaction, batch: EventBatch, ) -> Result<(), anyhow::Error> { + metadata::set(dbtx, self.denom).await?; let mut charts = HashMap::new(); let mut snapshots = HashMap::new(); let mut last_time = None; diff --git a/crates/bin/pindexer/src/dex_ex/schema.sql b/crates/bin/pindexer/src/dex_ex/schema.sql index 6de17b2be2..ddbf108887 100644 --- a/crates/bin/pindexer/src/dex_ex/schema.sql +++ b/crates/bin/pindexer/src/dex_ex/schema.sql @@ -76,3 +76,9 @@ CREATE TABLE IF NOT EXISTS dex_ex_aggregate_summary ( top_price_mover_end BYTEA NOT NULL, top_price_mover_change_percent FLOAT8 NOT NULL ); + +CREATE TABLE IF NOT EXISTS dex_ex_metadata ( + id INT PRIMARY KEY, + -- The asset id to use for prices in places such as the aggregate summary. + quote_asset_id BYTEA NOT NULL +); diff --git a/tools/parameter-setup/Cargo.toml b/tools/parameter-setup/Cargo.toml index 43b40629d0..19c2bf11e2 100644 --- a/tools/parameter-setup/Cargo.toml +++ b/tools/parameter-setup/Cargo.toml @@ -1,26 +1,22 @@ [package] name = "penumbra-parameter-setup" -version = "0.1.0" -edition = "2021" +authors = { workspace = true } +edition = { workspace = true } +homepage = { workspace = true } +license = { workspace = true } publish = false +repository = { workspace = true } +version = { workspace = true } [dependencies] -penumbra-proof-params = { path = "../../crates/crypto/proof-params" } -penumbra-proof-setup = { path = "../../crates/crypto/proof-setup", features = [ - "parallel", -] } -penumbra-dex = { path = "../../crates/core/component/dex/" } -penumbra-community-pool = { path = "../../crates/core/component/community-pool/", features = [ - "component", -] } -penumbra-governance = { path = "../../crates/core/component/governance/" } -penumbra-shielded-pool = { path = "../../crates/core/component/shielded-pool/", features = [ - "parallel", -] } -penumbra-stake = { path = "../../crates/core/component/stake/", features = [ - "component", -] } ark-groth16 = { workspace = true } ark-serialize = { workspace = true } decaf377 = { workspace = true, features = ["r1cs"] } +penumbra-community-pool = { workspace = true , features = ["component"] } +penumbra-dex = { workspace = true } +penumbra-governance = { workspace = true } +penumbra-proof-params = { workspace = true } +penumbra-proof-setup = { workspace = true, features = ["parallel"] } +penumbra-shielded-pool = { workspace = true, features = ["parallel"] } +penumbra-stake = { workspace = true, features = ["component"] } rand_core = { workspace = true }