Skip to content

Commit

Permalink
Support for latest penumbra
Browse files Browse the repository at this point in the history
  • Loading branch information
zbuc committed Jun 16, 2024
1 parent 87d0868 commit e0c9b5b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 46 deletions.
File renamed without changes.
22 changes: 9 additions & 13 deletions src/opt/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@ use directories::ProjectDirs;
use futures::TryStreamExt;
use penumbra_custody::soft_kms::SoftKms;
use penumbra_proto::{
custody::v1alpha1::{
custody_protocol_service_client::CustodyProtocolServiceClient,
custody_protocol_service_server::CustodyProtocolServiceServer,
},
view::v1alpha1::{
view_protocol_service_client::ViewProtocolServiceClient,
view_protocol_service_server::ViewProtocolServiceServer,
custody::v1::{
custody_service_client::CustodyServiceClient, custody_service_server::CustodyServiceServer,
},
view::v1::{view_service_client::ViewServiceClient, view_service_server::ViewServiceServer},
};
use penumbra_view::{ViewClient, ViewService};
use penumbra_view::{ViewClient, ViewServer};
use std::path::PathBuf;
use url::Url;

Expand Down Expand Up @@ -102,8 +98,7 @@ impl Serve {
let wallet =
Wallet::load(custody_file).context("Failed to load wallet from local custody file")?;
let soft_kms = SoftKms::new(wallet.spend_key.clone().into());
let custody =
CustodyProtocolServiceClient::new(CustodyProtocolServiceServer::new(soft_kms));
let custody = CustodyServiceClient::new(CustodyServiceServer::new(soft_kms));

let fvk = wallet.spend_key.full_viewing_key().clone();

Expand All @@ -113,11 +108,12 @@ impl Serve {
let view_storage =
penumbra_view::Storage::load_or_initialize(None::<&str>, &fvk, self.node.clone())
.await?;
let view_service = ViewService::new(view_storage, self.node.clone()).await?;

// Now build the view and custody clients, doing gRPC with ourselves
let mut view = ViewProtocolServiceClient::new(ViewProtocolServiceServer::new(view_service));
let view_server = ViewServer::new(view_storage, self.node.clone()).await?;
let mut view = ViewServiceClient::new(ViewServiceServer::new(view_server));

ViewClient::status_stream(&mut view, fvk.wallet_id())
ViewClient::status_stream(&mut view)
.await?
.try_collect::<Vec<_>>()
.await?;
Expand Down
49 changes: 16 additions & 33 deletions src/trader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{collections::BTreeMap, future, str::FromStr};
use anyhow::Context;
use binance::model::BookTickerEvent;
use futures::{StreamExt, TryStreamExt};
use penumbra_asset::asset::DenomMetadata;
use penumbra_asset::asset::Metadata;
use penumbra_custody::{AuthorizeRequest, CustodyClient};
use penumbra_dex::{
lp::{
Expand All @@ -12,11 +12,10 @@ use penumbra_dex::{
},
DirectedUnitPair,
};
use penumbra_fee::Fee;
use penumbra_keys::keys::{AddressIndex, FullViewingKey};
use penumbra_num::Amount;
use penumbra_proto::core::component::dex::v1alpha1::query_service_client::QueryServiceClient as DexQueryServiceClient;
use penumbra_proto::core::component::dex::v1alpha1::{
use penumbra_proto::core::component::dex::v1::query_service_client::QueryServiceClient as DexQueryServiceClient;
use penumbra_proto::core::component::dex::v1::{
LiquidityPositionsByPriceRequest, LiquidityPositionsRequest,
};
use penumbra_view::{Planner, ViewClient};
Expand Down Expand Up @@ -147,11 +146,7 @@ where
tracing::debug!("trader received event: {:?}", book_ticker_event);

// Only update positions for a given symbol at most once per block
let current_height = self
.view
.status(self.fvk.wallet_id())
.await?
.partial_sync_height;
let current_height = self.view.status().await?.partial_sync_height;
if let Some(last_updated_height) = self.last_updated_height.get(symbol) {
if current_height <= *last_updated_height {
tracing::debug!(?symbol, "skipping symbol, already updated this block");
Expand Down Expand Up @@ -258,18 +253,15 @@ where
}

async fn finalize_and_submit(&mut self, planner: &mut Planner<OsRng>) -> anyhow::Result<()> {
// Pay no fee for the transaction.
let fee = Fee::from_staking_token_amount(0u32.into());

// Sometimes building the plan can fail with an error, because there were no actions
// present. There's not an easy way to check this in the planner API right now.

// Fetch latest gas prices.
let gas_prices = self.view.gas_prices().await?;

planner.set_gas_prices(gas_prices);
let plan = planner
.fee(fee)
.plan(
&mut self.view,
self.fvk.wallet_id(),
AddressIndex::from(self.account),
)
.plan(&mut self.view, AddressIndex::from(self.account))
.await?;

// 2. Authorize and build the transaction.
Expand All @@ -283,7 +275,7 @@ where
.data
.ok_or_else(|| anyhow::anyhow!("no auth data"))?
.try_into()?;
let witness_data = self.view.witness(self.fvk.wallet_id(), &plan).await?;
let witness_data = self.view.witness(&plan).await?;
let tx = plan
.build_concurrent(&self.fvk, &witness_data, &auth_data)
.await?;
Expand Down Expand Up @@ -410,12 +402,9 @@ where
market: &DirectedUnitPair,
) -> anyhow::Result<Vec<Position>> {
// We need to use the list of our notes to determine which positions we own.
let notes = self
.view
.unspent_notes_by_address_and_asset(self.fvk.wallet_id())
.await?;
let notes = self.view.unspent_notes_by_address_and_asset().await?;

fn is_closed_position_nft(denom: &DenomMetadata) -> bool {
fn is_closed_position_nft(denom: &Metadata) -> bool {
let prefix = "lpnft_closed_".to_string();

denom.starts_with(&prefix)
Expand Down Expand Up @@ -476,12 +465,9 @@ where
market: &DirectedUnitPair,
) -> anyhow::Result<Vec<Position>> {
// We need to use the list of our notes to determine which positions we own.
let notes = self
.view
.unspent_notes_by_address_and_asset(self.fvk.wallet_id())
.await?;
let notes = self.view.unspent_notes_by_address_and_asset().await?;

fn is_opened_position_nft(denom: &DenomMetadata) -> bool {
fn is_opened_position_nft(denom: &Metadata) -> bool {
let prefix = "lpnft_opened_".to_string();

denom.starts_with(&prefix)
Expand Down Expand Up @@ -659,10 +645,7 @@ where
// We could do this outside of the loop, but checking here
// assures we have the latest data, and the in-memory gRPC interface
// should be fast.
let notes = self
.view
.unspent_notes_by_address_and_asset(self.fvk.wallet_id())
.await?;
let notes = self.view.unspent_notes_by_address_and_asset().await?;

let (mut reserves_1, mut reserves_2) = (Amount::from(0u32), Amount::from(0u32));

Expand Down

0 comments on commit e0c9b5b

Please sign in to comment.