Skip to content

Commit e364476

Browse files
committed
turn an AssetMap into JSON
1 parent 75529a7 commit e364476

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/vm/contexts.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,59 @@ pub struct AssetMap {
9191
asset_map: HashMap<PrincipalData, HashMap<AssetIdentifier, Vec<Value>>>,
9292
}
9393

94+
impl AssetMap {
95+
pub fn to_json(&self) -> serde_json::Value {
96+
let stx : serde_json::map::Map<_, _> = self.stx_map
97+
.iter()
98+
.map(|(principal, amount)| (format!("{}", principal), serde_json::value::Value::String(format!("{}", amount))))
99+
.collect();
100+
101+
let burns : serde_json::map::Map<_, _> = self.burn_map
102+
.iter()
103+
.map(|(principal, amount)| (format!("{}", principal), serde_json::value::Value::String(format!("{}", amount))))
104+
.collect();
105+
106+
let tokens : serde_json::map::Map<_, _> = self.token_map
107+
.iter()
108+
.map(|(principal, token_map)| {
109+
let token_json : serde_json::map::Map<_, _> = token_map
110+
.iter()
111+
.map(|(asset_id, amount)| (format!("{}", asset_id), serde_json::value::Value::String(format!("{}", amount))))
112+
.collect();
113+
114+
(format!("{}", principal), serde_json::value::Value::Object(token_json))
115+
})
116+
.collect();
117+
118+
let assets : serde_json::map::Map<_, _> = self.asset_map
119+
.iter()
120+
.map(|(principal, nft_map)| {
121+
let nft_json : serde_json::map::Map<_, _> = nft_map
122+
.iter()
123+
.map(|(asset_id, nft_values)| {
124+
let nft_array = nft_values
125+
.iter()
126+
.map(|nft_value| serde_json::value::Value::String(format!("{}", nft_value)))
127+
.collect();
128+
129+
(format!("{}", asset_id), serde_json::value::Value::Array(nft_array))
130+
})
131+
.collect();
132+
133+
(format!("{}", principal), serde_json::value::Value::Object(nft_json))
134+
})
135+
.collect();
136+
137+
json!({
138+
"stx": stx,
139+
"burns": burns,
140+
"tokens": tokens,
141+
"assets": assets
142+
})
143+
}
144+
}
145+
146+
94147
#[derive(Debug, Clone)]
95148
pub struct EventBatch {
96149
pub events: Vec<StacksTransactionEvent>,
@@ -640,6 +693,10 @@ impl<'a> OwnedEnvironment<'a> {
640693
Ok((asset_map, event_batch))
641694
}
642695

696+
pub fn get_cost_total(&self) -> ExecutionCost {
697+
self.context.cost_track.get_total()
698+
}
699+
643700
/// Destroys this environment, returning ownership of its database reference.
644701
/// If the context wasn't top-level (i.e., it had uncommitted data), return None,
645702
/// because the database is not guaranteed to be in a sane state.

0 commit comments

Comments
 (0)