Skip to content

Commit

Permalink
Add ability to query for balances by account and asset type
Browse files Browse the repository at this point in the history
  • Loading branch information
aubrika committed Jul 7, 2023
1 parent 3beed38 commit 21c1a0b
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 125 deletions.
65 changes: 36 additions & 29 deletions crates/proto/src/gen/penumbra.view.v1alpha1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,27 @@ pub struct EphemeralAddressResponse {
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct BalanceByAddressRequest {
pub struct BalancesRequest {
/// If present, filter balances to only include the account specified by the `AddressIndex`.
#[prost(message, optional, tag = "1")]
pub address: ::core::option::Option<super::super::core::crypto::v1alpha1::Address>,
pub account_filter: ::core::option::Option<
super::super::core::crypto::v1alpha1::AddressIndex,
>,
/// If present, filter balances to only include the specified asset ID.
#[prost(message, optional, tag = "2")]
pub asset_id_filter: ::core::option::Option<
super::super::core::crypto::v1alpha1::AssetId,
>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct BalanceByAddressResponse {
pub struct BalancesResponse {
#[prost(message, optional, tag = "1")]
pub asset: ::core::option::Option<super::super::core::crypto::v1alpha1::AssetId>,
pub account: ::core::option::Option<
super::super::core::crypto::v1alpha1::AddressIndex,
>,
#[prost(message, optional, tag = "2")]
pub amount: ::core::option::Option<super::super::core::crypto::v1alpha1::Amount>,
pub balance: ::core::option::Option<super::super::core::crypto::v1alpha1::Value>,
}
/// Scaffolding for bearer-token authentication for the ViewService.
#[allow(clippy::derive_partial_eq_without_eq)]
Expand Down Expand Up @@ -972,12 +982,12 @@ pub mod view_protocol_service_client {
self.inner.unary(request.into_request(), path, codec).await
}
/// Query for balance of a given address.
/// Returns a stream of `BalanceByAddressResponses`.
pub async fn balance_by_address(
/// Returns a stream of `BalancesResponses`.
pub async fn balances(
&mut self,
request: impl tonic::IntoRequest<super::BalanceByAddressRequest>,
request: impl tonic::IntoRequest<super::BalancesRequest>,
) -> Result<
tonic::Response<tonic::codec::Streaming<super::BalanceByAddressResponse>>,
tonic::Response<tonic::codec::Streaming<super::BalancesResponse>>,
tonic::Status,
> {
self.inner
Expand All @@ -991,7 +1001,7 @@ pub mod view_protocol_service_client {
})?;
let codec = tonic::codec::ProstCodec::default();
let path = http::uri::PathAndQuery::from_static(
"/penumbra.view.v1alpha1.ViewProtocolService/BalanceByAddress",
"/penumbra.view.v1alpha1.ViewProtocolService/Balances",
);
self.inner.server_streaming(request.into_request(), path, codec).await
}
Expand Down Expand Up @@ -1377,18 +1387,18 @@ pub mod view_protocol_service_server {
&self,
request: tonic::Request<super::EphemeralAddressRequest>,
) -> Result<tonic::Response<super::EphemeralAddressResponse>, tonic::Status>;
/// Server streaming response type for the BalanceByAddress method.
type BalanceByAddressStream: futures_core::Stream<
Item = Result<super::BalanceByAddressResponse, tonic::Status>,
/// Server streaming response type for the Balances method.
type BalancesStream: futures_core::Stream<
Item = Result<super::BalancesResponse, tonic::Status>,
>
+ Send
+ 'static;
/// Query for balance of a given address.
/// Returns a stream of `BalanceByAddressResponses`.
async fn balance_by_address(
/// Returns a stream of `BalancesResponses`.
async fn balances(
&self,
request: tonic::Request<super::BalanceByAddressRequest>,
) -> Result<tonic::Response<Self::BalanceByAddressStream>, tonic::Status>;
request: tonic::Request<super::BalancesRequest>,
) -> Result<tonic::Response<Self::BalancesStream>, tonic::Status>;
/// Query for a note by its note commitment, optionally waiting until the note is detected.
async fn note_by_commitment(
&self,
Expand Down Expand Up @@ -1987,28 +1997,25 @@ pub mod view_protocol_service_server {
};
Box::pin(fut)
}
"/penumbra.view.v1alpha1.ViewProtocolService/BalanceByAddress" => {
"/penumbra.view.v1alpha1.ViewProtocolService/Balances" => {
#[allow(non_camel_case_types)]
struct BalanceByAddressSvc<T: ViewProtocolService>(pub Arc<T>);
struct BalancesSvc<T: ViewProtocolService>(pub Arc<T>);
impl<
T: ViewProtocolService,
> tonic::server::ServerStreamingService<
super::BalanceByAddressRequest,
> for BalanceByAddressSvc<T> {
type Response = super::BalanceByAddressResponse;
type ResponseStream = T::BalanceByAddressStream;
> tonic::server::ServerStreamingService<super::BalancesRequest>
for BalancesSvc<T> {
type Response = super::BalancesResponse;
type ResponseStream = T::BalancesStream;
type Future = BoxFuture<
tonic::Response<Self::ResponseStream>,
tonic::Status,
>;
fn call(
&mut self,
request: tonic::Request<super::BalanceByAddressRequest>,
request: tonic::Request<super::BalancesRequest>,
) -> Self::Future {
let inner = self.0.clone();
let fut = async move {
(*inner).balance_by_address(request).await
};
let fut = async move { (*inner).balances(request).await };
Box::pin(fut)
}
}
Expand All @@ -2017,7 +2024,7 @@ pub mod view_protocol_service_server {
let inner = self.inner.clone();
let fut = async move {
let inner = inner.0;
let method = BalanceByAddressSvc(inner);
let method = BalancesSvc(inner);
let codec = tonic::codec::ProstCodec::default();
let mut grpc = tonic::server::Grpc::new(codec)
.apply_compression_config(
Expand Down
123 changes: 71 additions & 52 deletions crates/proto/src/gen/penumbra.view.v1alpha1.serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,37 +691,47 @@ impl<'de> serde::Deserialize<'de> for AuthorizeAndBuildResponse {
deserializer.deserialize_struct("penumbra.view.v1alpha1.AuthorizeAndBuildResponse", FIELDS, GeneratedVisitor)
}
}
impl serde::Serialize for BalanceByAddressRequest {
impl serde::Serialize for BalancesRequest {
#[allow(deprecated)]
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
use serde::ser::SerializeStruct;
let mut len = 0;
if self.address.is_some() {
if self.account_filter.is_some() {
len += 1;
}
let mut struct_ser = serializer.serialize_struct("penumbra.view.v1alpha1.BalanceByAddressRequest", len)?;
if let Some(v) = self.address.as_ref() {
struct_ser.serialize_field("address", v)?;
if self.asset_id_filter.is_some() {
len += 1;
}
let mut struct_ser = serializer.serialize_struct("penumbra.view.v1alpha1.BalancesRequest", len)?;
if let Some(v) = self.account_filter.as_ref() {
struct_ser.serialize_field("accountFilter", v)?;
}
if let Some(v) = self.asset_id_filter.as_ref() {
struct_ser.serialize_field("assetIdFilter", v)?;
}
struct_ser.end()
}
}
impl<'de> serde::Deserialize<'de> for BalanceByAddressRequest {
impl<'de> serde::Deserialize<'de> for BalancesRequest {
#[allow(deprecated)]
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
const FIELDS: &[&str] = &[
"address",
"account_filter",
"accountFilter",
"asset_id_filter",
"assetIdFilter",
];

#[allow(clippy::enum_variant_names)]
enum GeneratedField {
Address,
AccountFilter,
AssetIdFilter,
}
impl<'de> serde::Deserialize<'de> for GeneratedField {
fn deserialize<D>(deserializer: D) -> std::result::Result<GeneratedField, D::Error>
Expand All @@ -743,7 +753,8 @@ impl<'de> serde::Deserialize<'de> for BalanceByAddressRequest {
E: serde::de::Error,
{
match value {
"address" => Ok(GeneratedField::Address),
"accountFilter" | "account_filter" => Ok(GeneratedField::AccountFilter),
"assetIdFilter" | "asset_id_filter" => Ok(GeneratedField::AssetIdFilter),
_ => Err(serde::de::Error::unknown_field(value, FIELDS)),
}
}
Expand All @@ -753,74 +764,82 @@ impl<'de> serde::Deserialize<'de> for BalanceByAddressRequest {
}
struct GeneratedVisitor;
impl<'de> serde::de::Visitor<'de> for GeneratedVisitor {
type Value = BalanceByAddressRequest;
type Value = BalancesRequest;

fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("struct penumbra.view.v1alpha1.BalanceByAddressRequest")
formatter.write_str("struct penumbra.view.v1alpha1.BalancesRequest")
}

fn visit_map<V>(self, mut map: V) -> std::result::Result<BalanceByAddressRequest, V::Error>
fn visit_map<V>(self, mut map: V) -> std::result::Result<BalancesRequest, V::Error>
where
V: serde::de::MapAccess<'de>,
{
let mut address__ = None;
let mut account_filter__ = None;
let mut asset_id_filter__ = None;
while let Some(k) = map.next_key()? {
match k {
GeneratedField::Address => {
if address__.is_some() {
return Err(serde::de::Error::duplicate_field("address"));
GeneratedField::AccountFilter => {
if account_filter__.is_some() {
return Err(serde::de::Error::duplicate_field("accountFilter"));
}
address__ = map.next_value()?;
account_filter__ = map.next_value()?;
}
GeneratedField::AssetIdFilter => {
if asset_id_filter__.is_some() {
return Err(serde::de::Error::duplicate_field("assetIdFilter"));
}
asset_id_filter__ = map.next_value()?;
}
}
}
Ok(BalanceByAddressRequest {
address: address__,
Ok(BalancesRequest {
account_filter: account_filter__,
asset_id_filter: asset_id_filter__,
})
}
}
deserializer.deserialize_struct("penumbra.view.v1alpha1.BalanceByAddressRequest", FIELDS, GeneratedVisitor)
deserializer.deserialize_struct("penumbra.view.v1alpha1.BalancesRequest", FIELDS, GeneratedVisitor)
}
}
impl serde::Serialize for BalanceByAddressResponse {
impl serde::Serialize for BalancesResponse {
#[allow(deprecated)]
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
use serde::ser::SerializeStruct;
let mut len = 0;
if self.asset.is_some() {
if self.account.is_some() {
len += 1;
}
if self.amount.is_some() {
if self.balance.is_some() {
len += 1;
}
let mut struct_ser = serializer.serialize_struct("penumbra.view.v1alpha1.BalanceByAddressResponse", len)?;
if let Some(v) = self.asset.as_ref() {
struct_ser.serialize_field("asset", v)?;
let mut struct_ser = serializer.serialize_struct("penumbra.view.v1alpha1.BalancesResponse", len)?;
if let Some(v) = self.account.as_ref() {
struct_ser.serialize_field("account", v)?;
}
if let Some(v) = self.amount.as_ref() {
struct_ser.serialize_field("amount", v)?;
if let Some(v) = self.balance.as_ref() {
struct_ser.serialize_field("balance", v)?;
}
struct_ser.end()
}
}
impl<'de> serde::Deserialize<'de> for BalanceByAddressResponse {
impl<'de> serde::Deserialize<'de> for BalancesResponse {
#[allow(deprecated)]
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
const FIELDS: &[&str] = &[
"asset",
"amount",
"account",
"balance",
];

#[allow(clippy::enum_variant_names)]
enum GeneratedField {
Asset,
Amount,
Account,
Balance,
}
impl<'de> serde::Deserialize<'de> for GeneratedField {
fn deserialize<D>(deserializer: D) -> std::result::Result<GeneratedField, D::Error>
Expand All @@ -842,8 +861,8 @@ impl<'de> serde::Deserialize<'de> for BalanceByAddressResponse {
E: serde::de::Error,
{
match value {
"asset" => Ok(GeneratedField::Asset),
"amount" => Ok(GeneratedField::Amount),
"account" => Ok(GeneratedField::Account),
"balance" => Ok(GeneratedField::Balance),
_ => Err(serde::de::Error::unknown_field(value, FIELDS)),
}
}
Expand All @@ -853,41 +872,41 @@ impl<'de> serde::Deserialize<'de> for BalanceByAddressResponse {
}
struct GeneratedVisitor;
impl<'de> serde::de::Visitor<'de> for GeneratedVisitor {
type Value = BalanceByAddressResponse;
type Value = BalancesResponse;

fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("struct penumbra.view.v1alpha1.BalanceByAddressResponse")
formatter.write_str("struct penumbra.view.v1alpha1.BalancesResponse")
}

fn visit_map<V>(self, mut map: V) -> std::result::Result<BalanceByAddressResponse, V::Error>
fn visit_map<V>(self, mut map: V) -> std::result::Result<BalancesResponse, V::Error>
where
V: serde::de::MapAccess<'de>,
{
let mut asset__ = None;
let mut amount__ = None;
let mut account__ = None;
let mut balance__ = None;
while let Some(k) = map.next_key()? {
match k {
GeneratedField::Asset => {
if asset__.is_some() {
return Err(serde::de::Error::duplicate_field("asset"));
GeneratedField::Account => {
if account__.is_some() {
return Err(serde::de::Error::duplicate_field("account"));
}
asset__ = map.next_value()?;
account__ = map.next_value()?;
}
GeneratedField::Amount => {
if amount__.is_some() {
return Err(serde::de::Error::duplicate_field("amount"));
GeneratedField::Balance => {
if balance__.is_some() {
return Err(serde::de::Error::duplicate_field("balance"));
}
amount__ = map.next_value()?;
balance__ = map.next_value()?;
}
}
}
Ok(BalanceByAddressResponse {
asset: asset__,
amount: amount__,
Ok(BalancesResponse {
account: account__,
balance: balance__,
})
}
}
deserializer.deserialize_struct("penumbra.view.v1alpha1.BalanceByAddressResponse", FIELDS, GeneratedVisitor)
deserializer.deserialize_struct("penumbra.view.v1alpha1.BalancesResponse", FIELDS, GeneratedVisitor)
}
}
impl serde::Serialize for BroadcastTransactionRequest {
Expand Down
4 changes: 2 additions & 2 deletions crates/proto/src/gen/proto_descriptor.bin
Git LFS file not shown
Loading

0 comments on commit 21c1a0b

Please sign in to comment.