Skip to content

Commit

Permalink
Save unbonding token metadata when it's needed
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepinho committed Apr 6, 2024
1 parent 69e02d5 commit 23d0ef1
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 9 deletions.
2 changes: 1 addition & 1 deletion apps/extension/.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

PRAX=lkpmkhpnhknhmibgnmmhdhgdilepfghe
IDB_VERSION=32
IDB_VERSION=33
USDC_ASSET_ID="reum7wQmk/owgvGMWMZn/6RFPV24zIKq3W6In/WwZgg="
MINIFRONT_URL=https://app.testnet.penumbra.zone/
PENUMBRA_NODE_PD_URL=https://grpc.testnet.penumbra.zone/
4 changes: 2 additions & 2 deletions apps/extension/src/state/tx-approval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ export const createTxApprovalSlice = (): SliceCreator<TxApprovalSlice> => (set,
const getMetadata = async (assetId: AssetId) => {
try {
const { denomMetadata } = await viewClient.assetMetadataById({ assetId });
return denomMetadata ?? new Metadata();
return denomMetadata ?? new Metadata({ penumbraAssetId: assetId });
} catch {
return new Metadata();
return new Metadata({ penumbraAssetId: assetId });
}
};

Expand Down
5 changes: 4 additions & 1 deletion packages/storage/src/indexed-db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ export class IndexedDb implements IndexedDbInterface {

const instance = new this(db, new IbdUpdater(db), constants, chainId);
await instance.saveLocalAssetsMetadata(); // Pre-load asset metadata
await instance.addEpoch(0n); // Create first epoch

const existing0thEpoch = await instance.getEpochByHeight(0n);
if (!existing0thEpoch) await instance.addEpoch(0n); // Create first epoch

return instance;
}
close(): void {
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/indexed-db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,5 @@ export const IDB_TABLES: Tables = {
prices: 'PRICES',
validator_infos: 'VALIDATOR_INFOS',
transactions: 'TRANSACTIONS',
full_sync_height: 'FULL_SYNC_HEIGHT',
};
1 change: 1 addition & 0 deletions packages/wasm/crate/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/wasm/crate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ hex = "0.4.3"
indexed_db_futures = "0.4.1"
prost = "0.12.3"
rand_core = { version = "0.6.4", features = ["getrandom"] }
regex = { version = "1.8.1" }
serde = { version = "1.0.197", features = ["derive"] }
serde-wasm-bindgen = "0.6.5"
thiserror = "1.0"
Expand Down
1 change: 1 addition & 0 deletions packages/wasm/crate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod build;
pub mod dex;
pub mod error;
pub mod keys;
pub mod metadata;
pub mod note_record;
pub mod planner;
pub mod storage;
Expand Down
47 changes: 47 additions & 0 deletions packages/wasm/crate/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use penumbra_proto::core::asset::v1::Metadata;
use regex::Regex;

pub static UNBONDING_TOKEN_REGEX: &str = "^uunbonding_(?P<data>start_at_(?P<start>[0-9]+)_(?P<validator>penumbravalid1(?P<id>[a-zA-HJ-NP-Z0-9]+)))$";
pub static DELEGATION_TOKEN_REGEX: &str =
"^udelegation_(?P<data>penumbravalid1[a-zA-HJ-NP-Z0-9]+)$";
pub static SHORTENED_ID_LENGTH: usize = 8;

pub fn customize_symbol(metadata: Metadata) -> Metadata {
if let Some(unbonding_match) = Regex::new(UNBONDING_TOKEN_REGEX)
.expect("regex is valid")
.captures(&metadata.base)
{
let id_match = unbonding_match.name(&"id").unwrap().as_str();
let shortened_id = id_match
.chars()
.take(SHORTENED_ID_LENGTH)
.collect::<String>();
let start_match = unbonding_match.name(&"start").unwrap().as_str();

let customized = Metadata {
symbol: format!("unbondUMat{start_match}({shortened_id}…)"),
..metadata
};

return customized;
} else if let Some(delegation_match) = Regex::new(DELEGATION_TOKEN_REGEX)
.expect("regex is valid")
.captures(&metadata.base)
{
let id_match = delegation_match.name(&"id").unwrap().as_str();
let shortened_id = id_match
.chars()
.take(SHORTENED_ID_LENGTH)
.collect::<String>();

// let customized = metadata.clone();
let customized = Metadata {
symbol: format!("delUM({shortened_id}…)"),
..metadata
};

return customized;
} else {
metadata
}
}
16 changes: 11 additions & 5 deletions packages/wasm/crate/src/planner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::BTreeMap;
use anyhow::anyhow;
use ark_ff::UniformRand;
use decaf377::{Fq, Fr};
use penumbra_asset::asset::Metadata;
use penumbra_asset::{asset, Balance, Value};
use penumbra_dex::swap_claim::SwapClaimPlan;
use penumbra_dex::{
Expand Down Expand Up @@ -31,6 +32,7 @@ use rand_core::OsRng;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::JsValue;

use crate::metadata::customize_symbol;
use crate::note_record::SpendableNoteRecord;
use crate::storage::IndexedDBStorage;
use crate::utils;
Expand Down Expand Up @@ -319,11 +321,15 @@ pub async fn plan_transaction(
let rate_data: RateData = rate_data
.ok_or_else(|| anyhow!("missing rate data in undelegation"))?
.try_into()?;
actions.push(
rate_data
.build_undelegate(epoch.into(), value.amount)
.into(),
);

let undelegate = rate_data.build_undelegate(epoch.into(), value.amount);

let metadata_proto = undelegate.unbonding_token().denom().to_proto();
let customized_metadata_proto = customize_symbol(metadata_proto);
let customized_metadata = Metadata::try_from(customized_metadata_proto)?;
storage.add_asset(&customized_metadata).await?;

actions.push(undelegate.into());
}

for tpr::UndelegateClaim {
Expand Down
26 changes: 26 additions & 0 deletions packages/wasm/crate/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub struct Tables {
pub gas_prices: String,
pub epochs: String,
pub transactions: String,
pub full_sync_height: String,
}

pub struct IndexedDBStorage {
Expand Down Expand Up @@ -176,6 +177,31 @@ impl IndexedDBStorage {
.transpose()?)
}

pub async fn add_asset(&self, metadata: &Metadata) -> WasmResult<()> {
let tx = self
.db
.transaction_on_one_with_mode(&self.constants.tables.assets, Readwrite)?;
let store = tx.object_store(&self.constants.tables.assets)?;
let metadata_js = serde_wasm_bindgen::to_value(&metadata.to_proto())?;

store.put_val_owned(&metadata_js)?;

Ok(())
}

pub async fn get_full_sync_height(&self) -> WasmResult<Option<u64>> {
let tx = self
.db
.transaction_on_one(&self.constants.tables.full_sync_height)?;
let store = tx.object_store(&self.constants.tables.full_sync_height)?;

Ok(store
.get_owned("height")?
.await?
.map(serde_wasm_bindgen::from_value)
.transpose()?)
}

pub async fn get_note(
&self,
commitment: &note::StateCommitment,
Expand Down

0 comments on commit 23d0ef1

Please sign in to comment.