Skip to content

Commit add089e

Browse files
committed
The MASP conversions query now requires a MASP epoch to be specified.
1 parent c10115c commit add089e

File tree

3 files changed

+59
-45
lines changed

3 files changed

+59
-45
lines changed

crates/apps_lib/src/client/rpc.rs

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,55 +2123,67 @@ pub async fn query_conversions(
21232123
.wallet()
21242124
.await
21252125
.get_addresses_with_vp_type(AddressVpType::Token);
2126-
let conversions = rpc::query_conversions(context.client())
2127-
.await
2128-
.expect("Conversions should be defined");
2129-
2130-
if args.dump_tree {
2131-
display_line!(context.io(), "Conversions: {conversions:?}");
2132-
}
2133-
2126+
// Determine the range of relevant MASP epochs
2127+
let epoch_range = if let Some(target_epoch) = &args.epoch {
2128+
MaspEpoch::iter_bounds_inclusive(*target_epoch, *target_epoch)
2129+
} else {
2130+
let from = MaspEpoch::zero();
2131+
let to = rpc::query_masp_epoch(context.client())
2132+
.await
2133+
.expect("Unable to query current MASP epoch");
2134+
MaspEpoch::iter_bounds_inclusive(from, to)
2135+
};
21342136
// Track whether any non-sentinel conversions are found
21352137
let mut conversions_found = false;
2136-
for (addr, _denom, digit, epoch, amt) in conversions.values() {
2137-
// If the user has specified any targets, then meet them
2138-
// If we have a sentinel conversion, then skip printing
2139-
if matches!(&target_token, Some(target) if target != addr)
2140-
|| matches!(&args.epoch, Some(target) if target != epoch)
2141-
|| amt.is_zero()
2142-
{
2143-
continue;
2138+
// Query the conversions for each relevant epoch
2139+
for epoch in epoch_range {
2140+
let conversions = rpc::query_conversions(context.client(), &epoch)
2141+
.await
2142+
.expect("Conversions should be defined");
2143+
2144+
if args.dump_tree {
2145+
display_line!(context.io(), "Conversions: {conversions:?}");
21442146
}
2145-
conversions_found = true;
2146-
// Print the asset to which the conversion applies
2147-
display!(
2148-
context.io(),
2149-
"{}*2^{}[{}]: ",
2150-
tokens.get(addr).cloned().unwrap_or_else(|| addr.clone()),
2151-
*digit as u8 * 64,
2152-
epoch,
2153-
);
2154-
// Now print out the components of the allowed conversion
2155-
let mut prefix = "";
2156-
for (asset_type, val) in amt.components() {
2157-
// Look up the address and epoch of asset to facilitate pretty
2158-
// printing
2159-
let (addr, _denom, digit, epoch, _) = &conversions[asset_type];
2160-
// Now print out this component of the conversion
2147+
2148+
for (addr, _denom, digit, amt) in conversions.values() {
2149+
// If the user has specified any targets, then meet them
2150+
// If we have a sentinel conversion, then skip printing
2151+
if matches!(&target_token, Some(target) if target != addr)
2152+
|| amt.is_zero()
2153+
{
2154+
continue;
2155+
}
2156+
conversions_found = true;
2157+
// Print the asset to which the conversion applies
21612158
display!(
21622159
context.io(),
2163-
"{}{} {}*2^{}[{}]",
2164-
prefix,
2165-
val,
2160+
"{}*2^{}[{}]: ",
21662161
tokens.get(addr).cloned().unwrap_or_else(|| addr.clone()),
21672162
*digit as u8 * 64,
2168-
epoch
2163+
epoch,
21692164
);
2170-
// Future iterations need to be prefixed with +
2171-
prefix = " + ";
2165+
// Now print out the components of the allowed conversion
2166+
let mut prefix = "";
2167+
for (asset_type, val) in amt.components() {
2168+
// Look up the address and epoch of asset to facilitate pretty
2169+
// printing
2170+
let (addr, _denom, digit, _) = &conversions[asset_type];
2171+
// Now print out this component of the conversion
2172+
display!(
2173+
context.io(),
2174+
"{}{} {}*2^{}[{}]",
2175+
prefix,
2176+
val,
2177+
tokens.get(addr).cloned().unwrap_or_else(|| addr.clone()),
2178+
*digit as u8 * 64,
2179+
epoch
2180+
);
2181+
// Future iterations need to be prefixed with +
2182+
prefix = " + ";
2183+
}
2184+
// Allowed conversions are always implicit equations
2185+
display_line!(context.io(), " = 0");
21722186
}
2173-
// Allowed conversions are always implicit equations
2174-
display_line!(context.io(), " = 0");
21752187
}
21762188
if !conversions_found {
21772189
display_line!(

crates/sdk/src/queries/shell.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ type ConversionWithoutPath = (
4242
Address,
4343
Denomination,
4444
MaspDigitPos,
45-
MaspEpoch,
4645
masp_primitives::transaction::components::I128Sum,
4746
);
4847

@@ -98,7 +97,7 @@ router! {SHELL,
9897
( "conv" / [asset_type: AssetType] ) -> Option<Conversion> = read_conversion,
9998

10099
// Conversion state access - read conversion
101-
( "conversions" ) -> BTreeMap<AssetType, ConversionWithoutPath> = read_conversions,
100+
( "conversions" / [masp_epoch: MaspEpoch] ) -> BTreeMap<AssetType, ConversionWithoutPath> = read_conversions,
102101

103102
// Conversion state access - read conversion
104103
( "masp_reward_tokens" ) -> Vec<MaspTokenRewardData> = masp_reward_tokens,
@@ -211,6 +210,7 @@ where
211210
/// Query to read the conversion state
212211
fn read_conversions<D, H, V, T>(
213212
ctx: RequestCtx<'_, D, H, V, T>,
213+
masp_epoch: MaspEpoch,
214214
) -> namada_storage::Result<BTreeMap<AssetType, ConversionWithoutPath>>
215215
where
216216
D: 'static + DB + for<'iter> DBIter<'iter> + Sync,
@@ -222,14 +222,14 @@ where
222222
.conversion_state
223223
.assets
224224
.iter()
225+
.filter(|&(_, asset)| (asset.epoch == masp_epoch))
225226
.map(|(&asset_type, asset)| {
226227
(
227228
asset_type,
228229
(
229230
asset.token.clone(),
230231
asset.denom,
231232
asset.digit_pos,
232-
asset.epoch,
233233
asset.conversion.clone().into(),
234234
),
235235
)

crates/sdk/src/rpc.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,20 +390,22 @@ pub async fn query_conversion<C: namada_io::Client + Sync>(
390390
/// Query conversions
391391
pub async fn query_conversions<C: namada_io::Client + Sync>(
392392
client: &C,
393+
masp_epoch: &MaspEpoch,
393394
) -> Result<
394395
BTreeMap<
395396
AssetType,
396397
(
397398
Address,
398399
Denomination,
399400
MaspDigitPos,
400-
MaspEpoch,
401401
masp_primitives::transaction::components::I128Sum,
402402
),
403403
>,
404404
error::Error,
405405
> {
406-
convert_response::<C, _>(RPC.shell().read_conversions(client).await)
406+
convert_response::<C, _>(
407+
RPC.shell().read_conversions(client, masp_epoch).await,
408+
)
407409
}
408410

409411
/// Query the total rewards minted by MASP

0 commit comments

Comments
 (0)