-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
asset: add proto and domain support for equivalent values
I added the `EquivalentPrice` to the asset proto package rather than solely in the TxP, because I realized that the use cases we care about most immediately (having equivalent prices for balances) don't actually involve `TransactionView`s at all. While doing that I realized that in the context of viewing a _transaction_, it doesn't really make sense to just have an "equivalent value" on its own, so I added an `as_of_height` to allow indicating when the prices were relevant. We probably shouldn't try to expose this in the transaction view just yet, since it's a little unclear how we'd want to indicate historical prices.
- Loading branch information
1 parent
9e22cf2
commit 1e19b9b
Showing
17 changed files
with
801 additions
and
185 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
Binary file not shown.
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,49 @@ | ||
use crate::asset::Metadata; | ||
use penumbra_num::Amount; | ||
use penumbra_proto::{penumbra::core::asset::v1 as pb, DomainType}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// An equivalent value in terms of a different numeraire. | ||
/// | ||
/// This is used within | ||
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)] | ||
#[serde(try_from = "pb::EquivalentValue", into = "pb::EquivalentValue")] | ||
pub struct EquivalentValue { | ||
/// The equivalent amount of the parent [`Value`] in terms of the numeraire. | ||
pub equivalent_amount: Amount, | ||
/// Metadata describing the numeraire. | ||
pub numeraire: Metadata, | ||
/// If nonzero, gives some idea of when the equivalent value was estimated (in terms of block height). | ||
pub as_of_height: u64, | ||
} | ||
|
||
impl DomainType for EquivalentValue { | ||
type Proto = pb::EquivalentValue; | ||
} | ||
|
||
impl From<EquivalentValue> for pb::EquivalentValue { | ||
fn from(v: EquivalentValue) -> Self { | ||
pb::EquivalentValue { | ||
equivalent_amount: Some(v.equivalent_amount.into()), | ||
numeraire: Some(v.numeraire.into()), | ||
as_of_height: v.as_of_height, | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<pb::EquivalentValue> for EquivalentValue { | ||
type Error = anyhow::Error; | ||
fn try_from(value: pb::EquivalentValue) -> Result<Self, Self::Error> { | ||
Ok(EquivalentValue { | ||
equivalent_amount: value | ||
.equivalent_amount | ||
.ok_or_else(|| anyhow::anyhow!("missing equivalent_amount field"))? | ||
.try_into()?, | ||
numeraire: value | ||
.numeraire | ||
.ok_or_else(|| anyhow::anyhow!("missing numeraire field"))? | ||
.try_into()?, | ||
as_of_height: value.as_of_height, | ||
}) | ||
} | ||
} |
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,57 @@ | ||
use crate::asset; | ||
use penumbra_proto::{penumbra::core::asset::v1 as pb, DomainType}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
/// The estimated price of one asset in terms of another. | ||
/// | ||
/// This is used to generate an [`EquivalentValue`](crate::EquivalentValue) | ||
/// that may be helpful in interpreting a [`Value`](crate::Value). | ||
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)] | ||
#[serde(try_from = "pb::EstimatedPrice", into = "pb::EstimatedPrice")] | ||
|
||
pub struct EstimatedPrice { | ||
/// The asset that is being priced. | ||
pub priced_asset: asset::Id, | ||
/// The numeraire that the price is being expressed in. | ||
pub numeraire: asset::Id, | ||
/// Multiply units of the priced asset by this number to get the value in the numeraire. | ||
/// | ||
/// This is a floating-point number since the price is approximate. | ||
pub numeraire_per_unit: f64, | ||
/// If nonzero, gives some idea of when the price was estimated (in terms of block height). | ||
pub as_of_height: u64, | ||
} | ||
|
||
impl DomainType for EstimatedPrice { | ||
type Proto = pb::EstimatedPrice; | ||
} | ||
|
||
impl From<EstimatedPrice> for pb::EstimatedPrice { | ||
fn from(msg: EstimatedPrice) -> Self { | ||
Self { | ||
priced_asset: Some(msg.priced_asset.into()), | ||
numeraire: Some(msg.numeraire.into()), | ||
numeraire_per_unit: msg.numeraire_per_unit, | ||
as_of_height: msg.as_of_height, | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<pb::EstimatedPrice> for EstimatedPrice { | ||
type Error = anyhow::Error; | ||
|
||
fn try_from(msg: pb::EstimatedPrice) -> Result<Self, Self::Error> { | ||
Ok(Self { | ||
priced_asset: msg | ||
.priced_asset | ||
.ok_or_else(|| anyhow::anyhow!("missing priced asset"))? | ||
.try_into()?, | ||
numeraire: msg | ||
.numeraire | ||
.ok_or_else(|| anyhow::anyhow!("missing numeraire"))? | ||
.try_into()?, | ||
numeraire_per_unit: msg.numeraire_per_unit, | ||
as_of_height: msg.as_of_height, | ||
}) | ||
} | ||
} |
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.