-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate unbonding token metadata on the fly for the approval dialog #896
Changes from 1 commit
23d0ef1
1acb6c0
3ae8218
3bd087b
6e784da
e18492e
efbdf3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 }); | ||
Comment on lines
+69
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not strictly needed for this PR, but it should have been like this already. |
||
} | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
Comment on lines
+122
to
+123
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hadn't realized that this code was run every time the extension was restarted, so I added a check to make sure the 0th epoch doesn't exist before creating it. |
||
|
||
return instance; | ||
} | ||
close(): void { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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]+)$"; | ||
jessepinho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub static SHORTENED_ID_LENGTH: usize = 8; | ||
|
||
pub fn customize_symbol(metadata: Metadata) -> Metadata { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the exact same functionality as our TypeScript I briefly spiked on just calling out to this Rust function from our TypeScript code, rather than having one copy of this function on each of the Rust and TypeScript sides. However, that would mean modifying this function to take a Not sure what the best way forward for this is — reviewers, thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two things:
What do you think of this? // along with other errors in error.rs...
#[derive(Error, Debug)]
pub enum WasmError {
// ...
#[error("{0}")]
RegexError(#[from] regex::Error),
}
#[wasm_bindgen]
pub fn customize_symbol(metadata_bytes: &[u8]) -> WasmResult<Vec<u8>> {
utils::set_panic_hook();
let metadata = Metadata::decode(metadata_bytes)?;
customize_symbol_inner(metadata)?;
Ok(metadata.encode_to_vec())
}
pub fn customize_symbol_inner(mut metadata: Metadata) -> WasmResult<Metadata> {
let unbonding_re = Regex::new(UNBONDING_TOKEN_REGEX)?;
let delegation_re = Regex::new(DELEGATION_TOKEN_REGEX)?;
if let Some(captures) = unbonding_re.captures(&metadata.base) {
let shortened_id = shorten_id(&captures)?;
let start_match = captures.name("start")
.ok_or_else(|| anyhow!("<start> not matched in unbonding token regex"))?
.as_str();
metadata.symbol = format!("unbondUMat{start_match}({shortened_id}...)");
} else if let Some(captures) = delegation_re.captures(&metadata.base) {
let shortened_id = shorten_id(&captures)?;
metadata.symbol = format!("delUM({shortened_id}...)");
}
Ok(metadata)
}
fn shorten_id(captures: ®ex::Captures) -> WasmResult<String> {
let id_match = captures.name("id").ok_or_else(|| anyhow!("<id> not matched in staking token regex"))?;
Ok(id_match.as_str()
.chars()
.take(SHORTENED_ID_LENGTH)
.collect())
} If you don't like mutating the input, this can be converted to just cloning the input and mutating that. |
||
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 | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needed for this fix