Skip to content

Commit

Permalink
view server auction stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
TalDerei committed Apr 22, 2024
1 parent b7da9ec commit 2418bba
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 14 deletions.
35 changes: 24 additions & 11 deletions crates/bin/pcli/src/command/query.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
use anyhow::{anyhow, Context, Result};

mod auction;
mod chain;
mod community_pool;
mod dex;
mod governance;
mod ibc_query;
mod shielded_pool;
use colored_json::ToColoredJson;
use shielded_pool::ShieldedPool;
mod tx;
use tx::Tx;
mod chain;
mod validator;

use auction::AuctionCmd;
use chain::ChainCmd;
mod dex;
use colored_json::ToColoredJson;
use community_pool::CommunityPoolCmd;
use dex::DexCmd;
mod governance;
use governance::GovernanceCmd;
mod community_pool;
use community_pool::CommunityPoolCmd;
mod validator;
pub(super) use validator::ValidatorCmd;
mod ibc_query;
use ibc_query::IbcCmd;
use shielded_pool::ShieldedPool;
use tx::Tx;
pub(super) use validator::ValidatorCmd;

use crate::App;

Expand Down Expand Up @@ -75,6 +78,9 @@ pub enum QueryCmd {
#[clap(long, default_value = "")]
nv_key_regex: String,
},
/// Queries information about a Dutch auction.
#[clap(subcommand)]
Auction(AuctionCmd),
}

impl QueryCmd {
Expand Down Expand Up @@ -116,6 +122,10 @@ impl QueryCmd {
return ibc.exec(app).await;
}

if let QueryCmd::Auction(AuctionCmd::Dutch(auction)) = self {
return auction.exec(app).await;
}

// TODO: this is a hack; we should replace all raw state key uses with RPC methods.
if let QueryCmd::ShieldedPool(ShieldedPool::CompactBlock { height }) = self {
use penumbra_proto::core::component::compact_block::v1::{
Expand Down Expand Up @@ -143,6 +153,7 @@ impl QueryCmd {
| QueryCmd::Governance(_)
| QueryCmd::CommunityPool(_)
| QueryCmd::Watch { .. }
| QueryCmd::Auction { .. }
| QueryCmd::Ibc(_) => {
unreachable!("query handled in guard");
}
Expand Down Expand Up @@ -181,6 +192,7 @@ impl QueryCmd {
| QueryCmd::Governance { .. }
| QueryCmd::Key { .. }
| QueryCmd::Watch { .. }
| QueryCmd::Auction { .. }
| QueryCmd::Ibc(_) => true,
}
}
Expand All @@ -198,6 +210,7 @@ impl QueryCmd {
| QueryCmd::Governance { .. }
| QueryCmd::CommunityPool { .. }
| QueryCmd::Watch { .. }
| QueryCmd::Auction { .. }
| QueryCmd::Ibc(_) => {
unreachable!("query is special cased")
}
Expand Down
52 changes: 52 additions & 0 deletions crates/bin/pcli/src/command/query/auction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use crate::App;
use anyhow::Context;
use clap::Subcommand;
use penumbra_asset::{asset, Value};
use penumbra_auction::auction::AuctionId;
use penumbra_keys::keys::AddressIndex;
use penumbra_num::Amount;
use penumbra_proto::view::v1::GasPricesRequest;
use penumbra_view::ViewClient;
use penumbra_wallet::plan::Planner;
use rand_core::OsRng;

#[derive(Debug, Subcommand)]
pub enum AuctionCmd {
/// Commands related to Dutch auctions
#[clap(display_order = 100, subcommand)]
Dutch(DutchCmd),
}

/// Commands related to querying Dutch auctions.
#[derive(Debug, Subcommand)]
pub enum DutchCmd {
/// Withdraws the reserves of the Dutch auction.
#[clap(display_order = 100, name = "id")]
DutchAuctionQueryId {
/// The auction to withdraw funds from.
#[clap(long, display_order = 100)]
auction_id: String,
},
}

impl DutchCmd {
/// Process the command by performing the appropriate action.
pub async fn exec(&self, app: &mut App) -> anyhow::Result<()> {
match self {
DutchCmd::DutchAuctionQueryId { auction_id } => {
let auction_id = auction_id.parse::<AuctionId>()?;

// Query stateful auction information from view server
let client = app.view().auctions_by_id(auction_id).await?;

let dutch_auction = &client[0];

println!(
"Dutch auction description and state: {:?} and {:?}",
dutch_auction.description, dutch_auction.state
);
}
}
Ok(())
}
}
34 changes: 32 additions & 2 deletions crates/view/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::{collections::BTreeMap, future::Future, pin::Pin};

use anyhow::Result;
use futures::{FutureExt, Stream, StreamExt, TryStreamExt};
use penumbra_auction::auction::{dutch::{DutchAuction, DutchAuctionState}, AuctionId};
use penumbra_tct::Position;
use tonic::{codegen::Bytes, Streaming};
use tracing::instrument;

Expand All @@ -17,10 +19,10 @@ use penumbra_dex::{
use penumbra_fee::GasPrices;
use penumbra_keys::{keys::AddressIndex, Address};
use penumbra_num::Amount;
use penumbra_proto::view::v1::{
use penumbra_proto::{core::component::auction, serializers::bech32str::auction_id, view::v1::{
self as pb, view_service_client::ViewServiceClient, BalancesResponse,
BroadcastTransactionResponse, WitnessRequest,
};
}};
use penumbra_sct::Nullifier;
use penumbra_shielded_pool::{fmd, note};
use penumbra_stake::IdentityKey;
Expand Down Expand Up @@ -306,6 +308,12 @@ pub trait ViewClient {
fn unclaimed_swaps(
&mut self,
) -> Pin<Box<dyn Future<Output = Result<Vec<SwapRecord>>> + Send + 'static>>;

/// Queries for auctions controlled by the user's wallet.
fn auctions_by_index(&mut self, address_index: AddressIndex) -> Pin<Box<dyn Future<Output = Result<Vec<(AuctionId, SpendableNoteRecord, DutchAuctionState, Position)>>> + Send + 'static>>;

/// Queries for auctions controlled by the user's wallet.
fn auctions_by_id(&mut self, auction_id: AuctionId) -> Pin<Box<dyn Future<Output = Result<Vec<DutchAuction>>> + Send + 'static>>;
}

// We need to tell `async_trait` not to add a `Send` bound to the boxed
Expand Down Expand Up @@ -915,4 +923,26 @@ where
}
.boxed()
}

fn auctions_by_index(
&mut self,
address_index: AddressIndex
) -> Pin<Box<dyn Future<Output = Result<Vec<(AuctionId, SpendableNoteRecord, DutchAuctionState, Position)>>> + Send + 'static>> {
let _ = address_index;
let _request = pb::AuctionsRequest {
account_filter: todo!(),
include_inactive: todo!(),
query_latest_state: todo!(),
};

// TODO: craft a tonic request and handle responses.
}

fn auctions_by_id(
&mut self,
_auction_id: AuctionId
) -> Pin<Box<dyn Future<Output = Result<Vec<DutchAuction>>> + Send + 'static>> {
// TODO: fill in stub for `pcli query auction id AUCTION_ID` (#4243)
todo!();
}
}
17 changes: 16 additions & 1 deletion crates/view/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use async_stream::try_stream;
use camino::Utf8Path;
use decaf377::Fq;
use futures::stream::{StreamExt, TryStreamExt};
use penumbra_auction::auction;
use rand::Rng;
use rand_core::OsRng;
use tokio::sync::{watch, RwLock};
Expand Down Expand Up @@ -394,7 +395,21 @@ impl ViewService for ViewServer {
&self,
_request: tonic::Request<pb::AuctionsRequest>,
) -> Result<tonic::Response<Self::AuctionsStream>, tonic::Status> {
unimplemented!("auctions")
let _auction = self.storage.auction_by_address().await.map_err(|_| {
tonic::Status::failed_precondition("Error retrieving auction information.")
})?;

let stream = try_stream! {
yield pb::AuctionsResponse { id: todo!(), note_record: todo!(), auction: todo!(), positions: todo!() }
};

Ok(tonic::Response::new(
stream
.map_err(|e: anyhow::Error| {
tonic::Status::unavailable(format!("error getting auction: {e}"))
})
.boxed(),
))
}

async fn broadcast_transaction(
Expand Down
9 changes: 9 additions & 0 deletions crates/view/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use camino::Utf8Path;
use decaf377::{FieldExt, Fq};
use once_cell::sync::Lazy;
use parking_lot::Mutex;
use penumbra_auction::auction::dutch::DutchAuctionState;
use penumbra_auction::auction::AuctionId;
use r2d2_sqlite::{
rusqlite::{OpenFlags, OptionalExtension},
SqliteConnectionManager,
Expand Down Expand Up @@ -1592,4 +1594,11 @@ impl Storage {

Ok(records)
}

pub async fn auction_by_address(
&self,
) -> anyhow::Result<Vec<(AuctionId, SpendableNoteRecord, DutchAuctionState, Position)>> {
// TODO: Add a sqlite table to track auction state/
todo!()
}
}

0 comments on commit 2418bba

Please sign in to comment.