Skip to content

Commit 005845d

Browse files
committed
Implemented a migration to set token precisions.
1 parent ecc7e81 commit 005845d

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

crates/migrations/src/foreign_types.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ use crate::TypeHash;
1111
derive_typehash!(Vec::<u8>);
1212
derive_typehash!(Vec::<String>);
1313
derive_typehash!(u64);
14+
derive_typehash!(u128);

examples/make-db-migration.rs

+57-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ use std::collections::BTreeMap;
33
use borsh::{BorshDeserialize, BorshSerialize};
44
use namada_macros::BorshDeserializer;
55
use namada_sdk::address::Address;
6+
use namada_sdk::ibc::trace::ibc_token;
67
use namada_sdk::masp_primitives::asset_type::AssetType;
78
use namada_sdk::masp_primitives::merkle_tree::FrozenCommitmentTree;
89
use namada_sdk::masp_primitives::sapling;
910
use namada_sdk::migrations;
1011
use namada_sdk::storage::DbColFam;
12+
use namada_shielded_token::storage_key::masp_reward_precision_key;
1113
use namada_shielded_token::{ConversionLeaf, ConversionState};
1214
use namada_trans_token::storage_key::{balance_key, minted_balance_key};
13-
use namada_trans_token::Amount;
15+
use namada_trans_token::{Amount, Store};
1416

1517
pub const OLD_CONVERSION_STATE_TYPE_HASH: &str =
1618
"05E2FD0BEBD54A05AAE349BBDE61F90893F09A72850EFD4F69060821EC5DE65F";
@@ -38,8 +40,8 @@ impl From<ConversionState> for NewConversionState {
3840
}
3941
}
4042

41-
#[allow(dead_code)]
42-
fn example() {
43+
// Demonstrate how to set the minted balance using a migration
44+
fn minted_balance_migration() {
4345
let person =
4446
Address::decode("tnam1q9rhgyv3ydq0zu3whnftvllqnvhvhm270qxay5tn")
4547
.unwrap();
@@ -68,10 +70,59 @@ fn example() {
6870
let changes = migrations::DbChanges {
6971
changes: updates.into_iter().collect(),
7072
};
71-
std::fs::write("migrations.json", serde_json::to_string(&changes).unwrap())
72-
.unwrap();
73+
std::fs::write(
74+
"minted_balance_migration.json",
75+
serde_json::to_string(&changes).unwrap(),
76+
)
77+
.unwrap();
78+
}
79+
80+
// Demonstrate how to set the shielded reward precision of IBC tokens using a
81+
// migration
82+
fn shielded_reward_precision_migration() {
83+
pub type ChannelId = &'static str;
84+
pub type BaseToken = &'static str;
85+
pub type Precision = u128;
86+
87+
const IBC_TOKENS: [(ChannelId, BaseToken, Precision); 6] = [
88+
("channel-1", "uosmo", 1000u128),
89+
("channel-2", "uatom", 1000u128),
90+
("channel-3", "utia", 1000u128),
91+
("channel-0", "stuosmo", 1000u128),
92+
("channel-0", "stuatom", 1000u128),
93+
("channel-0", "stutia", 1000u128),
94+
];
95+
96+
let mut updates = Vec::new();
97+
// Set IBC token shielded reward precisions
98+
for (channel_id, base_token, precision) in IBC_TOKENS {
99+
let ibc_denom = format!("transfer/{channel_id}/{base_token}");
100+
let token_address = ibc_token(&ibc_denom).clone();
101+
102+
// The key holding the shielded reward precision of current token
103+
let shielded_token_reward_precision_key =
104+
masp_reward_precision_key::<Store<()>>(&token_address);
105+
106+
updates.push(migrations::DbUpdateType::Add {
107+
key: shielded_token_reward_precision_key,
108+
cf: DbColFam::SUBSPACE,
109+
value: precision.into(),
110+
force: false,
111+
});
112+
}
113+
114+
let changes = migrations::DbChanges {
115+
changes: updates.into_iter().collect(),
116+
};
117+
std::fs::write(
118+
"shielded_reward_precision_migration.json",
119+
serde_json::to_string(&changes).unwrap(),
120+
)
121+
.unwrap();
73122
}
74123

124+
// Generate various migrations
75125
fn main() {
76-
example()
126+
minted_balance_migration();
127+
shielded_reward_precision_migration();
77128
}

0 commit comments

Comments
 (0)